Parcourir la source

1.Assembly使用AssemblyLoadContext加载,方便加载了新的Assembly,卸载旧的Assembly,这个非常有用,否则线上几十个进程,一个进程一次reload增加10来M内存,几十个进程就是几百M内存,内存经不起几次reload消耗
2.增加Console Reload指令,输入R即可Reload dll,这是方便开发使用,线上需要自己开发,向进程发送消息进行Reload

tanghai il y a 4 ans
Parent
commit
22f15d1133

+ 21 - 0
Server/Hotfix/Module/Console/ReloadDllConsoleHandler.cs

@@ -0,0 +1,21 @@
+namespace ET
+{
+    [ConsoleHandler(ConsoleMode.ReloadDll)]
+    public class ReloadDllConsoleHandler: IConsoleHandler
+    {
+        public async ETTask Run(ModeContex contex, string content)
+        {
+            switch (content)
+            {
+                case ConsoleMode.ReloadDll:
+                    contex.Parent.RemoveComponent<ModeContex>();
+                    
+                    Game.EventSystem.Add(DllHelper.GetHotfixAssembly());
+                    Game.EventSystem.Load();
+                    break;
+            }
+            
+            await ETTask.CompletedTask;
+        }
+    }
+}

+ 7 - 1
Server/Model/Base/DllHelper.cs

@@ -1,15 +1,21 @@
 using System.IO;
 using System.Reflection;
+using System.Runtime.Loader;
 
 namespace ET
 {
     public static class DllHelper
     {
+        private static AssemblyLoadContext assemblyLoadContext;
+        
         public static Assembly GetHotfixAssembly()
         {
+            assemblyLoadContext?.Unload();
+            System.GC.Collect();
+            assemblyLoadContext = new AssemblyLoadContext("Hotfix", true);
             byte[] dllBytes = File.ReadAllBytes("./Server.Hotfix.dll");
             byte[] pdbBytes = File.ReadAllBytes("./Server.Hotfix.pdb");
-            Assembly assembly = Assembly.Load(dllBytes, pdbBytes);
+            Assembly assembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
             return assembly;
         }
     }