Ltc.cs 6.6 KB

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