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

去掉ISingletonLoad接口,需要Reload的Singleton全部由CodeTypes的CreateCodeSingleton创建

tanghai 2 лет назад
Родитель
Сommit
eed4d81c28
27 измененных файлов с 90 добавлено и 140 удалено
  1. 6 6
      DotNet/Loader/CodeLoader.cs
  2. 1 0
      DotNet/Loader/Init.cs
  3. 1 1
      Unity/Assets/Resources/GlobalConfig.asset
  4. 2 6
      Unity/Assets/Scripts/Core/Entity/EntitySystemSingleton.cs
  5. 0 7
      Unity/Assets/Scripts/Core/World/ISingletonLoad.cs
  6. 0 3
      Unity/Assets/Scripts/Core/World/ISingletonLoad.cs.meta
  7. 2 6
      Unity/Assets/Scripts/Core/World/Module/Actor/ActorMessageDispatcherComponent.cs
  8. 7 0
      Unity/Assets/Scripts/Core/World/Module/Code/CodeAttribute.cs
  9. 11 0
      Unity/Assets/Scripts/Core/World/Module/Code/CodeAttribute.cs.meta
  10. 11 0
      Unity/Assets/Scripts/Core/World/Module/Code/CodeTypes.cs
  11. 2 2
      Unity/Assets/Scripts/Core/World/Module/Config/ConfigLoader.cs
  12. 2 6
      Unity/Assets/Scripts/Core/World/Module/EventSystem/EventSystem.cs
  13. 2 2
      Unity/Assets/Scripts/Core/World/Singleton.cs
  14. 10 28
      Unity/Assets/Scripts/Core/World/World.cs
  15. 1 3
      Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs
  16. 0 3
      Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs
  17. 0 2
      Unity/Assets/Scripts/HotfixView/Client/Demo/EntryEvent3_InitClient.cs
  18. 12 13
      Unity/Assets/Scripts/Loader/CodeLoader.cs
  19. 1 0
      Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs
  20. 2 6
      Unity/Assets/Scripts/Model/Server/Module/Http/HttpDispatcher.cs
  21. 5 10
      Unity/Assets/Scripts/Model/Share/Entry.cs
  22. 2 6
      Unity/Assets/Scripts/Model/Share/LockStep/LSEntitySystemSingleton.cs
  23. 2 6
      Unity/Assets/Scripts/Model/Share/Module/AI/AIDispatcherComponent.cs
  24. 2 6
      Unity/Assets/Scripts/Model/Share/Module/Console/ConsoleDispatcher.cs
  25. 2 6
      Unity/Assets/Scripts/Model/Share/Module/Message/MessageDispatcherComponent.cs
  26. 2 6
      Unity/Assets/Scripts/Model/Share/Module/Numeric/NumericWatcherComponent.cs
  27. 2 6
      Unity/Assets/Scripts/ModelView/Client/Module/UI/UIEventComponent.cs

+ 6 - 6
DotNet/Loader/CodeLoader.cs

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

+ 1 - 0
DotNet/Loader/Init.cs

@@ -21,6 +21,7 @@ namespace ET
 						.WithParsed((o)=>World.Instance.AddSingleton(o));
 				World.Instance.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
 				ETTask.ExceptionHandler += Log.Error;
+
 				World.Instance.AddSingleton<CodeLoader>();
 			}
 			catch (Exception e)

+ 1 - 1
Unity/Assets/Resources/GlobalConfig.asset

@@ -12,6 +12,6 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 36527db572638af47b03c805671cba75, type: 3}
   m_Name: GlobalConfig
   m_EditorClassIdentifier: 
-  CodeMode: 1
+  CodeMode: 3
   BuildType: 1
   AppType: 7

+ 2 - 6
Unity/Assets/Scripts/Core/Entity/EntitySystemSingleton.cs

@@ -3,7 +3,8 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class EntitySystemSingleton: Singleton<EntitySystemSingleton>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class EntitySystemSingleton: Singleton<EntitySystemSingleton>, ISingletonAwake
     {
         public TypeSystems TypeSystems { get; private set; }
         
@@ -28,11 +29,6 @@ namespace ET
             }
         }
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<EntitySystemSingleton>(true);
-        }
-        
         public void Serialize(Entity component)
         {
             if (component is not ISerialize)

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

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

+ 0 - 3
Unity/Assets/Scripts/Core/World/ISingletonLoad.cs.meta

@@ -1,3 +0,0 @@
-fileFormatVersion: 2
-guid: a6552160d3164dee967d0dc9db1611a3
-timeCreated: 1687250763

+ 2 - 6
Unity/Assets/Scripts/Core/World/Module/Actor/ActorMessageDispatcherComponent.cs

@@ -19,7 +19,8 @@ namespace ET
     /// <summary>
     /// Actor消息分发组件
     /// </summary>
-    public class ActorMessageDispatcherComponent: Singleton<ActorMessageDispatcherComponent>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class ActorMessageDispatcherComponent: Singleton<ActorMessageDispatcherComponent>, ISingletonAwake
     {
         private readonly Dictionary<Type, List<ActorMessageDispatcherInfo>> ActorMessageHandlers = new();
 
@@ -40,11 +41,6 @@ namespace ET
             }
         }
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<ActorMessageDispatcherComponent>(true);
-        }
-        
         private void Register(Type type)
         {
             object obj = Activator.CreateInstance(type);

+ 7 - 0
Unity/Assets/Scripts/Core/World/Module/Code/CodeAttribute.cs

@@ -0,0 +1,7 @@
+namespace ET
+{
+    public class CodeAttribute: BaseAttribute
+    {
+
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Core/World/Module/Code/CodeAttribute.cs.meta

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

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

@@ -50,5 +50,16 @@ namespace ET
         {
             return this.allTypes[typeName];
         }
+        
+        public void CreateCodeSingleton()
+        {
+            var hashSet = this.GetTypes(typeof (CodeAttribute));
+            foreach (Type type in hashSet)
+            {
+                object obj = Activator.CreateInstance(type);
+                ((ISingletonAwake)obj).Awake();
+                World.Instance.AddSingleton((ASingleton)obj, true);
+            }
+        }
     }
 }

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

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

+ 2 - 6
Unity/Assets/Scripts/Core/World/Module/EventSystem/EventSystem.cs

@@ -3,7 +3,8 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class EventSystem: Singleton<EventSystem>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class EventSystem: Singleton<EventSystem>, ISingletonAwake
     {
         private class EventInfo
         {
@@ -82,11 +83,6 @@ namespace ET
             }
         }
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<EventSystem>(true);
-        }
-
         public async ETTask PublishAsync<S, T>(S scene, T a) where S: class, IScene where T : struct
         {
             List<EventInfo> iEvents;

+ 2 - 2
Unity/Assets/Scripts/Core/World/Singleton.cs

@@ -2,7 +2,7 @@
 {
     public abstract class ASingleton: DisposeObject
     {
-        public abstract void Register();
+        internal abstract void Register();
     }
     
     public abstract class Singleton<T>: ASingleton where T: Singleton<T>, new()
@@ -24,7 +24,7 @@
             }
         }
 
-        public override void Register()
+        internal override void Register()
         {
             Instance = (T)this;
         }

+ 10 - 28
Unity/Assets/Scripts/Core/World/World.cs

@@ -37,48 +37,48 @@ namespace ET
             }
         }
 
-        public T AddSingleton<T>(bool replace = false) where T : ASingleton, ISingletonAwake, new()
+        public T AddSingleton<T>(bool noStack = false) where T : ASingleton, ISingletonAwake, new()
         {
             T singleton = new();
             singleton.Awake();
 
-            AddSingleton(singleton, replace);
+            AddSingleton(singleton, noStack);
             return singleton;
         }
         
-        public T AddSingleton<T, A>(A a, bool replace = false) where T : ASingleton, ISingletonAwake<A>, new()
+        public T AddSingleton<T, A>(A a, bool noStack = false) where T : ASingleton, ISingletonAwake<A>, new()
         {
             T singleton = new();
             singleton.Awake(a);
 
-            AddSingleton(singleton, replace);
+            AddSingleton(singleton, noStack);
             return singleton;
         }
         
-        public T AddSingleton<T, A, B>(A a, B b, bool replace = false) where T : ASingleton, ISingletonAwake<A, B>, new()
+        public T AddSingleton<T, A, B>(A a, B b, bool noStack = false) where T : ASingleton, ISingletonAwake<A, B>, new()
         {
             T singleton = new();
             singleton.Awake(a, b);
 
-            AddSingleton(singleton, replace);
+            AddSingleton(singleton, noStack);
             return singleton;
         }
         
-        public T AddSingleton<T, A, B, C>(A a, B b, C c, bool replace = false) where T : ASingleton, ISingletonAwake<A, B, C>, new()
+        public T AddSingleton<T, A, B, C>(A a, B b, C c, bool noStack = false) where T : ASingleton, ISingletonAwake<A, B, C>, new()
         {
             T singleton = new();
             singleton.Awake(a, b, c);
 
-            AddSingleton(singleton, replace);
+            AddSingleton(singleton, noStack);
             return singleton;
         }
 
-        public void AddSingleton(ASingleton singleton, bool replace = false)
+        public void AddSingleton(ASingleton singleton, bool noStack = false)
         {
             lock (this)
             {
                 Type type = singleton.GetType();
-                if (!replace)
+                if (!noStack)
                 {
                     this.stack.Push(type);    
                 }
@@ -87,23 +87,5 @@ namespace ET
 
             singleton.Register();
         }
-        
-        public void Load()
-        {
-            lock (this)
-            {
-                foreach (Type type in this.stack)
-                {
-                    ASingleton singleton = this.singletons[type];
-
-                    if (singleton is not ISingletonLoad iSingletonLoad)
-                    {
-                        continue;
-                    }
-                    
-                    iSingletonLoad.Load();
-                }
-            }
-        }
     }
 }

+ 1 - 3
Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs

@@ -44,9 +44,7 @@ namespace ET
                 return;
             }
 
-            Logger log = new();
-            log.Register();
-            log.ILog = new UnityLogger();
+            World.Instance.AddSingleton<Logger>().ILog = new UnityLogger();
         }
 
         private static void DestroyLog()

+ 0 - 3
Unity/Assets/Scripts/Hotfix/Server/Demo/EntryEvent2_InitServer.cs

@@ -8,9 +8,6 @@ namespace ET.Server
     {
         protected override async ETTask Run(Scene root, ET.EventType.EntryEvent2 args)
         {
-            World.Instance.AddSingleton<HttpDispatcher>();
-            World.Instance.AddSingleton<ConsoleDispatcher>();
-            
             switch (Options.Instance.AppType)
             {
                 case AppType.Server:

+ 0 - 2
Unity/Assets/Scripts/HotfixView/Client/Demo/EntryEvent3_InitClient.cs

@@ -8,8 +8,6 @@ namespace ET.Client
     {
         protected override async ETTask Run(Scene root, EventType.EntryEvent3 args)
         {
-            World.Instance.AddSingleton<UIEventComponent>();
-            
             root.AddComponent<TimerComponent>();
             root.AddComponent<CoroutineLockComponent>();
             

+ 12 - 13
Unity/Assets/Scripts/Loader/CodeLoader.cs

@@ -27,8 +27,6 @@ namespace ET
 				
 				Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
 				
-				World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
-				
 				foreach (Assembly ass in assemblies)
 				{
 					string name = ass.GetName().Name;
@@ -37,6 +35,8 @@ namespace ET
 						this.assembly = ass;
 					}
 				}
+				
+				World.Instance.AddSingleton<CodeTypes, Assembly[]>(assemblies);
 			}
 			else
 			{
@@ -64,16 +64,13 @@ namespace ET
 				}
 			
 				this.assembly = Assembly.Load(assBytes, pdbBytes);
-			}
 
-			{
 				Assembly hotfixAssembly = this.LoadHotfix();
-
-				World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly});
-
-				IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
-				start.Run();
+				World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
 			}
+			
+			IStaticMethod start = new StaticMethod(this.assembly, "ET.Entry", "Start");
+			start.Run();
 		}
 
 		private Assembly LoadHotfix()
@@ -105,11 +102,13 @@ namespace ET
 		{
 			Assembly hotfixAssembly = this.LoadHotfix();
 
-			World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
-			
-			World.Instance.Load();
-			
+			CodeTypes codeTypes = World.Instance.AddSingleton<CodeTypes, Assembly[]>(new []{typeof (World).Assembly, typeof(Init).Assembly, this.assembly, hotfixAssembly}, true);
+
+			codeTypes.CreateCodeSingleton();
+
 			Log.Debug($"reload dll finish!");
 		}
+
+
 	}
 }

+ 1 - 0
Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs

@@ -25,6 +25,7 @@ namespace ET
 			
 			World.Instance.AddSingleton<Logger>().ILog = new UnityLogger();
 			ETTask.ExceptionHandler += Log.Error;
+			
 			World.Instance.AddSingleton<CodeLoader>().Start();
 		}
 

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

@@ -3,15 +3,11 @@ using System.Collections.Generic;
 
 namespace ET.Server
 {
-    public class HttpDispatcher: Singleton<HttpDispatcher>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class HttpDispatcher: Singleton<HttpDispatcher>, ISingletonAwake
     {
         private readonly Dictionary<string, Dictionary<int, IHttpHandler>> dispatcher = new();
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<HttpDispatcher>(true);
-        }
-
         public void Awake()
         {
             HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof (HttpHandlerAttribute));

+ 5 - 10
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -36,24 +36,19 @@ namespace ET
             
             MongoHelper.RegisterStruct<LSInput>();
             MongoHelper.Register();
-
-            World.Instance.AddSingleton<EventSystem>();
+            
             World.Instance.AddSingleton<OpcodeType>();
             World.Instance.AddSingleton<IdValueGenerater>();
             World.Instance.AddSingleton<ObjectPool>();
             World.Instance.AddSingleton<ActorMessageQueue>();
-            World.Instance.AddSingleton<EntitySystemSingleton>();
-            World.Instance.AddSingleton<LSEntitySystemSingleton>();
-            World.Instance.AddSingleton<MessageDispatcherComponent>();
-            World.Instance.AddSingleton<NumericWatcherComponent>();
-            World.Instance.AddSingleton<AIDispatcherComponent>();
-            World.Instance.AddSingleton<ActorMessageDispatcherComponent>();
             World.Instance.AddSingleton<NetServices>();
             World.Instance.AddSingleton<NavmeshComponent>();
-            World.Instance.AddSingleton<LogMsg>();
-            
             World.Instance.AddSingleton<FiberManager>();
+            World.Instance.AddSingleton<LogMsg>();
             
+            // 创建需要reload的code singleton
+            CodeTypes.Instance.CreateCodeSingleton();
+
             await FiberManager.Instance.Create(SchedulerType.Main, ConstFiberId.Main, 0, SceneType.Main, "");
         }
     }

+ 2 - 6
Unity/Assets/Scripts/Model/Share/LockStep/LSEntitySystemSingleton.cs

@@ -11,7 +11,8 @@ namespace ET
         public const int Max = 1;
     }
     
-    public class LSEntitySystemSingleton: Singleton<LSEntitySystemSingleton>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class LSEntitySystemSingleton: Singleton<LSEntitySystemSingleton>, ISingletonAwake
     {
         public TypeSystems TypeSystems { get; private set; }
         
@@ -37,11 +38,6 @@ namespace ET
             }
         }
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<LSEntitySystemSingleton>(true);
-        }
-        
         public TypeSystems.OneTypeSystems GetOneTypeSystems(Type type)
         {
             return this.TypeSystems.GetOneTypeSystems(type);

+ 2 - 6
Unity/Assets/Scripts/Model/Share/Module/AI/AIDispatcherComponent.cs

@@ -3,7 +3,8 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class AIDispatcherComponent: Singleton<AIDispatcherComponent>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class AIDispatcherComponent: Singleton<AIDispatcherComponent>, ISingletonAwake
     {
         private readonly Dictionary<string, AAIHandler> aiHandlers = new();
         
@@ -27,10 +28,5 @@ namespace ET
             this.aiHandlers.TryGetValue(key, out var aaiHandler);
             return aaiHandler;
         }
-
-        public void Load()
-        {
-            World.Instance.AddSingleton<AIDispatcherComponent>(true);
-        }
     }
 }

+ 2 - 6
Unity/Assets/Scripts/Model/Share/Module/Console/ConsoleDispatcher.cs

@@ -3,15 +3,11 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class ConsoleDispatcher: Singleton<ConsoleDispatcher>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class ConsoleDispatcher: Singleton<ConsoleDispatcher>, ISingletonAwake
     {
         private readonly Dictionary<string, IConsoleHandler> handlers = new();
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<ConsoleDispatcher>(true);
-        }
-
         public void Awake()
         {
             HashSet<Type> types = CodeTypes.Instance.GetTypes(typeof (ConsoleHandlerAttribute));

+ 2 - 6
Unity/Assets/Scripts/Model/Share/Module/Message/MessageDispatcherComponent.cs

@@ -15,7 +15,8 @@ namespace ET
         }
     }
     
-    public class MessageDispatcherComponent: Singleton<MessageDispatcherComponent>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class MessageDispatcherComponent: Singleton<MessageDispatcherComponent>, ISingletonAwake
     {
         private readonly Dictionary<ushort, List<MessageDispatcherInfo>> handlers = new();
         
@@ -53,11 +54,6 @@ namespace ET
             }
         }
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<MessageDispatcherComponent>(true);
-        }
-        
         private void RegisterHandler(ushort opcode, MessageDispatcherInfo handler)
         {
             if (!this.handlers.ContainsKey(opcode))

+ 2 - 6
Unity/Assets/Scripts/Model/Share/Module/Numeric/NumericWatcherComponent.cs

@@ -18,7 +18,8 @@ namespace ET
     /// <summary>
     /// 监视数值变化组件,分发监听
     /// </summary>
-    public class NumericWatcherComponent : Singleton<NumericWatcherComponent>, ISingletonAwake, ISingletonLoad
+    [Code]
+    public class NumericWatcherComponent : Singleton<NumericWatcherComponent>, ISingletonAwake
     {
         private readonly Dictionary<int, List<NumericWatcherInfo>> allWatchers = new();
         
@@ -43,11 +44,6 @@ namespace ET
             }
         }
         
-        public void Load()
-        {
-            World.Instance.AddSingleton<NumericWatcherComponent>(true);
-        }
-        
         public void Run(Unit unit, EventType.NumbericChange args)
         {
             List<NumericWatcherInfo> list;

+ 2 - 6
Unity/Assets/Scripts/ModelView/Client/Module/UI/UIEventComponent.cs

@@ -6,7 +6,8 @@ namespace ET.Client
 	/// <summary>
 	/// 管理所有UI GameObject
 	/// </summary>
-	public class UIEventComponent: Singleton<UIEventComponent>, ISingletonLoad, ISingletonAwake
+	[Code]
+	public class UIEventComponent: Singleton<UIEventComponent>, ISingletonAwake
 	{
 		public Dictionary<string, AUIEvent> UIEvents { get; } = new();
 		
@@ -26,10 +27,5 @@ namespace ET.Client
                 this.UIEvents.Add(uiEventAttribute.UIType, aUIEvent);
             }
         }
-        
-        public void Load()
-        {
-	        World.Instance.AddSingleton<UIEventComponent>(true);
-        }
 	}
 }