using ProtoCSStruct; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace Sog { public class GameConfigMgr : Singleton { private Dictionary m_allConfigMgr = new Dictionary(); private string filePath = "../cfg/data/"; public bool m_isBin = true; //有些配置只有一些服务器需要,需要调用接口关闭 private Dictionary m_filterCongfigs = new Dictionary(); public GameConfigMgr() { IsBin(); if (m_isBin) { DescHelper.RegisterAllBin(m_allConfigMgr); } else { //RegisterAll(); } } public void SetFilePath(string path) { filePath = path; } private bool IsBin() { string cfgPath = "../cfg/sog/ExcleFormat.txt"; if(!File.Exists(cfgPath)) { TraceLog.Debug("Config of bin is not Exists!"); return false; } string cfgText = File.ReadAllText(cfgPath); if (cfgText.Split(":").Length > 1) { int isBin = Convert.ToInt32(cfgText.Split(":")[1]); if (isBin == 1) { m_isBin = true; } } return false; } public void ReadAllConfig(bool isReadFilter = false) { foreach (KeyValuePair pair in m_allConfigMgr) { if (m_filterCongfigs.TryGetValue(pair.Key, out bool isFilter)) { if (isFilter || !isReadFilter) //在过滤列表里,不主动读取或者开启过滤就不读取 { continue; } } string fullPath = filePath + pair.Key; if (ReadConfig(pair.Value, pair.Key, fullPath) != 0) { TraceLog.Error("GameConfigMgr.ReadConfig desc file {0} read failed", pair.Key); } } ReadAllComplete(); } //public void ReadAllConfig() //{ // ReadAllConfig(false); //} public void SetFilterConfig(string name, bool isFilter) { if (string.IsNullOrEmpty(name)) { TraceLog.Error("GameConfigMgr.SetFilterConfig name null"); return; } if (m_filterCongfigs.ContainsKey(name)) { m_filterCongfigs[name] = isFilter; } else { TraceLog.Error("GameConfigMgr.SetFilterConfig {0} no in filter list", name); } } public void ReadAllConfig(params string[] fullPath) { foreach (KeyValuePair pair in m_allConfigMgr) { if (fullPath.Length > 0) { for (int i = 0; i < fullPath.Length; i++) { string fullPathStr = fullPath[i] + pair.Key; if (!File.Exists(fullPathStr)) { continue; } else if (ReadConfig(pair.Value, pair.Key, fullPathStr) != 0) { TraceLog.Error("GameConfigMgr.ReadConfig desc file {0} read failed", pair.Key); } } } } } public void ReadSomeConfig(List checkList, params string[] fullPath) { foreach (KeyValuePair pair in m_allConfigMgr) { if(!checkList.Contains(pair.Key)) { continue; } if (fullPath.Length > 0) { for (int i = 0; i < fullPath.Length; i++) { string fullPathStr = fullPath[i] + pair.Key; if (!File.Exists(fullPathStr)) { continue; } else if (ReadConfig(pair.Value, pair.Key, fullPathStr) != 0) { TraceLog.Error("GameConfigMgr.ReadConfig desc file {0} read failed", pair.Key); } } } } } public int CheckAllConfig(List filenameList) { int count = 0; foreach (var filename in filenameList) { if (m_allConfigMgr.ContainsKey(filename) == false) { ++count; TraceLog.Error("GameConfigMgr.CheckAllConfig desc file {0} not register", filename); } } return count; } public void ReadOneConfig(string filename) { IConfigManager configMgr = null; if (false == m_allConfigMgr.TryGetValue(filename, out configMgr)) { string errorMsg = string.Format("GameConfigMgr.ReadConfig desc file {0} not in config file list ", filename); TraceLog.Error(errorMsg); if (OSUtils.IsWindows()) { System.Threading.Thread.Sleep(2000); throw new Exception(errorMsg); } return; } string fullPath = filePath + filename; ReadConfig(configMgr, filename, fullPath); } private int ReadConfig(IConfigManager configMgr, string filename, string fullPath) { bool ret; //--**-- 添加配置 是否为 bin --**-- if (m_isBin) { if (!File.Exists(fullPath)) { TraceLog.Error("GameConfigMgr.ReadConfig desc file {0} not exists, fullPath:{1}", filename, fullPath); return -1; } byte[] content = File.ReadAllBytes(fullPath); ret = configMgr.InitBin(filename, content); //configMgr.InitJson(filename); } else { if (!File.Exists(fullPath)) { TraceLog.Error("GameConfigMgr.ReadConfig desc file {0} not exists, fullPath:{1}", filename, fullPath); return -1; } string content = File.ReadAllText(fullPath); ret = configMgr.Init(filename, content); } if (ret == false) { TraceLog.Error("GameConfigMgr.ReadConfig desc file {0} read failed", filename); return -1; } configMgr.ReadComplete(); if (configMgr.GetErrorCount() > 0) { if (OSUtils.IsWindows()) { string errorMsg = string.Format("GameConfigMgr.ReadConfig desc file {0} error count {1} ", filename, configMgr.GetErrorCount()); TraceLog.Error(errorMsg); System.Threading.Thread.Sleep(2000); throw new Exception(errorMsg); } return -1; } TraceLog.Debug("GameConfigMgr.ReadConfig desc file {0} read success", filename); return 0; } public void ReloadConfig(IConfigManager configMgr) { string fullPath = string.Empty; foreach (var item in m_allConfigMgr) { if (item.Value == configMgr) { fullPath = filePath + item.Key; TraceLog.Debug("GameConfigMgr.ReloadConfig desc file {0} reload begin", item.Key); ReadConfig(configMgr, item.Key, fullPath); break; } } } public IConfigManager GetConfigMgr(string name) { m_allConfigMgr.TryGetValue(name,out var config); return config; } public void ReadAllComplete() { foreach (KeyValuePair pair in m_allConfigMgr) { pair.Value.ReadAllComplete(); } } public List GetConfigNames() { return m_allConfigMgr.Keys.ToList(); } } }