Просмотр исходного кода

增加一个接口ISingletonReverseDispose,有这个接口的对象,在加到world中的时候,会加到stack中
dispose会按照添加的反顺序进行dispose,大部分singleton没有释放依赖,并不需要加这个接口

tanghai 2 лет назад
Родитель
Сommit
e369e0b6ed

+ 3 - 3
DotNet/Loader/CodeLoader.cs

@@ -25,8 +25,8 @@ namespace ET
             }
             }
 
 
             Assembly hotfixAssembly = this.LoadHotfix();
             Assembly hotfixAssembly = this.LoadHotfix();
-            
-            World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly }, true);
+
+            World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly });
 
 
             IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
             IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
             start.Run();
             start.Run();
@@ -47,7 +47,7 @@ namespace ET
         {
         {
             Assembly hotfixAssembly = this.LoadHotfix();
             Assembly hotfixAssembly = this.LoadHotfix();
 			
 			
-            CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly }, true);
+            CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly });
 
 
             codeTypes.CreateCodeSingleton();
             codeTypes.CreateCodeSingleton();
             Log.Debug($"reload dll finish!");
             Log.Debug($"reload dll finish!");

+ 0 - 16
Share/Tool/Init.cs

@@ -23,14 +23,6 @@ namespace ET.Server
                     .WithParsed((o)=>World.Instance.AddSingleton(o));
                     .WithParsed((o)=>World.Instance.AddSingleton(o));
                 World.Instance.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
                 World.Instance.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
                 
                 
-                //Process process = Game.Instance.Create();
-                // 异步方法全部会回掉到主线程
-                //process.AddSingleton<MainThreadSynchronizationContext>();
-                //process.AddSingleton<TimeInfo>();
-                //process.AddSingleton<ObjectPool>();
-                //process.AddSingleton<IdGenerater>();
-                
-                
                 World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (Init).Assembly });
                 World.Instance.AddSingleton<CodeTypes, Assembly[]>(new[] { typeof (Init).Assembly });
                 World.Instance.AddSingleton<EventSystem>();
                 World.Instance.AddSingleton<EventSystem>();
                 
                 
@@ -38,14 +30,6 @@ namespace ET.Server
                 
                 
                 ETTask.ExceptionHandler += Log.Error;
                 ETTask.ExceptionHandler += Log.Error;
                 
                 
-
-//
-                //process.AddSingleton<EntitySystemSingleton>();
-                //
-                //process.AddSingleton<Root>();
-
-                //MongoHelper.Register();
-				
                 Log.Info($"server start........................ ");
                 Log.Info($"server start........................ ");
 				
 				
                 switch (Options.Instance.AppType)
                 switch (Options.Instance.AppType)

+ 0 - 7
Unity/Assets/Scripts/Core/World/ISingletonDestroy.cs

@@ -1,7 +0,0 @@
-namespace ET
-{
-    public interface ISingletonDestroy
-    {
-        void Destroy();
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/Core/World/ISingletonDestroy.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 05613b4e63ae5a14887a34e97a0c1a0f
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 2 - 2
Unity/Assets/Scripts/Core/World/Module/Code/CodeTypes.cs

@@ -8,7 +8,7 @@ namespace ET
     {
     {
         private readonly Dictionary<string, Type> allTypes = new();
         private readonly Dictionary<string, Type> allTypes = new();
         private readonly UnOrderMultiMapSet<Type, Type> types = new();
         private readonly UnOrderMultiMapSet<Type, Type> types = new();
-        
+
         public void Awake(Assembly[] assemblies)
         public void Awake(Assembly[] assemblies)
         {
         {
             Dictionary<string, Type> addTypes = AssemblyHelper.GetAssemblyTypes(assemblies);
             Dictionary<string, Type> addTypes = AssemblyHelper.GetAssemblyTypes(assemblies);
@@ -58,7 +58,7 @@ namespace ET
             {
             {
                 object obj = Activator.CreateInstance(type);
                 object obj = Activator.CreateInstance(type);
                 ((ISingletonAwake)obj).Awake();
                 ((ISingletonAwake)obj).Awake();
-                World.Instance.AddSingleton((ASingleton)obj, true);
+                World.Instance.AddSingleton((ASingleton)obj);
             }
             }
         }
         }
     }
     }

+ 2 - 2
Unity/Assets/Scripts/Core/World/Module/Config/ConfigLoader.cs

@@ -34,7 +34,7 @@ namespace ET
 			ASingleton singleton = category as ASingleton;
 			ASingleton singleton = category as ASingleton;
 			this.allConfig[configType] = singleton;
 			this.allConfig[configType] = singleton;
 			
 			
-			World.Instance.AddSingleton(singleton, true);
+			World.Instance.AddSingleton(singleton);
 		}
 		}
 		
 		
 		public void Load()
 		public void Load()
@@ -75,7 +75,7 @@ namespace ET
 				ASingleton singleton = category as ASingleton;
 				ASingleton singleton = category as ASingleton;
 				this.allConfig[configType] = singleton;
 				this.allConfig[configType] = singleton;
 				
 				
-				World.Instance.AddSingleton(singleton, true);
+				World.Instance.AddSingleton(singleton);
 			}
 			}
 		}
 		}
     }
     }

+ 1 - 1
Unity/Assets/Scripts/Core/World/Module/Fiber/FiberManager.cs

@@ -13,7 +13,7 @@ namespace ET
         ThreadPool,
         ThreadPool,
     }
     }
     
     
-    public class FiberManager: Singleton<FiberManager>, ISingletonAwake
+    public class FiberManager: Singleton<FiberManager>, ISingletonAwake, ISingletonReverseDispose
     {
     {
         private readonly IScheduler[] schedulers = new IScheduler[3];
         private readonly IScheduler[] schedulers = new IScheduler[3];
         
         

+ 6 - 1
Unity/Assets/Scripts/Core/World/Singleton.cs

@@ -1,11 +1,16 @@
 namespace ET
 namespace ET
 {
 {
+    public interface ISingletonReverseDispose
+    {
+        
+    }
+    
     public abstract class ASingleton: DisposeObject
     public abstract class ASingleton: DisposeObject
     {
     {
         internal abstract void Register();
         internal abstract void Register();
     }
     }
     
     
-    public abstract class Singleton<T>: ASingleton where T: Singleton<T>, new()
+    public abstract class Singleton<T>: ASingleton where T: Singleton<T>
     {
     {
         private bool isDisposed;
         private bool isDisposed;
         
         

+ 21 - 12
Unity/Assets/Scripts/Core/World/World.cs

@@ -32,55 +32,64 @@ namespace ET
                 while (this.stack.Count > 0)
                 while (this.stack.Count > 0)
                 {
                 {
                     Type type = this.stack.Pop();
                     Type type = this.stack.Pop();
-                    this.singletons[type].Dispose();
+                    if (this.singletons.Remove(type, out ASingleton singleton))
+                    {
+                        singleton.Dispose();
+                    }
+                }
+
+                // dispose剩下的singleton,主要为了把instance置空
+                foreach (var kv in this.singletons)
+                {
+                    kv.Value.Dispose();
                 }
                 }
             }
             }
         }
         }
 
 
-        public T AddSingleton<T>(bool noStack = false) where T : ASingleton, ISingletonAwake, new()
+        public T AddSingleton<T>() where T : ASingleton, ISingletonAwake, new()
         {
         {
             T singleton = new();
             T singleton = new();
             singleton.Awake();
             singleton.Awake();
 
 
-            AddSingleton(singleton, noStack);
+            AddSingleton(singleton);
             return singleton;
             return singleton;
         }
         }
         
         
-        public T AddSingleton<T, A>(A a, bool noStack = false) where T : ASingleton, ISingletonAwake<A>, new()
+        public T AddSingleton<T, A>(A a) where T : ASingleton, ISingletonAwake<A>, new()
         {
         {
             T singleton = new();
             T singleton = new();
             singleton.Awake(a);
             singleton.Awake(a);
 
 
-            AddSingleton(singleton, noStack);
+            AddSingleton(singleton);
             return singleton;
             return singleton;
         }
         }
         
         
-        public T AddSingleton<T, A, B>(A a, B b, bool noStack = false) where T : ASingleton, ISingletonAwake<A, B>, new()
+        public T AddSingleton<T, A, B>(A a, B b) where T : ASingleton, ISingletonAwake<A, B>, new()
         {
         {
             T singleton = new();
             T singleton = new();
             singleton.Awake(a, b);
             singleton.Awake(a, b);
 
 
-            AddSingleton(singleton, noStack);
+            AddSingleton(singleton);
             return singleton;
             return singleton;
         }
         }
         
         
-        public T AddSingleton<T, A, B, C>(A a, B b, C c, bool noStack = false) where T : ASingleton, ISingletonAwake<A, B, C>, new()
+        public T AddSingleton<T, A, B, C>(A a, B b, C c) where T : ASingleton, ISingletonAwake<A, B, C>, new()
         {
         {
             T singleton = new();
             T singleton = new();
             singleton.Awake(a, b, c);
             singleton.Awake(a, b, c);
 
 
-            AddSingleton(singleton, noStack);
+            AddSingleton(singleton);
             return singleton;
             return singleton;
         }
         }
 
 
-        public void AddSingleton(ASingleton singleton, bool noStack = false)
+        public void AddSingleton(ASingleton singleton)
         {
         {
             lock (this)
             lock (this)
             {
             {
                 Type type = singleton.GetType();
                 Type type = singleton.GetType();
-                if (!noStack)
+                if (singleton is ISingletonReverseDispose)
                 {
                 {
-                    this.stack.Push(type);    
+                    this.stack.Push(type);
                 }
                 }
                 singletons[type] = singleton;
                 singletons[type] = singleton;
             }
             }

+ 4 - 6
Unity/Assets/Scripts/Loader/CodeLoader.cs

@@ -36,7 +36,7 @@ namespace ET
 					}
 					}
 				}
 				}
 				
 				
-				World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies, true);
+				World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
 			}
 			}
 			else
 			else
 			{
 			{
@@ -66,7 +66,8 @@ namespace ET
 				this.assembly = Assembly.Load(assBytes, pdbBytes);
 				this.assembly = Assembly.Load(assBytes, pdbBytes);
 
 
 				Assembly hotfixAssembly = this.LoadHotfix();
 				Assembly hotfixAssembly = this.LoadHotfix();
-				World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
+				
+				World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
 			}
 			}
 			
 			
 			IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
 			IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
@@ -102,13 +103,10 @@ namespace ET
 		{
 		{
 			Assembly hotfixAssembly = this.LoadHotfix();
 			Assembly hotfixAssembly = this.LoadHotfix();
 
 
-			CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
-
+			CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
 			codeTypes.CreateCodeSingleton();
 			codeTypes.CreateCodeSingleton();
 
 
 			Log.Debug($"reload dll finish!");
 			Log.Debug($"reload dll finish!");
 		}
 		}
-
-
 	}
 	}
 }
 }