GameConfig.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. using KairoEngine.Core.ConfigOptions;
  9. using UniRx;
  10. namespace KairoEngine.Core.ModuleSystem
  11. {
  12. [CreateAssetMenu(fileName = "GameConfig", menuName = "KairoEngine/GameConfig"), HideMonoScript]
  13. public class GameConfig : ScriptableObject, ISerializationCallbackReceiver
  14. {
  15. [ListDrawerSettings(DraggableItems = false, HideRemoveButton = true, ShowPaging = false)]
  16. [ShowInInspector, NonSerialized, OnValueChanged("StartModule"), LabelText("Game Modules")]
  17. public List<GameModule> modules = new List<GameModule>();
  18. [OnInspectorInit("GetModuleNames"), OnValueChanged("AddNewModule")]
  19. [ValueDropdown("possibleModules", IsUniqueList = false)]
  20. [LabelText("Add New Module")]
  21. [NonSerialized, PropertySpace(0, 8)]
  22. public GameModule newModule;
  23. [InlineProperty, HideLabel] public ConfigOptionsManager configOptions = new ConfigOptionsManager();
  24. private void StartModule()
  25. {
  26. foreach (var module in modules)
  27. {
  28. if(module.isInitialized == false)
  29. {
  30. module.isInitialized = true;
  31. module.gameConfig = this;
  32. }
  33. }
  34. }
  35. [NonSerialized] private IEnumerable possibleModules = new ValueDropdownList<GameModule>();
  36. private void GetModuleNames()
  37. {
  38. possibleModules = ReflectiveEnumerator.GetEnumerableOfType<GameModule>(this)
  39. .Where(x => x.typeName != "GameModule")
  40. .Where(x => x.typeName != "GameModuleBase")
  41. .Select(x => new ValueDropdownItem(x.name, x));
  42. }
  43. private void AddNewModule()
  44. {
  45. for (int i = 0; i < modules.Count; i++)
  46. {
  47. if(modules[i].name == newModule.name)
  48. {
  49. newModule = null;
  50. return;
  51. }
  52. }
  53. if(newModule != null)
  54. {
  55. modules.Add(newModule);
  56. newModule = null;
  57. }
  58. }
  59. [SerializeField, HideInInspector] private List<string> serializedModules = new List<string>();
  60. [SerializeField, HideInInspector] private ObjectSerializer serializer = new ObjectSerializer();
  61. public void OnBeforeSerialize()
  62. {
  63. if(modules == null) return;
  64. serializedModules = new List<string>();
  65. serializer.Clear();
  66. for (int i = 0; i < modules.Count; i++)
  67. {
  68. modules[i].OnBeforeSerialize(serializer);
  69. string data = JsonUtility.ToJson(modules[i]);
  70. serializedModules.Add(data);
  71. }
  72. }
  73. public void OnAfterDeserialize()
  74. {
  75. if(serializedModules == null) return;
  76. if(modules == null) modules = new List<GameModule>();
  77. if(modules.Count > 0) modules.Clear();
  78. for (int i = 0; i < serializedModules.Count; i++)
  79. {
  80. GameModule module= null;
  81. try
  82. {
  83. module = JsonUtility.FromJson<GameModuleBase>(serializedModules[i]);
  84. }
  85. catch (System.Exception e)
  86. {
  87. Debug.LogError($"Could not deserialize GameModule: \n{e}");
  88. }
  89. if(module == null) continue;
  90. module = GameModule.InvokeStringMethod(module.className, module.typeName, serializedModules[i]);
  91. if(module == null) continue;
  92. module.gameConfig = this;
  93. module.OnBeforeDeserialize(serializer);
  94. modules.Add(module);
  95. }
  96. serializer.Clear();
  97. serializedModules.Clear();
  98. }
  99. }
  100. }