| using System.Collections; | 
| using System.Collections.Generic; | 
| using System.Text; | 
| using UnityEngine; | 
| using System; | 
| using System.IO; | 
|   | 
|      | 
|     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) | 
|                             { | 
|                                 ChatManager.Instance.KeepLocalChat(chat); | 
|                             } | 
|                             Save(type, line); | 
|                         } | 
|                         catch (Exception e) | 
|                         { | 
|                             Debug.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; | 
|         } | 
|     } |