using System;
using Games.GlobeDefine;
using Module.Log;
using UnityEngine;
namespace GCGame.Table
{
public class StrDictionary
{
///
/// 替换字符串里面的$R,$G,$N等特殊字符,注意是字符串,多为剧情表格、任务等定制
/// 例子:
/// string clientStr = "各种中文字符等"; //这里通常是读剧情表格,里面是直接配置的中文
/// string dicStr = StrDictionary.GetClientString_WithNameSex(clientStr);
///
///
///
public static string GetClientString_WithNameSex(string ClientStr)
{
if (string.IsNullOrEmpty(ClientStr)) return "";
var goodNameID = GlobeVar.INVALID_ID;
var badNameID = GlobeVar.INVALID_ID;
if (LoginData.m_sbIsMale)
{
goodNameID = 1387;
badNameID = 1389;
}
else
{
goodNameID = 1388;
badNameID = 1390;
}
var goodName = "";
var pGoodNameDictionary = TableManager.GetStrDictionaryByID(goodNameID, 0);
if (pGoodNameDictionary != null)
goodName = pGoodNameDictionary.StrDictionary;
else
goodName = "";
var badName = "";
var pbadNameDictionary = TableManager.GetStrDictionaryByID(badNameID, 0);
if (pbadNameDictionary != null)
badName = pbadNameDictionary.StrDictionary;
else
badName = "";
var strFullDic = ClientStr.Replace("$R", goodName);
strFullDic = strFullDic.Replace("$G", badName);
strFullDic = strFullDic.Replace("$N", LoginData.m_sRoleName);
strFullDic = strFullDic.Replace("#r", "\n"); //支持换行符
return strFullDic;
}
///
/// 解析$R,$G,$N等特殊字符的字典解析函数,除非确定要用,否则使用GetClientDictionaryString函数
/// 例子:
/// string dicStr = StrDictionary.GetClientDictionaryString_WithNameSex("#{2344}", element1, element2, element3 ... );
/// //其中element1等是字符串或者整数
///
///
/// ClientDictionaryStr为客户端字典号,例如#{5676}
///
///
///
public static string GetClientDictionaryString_WithNameSex(string ClientDictionaryStr, params object[] args)
{
var strFullDic = GetClientDictionaryString(ClientDictionaryStr, args);
return GetClientString_WithNameSex(strFullDic);
}
///
/// 返回翻译完的字典内容,只能用于客户端本地字典的解析
/// 使用例子:
/// string dicStr = StrDictionary.GetClientDictionaryString("#{2344}", element1, element2, element3 ... );
/// //其中element1等是字符串或者整数
/// 或者
/// string dicStr2 = StrDictionary.GetClientDictionaryString("#{2344}"); //直接在客户端写字典号
///
///
/// ClientDictionaryStr为客户端字典号,例如#{5676}
///
/// 变参内容,要依据字典中填写的{x}来确定有多少个内容,如果不对应,会报错(解析出的也是乱的);
///
/// 返回翻译完的字典内容,可以直接显示
public static string GetClientDictionaryString(string ClientDictionaryStr, params object[] args)
{
if (string.IsNullOrEmpty(ClientDictionaryStr)) return "EmptyString -- ClientDictionary ERROR0";
if (ClientDictionaryStr.Length < 3) return ClientDictionaryStr + " -- ClientDictionary ERROR1 Length < 3";
var dicIdStr = ClientDictionaryStr.Substring(2, ClientDictionaryStr.Length - 3);
var dicID = int.Parse(dicIdStr);
var pDictionary = TableManager.GetStrDictionaryByID(dicID, 0);
if (pDictionary != null)
{
string full;
if (args.Length > 0)
{
full = GetActDictionaryString(pDictionary.StrDictionary, args);
if (string.IsNullOrEmpty(full))
full = pDictionary.StrDictionary;
full = string.Format(full, args);
}
else
{
full = pDictionary.StrDictionary;
}
return full.Replace("#r", "\n"); //支持换行符
}
return ClientDictionaryStr + " -- ClientDictionary ERROR2";
}
// 增加一种活动的类型字符串的解析
// 服务器可能会发送多个活动消息到一条信息中
// 所以增加循环解析
public static string GetActDictionaryString(string pDictionaryStr, params object[] args)
{
// 安全计数
var saveCount = 0;
var maxCount = pDictionaryStr.Length;
while (pDictionaryStr.Contains("[act,"))
{
++saveCount;
if (saveCount > maxCount)
{
LogModule.ErrorLog(pDictionaryStr + "-- ClientDictionary ERROR, Endless looping");
return "";
}
var index = pDictionaryStr.IndexOf("[act,");
var i = index + 5;
var len = 0;
for (; i < pDictionaryStr.Length; i++)
if (pDictionaryStr[i] != ']')
len++;
else
break;
var act = pDictionaryStr.Substring(index + 5, len);
var actIdIndex = -1;
var actId = -1;
if (int.TryParse(act, out actIdIndex))
if (args.Length > actIdIndex)
if (int.TryParse((string) args[actIdIndex], out actId))
{
var activityBaseDic = TableManager.GetActivityBase().Values;
foreach (var d in activityBaseDic)
if (d.ActivityServerType == actId)
{
args[actIdIndex] = d.ActivityName;
var replace = string.Format("[act,{0}]", actIdIndex);
pDictionaryStr = pDictionaryStr.Replace(replace,
string.Format("{0}{1}{2}", "{", actIdIndex, "}"));
break;
}
}
}
try
{
pDictionaryStr = string.Format(pDictionaryStr, args);
}
catch (Exception e)
{
Debug.LogError(e);
}
return pDictionaryStr.Replace("#r", "\n"); //支持换行符
}
///
/// 解析由服务器函数DictionaryFormat::FormatDictionary()返回的包含字典的字符串。
/// 返回翻译完的字典内容,只能用于从服务器发过来字典的解析
/// 使用例子:
/// string serverSendedStr = "#{12345}*hello*newWorld";
/// //Server发过来的字符串,由Server端的DictionaryFormat::FormatDictionary()生成的。
/// 或者
/// string serverSendedStr2 = "#{12345}"; //Server发过来的字符串,就是一个字典号
/// string dicStr = StrDictionary.GetServerDictionaryFormatString(serverSendedStr);
/// string dicStr2 = StrDictionary.GetServerDictionaryFormatString(serverSendedStr2);
///
///
/// ServerSendedDictionaryStr为服务器发过来的包含字典号的字符串
/// 格式举例如下:
/// #{12345}*hello*newWorld
/// 其中字典#{12345}的内容为:{0}{1}
/// 上面的两个变量字符串分别为hello,newWorld
/// 解析完整个字典串返回值为:hello newWorld
///
/// 返回翻译完的字典内容,可以直接显示
public static string GetServerDictionaryFormatString(string ServerSendedDictionaryStr) //#{12345}*hello*newWorld
{
if (string.IsNullOrEmpty(ServerSendedDictionaryStr)) return "EmptyString -- ServerDictionaryFormat ERROR0";
var firstChar = ServerSendedDictionaryStr[0];
if (firstChar != '#')
return ServerSendedDictionaryStr + " -- ServerDictionaryFormat ERROR1 FirstChar is not #";
var dicEndPos = ServerSendedDictionaryStr.IndexOf('*');
if (dicEndPos > 0) //#{12345}*hello*newWorld 这种格式
{
var dictionaryStr = ServerSendedDictionaryStr.Substring(0, dicEndPos);
var elementStr =
ServerSendedDictionaryStr.Substring(dicEndPos + 1,
ServerSendedDictionaryStr.Length - dicEndPos - 1);
var allElements = elementStr.Split('*');
return GetClientDictionaryString(dictionaryStr, allElements);
}
if (ServerSendedDictionaryStr.Length < 3)
return ServerSendedDictionaryStr + " -- ServerDictionaryFormat ERROR2 Length < 3";
var dicIdStr = ServerSendedDictionaryStr.Substring(2, ServerSendedDictionaryStr.Length - 3);
var dicID = -1;
int.TryParse(dicIdStr, out dicID);
var pDictionary = TableManager.GetStrDictionaryByID(dicID, 0);
if (pDictionary != null)
return pDictionary.StrDictionary.Replace("#r", "\n");
//return pDictionary.StrDictionary;
return ServerSendedDictionaryStr + " -- ServerDictionaryFormat ERROR3";
}
///
/// 检测由服务器函数DictionaryFormat::FormatDictionary()字典是否可以在客户端解析,返回true或false。
///
///
///
/// 返回true或false
public static bool IsServerDictionaryFormatString(string ServerSendedDictionaryStr) //#{12345}*hello*newWorld
{
if (string.IsNullOrEmpty(ServerSendedDictionaryStr)) return false;
var firstChar = ServerSendedDictionaryStr[0];
if (firstChar != '#') return false;
var dicEndPos = ServerSendedDictionaryStr.IndexOf('*');
if (dicEndPos > 0) //#{12345}*hello*newWorld 这种格式
{
var dictionaryStr = ServerSendedDictionaryStr.Substring(0, dicEndPos);
var elementStr =
ServerSendedDictionaryStr.Substring(dicEndPos + 1,
ServerSendedDictionaryStr.Length - dicEndPos - 1);
var allElements = elementStr.Split('*');
return IsClientDictionaryString(dictionaryStr, allElements);
}
if (ServerSendedDictionaryStr.Length < 3) return false;
var dicIdStr = ServerSendedDictionaryStr.Substring(2, ServerSendedDictionaryStr.Length - 3);
var dicID = int.Parse(dicIdStr);
var pDictionary = TableManager.GetStrDictionaryByID(dicID, 0);
if (pDictionary != null) return true;
return false;
}
/// ///
///
/// 检测客户端本地字典的解析,返回true或false。
///
///
///
/// 返回true或false
public static bool IsClientDictionaryString(string ClientDictionaryStr, params object[] args)
{
if (string.IsNullOrEmpty(ClientDictionaryStr)) return false;
if (ClientDictionaryStr.Length < 3) return false;
var dicIdStr = ClientDictionaryStr.Substring(2, ClientDictionaryStr.Length - 3);
var dicID = int.Parse(dicIdStr);
var pDictionary = TableManager.GetStrDictionaryByID(dicID, 0);
if (pDictionary != null) return true; //支持换行符
return false;
}
public static string GetClientStrMoneyType(MONEYTYPE moneyType)
{
switch (moneyType)
{
case MONEYTYPE.MONEYTYPE_COIN:
return GetClientDictionaryString("#{10390}");
case MONEYTYPE.MONEYTYPE_COIN_BIND:
return GetClientDictionaryString("#{10391}");
case MONEYTYPE.MONEYTYPE_YUANBAO:
return GetClientDictionaryString("#{10388}");
case MONEYTYPE.MONEYTYPE_YUANBAO_BIND:
return GetClientDictionaryString("#{10389}");
}
return "";
}
}
}