GameConfig.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. [OnInspectorInit("GetApplicationName"), ReadOnly] public string applicationName;
  16. [OnInspectorInit("GetAppVersion"), ReadOnly] public string appVersion;
  17. [ListDrawerSettings(DraggableItems = true, HideRemoveButton = false, ShowPaging = false, ListElementLabelName = "@name", HideAddButton = true)]
  18. [ShowInInspector, NonSerialized, OnValueChanged("StartModule"), LabelText("Game Modules")]
  19. public List<GameModule> modules = new List<GameModule>();
  20. [OnInspectorInit("GetModuleNames"), OnValueChanged("AddNewModule")]
  21. [ValueDropdown("possibleModules", IsUniqueList = false)]
  22. [LabelText("Add New Module")]
  23. [NonSerialized, PropertySpace(0, 8)]
  24. public GameModule newModule;
  25. [InlineProperty, HideLabel] public ConfigOptionsManager configOptions = new ConfigOptionsManager();
  26. private void StartModule()
  27. {
  28. foreach (var module in modules)
  29. {
  30. if(module.isInitialized == false)
  31. {
  32. module.isInitialized = true;
  33. module.gameConfig = this;
  34. }
  35. }
  36. }
  37. private string GetApplicationName() => applicationName = Application.productName;
  38. private string GetAppVersion() => appVersion = Application.version;
  39. [NonSerialized] private IEnumerable possibleModules = new ValueDropdownList<GameModule>();
  40. private void GetModuleNames()
  41. {
  42. possibleModules = ReflectiveEnumerator.GetEnumerableOfType<GameModule>(this)
  43. .Where(x => x.typeName != "GameModule")
  44. .Where(x => x.typeName != "GameModuleBase")
  45. .Select(x => new ValueDropdownItem(x.name, x));
  46. }
  47. private void AddNewModule()
  48. {
  49. for (int i = 0; i < modules.Count; i++)
  50. {
  51. if(modules[i].name == newModule.name)
  52. {
  53. newModule = null;
  54. return;
  55. }
  56. }
  57. if(newModule != null)
  58. {
  59. modules.Add(newModule);
  60. newModule = null;
  61. }
  62. }
  63. [SerializeField, HideInInspector] private List<string> serializedModules = new List<string>();
  64. [SerializeField, HideInInspector] private ObjectSerializer serializer = new ObjectSerializer();
  65. public void OnBeforeSerialize()
  66. {
  67. if(modules == null) return;
  68. serializedModules = new List<string>();
  69. serializer.Clear();
  70. for (int i = 0; i < modules.Count; i++)
  71. {
  72. modules[i].OnBeforeSerialize(serializer);
  73. string data = JsonUtility.ToJson(modules[i]);
  74. serializedModules.Add(data);
  75. }
  76. }
  77. public void OnAfterDeserialize()
  78. {
  79. if(serializedModules == null) return;
  80. if(modules == null) modules = new List<GameModule>();
  81. if(modules.Count > 0) modules.Clear();
  82. for (int i = 0; i < serializedModules.Count; i++)
  83. {
  84. GameModule module= null;
  85. try
  86. {
  87. module = JsonUtility.FromJson<GameModuleBase>(serializedModules[i]);
  88. }
  89. catch (System.Exception e)
  90. {
  91. Debug.LogError($"Could not deserialize GameModule: \n{e}");
  92. }
  93. if(module == null) continue;
  94. module = GameModule.InvokeStringMethod(module.className, module.typeName, serializedModules[i]);
  95. if(module == null) continue;
  96. module.gameConfig = this;
  97. module.OnBeforeDeserialize(serializer);
  98. modules.Add(module);
  99. }
  100. serializer.Clear();
  101. serializedModules.Clear();
  102. }
  103. }
  104. }