| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- #include "CmResourceManifest.h"
- #include "CmResourceManifestRTTI.h"
- #include "CmPath.h"
- #include "CmFileSerializer.h"
- #include "CmException.h"
- namespace CamelotFramework
- {
- ResourceManifest::ResourceManifest(const ConstructPrivately& dummy)
- {
- }
- ResourceManifest::ResourceManifest(const String& name)
- :mName(name)
- {
- }
- ResourceManifestPtr ResourceManifest::create(const String& name)
- {
- return cm_shared_ptr<ResourceManifest>(name);
- }
- ResourceManifestPtr ResourceManifest::createEmpty()
- {
- return cm_shared_ptr<ResourceManifest>(ConstructPrivately());
- }
- void ResourceManifest::registerResource(const String& uuid, const WString& filePath)
- {
- auto iterFind = mUUIDToFilePath.find(uuid);
- WString standardizedFilePath = Path::standardizePath(filePath);
- StringUtil::toLowerCase(standardizedFilePath);
- if(iterFind != mUUIDToFilePath.end())
- {
- if(iterFind->second != standardizedFilePath)
- {
- mFilePathToUUID.erase(iterFind->second);
- mUUIDToFilePath[uuid] = standardizedFilePath;
- mFilePathToUUID[standardizedFilePath] = uuid;
- }
- }
- else
- {
- mUUIDToFilePath[uuid] = standardizedFilePath;
- mFilePathToUUID[standardizedFilePath] = uuid;
- }
- }
- void ResourceManifest::unregisterResource(const String& uuid)
- {
- auto iterFind = mUUIDToFilePath.find(uuid);
- if(iterFind != mUUIDToFilePath.end())
- {
- mFilePathToUUID.erase(iterFind->second);
- mUUIDToFilePath.erase(uuid);
- }
- }
- bool ResourceManifest::uuidToFilePath(const String& uuid, WString& filePath) const
- {
- auto iterFind = mUUIDToFilePath.find(uuid);
- if(iterFind != mUUIDToFilePath.end())
- {
- filePath = iterFind->second;
- return true;
- }
- else
- {
- filePath = StringUtil::WBLANK;
- return false;
- }
- }
- bool ResourceManifest::filePathToUUID(const WString& filePath, String& outUUID) const
- {
- WString standardizedFilePath = Path::standardizePath(filePath);
- StringUtil::toLowerCase(standardizedFilePath);
- auto iterFind = mFilePathToUUID.find(standardizedFilePath);
- if(iterFind != mFilePathToUUID.end())
- {
- outUUID = iterFind->second;
- return true;
- }
- else
- {
- outUUID = StringUtil::BLANK;
- return false;
- }
- }
- bool ResourceManifest::uuidExists(const String& uuid) const
- {
- auto iterFind = mUUIDToFilePath.find(uuid);
- return iterFind != mUUIDToFilePath.end();
- }
- bool ResourceManifest::filePathExists(const WString& filePath) const
- {
- WString standardizedFilePath = Path::standardizePath(filePath);
- StringUtil::toLowerCase(standardizedFilePath);
- auto iterFind = mFilePathToUUID.find(standardizedFilePath);
- return iterFind != mFilePathToUUID.end();
- }
- void ResourceManifest::save(const ResourceManifestPtr& manifest, const WString& path, const WString& relativePath)
- {
- WString standRelativePath = Path::standardizePath(relativePath);
- StringUtil::toLowerCase(standRelativePath);
- ResourceManifestPtr copy = create(manifest->mName);
- for(auto& elem : manifest->mFilePathToUUID)
- {
- if(!Path::includes(elem.first, standRelativePath))
- {
- CM_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
- toString(relativePath) + "\". Path: \"" + toString(elem.first) + "\"");
- }
- WString relativePath = Path::relative(standRelativePath, elem.first);
- copy->mFilePathToUUID[relativePath] = elem.second;
- }
- for(auto& elem : manifest->mUUIDToFilePath)
- {
- if(!Path::includes(elem.second, standRelativePath))
- {
- CM_EXCEPT(InvalidStateException, "Path in resource manifest cannot be made relative to: \"" +
- toString(relativePath) + "\". Path: \"" + toString(elem.second) + "\"");
- }
- WString relativePath = Path::relative(standRelativePath, elem.second);
- copy->mUUIDToFilePath[elem.first] = relativePath;
- }
- FileSerializer fs;
- fs.encode(copy.get(), path);
- }
- ResourceManifestPtr ResourceManifest::load(const WString& path, const WString& relativePath)
- {
- WString standRelativePath = Path::standardizePath(relativePath);
- StringUtil::toLowerCase(standRelativePath);
- FileSerializer fs;
- ResourceManifestPtr manifest = std::static_pointer_cast<ResourceManifest>(fs.decode(path));
- ResourceManifestPtr copy = create(manifest->mName);
- for(auto& elem : manifest->mFilePathToUUID)
- {
- WString absPath = Path::combine(standRelativePath, elem.first);
- copy->mFilePathToUUID[absPath] = elem.second;
- }
- for(auto& elem : manifest->mUUIDToFilePath)
- {
- WString absPath = Path::combine(standRelativePath, elem.second);
- copy->mUUIDToFilePath[elem.first] = absPath;
- }
- return copy;
- }
- RTTITypeBase* ResourceManifest::getRTTIStatic()
- {
- return ResourceManifestRTTI::instance();
- }
- RTTITypeBase* ResourceManifest::getRTTI() const
- {
- return ResourceManifest::getRTTIStatic();
- }
- }
|