Module_SDKConfiguration.cs 4.9 KB

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