ConfigMgr.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class ConfigMgr : Singleton<ConfigMgr>
  5. {
  6. private int linestart = 1;
  7. private int rowstart = 1;
  8. private string splitword = ",";
  9. private static bool m_bInitCSVFinished = false;
  10. private static bool m_bInitXMLFinished = false;
  11. public static bool InitFinished
  12. {
  13. get { return m_bInitCSVFinished && m_bInitXMLFinished; }
  14. }
  15. private static string m_strCurLangKey = LangKeys.ChinaSimplifiedFull;
  16. public static string CurLangKey
  17. {
  18. get { return m_strCurLangKey; }
  19. set
  20. {
  21. if (m_strCurLangKey != value)
  22. {
  23. //m_bInitCSVFinished = false;
  24. m_strCurLangKey = value;
  25. //I18N
  26. }
  27. }
  28. }
  29. public Dictionary<string, Dictionary<string, Dictionary<string, string>>> ConfigDictionary;
  30. public Dictionary<string, int> ConfigLongthDictionary;
  31. private Dictionary<string, string> mXmlConfigDict;
  32. public Dictionary<string, string> XmlConfigDict
  33. {
  34. get { return mXmlConfigDict; }
  35. }
  36. public override void Init()
  37. {
  38. base.Init();
  39. m_bInitCSVFinished = false;
  40. m_bInitXMLFinished = false;
  41. ConfigDictionary = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>(19);
  42. ConfigLongthDictionary = new Dictionary<string, int>(19);
  43. mXmlConfigDict = new Dictionary<string, string>();
  44. GetConfigAsset();
  45. }
  46. public override void UnInit()
  47. {
  48. if (LaunchThread.HasInstance())
  49. {
  50. LaunchThread.Instance.Stop(LaunchThread.AsyncLaunchState.ConfigInit);
  51. }
  52. base.UnInit();
  53. }
  54. private void GetConfigAsset()
  55. {
  56. if (InitFinished)
  57. return;
  58. ResourceMgr.Instance.LoadDirAsset<List<TextAsset>>(OnLoadXmlCallback, Constants.XmlConfig);
  59. ResourceMgr.Instance.LoadDirAsset<List<TextAsset>>(OnCallBack, Constants.CsvConfig);
  60. }
  61. public void ResetKeywords()
  62. {
  63. if (InitFinished)
  64. return;
  65. string m_tableName = string.Format("LanguagePackage{0}", ConfigMgr.CurLangKey);
  66. if (ConfigDictionary != null && ConfigDictionary.ContainsKey(m_tableName))
  67. {
  68. m_bInitCSVFinished = true;
  69. DebugHelper.Log(" [ConfigMgr] getKeywordsConfigAsset");
  70. }
  71. else
  72. {
  73. ResourceMgr.Instance.LoadAsset<List<TextAsset>>(OnResetKeywordsCallBack, Constants.CsvConfig, ELoadType.UI, m_tableName);
  74. }
  75. }
  76. void OnCallBack(List<TextAsset> objs, string path_, string[] assetNames_)
  77. {
  78. if (objs == null || objs.Count <= 0)
  79. {
  80. EventMgr.DispatchEvent(new CoreEvent<int>(ECoreEventType.EID_ConfigMgrInit, 0));
  81. return;
  82. }
  83. if (LaunchThread.HasInstance())
  84. {
  85. string keyword = "LanguagePackage";
  86. string keywordname = string.Format("{0}{1}", keyword, CurLangKey);
  87. string curname = string.Empty;
  88. TextAsset tx;
  89. List<string> names = new List<string>();
  90. List<string> contents = new List<string>();
  91. for (int i = 0; i < objs.Count; ++i)
  92. {
  93. tx = objs[i];
  94. curname = tx.name;
  95. //Debug.Log($"Config Name = [{curname}]");
  96. if (/*curname.Contains(keyword) && !curname.Equals(keywordname) ||*/ tx.text.Length <= 0)
  97. continue;
  98. names.Add(curname);
  99. contents.Add(tx.text);
  100. }
  101. keyword = string.Empty;
  102. keywordname = string.Empty;
  103. curname = string.Empty;
  104. System.Action action = () =>
  105. {
  106. for (int i = 0, iMax = names.Count; i < iMax; i++)
  107. {
  108. curname = names[i];
  109. Dictionary<string, Dictionary<string, string>> ts = getData(curname, contents[i]);
  110. if (ConfigDictionary.ContainsKey(curname))
  111. {
  112. ConfigDictionary[curname] = ts;
  113. }
  114. else
  115. {
  116. ConfigDictionary.Add(curname, ts);
  117. }
  118. }
  119. m_bInitCSVFinished = true;
  120. };
  121. LaunchThread.Instance.Start(LaunchThread.AsyncLaunchState.ConfigInit, action, CheckCfgOk, null);
  122. }
  123. else
  124. {
  125. string keyword = "LanguagePackage";
  126. string keywordname = string.Format("{0}{1}", keyword, CurLangKey);
  127. string curname = string.Empty;
  128. TextAsset tx;
  129. for (int i = 0; i < objs.Count; ++i)
  130. {
  131. tx = objs[i];
  132. curname = tx.name;
  133. if (/*curname.Contains(keyword) && !curname.Equals(keywordname) || */tx.text.Length <= 0)
  134. continue;
  135. Dictionary<string, Dictionary<string, string>> ts = getData(curname, tx.text);
  136. if (ConfigDictionary.ContainsKey(curname))
  137. {
  138. ConfigDictionary[curname] = ts;
  139. }
  140. else
  141. {
  142. ConfigDictionary.Add(curname, ts);
  143. }
  144. }
  145. m_bInitCSVFinished = true;
  146. keyword = string.Empty;
  147. keywordname = string.Empty;
  148. curname = string.Empty;
  149. tx = null;
  150. CheckCfgOk();
  151. }
  152. }
  153. void OnLoadXmlCallback(List<TextAsset> objs, string path_, string[] assetNames_)
  154. {
  155. TextAsset tx;
  156. for (int i = 0; i < objs.Count; ++i)
  157. {
  158. tx = objs[i];
  159. mXmlConfigDict.Add(tx.name, tx.text);
  160. }
  161. m_bInitXMLFinished = true;
  162. CheckCfgOk();
  163. }
  164. void CheckCfgOk()
  165. {
  166. if(InitFinished)
  167. {
  168. EventMgr.DispatchEvent(new CoreEvent<int>(ECoreEventType.EID_ConfigMgrInit, 1));
  169. }
  170. }
  171. void OnResetKeywordsCallBack(List<TextAsset> objs, string assetPath_, string[] assetNames_)
  172. {
  173. if (objs == null || objs.Count <= 0)
  174. {
  175. DebugHelper.Log("No Config Data AssetBundle");
  176. return;
  177. }
  178. string keyword = "LanguagePackage";
  179. string keywordname = string.Format("{0}{1}", keyword, CurLangKey);
  180. TextAsset tx;
  181. string curname = string.Empty;
  182. for (int i = 0; i < objs.Count; ++i)
  183. {
  184. tx = objs[i];
  185. curname = tx.name;
  186. if (/*curname.Contains(keyword) && !curname.Equals(keywordname) ||*/ tx.text.Length <= 0)
  187. continue;
  188. Dictionary<string, Dictionary<string, string>> ts = getData(curname, tx.text);
  189. if (ConfigDictionary != null)
  190. {
  191. if (ConfigDictionary.ContainsKey(curname))
  192. {
  193. ConfigDictionary[curname] = ts;
  194. }
  195. else
  196. {
  197. ConfigDictionary.Add(curname, ts);
  198. }
  199. }
  200. }
  201. m_bInitCSVFinished = true;
  202. }
  203. private Dictionary<string, Dictionary<string, string>> getData(string name, string content)
  204. {
  205. string[] lineArray;
  206. string[] charArray = null;
  207. // string text = content.Replace("\r", "@");
  208. // lineArray = text.Split("@"[0]);
  209. System.IO.StringReader reader = new System.IO.StringReader(content);
  210. string str = reader.ReadLine();
  211. List<string> tempList = new List<string>();
  212. while (!string.IsNullOrEmpty(str))
  213. {
  214. tempList.Add(str);
  215. str = reader.ReadLine();
  216. }
  217. lineArray = tempList.ToArray();
  218. //DebugHelper.LogWarning("[TableName: {0}]lineArray {1}",name, lineArray[0]);
  219. string[] indexname = null;
  220. if (name.Contains("Language"))
  221. {
  222. indexname = FileHelper.SpiltData(lineArray[1]);
  223. }
  224. else
  225. {
  226. indexname = lineArray[1].Split(splitword[0]);
  227. }
  228. Dictionary<string, Dictionary<string, string>> Table = new Dictionary<string, Dictionary<string, string>>(lineArray.Length);
  229. int linelength = 0;
  230. for (int i = linestart; i < lineArray.Length; ++i)
  231. {
  232. if (name.Contains("Language"))
  233. {
  234. charArray = FileHelper.SpiltData(lineArray[i]);
  235. }
  236. else
  237. {
  238. charArray = lineArray[i].Split(splitword[0]);
  239. }
  240. linelength = charArray.Length;
  241. if (linelength > 0 && charArray[0] == string.Empty)
  242. {
  243. //DebugHelper.LogWarning("[ConfigMgr. getData] Empty Index {0} {1}", name, i);
  244. continue;
  245. }
  246. Dictionary<string, string> Row = new Dictionary<string, string>(linelength);
  247. for (int j = rowstart; j < linelength; ++j)
  248. {
  249. if (j == rowstart && indexname[j] == string.Empty)
  250. {
  251. DebugHelper.LogWarning("[ConfigMgr. getData] Empty key {0} {1}", name, j);
  252. continue;
  253. }
  254. #if UNITY_EDITOR
  255. if (j >= indexname.Length)
  256. {
  257. DebugHelper.LogError("{0},Split error in {1}", name, charArray[0]);
  258. }
  259. #endif
  260. if (Row.ContainsKey(indexname[j]))
  261. {
  262. //DebugHelper.LogWarning("[ConfigMgr. getData] {0} {1}", name, indexname[j]);
  263. continue;
  264. }
  265. Row.Add(indexname[j], charArray[j]);
  266. }
  267. if (Table.ContainsKey(charArray[0]))
  268. {
  269. //DebugHelper.LogWarning("[ConfigMgr. getData] {0} {1} {2}" , name , i, lineArray[i-1]);
  270. continue;
  271. }
  272. Table.Add(charArray[0], Row);
  273. }
  274. if (ConfigLongthDictionary != null && !ConfigLongthDictionary.ContainsKey(name))
  275. {
  276. //int length = lineArray.Length - (linestart + 1);
  277. int length = lineArray.Length - (linestart);
  278. ConfigLongthDictionary.Add(name, length);
  279. }
  280. return Table;
  281. }
  282. public Dictionary<string, Dictionary<string, string>> getTable(string tablename)
  283. {
  284. if (!InitFinished || ConfigDictionary == null)
  285. return null;
  286. Dictionary<string, Dictionary<string, string>> ts;
  287. ConfigDictionary.TryGetValue(tablename, out ts);
  288. if (ts != null && ts.Count > 0)
  289. return ts;
  290. else
  291. {
  292. DebugHelper.LogWarning("[ConfigMgr] {0} cant find", tablename);
  293. return null;
  294. }
  295. }
  296. public string GetXmlCfg(string fileName)
  297. {
  298. string cfg;
  299. mXmlConfigDict.TryGetValue(fileName, out cfg);
  300. return cfg;
  301. }
  302. public int getTableLength(string tablename)
  303. {
  304. if (!InitFinished)
  305. return -1;
  306. if (ConfigLongthDictionary.ContainsKey(tablename))
  307. return ConfigLongthDictionary[tablename];
  308. return 0;
  309. }
  310. public Dictionary<string, string> getLine(int id, string tablename)
  311. {
  312. return getLine(id.ToString(), tablename);
  313. }
  314. public Dictionary<string, string> getLine(string id, string tablename)
  315. {
  316. if (!InitFinished) {
  317. Debug.LogError("配置未加载!!!");
  318. return null;
  319. }
  320. Dictionary<string, Dictionary<string, string>> ts = getTable(tablename);
  321. if (ts != null && ts.ContainsKey(id))
  322. {
  323. return ts[id];
  324. }
  325. else
  326. return null;
  327. }
  328. public string getValue(int id, string keyname, string tablename)
  329. {
  330. return getValue(id.ToString(), keyname, tablename);
  331. }
  332. public string getValue(string id, string keyname, string tablename)
  333. {
  334. if (!InitFinished)
  335. return null;
  336. Dictionary<string, Dictionary<string, string>> ts = getTable(tablename);
  337. //Log.E("id {0} {1} {2}",id,keyname,tablename);
  338. if (ts != null && ts.ContainsKey(id) && ts[id].ContainsKey(keyname))
  339. {
  340. return ts[id][keyname];
  341. }
  342. else
  343. return null;
  344. }
  345. }
  346. public class I18N
  347. {
  348. static string m_tableName = "";
  349. static string m_curLangKey = "";
  350. static string tableName
  351. {
  352. get
  353. {
  354. if (string.IsNullOrEmpty(m_tableName) || m_curLangKey != ConfigMgr.CurLangKey)
  355. {
  356. m_tableName = string.Format("LanguagePackage{0}", ConfigMgr.CurLangKey);
  357. m_curLangKey = ConfigMgr.CurLangKey;
  358. }
  359. return m_tableName;
  360. }
  361. }
  362. public static string T(string key)
  363. {
  364. string result = key;
  365. if (key == null)
  366. {
  367. DebugHelper.LogWarning("[ I18N.T ] keyword is null!!!");
  368. return "";
  369. }
  370. if (ConfigMgr.HasInstance())
  371. {
  372. if (!ConfigMgr.InitFinished)
  373. {
  374. ConfigMgr.Instance.ResetKeywords();
  375. m_tableName = string.Empty;
  376. }
  377. string value = ConfigMgr.Instance.getValue(key, "Language", tableName);
  378. if (value != null)
  379. {
  380. result = value.Replace("\\n", "\n");
  381. }
  382. }
  383. return result;
  384. }
  385. public static string SetLanguageValue(string key, params string[] param)
  386. {
  387. if (param == null || param.Length == 0)
  388. {
  389. return T(key);
  390. }
  391. string result = key;
  392. if (key == null)
  393. {
  394. DebugHelper.LogWarning("[ I18N.T ] keyword is null!!!");
  395. return "";
  396. }
  397. if (ConfigMgr.HasInstance())
  398. {
  399. if (!ConfigMgr.InitFinished)
  400. {
  401. ConfigMgr.Instance.ResetKeywords();
  402. m_tableName = string.Empty;
  403. }
  404. string value = ConfigMgr.Instance.getValue(key, "Language", tableName);
  405. if (value != null)
  406. {
  407. result = value.Replace("\\n", "\n");
  408. result = string.Format(result, param);
  409. }
  410. else
  411. {
  412. DebugHelper.LogWarning("[ I18N.T ]ConfigMgr.cant find the key or keyword is null!!! " + key);
  413. }
  414. }
  415. return result;
  416. }
  417. }
  418. public class I18NOpera
  419. {
  420. static string m_tableName = "";
  421. static string tableName
  422. {
  423. get
  424. {
  425. if (string.IsNullOrEmpty(m_tableName))
  426. m_tableName = string.Format("OperaKeywords{0}", ConfigMgr.CurLangKey);
  427. return m_tableName;
  428. }
  429. }
  430. public static string T(int key)
  431. {
  432. string result = key.ToString();
  433. if (ConfigMgr.HasInstance())
  434. {
  435. if (!ConfigMgr.InitFinished)
  436. {
  437. ConfigMgr.Instance.ResetKeywords();
  438. m_tableName = string.Empty;
  439. }
  440. result = ConfigMgr.Instance.getValue(key, "info", tableName);
  441. result = result.Replace("\\n", "\n");//��������
  442. //if (value != null)
  443. //{
  444. // result = value.Replace("|", "\r\n");
  445. //}
  446. //else
  447. //{
  448. // DebugHelper.LogWarning("[ I18NOpera.T ]ConfigMgr.cant find the key or keyword is null!!!");
  449. //}
  450. }
  451. return result;
  452. }
  453. }
  454. public class I18NOperaBranch
  455. {
  456. static string m_tableName = "";
  457. static string tableName
  458. {
  459. get
  460. {
  461. if (string.IsNullOrEmpty(m_tableName))
  462. m_tableName = string.Format("OperaBranchwords{0}", ConfigMgr.CurLangKey);
  463. return m_tableName;
  464. }
  465. }
  466. public static string T(int key)
  467. {
  468. string result = key.ToString();
  469. if (ConfigMgr.HasInstance())
  470. {
  471. if (!ConfigMgr.InitFinished)
  472. {
  473. ConfigMgr.Instance.ResetKeywords();
  474. m_tableName = string.Empty;
  475. }
  476. result = ConfigMgr.Instance.getValue(key, "info", tableName);
  477. if (result == null)
  478. {
  479. DebugHelper.LogError(string.Format("in {0},can not find value by key :{1}", tableName, key));
  480. }
  481. result = result.Replace("\\n", "\n");//��������
  482. }
  483. return result;
  484. }
  485. }
  486. public class EditorConfigCSV
  487. {
  488. private string m_tableName = "";
  489. public string TableName
  490. {
  491. get
  492. {
  493. return m_tableName;
  494. }
  495. }
  496. private int linestart = 1;
  497. private int rowstart = 1;
  498. private string splitword = ",";
  499. private Dictionary<string, Dictionary<string, string>> m_tableData;
  500. public string T(string key, string filed, string tableName_)
  501. {
  502. m_tableName = tableName_;
  503. string result = key;
  504. #if UNITY_EDITOR
  505. string value = GetValue(key, filed);
  506. if (value != null)
  507. {
  508. result = value.Replace("\\n", "\n");
  509. }
  510. #endif //UNITY_EDITOR
  511. return result;
  512. }
  513. public string GetValue(string id, string keyName_, string tableName_ = null)
  514. {
  515. #if UNITY_EDITOR
  516. if (m_tableData == null)
  517. {
  518. if (tableName_ != null)
  519. m_tableName = tableName_;
  520. string path = string.Format("{0}/{1}.csv", Constants.CsvConfig, TableName);
  521. TextAsset tx = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path) as TextAsset;
  522. m_tableData = GetData(tx);
  523. }
  524. if (id == null)
  525. {
  526. return null;
  527. }
  528. if (m_tableData.ContainsKey(id) && m_tableData[id].ContainsKey(keyName_))
  529. {
  530. return m_tableData[id][keyName_];
  531. }
  532. #endif //UNITY_EDITOR
  533. return null;
  534. }
  535. public List<string> GetId2Key(string keyName_, string keyValue_, string tableName_ = null)
  536. {
  537. List<string> idList = new List<string>();
  538. #if UNITY_EDITOR
  539. if (m_tableData == null)
  540. {
  541. if (tableName_ != null)
  542. m_tableName = tableName_;
  543. string path = string.Format("{0}/{1}.csv", Constants.CsvConfig, TableName);
  544. TextAsset tx = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path) as TextAsset;
  545. m_tableData = GetData(tx);
  546. }
  547. foreach (var dic in m_tableData)
  548. {
  549. if (dic.Value[keyName_] == keyValue_)
  550. {
  551. idList.Add(dic.Key);
  552. }
  553. }
  554. #endif //UNITY_EDITOR
  555. return idList;
  556. }
  557. private Dictionary<string, Dictionary<string, string>> GetData(Object obj)
  558. {
  559. if (obj == null)
  560. return null;
  561. string[] lineArray;
  562. string[] charArray;
  563. TextAsset binAsset = obj as TextAsset;
  564. string text = binAsset.text.Replace("\r\n", "@");
  565. lineArray = text.Split("@"[0]);
  566. //DebugHelper.LogWarning("[TableName: {0}]lineArray {1}",binAsset.name, lineArray[0]);
  567. string[] indexname = lineArray[1].Split(splitword[0]);
  568. Dictionary<string, Dictionary<string, string>> Table = new Dictionary<string, Dictionary<string, string>>();
  569. for (int i = linestart; i < lineArray.Length; ++i)
  570. {
  571. charArray = lineArray[i].Split(splitword[0]);
  572. if (charArray.Length > 0 && charArray[0] == string.Empty)
  573. {
  574. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty Index {0} {1}", binAsset.name, i);
  575. continue;
  576. }
  577. Dictionary<string, string> Row = new Dictionary<string, string>();
  578. for (int j = rowstart; j < charArray.Length; ++j)
  579. {
  580. if (indexname[j] == "" || indexname[j] == string.Empty)
  581. {
  582. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty key {0} {1}", binAsset.name, j);
  583. continue;
  584. }
  585. if (Row.ContainsKey(indexname[j]))
  586. {
  587. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1}", binAsset.name, indexname[j]);
  588. continue;
  589. }
  590. Row.Add(indexname[j], charArray[j]);
  591. }
  592. if (Table.ContainsKey(charArray[0]))
  593. {
  594. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1} {2}", binAsset.name, i, lineArray[i - 1]);
  595. continue;
  596. }
  597. Table.Add(charArray[0], Row);
  598. }
  599. return Table;
  600. }
  601. }