|
@@ -2,12 +2,24 @@
|
|
|
#include "CmResource.h"
|
|
#include "CmResource.h"
|
|
|
#include "CmException.h"
|
|
#include "CmException.h"
|
|
|
#include "CmFileSerializer.h"
|
|
#include "CmFileSerializer.h"
|
|
|
|
|
+#include "CmFileSystem.h"
|
|
|
|
|
+#include "CmUUID.h"
|
|
|
|
|
+#include "CmPath.h"
|
|
|
|
|
+#include "CmDebug.h"
|
|
|
|
|
+#include "CmResourcesRTTI.h"
|
|
|
|
|
|
|
|
namespace CamelotEngine
|
|
namespace CamelotEngine
|
|
|
{
|
|
{
|
|
|
- Resources::Resources()
|
|
|
|
|
|
|
+ Resources::Resources(const String& metaDataFolder)
|
|
|
{
|
|
{
|
|
|
|
|
+ mMetaDataFolderPath = metaDataFolder;
|
|
|
|
|
|
|
|
|
|
+ if(!FileSystem::dirExists(mMetaDataFolderPath))
|
|
|
|
|
+ {
|
|
|
|
|
+ FileSystem::createDir(mMetaDataFolderPath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ loadMetaData();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Resources::~Resources()
|
|
Resources::~Resources()
|
|
@@ -34,9 +46,17 @@ namespace CamelotEngine
|
|
|
return BaseResourceRef(resource);
|
|
return BaseResourceRef(resource);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- BaseResourceRef Resources::load(const UUID& uuid)
|
|
|
|
|
|
|
+ BaseResourceRef Resources::loadFromUUID(const String& uuid)
|
|
|
{
|
|
{
|
|
|
- CM_EXCEPT(NotImplementedException, "Not implemented");
|
|
|
|
|
|
|
+ if(!exists(uuid))
|
|
|
|
|
+ {
|
|
|
|
|
+ gDebug().logWarning("Cannot load resource. Resource with UUID '" + uuid + "' doesn't exist.");
|
|
|
|
|
+ return nullptr;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ResourceMetaDataPtr metaEntry = mResourceMetaData[uuid];
|
|
|
|
|
+
|
|
|
|
|
+ return load(metaEntry->mPath);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void Resources::save(BaseResourceRef resource, const String& filePath)
|
|
void Resources::save(BaseResourceRef resource, const String& filePath)
|
|
@@ -45,12 +65,103 @@ namespace CamelotEngine
|
|
|
|
|
|
|
|
// TODO - Low priority. Check is file path valid?
|
|
// TODO - Low priority. Check is file path valid?
|
|
|
|
|
|
|
|
|
|
+ // TODO - When saving we just overwrite any existing resource and generate new meta file
|
|
|
|
|
+ // - What if we just want to update existing resource, ie. reimport it?
|
|
|
|
|
+
|
|
|
FileSerializer fs;
|
|
FileSerializer fs;
|
|
|
fs.encode(resource.get(), filePath);
|
|
fs.encode(resource.get(), filePath);
|
|
|
|
|
+
|
|
|
|
|
+ if(exists(resource->getUUID()))
|
|
|
|
|
+ updateMetaData(resource->getUUID(), filePath);
|
|
|
|
|
+ else
|
|
|
|
|
+ addMetaData(resource->getUUID(), filePath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void Resources::loadMetaData()
|
|
|
|
|
+ {
|
|
|
|
|
+ vector<String>::type allFiles = FileSystem::getFiles(mMetaDataFolderPath);
|
|
|
|
|
+
|
|
|
|
|
+ for(auto iter = allFiles.begin(); iter != allFiles.end(); ++iter)
|
|
|
|
|
+ {
|
|
|
|
|
+ String& path = *iter;
|
|
|
|
|
+ if(Path::hasExtension(path, "resmeta"))
|
|
|
|
|
+ {
|
|
|
|
|
+ FileSerializer fs;
|
|
|
|
|
+ std::shared_ptr<IReflectable> loadedData = fs.decode(path);
|
|
|
|
|
+
|
|
|
|
|
+ ResourceMetaDataPtr metaData = std::static_pointer_cast<ResourceMetaData>(loadedData);
|
|
|
|
|
+ mResourceMetaData[metaData->mUUID] = metaData;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void Resources::saveMetaData(const ResourceMetaDataPtr metaData)
|
|
|
|
|
+ {
|
|
|
|
|
+ String fullPath = Path::combine(mMetaDataFolderPath, metaData->mUUID + ".resmeta");
|
|
|
|
|
+
|
|
|
|
|
+ FileSerializer fs;
|
|
|
|
|
+ fs.encode(metaData.get(), fullPath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void Resources::addMetaData(const String& uuid, const String& filePath)
|
|
|
|
|
+ {
|
|
|
|
|
+ for(auto iter = mResourceMetaData.begin(); iter != mResourceMetaData.end(); ++iter)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(iter->second->mPath == filePath)
|
|
|
|
|
+ {
|
|
|
|
|
+ CM_EXCEPT(InvalidParametersException, "Resource with the path '" + filePath + "' already exists.");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(exists(uuid))
|
|
|
|
|
+ {
|
|
|
|
|
+ CM_EXCEPT(InternalErrorException, "Resource with the same UUID already exists. UUID: " + uuid);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ResourceMetaDataPtr dbEntry(new ResourceMetaData());
|
|
|
|
|
+ dbEntry->mPath = filePath;
|
|
|
|
|
+ dbEntry->mUUID = uuid;
|
|
|
|
|
+
|
|
|
|
|
+ mResourceMetaData[uuid] = dbEntry;
|
|
|
|
|
+
|
|
|
|
|
+ saveMetaData(dbEntry);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ void Resources::updateMetaData(const String& uuid, const String& newFilePath)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(!exists(uuid))
|
|
|
|
|
+ {
|
|
|
|
|
+ CM_EXCEPT(InvalidParametersException, "Cannot update a resource that doesn't exist. UUID: " + uuid + ". File path: " + newFilePath);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ResourceMetaDataPtr dbEntry = mResourceMetaData[uuid];
|
|
|
|
|
+ dbEntry->mPath = newFilePath;
|
|
|
|
|
+
|
|
|
|
|
+ saveMetaData(dbEntry);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bool Resources::exists(const String& uuid) const
|
|
|
|
|
+ {
|
|
|
|
|
+ auto findIter = mResourceMetaData.find(uuid);
|
|
|
|
|
+
|
|
|
|
|
+ return findIter != mResourceMetaData.end();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
CM_EXPORT Resources& gResources()
|
|
CM_EXPORT Resources& gResources()
|
|
|
{
|
|
{
|
|
|
return Resources::instance();
|
|
return Resources::instance();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /************************************************************************/
|
|
|
|
|
+ /* SERIALIZATION */
|
|
|
|
|
+ /************************************************************************/
|
|
|
|
|
+ RTTITypeBase* Resources::ResourceMetaData::getRTTIStatic()
|
|
|
|
|
+ {
|
|
|
|
|
+ return ResourceMetaDataRTTI::instance();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ RTTITypeBase* Resources::ResourceMetaData::getRTTI() const
|
|
|
|
|
+ {
|
|
|
|
|
+ return Resources::ResourceMetaData::getRTTIStatic();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|