Bläddra i källkod

Fixed deserialization issues in GameModule

James Peret 2 år sedan
förälder
incheckning
77a16dbf82

+ 13 - 2
Runtime/ModuleSystem/GameConfig.cs

@@ -15,7 +15,7 @@ namespace KairoEngine.Core.ModuleSystem
     {
         [Title("Game Modules", "List of game system modules in load order")]
         [ListDrawerSettings(DraggableItems = false, HideRemoveButton = true, ShowPaging = false)]
-        [ShowInInspector, NonSerialized, OnValueChanged("StartModule"), OnInspectorInit("StartModule")]
+        [ShowInInspector, NonSerialized, OnValueChanged("StartModule")]
         public List<GameModule> modules = new List<GameModule>();
 
         [OnInspectorInit("GetModuleNames"), OnValueChanged("AddNewModule")]
@@ -86,18 +86,29 @@ namespace KairoEngine.Core.ModuleSystem
 
         public void OnAfterDeserialize()
         {
+            
             if(serializedModules == null) return;
             if(modules == null) modules = new List<GameModule>();
             if(modules.Count > 0) modules.Clear();
             for (int i = 0; i < serializedModules.Count; i++)
             {
-                GameModule module = JsonUtility.FromJson<GameModuleBase>(serializedModules[i]);
+                GameModule module= null;
+                try
+                {
+                    module = JsonUtility.FromJson<GameModuleBase>(serializedModules[i]);
+                }
+                catch (System.Exception e)
+                {
+                    Debug.LogError($"Could not deserialize GameModule: \n{e}");
+                }
+                if(module == null) continue;
                 module = GameModule.InvokeStringMethod(module.className, module.typeName, serializedModules[i]);
                 module.gameConfig = this;
                 module.OnBeforeDeserialize(serializer);
                 modules.Add(module);
             }
             serializer.Clear();
+            serializedModules.Clear();
         }
 
     }

+ 1 - 1
Runtime/ModuleSystem/GameInstaller.cs

@@ -9,7 +9,7 @@ namespace KairoEngine.Core.ModuleSystem
     [HideMonoScript, AddComponentMenu("KairoEngine/Game Installer"), RequireComponent(typeof(SceneContext))]
     public class GameInstaller : MonoInstaller
     {   
-        public GameConfig gameConfig;
+        [InlineEditor(InlineEditorObjectFieldModes.Boxed)] public GameConfig gameConfig;
         public override void InstallBindings()
         {
             //Container.Bind<string>().FromInstance("Hello World!");

+ 12 - 3
Runtime/ModuleSystem/GameModule.cs

@@ -19,7 +19,7 @@ namespace KairoEngine.Core.ModuleSystem
         [FoldoutGroup("@name")] public bool enableModule = true;
 
         [HideInInspector] public bool isInitialized = false;
-        [HideInInspector] public GameConfig gameConfig;
+        [HideInInspector, NonSerialized] public GameConfig gameConfig;
 
         public GameModule(GameConfig config)
         {
@@ -62,12 +62,21 @@ namespace KairoEngine.Core.ModuleSystem
                 Debug.LogError($"Could not find type: \"{typeName}\"");
                 return null;
             }
-            GameModuleBase module = (GameModuleBase)calledType.InvokeMember($"JSONTo{methodName}", 
+            try
+            {
+                GameModuleBase module = (GameModuleBase)calledType.InvokeMember($"JSONTo{methodName}", 
                 System.Reflection.BindingFlags.InvokeMethod | 
                 System.Reflection.BindingFlags.Public |
                 System.Reflection.BindingFlags.Static, 
                 null, null, new object[] { data });
-            return module;
+                return module;
+            }
+            catch (System.Exception e)
+            {
+                Debug.LogError($"Error deserializing GameModule: \n{e}");
+                return null;
+            }
+            
         }
 
         public static GameModule JSONToGameModule(string data)

+ 79 - 9
Runtime/ObjectSerializer.cs

@@ -4,21 +4,91 @@ using UnityEngine;
 
 namespace KairoEngine.Core
 {
+    // Todo: Refactor components to use lists instead of a dictionary
+    // Todo: Refactor ScriptableObjects to use lists instead of a dictionary
     [System.Serializable]
     public class ObjectSerializer
     {
-        [SerializeField, HideInInspector] public Dictionary<string, object> objects = new Dictionary<string, object>();
-        [SerializeField, HideInInspector] public Dictionary<string, GameObject> gameObjecs = new Dictionary<string, GameObject>();
-        [SerializeField, HideInInspector] public Dictionary<string, Component> components = new Dictionary<string, Component>();
-        [SerializeField, HideInInspector] public Dictionary<string, ScriptableObject> scriptableObjects = new Dictionary<string, ScriptableObject>();
+        //[HideInInspector] public Dictionary<string, Component> components = new Dictionary<string, Component>();
+        //[HideInInspector] public Dictionary<string, ScriptableObject> scriptableObjects = new Dictionary<string, ScriptableObject>();
 
         public void Clear()
         {
-            objects.Clear();
-            gameObjecs.Clear();
-            components.Clear();
-            scriptableObjects.Clear();
+            objectKeyList.Clear();
+            objectList.Clear();
+            gameObjectKeyList.Clear();
+            gameObjectList.Clear();
+            //components.Clear();
+            //scriptableObjects.Clear();
         }
-    }
 
+        #region Objects
+        [SerializeField] private List<string> objectKeyList = new List<string>();
+        [SerializeField] private List<object> objectList = new List<object>();
+
+        private int GetObjectIndex(string key)
+        {
+            for (int i = 0; i < objectKeyList.Count; i++)
+            {
+                if(objectKeyList[i] == key) return i;
+            }
+            return -1;
+        }
+
+        public void AddObject(string key, object obj)
+        {
+            int index = GetObjectIndex(key);
+            if(index == -1) 
+            {
+                objectKeyList.Add(key);
+                objectList.Add(obj);
+            }
+            else objectList[index] = obj;
+        }
+
+        public object GetObject(string key)
+        {
+            int index = GetObjectIndex(key);
+            if(index == -1) return null;
+            else return objectList[index];
+        }
+
+        #endregion
+
+        #region gameObjects
+
+        [SerializeField] private List<string> gameObjectKeyList = new List<string>();
+        [SerializeField] private List<GameObject> gameObjectList = new List<GameObject>();
+
+        private int GetGameObjectIndex(string key)
+        {
+            for (int i = 0; i < gameObjectKeyList.Count; i++)
+            {
+                if(gameObjectKeyList[i] == key) return i;
+            }
+            return -1;
+        }
+
+        public void AddGameObject(string key, GameObject obj)
+        {
+            int index = GetGameObjectIndex(key);
+            if(index == -1) 
+            {
+                gameObjectKeyList.Add(key);
+                gameObjectList.Add(obj);
+            }
+            else gameObjectList[index] = obj;
+        }
+
+        public GameObject GetGameObject(string key)
+        {
+            int index = GetGameObjectIndex(key);
+            if(index == -1) return null;
+            else return gameObjectList[index];
+        }
+
+        public int GameObjectCount() => gameObjectList.Count;
+
+        #endregion
+    }
 }