Module_SDKGlobalConfiguration.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Android;
  6. namespace SC.XR.Unity
  7. {
  8. public class Module_SDKGlobalConfiguration : MonoBehaviour
  9. {
  10. private static Module_SDKGlobalConfiguration mInstance;
  11. public static Module_SDKGlobalConfiguration getInstance
  12. {
  13. get
  14. {
  15. if (mInstance == null)
  16. {
  17. mInstance = new GameObject("SDKGlobalConfiguration").AddComponent<Module_SDKGlobalConfiguration>();
  18. }
  19. return mInstance;
  20. }
  21. }
  22. private SDKGlobalConfigTranslation SDKGlobalConfigurationAsset;
  23. private ConfigFile configFile;
  24. private string fileConfigPath
  25. {
  26. get
  27. {
  28. return Application.persistentDataPath + "/SDKGlobalConfigs/SDKGlobal" + "_Configs_"+ API_Module_SDKVersion.Version + ".txt";
  29. }
  30. }
  31. public bool IsInit { get => mIsInit; }
  32. private bool mIsInit = false;
  33. void Awake() {
  34. mIsInit = Init();
  35. }
  36. private bool Init() {
  37. configFile = new ConfigFile(fileConfigPath, true);
  38. DebugMy.Log("Create GlobalConfig File", this, true);
  39. if (Application.isEditor) {
  40. DebugMy.Log("Platfom isEditor: Remove SDKConfigs File", this, true);
  41. configFile.RemoveConfigFile();
  42. }
  43. LoadAllConfigFromFileAndAsset();
  44. DebugMy.Log("Module_GlobalConfiguration Init Finish !", this, true);
  45. return true;
  46. }
  47. private void OnDisable() {
  48. SaveAllConfigToFile();
  49. }
  50. private void OnDestroy() {
  51. if (mInstance != null) {
  52. SaveAllConfigToFile();
  53. }
  54. }
  55. private void OnApplicationPause(bool pause)
  56. {
  57. return;
  58. if (pause) {
  59. SaveAllConfigToFile();
  60. } else {
  61. LoadAllConfigFromFileAndAsset();
  62. }
  63. }
  64. private void OnApplicationQuit() {
  65. SaveAllConfigToFile();
  66. }
  67. private void LoadAllConfigFromFileAndAsset()
  68. {
  69. bool isExist = configFile.ParseConfig();
  70. if (isExist == false) {
  71. DebugMy.Log("GlobalConfigFile:" + fileConfigPath + " Not Exist !", this, true);
  72. }
  73. if (SDKGlobalConfigurationAsset == null) {
  74. SDKGlobalConfigurationAsset = SDKGlobalConfigTranslation.getInstance;
  75. if (SDKGlobalConfigurationAsset == null) {
  76. DebugMy.Log("SDKGlobalConfiguration Not Exist !", this, true);
  77. }
  78. SDKGlobalConfigurationAsset.gameObject.transform.SetParent(transform);
  79. }
  80. if (SDKGlobalConfigurationAsset) {
  81. if (isExist == false) {
  82. foreach (var section in SDKGlobalConfigurationAsset.Configs) {
  83. foreach (var keyValue in section.KEY_VALUE) {
  84. configFile.SetString(section.section, keyValue.Name, keyValue.Value);
  85. DebugMy.Log("Write To GlobalConfigsFile ==> [" + section.section + "]:" + keyValue.Name + "=" + keyValue.Value, this, true);
  86. }
  87. }
  88. configFile.SaveConfig();
  89. } else {
  90. foreach (var dic in configFile.Configs.Keys) {
  91. foreach (var keyValue in configFile.Configs[dic]) {
  92. DebugMy.Log("Read From ConfigsFile ==> [" + dic + "]:" + keyValue.Key + "=" + keyValue.Value, this, true);
  93. }
  94. }
  95. foreach (var section in SDKGlobalConfigurationAsset.Configs) {
  96. foreach (var keyValue in section.KEY_VALUE) {
  97. if (configFile.AddString(section.section, keyValue.Name, keyValue.Value)) {
  98. DebugMy.Log("Add Extra Config From SDKGlobalConfigurationAsset ==> [" + section.section + "]:" + keyValue.Name + "=" + keyValue.Value, this, true);
  99. }
  100. }
  101. }
  102. configFile.SaveConfig();
  103. }
  104. }
  105. }
  106. public int GetInt(string section, string key, int defaultVal) {
  107. return getInstance.configFile.GetInt(section, key, defaultVal);
  108. }
  109. public bool GetBool(string section, string key, int defaultVal) {
  110. return getInstance.configFile.GetInt(section, key, defaultVal) > 0 ? true : false;
  111. }
  112. public string GetString(string section, string key, string defaultVal) {
  113. return getInstance.configFile.GetString(section, key, defaultVal);
  114. }
  115. public float GetFloat(string section, string key, float defaultVal) {
  116. return getInstance.configFile.GetFloat(section, key, defaultVal);
  117. }
  118. public bool HasKey(string section, string key) {
  119. return getInstance.configFile.HasKey(section, key);
  120. }
  121. public void SaveAllConfigToFile() {
  122. getInstance.configFile.SaveConfig();
  123. }
  124. public void SetString(string sec, string key, string val) {
  125. getInstance.configFile.SetString(sec, key, val);
  126. }
  127. }
  128. }