Browse Source

Resource saving/loading (untested)

Marko Pintera 13 years ago
parent
commit
a9653ac0b0

+ 2 - 2
CamelotRenderer/Include/CmResources.h

@@ -16,8 +16,8 @@ namespace CamelotEngine
 		 * 								If the database file doesn't exist it will be created in that location.
 		 * 								Meta data for resources is stored in the asset database.
 		 */
-		Resources(const String& assetDatabasePath); // TODO
-		~Resources(); // TODO
+		Resources();
+		~Resources();
 
 		/**
 		 * @brief	Loads the resource from a given path. Returns null if resource can't be loaded.

+ 42 - 0
CamelotRenderer/Source/CmResources.cpp

@@ -1,6 +1,48 @@
 #include "CmResources.h"
+#include "CmResource.h"
+#include "CmException.h"
+#include "CmFileSerializer.h"
 
 namespace CamelotEngine
 {
+	Resources::Resources()
+	{
 
+	}
+
+	Resources::~Resources()
+	{
+
+	}
+
+	ResourcePtr Resources::load(const String& filePath)
+	{
+		FileSerializer fs;
+		std::shared_ptr<IReflectable> resource = fs.decode(filePath);
+
+		// TODO - Low priority. Check is file path valid?
+
+		if(resource == nullptr)
+			CM_EXCEPT(InternalErrorException, "Unable to load resource.");
+
+		if(!resource->isDerivedFrom(Resource::getRTTIStatic()))
+			CM_EXCEPT(InternalErrorException, "Loaded class doesn't derive from Resource.");
+
+		return std::static_pointer_cast<Resource>(resource);
+	}
+
+	ResourcePtr Resources::load(const UUID& uuid)
+	{
+		CM_EXCEPT(NotImplementedException, "Not implemented");
+	}
+
+	void Resources::save(ResourcePtr resource, const String& filePath)
+	{
+		assert(resource != nullptr);
+
+		// TODO - Low priority. Check is file path valid?
+		
+		FileSerializer fs;
+		fs.encode(resource.get(), filePath);
+	}
 }

+ 0 - 5
CamelotRenderer/TODO.txt

@@ -51,11 +51,6 @@ TOMORROW:
  - How do I serialize derived classes, without rewriting all the base class serialization?
    - Unless something better dawns on me by Monday, just inherit from parents SerializableType and manually make sure no IDs overlap.
      - We can improve later if needed. I though about it too much for now.
-TextureData
- - When serializing this texture, request data from GPU, populate TextureData array and return
-Texture
- - Gets initialized by calling SetData(vector<TextureData). 
- - Returns TextureData by GetData (retrieves data from GPU)
  Continue working on Importer, Resources (IMPORTANT - AssetDatabase in a single file probably won't work. Multiple people won't be able to work on it. Unless we make it part of a local cache?)
  Try to fully implement free image and maybe FBX importers
   - Less important notes:

+ 6 - 0
CamelotUtility/Include/CmIReflectable.h

@@ -37,5 +37,11 @@ namespace CamelotEngine
 		static std::shared_ptr<IReflectable> createInstanceFromTypeId(UINT32 rttiTypeId);
 		static RTTITypeBase* getRTTIfromTypeId(UINT32 rttiTypeId);
 		static bool isTypeIdDuplicate(UINT32 typeId);
+
+		/**
+		 * @brief	Returns true if current RTTI class is derived from "base".
+		 * 			(Or if it is the same type as base)
+		 */
+		bool isDerivedFrom(RTTITypeBase* base);
 	};
 }

+ 23 - 0
CamelotUtility/Source/CmIReflectable.cpp

@@ -53,4 +53,27 @@ namespace CamelotEngine
 	{
 		return IReflectable::getRTTIfromTypeId(typeId) != nullptr;
 	}
+
+	bool IReflectable::isDerivedFrom(RTTITypeBase* base)
+	{
+		assert(base != nullptr);
+
+		stack<RTTITypeBase*>::type todo;
+		todo.push(base);
+
+		while (!todo.empty())
+		{
+			RTTITypeBase* currentType = todo.top();
+			todo.pop();
+
+			if(currentType->getRTTIId() == getRTTI()->getRTTIId())
+				return true;
+
+			vector<RTTITypeBase*>::type& derivedClasses = currentType->getDerivedClasses();
+			for(auto iter = derivedClasses.begin(); iter != derivedClasses.end(); ++iter)
+				todo.push(*iter);
+		}
+
+		return false;
+	}
 }