ConfigMgr.cs 20 KB

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