using FreeRedis; using System; using System.Collections.Concurrent; using System.Linq.Expressions; using System.Threading; using System.Threading.Channels; namespace WCS.Core { public static class Ltc { public static T GetWorld() where T : World { return (T)World.Worlds.Where(v => v.GetType() == typeof(T)).First(); } public static T GetSystem() where T : SystemBase { return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First(); } private static ConcurrentDictionary Channels = new ConcurrentDictionary(); public static void SetChannel(Channel channel) { Channels[Thread.CurrentThread] = channel; } public static Channel GetChannel() { return Channels[Thread.CurrentThread]; } static ConcurrentDictionary> Msgs = new ConcurrentDictionary>(); public static void ClearChannel() { if (Msgs.TryGetValue(GetChannel(), out var list)) list.Clear(); } public static string GetLogStr() { var channel = GetChannel(); if (Msgs.TryGetValue(channel, out var list)) { var msg = "-------------------"+channel + "--------------------\n" + string.Join('\n', list.Select(v => $"{string.Join('\n', v)}")); return msg; } else { return ""; } } public static List GetLogInfo() { var channel = GetChannel(); if (Msgs.TryGetValue(channel, out var list)) { return list; } return new List(); } public static void Log(string msg,LogLevel level,ErrorType type) { var channel = GetChannel(); if (!Msgs.TryGetValue(channel, out var list)) { list = new List(); Msgs[channel] = list; } list.Add(new LogInfo { Message = msg, Level = level, Type = type, Channel = channel }); } public static void Publish(World world) { var channel = GetChannel(); if (Msgs.TryGetValue(channel, out var list)) { var msg = string.Join('\n', list); world.Ex().Publish(channel, msg); } } private static string ResultString(T obj) { if (obj == null) { return "null"; } else if (obj is bool) { var b = obj as Boolean?; return b.Value ? "成立" : "不成立"; } else if (obj is System.Collections.ICollection) { var coll = obj as System.Collections.ICollection; return coll.Count.ToString() + "元素"; } return obj.ToString(); } public static T Do(Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(); msg += res; return res; } catch (Exception ex) { throw; } finally { Log(msg, LogLevel.低, ErrorType.已知); } } public static T Do(T1 obj, Expression> exp) { var msg = ""; try { try { msg = exp.ExpToString(); } catch (Exception ex2) { } msg += " 结果:"; var res = exp.Compile().Invoke(obj); msg += ResultString(res); return res; } catch (Exception ex) { throw; } finally { Log(msg, LogLevel.低, ErrorType.已知); } } public static T Do(T1 obj, T2 obj2, Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(obj, obj2); msg += ResultString(res); return res; } catch (Exception ex) { throw; } finally { Log(msg, LogLevel.低, ErrorType.已知); } } public static T Do(T1 obj, T2 obj2, T3 obj3, Expression> exp) { var msg = exp.ExpToString(); msg += " 结果:"; try { var res = exp.Compile().Invoke(obj, obj2, obj3); msg += ResultString(res); return res; } catch (Exception ex) { throw; } finally { Log(msg, LogLevel.低, ErrorType.已知); } } } public class LogInfo { public ErrorType Type { get; set; } public LogLevel Level { get; set; } public Channel Channel { get; set; } public string Message { get; set; } = ""; public override string ToString() { return $"类型:{Type},级别:{Level},内容:{Message}"; } } public class Channel { public string World; public string System; public string Item; public override string ToString() { return $"{World}-{System}-{Item}"; } } public enum ErrorType { 未知 = 0, 已知 = 1 } public enum LogLevel { 低 = 0, 中 = 1, 高 = 2 } public class KnownException : Exception { public LogLevel Level { get; set; } public KnownException(string msg, LogLevel level) : base(msg) { this.Level = level; } } }