GameModule.cs 3.1 KB

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