using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Android; namespace SC.XR.Unity { public class Module_SDKGlobalConfiguration : MonoBehaviour { private static Module_SDKGlobalConfiguration mInstance; public static Module_SDKGlobalConfiguration getInstance { get { if (mInstance == null) { mInstance = new GameObject("SDKGlobalConfiguration").AddComponent(); } return mInstance; } } private SDKGlobalConfigTranslation SDKGlobalConfigurationAsset; private ConfigFile configFile; private string fileConfigPath { get { return Application.persistentDataPath + "/SDKGlobalConfigs/SDKGlobal" + "_Configs_"+ API_Module_SDKVersion.Version + ".txt"; } } public bool IsInit { get => mIsInit; } private bool mIsInit = false; void Awake() { mIsInit = Init(); } private bool Init() { configFile = new ConfigFile(fileConfigPath, true); DebugMy.Log("Create GlobalConfig File", this, true); if (Application.isEditor) { DebugMy.Log("Platfom isEditor: Remove SDKConfigs File", this, true); configFile.RemoveConfigFile(); } LoadAllConfigFromFileAndAsset(); DebugMy.Log("Module_GlobalConfiguration Init Finish !", this, true); return true; } private void OnDisable() { SaveAllConfigToFile(); } private void OnDestroy() { if (mInstance != null) { SaveAllConfigToFile(); } } private void OnApplicationPause(bool pause) { return; if (pause) { SaveAllConfigToFile(); } else { LoadAllConfigFromFileAndAsset(); } } private void OnApplicationQuit() { SaveAllConfigToFile(); } private void LoadAllConfigFromFileAndAsset() { bool isExist = configFile.ParseConfig(); if (isExist == false) { DebugMy.Log("GlobalConfigFile:" + fileConfigPath + " Not Exist !", this, true); } if (SDKGlobalConfigurationAsset == null) { SDKGlobalConfigurationAsset = SDKGlobalConfigTranslation.getInstance; if (SDKGlobalConfigurationAsset == null) { DebugMy.Log("SDKGlobalConfiguration Not Exist !", this, true); } SDKGlobalConfigurationAsset.gameObject.transform.SetParent(transform); } if (SDKGlobalConfigurationAsset) { if (isExist == false) { foreach (var section in SDKGlobalConfigurationAsset.Configs) { foreach (var keyValue in section.KEY_VALUE) { configFile.SetString(section.section, keyValue.Name, keyValue.Value); DebugMy.Log("Write To GlobalConfigsFile ==> [" + section.section + "]:" + keyValue.Name + "=" + keyValue.Value, this, true); } } configFile.SaveConfig(); } else { foreach (var dic in configFile.Configs.Keys) { foreach (var keyValue in configFile.Configs[dic]) { DebugMy.Log("Read From ConfigsFile ==> [" + dic + "]:" + keyValue.Key + "=" + keyValue.Value, this, true); } } foreach (var section in SDKGlobalConfigurationAsset.Configs) { foreach (var keyValue in section.KEY_VALUE) { if (configFile.AddString(section.section, keyValue.Name, keyValue.Value)) { DebugMy.Log("Add Extra Config From SDKGlobalConfigurationAsset ==> [" + section.section + "]:" + keyValue.Name + "=" + keyValue.Value, this, true); } } } configFile.SaveConfig(); } } } public int GetInt(string section, string key, int defaultVal) { return getInstance.configFile.GetInt(section, key, defaultVal); } public bool GetBool(string section, string key, int defaultVal) { return getInstance.configFile.GetInt(section, key, defaultVal) > 0 ? true : false; } public string GetString(string section, string key, string defaultVal) { return getInstance.configFile.GetString(section, key, defaultVal); } public float GetFloat(string section, string key, float defaultVal) { return getInstance.configFile.GetFloat(section, key, defaultVal); } public bool HasKey(string section, string key) { return getInstance.configFile.HasKey(section, key); } public void SaveAllConfigToFile() { getInstance.configFile.SaveConfig(); } public void SetString(string sec, string key, string val) { getInstance.configFile.SetString(sec, key, val); } } }