Add a resource manager for BGSM files

pull/3235/head
Alexei Kotov 9 months ago
parent cb77bcc4c8
commit fe1cb3a5ae

@ -129,7 +129,7 @@ add_component_dir (vfs
add_component_dir (resource add_component_dir (resource
scenemanager keyframemanager imagemanager bulletshapemanager bulletshape niffilemanager objectcache multiobjectcache resourcesystem scenemanager keyframemanager imagemanager bulletshapemanager bulletshape niffilemanager objectcache multiobjectcache resourcesystem
resourcemanager stats animation foreachbulletobject errormarker cachestats resourcemanager stats animation foreachbulletobject errormarker cachestats bgsmfilemanager
) )
add_component_dir (shader add_component_dir (shader

@ -20,6 +20,8 @@ namespace Bgsm
void parse(Files::IStreamPtr&& stream); void parse(Files::IStreamPtr&& stream);
std::uint32_t getVersion() const { return mFile->mVersion; } std::uint32_t getVersion() const { return mFile->mVersion; }
std::unique_ptr<MaterialFile>& getFile() { return mFile; }
}; };
} }
#endif #endif

@ -0,0 +1,62 @@
#include "bgsmfilemanager.hpp"
#include <iostream>
#include <osg/Object>
#include <components/bgsm/reader.hpp>
#include <components/vfs/manager.hpp>
#include "objectcache.hpp"
namespace Resource
{
class BgsmFileHolder : public osg::Object
{
public:
BgsmFileHolder(const Bgsm::MaterialFilePtr& file)
: mBgsmFile(file)
{
}
BgsmFileHolder(const BgsmFileHolder& copy, const osg::CopyOp& copyop)
: mBgsmFile(copy.mBgsmFile)
{
}
BgsmFileHolder() = default;
META_Object(Resource, BgsmFileHolder)
Bgsm::MaterialFilePtr mBgsmFile;
};
BgsmFileManager::BgsmFileManager(const VFS::Manager* vfs, double expiryDelay)
: ResourceManager(vfs, expiryDelay)
{
}
BgsmFileManager::~BgsmFileManager() = default;
Bgsm::MaterialFilePtr BgsmFileManager::get(VFS::Path::NormalizedView name)
{
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(name);
if (obj)
return static_cast<BgsmFileHolder*>(obj.get())->mBgsmFile;
else
{
Bgsm::Reader reader;
reader.parse(mVFS->get(name));
Bgsm::MaterialFilePtr file = std::move(reader.getFile());
obj = new BgsmFileHolder(file);
mCache->addEntryToObjectCache(name.value(), obj);
return file;
}
}
void BgsmFileManager::reportStats(unsigned int frameNumber, osg::Stats* stats) const
{
Resource::reportStats("BSShader Material", frameNumber, mCache->getStats(), *stats);
}
}

@ -0,0 +1,27 @@
#ifndef OPENMW_COMPONENTS_RESOURCE_BGSMFILEMANAGER_H
#define OPENMW_COMPONENTS_RESOURCE_BGSMFILEMANAGER_H
#include <components/bgsm/file.hpp>
#include "resourcemanager.hpp"
namespace Resource
{
/// @brief Handles caching of material files.
/// @note May be used from any thread.
class BgsmFileManager : public ResourceManager
{
public:
BgsmFileManager(const VFS::Manager* vfs, double expiryDelay);
~BgsmFileManager();
/// Retrieve a material file from the cache or load it from the VFS if not cached yet.
Bgsm::MaterialFilePtr get(VFS::Path::NormalizedView name);
void reportStats(unsigned int frameNumber, osg::Stats* stats) const override;
};
}
#endif

@ -87,6 +87,7 @@ namespace Resource
"Image", "Image",
"Nif", "Nif",
"Keyframe", "Keyframe",
"BSShader Material",
"Groundcover Chunk", "Groundcover Chunk",
"Object Chunk", "Object Chunk",
"Terrain Chunk", "Terrain Chunk",

Loading…
Cancel
Save