using UnityEngine; using UnityEditor; using GameCore; using System.Collections; using System.IO; using System; namespace GameLogic { public class MConfig { Hashtable info; public MConfig(string json) { info = MiniJSON.jsonDecode(json) as Hashtable; } public string ToJson() { return MiniJSON.jsonEncode(info); } public string GetInfo(string key) { string[] ks = key.Split('.'); Hashtable ins = info; for (int i = 0; i < ks.Length; i++) { string k = ks[i]; if (!ins.ContainsKey(k)) { break; } // 没有下一个值直接返回 if (i + 1 == ks.Length) { return ins[k] as string; } else { ins = ins[k] as Hashtable; } } return null; } public void RemoveInfo(string key) { string[] ks = key.Split('.'); Hashtable ins = info; for (int i = 0; i < ks.Length; i++) { string k = ks[i]; if (!ins.ContainsKey(k)) { break; } // if (i + 1 == ks.Length) { ins.Remove(k); } else { ins = ins[k] as Hashtable; } } } public void SetInfo(string key, string value) { string[] ks = key.Split('.'); Hashtable ins = info; for (int i = 0; i < ks.Length; i++) { string k = ks[i]; if (!ins.ContainsKey(k)) { break; } // if (i + 1 == ks.Length) { ins[k] = value; } else { ins = ins[k] as Hashtable; } } } } public class ConfigManager : Singleton { /// /// 版本号文件 /// const string configFile = "config"; /// /// 版本文件路径 /// string configFilePath { get { return AppConst.PersistentDataPath + configFile + ".txt"; } } /// /// Stream版本文件路径 /// string configStreamFilePath { get { return AppConst.StreamPath + configFile + ".txt"; } } MConfig NetInfo; // 初始化 获取本地的数据 public IEnumerator Init() { if (File.Exists(configFilePath)) { NetInfo = new MConfig(File.ReadAllText(configFilePath, System.Text.Encoding.UTF8)); } else { string url = AppConst.FilePathEx + configStreamFilePath; XDebug.Log.l("[ConfigLoader Start ]@" + url); WWW _www = new WWW(url); yield return _www; if (string.IsNullOrEmpty(_www.error)) { XDebug.Log.l("[ConfigLoader OK ]=" + _www.text); NetInfo = new MConfig(_www.text); } else { Debug.LogError("[ConfigLoader ERR ]=" + _www.error); } } yield return null; } public void SetNetInfo(string info) { try { NetInfo = new MConfig(info); SaveToFiles(); } catch (Exception e) { Debug.LogError(e); } } /// /// 保存到文件 /// void SaveToFiles() { string directoryPath = Path.GetDirectoryName(configFilePath); if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath); File.WriteAllText(configFilePath, NetInfo.ToJson(), System.Text.Encoding.UTF8); } /// /// /// /// /// public string GetConfigInfo(string key) { string v = GetConfigNetInfo(key); if (v != null) return v; return null; } public string GetConfigNetInfo(string key) { if (NetInfo == null) return null; string v = NetInfo.GetInfo(key); return v; } // 判断设置是否激活 public Boolean IsSettingActive(string settingType) { string s_isActive = "Setting." + settingType + ".isActive"; string s_versionCode = "Setting." + settingType + ".versionCode"; string isActive = this.GetConfigInfo(s_isActive); if (isActive != null && isActive.Equals("1")) { if (!AppConst.isSDK) { return true; } int vc = AndroidDeviceInfo.Instance.GetVersionCode(); if (vc >= Convert.ToInt32(this.GetConfigInfo(s_versionCode))) { //符合包版本 return true; } } return false; } public string GetSettingValue(string settingType) { if (this.IsSettingActive(settingType)) { string s_value = "Setting." + settingType + ".value"; string value = this.GetConfigInfo(s_value); return value; } return null; } } }