using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
|
using UnityEngine;
|
public class HrefAnalysis : TRichAnalysis<HrefAnalysis>
|
{
|
public static readonly Regex HrefRegex = new Regex(@"<a>(.*?)</a>", RegexOptions.Singleline);
|
|
private RichTextMgr.HrefInfo presentHrefInfo = null;
|
|
private static StringBuilder textBuilder = new StringBuilder();
|
|
static int insertIndex = 0;
|
|
public override string Analysis(string val, bool IsRich)
|
{
|
if (!HrefRegex.IsMatch(val))
|
{
|
return val;
|
}
|
int index = 0;
|
m_StringBuilder.Length = 0;
|
foreach (Match match in HrefRegex.Matches(val))
|
{
|
textBuilder.Length = 0;
|
RichTextMgr.HrefInfo hrefInfo = new RichTextMgr.HrefInfo();
|
presentHrefInfo = hrefInfo;
|
if (RichTextMgr.Inst.presentRichText != null)
|
{
|
presentHrefInfo.unlineColor = UIHelper.GetUIColor(TextColType.Green, RichTextMgr.Inst.presentRichText.colorType == RichText.ColorType.Bright);
|
}
|
m_StringBuilder.Append(val.Substring(index, match.Groups[1].Index - index));
|
insertIndex = m_StringBuilder.Length - 3;
|
AnalysisSplitEvent(match.Groups[1].Value);
|
m_StringBuilder.Append(textBuilder);
|
AnalysisImg(textBuilder.ToString(), m_StringBuilder.Length);
|
index = match.Groups[1].Index + match.Groups[1].Length;
|
if (RichTextMgr.Inst.GetHrefList() != null)
|
{
|
RichTextMgr.Inst.GetHrefList().Add(hrefInfo);
|
}
|
else
|
{
|
hrefInfo = null;
|
}
|
}
|
m_StringBuilder.Append(val.Substring(index, val.Length - index));
|
presentHrefInfo = null;
|
return m_StringBuilder.ToString();
|
}
|
|
public override string CalculateTextIndex(string val, int index)
|
{
|
if (!HrefRegex.IsMatch(val))
|
{
|
return val;
|
}
|
MatchCollection matchArray = HrefRegex.Matches(val);
|
var filters = ImgAnalysis.s_VertexFilter.Matches(val);
|
var ImgMatch = ImgAnalysis.Unity_Img_Regex.Matches(val);
|
|
m_StringBuilder.Length = 0;
|
int length = 0;
|
for (int i = 0; i < matchArray.Count; i++)
|
{
|
|
Match match = matchArray[i];
|
var picIndex = match.Index;
|
|
//!!!unity 2019之后 建议使用TMP 参考 http://mot.ttthyy.com/374.html
|
//过滤非显示部分 如<color>的长度
|
for (int idx = 0; idx < filters.Count; idx++)
|
{
|
var filter = filters[idx];
|
if (filter.Index >= match.Index)
|
break;
|
picIndex -= filter.Length;
|
}
|
|
//增加图片的长度,不然会偏移
|
for (int idx = 0; idx < ImgMatch.Count; idx++)
|
{
|
var filter = ImgMatch[idx];
|
if (filter.Index >= match.Index)
|
break;
|
picIndex += 1;
|
}
|
|
m_StringBuilder.Append(val.Substring(length, match.Index - length));
|
length = match.Index + match.Length;
|
if (RichTextMgr.Inst.GetHrefList() != null)
|
{
|
//match.Groups[1] 的length包含了非显示部分会偏长
|
var filterAll = ImgAnalysis.s_VertexFilter.Matches(match.Groups[1].ToString());
|
var lenFilter = 0;
|
for (int idx = 0; idx < filterAll.Count; idx++)
|
{
|
var filter = filterAll[idx];
|
lenFilter += filter.Length;
|
}
|
|
|
RichTextMgr.HrefInfo hrefInfo = RichTextMgr.Inst.GetHrefList()[i];
|
hrefInfo.start = picIndex * 4;
|
hrefInfo.end = (picIndex + match.Groups[1].Length - lenFilter - 1) * 4 + 3;
|
//hrefInfo.mPosDic.Add(m_StringBuilder.Length * 4, (m_StringBuilder.Length + match.Groups[1].Length - 1) * 4 + 3);
|
}
|
m_StringBuilder.Append(match.Groups[1].Value);
|
}
|
m_StringBuilder.Append(val.Substring(length, val.Length - length));
|
return m_StringBuilder.ToString();
|
}
|
|
private void AnalysisSplitEvent(string val)
|
{
|
string[] array = GetSplitEvent(val);
|
if (array.Length > 0)
|
{
|
foreach (var split_event in array)
|
{
|
if (ImgAnalysis.Unity_Img_Regex.IsMatch(split_event))
|
{
|
//textBuilder.Append(split_event);
|
continue;
|
}
|
if (SuitNameAnalysis.SuitName_Regex.IsMatch(split_event))
|
{
|
m_StringBuilder.Insert(insertIndex, split_event);
|
continue;
|
}
|
AnalysisSplitData(split_event);
|
}
|
}
|
}
|
|
private void AnalysisSplitData(string val)
|
{
|
string[] array = GetSplitData(val);
|
if (array.Length > 0)
|
{
|
string normalStr = string.Empty;
|
int i = 0;
|
foreach (var split_data in array)
|
{
|
if (split_data.Contains("="))
|
{
|
if (normalStr != string.Empty)
|
{
|
AnalysisSplitValue(normalStr);
|
normalStr = string.Empty;
|
}
|
AnalysisSplitValue(split_data);
|
}
|
else
|
{
|
//正常的字符串拼接,但是不支持多个空格还原
|
if (normalStr != string.Empty)
|
{
|
normalStr = normalStr + " " + split_data;
|
}
|
else
|
{
|
if (i == 0)
|
{
|
normalStr = split_data;
|
}
|
else
|
{
|
normalStr = SPLITDATA + split_data;
|
}
|
}
|
}
|
i++;
|
}
|
if (normalStr != string.Empty)
|
{
|
AnalysisSplitValue(normalStr);
|
normalStr = string.Empty;
|
}
|
}
|
|
}
|
|
private void AnalysisSplitValue(string val)
|
{
|
string[] array = GetSplitValue(val);
|
if (array.Length == 2)
|
{
|
string split_value = array[0].ToLower();
|
switch (split_value)
|
{
|
case "movenpc":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.MOVENPC);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "movepos":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.MOVEPOS);
|
}
|
break;
|
case "flynpc":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.FLYNPC);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "applyfairy":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.APPLYFAIRY);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "ul":
|
{
|
int ul = 1;
|
int.TryParse(array[1], out ul);
|
presentHrefInfo.unline = ul > 0;
|
}
|
break;
|
// case "userdata":
|
// {
|
// if (presentHrefInfo.mSplits.ContainsKey("showitem"))
|
// {
|
// var _id = 0;
|
// if (int.TryParse(presentHrefInfo.mSplits["showitem"], out _id))
|
// {
|
// ItemConfig cfg = ItemConfig.Get(_id);
|
// if (cfg != null && cfg.Type == 113)
|
// {
|
// Dictionary<int, List<int>> userdatadic = null;
|
// string userdata = array[1];
|
// userdatadic = ConfigParse.Analysis(userdata);
|
// int _itemColor = ItemLogicUtility.Instance.GetItemQuality(cfg.ID, userdatadic);
|
// var text = RichTextMgr.Inst.presentRichText;
|
// int colorType = 0;
|
// if (text != null)
|
// {
|
// colorType = text.colorType == RichText.ColorType.Dark ? 0 : 1;
|
// }
|
// presentHrefInfo.unlineColor = UIHelper.GetUIColor(_itemColor, colorType == 1);
|
// }
|
// }
|
// }
|
// presentHrefInfo.Add(split_value, array[1]);
|
// }
|
// break;
|
case "color":
|
{
|
var quality = -1;
|
int.TryParse(array[1],out quality);
|
if (quality != -1)
|
{
|
var text = RichTextMgr.Inst.presentRichText;
|
int colorType = 0;
|
if (text != null)
|
{
|
colorType = text.colorType == RichText.ColorType.Dark ? 0 : 1;
|
}
|
presentHrefInfo.unlineColor = UIHelper.GetUIColor(quality, colorType == 1);
|
}
|
}
|
break;
|
// case "showitem":
|
// {
|
// presentHrefInfo.mEvents.Add(RichTextEventEnum.SHOWITEM);
|
// presentHrefInfo.Add(split_value, array[1]);
|
// var _itemId = int.Parse(array[1]);
|
// var _itemCfg = ItemConfig.Get(_itemId);
|
// var text = RichTextMgr.Inst.presentRichText;
|
// int colorType = 0;
|
// if (text != null)
|
// {
|
// colorType = text.colorType == RichText.ColorType.Dark ? 0 : 1;
|
// }
|
// presentHrefInfo.unlineColor = UIHelper.GetUIColor(_itemCfg.ItemColor, colorType == 1);
|
// }
|
// break;
|
case "enterfb":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.ENTERFB);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "invite":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.INVITE);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "jointeam":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.JOINTEAM);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "openui":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.OPENUI);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "openurl":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.OPENURL);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "showplayer":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.SHOWPLAYER);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "findplayer":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.FindPlayer);
|
}
|
break;
|
case "tip":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.TIP);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "getway":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.GetWay);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "auctionbidding":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.AuctionBidding);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "randomchat":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.RandomChat);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "taskfeedback":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.TaskFeedback);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "blsos":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.BLSOS);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "gszd":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.GSZD);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "copy":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.COPY);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "startguide":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.GUIDE);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
case "xjdhserveridrangeinfo":
|
{
|
presentHrefInfo.mEvents.Add(RichTextEventEnum.XJDHServerIDRangeInfo);
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
default:
|
{
|
presentHrefInfo.Add(split_value, array[1]);
|
}
|
break;
|
}
|
}
|
else
|
{
|
textBuilder.Append(array[0]);
|
}
|
}
|
|
private void AnalysisImg(string val, int length)
|
{
|
if (!ImgAnalysis.Unity_Img_Regex.IsMatch(val))
|
{
|
return;
|
}
|
foreach (Match match in ImgAnalysis.Unity_Img_Regex.Matches(val))
|
{
|
int index = int.Parse(match.Groups[2].Value);
|
if (RichTextMgr.Inst.GetImgList() != null)
|
{
|
RichTextMgr.Inst.GetImgList()[index].href = presentHrefInfo;
|
}
|
m_StringBuilder.Append(match.Value);
|
}
|
}
|
|
public static Regex EquipRegex = new Regex(@"\[(.*?)\]", RegexOptions.Singleline);
|
public static Regex EquipDetailRegex = new Regex("#item#(.*?)#item#", RegexOptions.Singleline);
|
|
public bool ExcuteHrefEvent(string msg, int index = 0, bool fromAutoTask = false)
|
{
|
presentHrefInfo = new RichTextMgr.HrefInfo();
|
presentHrefInfo.fromAutoTask = fromAutoTask;
|
foreach (Match match in HrefRegex.Matches(msg))
|
{
|
AnalysisSplitEvent(match.Groups[1].Value);
|
}
|
return presentHrefInfo.Execute(index);
|
}
|
|
public bool ContainsKey(string _msg, string _key)
|
{
|
presentHrefInfo = new RichTextMgr.HrefInfo();
|
foreach (Match match in HrefRegex.Matches(_msg))
|
{
|
AnalysisSplitEvent(match.Groups[1].Value);
|
}
|
return presentHrefInfo.mSplits.ContainsKey(_key);
|
}
|
}
|