using FreeRedis; using System; using System.Collections.Concurrent; using System.ComponentModel; 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 { try { return (T)World.Worlds.SelectMany(v => v.Systems).Where(v => v.GetType() == typeof(T)).First(); } catch (Exception ex) { throw new Exception($"系统:{typeof(T).Name}未设置BelongToAttribute"); } } private static ConcurrentDictionary Channels = new ConcurrentDictionary(); public static void SetChannel(Channel channel) { Channels[Thread.CurrentThread] = channel; ClearChannel(); } public static Channel GetChannel() { return Channels[Thread.CurrentThread]; } static ConcurrentDictionary> Msgs = new ConcurrentDictionary>(); 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.Low, ErrorType.Kown); } } 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.Low, ErrorType.Kown); } } 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.Low, ErrorType.Kown); } } 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.Low, ErrorType.Kown); } } } 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() { //var a = ErrorType.Unkown; return $"类型:{Type.Description()},级别:{Level.Description()},内容:{Message}"; } } public class Channel { public string World = ""; public string Stage = ""; public string System = ""; public string Item = ""; public override string ToString() { return $"{World}-{Stage}-{System}-{Item}"; } } public enum ErrorType { [Description("未知")] Unkown = 0, [Description("已知")] Kown = 1 } public enum LogLevel { [Description("低")] Low = 0, [Description("中")] Mid = 1, [Description("高")] High = 2 } public class KnownException : Exception { public LogLevel Level { get; set; } public KnownException(string msg, LogLevel level) : base(msg) { this.Level = level; } } }