Ltc.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using FreeRedis;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Linq.Expressions;
  5. using System.Threading;
  6. using System.Threading.Channels;
  7. namespace WCS.Core
  8. {
  9. public static class Ltc
  10. {
  11. public static T GetWorld<T>() where T : World
  12. {
  13. return (T)World.Worlds.Where(v => v.GetType() == typeof(T)).First();
  14. }
  15. public static T GetSystem<T>() where T : SystemBase
  16. {
  17. return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First();
  18. }
  19. private static ConcurrentDictionary<Thread, Channel> Channels = new ConcurrentDictionary<Thread, Channel>();
  20. public static void SetChannel(Channel channel)
  21. {
  22. Channels[Thread.CurrentThread] = channel;
  23. }
  24. public static Channel GetChannel()
  25. {
  26. return Channels[Thread.CurrentThread];
  27. }
  28. static ConcurrentDictionary<Channel, List<LogInfo>> Msgs = new ConcurrentDictionary<Channel, List<LogInfo>>();
  29. public static void ClearChannel()
  30. {
  31. if (Msgs.TryGetValue(GetChannel(), out var list))
  32. list.Clear();
  33. }
  34. public static string GetLogStr()
  35. {
  36. var channel = GetChannel();
  37. if (Msgs.TryGetValue(channel, out var list))
  38. {
  39. var msg = "-------------------"+channel + "--------------------\n" + string.Join('\n', list.Select(v => $"{string.Join('\n', v)}"));
  40. return msg;
  41. }
  42. else
  43. {
  44. return "";
  45. }
  46. }
  47. public static List<LogInfo> GetLogInfo()
  48. {
  49. var channel = GetChannel();
  50. if (Msgs.TryGetValue(channel, out var list))
  51. {
  52. return list;
  53. }
  54. return new List<LogInfo>();
  55. }
  56. public static void Log(string msg,LogLevel level,ErrorType type)
  57. {
  58. var channel = GetChannel();
  59. if (!Msgs.TryGetValue(channel, out var list))
  60. {
  61. list = new List<LogInfo>();
  62. Msgs[channel] = list;
  63. }
  64. list.Add(new LogInfo { Message = msg, Level = level, Type = type, Channel = channel });
  65. }
  66. public static void Publish(World world)
  67. {
  68. var channel = GetChannel();
  69. if (Msgs.TryGetValue(channel, out var list))
  70. {
  71. var msg = string.Join('\n', list);
  72. world.Ex().Publish(channel, msg);
  73. }
  74. }
  75. private static string ResultString<T>(T obj)
  76. {
  77. if (obj == null)
  78. {
  79. return "null";
  80. }
  81. else if (obj is bool)
  82. {
  83. var b = obj as Boolean?;
  84. return b.Value ? "成立" : "不成立";
  85. }
  86. else if (obj is System.Collections.ICollection)
  87. {
  88. var coll = obj as System.Collections.ICollection;
  89. return coll.Count.ToString() + "元素";
  90. }
  91. return obj.ToString();
  92. }
  93. public static T Do<T>(Expression<Func<T>> exp)
  94. {
  95. var msg = exp.ExpToString();
  96. msg += " 结果:";
  97. try
  98. {
  99. var res = exp.Compile().Invoke();
  100. msg += res;
  101. return res;
  102. }
  103. catch (Exception ex)
  104. {
  105. throw;
  106. }
  107. finally
  108. {
  109. Log(msg, LogLevel.低, ErrorType.已知);
  110. }
  111. }
  112. public static T Do<T1, T>(T1 obj, Expression<Func<T1, T>> exp)
  113. {
  114. var msg = "";
  115. try
  116. {
  117. try
  118. {
  119. msg = exp.ExpToString();
  120. }
  121. catch (Exception ex2)
  122. {
  123. }
  124. msg += " 结果:";
  125. var res = exp.Compile().Invoke(obj);
  126. msg += ResultString(res);
  127. return res;
  128. }
  129. catch (Exception ex)
  130. {
  131. throw;
  132. }
  133. finally
  134. {
  135. Log(msg, LogLevel.低, ErrorType.已知);
  136. }
  137. }
  138. public static T Do<T1, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> exp)
  139. {
  140. var msg = exp.ExpToString();
  141. msg += " 结果:";
  142. try
  143. {
  144. var res = exp.Compile().Invoke(obj, obj2);
  145. msg += ResultString(res);
  146. return res;
  147. }
  148. catch (Exception ex)
  149. {
  150. throw;
  151. }
  152. finally
  153. {
  154. Log(msg, LogLevel.低, ErrorType.已知);
  155. }
  156. }
  157. public static T Do<T1, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> exp)
  158. {
  159. var msg = exp.ExpToString();
  160. msg += " 结果:";
  161. try
  162. {
  163. var res = exp.Compile().Invoke(obj, obj2, obj3);
  164. msg += ResultString(res);
  165. return res;
  166. }
  167. catch (Exception ex)
  168. {
  169. throw;
  170. }
  171. finally
  172. {
  173. Log(msg, LogLevel.低, ErrorType.已知);
  174. }
  175. }
  176. }
  177. public class LogInfo
  178. {
  179. public ErrorType Type { get; set; }
  180. public LogLevel Level { get; set; }
  181. public Channel Channel { get; set; }
  182. public string Message { get; set; } = "";
  183. public override string ToString()
  184. {
  185. return $"类型:{Type},级别:{Level},内容:{Message}";
  186. }
  187. }
  188. public class Channel
  189. {
  190. public string World;
  191. public string System;
  192. public string Item;
  193. public override string ToString()
  194. {
  195. return $"{World}-{System}-{Item}";
  196. }
  197. }
  198. public enum ErrorType
  199. {
  200. 未知 = 0,
  201. 已知 = 1
  202. }
  203. public enum LogLevel
  204. {
  205. 低 = 0,
  206. 中 = 1,
  207. 高 = 2
  208. }
  209. public class KnownException : Exception
  210. {
  211. public LogLevel Level { get; set; }
  212. public KnownException(string msg, LogLevel level) : base(msg)
  213. {
  214. this.Level = level;
  215. }
  216. }
  217. }