#include "animblendrulesmanager.hpp" #include #include #include #include #include #include #include #include #include #include #include #include "objectcache.hpp" #include "scenemanager.hpp" namespace Resource { using AnimBlendRules = SceneUtil::AnimBlendRules; AnimBlendRulesManager::AnimBlendRulesManager(const VFS::Manager* vfs, double expiryDelay) : ResourceManager(vfs, expiryDelay) , mVfs(vfs) { } osg::ref_ptr AnimBlendRulesManager::getRules( const VFS::Path::NormalizedView path, const VFS::Path::NormalizedView overridePath) { // Note: Providing a non-existing path but an existing overridePath is not supported! auto tmpl = loadRules(path); if (!tmpl) return nullptr; // Create an instance based on template and store template reference inside so the template will not be removed // from cache osg::ref_ptr blendRules(new AnimBlendRules(*tmpl, osg::CopyOp::SHALLOW_COPY)); blendRules->getOrCreateUserDataContainer()->addUserObject(new Resource::TemplateRef(tmpl)); if (!overridePath.value().empty()) { auto blendRuleOverrides = loadRules(overridePath); if (blendRuleOverrides) { blendRules->addOverrideRules(*blendRuleOverrides); } blendRules->getOrCreateUserDataContainer()->addUserObject(new Resource::TemplateRef(blendRuleOverrides)); } return blendRules; } osg::ref_ptr AnimBlendRulesManager::loadRules(VFS::Path::NormalizedView path) { const std::string normalized = VFS::Path::normalizeFilename(path.value()); std::optional> obj = mCache->getRefFromObjectCacheOrNone(normalized); if (obj.has_value()) { return osg::ref_ptr(static_cast(obj->get())); } else { osg::ref_ptr blendRules = AnimBlendRules::fromFile(mVfs, path); if (blendRules) { // Blend rules were found in VFS, cache them. mCache->addEntryToObjectCache(normalized, blendRules); return blendRules; } } // No blend rules were found in VFS, cache a nullptr. osg::ref_ptr nullRules = nullptr; mCache->addEntryToObjectCache(normalized, nullRules); return nullRules; } void AnimBlendRulesManager::reportStats(unsigned int frameNumber, osg::Stats* stats) const { Resource::reportStats("Blending Rules", frameNumber, mCache->getStats(), *stats); } }