GameModule.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. namespace KairoEngine.Core.ModuleSystem
  8. {
  9. [System.Serializable, HideReferenceObjectPicker, Toggle("enableModule")]
  10. public class GameModule : IComparable<GameModule>
  11. {
  12. [SerializeField, HideInInspector] public virtual string name => "Empty Module Slot";
  13. [SerializeField, HideInInspector] public string typeName = "GameModule";
  14. [SerializeField, HideInInspector] public string className;
  15. // [InlineButton("Reset", "Reset")]
  16. // [InlineButton("Remove", "Remove")]
  17. // [FoldoutGroup("@name")]
  18. public bool enableModule = true;
  19. [HideInInspector] public bool isInitialized = false;
  20. [HideInInspector, NonSerialized] public GameConfig gameConfig;
  21. public GameModule(GameConfig config)
  22. {
  23. this.gameConfig = config;
  24. this.className = this.GetType().AssemblyQualifiedName;
  25. }
  26. public virtual void Load(Transform parent)
  27. {
  28. }
  29. public virtual void Reset()
  30. {
  31. }
  32. public virtual void Destroy()
  33. {
  34. }
  35. public virtual void Start()
  36. {
  37. }
  38. [FoldoutGroup("@name")]
  39. public virtual void Remove()
  40. {
  41. gameConfig.modules.Remove(this);
  42. }
  43. public virtual int CompareTo(GameModule other)
  44. {
  45. if(other == null) return 1;
  46. else return -1;
  47. }
  48. public static GameModuleBase InvokeStringMethod(string typeName, string methodName, string data)
  49. {
  50. Type calledType = Type.GetType(typeName);
  51. if(calledType == null)
  52. {
  53. Debug.LogError($"Could not find type: \"{typeName}\"");
  54. return null;
  55. }
  56. try
  57. {
  58. GameModuleBase module = (GameModuleBase)calledType.InvokeMember($"JSONTo{methodName}",
  59. System.Reflection.BindingFlags.InvokeMethod |
  60. System.Reflection.BindingFlags.Public |
  61. System.Reflection.BindingFlags.Static,
  62. null, null, new object[] { data });
  63. return module;
  64. }
  65. catch (System.Exception e)
  66. {
  67. Debug.LogError($"Error deserializing GameModule: \n{e}");
  68. return null;
  69. }
  70. }
  71. public static GameModule JSONToGameModule(string data)
  72. {
  73. return JsonUtility.FromJson<GameModule>(data);
  74. }
  75. public virtual void OnBeforeSerialize(ObjectSerializer serializer) { }
  76. public virtual void OnBeforeDeserialize(ObjectSerializer serializer) { }
  77. }
  78. [System.Serializable, HideReferenceObjectPicker]
  79. public class GameModuleBase : GameModule
  80. {
  81. public GameModuleBase(GameConfig config) : base(config)
  82. {
  83. this.gameConfig = config;
  84. this.className = this.GetType().AssemblyQualifiedName;
  85. this.typeName = "GameModuleBase";
  86. }
  87. }
  88. }