少年修仙传客户端代码仓库
lcy
2024-12-16 a39c35fc6449430cd02bccb681c4a0a880e46cd9
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System;
using System.IO;
 
namespace vnxbqy.UI
{
    
    public class LocalChatHistory
    {
        public static Dictionary<ChatInfoType, List<string>> chatHistory = new Dictionary<ChatInfoType, List<string>>();
 
        const string fileName = "ChatHistory";
 
        public static int localSaveCount = 50;
        public static int localChatKeepHour = 12;
 
        static StringBuilder sb = new StringBuilder();
 
        public static void Save()
        {
            if (!ExistAnyLocalChatHistory())
            {
                return;
            }
            var playerId = PlayerDatas.Instance.baseData.PlayerID;
            var path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, fileName, "_", playerId, ".log");
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    foreach (var type in chatHistory.Keys)
                    {
                        var list = chatHistory[type];
                        if (list.Count > 0)
                        {
                            sw.WriteLine(StringUtility.Contact("channel:", (int)type));
                            for (int i = 0; i < list.Count; i++)
                            {
                                sw.WriteLine(list[i]);
                            }
                        }
                    }
                }
            }
        }
 
        static bool ExistAnyLocalChatHistory()
        {
            foreach (var list in chatHistory.Values)
            {
                if (list.Count > 0)
                {
                    return true;
                }
            }
            return false;
        }
 
        public static void Save<T>(T chat) where T : ChatUeseData
        {
            if (null == chat)
            {
                return;
            }
            switch (chat.type)
            {
                //case ChatInfoType.World:
                case ChatInfoType.CrossServer:
                case ChatInfoType.Area:
                case ChatInfoType.Trumpet:
                //case ChatInfoType.Fairy:
                case ChatInfoType.Friend:
                    if (chat.IsSound && string.IsNullOrEmpty(chat.content))
                    {
                        return;
                    }
                    var jsonString = ChatToString(chat);
                    if (!string.IsNullOrEmpty(jsonString))
                    {
                        List<string> list;
                        if (!chatHistory.TryGetValue(chat.type, out list))
                        {
                            list = new List<string>();
                            chatHistory.Add(chat.type, list);
                        }
                        if (list.Count >= localSaveCount)
                        {
                            list.RemoveAt(0);
                        }
                        list.Add(jsonString);
                    }
                    break;
            }
            Save();
        }
 
        static void Save(ChatInfoType type, string line)
        {
            if (!string.IsNullOrEmpty(line))
            {
                List<string> list;
                if (!chatHistory.TryGetValue(type, out list))
                {
                    list = new List<string>();
                    chatHistory.Add(type, list);
                }
                if (list.Count >= localSaveCount)
                {
                    list.RemoveAt(0);
                }
                list.Add(line);
            }
        }
 
        public static string ChatToString(ChatUeseData chat)
        {
            switch (chat.type)
            {
                //case ChatInfoType.World:
                case ChatInfoType.Area:
                case ChatInfoType.CrossServer:
                case ChatInfoType.Team:
                //case ChatInfoType.Fairy:
                    {
                        LocalChat localChat = new LocalChat();
                        localChat.type = chat.type;
                        localChat.player = chat.player;
                        localChat.name = chat.name;
                        localChat.extra = chat.extra;
                        localChat.content = chat.content;
                        localChat.time = chat.createTime;
                        return LitJson.JsonMapper.ToJson(localChat);
                    }
                case ChatInfoType.Trumpet:
                    {
                        var chatTrumpet = chat as ChatTrumpetData;
                        LocalTrumpetChat localChat = new LocalTrumpetChat();
                        localChat.player = chat.player;
                        localChat.name = chat.name;
                        localChat.extra = chat.extra;
                        localChat.content = chat.content;
                        localChat.speakType = chatTrumpet.speakType;
                        localChat.accId = chatTrumpet.accId;
                        localChat.time = chat.createTime;
                        return LitJson.JsonMapper.ToJson(localChat);
                    }
                case ChatInfoType.Friend:
                    {
                        var chatFriend = chat as ChatFriendData;
                        LocalFriendChat localChat = new LocalFriendChat();
                        localChat.player = chat.player;
                        localChat.name = chat.name;
                        localChat.extra = chat.extra;
                        localChat.toName = chatFriend.toName;
                        localChat.toPlayer = chatFriend.toPlayer;
                        localChat.talkType = chatFriend.talkType;
                        localChat.content = chat.content;
                        localChat.time = chat.createTime;
                        return LitJson.JsonMapper.ToJson(localChat);
                    }
            }
            return string.Empty;
        }
 
        public static void Read()
        {
            Clear();
            var playerId = PlayerDatas.Instance.baseData.PlayerID;
            var path = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, fileName, "_", playerId, ".log");
            if (!File.Exists(path))
            {
                return;
            }
            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    ChatInfoType type = ChatInfoType.World;
                    while (!sr.EndOfStream)
                    {
                        try
                        {
                            var line = sr.ReadLine();
                            if (line.StartsWith("channel"))
                            {
                                type = (ChatInfoType)int.Parse(line.Split(':')[1]);
                                continue;
                            }
                            if (type == ChatInfoType.Fairy)
                            {
                                if (PlayerDatas.Instance.baseData.FamilyId == 0)
                                {
                                    continue;
                                }
                            }
                            ChatData chat = null;
                            switch (type)
                            {
                                //case ChatInfoType.World:
                                case ChatInfoType.Area:
                                case ChatInfoType.CrossServer:
                                case ChatInfoType.Team:
                                    //case ChatInfoType.Fairy:
                                    {
                                        LocalChat localChat = LitJson.JsonMapper.ToObject<LocalChat>(line);
                                        var ts = DateTime.Now - localChat.time;
                                        if (ts.TotalHours >= localChatKeepHour)
                                        {
                                            continue;
                                        }
                                        if (type == ChatInfoType.World)
                                        {
                                            chat = new ChatWorldData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.Area)
                                        {
                                            chat = new ChatAreaData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.Team)
                                        {
                                            chat = new ChatTeamData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.Fairy)
                                        {
                                            chat = new ChatFamilyData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        else if (type == ChatInfoType.CrossServer)
                                        {
                                            chat = new ChatCrossServerData(localChat.content, localChat.player, localChat.name, localChat.extra);
                                        }
                                        chat.createTime = localChat.time;
                                    }
                                    break;
                                case ChatInfoType.Trumpet:
                                    {
                                        LocalTrumpetChat localChat = LitJson.JsonMapper.ToObject<LocalTrumpetChat>(line);
                                        var ts = DateTime.Now - localChat.time;
                                        if (ts.TotalHours >= localChatKeepHour)
                                        {
                                            continue;
                                        }
                                        chat = new ChatTrumpetData(localChat.content, localChat.player, localChat.name,
                                            localChat.extra, localChat.speakType, localChat.accId);
                                        chat.createTime = localChat.time;
                                    }
                                    break;
                                case ChatInfoType.Friend:
                                    {
                                        LocalFriendChat localChat = LitJson.JsonMapper.ToObject<LocalFriendChat>(line);
                                        var ts = DateTime.Now - localChat.time;
                                        if (ts.TotalHours >= localChatKeepHour)
                                        {
                                            continue;
                                        }
                                        chat = new ChatFriendData(localChat.content, localChat.player, localChat.name,
                                            localChat.extra, localChat.toName, localChat.talkType, (uint)localChat.toPlayer);
                                        chat.createTime = localChat.time;
                                    }
                                    break;
                            }
                            if (chat != null)
                            {
                                ChatCtrl.Inst.KeepLocalChat(chat);
                            }
                            Save(type, line);
                        }
                        catch (Exception e)
                        {
                            DebugEx.Log(e.Message);
                            continue;
                        }
                    }
                }
            }
        }
 
        public static void Clear(ChatInfoType type)
        {
            if (chatHistory.ContainsKey(type))
            {
                chatHistory.Remove(type);
                Save();
            }
        }
 
        public static void Clear()
        {
            chatHistory.Clear();
        }
 
        public struct LocalChat
        {
            public ChatInfoType type;
            public DateTime time;
            public int player;
            public string name;
            public string extra;
            public string content;
        }
 
        public struct LocalFriendChat
        {
            public DateTime time;
            public int player;
            public string name;
            public string extra;
            public string content;
            public string toName;
            public byte talkType;
            public int toPlayer;
        }
 
        public struct LocalTrumpetChat
        {
            public DateTime time;
            public int player;
            public string name;
            public string extra;
            public string content;
            public byte speakType;
            public string accId;
        }
    }
}