<commit_before>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2014 Tor<NAME> Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgreStableHeaders.h"
#include "OgreResourceManager.h"

#include "OgreException.h"
#include "OgreResourceGroupManager.h"

namespace Ogre {

    //-----------------------------------------------------------------------
    ResourceManager::ResourceManager()
        : mNextHandle(1), mMemoryUsage(0), mVerbose(true), mLoadOrder(0)
    {
        // Init memory limit & usage
        mMemoryBudget = std::numeric_limits<unsigned long>::max();
    }
    //-----------------------------------------------------------------------
    ResourceManager::~ResourceManager()
    {
        destroyAllResourcePools();
        removeAll();
    }
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::createResource(const String& name, const String& group,
        bool isManual, ManualResourceLoader* loader, const NameValuePairList* params)
    {
        // Call creation implementation
        ResourcePtr ret = ResourcePtr(
            createImpl(name, getNextHandle(), group, isManual, loader, params));
        if (params)
            ret->setParameterList(*params);

        addImpl(ret);
        // Tell resource group manager
        ResourceGroupManager::getSingleton()._notifyResourceCreated(ret);
        return ret;

    }
    //-----------------------------------------------------------------------
    ResourceManager::ResourceCreateOrRetrieveResult 
    ResourceManager::createOrRetrieve(
        const String& name, const String& group, 
        bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* params)
    {
        // Lock for the whole get / insert
            OGRE_LOCK_AUTO_MUTEX;

        ResourcePtr res = getResourceByName(name, group);
        bool created = false;
        if (!res)
        {
            created = true;
            res = createResource(name, group, isManual, loader, params);
        }

        return ResourceCreateOrRetrieveResult(res, created);
    }
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::prepare(const String& name, 
        const String& group, bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* loadParams, bool backgroundThread)
    {
        ResourcePtr r = createOrRetrieve(name,group,isManual,loader,loadParams).first;
        // ensure prepared
        r->prepare(backgroundThread);
        return r;
    }
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::load(const String& name, 
        const String& group, bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* loadParams, bool backgroundThread)
    {
        ResourcePtr r = createOrRetrieve(name,group,isManual,loader,loadParams).first;
        // ensure loaded
        r->load(backgroundThread);

        return r;
    }
    //-----------------------------------------------------------------------
    void ResourceManager::addImpl( ResourcePtr& res )
    {
            OGRE_LOCK_AUTO_MUTEX;

            std::pair<ResourceMap::iterator, bool> result;
        if(ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(res->getGroup()))
        {
            result = mResources.insert( ResourceMap::value_type( res->getName(), res ) );
        }
        else
        {
            ResourceWithGroupMap::iterator itGroup = mResourcesWithGroup.find(res->getGroup());

            // we will create the group if it doesn't exists in our list
            if( itGroup == mResourcesWithGroup.end())
            {
                ResourceMap dummy;
                mResourcesWithGroup.insert( ResourceWithGroupMap::value_type( res->getGroup(), dummy ) );
                itGroup = mResourcesWithGroup.find(res->getGroup());
            }
            result = itGroup->second.insert( ResourceMap::value_type( res->getName(), res ) );

        }

        // Attempt to resolve the collision
        ResourceLoadingListener* listener = ResourceGroupManager::getSingleton().getLoadingListener();
        if (!result.second && listener && listener->resourceCollision(res.get(), this))
        {
            // Try to do the addition again, no seconds attempts to resolve collisions are allowed
            if(ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(res->getGroup()))
            {
                result = mResources.insert( ResourceMap::value_type( res->getName(), res ) );
            }
            else
            {
                ResourceWithGroupMap::iterator itGroup = mResourcesWithGroup.find(res->getGroup());
                result = itGroup->second.insert( ResourceMap::value_type( res->getName(), res ) );
            }
        }

        if (!result.second)
        {
            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the name " + res->getName() +
                " already exists.", "ResourceManager::add");
        }

        // Insert the handle
        std::pair<ResourceHandleMap::iterator, bool> resultHandle =
            mResourcesByHandle.insert( ResourceHandleMap::value_type( res->getHandle(), res ) );
        if (!resultHandle.second)
        {
            OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the handle " +
                StringConverter::toString((long) (res->getHandle())) +
                " already exists.", "ResourceManager::add");
        }
    }
    //-----------------------------------------------------------------------
    void ResourceManager::removeImpl(const ResourcePtr& res )
    {
        OgreAssert(res, "attempting to remove nullptr");

        OGRE_LOCK_AUTO_MUTEX;

        if(ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(res->getGroup()))<commit_msg>Backed out changeset: 43c657731fe4 (was lost the ability to resolve conflict by preferring existing resource)<commit_after>/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2014 <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#include "OgreStableHeaders.h"
#include "OgreResourceManager.h"

#include "OgreException.h"
#include "OgreResourceGroupManager.h"

namespace Ogre {

    //-----------------------------------------------------------------------
    ResourceManager::ResourceManager()
        : mNextHandle(1), mMemoryUsage(0), mVerbose(true), mLoadOrder(0)
    {
        // Init memory limit & usage
        mMemoryBudget = std::numeric_limits<unsigned long>::max();
    }
    //-----------------------------------------------------------------------
    ResourceManager::~ResourceManager()
    {
        destroyAllResourcePools();
        removeAll();
    }
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::createResource(const String& name, const String& group,
        bool isManual, ManualResourceLoader* loader, const NameValuePairList* params)
    {
        // Call creation implementation
        ResourcePtr ret = ResourcePtr(
            createImpl(name, getNextHandle(), group, isManual, loader, params));
        if (params)
            ret->setParameterList(*params);

        addImpl(ret);
        // Tell resource group manager
        ResourceGroupManager::getSingleton()._notifyResourceCreated(ret);
        return ret;

    }
    //-----------------------------------------------------------------------
    ResourceManager::ResourceCreateOrRetrieveResult 
    ResourceManager::createOrRetrieve(
        const String& name, const String& group, 
        bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* params)
    {
        // Lock for the whole get / insert
            OGRE_LOCK_AUTO_MUTEX;

        ResourcePtr res = getResourceByName(name, group);
        bool created = false;
        if (!res)
        {
            created = true;
            res = createResource(name, group, isManual, loader, params);
        }

        return ResourceCreateOrRetrieveResult(res, created);
    }
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::prepare(const String& name, 
        const String& group, bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* loadParams, bool backgroundThread)
    {
        ResourcePtr r = createOrRetrieve(name,group,isManual,loader,loadParams).first;
        // ensure prepared
        r->prepare(backgroundThread);
        return r;
    }
    //-----------------------------------------------------------------------
    ResourcePtr ResourceManager::load(const String& name, 
        const String& group, bool isManual, ManualResourceLoader* loader, 
        const NameValuePairList* loadParams, bool backgroundThread)
    {
        ResourcePtr r = createOrRetrieve(name,group,isManual,loader,loadParams).first;
        // ensure loaded
        r->load(backgroundThread);

        return r;
    }
    //-----------------------------------------------------------------------
    void ResourceManager::addImpl( ResourcePtr& res )
    {
            OGRE_LOCK_AUTO_MUTEX;

            std::pair<ResourceMap::iterator, bool> result;
        if(ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(res->getGroup()))
        {
            result = mResources.insert( ResourceMap::value_type( res->getName(), res ) );
        }
        else
        {
            ResourceWithGroupMap::iterator itGroup = mResourcesWithGroup.find(res->getGroup());

            // we will create the group if it doesn't exists in our list
            if( itGroup == mResourcesWithGroup.end())
            {
                ResourceMap dummy;
                mResourcesWithGroup.insert( ResourceWithGroupMap::value_type( res->getGroup(), dummy ) );
                itGroup = mResourcesWithGroup.find(res->getGroup());
            }
            result = itGroup->second.insert( ResourceMap::value_type( res->getName(), res ) );

        }

        if (!result.second)
        {
            // Attempt to resolve the collision
            if(ResourceGroupManager::getSingleton().getLoadingListener())
            {
                if(ResourceGroupManager::getSingleton().getLoadingListener()->resourceCollision(res.get(), this))
                {
                    // Try to do the addition again, no seconds attempts to resolve collisions are allowed
                    std::pair<ResourceMap::iterator, bool> insertResult;
                    if(ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(res->getGroup()))
                    {
                        insertResult = mResources.insert( ResourceMap::value_type( res->getName(), res ) );
                    }
                    else
                    {
                        ResourceWithGroupMap::iterator itGroup = mResourcesWithGroup.find(res->getGroup());
                        insertResult = itGroup->second.insert( ResourceMap::value_type( res->getName(), res ) );
                    }
                    if (!insertResult.second)
                    {
                        OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the name " + res->getName() + 
                            " already exists.", "ResourceManager::add");
                    }

                    std::pair<ResourceHandleMap::iterator, bool> resultHandle = 
                        mResourcesByHandle.insert( ResourceHandleMap::value_type( res->getHandle(), res ) );
                    if (!resultHandle.second)
                    {
                        OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the handle " + 
                            StringConverter::toString((long) (res->getHandle())) + 
                            " already exists.", "ResourceManager::add");
                    }
                }
            }
            else
            {
                OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the name " + res->getName() +
                    " already exists.", "ResourceManager::add");
            }
        }
        else
        {
            // Insert the handle
            std::pair<ResourceHandleMap::iterator, bool> resultHandle = 
                mResourcesByHandle.insert( ResourceHandleMap::value_type( res->getHandle(), res ) );
            if (!resultHandle.second)
            {
                OGRE_EXCEPT(Exception::ERR_DUPLICATE_ITEM, "Resource with the handle " + 
                    StringConverter::toString((long) (res->getHandle())) + 
                    " already exists.", "ResourceManager::add");
            }
        }
    }
    //-----------------------------------------------------------------------
    void ResourceManager::removeImpl(const ResourcePtr& res )
    {
        OgreAssert(res, "attempting to remove nullptr");

        OGRE_LOCK_AUTO_MUTEX;

        if(ResourceGroupManager::getSingleton().isResourceGroupInGlobalPool(res->getGroup()))
{'fraction_non_alphanumeric': 0.1777519979376128, 'fraction_numerical': 0.0021268368136117557, 'mean_word_length': 1.8212727272727274, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2997986', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3505, 'n_words': 980}