Przeglądaj źródła

使用ILRuntime预编译项可以切换到IL执行Hotfix, 否则使用直接加载dll,这样可以平常方便调试,发布的时候再切换成IL执行

tanghai 9 lat temu
rodzic
commit
3f235c7123

+ 2 - 1
Unity/Assets/Scripts/Component/EventComponent.cs

@@ -37,7 +37,7 @@ namespace Model
 					this.allEvents[aEventAttribute.Type].Add(method);
 				}
 			}
-
+#if ILRuntime
 			types = DllHelper.GetHotfixTypes();
 			foreach (Type type in types)
 			{
@@ -54,6 +54,7 @@ namespace Model
 					this.allEvents[aEventAttribute.Type].Add(method);
 				}
 			}
+#endif
 		}
 
 		public void Run(int type, params object[] param)

+ 10 - 3
Unity/Assets/Scripts/Component/MessageDispatherComponent.cs

@@ -42,14 +42,21 @@ namespace Model
 
 				this.opcodeTypes.Add(messageAttribute.Opcode, monoType);
 			}
-
-			Type[] ilTypes = DllHelper.GetHotfixTypes();
-			foreach (Type type in ilTypes)
+#if ILRuntime
+			Type[] types = DllHelper.GetHotfixTypes();
+#else
+			Type[] types = DllHelper.GetMonoTypes();
+#endif
+			foreach (Type type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
 
 				MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
+#if ILRuntime
 				IInstanceMethod method = new ILInstanceMethod(type, "Handle");
+#else
+				IInstanceMethod method = new MonoInstanceMethod(type, "Handle");
+#endif
 				if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
 				{
 					this.handlers.Add(messageHandlerAttribute.Opcode, new List<IInstanceMethod>());

+ 32 - 4
Unity/Assets/Scripts/Component/UIComponent.cs

@@ -1,20 +1,21 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using ILRuntime.CLR.Method;
 using ILRuntime.Runtime.Intepreter;
 using UnityEngine;
 
 namespace Model
 {
-	public class IUIFactoryMethod: IUIFactory
+	public class IILUIFactoryMethod: IUIFactory
 	{
 		private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
 		private readonly ILTypeInstance instance;
 		private readonly IMethod method;
 		private readonly object[] params3 = new object[3];
 
-		public IUIFactoryMethod(Type type)
+		public IILUIFactoryMethod(Type type)
 		{
 			appDomain = Game.EntityEventManager.AppDomain;
 			this.instance = this.appDomain.Instantiate(type.FullName);
@@ -31,6 +32,26 @@ namespace Model
 		}
 	}
 
+	public class IMonoUIFactoryMethod : IUIFactory
+	{
+		private readonly object instance;
+		private readonly MethodInfo methodInfo;
+		private readonly object[] params3 = new object[3];
+		public IMonoUIFactoryMethod(Type type)
+		{
+			this.instance = Activator.CreateInstance(type);
+			this.methodInfo = type.GetMethod("Create");
+		}
+
+		public UI Create(Scene scene, int type, UI parent)
+		{
+			this.params3[0] = scene;
+			this.params3[1] = type;
+			this.params3[2] = parent;
+			return (UI)this.methodInfo.Invoke(this.instance, params3);
+		}
+	}
+
 	/// <summary>
 	/// 管理所有UI
 	/// </summary>
@@ -73,8 +94,11 @@ namespace Model
 		{
 			this.UiTypes = new Dictionary<int, IUIFactory>();
 
+#if ILRuntime
 			Type[] types = DllHelper.GetHotfixTypes();
-
+#else
+			Type[] types = DllHelper.GetMonoTypes();
+#endif
 			foreach (Type type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof (UIFactoryAttribute), false);
@@ -89,7 +113,11 @@ namespace Model
 					throw new GameException($"已经存在同类UI Factory: {attribute.Type}");
 				}
 
-				IUIFactory iuiFactory = new IUIFactoryMethod(type);
+#if ILRuntime
+				IUIFactory iuiFactory = new IILUIFactoryMethod(type);
+#else
+				IUIFactory iuiFactory = new IMonoUIFactoryMethod(type);
+#endif
 
 				this.UiTypes.Add(attribute.Type, iuiFactory);
 			}

+ 10 - 0
Unity/Assets/Scripts/Helper/DllHelper.cs

@@ -2,11 +2,21 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
+using UnityEngine;
 
 namespace Model
 {
 	public static class DllHelper
 	{
+		public static Assembly LoadHotfixAssembly()
+		{
+			GameObject code = (GameObject)Resources.Load("Code");
+			byte[] assBytes = code.Get<TextAsset>("Hotfix.dll").bytes;
+			byte[] mdbBytes = code.Get<TextAsset>("Hotfix.pdb").bytes;
+			Assembly assembly = Assembly.Load(assBytes, mdbBytes);
+			return assembly;
+		}
+
 		public static Type[] GetMonoTypes()
 		{
 			List<Type> types = new List<Type>();

+ 4 - 9
Unity/Assets/Scripts/Init.cs

@@ -1,30 +1,25 @@
 using System;
-using System.IO;
 using Base;
-using ILRuntime.CLR.Method;
 using UnityEngine;
 
 namespace Model
 {
 	public class Init: MonoBehaviour
 	{
-		private readonly object[] param0 = new object[0];
-		private IMethod start;
-
 		private void Start()
 		{
+#if ILRuntime
 			Game.EntityEventManager.RegisterILRuntime();
 			Game.EntityEventManager.RegisterILAdapter();
+#else
+			Game.EntityEventManager.Register("Hotfix", DllHelper.LoadHotfixAssembly());
+#endif
 			Game.EntityEventManager.Register("Model", typeof (Game).Assembly);
-			ILRuntime.Runtime.Enviorment.AppDomain appDomain = Game.EntityEventManager.AppDomain;
 			Game.Scene.AddComponent<ResourcesComponent>();
 			Game.Scene.AddComponent<UIComponent>();
 			Game.Scene.AddComponent<UnitComponent>();
 
 			EventHelper.Run(EventIdType.InitSceneStart);
-
-			//this.start = appDomain.LoadedTypes["Hotfix.HotfixEntry"].GetMethod("Start", 0);
-			//appDomain.Invoke(this.start, null, param0);
 		}
 
 		private void Update()

+ 1 - 1
Unity/Hotfix/Component/UnitComponentE.cs

@@ -8,7 +8,7 @@ namespace Hotfix
 	{
 		public static void Awake(this UnitComponent component)
 		{
-			Log.Debug("测试IL层EntityEvent");
+			Log.Debug("UnitComponent Awake");
 		}
 	}
 }

+ 1 - 1
Unity/Unity.csproj

@@ -384,8 +384,8 @@
     <Compile Include="Assets\Scripts\Component\Config\OuterConfig.cs" />
     <Compile Include="Assets\Scripts\Component\Config\RunServerConfig.cs" />
     <Compile Include="Assets\Scripts\Component\ConfigComponent.cs" />
-    <Compile Include="Assets\Scripts\Component\GameObjectComponent.cs" />
     <Compile Include="Assets\Scripts\Component\EventComponent.cs" />
+    <Compile Include="Assets\Scripts\Component\GameObjectComponent.cs" />
     <Compile Include="Assets\Scripts\Component\KVComponent.cs" />
     <Compile Include="Assets\Scripts\Component\MessageDispatherComponent.cs" />
     <Compile Include="Assets\Scripts\Component\NetInnerComponent.cs" />