using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; namespace KairoEngine.Core.ModuleSystem { [System.Serializable, HideReferenceObjectPicker] public class GameModule : IComparable { [SerializeField, HideInInspector] public virtual string name => "Empty Module Slot"; [SerializeField, HideInInspector] public string typeName = "GameModule"; [SerializeField, HideInInspector] public string className; [InlineButton("Reset", "Reset")] [InlineButton("Remove", "Remove")] [FoldoutGroup("@name")] public bool enableModule = true; [HideInInspector] public bool isInitialized = false; [HideInInspector, NonSerialized] public GameConfig gameConfig; public GameModule(GameConfig config) { this.gameConfig = config; this.className = this.GetType().AssemblyQualifiedName; } public virtual void Load(Transform parent) { } public virtual void Reset() { } public virtual void Destroy() { } public virtual void Start() { } [FoldoutGroup("@name")] public virtual void Remove() { gameConfig.modules.Remove(this); } public virtual int CompareTo(GameModule other) { if(other == null) return 1; else return -1; } public static GameModuleBase InvokeStringMethod(string typeName, string methodName, string data) { Type calledType = Type.GetType(typeName); if(calledType == null) { Debug.LogError($"Could not find type: \"{typeName}\""); return null; } 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; } catch (System.Exception e) { Debug.LogError($"Error deserializing GameModule: \n{e}"); return null; } } public static GameModule JSONToGameModule(string data) { return JsonUtility.FromJson(data); } public virtual void OnBeforeSerialize(ObjectSerializer serializer) { } public virtual void OnBeforeDeserialize(ObjectSerializer serializer) { } } [System.Serializable, HideReferenceObjectPicker] public class GameModuleBase : GameModule { public GameModuleBase(GameConfig config) : base(config) { this.gameConfig = config; this.className = this.GetType().AssemblyQualifiedName; this.typeName = "GameModuleBase"; } } }