瀏覽代碼

修复reload dll的bug

tanghai 2 年之前
父節點
當前提交
5eb01c438f

+ 20 - 10
DotNet/Loader/CodeLoader.cs

@@ -11,7 +11,7 @@ namespace ET
     {
     {
         private AssemblyLoadContext assemblyLoadContext;
         private AssemblyLoadContext assemblyLoadContext;
 
 
-        private Assembly model;
+        private Assembly assembly;
 
 
         public void Awake()
         public void Awake()
         {
         {
@@ -20,17 +20,20 @@ namespace ET
             {
             {
                 if (assembly.GetName().Name == "Model")
                 if (assembly.GetName().Name == "Model")
                 {
                 {
-                    this.model = assembly;
+                    this.assembly = assembly;
                     break;
                     break;
                 }
                 }
             }
             }
-            this.LoadHotfix();
+            Assembly hotfixAssembly = this.LoadHotfix();
             
             
-            IStaticMethod start = new StaticMethod(this.model, "ET.Entry", "Start");
+            Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
+            World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
+            
+            IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
             start.Run();
             start.Run();
         }
         }
 
 
-        public void LoadHotfix()
+        private Assembly LoadHotfix()
         {
         {
             assemblyLoadContext?.Unload();
             assemblyLoadContext?.Unload();
             GC.Collect();
             GC.Collect();
@@ -38,12 +41,19 @@ namespace ET
             byte[] dllBytes = File.ReadAllBytes("./Hotfix.dll");
             byte[] dllBytes = File.ReadAllBytes("./Hotfix.dll");
             byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
             byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
             Assembly hotfixAssembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
             Assembly hotfixAssembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
-
-            Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(Assembly.GetEntryAssembly(), typeof(Init).Assembly, typeof (Fiber).Assembly, this.model, hotfixAssembly);
-
-            World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
-            
+            return hotfixAssembly;
+        }
+        
+        public void Reload()
+        {
+            Assembly hotfixAssembly = this.LoadHotfix();
+			
+            Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
+            World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types, true);
+			
             World.Instance.Load();
             World.Instance.Load();
+			
+            Log.Debug($"reload dll finish!");
         }
         }
     }
     }
 }
 }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/Console/ReloadDllConsoleHandler.cs

@@ -6,7 +6,7 @@ namespace ET
         public async ETTask Run(Fiber fiber, ModeContex contex, string content)
         public async ETTask Run(Fiber fiber, ModeContex contex, string content)
         {
         {
             await ETTask.CompletedTask;
             await ETTask.CompletedTask;
-            CodeLoader.Instance.LoadHotfix();
+            CodeLoader.Instance.Reload();
         }
         }
     }
     }
 }
 }

+ 1 - 1
Unity/Assets/Scripts/HotfixView/Client/Demo/Opera/OperaComponentSystem.cs

@@ -30,7 +30,7 @@ namespace ET.Client
 
 
             if (Input.GetKeyDown(KeyCode.R))
             if (Input.GetKeyDown(KeyCode.R))
             {
             {
-                CodeLoader.Instance.LoadHotfix();
+                CodeLoader.Instance.Reload();
                 return;
                 return;
             }
             }
         
         

+ 24 - 7
Unity/Assets/Scripts/Loader/CodeLoader.cs

@@ -64,14 +64,20 @@ namespace ET
 			
 			
 				this.assembly = Assembly.Load(assBytes, pdbBytes);
 				this.assembly = Assembly.Load(assBytes, pdbBytes);
 			}
 			}
-			
-			this.LoadHotfix();
-			
-			IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
-			start.Run();
+
+			{
+				Assembly hotfixAssembly = this.LoadHotfix();
+
+				Dictionary<string, Type> types =
+						AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof (Init).Assembly, this.assembly, hotfixAssembly);
+				World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
+
+				IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
+				start.Run();
+			}
 		}
 		}
 
 
-		public void LoadHotfix()
+		private Assembly LoadHotfix()
 		{
 		{
 			byte[] assBytes;
 			byte[] assBytes;
 			byte[] pdbBytes;
 			byte[] pdbBytes;
@@ -92,9 +98,20 @@ namespace ET
 			}
 			}
 			
 			
 			Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
 			Assembly hotfixAssembly = Assembly.Load(assBytes, pdbBytes);
+
+			return hotfixAssembly;
+		}
+
+		public void Reload()
+		{
+			Assembly hotfixAssembly = this.LoadHotfix();
 			
 			
 			Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
 			Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly);
-			World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types);
+			World.Instance.AddSingleton<EventSystem, Dictionary<string, Type>>(types, true);
+			
+			World.Instance.Load();
+			
+			Log.Debug($"reload dll finish!");
 		}
 		}
 	}
 	}
 }
 }

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Http/HttpDispatcher.cs

@@ -9,7 +9,7 @@ namespace ET.Server
         
         
         public override void Load()
         public override void Load()
         {
         {
-            World.Instance.AddSingleton<HttpDispatcher>();
+            World.Instance.AddSingleton<HttpDispatcher>(true);
         }
         }
 
 
         public void Awake()
         public void Awake()