Просмотр исходного кода

Modified GameObject serialization so it doesn't require manual calls to GameObjectManager

Marko Pintera 11 лет назад
Родитель
Сommit
38b79673f7

+ 33 - 0
BansheeCore/Include/BsSceneObjectRTTI.h

@@ -11,6 +11,15 @@ namespace BansheeEngine
 	class BS_CORE_EXPORT SceneObjectRTTI : public RTTIType<SceneObject, GameObject, SceneObjectRTTI>
 	class BS_CORE_EXPORT SceneObjectRTTI : public RTTIType<SceneObject, GameObject, SceneObjectRTTI>
 	{
 	{
 	private:
 	private:
+		struct DeserializationData
+		{
+			DeserializationData(bool isDeserializationParent)
+				:isDeserializationParent(isDeserializationParent)
+			{ }
+
+			bool isDeserializationParent;
+		};
+
 		// NOTE - These can only be set sequentially, specific array index is ignored
 		// NOTE - These can only be set sequentially, specific array index is ignored
 		std::shared_ptr<SceneObject> getChild(SceneObject* obj, UINT32 idx) { return obj->mChildren[idx].getInternalPtr(); }
 		std::shared_ptr<SceneObject> getChild(SceneObject* obj, UINT32 idx) { return obj->mChildren[idx].getInternalPtr(); }
 		void setChild(SceneObject* obj, UINT32 idx, std::shared_ptr<SceneObject> param) { param->setParent(obj->mThisHandle); } // NOTE: Can only be used for sequentially adding elements
 		void setChild(SceneObject* obj, UINT32 idx, std::shared_ptr<SceneObject> param) { param->setParent(obj->mThisHandle); } // NOTE: Can only be used for sequentially adding elements
@@ -32,6 +41,30 @@ namespace BansheeEngine
 				&SceneObjectRTTI::getNumComponents, &SceneObjectRTTI::setComponent, &SceneObjectRTTI::setNumComponents);
 				&SceneObjectRTTI::getNumComponents, &SceneObjectRTTI::setComponent, &SceneObjectRTTI::setNumComponents);
 		}
 		}
 
 
+		virtual void onDeserializationStarted(IReflectable* obj)
+		{
+			SceneObject* so = static_cast<SceneObject*>(obj);
+
+			if (!GameObjectManager::instance().isGameObjectDeserializationActive())
+			{
+				GameObjectManager::instance().startDeserialization();
+				so->mRTTIData = DeserializationData(true);
+			}
+			else
+				so->mRTTIData = DeserializationData(false);
+		}
+
+		virtual void onDeserializationEnded(IReflectable* obj)
+		{
+			SceneObject* so = static_cast<SceneObject*>(obj);
+			DeserializationData deserializationData = any_cast<DeserializationData>(so->mRTTIData);
+
+			if (deserializationData.isDeserializationParent)
+				GameObjectManager::instance().endDeserialization();
+
+			so->mRTTIData = nullptr;
+		}
+
 		virtual const String& getRTTIName()
 		virtual const String& getRTTIName()
 		{
 		{
 			static String name = "SceneObject";
 			static String name = "SceneObject";

+ 0 - 2
BansheeCore/Source/BsSceneObject.cpp

@@ -395,10 +395,8 @@ namespace BansheeEngine
 		MemorySerializer serializer;
 		MemorySerializer serializer;
 		UINT8* buffer = serializer.encode(this, bufferSize, &bs_alloc);
 		UINT8* buffer = serializer.encode(this, bufferSize, &bs_alloc);
 
 
-		GameObjectManager::instance().startDeserialization();
 		std::shared_ptr<SceneObject> cloneObj = std::static_pointer_cast<SceneObject>(serializer.decode(buffer, bufferSize));
 		std::shared_ptr<SceneObject> cloneObj = std::static_pointer_cast<SceneObject>(serializer.decode(buffer, bufferSize));
 		bs_free(buffer);
 		bs_free(buffer);
-		GameObjectManager::instance().endDeserialization();
 
 
 		return cloneObj->mThisHandle;
 		return cloneObj->mThisHandle;
 	}
 	}

+ 1 - 1
MBansheeEditor/Program.cs

@@ -18,7 +18,7 @@ namespace BansheeEditor
             window.Refresh(); // TODO - This should be called N times per second
             window.Refresh(); // TODO - This should be called N times per second
 
 
             DbgResource testResource = new DbgResource();
             DbgResource testResource = new DbgResource();
-            ProjectLibrary.Create(testResource, @"D:\DummyBansheeProject\Resources\testResource");
+            //ProjectLibrary.Create(testResource, @"D:\DummyBansheeProject\Resources\testResource");
 
 
 
 
 
 

+ 56 - 56
MBansheeEngine/Program.cs

@@ -10,49 +10,49 @@ namespace BansheeEngine
     {
     {
         static void Main()
         static void Main()
         {
         {
-            //SceneObject otherSO = new SceneObject("OtherSO");
-            //DbgComponent2 dbgComponent2 = otherSO.AddComponent<DbgComponent2>();
-            //dbgComponent2.a2 = 33;
-
-            //GUIElementStateStyle dbgStyle = new GUIElementStateStyle();
-            //SceneObject so = new SceneObject("TestSO");
-            //DbgComponent dbgComponent = so.AddComponent<DbgComponent>();
-
-            //dbgComponent.a = 5;
-            //dbgComponent.b = "SomeTestVal";
-            //dbgComponent.complex.someValue = 19;
-            //dbgComponent.complex.anotherValue = "AnotherValue";
-            //dbgComponent.complex2.someValue2 = 21;
-            //dbgComponent.complex2.anotherValue2 = "AnotherValue2";
-
-            //dbgComponent.arrA = new int[5];
-            //dbgComponent.arrA[4] = 5;
-            //dbgComponent.arrB = new string[5];
-            //dbgComponent.arrB[4] = "ArrAnotherValue";
-            //dbgComponent.arrComplex = new DbgSerzObj[5];
-            //dbgComponent.arrComplex[4].someValue = 99;
-            //dbgComponent.arrComplex[4].anotherValue = "ArrComplexAnotherValue";
-            //dbgComponent.arrComplex2 = new DbgSerzCls[5];
-            //dbgComponent.arrComplex2[4] = new DbgSerzCls();
-            //dbgComponent.arrComplex2[4].someValue2 = 101;
-            //dbgComponent.arrComplex2[4].anotherValue2 = "ArrComplex2AnotherValue";
-
-            //dbgComponent.listA = new List<int>();
-            //dbgComponent.listA.Add(5);
-            //dbgComponent.listB = new List<string>();
-            //dbgComponent.listB.Add("ListAnotherValue");
-            //dbgComponent.listB.Add(null);
-            //dbgComponent.listComplex = new List<DbgSerzObj>();
-            //dbgComponent.listComplex.Add(new DbgSerzObj());
-            //dbgComponent.listComplex.Add(new DbgSerzObj(99, "ListComplexAnotherValue"));
-            //dbgComponent.listComplex2 = new List<DbgSerzCls>();
-            //dbgComponent.listComplex2.Add(new DbgSerzCls());
-            //dbgComponent.listComplex2[0].someValue2 = 101;
-            //dbgComponent.listComplex2[0].anotherValue2 = "ListComplexAnotherValue";
-            //dbgComponent.listComplex2.Add(null);
-
-            //dbgComponent.otherComponent = dbgComponent2;
-            //dbgComponent.otherSO = otherSO;
+            SceneObject otherSO = new SceneObject("OtherSO");
+            DbgComponent2 dbgComponent2 = otherSO.AddComponent<DbgComponent2>();
+            dbgComponent2.a2 = 33;
+
+            GUIElementStateStyle dbgStyle = new GUIElementStateStyle();
+            SceneObject so = new SceneObject("TestSO");
+            DbgComponent dbgComponent = so.AddComponent<DbgComponent>();
+
+            dbgComponent.a = 5;
+            dbgComponent.b = "SomeTestVal";
+            dbgComponent.complex.someValue = 19;
+            dbgComponent.complex.anotherValue = "AnotherValue";
+            dbgComponent.complex2.someValue2 = 21;
+            dbgComponent.complex2.anotherValue2 = "AnotherValue2";
+
+            dbgComponent.arrA = new int[5];
+            dbgComponent.arrA[4] = 5;
+            dbgComponent.arrB = new string[5];
+            dbgComponent.arrB[4] = "ArrAnotherValue";
+            dbgComponent.arrComplex = new DbgSerzObj[5];
+            dbgComponent.arrComplex[4].someValue = 99;
+            dbgComponent.arrComplex[4].anotherValue = "ArrComplexAnotherValue";
+            dbgComponent.arrComplex2 = new DbgSerzCls[5];
+            dbgComponent.arrComplex2[4] = new DbgSerzCls();
+            dbgComponent.arrComplex2[4].someValue2 = 101;
+            dbgComponent.arrComplex2[4].anotherValue2 = "ArrComplex2AnotherValue";
+
+            dbgComponent.listA = new List<int>();
+            dbgComponent.listA.Add(5);
+            dbgComponent.listB = new List<string>();
+            dbgComponent.listB.Add("ListAnotherValue");
+            dbgComponent.listB.Add(null);
+            dbgComponent.listComplex = new List<DbgSerzObj>();
+            dbgComponent.listComplex.Add(new DbgSerzObj());
+            dbgComponent.listComplex.Add(new DbgSerzObj(99, "ListComplexAnotherValue"));
+            dbgComponent.listComplex2 = new List<DbgSerzCls>();
+            dbgComponent.listComplex2.Add(new DbgSerzCls());
+            dbgComponent.listComplex2[0].someValue2 = 101;
+            dbgComponent.listComplex2[0].anotherValue2 = "ListComplexAnotherValue";
+            dbgComponent.listComplex2.Add(null);
+
+            dbgComponent.otherComponent = dbgComponent2;
+            dbgComponent.otherSO = otherSO;
 
 
             //dbgComponent.zeArray = new int[5][][];
             //dbgComponent.zeArray = new int[5][][];
             //dbgComponent.zeList = new List<DbgSerzObj>();
             //dbgComponent.zeList = new List<DbgSerzObj>();
@@ -83,24 +83,24 @@ namespace BansheeEngine
 
 
             //dbgComponent.zeArray[4][1][3] = 129;
             //dbgComponent.zeArray[4][1][3] = 129;
 
 
-            //dbgTestComponentClone(so);
+            dbgTestComponentClone(so);
 
 
-            //for (int i = 0; i < so.GetNumChildren(); i++)
-            //{
-            //    SceneObject childSO = so.GetChild(i);
+            for (int i = 0; i < so.GetNumChildren(); i++)
+            {
+                SceneObject childSO = so.GetChild(i);
 
 
-            //    DbgComponent otherComponent = childSO.GetComponent<DbgComponent>();
-            //    reportDbgValue(otherComponent.a, otherComponent.b, otherComponent.complex.someValue,
-            //                   otherComponent.complex2.anotherValue2);
+                DbgComponent otherComponent = childSO.GetComponent<DbgComponent>();
+                reportDbgValue(otherComponent.a, otherComponent.b, otherComponent.complex.someValue,
+                               otherComponent.complex2.anotherValue2);
 
 
-            //    reportDbgValue(otherComponent.arrA[4], otherComponent.arrB[4], otherComponent.arrComplex[4].someValue,
-            //      otherComponent.arrComplex2[4].anotherValue2);
+                reportDbgValue(otherComponent.arrA[4], otherComponent.arrB[4], otherComponent.arrComplex[4].someValue,
+                  otherComponent.arrComplex2[4].anotherValue2);
 
 
-            //    reportDbgValue(otherComponent.listA[0], otherComponent.listB[0], otherComponent.listComplex[1].someValue,
-            //        otherComponent.listComplex2[0].anotherValue2);
+                reportDbgValue(otherComponent.listA[0], otherComponent.listB[0], otherComponent.listComplex[1].someValue,
+                    otherComponent.listComplex2[0].anotherValue2);
 
 
-            //    //reportDbgValue(childSO.GetComponent<DbgComponent>().zeDict["lolz"], childSO.GetComponent<DbgComponent>().zeList[2].someValue, childSO.GetComponent<DbgComponent>().zeArray[4][1][3], typeof(DbgComponent));
-            //}
+                //reportDbgValue(childSO.GetComponent<DbgComponent>().zeDict["lolz"], childSO.GetComponent<DbgComponent>().zeList[2].someValue, childSO.GetComponent<DbgComponent>().zeArray[4][1][3], typeof(DbgComponent));
+            }
 
 
             //Color newColor = Color.red;
             //Color newColor = Color.red;