/*
* 文件: 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;
///
/// 游戏配置类.
///
public class GameCfg
{
///
/// 初始化
///
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();
}
}
///
/// 本地设置
///
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;
}
}
///
/// 回存本地设置
///
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 ' 多语言处理 '
///
/// 根据语言类型,获取本地文本显示的具体信息
///
///
///
///
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();
break;
}
}
}
return result;
}
///
/// 检查语言库中是否存在某个key
///
///
///
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
}