123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- /*
- * 文件: GameCfg.cs
- * 由SharpDevelop创建。
- * 作者: gwang
- *
- * 功能: 游戏配置类,加载游戏配置文件并对外提供一个Json对象,供各个模块访问
- * 版本:
- * 1.0.0 Created by gwang - 2016/6/27 11:31
- */
- using System;
- using UnityEngine;
- using System.IO;
- using Newtonsoft.Json.Linq;
- using CSharpUtil;
- /// <summary>
- /// 游戏配置类.
- /// </summary>
- public class GameCfg
- {
- /// <summary>
- /// 初始化
- /// </summary>
- static public void Init(Action ac)
- {
- _InitGameCfg(ac);
- }
- #region ' 本地设置 '
- private static JObject _localsettings;
- public static bool DownloadIsOver = false;
- public static bool CloseServer = false;
- public static string StrCloseServerReason = "例行维护";
- public static long startts = 0;
- public static long endts = 0;
- public static string NoticeImageURL = "";
- public static void _InitLocalSettigs()
- {
- if (!File.Exists(Const.LocalConfigFileName))
- {
- _localsettings = JObject.Parse("{}");
- SaveLocalSettings();
- }
- }
- /// <summary>
- /// 本地设置
- /// </summary>
- public static JObject LocalSettings
- {
- get
- {
- if (null == _localsettings)
- {
- _InitLocalSettigs();
- using (StreamReader sr = new StreamReader(Const.LocalConfigFileName))
- {
- _localsettings = JObject.Parse(sr.ReadToEnd());
- }
- }
- return _localsettings;
- }
- set
- {
- _localsettings = value;
- }
- }
- /// <summary>
- /// 回存本地设置
- /// </summary>
- public static void SaveLocalSettings()
- {
- using (StreamWriter sw = new StreamWriter(Const.LocalConfigFileName))
- {
- sw.Write(_localsettings.ToString());
- }
- }
- #endregion
- #region ' 游戏配置 '
- static private JObject cfg;
- static JObject Cfg
- {
- get
- {
- if (null == cfg)
- {
- if (File.Exists(Const.ServerConfigFileName))
- {
- using StreamReader sr = new StreamReader(Const.ServerConfigFileName);
- cfg = JObject.Parse(Base64Util.Decode(sr.ReadToEnd(), true));
- }
- }
- return cfg;
- }
- }
- static void _InitGameCfg(Action ac)
- {
- var ver = null == Cfg ? "0" : Cfg["ver"].ToString();
- UserProxy.Instance.DownLoadGameConfig(ver, jobj =>
- {
- if (null != jobj)
- {
- cfg = jobj; // 更新下配置对象
- }
- GameConfigData.Init(Cfg);
- if (GameConfigData.IsReady)
- {
- if (GameConfigData.Ins.service_schedule.ContainsKey(1))
- {
- StrCloseServerReason = GameConfigData.Ins.service_schedule[1].reason;
- var ts = UserProxy.Instance.GetCurrentUnixTimeStamp();
- if (ts > GameConfigData.Ins.service_schedule[1].startts
- && ts < GameConfigData.Ins.service_schedule[1].endts)
- {
- CloseServer = true; // 这个控制是否处在停机维护状态
- }
- }
- NoticeImageURL = GameConfigData.Ins.globalsettings.Game_PublicNotice.ToString();
- }
- DownloadIsOver = true;
- ac?.Invoke();
- });
- }
- #endregion
- #region ' 多语言处理 '
- /// <summary>
- /// 根据语言类型,获取本地文本显示的具体信息
- /// </summary>
- /// <param name="tid"></param>
- ///
- /// <returns></returns>
- static public string GetStr(String tid)
- {
- if (string.IsNullOrEmpty(tid))
- return "";
- string result = "";
- GameSeting_Language language = mySettingLanguage;
- JObject xx = Cfg["localtexts"][tid] as JObject;
- if (xx != null)
- {
- //LogHelper.Log(xx.h);
- foreach (JProperty key in xx.Properties())
- {
- if (key.Name == language.ToString())
- {
- result = key.Value.Value<string>();
- break;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 检查语言库中是否存在某个key
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- static public bool CheckIsExistKey(string key)
- {
- if (Cfg == null || string.IsNullOrEmpty(key))
- {
- return false;
- }
- if (!Cfg.HasValues)
- {
- return false;
- }
- JToken local;
- if (!Cfg.TryGetValue("localtexts", out local))
- {
- return false;
- }
- JObject xx = Cfg["localtexts"][key] as JObject;
- if (xx != null)
- {
- return true;
- }
- return false;
- }
- static public GameSeting_Language mySettingLanguage = GameSeting_Language.zh;
- #endregion
- }
- public enum GameSeting_Language
- {
- zh,
- en,
- jp
- }
|