using System; using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; using TableConfig; using UnityEngine; public class ImgAnalysis : TRichAnalysis { public static Regex Img_Regex = new Regex(@"", RegexOptions.Singleline); public static Regex Unity_Img_Regex = new Regex(@"", RegexOptions.Singleline); private RichTextMgr.ImgInfo presentImgInfo = null; public override string Analysis(string val, bool IsRich) { if (!Img_Regex.IsMatch(val)) { return val; } m_StringBuilder.Length = 0; int index = 0; int imgCnt = 0; foreach (Match match in Img_Regex.Matches(val)) { RichTextMgr.ImgInfo imgInfo = new RichTextMgr.ImgInfo(); imgInfo.width = 30; imgInfo.height = 30; presentImgInfo = imgInfo; m_StringBuilder.Append(val.Substring(index, match.Index - index)); AnalysisSplitEvent(match.Groups[1].Value); if (IsRich) { presentImgInfo.index = imgCnt; InverseToRichText(); if (RichTextMgr.Inst.GetImgList() != null) RichTextMgr.Inst.GetImgList().Add(presentImgInfo); else imgInfo = null; imgCnt++; } index = match.Index + match.Length; } m_StringBuilder.Append(val.Substring(index, val.Length - index)); presentImgInfo = null; return m_StringBuilder.ToString(); } public override string CalculateTextIndex(string val, int index) { if (!Unity_Img_Regex.IsMatch(val)) { return val; } MatchCollection matchArray = Unity_Img_Regex.Matches(val); if (index < matchArray.Count) { Match match = matchArray[index]; if (RichTextMgr.Inst.GetImgList() != null) { RichTextMgr.ImgInfo imgInfo = RichTextMgr.Inst.GetImgList()[index]; imgInfo.end = match.Index * 4 + 3; } } return val; } private void AnalysisSplitEvent(string val) { string[] array = GetSplitEvent(val); if (array.Length > 0) { foreach (var split_event in array) { AnalysisSplitData(split_event); } } LoadSprite(); } private void AnalysisSplitData(string val) { string[] array = GetSplitData(val); if (array.Length > 0) { foreach (var split_data in array) { AnalysisSplitValue(split_data); } } } private void AnalysisSplitValue(string val) { string[] array = GetSplitValue(val); if (array.Length == 2) { string split_value = array[0].ToLower(); switch (split_value) { case "img": case "chat": { presentImgInfo.spriteName = array[1]; } break; case "face": { presentImgInfo.spriteName = array[1]; presentImgInfo.IsFace = true; if (RichTextMgr.Inst.presentRichText != null) { presentImgInfo.height = RichTextMgr.Inst.presentRichText.FaceSize; presentImgInfo.width = presentImgInfo.height; } } break; case "size": { presentImgInfo.height = float.Parse(array[1]); presentImgInfo.width = float.Parse(array[1]); } break; } } } private void InverseToRichText() { m_StringBuilder.Append(""); } private void LoadSprite() { if (presentImgInfo.IsFace) return; if (ConfigManager.Instance != null) { presentImgInfo.sprite = UILoader.LoadSprite(presentImgInfo.spriteName); } if (presentImgInfo.sprite != null) { RichText text = RichTextMgr.Inst.presentRichText; if (text != null) { if (text.LockImgSize) { presentImgInfo.width = presentImgInfo.height = text.fontSize; return; } else if (text.ModifyImgSiez) { presentImgInfo.width = text.ModifyImgWidth; presentImgInfo.height = text.ModifyImgHeight; return; } } presentImgInfo.width = presentImgInfo.sprite.textureRect.width; presentImgInfo.height = presentImgInfo.sprite.textureRect.height; } } private const string FACE_REPLACE = @"#~([0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z])"; public static Regex FaceRegex = new Regex(FACE_REPLACE, RegexOptions.Singleline); public static string ReplaceFace(string msg) { if (!FaceRegex.IsMatch(msg)) { return msg; } else { m_StringBuilder.Length = 0; int index = 0; foreach (Match match in FaceRegex.Matches(msg)) { string result = match.Groups[1].Value.Replace("0", ""); m_StringBuilder.Append(msg.Substring(index, match.Index - index)); if (UIFrameMgr.Inst.ContainsFace(result)) { m_StringBuilder.Append(string.Format("", result)); } else { m_StringBuilder.Append(match.Groups[0].Value); } index = match.Index + match.Length; } m_StringBuilder.Append(msg.Substring(index, msg.Length - index)); return m_StringBuilder.ToString(); } } public static string ReplaceFace(string msg, out int cnt) { cnt = 0; if (!FaceRegex.IsMatch(msg)) { return msg; } else { m_StringBuilder.Length = 0; int index = 0; foreach (Match match in FaceRegex.Matches(msg)) { cnt++; m_StringBuilder.Append(msg.Substring(index, match.Index - index)); index = match.Index + match.Length; } m_StringBuilder.Append(msg.Substring(index, msg.Length - index)); return m_StringBuilder.ToString(); } } }