123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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<Module_SDKGlobalConfiguration>();
- }
- 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);
- }
- }
- }
|