Ltc.cs 6.8 KB

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