You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.4 KiB
172 lines
5.4 KiB
/*
|
|
Sog 游戏基础库
|
|
2016 by zouwei
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Runtime.Serialization.Json;
|
|
|
|
using System.IO;
|
|
using System.Text;
|
|
using LitJson;
|
|
|
|
namespace Sog
|
|
{
|
|
public static class JsonConfig
|
|
{
|
|
public static void InitLitJson()
|
|
{
|
|
//扩展支持下 string 到 int 的转换, 类似这种 "intkey" : "1", json的标准应该是 "intkey" : 1
|
|
static int ImporterStringInt(string obj)
|
|
{
|
|
return int.Parse(obj);
|
|
}
|
|
|
|
JsonMapper.RegisterImporter<string, int>((ImporterFunc<string, int>)ImporterStringInt);
|
|
}
|
|
|
|
public static T parse<T>(string jsonString)
|
|
{
|
|
var set = new System.Runtime.Serialization.Json.DataContractJsonSerializerSettings();
|
|
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
|
|
{
|
|
return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(ms);
|
|
}
|
|
}
|
|
|
|
public static T parseFile<T>(string filePathName)
|
|
{
|
|
string jsonString = File.ReadAllText(filePathName);
|
|
|
|
return parse<T>(jsonString);
|
|
}
|
|
|
|
|
|
public static T parseLitJson<T>(string jsonString)
|
|
{
|
|
JsonReader reader = new JsonReader(jsonString);
|
|
|
|
//允许注释
|
|
reader.AllowComments = true;
|
|
|
|
return JsonMapper.ToObject<T>(reader);
|
|
}
|
|
|
|
public static T parseFileLitJson<T>(string filePathName)
|
|
{
|
|
string jsonString = File.ReadAllText(filePathName);
|
|
if(String.IsNullOrEmpty(jsonString))
|
|
{
|
|
Console.WriteLine("JsonConfig.parseFileLitJson read json file {0} failed!", filePathName);
|
|
}
|
|
|
|
return parseLitJson<T>(jsonString);
|
|
}
|
|
|
|
public static string stringify(object jsonObject)
|
|
{
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
new DataContractJsonSerializer(jsonObject.GetType()).WriteObject(ms, jsonObject);
|
|
return Encoding.UTF8.GetString(ms.ToArray());
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 进行变量替换,规则是用$作为开始和结尾的字符串为变量,$port$表示一个变量,port为变量名
|
|
/// 变量根据cluster的配置进行替换,上层服务器可以不关心这个变量定义规则,这里统一处理了就可以了
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="app"></param>
|
|
/// <param name="filePathName"></param>
|
|
/// <returns></returns>
|
|
public static T ParseAppConfigFileAndReplaceVariable<T>(ServerApp app, string filePathName)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
string[] lines = File.ReadAllLines(filePathName);
|
|
|
|
for(int i=0; i< lines.Length; i++)
|
|
{
|
|
string line = lines[i];
|
|
|
|
line = ReplaceOneLineVariable(app, line);
|
|
|
|
sb.Append(line);
|
|
sb.Append('\n');
|
|
}
|
|
|
|
string jsonString = sb.ToString();
|
|
if (jsonString.IndexOf('$') != -1)
|
|
{
|
|
TraceLog.Error("JsonConfig.ParseAppConfigFileAndReplaceVariable params not config");
|
|
}
|
|
|
|
return parseLitJson<T>(jsonString);
|
|
}
|
|
|
|
private static string ReplaceOneLineVariable(ServerApp app, string line)
|
|
{
|
|
//一行最多5个变量,有必要超过5个变量这么复杂吗
|
|
for (int i=0; i<5; i++)
|
|
{
|
|
//2个$符号表示一个变量
|
|
int pos1 = line.IndexOf('$');
|
|
|
|
if (pos1 == -1)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//结尾
|
|
if (pos1 >= line.Length - 1)
|
|
{
|
|
break;
|
|
}
|
|
|
|
int pos2 = line.IndexOf('$', pos1 + 1);
|
|
|
|
if (pos2 == -1)
|
|
{
|
|
break;
|
|
}
|
|
|
|
string varNameWithBeginEndDollar = line.Substring(pos1, pos2 - pos1 + 1);
|
|
string varName = varNameWithBeginEndDollar.Replace("$", "");
|
|
string varValue = "";
|
|
|
|
|
|
if (varName.Length > 0)
|
|
{
|
|
//这里写上一些固定规则
|
|
//比如 instid,id,serverid,appid,apptype
|
|
if (varName == "instid")
|
|
{
|
|
uint instid = ServerIDUtils.GetInstanceID(app.ServerID);
|
|
varValue = instid.ToString();
|
|
}
|
|
else if (varName == "id" || varName == "serverid" || varName == "appid")
|
|
{
|
|
varValue = app.StrServerID;
|
|
}
|
|
else if(varName == "apptype")
|
|
{
|
|
int apptype = ServerIDUtils.GetServerType(app.ServerID);
|
|
varValue = apptype.ToString();
|
|
}
|
|
else
|
|
{
|
|
varValue = app.GetCluster().GetAppParamByKey(varName);
|
|
}
|
|
}
|
|
|
|
line = line.Replace(varNameWithBeginEndDollar, varValue);
|
|
}
|
|
|
|
return line;
|
|
}
|
|
}
|
|
}
|
|
|