GameCfg.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * 文件: GameCfg.cs
  3. * 由SharpDevelop创建。
  4. * 作者: gwang
  5. *
  6. * 功能: 游戏配置类,加载游戏配置文件并对外提供一个Json对象,供各个模块访问
  7. * 版本:
  8. * 1.0.0 Created by gwang - 2016/6/27 11:31
  9. */
  10. using System;
  11. using UnityEngine;
  12. using System.IO;
  13. using Newtonsoft.Json.Linq;
  14. using CSharpUtil;
  15. /// <summary>
  16. /// 游戏配置类.
  17. /// </summary>
  18. public class GameCfg
  19. {
  20. /// <summary>
  21. /// 初始化
  22. /// </summary>
  23. static public void Init(Action ac)
  24. {
  25. _InitGameCfg(ac);
  26. }
  27. #region ' 本地设置 '
  28. private static JObject _localsettings;
  29. public static bool DownloadIsOver = false;
  30. public static bool CloseServer = false;
  31. public static string StrCloseServerReason = "例行维护";
  32. public static long startts = 0;
  33. public static long endts = 0;
  34. public static string NoticeImageURL = "";
  35. public static void _InitLocalSettigs()
  36. {
  37. if (!File.Exists(Const.LocalConfigFileName))
  38. {
  39. _localsettings = JObject.Parse("{}");
  40. SaveLocalSettings();
  41. }
  42. }
  43. /// <summary>
  44. /// 本地设置
  45. /// </summary>
  46. public static JObject LocalSettings
  47. {
  48. get
  49. {
  50. if (null == _localsettings)
  51. {
  52. _InitLocalSettigs();
  53. using (StreamReader sr = new StreamReader(Const.LocalConfigFileName))
  54. {
  55. _localsettings = JObject.Parse(sr.ReadToEnd());
  56. }
  57. }
  58. return _localsettings;
  59. }
  60. set
  61. {
  62. _localsettings = value;
  63. }
  64. }
  65. /// <summary>
  66. /// 回存本地设置
  67. /// </summary>
  68. public static void SaveLocalSettings()
  69. {
  70. using (StreamWriter sw = new StreamWriter(Const.LocalConfigFileName))
  71. {
  72. sw.Write(_localsettings.ToString());
  73. }
  74. }
  75. #endregion
  76. #region ' 游戏配置 '
  77. static private JObject cfg;
  78. static JObject Cfg
  79. {
  80. get
  81. {
  82. if (null == cfg)
  83. {
  84. if (File.Exists(Const.ServerConfigFileName))
  85. {
  86. using StreamReader sr = new StreamReader(Const.ServerConfigFileName);
  87. cfg = JObject.Parse(Base64Util.Decode(sr.ReadToEnd(), true));
  88. }
  89. }
  90. return cfg;
  91. }
  92. }
  93. static void _InitGameCfg(Action ac)
  94. {
  95. var ver = null == Cfg ? "0" : Cfg["ver"].ToString();
  96. UserProxy.Instance.DownLoadGameConfig(ver, jobj =>
  97. {
  98. if (null != jobj)
  99. {
  100. cfg = jobj; // 更新下配置对象
  101. }
  102. GameConfigData.Init(Cfg);
  103. if (GameConfigData.IsReady)
  104. {
  105. if (GameConfigData.Ins.service_schedule.ContainsKey(1))
  106. {
  107. StrCloseServerReason = GameConfigData.Ins.service_schedule[1].reason;
  108. var ts = UserProxy.Instance.GetCurrentUnixTimeStamp();
  109. if (ts > GameConfigData.Ins.service_schedule[1].startts
  110. && ts < GameConfigData.Ins.service_schedule[1].endts)
  111. {
  112. CloseServer = true; // 这个控制是否处在停机维护状态
  113. }
  114. }
  115. NoticeImageURL = GameConfigData.Ins.globalsettings.Game_PublicNotice.ToString();
  116. }
  117. DownloadIsOver = true;
  118. ac?.Invoke();
  119. });
  120. }
  121. #endregion
  122. #region ' 多语言处理 '
  123. /// <summary>
  124. /// 根据语言类型,获取本地文本显示的具体信息
  125. /// </summary>
  126. /// <param name="tid"></param>
  127. ///
  128. /// <returns></returns>
  129. static public string GetStr(String tid)
  130. {
  131. if (string.IsNullOrEmpty(tid))
  132. return "";
  133. string result = "";
  134. GameSeting_Language language = mySettingLanguage;
  135. JObject xx = Cfg["localtexts"][tid] as JObject;
  136. if (xx != null)
  137. {
  138. //LogHelper.Log(xx.h);
  139. foreach (JProperty key in xx.Properties())
  140. {
  141. if (key.Name == language.ToString())
  142. {
  143. result = key.Value.Value<string>();
  144. break;
  145. }
  146. }
  147. }
  148. return result;
  149. }
  150. /// <summary>
  151. /// 检查语言库中是否存在某个key
  152. /// </summary>
  153. /// <param name="key"></param>
  154. /// <returns></returns>
  155. static public bool CheckIsExistKey(string key)
  156. {
  157. if (Cfg == null || string.IsNullOrEmpty(key))
  158. {
  159. return false;
  160. }
  161. if (!Cfg.HasValues)
  162. {
  163. return false;
  164. }
  165. JToken local;
  166. if (!Cfg.TryGetValue("localtexts", out local))
  167. {
  168. return false;
  169. }
  170. JObject xx = Cfg["localtexts"][key] as JObject;
  171. if (xx != null)
  172. {
  173. return true;
  174. }
  175. return false;
  176. }
  177. static public GameSeting_Language mySettingLanguage = GameSeting_Language.zh;
  178. #endregion
  179. }
  180. public enum GameSeting_Language
  181. {
  182. zh,
  183. en,
  184. jp
  185. }