From 40fc09772290dc16ab5026bb8d42b499a0e59a1b Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 19 Mar 2015 17:49:41 +0100 Subject: [PATCH] OpenCS: use the new VFS, restored resource tables --- apps/opencs/editor.cpp | 22 ++----- apps/opencs/editor.hpp | 8 +++ apps/opencs/model/doc/documentmanager.cpp | 12 ++-- apps/opencs/model/doc/documentmanager.hpp | 11 +++- apps/opencs/model/world/resources.cpp | 64 ++++++++------------ apps/opencs/model/world/resources.hpp | 7 ++- apps/opencs/model/world/resourcesmanager.cpp | 18 +++--- apps/opencs/model/world/resourcesmanager.hpp | 10 ++- components/CMakeLists.txt | 2 +- components/vfs/manager.cpp | 5 ++ components/vfs/manager.hpp | 3 + components/vfs/registerarchives.cpp | 42 +++++++++++++ components/vfs/registerarchives.hpp | 15 +++++ 13 files changed, 143 insertions(+), 76 deletions(-) create mode 100644 components/vfs/registerarchives.cpp create mode 100644 components/vfs/registerarchives.hpp diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 92fd40492..3c4ce6868 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -1,21 +1,13 @@ #include "editor.hpp" -#include - #include #include #include #include -#include -#include - -#include -#include - -#include -#include +#include +#include #include "model/doc/document.hpp" #include "model/world/data.hpp" @@ -34,10 +26,11 @@ CS::Editor::Editor () //NifOgre::Loader::setShowMarkers(true); - //Bsa::registerResources (Files::Collections (config.first, !mFsStrict), config.second, true, - // mFsStrict); + mVFS.reset(new VFS::Manager(mFsStrict)); - mDocumentManager.listResources(); + VFS::registerArchives(mVFS.get(), Files::Collections(config.first, !mFsStrict), config.second, true); + + mDocumentManager.setVFS(mVFS.get()); mNewGame.setLocalData (mLocal); mFileDialog.setLocalData (mLocal); @@ -74,9 +67,6 @@ CS::Editor::~Editor () if(mServer && boost::filesystem::exists(mPid)) static_cast ( // silence coverity warning remove(mPid.string().c_str())); // ignore any error - - // cleanup global resources used by OEngine - delete OEngine::Physic::BulletShapeManager::getSingletonPtr(); } void CS::Editor::setupDataFiles (const Files::PathContainer& dataDirs) diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index da4d63985..e973b9c55 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -27,12 +27,20 @@ #include "view/settings/dialog.hpp" +namespace VFS +{ + class Manager; +} + namespace CS { class Editor : public QObject { Q_OBJECT + // FIXME: should be moved to document, so we can have different resources for each opened project + std::auto_ptr mVFS; + Files::ConfigurationManager mCfgMgr; CSMSettings::UserSettings mUserSettings; CSMDoc::DocumentManager mDocumentManager; diff --git a/apps/opencs/model/doc/documentmanager.cpp b/apps/opencs/model/doc/documentmanager.cpp index 9b807225c..ecff4bbed 100644 --- a/apps/opencs/model/doc/documentmanager.cpp +++ b/apps/opencs/model/doc/documentmanager.cpp @@ -90,11 +90,6 @@ void CSMDoc::DocumentManager::setBlacklistedScripts (const std::vector mBlacklistedScripts; + VFS::Manager* mVFS; DocumentManager (const DocumentManager&); DocumentManager& operator= (const DocumentManager&); @@ -56,8 +62,7 @@ namespace CSMDoc void setBlacklistedScripts (const std::vector& scriptIds); - /// Ask OGRE for a list of available resources. - void listResources(); + void setVFS(const VFS::Manager* vfs); private: @@ -99,4 +104,4 @@ namespace CSMDoc }; } -#endif \ No newline at end of file +#endif diff --git a/apps/opencs/model/world/resources.cpp b/apps/opencs/model/world/resources.cpp index 25fada93a..158793173 100644 --- a/apps/opencs/model/world/resources.cpp +++ b/apps/opencs/model/world/resources.cpp @@ -7,62 +7,50 @@ #include +#include + #include -CSMWorld::Resources::Resources (const std::string& baseDirectory, UniversalId::Type type, +CSMWorld::Resources::Resources (const VFS::Manager* vfs, const std::string& baseDirectory, UniversalId::Type type, const char * const *extensions) : mBaseDirectory (baseDirectory), mType (type) { int baseSize = mBaseDirectory.size(); - /* - Ogre::StringVector resourcesGroups = - Ogre::ResourceGroupManager::getSingleton().getResourceGroups(); - - for (Ogre::StringVector::iterator iter (resourcesGroups.begin()); - iter!=resourcesGroups.end(); ++iter) + const std::map& index = vfs->getIndex(); + for (std::map::const_iterator it = index.begin(); it != index.end(); ++it) { - if (*iter=="General" || *iter=="Internal" || *iter=="Autodetect") + std::string filepath = it->first; + if (static_cast (filepath.size())begin()); - iter!=resources->end(); ++iter) + if (extensions) { - if (static_cast (iter->size())substr (0, baseSize)!=mBaseDirectory || - ((*iter)[baseSize]!='/' && (*iter)[baseSize]!='\\')) - continue; - - if (extensions) - { - std::string::size_type index = iter->find_last_of ('.'); + std::string::size_type index = filepath.find_last_of ('.'); - if (index==std::string::npos) - continue; - - std::string extension = iter->substr (index+1); + if (index==std::string::npos) + continue; - int i = 0; + std::string extension = filepath.substr (index+1); - for (; extensions[i]; ++i) - if (extensions[i]==extension) - break; + int i = 0; - if (!extensions[i]) - continue; - } + for (; extensions[i]; ++i) + if (extensions[i]==extension) + break; - std::string file = iter->substr (baseSize+1); - mFiles.push_back (file); - std::replace (file.begin(), file.end(), '\\', '/'); - mIndex.insert (std::make_pair ( - Misc::StringUtils::lowerCase (file), static_cast (mFiles.size())-1)); + if (!extensions[i]) + continue; } + + std::string file = filepath.substr (baseSize+1); + mFiles.push_back (file); + std::replace (file.begin(), file.end(), '\\', '/'); + mIndex.insert (std::make_pair ( + Misc::StringUtils::lowerCase (file), static_cast (mFiles.size())-1)); } - */ } int CSMWorld::Resources::getSize() const diff --git a/apps/opencs/model/world/resources.hpp b/apps/opencs/model/world/resources.hpp index 9c1c76b6f..d6998da9f 100644 --- a/apps/opencs/model/world/resources.hpp +++ b/apps/opencs/model/world/resources.hpp @@ -7,6 +7,11 @@ #include "universalid.hpp" +namespace VFS +{ + class Manager; +} + namespace CSMWorld { class Resources @@ -19,7 +24,7 @@ namespace CSMWorld public: /// \param type Type of resources in this table. - Resources (const std::string& baseDirectory, UniversalId::Type type, + Resources (const VFS::Manager* vfs, const std::string& baseDirectory, UniversalId::Type type, const char * const *extensions = 0); int getSize() const; diff --git a/apps/opencs/model/world/resourcesmanager.cpp b/apps/opencs/model/world/resourcesmanager.cpp index 50014f4b5..218937924 100644 --- a/apps/opencs/model/world/resourcesmanager.cpp +++ b/apps/opencs/model/world/resourcesmanager.cpp @@ -10,16 +10,18 @@ void CSMWorld::ResourcesManager::addResources (const Resources& resources) resources)); } -void CSMWorld::ResourcesManager::listResources() +void CSMWorld::ResourcesManager::setVFS(const VFS::Manager *vfs) { + mResources.clear(); + static const char * const sMeshTypes[] = { "nif", 0 }; - addResources (Resources ("meshes", UniversalId::Type_Mesh, sMeshTypes)); - addResources (Resources ("icons", UniversalId::Type_Icon)); - addResources (Resources ("music", UniversalId::Type_Music)); - addResources (Resources ("sound", UniversalId::Type_SoundRes)); - addResources (Resources ("textures", UniversalId::Type_Texture)); - addResources (Resources ("videos", UniversalId::Type_Video)); + addResources (Resources (vfs, "meshes", UniversalId::Type_Mesh, sMeshTypes)); + addResources (Resources (vfs, "icons", UniversalId::Type_Icon)); + addResources (Resources (vfs, "music", UniversalId::Type_Music)); + addResources (Resources (vfs, "sound", UniversalId::Type_SoundRes)); + addResources (Resources (vfs, "textures", UniversalId::Type_Texture)); + addResources (Resources (vfs, "videos", UniversalId::Type_Video)); } const CSMWorld::Resources& CSMWorld::ResourcesManager::get (UniversalId::Type type) const @@ -30,4 +32,4 @@ const CSMWorld::Resources& CSMWorld::ResourcesManager::get (UniversalId::Type ty throw std::logic_error ("Unknown resource type"); return iter->second; -} \ No newline at end of file +} diff --git a/apps/opencs/model/world/resourcesmanager.hpp b/apps/opencs/model/world/resourcesmanager.hpp index 77f210c47..ee939389f 100644 --- a/apps/opencs/model/world/resourcesmanager.hpp +++ b/apps/opencs/model/world/resourcesmanager.hpp @@ -6,6 +6,11 @@ #include "universalid.hpp" #include "resources.hpp" +namespace VFS +{ + class Manager; +} + namespace CSMWorld { class ResourcesManager @@ -18,11 +23,10 @@ namespace CSMWorld public: - /// Ask OGRE for a list of available resources. - void listResources(); + void setVFS(const VFS::Manager* vfs); const Resources& get (UniversalId::Type type) const; }; } -#endif \ No newline at end of file +#endif diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index 311b98039..a78f164a6 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -35,7 +35,7 @@ add_component_dir (bsa ) add_component_dir (vfs - manager archive bsaarchive filesystemarchive + manager archive bsaarchive filesystemarchive registerarchives ) add_component_dir (nif diff --git a/components/vfs/manager.cpp b/components/vfs/manager.cpp index 82f4cd7be..d0e0cf586 100644 --- a/components/vfs/manager.cpp +++ b/components/vfs/manager.cpp @@ -73,4 +73,9 @@ namespace VFS return mIndex.find(normalized) != mIndex.end(); } + const std::map& Manager::getIndex() const + { + return mIndex; + } + } diff --git a/components/vfs/manager.hpp b/components/vfs/manager.hpp index 31538cc99..ebbec7d15 100644 --- a/components/vfs/manager.hpp +++ b/components/vfs/manager.hpp @@ -35,6 +35,9 @@ namespace VFS /// Does a file with this name exist? bool exists(const std::string& name) const; + /// Get a complete list of files from all archives + const std::map& getIndex() const; + /// Retrieve a file by name. /// @note Throws an exception if the file can not be found. Files::IStreamPtr get(const std::string& name) const; diff --git a/components/vfs/registerarchives.cpp b/components/vfs/registerarchives.cpp new file mode 100644 index 000000000..cd077356f --- /dev/null +++ b/components/vfs/registerarchives.cpp @@ -0,0 +1,42 @@ +#include "registerarchives.hpp" + +#include +#include +#include + +namespace VFS +{ + + void registerArchives(VFS::Manager *vfs, const Files::Collections &collections, const std::vector &archives, bool useLooseFiles) + { + const Files::PathContainer& dataDirs = collections.getPaths(); + + if (useLooseFiles) + for (Files::PathContainer::const_iterator iter = dataDirs.begin(); iter != dataDirs.end(); ++iter) + { + // Last data dir has the highest priority + vfs->addArchive(new FileSystemArchive(iter->string())); + } + + for (std::vector::const_iterator archive = archives.begin(); archive != archives.end(); ++archive) + { + if (collections.doesExist(*archive)) + { + // Last BSA has the highest priority + const std::string archivePath = collections.getPath(*archive).string(); + std::cout << "Adding BSA archive " << archivePath << std::endl; + + vfs->addArchive(new BsaArchive(archivePath)); + } + else + { + std::stringstream message; + message << "Archive '" << *archive << "' not found"; + throw std::runtime_error(message.str()); + } + } + + vfs->buildIndex(); + } + +} diff --git a/components/vfs/registerarchives.hpp b/components/vfs/registerarchives.hpp new file mode 100644 index 000000000..1ef13f0f9 --- /dev/null +++ b/components/vfs/registerarchives.hpp @@ -0,0 +1,15 @@ +#ifndef OPENMW_COMPONENTS_VFS_REGISTER_ARCHIVES_H +#define OPENMW_COMPONENTS_VFS_REGISTER_ARCHIVES_H + +#include + +namespace VFS +{ + class Manager; + + /// @brief Register BSA and file system archives based on the given OpenMW configuration. + void registerArchives (VFS::Manager* vfs, const Files::Collections& collections, + const std::vector& archives, bool useLooseFiles); +} + +#endif