GameConfig.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. [Title("Game Modules", "List of game system modules in load order")]
  16. [ListDrawerSettings(DraggableItems = false, HideRemoveButton = true, ShowPaging = false)]
  17. [ShowInInspector, NonSerialized, OnValueChanged("StartModule"), OnInspectorInit("StartModule")]
  18. public List<GameModule> modules = new List<GameModule>();
  19. [OnInspectorInit("GetModuleNames"), OnValueChanged("AddNewModule")]
  20. [ValueDropdown("possibleModules", IsUniqueList = false)]
  21. [LabelText("Add New Module")]
  22. [NonSerialized]
  23. public GameModule newModule;
  24. [Title("Startup Configurations", "List of configurations to be set up when the game starts")]
  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. [NonSerialized] private IEnumerable possibleModules = new ValueDropdownList<GameModule>();
  38. private void GetModuleNames()
  39. {
  40. possibleModules = ReflectiveEnumerator.GetEnumerableOfType<GameModule>(this)
  41. .Where(x => x.typeName != "GameModule")
  42. .Where(x => x.typeName != "GameModuleBase")
  43. .Select(x => new ValueDropdownItem(x.name, x));
  44. }
  45. private void AddNewModule()
  46. {
  47. for (int i = 0; i < modules.Count; i++)
  48. {
  49. if(modules[i].name == newModule.name)
  50. {
  51. newModule = null;
  52. return;
  53. }
  54. }
  55. if(newModule != null)
  56. {
  57. modules.Add(newModule);
  58. newModule = null;
  59. }
  60. }
  61. [SerializeField, HideInInspector] private List<string> serializedModules = new List<string>();
  62. [SerializeField, HideInInspector] private ObjectSerializer serializer = new ObjectSerializer();
  63. public void OnBeforeSerialize()
  64. {
  65. if(modules == null) return;
  66. serializedModules = new List<string>();
  67. serializer.Clear();
  68. for (int i = 0; i < modules.Count; i++)
  69. {
  70. modules[i].OnBeforeSerialize(serializer);
  71. string data = JsonUtility.ToJson(modules[i]);
  72. serializedModules.Add(data);
  73. }
  74. }
  75. public void OnAfterDeserialize()
  76. {
  77. if(serializedModules == null) return;
  78. if(modules == null) modules = new List<GameModule>();
  79. if(modules.Count > 0) modules.Clear();
  80. for (int i = 0; i < serializedModules.Count; i++)
  81. {
  82. GameModule module = JsonUtility.FromJson<GameModuleBase>(serializedModules[i]);
  83. module = GameModule.InvokeStringMethod(module.className, module.typeName, serializedModules[i]);
  84. module.gameConfig = this;
  85. module.OnBeforeDeserialize(serializer);
  86. modules.Add(module);
  87. }
  88. serializer.Clear();
  89. }
  90. }
  91. }