Main/Assets/Code/Logic/Chat/ChatParse.cs

204 lines
7.0 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using Thousandto.Code.Center;
using Thousandto.Plugins.Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Thousandto.Core.RootSystem;
using Thousandto.Core.Base;
using System;
using Thousandto.Cfg.Data;
namespace Thousandto.Code.Logic
{
/// <summary>
/// UIEntergameForm的逻辑处理类
/// </summary>
public class ChatParse
{
/// <summary>
/// 解析接收到的字符串
/// </summary>
/// <param name="content">接收到的字符串</param>
/// <returns>解析后生成的节点用于创建对应的控件如UILabel、UISprite</returns>
public static List<ChatNode> ParseContent(String content, ulong specialId = 0, bool isPrivateChatNode = false)
{
List<ChatNode> nodes = new List<ChatNode>();
//以&开头是GM命令直接跳过解析
if (!content.StartsWith("<t="))
{
ChatNode node = new ChatNode();
node.isPrivateChatNode = isPrivateChatNode;
node.Type = (int)TNodeType.Text;
node.Param1 = content;
nodes.Add(node);
return nodes;
}
try
{
// node style
// ("<t={0}>{1},{2},{3}</t>", Type, Param3, Param2, Param1);
const string flag_s = "<t=";
const string flag_e = "</t>";
int s_p = 0;
int e_p = 0;
string tmp = "";
while (s_p < content.Length)
{
s_p = content.IndexOf(flag_s, e_p);
e_p = content.IndexOf(flag_e, s_p);
e_p += flag_e.Length; // + "</t>"
if (s_p < e_p)
{
ChatNode node = new ChatNode();
node.isPrivateChatNode = isPrivateChatNode;
node.specialId = specialId;
// chunk <t=type>p3,p2,p1</t>
tmp = content.Substring(s_p, e_p - s_p);
ParseNode(tmp, flag_s, flag_e, ref node);
nodes.Add(node);
}//if (s_p < e_p)
s_p = e_p + flag_e.Length; // reset start position
} // while(s_p < content.Length)
}
catch (System.Exception ex)
{
UnityEngine.Debug.LogError(ex.Message);
nodes.Clear();
ChatNode node = new ChatNode();
node.isPrivateChatNode = isPrivateChatNode;
node.Type = (int)TNodeType.Text;
node.Param1 = content;
nodes.Add(node);
}
return nodes;
}
/// <summary>
/// 解析单个字符串节点 如 <t=type>p3,p2,p1</t>
/// </summary>
/// <param name="tmp"></param>
/// <param name="flag_s"></param>
/// <param name="flag_e"></param>
/// <param name="node"></param>
static void ParseNode(string tmp, string flag_s, string flag_e, ref ChatNode node)
{
if (node != null && tmp.Length > 0)
{
int len = 0;
//<t=type>
int type_e = tmp.IndexOf(">", flag_s.Length);
if (type_e > flag_s.Length && type_e < tmp.Length)
{
//real text
len = type_e - flag_s.Length;
if (len >= 0)
{
string type = tmp.Substring(flag_s.Length, len);
node.Type = Convert.ToInt32(type);
}
else
{
node.Type = (int)TNodeType.Text;
}
}
// >p3,p2,p1<
int p3_e = tmp.IndexOf(",", type_e + 1); // ">"
if (p3_e < tmp.Length)
{
//real text
len = p3_e - type_e - 1;// 1 ">"
string p3 = len <= 0 ? "" : tmp.Substring(type_e + 1, len);
node.Param3 = p3;//Convert.ToUInt64(p2);
}
// >p3,p2,p1<
int p2_e = tmp.IndexOf(",", p3_e + 1); // ","
if (p2_e < tmp.Length)
{
//real text
len = p2_e - p3_e - 1; // 1 ">"
string p2 = len <= 0 ? "" : tmp.Substring(p3_e + 1, len);
node.Param2 = p2;//Convert.ToUInt64(p2);
}
// >p3,p2,p1<
{
//real text
len = tmp.Length - flag_e.Length - (p2_e + 1); // 1 ","
node.Param1 = len <= 0 ? "" : tmp.Substring(p2_e + 1, len);
}
switch (node.Type)
{
case (int)TNodeType.Equipment:
node.ItemParam = ItemBase.ToItemBase(node.Param3);
if (node.ItemParam != null)
{
node.Param1 = DeclareMessageString.Format(DeclareMessageString.C_CHAT_ITEMEQUIP_LINK, node.ItemParam.Name);
}
break;
case (int)TNodeType.Item:
int itemId = 0;
if(int.TryParse(node.Param2, out itemId))
{
node.ItemParam = ItemBase.CreateItemBase(itemId, true);
if (node.ItemParam != null)
{
node.Param1 = DeclareMessageString.Format(DeclareMessageString.C_CHAT_ITEMEQUIP_LINK, node.ItemParam.Name);
}
}
else
{
node.Param1 = string.Empty;
}
break;
case (int)TNodeType.Function:
node.Param1 = GameCenter.LanguageConvertSystem.ConvertLan(node.Param1);
break;
}
}
}
/// <summary>
/// 将结构转成纯字符串,便于发送
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
public static string GetTransferContent(List<ChatNode> msg, bool isPrivateChatNode = false)
{
string content = "";
for (int i = 0; i < msg.Count; ++i)
{
if (isPrivateChatNode)
{
if (msg[i].isPrivateChatNode)
{
content += msg[i].GetTransferText();
}
}
else
{
content += msg[i].GetTransferText();
}
}
return content;
}
public void OnInputChange()
{
}
}
}