LanguagePackConf.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using PublicTools.XMLDataBase;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using UnityEngine;
  7. using XRTool.UI;
  8. namespace XRTool.Util
  9. {
  10. /// <summary>
  11. /// 语言包的配置
  12. /// </summary>
  13. public class LanguagePackConf
  14. {
  15. private string language = "简体中文";
  16. private string packagePath;
  17. /// 语言包的名称
  18. /// </summary>
  19. public string Language { get => language; set => language = value; }
  20. /// <summary>
  21. /// 语言包的路径
  22. /// </summary>
  23. public string PackagePath { get => packagePath; set => packagePath = value; }
  24. public Dictionary<string, LanguageConf> LanguageMap
  25. {
  26. get
  27. {
  28. if (languageMap == null)
  29. {
  30. languageMap = new Dictionary<string, LanguageConf>();
  31. }
  32. return languageMap;
  33. }
  34. }
  35. public Dictionary<string, LanguageConf> ChineseMap
  36. {
  37. get
  38. {
  39. if (chineseMap == null)
  40. {
  41. chineseMap = new Dictionary<string, LanguageConf>();
  42. }
  43. return chineseMap;
  44. }
  45. }
  46. /// <summary>
  47. /// 语言文本映射
  48. /// </summary>
  49. private Dictionary<string, LanguageConf> languageMap;
  50. /// <summary>
  51. /// 语言文本映射
  52. /// </summary>
  53. private Dictionary<string, LanguageConf> chineseMap;
  54. public event Action<LanguagePackConf> OnReadComplete;
  55. /// <summary>
  56. /// 新旧编号
  57. /// </summary>
  58. public static Dictionary<string, string> numMap;
  59. public static string NULL = "null";
  60. /// <summary>
  61. /// 读取语言包,异步操作,读完后有对应的数据
  62. /// </summary>
  63. public void ReadLanguageMap()
  64. {
  65. LanguageMap.Clear();
  66. ChineseMap.Clear();
  67. string path = Path.Combine(Application.streamingAssetsPath, "Language", packagePath);
  68. DataConfMgr<LanguageConf>.Instance.OpenData(path, true);
  69. DataConfMgr<LanguageConf>.Instance.TableHelper.readConfComplete -= OnReadConfComplete;
  70. DataConfMgr<LanguageConf>.Instance.TableHelper.readConfComplete += OnReadConfComplete;
  71. DataConfMgr<LanguageConf>.Instance.OpenTable("PriKey");
  72. }
  73. private void OnReadConfComplete()
  74. {
  75. InitLanguage(DataConfMgr<LanguageConf>.Instance.TableHelper.DataList);
  76. OnReadComplete?.Invoke(this);
  77. }
  78. public void InitLanguage(List<LanguageConf> list)
  79. {
  80. LanguageMap.Clear();
  81. ChineseMap.Clear();
  82. bool isInitNum = (numMap == null);
  83. if (isInitNum)
  84. {
  85. numMap = new Dictionary<string, string>();
  86. }
  87. if (list != null)
  88. {
  89. for (int i = 0; i < list.Count; i++)
  90. {
  91. if (!LanguageMap.ContainsKey(list[i].PriKey))
  92. {
  93. LanguageMap.Add(list[i].PriKey, list[i]);
  94. string[] strs = list[i].AudioPath.Split(';');
  95. if (isInitNum && !string.IsNullOrEmpty(list[i].AudioPath) && list[i].AudioPath != NULL)
  96. {
  97. for (int j = 0; j < strs.Length; j++)
  98. {
  99. if (strs[j].Contains("T"))
  100. {
  101. if (!numMap.ContainsKey(strs[j]))
  102. {
  103. numMap.Add(strs[j], list[i].PriKey);
  104. }
  105. }
  106. else
  107. {
  108. if (!numMap.ContainsKey("T" + strs[j]))
  109. {
  110. numMap.Add("T"+strs[j], list[i].PriKey);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. if (!ChineseMap.ContainsKey(list[i].Message))
  117. {
  118. ChineseMap.Add(list[i].Message, list[i]);
  119. }
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// 获取对应的语言文本的配置
  125. ///
  126. /// </summary>
  127. /// <param name="prikey"></param>
  128. public LanguageConf GetLanguage(string prikey)
  129. {
  130. if (!string.IsNullOrEmpty(prikey))
  131. {
  132. if (LanguageMap.ContainsKey(prikey))
  133. {
  134. return LanguageMap[prikey];
  135. }
  136. else if (ChineseMap.ContainsKey(prikey))
  137. {
  138. return ChineseMap[prikey];
  139. }
  140. else if (numMap != null && numMap.ContainsKey(prikey))
  141. {
  142. return GetLanguage(numMap[prikey]);
  143. }
  144. }
  145. //UnityLog.LogError("GetLanguage=>Error:" + prikey);
  146. return null;
  147. }
  148. }
  149. }