diff --git a/Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs b/Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs index 2b6dfa71e3968cb791c356b876fd6fc5a98e464d..59c1902419094c47407052fcdb448695b442ab4e 100644 --- a/Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs +++ b/Unity/Assets/Scripts/Base/Message/ClientDispatcher.cs @@ -18,14 +18,7 @@ namespace Model if (message is AMessage || message is ARequest) { MessageInfo messageInfo = new MessageInfo(opcode, message); - if (opcode < 2000) - { - Game.Scene.GetComponent().Run(EventIdType.MessageDeserializeFinish, messageInfo); - } - else - { - Game.Scene.GetComponent().Handle(messageInfo); - } + Game.Scene.GetComponent().Handle(messageInfo); return; } diff --git a/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs b/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs index a73600541ed5eafbeaae845445cee237062e8f9b..6a13610b77459c784aeb9e9ddf8af5a632e52709 100644 --- a/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs +++ b/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs @@ -1,8 +1,58 @@ using System; using System.Collections.Generic; +using System.Reflection; +using ILRuntime.CLR.Method; +using ILRuntime.Runtime.Intepreter; namespace Model { + /// + /// 用来抹平ILRuntime跟mono层差异 + /// + public interface IMessageMethod + { + void Run(AMessage a); + } + + public class IMessageMonoMethod : IMessageMethod + { + private readonly IMHandler iMHandler; + + public IMessageMonoMethod(IMHandler iMHandler) + { + this.iMHandler = iMHandler; + } + + public void Run(AMessage a) + { + this.iMHandler.Handle(a); + } + } + + public class IMessageILMethod : IMessageMethod + { + private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain; + private readonly ILTypeInstance instance; + private readonly IMethod method; + private readonly object[] param; + + public IMessageILMethod(Type type, string methodName) + { + appDomain = Init.Instance.AppDomain; + this.instance = this.appDomain.Instantiate(type.FullName); + this.method = this.instance.Type.GetMethod(methodName); + int n = this.method.ParameterCount; + this.param = new object[n]; + } + + public void Run(AMessage a) + { + this.param[0] = a; + this.appDomain.Invoke(this.method, this.instance, param); + } + } + + [ObjectEvent] public class MessageDispatherComponentEvent : ObjectEvent, IAwake, ILoad { @@ -17,12 +67,41 @@ namespace Model } } + public static class Opcode2Name + { + private static Dictionary _init = new Dictionary(); + public static string GetName(int code) + { + if (_init.Count == 0) + { + Type type = typeof(Opcode); + FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); + foreach (FieldInfo field in fields) + { + if (!field.IsStatic) + { + continue; + } + int codeID = (ushort)field.GetValue(null); + if (_init.ContainsKey(codeID)) + { + Log.Warning($"重复的Opcode:{codeID}"); + continue; + } + _init.Add(codeID, field.Name); + } + } + return _init[code]; + } + } + /// /// 消息分发组件 /// public class MessageDispatherComponent : Component { - private Dictionary> handlers; + private Dictionary> handlers; + public void Awake() { @@ -31,7 +110,7 @@ namespace Model public void Load() { - handlers = new Dictionary>(); + handlers = new Dictionary>(); Type[] types = DllHelper.GetMonoTypes(); @@ -46,26 +125,49 @@ namespace Model IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type); if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode)) { - this.handlers.Add(messageHandlerAttribute.Opcode, new List()); + this.handlers.Add(messageHandlerAttribute.Opcode, new List()); + } + this.handlers[messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler)); + } + + // hotfix dll + Type[] hotfixTypes = DllHelper.GetHotfixTypes(); + foreach (Type type in hotfixTypes) + { + object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false); + if (attrs.Length == 0) + { + continue; + } + MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0]; +#if ILRuntime + IMessageMethod iMessageMethod = new IMessageILMethod(type, "Run"); +#else + IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type); + IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler); +#endif + if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode)) + { + this.handlers.Add(messageHandlerAttribute.Opcode, new List()); } - this.handlers[messageHandlerAttribute.Opcode].Add(iMHandler); + this.handlers[messageHandlerAttribute.Opcode].Add(iMessageMethod); } } public void Handle(MessageInfo messageInfo) { - List actions; + List actions; if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions)) { - Log.Error($"消息 {messageInfo.Opcode} 没有处理"); + Log.Error($"消息 {Opcode2Name.GetName(messageInfo.Opcode)}({messageInfo.Opcode}) 没有处理"); return; } - foreach (IMHandler ev in actions) + foreach (IMessageMethod ev in actions) { try { - ev.Handle(messageInfo.Message); + ev.Run(messageInfo.Message); } catch (Exception e) { @@ -76,7 +178,7 @@ namespace Model public override void Dispose() { - if (this.Id == 0) + if (Id == 0) { return; } diff --git a/Unity/Hotfix/Base/Message/AMHandler.cs b/Unity/Hotfix/Base/Message/AMHandler.cs index fc99a92942628318fcba332d249f6c3ae2e4cff4..5e5e4f0ff60c92fde758e24b1afa3d6fc5c34509 100644 --- a/Unity/Hotfix/Base/Message/AMHandler.cs +++ b/Unity/Hotfix/Base/Message/AMHandler.cs @@ -7,7 +7,7 @@ namespace Hotfix { protected abstract void Run(Message message); - public void Handle(object msg) + public void Handle(AMessage msg) { Message message = msg as Message; if (message == null) diff --git a/Unity/Hotfix/Base/Message/IMHandler.cs b/Unity/Hotfix/Base/Message/IMHandler.cs index 7314cff8a2de46f565bb7b756936e44681f96a66..6146a552c80fa1bcef0331656bbaa5001485fe54 100644 --- a/Unity/Hotfix/Base/Message/IMHandler.cs +++ b/Unity/Hotfix/Base/Message/IMHandler.cs @@ -2,9 +2,15 @@ namespace Hotfix { +#if ILRuntime public interface IMHandler { - void Handle(object message); + void Handle(AMessage message); Type GetMessageType(); } +#else + public interface IMHandler : Model.IMHandler + { + } +#endif } \ No newline at end of file diff --git a/Unity/Hotfix/Component/MessageDispatherComponent.cs b/Unity/Hotfix/Component/MessageDispatherComponent.cs deleted file mode 100644 index af05aff0764d6c41295794322531bac401bc8564..0000000000000000000000000000000000000000 --- a/Unity/Hotfix/Component/MessageDispatherComponent.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Collections.Generic; -using Model; - -namespace Hotfix -{ - [ObjectEvent] - public class MessageDispatherComponentEvent : ObjectEvent, IAwake, ILoad - { - public void Awake() - { - this.Get().Awake(); - } - - public void Load() - { - this.Get().Load(); - } - } - - /// - /// 消息分发组件 - /// - public class MessageDispatherComponent: Component, IAwake, ILoad - { - private Dictionary> handlers; - - - public void Awake() - { - this.Load(); - } - - public void Load() - { - handlers = new Dictionary>(); - - Type[] types = DllHelper.GetHotfixTypes(); - - foreach (Type type in types) - { - object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false); - if (attrs.Length == 0) - { - continue; - } - MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0]; - IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type); - if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode)) - { - this.handlers.Add(messageHandlerAttribute.Opcode, new List()); - } - this.handlers[messageHandlerAttribute.Opcode].Add(iMHandler); - } - } - - public void Handle(MessageInfo messageInfo) - { - List actions; - if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions)) - { - Log.Error($"消息 {messageInfo.Opcode} 没有处理"); - return; - } - - foreach (IMHandler ev in actions) - { - try - { - ev.Handle(messageInfo.Message); - } - catch (Exception e) - { - Log.Error(e.ToStr()); - } - } - } - - public override void Dispose() - { - if (this.Id == 0) - { - return; - } - - base.Dispose(); - } - } -} \ No newline at end of file diff --git a/Unity/Hotfix/Event/SessionRecvMessage_Dispatch.cs b/Unity/Hotfix/Event/SessionRecvMessage_Dispatch.cs deleted file mode 100644 index ad47f6c6d1a0932ac9d4b8a7f8e45abf48a8b9b4..0000000000000000000000000000000000000000 --- a/Unity/Hotfix/Event/SessionRecvMessage_Dispatch.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Model; - -namespace Hotfix -{ - // 订阅mono层的Session发出的事件 - [Event((int)EventIdType.MessageDeserializeFinish)] - public class MessageDeserializeFinish_Dispatch: IEvent - { - public void Run(MessageInfo messageInfo) - { - Hotfix.Scene.GetComponent().Handle(messageInfo); - } - } -} diff --git a/Unity/Hotfix/Unity.Hotfix.csproj b/Unity/Hotfix/Unity.Hotfix.csproj index 12d33b259693f0301e53599cafb733c7d938436a..b9de2163834fc9fb6ec161a5d8d1436d60c42766 100644 --- a/Unity/Hotfix/Unity.Hotfix.csproj +++ b/Unity/Hotfix/Unity.Hotfix.csproj @@ -80,14 +80,12 @@ - - @@ -97,6 +95,7 @@ + diff --git a/Unity/Unity.Editor.Plugins.csproj b/Unity/Unity.Editor.Plugins.csproj index 6701b0d2df2860b42d9627457f8c72bb8ba0d604..9696ec517c8f2ed25a2bf4562c132ef7ea830c13 100644 --- a/Unity/Unity.Editor.Plugins.csproj +++ b/Unity/Unity.Editor.Plugins.csproj @@ -164,6 +164,8 @@ + + diff --git a/Unity/Unity.Editor.csproj b/Unity/Unity.Editor.csproj index f33b76f90af6b34f3c3c435ebe05b4fb45cafdc5..2508ce0a84f203109c7bfd44c41546fd009ea0c9 100644 --- a/Unity/Unity.Editor.csproj +++ b/Unity/Unity.Editor.csproj @@ -214,6 +214,8 @@ + + diff --git a/Unity/Unity.Plugins.csproj b/Unity/Unity.Plugins.csproj index 4217839320f454fde393fc37e3b7bc922923da76..00fe8cf70fb2bc5d18744a91178d552cd1969d8b 100644 --- a/Unity/Unity.Plugins.csproj +++ b/Unity/Unity.Plugins.csproj @@ -516,6 +516,8 @@ + + diff --git a/Unity/Unity.csproj b/Unity/Unity.csproj index b0d4ecc49956364c704ae403b7c713d09d666852..6d778be23bc1b945233f1dad93b4b97f00e7a50b 100644 --- a/Unity/Unity.csproj +++ b/Unity/Unity.csproj @@ -581,6 +581,8 @@ + +