ConfigMgr.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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;
  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 = lineArray[1].Split(splitword[0]);
  220. Dictionary<string, Dictionary<string, string>> Table = new Dictionary<string, Dictionary<string, string>>(lineArray.Length);
  221. int linelength = 0;
  222. for (int i = linestart; i < lineArray.Length; ++i)
  223. {
  224. charArray = lineArray[i].Split(splitword[0]);
  225. linelength = charArray.Length;
  226. if (linelength > 0 && charArray[0] == string.Empty)
  227. {
  228. //DebugHelper.LogWarning("[ConfigMgr. getData] Empty Index {0} {1}", name, i);
  229. continue;
  230. }
  231. Dictionary<string, string> Row = new Dictionary<string, string>(linelength);
  232. for (int j = rowstart; j < linelength; ++j)
  233. {
  234. if (j == rowstart && indexname[j] == string.Empty)
  235. {
  236. DebugHelper.LogWarning("[ConfigMgr. getData] Empty key {0} {1}", name, j);
  237. continue;
  238. }
  239. #if UNITY_EDITOR
  240. if (j >= indexname.Length)
  241. {
  242. DebugHelper.LogError("{0},Split error in {1}", name, charArray[0]);
  243. }
  244. #endif
  245. if (Row.ContainsKey(indexname[j]))
  246. {
  247. //DebugHelper.LogWarning("[ConfigMgr. getData] {0} {1}", name, indexname[j]);
  248. continue;
  249. }
  250. Row.Add(indexname[j], charArray[j]);
  251. }
  252. if (Table.ContainsKey(charArray[0]))
  253. {
  254. //DebugHelper.LogWarning("[ConfigMgr. getData] {0} {1} {2}" , name , i, lineArray[i-1]);
  255. continue;
  256. }
  257. Table.Add(charArray[0], Row);
  258. }
  259. if (ConfigLongthDictionary != null && !ConfigLongthDictionary.ContainsKey(name))
  260. {
  261. //int length = lineArray.Length - (linestart + 1);
  262. int length = lineArray.Length - (linestart);
  263. ConfigLongthDictionary.Add(name, length);
  264. }
  265. return Table;
  266. }
  267. public Dictionary<string, Dictionary<string, string>> getTable(string tablename)
  268. {
  269. if (!InitFinished || ConfigDictionary == null)
  270. return null;
  271. Dictionary<string, Dictionary<string, string>> ts;
  272. ConfigDictionary.TryGetValue(tablename, out ts);
  273. if (ts != null && ts.Count > 0)
  274. return ts;
  275. else
  276. {
  277. DebugHelper.LogWarning("[ConfigMgr] {0} cant find", tablename);
  278. return null;
  279. }
  280. }
  281. public string GetXmlCfg(string fileName)
  282. {
  283. string cfg;
  284. mXmlConfigDict.TryGetValue(fileName, out cfg);
  285. return cfg;
  286. }
  287. public int getTableLength(string tablename)
  288. {
  289. if (!InitFinished)
  290. return -1;
  291. if (ConfigLongthDictionary.ContainsKey(tablename))
  292. return ConfigLongthDictionary[tablename];
  293. return 0;
  294. }
  295. public Dictionary<string, string> getLine(int id, string tablename)
  296. {
  297. return getLine(id.ToString(), tablename);
  298. }
  299. public Dictionary<string, string> getLine(string id, string tablename)
  300. {
  301. if (!InitFinished) {
  302. Debug.LogError("配置未加载!!!");
  303. return null;
  304. }
  305. Dictionary<string, Dictionary<string, string>> ts = getTable(tablename);
  306. if (ts != null && ts.ContainsKey(id))
  307. {
  308. return ts[id];
  309. }
  310. else
  311. return null;
  312. }
  313. public string getValue(int id, string keyname, string tablename)
  314. {
  315. return getValue(id.ToString(), keyname, tablename);
  316. }
  317. public string getValue(string id, string keyname, string tablename)
  318. {
  319. if (!InitFinished)
  320. return null;
  321. Dictionary<string, Dictionary<string, string>> ts = getTable(tablename);
  322. //Log.E("id {0} {1} {2}",id,keyname,tablename);
  323. if (ts != null && ts.ContainsKey(id) && ts[id].ContainsKey(keyname))
  324. {
  325. return ts[id][keyname];
  326. }
  327. else
  328. return null;
  329. }
  330. }
  331. public class I18N
  332. {
  333. static string m_tableName = "";
  334. static string m_curLangKey = "";
  335. static string tableName
  336. {
  337. get
  338. {
  339. if (string.IsNullOrEmpty(m_tableName) || m_curLangKey != ConfigMgr.CurLangKey)
  340. {
  341. m_tableName = string.Format("LanguagePackage{0}", ConfigMgr.CurLangKey);
  342. m_curLangKey = ConfigMgr.CurLangKey;
  343. }
  344. return m_tableName;
  345. }
  346. }
  347. public static string T(string key)
  348. {
  349. string result = key;
  350. if (key == null)
  351. {
  352. DebugHelper.LogWarning("[ I18N.T ] keyword is null!!!");
  353. return "";
  354. }
  355. if (ConfigMgr.HasInstance())
  356. {
  357. if (!ConfigMgr.InitFinished)
  358. {
  359. ConfigMgr.Instance.ResetKeywords();
  360. m_tableName = string.Empty;
  361. }
  362. string value = ConfigMgr.Instance.getValue(key, "Language", tableName);
  363. if (value != null)
  364. {
  365. result = value.Replace("\\n", "\n");
  366. }
  367. }
  368. return result;
  369. }
  370. public static string SetLanguageValue(string key, params string[] param)
  371. {
  372. if (param == null || param.Length == 0)
  373. {
  374. return T(key);
  375. }
  376. string result = key;
  377. if (key == null)
  378. {
  379. DebugHelper.LogWarning("[ I18N.T ] keyword is null!!!");
  380. return "";
  381. }
  382. if (ConfigMgr.HasInstance())
  383. {
  384. if (!ConfigMgr.InitFinished)
  385. {
  386. ConfigMgr.Instance.ResetKeywords();
  387. m_tableName = string.Empty;
  388. }
  389. string value = ConfigMgr.Instance.getValue(key, "Language", tableName);
  390. if (value != null)
  391. {
  392. result = value.Replace("\\n", "\n");
  393. result = string.Format(result, param);
  394. }
  395. else
  396. {
  397. DebugHelper.LogWarning("[ I18N.T ]ConfigMgr.cant find the key or keyword is null!!! " + key);
  398. }
  399. }
  400. return result;
  401. }
  402. }
  403. public class I18NOpera
  404. {
  405. static string m_tableName = "";
  406. static string tableName
  407. {
  408. get
  409. {
  410. if (string.IsNullOrEmpty(m_tableName))
  411. m_tableName = string.Format("OperaKeywords{0}", ConfigMgr.CurLangKey);
  412. return m_tableName;
  413. }
  414. }
  415. public static string T(int key)
  416. {
  417. string result = key.ToString();
  418. if (ConfigMgr.HasInstance())
  419. {
  420. if (!ConfigMgr.InitFinished)
  421. {
  422. ConfigMgr.Instance.ResetKeywords();
  423. m_tableName = string.Empty;
  424. }
  425. result = ConfigMgr.Instance.getValue(key, "info", tableName);
  426. result = result.Replace("\\n", "\n");//��������
  427. //if (value != null)
  428. //{
  429. // result = value.Replace("|", "\r\n");
  430. //}
  431. //else
  432. //{
  433. // DebugHelper.LogWarning("[ I18NOpera.T ]ConfigMgr.cant find the key or keyword is null!!!");
  434. //}
  435. }
  436. return result;
  437. }
  438. }
  439. public class I18NOperaBranch
  440. {
  441. static string m_tableName = "";
  442. static string tableName
  443. {
  444. get
  445. {
  446. if (string.IsNullOrEmpty(m_tableName))
  447. m_tableName = string.Format("OperaBranchwords{0}", ConfigMgr.CurLangKey);
  448. return m_tableName;
  449. }
  450. }
  451. public static string T(int key)
  452. {
  453. string result = key.ToString();
  454. if (ConfigMgr.HasInstance())
  455. {
  456. if (!ConfigMgr.InitFinished)
  457. {
  458. ConfigMgr.Instance.ResetKeywords();
  459. m_tableName = string.Empty;
  460. }
  461. result = ConfigMgr.Instance.getValue(key, "info", tableName);
  462. if (result == null)
  463. {
  464. DebugHelper.LogError(string.Format("in {0},can not find value by key :{1}", tableName, key));
  465. }
  466. result = result.Replace("\\n", "\n");//��������
  467. }
  468. return result;
  469. }
  470. }
  471. public class EditorConfigCSV
  472. {
  473. private string m_tableName = "";
  474. public string TableName
  475. {
  476. get
  477. {
  478. return m_tableName;
  479. }
  480. }
  481. private int linestart = 1;
  482. private int rowstart = 1;
  483. private string splitword = ",";
  484. private Dictionary<string, Dictionary<string, string>> m_tableData;
  485. public string T(string key, string filed, string tableName_)
  486. {
  487. m_tableName = tableName_;
  488. string result = key;
  489. #if UNITY_EDITOR
  490. string value = GetValue(key, filed);
  491. if (value != null)
  492. {
  493. result = value.Replace("\\n", "\n");
  494. }
  495. #endif //UNITY_EDITOR
  496. return result;
  497. }
  498. public string GetValue(string id, string keyName_, string tableName_ = null)
  499. {
  500. #if UNITY_EDITOR
  501. if (m_tableData == null)
  502. {
  503. if (tableName_ != null)
  504. m_tableName = tableName_;
  505. string path = string.Format("{0}/{1}.csv", Constants.CsvConfig, TableName);
  506. TextAsset tx = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path) as TextAsset;
  507. m_tableData = GetData(tx);
  508. }
  509. if (id == null)
  510. {
  511. return null;
  512. }
  513. if (m_tableData.ContainsKey(id) && m_tableData[id].ContainsKey(keyName_))
  514. {
  515. return m_tableData[id][keyName_];
  516. }
  517. #endif //UNITY_EDITOR
  518. return null;
  519. }
  520. public List<string> GetId2Key(string keyName_, string keyValue_, string tableName_ = null)
  521. {
  522. List<string> idList = new List<string>();
  523. #if UNITY_EDITOR
  524. if (m_tableData == null)
  525. {
  526. if (tableName_ != null)
  527. m_tableName = tableName_;
  528. string path = string.Format("{0}/{1}.csv", Constants.CsvConfig, TableName);
  529. TextAsset tx = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>(path) as TextAsset;
  530. m_tableData = GetData(tx);
  531. }
  532. foreach (var dic in m_tableData)
  533. {
  534. if (dic.Value[keyName_] == keyValue_)
  535. {
  536. idList.Add(dic.Key);
  537. }
  538. }
  539. #endif //UNITY_EDITOR
  540. return idList;
  541. }
  542. private Dictionary<string, Dictionary<string, string>> GetData(Object obj)
  543. {
  544. if (obj == null)
  545. return null;
  546. string[] lineArray;
  547. string[] charArray;
  548. TextAsset binAsset = obj as TextAsset;
  549. string text = binAsset.text.Replace("\r\n", "@");
  550. lineArray = text.Split("@"[0]);
  551. //DebugHelper.LogWarning("[TableName: {0}]lineArray {1}",binAsset.name, lineArray[0]);
  552. string[] indexname = lineArray[1].Split(splitword[0]);
  553. Dictionary<string, Dictionary<string, string>> Table = new Dictionary<string, Dictionary<string, string>>();
  554. for (int i = linestart; i < lineArray.Length; ++i)
  555. {
  556. charArray = lineArray[i].Split(splitword[0]);
  557. if (charArray.Length > 0 && charArray[0] == string.Empty)
  558. {
  559. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty Index {0} {1}", binAsset.name, i);
  560. continue;
  561. }
  562. Dictionary<string, string> Row = new Dictionary<string, string>();
  563. for (int j = rowstart; j < charArray.Length; ++j)
  564. {
  565. if (indexname[j] == "" || indexname[j] == string.Empty)
  566. {
  567. DebugHelper.LogWarning("[ConfigMgr. GetData] Empty key {0} {1}", binAsset.name, j);
  568. continue;
  569. }
  570. if (Row.ContainsKey(indexname[j]))
  571. {
  572. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1}", binAsset.name, indexname[j]);
  573. continue;
  574. }
  575. Row.Add(indexname[j], charArray[j]);
  576. }
  577. if (Table.ContainsKey(charArray[0]))
  578. {
  579. DebugHelper.LogWarning("[ConfigMgr. GetData] {0} {1} {2}", binAsset.name, i, lineArray[i - 1]);
  580. continue;
  581. }
  582. Table.Add(charArray[0], Row);
  583. }
  584. return Table;
  585. }
  586. }