123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
-
- 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<GameModule>
- {
- [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<GameModule>(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";
- }
- }
- }
|