Add NifFileManager to avoid duplicate parsing of the NIFFile in SceneManager and BulletShapeManager.
parent
84305a1297
commit
3d12b2ca9d
@ -0,0 +1,64 @@
|
||||
#include "niffilemanager.hpp"
|
||||
|
||||
#include <osgDB/ObjectCache>
|
||||
#include <components/vfs/manager.hpp>
|
||||
|
||||
#include <osg/Timer>
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
|
||||
class NifFileHolder : public osg::Object
|
||||
{
|
||||
public:
|
||||
NifFileHolder(const Nif::NIFFilePtr& file)
|
||||
: mNifFile(file)
|
||||
{
|
||||
}
|
||||
NifFileHolder(const NifFileHolder& copy, const osg::CopyOp& copyop)
|
||||
: mNifFile(copy.mNifFile)
|
||||
{
|
||||
}
|
||||
|
||||
NifFileHolder()
|
||||
{
|
||||
}
|
||||
|
||||
META_Object(Resource, NifFileHolder)
|
||||
|
||||
Nif::NIFFilePtr mNifFile;
|
||||
};
|
||||
|
||||
NifFileManager::NifFileManager(const VFS::Manager *vfs)
|
||||
: mVFS(vfs)
|
||||
{
|
||||
mCache = new osgDB::ObjectCache;
|
||||
}
|
||||
|
||||
NifFileManager::~NifFileManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void NifFileManager::clearCache()
|
||||
{
|
||||
// NIF files aren't needed any more when the converted objects are cached in SceneManager / BulletShapeManager,
|
||||
// so we'll simply drop all nif files here, unlikely to need them again
|
||||
mCache->clear();
|
||||
}
|
||||
|
||||
Nif::NIFFilePtr NifFileManager::get(const std::string &name)
|
||||
{
|
||||
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(name);
|
||||
if (obj)
|
||||
return static_cast<NifFileHolder*>(obj.get())->mNifFile;
|
||||
else
|
||||
{
|
||||
Nif::NIFFilePtr file (new Nif::NIFFile(mVFS->get(name), name));
|
||||
obj = new NifFileHolder(file);
|
||||
mCache->addEntryToObjectCache(name, obj);
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
#ifndef OPENMW_COMPONENTS_RESOURCE_NIFFILEMANAGER_H
|
||||
#define OPENMW_COMPONENTS_RESOURCE_NIFFILEMANAGER_H
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
|
||||
#include <components/nif/niffile.hpp>
|
||||
|
||||
namespace VFS
|
||||
{
|
||||
class Manager;
|
||||
}
|
||||
|
||||
namespace osgDB
|
||||
{
|
||||
class ObjectCache;
|
||||
}
|
||||
|
||||
namespace Resource
|
||||
{
|
||||
|
||||
/// @brief Handles caching of NIFFiles.
|
||||
/// @note The NifFileManager is completely thread safe.
|
||||
class NifFileManager
|
||||
{
|
||||
public:
|
||||
NifFileManager(const VFS::Manager* vfs);
|
||||
~NifFileManager();
|
||||
|
||||
void clearCache();
|
||||
|
||||
/// Retrieve a NIF file from the cache, or load it from the VFS if not cached yet.
|
||||
/// @note For performance reasons the NifFileManager does not handle case folding, needs
|
||||
/// to be done in advance by other managers accessing the NifFileManager.
|
||||
Nif::NIFFilePtr get(const std::string& name);
|
||||
|
||||
private:
|
||||
// Use the osgDB::ObjectCache so objects are retrieved in thread safe way
|
||||
osg::ref_ptr<osgDB::ObjectCache> mCache;
|
||||
|
||||
const VFS::Manager* mVFS;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue