少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
using UnityEngine;
public class ImgAnalysis : TRichAnalysis<ImgAnalysis>
{
    public static Regex Img_Regex = new Regex(@"<Img (.*?)/>", RegexOptions.Singleline);
    public static Regex Unity_Img_Regex = new Regex(@"<quad (.*?) index=([0-9]+)/>", 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("<quad ");
        m_StringBuilder.Append(string.Format("size={0} ", presentImgInfo.height));
        float ratio = (float)Math.Round((float)presentImgInfo.width / presentImgInfo.height, 1);
        m_StringBuilder.Append(string.Format("width={0} ", ratio));
        m_StringBuilder.Append(string.Format("index={0}", presentImgInfo.index));
        m_StringBuilder.Append("/>");
    }
 
    private void LoadSprite()
    {
        if (presentImgInfo.IsFace) return;
        if (IconConfig.inited)
        {
            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.rect.width;
            presentImgInfo.height = presentImgInfo.sprite.rect.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("<Img face={0}/>", 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();
        }
    }
}