123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- 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<T>() where T : World
- {
- return (T)World.Worlds.Where(v => v.GetType() == typeof(T)).First();
- }
- public static T GetSystem<T>() 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<Thread, Channel> Channels = new ConcurrentDictionary<Thread, Channel>();
- public static void SetChannel(Channel channel)
- {
- Channels[Thread.CurrentThread] = channel;
- ClearChannel();
- }
- public static Channel GetChannel()
- {
- return Channels[Thread.CurrentThread];
- }
- static ConcurrentDictionary<Channel, List<LogInfo>> Msgs = new ConcurrentDictionary<Channel, List<LogInfo>>();
- 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<LogInfo> GetLogInfo()
- {
- var channel = GetChannel();
- if (Msgs.TryGetValue(channel, out var list))
- {
- return list;
- }
- return new List<LogInfo>();
- }
- public static void Log(string msg,LogLevel level,ErrorType type)
- {
- var channel = GetChannel();
-
- if (!Msgs.TryGetValue(channel, out var list))
- {
- list = new List<LogInfo>();
- 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>(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<T>(Expression<Func<T>> 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, T>(T1 obj, Expression<Func<T1, T>> 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, T2, T>(T1 obj, T2 obj2, Expression<Func<T1, T2, T>> 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, T2, T3, T>(T1 obj, T2 obj2, T3 obj3, Expression<Func<T1, T2, T3, T>> 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;
- }
- }
- }
|