Quellcode durchsuchen

1.Entity Domain改成IScene
2.增加一个Process层级,类似erlang的Process,Process由Game管理
3.增加ProcessSingleton

tanghai vor 2 Jahren
Ursprung
Commit
890c24cfd1
73 geänderte Dateien mit 576 neuen und 313 gelöschten Zeilen
  1. 1 1
      DotNet/App/Program.cs
  2. 2 2
      DotNet/Loader/CodeLoader.cs
  3. 17 13
      DotNet/Loader/Init.cs
  4. 15 12
      Share/Tool/Init.cs
  5. 4 4
      Unity/Assets/Scripts/Core/Helper/ProcessHelper.cs
  6. 5 5
      Unity/Assets/Scripts/Core/Module/Config/ConfigComponent.cs
  7. 1 1
      Unity/Assets/Scripts/Core/Module/CoroutineLock/CoroutineLockComponent.cs
  8. 17 14
      Unity/Assets/Scripts/Core/Module/Entity/Entity.cs
  9. 4 4
      Unity/Assets/Scripts/Core/Module/Entity/EntitySceneFactory.cs
  10. 2 2
      Unity/Assets/Scripts/Core/Module/Entity/Root.cs
  11. 5 3
      Unity/Assets/Scripts/Core/Module/Entity/Scene.cs
  12. 3 3
      Unity/Assets/Scripts/Core/Module/Entity/SceneHelper.cs
  13. 1 1
      Unity/Assets/Scripts/Core/Module/EventSystem/EntitySystemSingleton.cs
  14. 2 2
      Unity/Assets/Scripts/Core/Module/EventSystem/EventSystem.cs
  15. 1 1
      Unity/Assets/Scripts/Core/Module/IdGenerater/IdGenerater.cs
  16. 1 1
      Unity/Assets/Scripts/Core/Module/Log/Logger.cs
  17. 4 4
      Unity/Assets/Scripts/Core/Module/Network/NetServices.cs
  18. 1 1
      Unity/Assets/Scripts/Core/Module/ObjectPool/ObjectPool.cs
  19. 1 1
      Unity/Assets/Scripts/Core/Module/Synchronization/MainThreadSynchronizationContext.cs
  20. 1 1
      Unity/Assets/Scripts/Core/Module/Time/TimeInfo.cs
  21. 1 1
      Unity/Assets/Scripts/Core/Module/Timer/TimerComponent.cs
  22. 1 1
      Unity/Assets/Scripts/Core/Singleton/ISingletonAwake.cs
  23. 1 1
      Unity/Assets/Scripts/Core/Singleton/ISingletonLateUpdate.cs
  24. 1 1
      Unity/Assets/Scripts/Core/Singleton/ISingletonLoad.cs
  25. 1 1
      Unity/Assets/Scripts/Core/Singleton/ISingletonUpdate.cs
  26. 260 0
      Unity/Assets/Scripts/Core/Singleton/Process.cs
  27. 11 0
      Unity/Assets/Scripts/Core/Singleton/Process.cs.meta
  28. 8 5
      Unity/Assets/Scripts/Core/Singleton/Singleton.cs
  29. 8 0
      Unity/Assets/Scripts/Core/World.meta
  30. 68 70
      Unity/Assets/Scripts/Core/World/Game.cs
  31. 1 1
      Unity/Assets/Scripts/Core/World/Game.cs.meta
  32. 0 0
      Unity/Assets/Scripts/Core/World/IMessage.cs
  33. 1 1
      Unity/Assets/Scripts/Core/World/IMessage.cs.meta
  34. 1 1
      Unity/Assets/Scripts/Core/World/ISingletonAwake.cs
  35. 11 0
      Unity/Assets/Scripts/Core/World/ISingletonAwake.cs.meta
  36. 1 1
      Unity/Assets/Scripts/Core/World/ISingletonLateUpdate.cs
  37. 11 0
      Unity/Assets/Scripts/Core/World/ISingletonLateUpdate.cs.meta
  38. 1 1
      Unity/Assets/Scripts/Core/World/ISingletonLoad.cs
  39. 11 0
      Unity/Assets/Scripts/Core/World/ISingletonLoad.cs.meta
  40. 1 1
      Unity/Assets/Scripts/Core/World/ISingletonUpdate.cs
  41. 11 0
      Unity/Assets/Scripts/Core/World/ISingletonUpdate.cs.meta
  42. 6 6
      Unity/Assets/Scripts/Core/World/Singleton.cs
  43. 11 0
      Unity/Assets/Scripts/Core/World/Singleton.cs.meta
  44. 0 73
      Unity/Assets/Scripts/Core/World/World.cs
  45. 2 2
      Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs
  46. 1 1
      Unity/Assets/Scripts/Editor/Helper/ShellHelper.cs
  47. 1 1
      Unity/Assets/Scripts/Hotfix/Client/Demo/Scene/SceneChangeHelper.cs
  48. 4 4
      Unity/Assets/Scripts/Hotfix/Client/Demo/Scene/SceneFactory.cs
  49. 2 2
      Unity/Assets/Scripts/Hotfix/Client/Module/Message/NetClientComponentSystem.cs
  50. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Helper/SceneFactory.cs
  51. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Gate/C2G_LoginGateHandler.cs
  52. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Map/Transfer/TransferHelper.cs
  53. 2 2
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotCaseSystem.cs
  54. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotManagerComponentSystem.cs
  55. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/Scene/RobotSceneFactory.cs
  56. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Watcher/WatcherComponentSystem.cs
  57. 2 2
      Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Watcher/WatcherHelper.cs
  58. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageDispatcherComponentSystem.cs
  59. 1 1
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetServerComponentSystem.cs
  60. 1 1
      Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs
  61. 1 1
      Unity/Assets/Scripts/Hotfix/Share/Module/Message/MessageDispatcherComponentSystem.cs
  62. 1 1
      Unity/Assets/Scripts/HotfixView/Client/Demo/EntryEvent3_InitClient.cs
  63. 1 1
      Unity/Assets/Scripts/HotfixView/Client/Demo/Opera/OperaComponentSystem.cs
  64. 1 1
      Unity/Assets/Scripts/HotfixView/Client/LockStep/LSUnitViewSystem.cs
  65. 1 1
      Unity/Assets/Scripts/Loader/GlobalComponent.cs
  66. 21 17
      Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs
  67. 1 1
      Unity/Assets/Scripts/Model/Server/Demo/Watcher/WatcherComponent.cs
  68. 6 22
      Unity/Assets/Scripts/Model/Share/Entry.cs
  69. 1 1
      Unity/Assets/Scripts/Model/Share/LockStep/LSEntitySystemSingleton.cs
  70. 1 1
      Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs
  71. 1 1
      Unity/Assets/Scripts/Model/Share/Module/Message/OpcodeHelper.cs
  72. 1 1
      Unity/Assets/Scripts/Model/Share/Module/Numeric/NumericWatcherComponent.cs
  73. 1 0
      Unity/Assets/Scripts/ThirdParty/ETTask/ETTask.cs

+ 1 - 1
DotNet/App/Program.cs

@@ -19,7 +19,7 @@ namespace ET
             
             while (true)
             {
-                Thread.Sleep(1);
+                System.Threading.Thread.Sleep(1);
                 try
                 {
                     Init.Update();

+ 2 - 2
DotNet/Loader/CodeLoader.cs

@@ -7,7 +7,7 @@ using System.Runtime.Loader;
 namespace ET
 {
    
-    public class CodeLoader: Singleton<CodeLoader>
+    public class CodeLoader: ProcessSingleton<CodeLoader>
     {
         private AssemblyLoadContext assemblyLoadContext;
 
@@ -39,7 +39,7 @@ namespace ET
             byte[] pdbBytes = File.ReadAllBytes("./Hotfix.pdb");
             Assembly hotfixAssembly = assemblyLoadContext.LoadFromStream(new MemoryStream(dllBytes), new MemoryStream(pdbBytes));
 
-            Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(Assembly.GetEntryAssembly(), typeof(Init).Assembly, typeof (Game).Assembly, this.model, hotfixAssembly);
+            Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(Assembly.GetEntryAssembly(), typeof(Init).Assembly, typeof (Process).Assembly, this.model, hotfixAssembly);
 			
             EventSystem.Instance.Add(types);
         }

+ 17 - 13
DotNet/Loader/Init.cs

@@ -7,6 +7,8 @@ namespace ET
 {
 	public static class Init
 	{
+		private static Process process;
+		
 		public static void Start()
 		{
 			try
@@ -15,28 +17,30 @@ namespace ET
 				{
 					Log.Error(e.ExceptionObject.ToString());
 				};
+
+				process = Game.Instance.Create(false);
 				
 				// 异步方法全部会回掉到主线程
-				Game.AddSingleton<MainThreadSynchronizationContext>();
+				process.AddSingleton<MainThreadSynchronizationContext>();
 
 				// 命令行参数
 				Parser.Default.ParseArguments<Options>(System.Environment.GetCommandLineArgs())
 					.WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
-					.WithParsed(Game.AddSingleton);
+					.WithParsed(Game.Instance.AddSingleton);
 				
-				Game.AddSingleton<TimeInfo>();
-				Game.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
-				Game.AddSingleton<ObjectPool>();
-				Game.AddSingleton<IdGenerater>();
-				Game.AddSingleton<EventSystem>();
-				Game.AddSingleton<TimerComponent>();
-				Game.AddSingleton<CoroutineLockComponent>();
+				process.AddSingleton<TimeInfo>();
+				process.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
+				process.AddSingleton<ObjectPool>();
+				process.AddSingleton<IdGenerater>();
+				process.AddSingleton<EventSystem>();
+				process.AddSingleton<TimerComponent>();
+				process.AddSingleton<CoroutineLockComponent>();
 				
 				ETTask.ExceptionHandler += Log.Error;
 				
 				Log.Console($"{Parser.Default.FormatCommandLine(Options.Instance)}");
 
-				Game.AddSingleton<CodeLoader>().Start();
+				process.AddSingleton<CodeLoader>().Start();
 			}
 			catch (Exception e)
 			{
@@ -46,17 +50,17 @@ namespace ET
 
 		public static void Update()
 		{
-			Game.Update();
+			process.Update();
 		}
 
 		public static void LateUpdate()
 		{
-			Game.LateUpdate();
+			process.LateUpdate();
 		}
 
 		public static void FrameFinishUpdate()
 		{
-			Game.FrameFinishUpdate();
+			process.FrameFinishUpdate();
 		}
 	}
 }

+ 15 - 12
Share/Tool/Init.cs

@@ -7,6 +7,8 @@ namespace ET.Server
 {
     internal static class Init
     {
+        private static Process process;
+        
         private static int Main(string[] args)
         {
             AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
@@ -16,28 +18,29 @@ namespace ET.Server
             
             try
             {
-                // 异步方法全部会回掉到主线程
-                Game.AddSingleton<MainThreadSynchronizationContext>();
-				
                 // 命令行参数
                 Parser.Default.ParseArguments<Options>(args)
                     .WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
-                    .WithParsed(Game.AddSingleton);
+                    .WithParsed(Game.Instance.AddSingleton);
+                
+                process = Game.Instance.Create(false);
+                // 异步方法全部会回掉到主线程
+                process.AddSingleton<MainThreadSynchronizationContext>();
 				
-                Game.AddSingleton<TimeInfo>();
-                Game.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
-                Game.AddSingleton<ObjectPool>();
-                Game.AddSingleton<IdGenerater>();
+                process.AddSingleton<TimeInfo>();
+                process.AddSingleton<Logger>().ILog = new NLogger(Options.Instance.AppType.ToString(), Options.Instance.Process, "../Config/NLog/NLog.config");
+                process.AddSingleton<ObjectPool>();
+                process.AddSingleton<IdGenerater>();
                 
                 ETTask.ExceptionHandler += Log.Error;
                 
-                Game.AddSingleton<EventSystem>();
-                Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Game).Assembly);
+                process.AddSingleton<EventSystem>();
+                Dictionary<string, Type> types = AssemblyHelper.GetAssemblyTypes(typeof (Process).Assembly);
                 EventSystem.Instance.Add(types);
 
-                Game.AddSingleton<EntitySystemSingleton>();
+                process.AddSingleton<EntitySystemSingleton>();
                 
-                Game.AddSingleton<Root>();
+                process.AddSingleton<Root>();
 
                 MongoHelper.Register();
 				

+ 4 - 4
Unity/Assets/Scripts/Core/Helper/ProcessHelper.cs

@@ -8,7 +8,7 @@ namespace ET
 {
     public static class ProcessHelper
     {
-        public static Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false)
+        public static System.Diagnostics.Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false)
         {
             //Log.Debug($"Process Run exe:{exe} ,arguments:{arguments} ,workingDirectory:{workingDirectory}");
             try
@@ -41,7 +41,7 @@ namespace ET
                     RedirectStandardError = redirectStandardError,
                 };
 
-                Process process = Process.Start(info);
+                System.Diagnostics.Process process = System.Diagnostics.Process.Start(info);
 
                 if (waitExit)
                 {
@@ -56,7 +56,7 @@ namespace ET
             }
         }
         
-        private static async ETTask WaitExitAsync(Process process)
+        private static async ETTask WaitExitAsync(System.Diagnostics.Process process)
         {
             await process.WaitForExitAsync();
 #if UNITY
@@ -65,7 +65,7 @@ namespace ET
         }
         
 #if UNITY
-        private static async Task WaitForExitAsync(this Process self)
+        private static async Task WaitForExitAsync(this System.Diagnostics.Process self)
         {
             if (!self.HasExited)
             {

+ 5 - 5
Unity/Assets/Scripts/Core/Module/Config/ConfigComponent.cs

@@ -7,7 +7,7 @@ namespace ET
 	/// <summary>
     /// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来
     /// </summary>
-    public class ConfigComponent: Singleton<ConfigComponent>
+    public class ConfigComponent: ProcessSingleton<ConfigComponent>
     {
         public struct GetAllConfigBytes
         {
@@ -18,7 +18,7 @@ namespace ET
             public string ConfigName;
         }
 		
-        private readonly Dictionary<Type, ISingleton> allConfig = new Dictionary<Type, ISingleton>();
+        private readonly Dictionary<Type, IProcessSingleton> allConfig = new Dictionary<Type, IProcessSingleton>();
 
 		public override void Dispose()
 		{
@@ -30,7 +30,7 @@ namespace ET
 
 		public object LoadOneConfig(Type configType)
 		{
-			this.allConfig.TryGetValue(configType, out ISingleton oneConfig);
+			this.allConfig.TryGetValue(configType, out IProcessSingleton oneConfig);
 			if (oneConfig != null)
 			{
 				oneConfig.Destroy();
@@ -39,7 +39,7 @@ namespace ET
 			byte[] oneConfigBytes = EventSystem.Instance.Invoke<GetOneConfigBytes, byte[]>(new GetOneConfigBytes() {ConfigName = configType.FullName});
 
 			object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
-			ISingleton singleton = category as ISingleton;
+			IProcessSingleton singleton = category as IProcessSingleton;
 			singleton.Register();
 			
 			this.allConfig[configType] = singleton;
@@ -81,7 +81,7 @@ namespace ET
 			
 			lock (this)
 			{
-				ISingleton singleton = category as ISingleton;
+				IProcessSingleton singleton = category as IProcessSingleton;
 				singleton.Register();
 				this.allConfig[configType] = singleton;
 			}

+ 1 - 1
Unity/Assets/Scripts/Core/Module/CoroutineLock/CoroutineLockComponent.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class CoroutineLockComponent: Singleton<CoroutineLockComponent>, ISingletonUpdate
+    public class CoroutineLockComponent: ProcessSingleton<CoroutineLockComponent>, ISingletonUpdate
     {
         private readonly Dictionary<int, CoroutineLockQueueType> dictionary = new();
         private readonly Queue<(int, long, int)> nextFrameRun = new Queue<(int, long, int)>();

+ 17 - 14
Unity/Assets/Scripts/Core/Module/Entity/Entity.cs

@@ -160,6 +160,8 @@ namespace ET
 
         [BsonIgnore]
         public bool IsDisposed => this.InstanceId == 0;
+        
+        public Process Process { get; protected set; }
 
         [BsonIgnore]
         private Entity parent;
@@ -182,7 +184,7 @@ namespace ET
                 }
 
                 // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
-                if (value.Domain == null)
+                if (value.IScene == null)
                 {
                     throw new Exception($"cant set parent because parent domain is null: {this.GetType().FullName} {value.GetType().FullName}");
                 }
@@ -203,7 +205,8 @@ namespace ET
                 this.IsComponent = false;
                 this.parent.AddToChildren(this);
 
-                this.Domain = this is IScene? this as IScene : this.parent.domain;
+                this.IScene = this is IScene? this as IScene : this.parent.iScene;
+                this.Process = this.parent.Process;
 
 #if ENABLE_VIEW && UNITY_EDITOR
                 this.viewGO.GetComponent<ComponentView>().Component = this;
@@ -238,7 +241,7 @@ namespace ET
                 }
 
                 // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
-                if (value.Domain == null)
+                if (value.IScene == null)
                 {
                     throw new Exception($"cant set parent because parent domain is null: {this.GetType().FullName} {value.GetType().FullName}");
                 }
@@ -258,7 +261,7 @@ namespace ET
                 this.parent = value;
                 this.IsComponent = true;
                 this.parent.AddToComponents(this);
-                this.Domain = this is IScene? this as IScene : this.parent.domain;
+                this.IScene = this is IScene? this as IScene : this.parent.iScene;
             }
         }
 
@@ -274,14 +277,14 @@ namespace ET
         public long Id { get; protected set; }
 
         [BsonIgnore]
-        protected IScene domain;
+        protected IScene iScene;
 
         [BsonIgnore]
-        public IScene Domain
+        public IScene IScene
         {
             get
             {
-                return this.domain;
+                return this.iScene;
             }
             protected set
             {
@@ -290,15 +293,15 @@ namespace ET
                     throw new Exception($"domain cant set null: {this.GetType().FullName}");
                 }
 
-                if (this.domain == value)
+                if (this.iScene == value)
                 {
                     return;
                 }
 
-                IScene preDomain = this.domain;
-                this.domain = value;
+                IScene preScene = this.iScene;
+                this.iScene = value;
 
-                if (preDomain == null)
+                if (preScene == null)
                 {
                     if (this.InstanceId == 0)
                     {
@@ -334,7 +337,7 @@ namespace ET
                 {
                     foreach (Entity entity in this.children.Values)
                     {
-                        entity.Domain = this.domain;
+                        entity.IScene = this.iScene;
                     }
                 }
 
@@ -342,7 +345,7 @@ namespace ET
                 {
                     foreach (Entity component in this.components.Values)
                     {
-                        component.Domain = this.domain;
+                        component.IScene = this.iScene;
                     }
                 }
 
@@ -489,7 +492,7 @@ namespace ET
                 EntitySystemSingleton.Instance.Destroy(this);
             }
 
-            this.domain = null;
+            this.iScene = null;
 
             if (this.parent != null && !this.parent.IsDisposed)
             {

+ 4 - 4
Unity/Assets/Scripts/Core/Module/Entity/EntitySceneFactory.cs

@@ -2,17 +2,17 @@
 {
     public static class EntitySceneFactory
     {
-        public static Scene CreateScene(long id, long instanceId, int zone, SceneType sceneType, string name, Entity parent = null)
+        public static Scene CreateScene(Process process, long id, long instanceId, int zone, SceneType sceneType, string name, Entity parent = null)
         {
-            Scene scene = new Scene(id, instanceId, zone, sceneType, name);
+            Scene scene = new Scene(process, id, instanceId, zone, sceneType, name);
             parent?.AddChild(scene);
             return scene;
         }
 
-        public static Scene CreateScene(int zone, SceneType sceneType, string name, Entity parent = null)
+        public static Scene CreateScene(Process process, int zone, SceneType sceneType, string name, Entity parent = null)
         {
             long instanceId = IdGenerater.Instance.GenerateInstanceId();
-            Scene scene = new Scene(zone, instanceId, zone, sceneType, name);
+            Scene scene = new Scene(process, zone, instanceId, zone, sceneType, name);
             parent?.AddChild(scene);
             return scene;
         }

+ 2 - 2
Unity/Assets/Scripts/Core/Module/Entity/Root.cs

@@ -6,13 +6,13 @@ using System.Text;
 namespace ET
 {
     // 管理根部的Scene
-    public class Root: Singleton<Root>, ISingletonAwake
+    public class Root: ProcessSingleton<Root>, ISingletonAwake
     {
         public Scene Scene { get; private set; }
 
         public void Awake()
         {
-            this.Scene = EntitySceneFactory.CreateScene(0, SceneType.Process, "Process");
+            this.Scene = EntitySceneFactory.CreateScene(this.Process, 0, SceneType.Process, "Process");
         }
 
         public override void Dispose()

+ 5 - 3
Unity/Assets/Scripts/Core/Module/Entity/Scene.cs

@@ -22,12 +22,14 @@ namespace ET
             get;
         }
 
-        public Scene()
+        public Scene(Process process)
         {
+            this.Process = process;
         }
 
-        public Scene(long id, long instanceId, int zone, SceneType sceneType, string name)
+        public Scene(Process process, long id, long instanceId, int zone, SceneType sceneType, string name)
         {
+            this.Process = process;
             this.Id = id;
             this.InstanceId = instanceId;
             this.Zone = zone;
@@ -36,7 +38,7 @@ namespace ET
             this.IsCreated = true;
             this.IsNew = true;
             this.IsRegister = true;
-            this.domain = this;
+            this.IScene = this;
             Log.Info($"scene create: {this.SceneType} {this.Name} {this.Id} {this.InstanceId} {this.Zone}");
         }
 

+ 3 - 3
Unity/Assets/Scripts/Core/Module/Entity/SceneHelper.cs

@@ -4,17 +4,17 @@ namespace ET
     {
         public static int DomainZone(this Entity entity)
         {
-            return (entity.Domain as Scene)?.Zone ?? 0;
+            return (entity.IScene as Scene)?.Zone ?? 0;
         }
 
         public static Scene DomainScene(this Entity entity)
         {
-            return entity.Domain as Scene;
+            return entity.IScene as Scene;
         }
         
         public static SceneType DomainSceneType(this Entity entity)
         {
-            return entity.Domain.SceneType;
+            return entity.IScene.SceneType;
         }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Core/Module/EventSystem/EntitySystemSingleton.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class EntitySystemSingleton: Singleton<EntitySystemSingleton>, ISingletonAwake, ISingletonUpdate, ISingletonLateUpdate, ISingletonLoad
+    public class EntitySystemSingleton: ProcessSingleton<EntitySystemSingleton>, ISingletonAwake, ISingletonUpdate, ISingletonLateUpdate, ISingletonLoad
     {
         private TypeSystems typeSystems;
 

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

@@ -5,7 +5,7 @@ using System.Text;
 
 namespace ET
 {
-    public class EventSystem: Singleton<EventSystem>
+    public class EventSystem: ProcessSingleton<EventSystem>
     {
         private class EventInfo
         {
@@ -109,7 +109,7 @@ namespace ET
                 }
             }
             
-            Game.Load();
+            Process.Load();
         }
 
         public HashSet<Type> GetTypes(Type systemAttributeType)

+ 1 - 1
Unity/Assets/Scripts/Core/Module/IdGenerater/IdGenerater.cs

@@ -139,7 +139,7 @@ namespace ET
         }
     }
 
-    public class IdGenerater: Singleton<IdGenerater>
+    public class IdGenerater: ProcessSingleton<IdGenerater>
     {
         public const int Mask18bit = 0x03ffff;
 

+ 1 - 1
Unity/Assets/Scripts/Core/Module/Log/Logger.cs

@@ -3,7 +3,7 @@ using System.Diagnostics;
 
 namespace ET
 {
-    public class Logger: Singleton<Logger>
+    public class Logger: ProcessSingleton<Logger>
     {
         private ILog iLog;
 

+ 4 - 4
Unity/Assets/Scripts/Core/Module/Network/NetServices.cs

@@ -42,7 +42,7 @@ namespace ET
         public object Object; // 参数
     }
 
-    public class NetServices: Singleton<NetServices>, ISingletonUpdate
+    public class NetServices: ProcessSingleton<NetServices>, ISingletonUpdate
     {
 #if !SINGLE_THREAD
         private readonly ConcurrentQueue<NetOperator> netThreadOperators = new ConcurrentQueue<NetOperator>();
@@ -71,7 +71,7 @@ namespace ET
 
 #if !SINGLE_THREAD
             // 网络线程
-            this.thread = new Thread(this.NetThreadUpdate);
+            this.thread = new System.Threading.Thread(this.NetThreadUpdate);
             this.thread.Start();
 #endif
         }
@@ -348,7 +348,7 @@ namespace ET
 #if !SINGLE_THREAD
 
         private bool isStop;
-        private readonly Thread thread;
+        private readonly System.Threading.Thread thread;
 
         // 网络线程Update
         private void NetThreadUpdate()
@@ -356,7 +356,7 @@ namespace ET
             while (!this.isStop)
             {
                 this.UpdateInNetThread();
-                Thread.Sleep(1);
+                System.Threading.Thread.Sleep(1);
             }
 
             // 停止的时候再执行一帧,把队列中的消息处理完成

+ 1 - 1
Unity/Assets/Scripts/Core/Module/ObjectPool/ObjectPool.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 
 namespace ET
 {
-    public class ObjectPool: Singleton<ObjectPool>
+    public class ObjectPool: ProcessSingleton<ObjectPool>
     {
         private readonly Dictionary<Type, Queue<object>> pool = new();
         

+ 1 - 1
Unity/Assets/Scripts/Core/Module/Synchronization/MainThreadSynchronizationContext.cs

@@ -4,7 +4,7 @@ using System.Threading;
 namespace ET
 {
 
-    public class MainThreadSynchronizationContext: Singleton<MainThreadSynchronizationContext>, ISingletonUpdate
+    public class MainThreadSynchronizationContext: ProcessSingleton<MainThreadSynchronizationContext>, ISingletonUpdate
     {
         private readonly ThreadSynchronizationContext threadSynchronizationContext = new ThreadSynchronizationContext();
 

+ 1 - 1
Unity/Assets/Scripts/Core/Module/Time/TimeInfo.cs

@@ -2,7 +2,7 @@ using System;
 
 namespace ET
 {
-    public class TimeInfo: Singleton<TimeInfo>, ISingletonUpdate
+    public class TimeInfo: ProcessSingleton<TimeInfo>, ISingletonUpdate
     {
         private int timeZone;
         

+ 1 - 1
Unity/Assets/Scripts/Core/Module/Timer/TimerComponent.cs

@@ -53,7 +53,7 @@ namespace ET
         public object Args;
     }
 
-    public class TimerComponent: Singleton<TimerComponent>, ISingletonUpdate
+    public class TimerComponent: ProcessSingleton<TimerComponent>, ISingletonUpdate
     {
         /// <summary>
         /// key: time, value: timer id

+ 1 - 1
Unity/Assets/Scripts/Core/Singleton/ISingletonAwake.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public interface ISingletonAwake
+    public interface IProcessSingletonAwake
     {
         void Awake();
     }

+ 1 - 1
Unity/Assets/Scripts/Core/Singleton/ISingletonLateUpdate.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public interface ISingletonLateUpdate
+    public interface IProcessSingletonLateUpdate
     {
         void LateUpdate();
     }

+ 1 - 1
Unity/Assets/Scripts/Core/Singleton/ISingletonLoad.cs

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

+ 1 - 1
Unity/Assets/Scripts/Core/Singleton/ISingletonUpdate.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public interface ISingletonUpdate
+    public interface IProcessSingletonUpdate
     {
         void Update();
     }

+ 260 - 0
Unity/Assets/Scripts/Core/Singleton/Process.cs

@@ -0,0 +1,260 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Threading;
+
+namespace ET
+{
+    public class Process: IDisposable
+    {
+        public int Id { get; private set; }
+        
+        public Barrier Barrier { get; set; }
+
+        public Process(int id)
+        {
+            this.Id = id;
+
+            this.loop = (_) =>
+            {
+                this.Init();
+                this.Update();
+                this.LateUpdate();
+                this.FrameFinishUpdate();
+                this.Barrier?.RemoveParticipant();
+            };
+        }
+
+        private readonly ConcurrentQueue<MessageObject> Messages = new();
+        
+        private readonly Stack<IProcessSingleton> singletons = new();
+
+        private readonly Queue<IProcessSingleton> updates = new();
+
+        private readonly Queue<IProcessSingleton> lateUpdates = new();
+        
+        private readonly Queue<IProcessSingleton> loads = new();
+
+        private readonly Queue<ETTask> frameFinishTask = new();
+
+        private readonly Dictionary<int, ETTask<IResponse>> requestCallbacks = new();
+
+        private readonly WaitCallback loop;
+
+        private int rpcId;
+
+        public void AddMessage(MessageObject messageObject)
+        {
+            this.Messages.Enqueue(messageObject);
+        }
+
+        public void HandleMessage(MessageObject messageObject)
+        {
+            switch (messageObject)
+            {
+                case IResponse iResponse:
+                {
+                    if (this.requestCallbacks.TryGetValue(iResponse.RpcId, out ETTask<IResponse> task))
+                    {
+                        task.SetResult(iResponse);
+                    }
+                    break;
+                }
+                case IRequest iRequest:
+                {
+                    break;
+                }
+                default: // IMessage:
+                {
+                    break;
+                }
+            }
+        }
+
+        private void Init()
+        {
+            foreach (IProcessSingleton singleton in this.singletons)
+            {
+                singleton.Register();
+            }
+        }
+        
+        public T AddSingleton<T>() where T: ProcessSingleton<T>, new()
+        {
+            T singleton = new T();
+            AddSingleton(singleton);
+            return singleton;
+        }
+
+        public void AddSingleton(IProcessSingleton singleton)
+        {
+            singleton.Process = this;
+            
+            singleton.Register();
+            
+            singletons.Push(singleton);
+            
+            if (singleton is IProcessSingletonAwake awake)
+            {
+                awake.Awake();
+            }
+            
+            if (singleton is IProcessSingletonUpdate)
+            {
+                updates.Enqueue(singleton);
+            }
+            
+            if (singleton is IProcessSingletonLateUpdate)
+            {
+                lateUpdates.Enqueue(singleton);
+            }
+
+            if (singleton is IProcessSingletonLoad)
+            {
+                loads.Enqueue(singleton);
+            }
+        }
+
+        public async ETTask WaitFrameFinish()
+        {
+            ETTask task = ETTask.Create(true);
+            frameFinishTask.Enqueue(task);
+            await task;
+        }
+        
+        public void Update()
+        {
+            int count = updates.Count;
+            while (count-- > 0)
+            {
+                IProcessSingleton singleton = updates.Dequeue();
+
+                if (singleton.IsDisposed())
+                {
+                    continue;
+                }
+
+                if (singleton is not IProcessSingletonUpdate update)
+                {
+                    continue;
+                }
+                
+                updates.Enqueue(singleton);
+                try
+                {
+                    update.Update();
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+        
+        public void LateUpdate()
+        {
+            int count = lateUpdates.Count;
+            while (count-- > 0)
+            {
+                IProcessSingleton singleton = lateUpdates.Dequeue();
+                
+                if (singleton.IsDisposed())
+                {
+                    continue;
+                }
+
+                if (singleton is not IProcessSingletonLateUpdate lateUpdate)
+                {
+                    continue;
+                }
+                
+                lateUpdates.Enqueue(singleton);
+                try
+                {
+                    lateUpdate.LateUpdate();
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public WaitCallback Loop
+        {
+            get
+            {
+                return this.loop;
+            }
+        }
+        
+        public void Load()
+        {
+            int count = loads.Count;
+            while (count-- > 0)
+            {
+                IProcessSingleton singleton = loads.Dequeue();
+                
+                if (singleton.IsDisposed())
+                {
+                    continue;
+                }
+
+                if (singleton is not IProcessSingletonLoad load)
+                {
+                    continue;
+                }
+                
+                loads.Enqueue(singleton);
+                try
+                {
+                    load.Load();
+                }
+                catch (Exception e)
+                {
+                    Log.Error(e);
+                }
+            }
+        }
+
+        public void FrameFinishUpdate()
+        {
+            while (frameFinishTask.Count > 0)
+            {
+                ETTask task = frameFinishTask.Dequeue();
+                task.SetResult();
+            }
+        }
+
+        public void Send(int threadId, MessageObject messageObject)
+        {
+            Game.Instance.Send(threadId, messageObject);
+        }
+        
+        public async ETTask<IResponse> Call(int threadId, IRequest request)
+        {
+            ETTask<IResponse> task = ETTask<IResponse>.Create(true);
+            request.RpcId = ++this.rpcId;
+            this.requestCallbacks.Add(request.RpcId, task);
+            Game.Instance.Send(threadId, request as MessageObject);
+            return await task;
+        }
+
+        public void Dispose()
+        {
+            if (this.Id == 0)
+            {
+                return;
+            }
+            
+            this.Id = 0;
+            
+            // 顺序反过来清理
+            while (singletons.Count > 0)
+            {
+                IProcessSingleton iSingleton = singletons.Pop();
+                iSingleton.Destroy();
+            }
+        }
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Core/Singleton/Process.cs.meta

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

+ 8 - 5
Unity/Assets/Scripts/Core/Singleton/Singleton.cs

@@ -2,20 +2,23 @@
 
 namespace ET
 {
-    public interface ISingleton: IDisposable
+    public interface IProcessSingleton: IDisposable
     {
+        Process Process { get; set; }
         void Register();
         void Destroy();
         bool IsDisposed();
     }
     
-    public abstract class Singleton<T>: ISingleton where T: Singleton<T>, new()
+    public abstract class ProcessSingleton<T>: IProcessSingleton where T: ProcessSingleton<T>, new()
     {
         private bool isDisposed; 
         [ThreadStatic]
         [StaticField]
         private static T instance;
 
+        public Process Process { get; set; }
+
         public static T Instance
         {
             get
@@ -24,12 +27,12 @@ namespace ET
             }
         }
 
-        void ISingleton.Register()
+        void IProcessSingleton.Register()
         {
             instance = (T)this;
         }
 
-        void ISingleton.Destroy()
+        void IProcessSingleton.Destroy()
         {
             if (this.isDisposed)
             {
@@ -41,7 +44,7 @@ namespace ET
             instance = null;
         }
 
-        bool ISingleton.IsDisposed()
+        bool IProcessSingleton.IsDisposed()
         {
             return this.isDisposed;
         }

+ 8 - 0
Unity/Assets/Scripts/Core/World.meta

@@ -0,0 +1,8 @@
+fileFormatVersion: 2
+guid: 780f97adbed71ad4fa77ce29bb21f6be
+folderAsset: yes
+DefaultImporter:
+  externalObjects: {}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 68 - 70
Unity/Assets/Scripts/Core/Singleton/Game.cs → Unity/Assets/Scripts/Core/World/Game.cs

@@ -1,59 +1,98 @@
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Runtime.InteropServices;
 using System.Threading;
+using System.Threading.Tasks;
 
 namespace ET
 {
-    public class Game: IDisposable
+    public class Game
     {
-        public int Id { get; private set; }
+        [StaticField]
+        public static Game Instance = new Game();
         
-        public Barrier Barrier { get; set; }
-
-        public Game(int id)
+        private Game()
         {
-            this.Id = id;
-
-            this.loop = new WaitCallback((_) =>
-            {
-                this.Init();
-                this.Update();
-                this.LateUpdate();
-                this.FrameFinishUpdate();
-                this.Barrier.RemoveParticipant();
-            });
         }
         
-        [StaticField]
         private readonly Stack<ISingleton> singletons = new();
-        [StaticField]
+
         private readonly Queue<ISingleton> updates = new();
-        [StaticField]
+
         private readonly Queue<ISingleton> lateUpdates = new();
-        [StaticField]
+
         private readonly Queue<ISingleton> loads = new();
-        [StaticField]
-        private readonly Queue<ETTask> frameFinishTask = new();
 
-        private readonly WaitCallback loop;
+        private readonly ConcurrentQueue<Process> loops = new();
+
+        private readonly ConcurrentDictionary<int, Process> processes = new();
 
-        private void Init()
+        private int idGenerate;
+
+        public Process Create(bool loop = true)
         {
-            foreach (ISingleton singleton in this.singletons)
+            Process process = new(++this.idGenerate);
+            this.processes.TryAdd(process.Id, process);
+            if (loop)
             {
-                singleton.Register();
+                this.loops.Enqueue(process);
             }
+            return process;
         }
         
-        public T AddSingleton<T>() where T: Singleton<T>, new()
+        public void Remove(int id)
         {
-            T singleton = new T();
-            AddSingleton(singleton);
-            return singleton;
+            if (this.processes.Remove(id, out Process thread))
+            {
+                thread.Dispose();    
+            }
+        }
+        
+        
+        // 简单线程调度,每次Loop会把所有Game Loop一遍
+        public void Loop()
+        {
+            int count = this.loops.Count;
+
+            using Barrier barrier = new Barrier(1);
+            
+            while (count-- > 0)
+            {
+                this.loops.TryDequeue(out Process thread);
+                if (thread == null)
+                {
+                    continue;
+                }
+                barrier.AddParticipant();
+                thread.Barrier = barrier;
+                if (thread.Id == 0)
+                {
+                    continue;
+                }
+                this.loops.Enqueue(thread);
+                ThreadPool.QueueUserWorkItem(thread.Loop);
+            }
+
+            barrier.SignalAndWait();
         }
 
+        public void Send(int threadId, MessageObject messageObject)
+        {
+            if (this.processes.TryGetValue(threadId, out Process thread))
+            {
+                return;
+            }
+            thread.AddMessage(messageObject);
+        }
+        
+        
+        
+        
+        
         public void AddSingleton(ISingleton singleton)
         {
+            
             singleton.Register();
             
             singletons.Push(singleton);
@@ -78,13 +117,6 @@ namespace ET
                 loads.Enqueue(singleton);
             }
         }
-
-        public async ETTask WaitFrameFinish()
-        {
-            ETTask task = ETTask.Create(true);
-            frameFinishTask.Enqueue(task);
-            await task;
-        }
         
         public void Update()
         {
@@ -144,14 +176,6 @@ namespace ET
             }
         }
 
-        public WaitCallback Loop
-        {
-            get
-            {
-                return this.loop;
-            }
-        }
-        
         public void Load()
         {
             int count = loads.Count;
@@ -180,31 +204,5 @@ namespace ET
                 }
             }
         }
-
-        public void FrameFinishUpdate()
-        {
-            while (frameFinishTask.Count > 0)
-            {
-                ETTask task = frameFinishTask.Dequeue();
-                task.SetResult();
-            }
-        }
-
-        public void Dispose()
-        {
-            if (this.Id == 0)
-            {
-                return;
-            }
-            
-            this.Id = 0;
-            
-            // 顺序反过来清理
-            while (singletons.Count > 0)
-            {
-                ISingleton iSingleton = singletons.Pop();
-                iSingleton.Destroy();
-            }
-        }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Core/Singleton/Game.cs.meta → Unity/Assets/Scripts/Core/World/Game.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 155ac10f5b348f64e89b3f40a05933d0
+guid: d6c62fc9ee0960943bd437c0b5ad37ca
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 0 - 0
Unity/Assets/Scripts/Model/Share/Module/Message/IMessage.cs → Unity/Assets/Scripts/Core/World/IMessage.cs


+ 1 - 1
Unity/Assets/Scripts/Model/Share/Module/Message/IMessage.cs.meta → Unity/Assets/Scripts/Core/World/IMessage.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: 92c1d19a46e7fef4a8aec8708e85518b
+guid: 21e594d3ffbf71b45ad9577e4c586518
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 1
Unity/Assets/Scripts/Core/World/IWorldSingletonAwake.cs → Unity/Assets/Scripts/Core/World/ISingletonAwake.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public interface IWorldSingletonAwake
+    public interface ISingletonAwake
     {
         void Awake();
     }

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

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

+ 1 - 1
Unity/Assets/Scripts/Core/World/IWorldSingletonLateUpdate.cs → Unity/Assets/Scripts/Core/World/ISingletonLateUpdate.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public interface IWorldSingletonLateUpdate
+    public interface ISingletonLateUpdate
     {
         void LateUpdate();
     }

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

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

+ 1 - 1
Unity/Assets/Scripts/Core/World/IWorldSingletonLoad.cs → Unity/Assets/Scripts/Core/World/ISingletonLoad.cs

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

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

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

+ 1 - 1
Unity/Assets/Scripts/Core/World/IWorldSingletonUpdate.cs → Unity/Assets/Scripts/Core/World/ISingletonUpdate.cs

@@ -1,6 +1,6 @@
 namespace ET
 {
-    public interface IWorldSingletonUpdate
+    public interface ISingletonUpdate
     {
         void Update();
     }

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

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

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

@@ -2,17 +2,17 @@
 
 namespace ET
 {
-    public interface IWorldSingleton: IDisposable
+    public interface ISingleton: IDisposable
     {
         void Register();
         void Destroy();
         bool IsDisposed();
     }
     
-    public abstract class WorldSingleton<T>: IWorldSingleton where T: WorldSingleton<T>, new()
+    public abstract class Singleton<T>: ISingleton where T: Singleton<T>, new()
     {
         private bool isDisposed;
-        [ThreadStatic]
+        
         [StaticField]
         private static T instance;
 
@@ -24,7 +24,7 @@ namespace ET
             }
         }
 
-        void IWorldSingleton.Register()
+        void ISingleton.Register()
         {
             if (instance != null)
             {
@@ -33,7 +33,7 @@ namespace ET
             instance = (T)this;
         }
 
-        void IWorldSingleton.Destroy()
+        void ISingleton.Destroy()
         {
             if (this.isDisposed)
             {
@@ -45,7 +45,7 @@ namespace ET
             instance = null;
         }
 
-        bool IWorldSingleton.IsDisposed()
+        bool ISingleton.IsDisposed()
         {
             return this.isDisposed;
         }

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

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

+ 0 - 73
Unity/Assets/Scripts/Core/World/World.cs

@@ -1,73 +0,0 @@
-using System.Collections.Generic;
-using System.Runtime.InteropServices;
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace ET
-{
-    public class World
-    {
-        [StaticField]
-        public static World Instance = new World();
-        
-        private World()
-        {
-        }
-        
-        private Stack<IWorldSingleton> singletons = new();
-
-        private Queue<IWorldSingleton> updates = new();
-
-        private Queue<IWorldSingleton> lateUpdates = new();
-
-        private Queue<IWorldSingleton> loads = new();
-
-        private readonly Queue<Game> loops = new Queue<Game>();
-
-        private readonly Dictionary<int, Game> games = new Dictionary<int, Game>();
-
-        private int idGenerate;
-
-        public Game Create(bool loop = true)
-        {
-            Game game = new(++this.idGenerate);
-            this.games.Add(game.Id, game);
-            if (loop)
-            {
-                this.loops.Enqueue(game);
-            }
-            return game;
-        }
-
-        public void Remove(int id)
-        {
-            if (this.games.Remove(id, out Game game))
-            {
-                game.Dispose();    
-            }
-        }
-        
-        // 简单线程调度,每次Loop会把所有Game Loop一遍
-        public void Loop()
-        {
-            int count = this.loops.Count;
-
-            using Barrier barrier = new Barrier(1);
-            
-            while (count-- > 0)
-            {
-                Game game = this.loops.Dequeue();
-                barrier.AddParticipant();
-                game.Barrier = barrier;
-                if (game.Id == 0)
-                {
-                    continue;
-                }
-                this.loops.Enqueue(game);
-                ThreadPool.QueueUserWorkItem(game.Loop);
-            }
-
-            barrier.SignalAndWait();
-        }
-    }
-}

+ 2 - 2
Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs

@@ -45,7 +45,7 @@ namespace ET
             }
 
             var log = new Logger();
-            ((ISingleton)log).Register();
+            ((IProcessSingleton)log).Register();
             log.ILog = new UnityLogger();
         }
 
@@ -56,7 +56,7 @@ namespace ET
                 return;
             }
 
-            ((ISingleton)Logger.Instance).Destroy();
+            ((IProcessSingleton)Logger.Instance).Destroy();
         }
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Editor/Helper/ShellHelper.cs

@@ -8,7 +8,7 @@ namespace ET
     {
         public static void Run(string cmd, string workDirectory, List<string> environmentVars = null)
         {
-            Process process = new();
+            System.Diagnostics.Process process = new();
             try
             {
 #if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Client/Demo/Scene/SceneChangeHelper.cs

@@ -9,7 +9,7 @@
             
             CurrentScenesComponent currentScenesComponent = clientScene.GetComponent<CurrentScenesComponent>();
             currentScenesComponent.Scene?.Dispose(); // 删除之前的CurrentScene,创建新的
-            Scene currentScene = SceneFactory.CreateCurrentScene(sceneInstanceId, clientScene.Zone, sceneName, currentScenesComponent);
+            Scene currentScene = SceneFactory.CreateCurrentScene(clientScene.Process, sceneInstanceId, clientScene.Zone, sceneName, currentScenesComponent);
             UnitComponent unitComponent = currentScene.AddComponent<UnitComponent>();
          
             // 可以订阅这个事件中创建Loading界面

+ 4 - 4
Unity/Assets/Scripts/Hotfix/Client/Demo/Scene/SceneFactory.cs

@@ -4,11 +4,11 @@ namespace ET.Client
 {
     public static partial class SceneFactory
     {
-        public static async ETTask<Scene> CreateClientScene(int zone, SceneType sceneType, string name)
+        public static async ETTask<Scene> CreateClientScene(Process process, int zone, SceneType sceneType, string name)
         {
             await ETTask.CompletedTask;
             
-            Scene clientScene = EntitySceneFactory.CreateScene(zone, sceneType, name, ClientSceneManagerComponent.Instance);
+            Scene clientScene = EntitySceneFactory.CreateScene(process, zone, sceneType, name, ClientSceneManagerComponent.Instance);
             clientScene.AddComponent<ObjectWait>();
             clientScene.AddComponent<PlayerComponent>();
             clientScene.AddComponent<CurrentScenesComponent>();
@@ -17,9 +17,9 @@ namespace ET.Client
             return clientScene;
         }
         
-        public static Scene CreateCurrentScene(long id, int zone, string name, CurrentScenesComponent currentScenesComponent)
+        public static Scene CreateCurrentScene(Process process, long id, int zone, string name, CurrentScenesComponent currentScenesComponent)
         {
-            Scene currentScene = EntitySceneFactory.CreateScene(id, IdGenerater.Instance.GenerateInstanceId(), zone, SceneType.Current, name, currentScenesComponent);
+            Scene currentScene = EntitySceneFactory.CreateScene(process, id, IdGenerater.Instance.GenerateInstanceId(), zone, SceneType.Current, name, currentScenesComponent);
             currentScenesComponent.Scene = currentScene;
             
             EventSystem.Instance.Publish(currentScene, new EventType.AfterCreateCurrentScene());

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Client/Module/Message/NetClientComponentSystem.cs

@@ -52,7 +52,7 @@ namespace ET.Client
             long channelId = NetServices.Instance.CreateConnectChannelId();
             Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
             session.RemoteAddress = realIPEndPoint;
-            if (self.Domain.SceneType != SceneType.Benchmark)
+            if (self.IScene.SceneType != SceneType.Benchmark)
             {
                 session.AddComponent<SessionIdleCheckerComponent>();
             }
@@ -66,7 +66,7 @@ namespace ET.Client
             long channelId = localConn;
             Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
             session.RemoteAddress = realIPEndPoint;
-            if (self.Domain.SceneType != SceneType.Benchmark)
+            if (self.IScene.SceneType != SceneType.Benchmark)
             {
                 session.AddComponent<SessionIdleCheckerComponent>();
             }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Helper/SceneFactory.cs

@@ -8,7 +8,7 @@ namespace ET.Server
         public static async ETTask<Scene> CreateServerScene(Entity parent, long id, long instanceId, int zone, string name, SceneType sceneType, StartSceneConfig startSceneConfig = null)
         {
             await ETTask.CompletedTask;
-            Scene scene = EntitySceneFactory.CreateScene(id, instanceId, zone, sceneType, name, parent);
+            Scene scene = EntitySceneFactory.CreateScene(parent.Process, id, instanceId, zone, sceneType, name, parent);
 
             scene.AddComponent<MailBoxComponent, MailboxType>(MailboxType.UnOrderMessageDispatcher);
 

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Gate/C2G_LoginGateHandler.cs

@@ -56,7 +56,7 @@ namespace ET.Server
 
         private static async ETTask CheckRoom(Player player, Session session)
         {
-            await Game.WaitFrameFinish();
+            await player.Process.WaitFrameFinish();
             
             Room2G_Reconnect room2GateReconnect = await ActorMessageSenderComponent.Instance.Call(
                 player.GetComponent<PlayerRoomComponent>().RoomInstanceId,

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Map/Transfer/TransferHelper.cs

@@ -7,7 +7,7 @@ namespace ET.Server
     {
         public static async ETTask TransferAtFrameFinish(Unit unit, long sceneInstanceId, string sceneName)
         {
-            await Game.WaitFrameFinish();
+            await unit.Process.WaitFrameFinish();
 
             await TransferHelper.Transfer(unit, sceneInstanceId, sceneName);
         }

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotCaseSystem.cs

@@ -73,7 +73,7 @@ namespace ET.Server
             Scene clientScene = null;
             try
             {
-                clientScene = await Client.SceneFactory.CreateClientScene(zone, SceneType.Robot, name);
+                clientScene = await Client.SceneFactory.CreateClientScene(self.Process, zone, SceneType.Robot, name);
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");
@@ -94,7 +94,7 @@ namespace ET.Server
 
             try
             {
-                clientScene = await Client.SceneFactory.CreateClientScene(zone, SceneType.Robot, $"Robot_{zone}");
+                clientScene = await Client.SceneFactory.CreateClientScene(self.Process, zone, SceneType.Robot, $"Robot_{zone}");
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/RobotManagerComponentSystem.cs

@@ -10,7 +10,7 @@ namespace ET.Server
             Scene clientScene = null;
             try
             {
-                clientScene = await Client.SceneFactory.CreateClientScene(zone, SceneType.Robot, "Robot");
+                clientScene = await Client.SceneFactory.CreateClientScene(self.Process, zone, SceneType.Robot, "Robot");
                 await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
                 await Client.EnterMapHelper.EnterMapAsync(clientScene);
                 Log.Debug($"create robot ok: {zone}");

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Robot/Scene/RobotSceneFactory.cs

@@ -14,7 +14,7 @@ namespace ET.Server
         {
             await ETTask.CompletedTask;
             Log.Info($"create scene: {sceneType} {name} {zone}");
-            Scene scene = EntitySceneFactory.CreateScene(id, instanceId, zone, sceneType, name, parent);
+            Scene scene = EntitySceneFactory.CreateScene(parent.Process, id, instanceId, zone, sceneType, name, parent);
 
             scene.AddComponent<MailBoxComponent, MailboxType>(MailboxType.UnOrderMessageDispatcher);
 

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Watcher/WatcherComponentSystem.cs

@@ -28,7 +28,7 @@ namespace ET.Server
                 {
                     continue;
                 }
-                Process process = WatcherHelper.StartProcess(startProcessConfig.Id, createScenes);
+                System.Diagnostics.Process process = WatcherHelper.StartProcess(startProcessConfig.Id, createScenes);
                 self.Processes.Add(startProcessConfig.Id, process);
             }
         }

+ 2 - 2
Unity/Assets/Scripts/Hotfix/Server/Demo/Scenes/Watcher/WatcherHelper.cs

@@ -37,7 +37,7 @@ namespace ET.Server
             return true;
         }
         
-        public static Process StartProcess(int processId, int createScenes = 0)
+        public static System.Diagnostics.Process StartProcess(int processId, int createScenes = 0)
         {
             StartProcessConfig startProcessConfig = StartProcessConfigCategory.Instance.Get(processId);
             const string exe = "dotnet";
@@ -50,7 +50,7 @@ namespace ET.Server
                     $" --LogLevel={Options.Instance.LogLevel}" +
                     $" --Console={Options.Instance.Console}";
             Log.Debug($"{exe} {arguments}");
-            Process process = ProcessHelper.Run(exe, arguments);
+            System.Diagnostics.Process process = ProcessHelper.Run(exe, arguments);
             return process;
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Module/Actor/ActorMessageDispatcherComponentSystem.cs

@@ -96,7 +96,7 @@ namespace ET.Server
                 throw new Exception($"not found message handler: {message} {entity.GetType().FullName}");
             }
 
-            SceneType sceneType = entity.Domain.SceneType;
+            SceneType sceneType = entity.IScene.SceneType;
             foreach (ActorMessageDispatcherInfo actorMessageDispatcherInfo in list)
             {
                 if (!actorMessageDispatcherInfo.SceneType.HasSameFlag(sceneType))

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetServerComponentSystem.cs

@@ -38,7 +38,7 @@ namespace ET.Server
             Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
             session.RemoteAddress = ipEndPoint;
 
-            if (self.Domain.SceneType != SceneType.BenchmarkServer)
+            if (self.IScene.SceneType != SceneType.BenchmarkServer)
             {
                 // 挂上这个组件,5秒就会删除session,所以客户端验证完成要删除这个组件。该组件的作用就是防止外挂一直连接不发消息也不进行权限验证
                 session.AddComponent<SessionAcceptTimeoutComponent>();

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/LockStep/RoomSystem.cs

@@ -9,7 +9,7 @@ namespace ET
     {
         public static Room Room(this Entity entity)
         {
-            return entity.Domain as Room;
+            return entity.IScene as Room;
         }
         
         public static void Init(this Room self, List<LockStepUnitInfo> unitInfos, long startTime, int frame = -1)

+ 1 - 1
Unity/Assets/Scripts/Hotfix/Share/Module/Message/MessageDispatcherComponentSystem.cs

@@ -81,7 +81,7 @@ namespace ET
                 return;
             }
 
-            SceneType sceneType = session.Domain.SceneType;
+            SceneType sceneType = session.IScene.SceneType;
             foreach (MessageDispatcherInfo ev in actions)
             {
                 if (!ev.SceneType.HasSameFlag(sceneType))

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

@@ -15,7 +15,7 @@ namespace ET.Client
 
             SceneType sceneType = EnumHelper.FromString<SceneType>(GlobalComponent.Instance.GlobalConfig.AppType.ToString());
             
-            Scene clientScene = await SceneFactory.CreateClientScene(1, sceneType, sceneType.ToString());
+            Scene clientScene = await SceneFactory.CreateClientScene(scene.Process, 1, sceneType, sceneType.ToString());
             
             await EventSystem.Instance.PublishAsync(clientScene, new EventType.AppStartInitFinish());
         }

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

@@ -29,7 +29,7 @@ namespace ET.Client
 
             if (Input.GetKeyDown(KeyCode.R))
             {
-                Game.Load();
+                self.Process.Load();
                 Log.Debug("hot reload success!");
             }
         

+ 1 - 1
Unity/Assets/Scripts/HotfixView/Client/LockStep/LSUnitViewSystem.cs

@@ -66,7 +66,7 @@ namespace ET.Client
                 return unit;
             }
 
-            self.Unit = (self.Domain as Room).LSWorld.GetComponent<LSUnitComponent>().GetChild<LSUnit>(self.Id);
+            self.Unit = (self.IScene as Room).LSWorld.GetComponent<LSUnitComponent>().GetChild<LSUnit>(self.Id);
             return self.Unit;
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Loader/GlobalComponent.cs

@@ -2,7 +2,7 @@ using UnityEngine;
 
 namespace ET
 {
-    public class GlobalComponent: Singleton<GlobalComponent>, ISingletonAwake
+    public class GlobalComponent: ProcessSingleton<GlobalComponent>, ISingletonAwake
     {
         public Transform Global;
         public Transform Unit { get; set; }

+ 21 - 17
Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs

@@ -7,6 +7,8 @@ namespace ET
 {
 	public class Init: MonoBehaviour
 	{
+		private Process process;
+		
 		private void Start()
 		{
 			DontDestroyOnLoad(gameObject);
@@ -15,46 +17,48 @@ namespace ET
 			{
 				Log.Error(e.ExceptionObject.ToString());
 			};
-				
-			Game.AddSingleton<MainThreadSynchronizationContext>();
 
-			Game.AddSingleton<GlobalComponent>();
-			
 			// 命令行参数
 			string[] args = "".Split(" ");
 			Parser.Default.ParseArguments<Options>(args)
 				.WithNotParsed(error => throw new Exception($"命令行格式错误! {error}"))
-				.WithParsed(Game.AddSingleton);
+				.WithParsed(Game.Instance.AddSingleton);
+			
+			process = Game.Instance.Create(false);
+				
+			process.AddSingleton<MainThreadSynchronizationContext>();
+
+			process.AddSingleton<GlobalComponent>();
 			
 			Options.Instance.StartConfig = $"StartConfig/Localhost";
 
-			Game.AddSingleton<TimeInfo>();
-			Game.AddSingleton<Logger>().ILog = new UnityLogger();
-			Game.AddSingleton<ObjectPool>();
-			Game.AddSingleton<IdGenerater>();
-			Game.AddSingleton<EventSystem>();
-			Game.AddSingleton<TimerComponent>();
-			Game.AddSingleton<CoroutineLockComponent>();
+			process.AddSingleton<TimeInfo>();
+			process.AddSingleton<Logger>().ILog = new UnityLogger();
+			process.AddSingleton<ObjectPool>();
+			process.AddSingleton<IdGenerater>();
+			process.AddSingleton<EventSystem>();
+			process.AddSingleton<TimerComponent>();
+			process.AddSingleton<CoroutineLockComponent>();
 
 			ETTask.ExceptionHandler += Log.Error;
 
-			Game.AddSingleton<CodeLoader>().Start();
+			process.AddSingleton<CodeLoader>().Start();
 		}
 
 		private void Update()
 		{
-			Game.Update();
+			process.Update();
 		}
 
 		private void LateUpdate()
 		{
-			Game.LateUpdate();
-			Game.FrameFinishUpdate();
+			process.LateUpdate();
+			process.FrameFinishUpdate();
 		}
 
 		private void OnApplicationQuit()
 		{
-			Game.Close();
+			Game.Instance.Remove(this.process.Id);
 		}
 	}
 	

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Demo/Watcher/WatcherComponent.cs

@@ -8,6 +8,6 @@ namespace ET.Server
     {
         public static WatcherComponent Instance { get; set; }
 
-        public readonly Dictionary<int, Process> Processes = new Dictionary<int, Process>();
+        public readonly Dictionary<int, System.Diagnostics.Process> Processes = new Dictionary<int, System.Diagnostics.Process>();
     }
 }

+ 6 - 22
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -29,22 +29,6 @@ namespace ET
         {
             StartAsync().Coroutine();
         }
-
-        private static async ETTask Test1()
-        {
-            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, 1, 2000))
-            {
-                await TimerComponent.Instance.WaitAsync(100000);
-            }
-        }
-        
-        private static async ETTask Test2()
-        {
-            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.DB, 1, 10000))
-            {
-                await TimerComponent.Instance.WaitAsync(100000);
-            }
-        }
         
         private static async ETTask StartAsync()
         {
@@ -53,14 +37,14 @@ namespace ET
             MongoHelper.RegisterStruct<LSInput>();
             MongoHelper.Register();
             
-            Game.AddSingleton<EntitySystemSingleton>();
-            Game.AddSingleton<LSEntitySystemSington>();
+            Process process = EventSystem.Instance.Process;
+            process.AddSingleton<EntitySystemSingleton>();
+            process.AddSingleton<LSEntitySystemSington>();
 
-            Game.AddSingleton<NetServices>();
-            Game.AddSingleton<Root>();
+            process.AddSingleton<NetServices>();
+            process.AddSingleton<Root>();
 
-            
-            await Game.AddSingleton<ConfigComponent>().LoadAsync();
+            await process.AddSingleton<ConfigComponent>().LoadAsync();
 
             await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent1());
             await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent2());

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

@@ -11,7 +11,7 @@ namespace ET
         public const int Max = 1;
     }
     
-    public class LSEntitySystemSington: Singleton<LSEntitySystemSington>, ISingletonAwake, ISingletonLoad
+    public class LSEntitySystemSington: ProcessSingleton<LSEntitySystemSington>, ISingletonAwake, ISingletonLoad
     {
         private TypeSystems typeSystems;
         

+ 1 - 1
Unity/Assets/Scripts/Model/Share/LockStep/LSWorld.cs

@@ -9,7 +9,7 @@ namespace ET
     {
         public static LSWorld LSWorld(this LSEntity entity)
         {
-            return entity.Domain as LSWorld;
+            return entity.IScene as LSWorld;
         }
 
         public static long GetId(this LSEntity entity)

+ 1 - 1
Unity/Assets/Scripts/Model/Share/Module/Message/OpcodeHelper.cs

@@ -47,7 +47,7 @@ namespace ET
                 return;
             }
             
-            Logger.Instance.Debug($"{entity.Domain.SceneType} {message}");
+            Logger.Instance.Debug($"{entity.IScene.SceneType} {message}");
         }
     }
 }

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

@@ -50,7 +50,7 @@ namespace ET
                 return;
             }
 
-            SceneType unitDomainSceneType = unit.Domain.SceneType;
+            SceneType unitDomainSceneType = unit.IScene.SceneType;
             foreach (NumericWatcherInfo numericWatcher in list)
             {
                 if (!numericWatcher.SceneType.HasSameFlag(unitDomainSceneType))

+ 1 - 0
Unity/Assets/Scripts/ThirdParty/ETTask/ETTask.cs

@@ -19,6 +19,7 @@ namespace ET
             }
         }
 
+        [ThreadStatic]
         private static readonly Queue<ETTask> queue = new Queue<ETTask>();
 
         /// <summary>