Ver Fonte

做完了NetService的调整,完全变成了单线程,因为有了Fiber,所以可以变成多个Fiber,这样照样可以利用多核

tanghai há 2 anos atrás
pai
commit
963714e92b
85 ficheiros alterados com 314 adições e 481 exclusões
  1. 1 1
      Share/Tool/Share.Tool.csproj
  2. 3 1
      Unity/Assets/Scripts/Core/Entity/EntityRef.cs
  3. 1 1
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLock.cs
  4. 1 1
      Unity/Assets/Scripts/Core/Fiber/Module/CoroutineLock/CoroutineLockQueue.cs
  5. 8 0
      Unity/Assets/Scripts/Core/Network.meta
  6. 0 0
      Unity/Assets/Scripts/Core/Network/AChannel.cs
  7. 0 0
      Unity/Assets/Scripts/Core/Network/AChannel.cs.meta
  8. 11 1
      Unity/Assets/Scripts/Core/Network/AService.cs
  9. 0 0
      Unity/Assets/Scripts/Core/Network/AService.cs.meta
  10. 0 0
      Unity/Assets/Scripts/Core/Network/ActorResponse.cs
  11. 0 0
      Unity/Assets/Scripts/Core/Network/ActorResponse.cs.meta
  12. 0 0
      Unity/Assets/Scripts/Core/Network/Circularbuffer.cs
  13. 0 0
      Unity/Assets/Scripts/Core/Network/Circularbuffer.cs.meta
  14. 0 0
      Unity/Assets/Scripts/Core/Network/ErrorCore.cs
  15. 0 0
      Unity/Assets/Scripts/Core/Network/ErrorCore.cs.meta
  16. 0 0
      Unity/Assets/Scripts/Core/Network/Extensions.cs
  17. 0 0
      Unity/Assets/Scripts/Core/Network/Extensions.cs.meta
  18. 0 0
      Unity/Assets/Scripts/Core/Network/IActorLocationMessage.cs
  19. 0 0
      Unity/Assets/Scripts/Core/Network/IActorLocationMessage.cs.meta
  20. 0 0
      Unity/Assets/Scripts/Core/Network/IActorMessage.cs
  21. 0 0
      Unity/Assets/Scripts/Core/Network/IActorMessage.cs.meta
  22. 0 0
      Unity/Assets/Scripts/Core/Network/IPEndPointNonAlloc.cs
  23. 0 0
      Unity/Assets/Scripts/Core/Network/IPEndPointNonAlloc.cs.meta
  24. 2 2
      Unity/Assets/Scripts/Core/Network/KChannel.cs
  25. 0 0
      Unity/Assets/Scripts/Core/Network/KChannel.cs.meta
  26. 2 22
      Unity/Assets/Scripts/Core/Network/KService.cs
  27. 0 0
      Unity/Assets/Scripts/Core/Network/KService.cs.meta
  28. 0 0
      Unity/Assets/Scripts/Core/Network/MessageAttribute.cs
  29. 0 0
      Unity/Assets/Scripts/Core/Network/MessageAttribute.cs.meta
  30. 0 0
      Unity/Assets/Scripts/Core/Network/MessagePool.cs
  31. 0 0
      Unity/Assets/Scripts/Core/Network/MessagePool.cs.meta
  32. 0 0
      Unity/Assets/Scripts/Core/Network/MessageSerializeHelper.cs
  33. 0 0
      Unity/Assets/Scripts/Core/Network/MessageSerializeHelper.cs.meta
  34. 110 0
      Unity/Assets/Scripts/Core/Network/NetServices.cs
  35. 0 0
      Unity/Assets/Scripts/Core/Network/NetServices.cs.meta
  36. 0 0
      Unity/Assets/Scripts/Core/Network/OpcodeHelper.cs
  37. 0 0
      Unity/Assets/Scripts/Core/Network/OpcodeHelper.cs.meta
  38. 0 0
      Unity/Assets/Scripts/Core/Network/OpcodeRangeDefine.cs
  39. 0 0
      Unity/Assets/Scripts/Core/Network/OpcodeRangeDefine.cs.meta
  40. 0 0
      Unity/Assets/Scripts/Core/Network/OpcodeType.cs
  41. 0 0
      Unity/Assets/Scripts/Core/Network/OpcodeType.cs.meta
  42. 0 0
      Unity/Assets/Scripts/Core/Network/PacketParser.cs
  43. 0 0
      Unity/Assets/Scripts/Core/Network/PacketParser.cs.meta
  44. 0 0
      Unity/Assets/Scripts/Core/Network/ResponseTypeAttribute.cs
  45. 0 0
      Unity/Assets/Scripts/Core/Network/ResponseTypeAttribute.cs.meta
  46. 0 0
      Unity/Assets/Scripts/Core/Network/RpcException.cs
  47. 0 0
      Unity/Assets/Scripts/Core/Network/RpcException.cs.meta
  48. 2 2
      Unity/Assets/Scripts/Core/Network/TChannel.cs
  49. 0 0
      Unity/Assets/Scripts/Core/Network/TChannel.cs.meta
  50. 5 6
      Unity/Assets/Scripts/Core/Network/TService.cs
  51. 0 0
      Unity/Assets/Scripts/Core/Network/TService.cs.meta
  52. 2 2
      Unity/Assets/Scripts/Core/Network/WChannel.cs
  53. 0 0
      Unity/Assets/Scripts/Core/Network/WChannel.cs.meta
  54. 1 1
      Unity/Assets/Scripts/Core/Network/WService.cs
  55. 0 0
      Unity/Assets/Scripts/Core/Network/WService.cs.meta
  56. 0 239
      Unity/Assets/Scripts/Core/World/Module/Network/NetServices.cs
  57. 2 3
      Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterCheckComponentSystem.cs
  58. 9 17
      Unity/Assets/Scripts/Hotfix/Client/Module/Message/NetClientComponentSystem.cs
  59. 13 21
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentSystem.cs
  60. 6 8
      Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetServerComponentSystem.cs
  61. 1 2
      Unity/Assets/Scripts/Hotfix/Server/Module/Router/RouterComponentSystem.cs
  62. 9 5
      Unity/Assets/Scripts/HotfixView/Client/Demo/EntryEvent3_InitClient.cs
  63. 0 1
      Unity/Assets/Scripts/HotfixView/Client/Demo/Scene/AfterCreateClientScene_AddComponent.cs
  64. 3 1
      Unity/Assets/Scripts/HotfixView/Client/Demo/Scene/SceneChangeStart_AddComponent.cs
  65. 3 2
      Unity/Assets/Scripts/HotfixView/Client/Demo/UI/UIHelp/UIHelpEvent.cs
  66. 3 3
      Unity/Assets/Scripts/HotfixView/Client/Demo/UI/UILobby/UILobbyEvent.cs
  67. 3 3
      Unity/Assets/Scripts/HotfixView/Client/Demo/UI/UILogin/UILoginEvent.cs
  68. 5 3
      Unity/Assets/Scripts/HotfixView/Client/Demo/Unit/AfterUnitCreate_CreateUnitView.cs
  69. 2 1
      Unity/Assets/Scripts/HotfixView/Client/LockStep/LSSceneChangeStart_AddComponent.cs
  70. 4 2
      Unity/Assets/Scripts/HotfixView/Client/LockStep/LSUnitViewComponentSystem.cs
  71. 0 1
      Unity/Assets/Scripts/HotfixView/Client/LockStep/Scene/AfterCreateClientScene_LSAddComponent.cs
  72. 3 3
      Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSLobby/UILSLobbyEvent.cs
  73. 3 3
      Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSLogin/UILSLoginEvent.cs
  74. 3 3
      Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSRoom/UILSRoomEvent.cs
  75. 0 69
      Unity/Assets/Scripts/HotfixView/Client/Module/UI/UIEventComponentSystem.cs
  76. 0 11
      Unity/Assets/Scripts/HotfixView/Client/Module/UI/UIEventComponentSystem.cs.meta
  77. 1 1
      Unity/Assets/Scripts/Loader/GlobalComponent.cs
  78. 1 1
      Unity/Assets/Scripts/Model/Client/Module/Message/NetClientComponent.cs
  79. 1 1
      Unity/Assets/Scripts/Model/Server/Module/Message/NetInnerComponent.cs
  80. 1 1
      Unity/Assets/Scripts/Model/Server/Module/Message/NetServerComponent.cs
  81. 0 2
      Unity/Assets/Scripts/Model/Share/Entry.cs
  82. 8 8
      Unity/Assets/Scripts/Model/Share/Module/Message/Session.cs
  83. 22 21
      Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesComponent.cs
  84. 3 3
      Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesLoaderComponent.cs
  85. 56 2
      Unity/Assets/Scripts/ModelView/Client/Module/UI/UIEventComponent.cs

+ 1 - 1
Share/Tool/Share.Tool.csproj

@@ -43,7 +43,7 @@
             <Link>Module\Log\%(RecursiveDir)%(FileName)%(Extension)</Link>
         </Compile>
 
-        <Compile Include="..\..\Unity\Assets\Scripts\Core\World\Module\Network\OpcodeRangeDefine.cs">
+        <Compile Include="..\..\Unity\Assets\Scripts\Core\Network\OpcodeRangeDefine.cs">
             <Link>Module\Message\OpcodeRangeDefine.cs</Link>
         </Compile>
     </ItemGroup>

+ 3 - 1
Unity/Assets/Scripts/Core/Entity/EntityRef.cs

@@ -50,8 +50,10 @@ namespace ET
 
         private EntityWeakRef(T t)
         {
-            if (t == null)
+            if (t != null)
             {
+                this.instanceId = 0;
+                this.weakRef = null;
                 return;
             }
             this.instanceId = t.InstanceId;

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

@@ -23,7 +23,7 @@ namespace ET
     }
     
     [ChildOf(typeof(CoroutineLockQueue))]
-    public class CoroutineLock: Entity, IAwake<int, long, int>
+    public class CoroutineLock: Entity, IAwake<int, long, int>, IDestroy
     {
         public int type;
         public long key;

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

@@ -59,7 +59,7 @@ namespace ET
     }
     
     [ChildOf(typeof(CoroutineLockQueueType))]
-    public class CoroutineLockQueue: Entity, IAwake<int>
+    public class CoroutineLockQueue: Entity, IAwake<int>, IDestroy
     {
         public int type;
 

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

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

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/AChannel.cs → Unity/Assets/Scripts/Core/Network/AChannel.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/AChannel.cs.meta → Unity/Assets/Scripts/Core/Network/AChannel.cs.meta


+ 11 - 1
Unity/Assets/Scripts/Core/World/Module/Network/AService.cs → Unity/Assets/Scripts/Core/Network/AService.cs

@@ -6,7 +6,11 @@ namespace ET
 {
     public abstract class AService: IDisposable
     {
-        public int Id { get; set; }
+        public Action<long, IPEndPoint> AcceptCallback;
+        public Action<long, ActorId, object> ReadCallback;
+        public Action<long, int> ErrorCallback;
+        
+        public long Id { get; set; }
         
         public ServiceType ServiceType { get; protected set; }
         
@@ -31,9 +35,15 @@ namespace ET
             this.lastMessageInfo = (message, stream);
             return stream;
         }
+
+        public AService()
+        {
+            NetServices.Instance.Add(this);
+        }
         
         public virtual void Dispose()
         {
+            NetServices.Instance.Remove(this.Id);
         }
 
         public abstract void Update();

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/AService.cs.meta → Unity/Assets/Scripts/Core/Network/AService.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/ActorResponse.cs → Unity/Assets/Scripts/Core/Network/ActorResponse.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/ActorResponse.cs.meta → Unity/Assets/Scripts/Core/Network/ActorResponse.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/Circularbuffer.cs → Unity/Assets/Scripts/Core/Network/Circularbuffer.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/Circularbuffer.cs.meta → Unity/Assets/Scripts/Core/Network/Circularbuffer.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/ErrorCore.cs → Unity/Assets/Scripts/Core/Network/ErrorCore.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/ErrorCore.cs.meta → Unity/Assets/Scripts/Core/Network/ErrorCore.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/Extensions.cs → Unity/Assets/Scripts/Core/Network/Extensions.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/Extensions.cs.meta → Unity/Assets/Scripts/Core/Network/Extensions.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/IActorLocationMessage.cs → Unity/Assets/Scripts/Core/Network/IActorLocationMessage.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/IActorLocationMessage.cs.meta → Unity/Assets/Scripts/Core/Network/IActorLocationMessage.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/IActorMessage.cs → Unity/Assets/Scripts/Core/Network/IActorMessage.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/IActorMessage.cs.meta → Unity/Assets/Scripts/Core/Network/IActorMessage.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/IPEndPointNonAlloc.cs → Unity/Assets/Scripts/Core/Network/IPEndPointNonAlloc.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/IPEndPointNonAlloc.cs.meta → Unity/Assets/Scripts/Core/Network/IPEndPointNonAlloc.cs.meta


+ 2 - 2
Unity/Assets/Scripts/Core/World/Module/Network/KChannel.cs → Unity/Assets/Scripts/Core/Network/KChannel.cs

@@ -521,7 +521,7 @@ namespace ET
 						break;
 					}
 				}
-				NetServices.Instance.OnRead(this.Service.Id, channelId, actorId, message);
+				this.Service.ReadCallback(channelId, actorId, message);
 			}
 			catch (Exception e)
 			{
@@ -534,7 +534,7 @@ namespace ET
 		{
 			long channelId = this.Id;
 			this.Service.Remove(channelId, error);
-			NetServices.Instance.OnError(this.Service.Id, channelId, error);
+			this.Service.ErrorCallback(channelId, error);
 		}
 	}
 }

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/KChannel.cs.meta → Unity/Assets/Scripts/Core/Network/KChannel.cs.meta


+ 2 - 22
Unity/Assets/Scripts/Core/World/Module/Network/KService.cs → Unity/Assets/Scripts/Core/Network/KService.cs

@@ -55,26 +55,6 @@ namespace ET
             Kcp.SetOutput(KcpOutput);
         }
         
-#if ENABLE_IL2CPP
-		[AOT.MonoPInvokeCallback(typeof(KcpOutput))]
-#endif
-        private static void KcpLog(IntPtr bytes, int len, IntPtr kcp, IntPtr user)
-        {
-            try
-            {
-                unsafe
-                {
-                    //Marshal.Copy(bytes, logBuffer, 0, len);
-                    Span<byte> span = new Span<byte>(bytes.ToPointer(), len);
-                    Log.Info(span.ToString());
-                }
-            }
-            catch (Exception e)
-            {
-                Log.Error(e);
-            }
-        }
-
 #if ENABLE_IL2CPP
 		[AOT.MonoPInvokeCallback(typeof(KcpOutput))]
 #endif
@@ -87,7 +67,7 @@ namespace ET
                     return 0;
                 }
                 
-                KService kService = NetServices.Instance.Get((int)user.ToInt64()) as KService;
+                KService kService = NetServices.Instance.Get(user.ToInt64()) as KService;
                 
                 if (!kService.KcpPtrChannels.TryGetValue(kcp.ToInt64(), out KChannel kChannel))
                 {
@@ -321,7 +301,7 @@ namespace ET
                                 kChannel.RealAddress = realAddress;
 
                                 IPEndPoint realEndPoint = kChannel.RealAddress == null? kChannel.RemoteAddress : NetworkHelper.ToIPEndPoint(kChannel.RealAddress);
-                                NetServices.Instance.OnAccept(this.Id, kChannel.Id, realEndPoint);
+                                this.AcceptCallback(kChannel.Id, realEndPoint);
                             }
                             if (kChannel.RemoteConn != remoteConn)
                             {

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/KService.cs.meta → Unity/Assets/Scripts/Core/Network/KService.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/MessageAttribute.cs → Unity/Assets/Scripts/Core/Network/MessageAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/MessageAttribute.cs.meta → Unity/Assets/Scripts/Core/Network/MessageAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/MessagePool.cs → Unity/Assets/Scripts/Core/Network/MessagePool.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/MessagePool.cs.meta → Unity/Assets/Scripts/Core/Network/MessagePool.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/MessageSerializeHelper.cs → Unity/Assets/Scripts/Core/Network/MessageSerializeHelper.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/MessageSerializeHelper.cs.meta → Unity/Assets/Scripts/Core/Network/MessageSerializeHelper.cs.meta


+ 110 - 0
Unity/Assets/Scripts/Core/Network/NetServices.cs

@@ -0,0 +1,110 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+using System.Threading;
+
+namespace ET
+{
+    public enum NetworkProtocol
+    {
+        TCP,
+        KCP,
+        Websocket,
+    }
+
+    public class NetServices: Singleton<NetServices>
+    {
+        private readonly ConcurrentDictionary<long, AService> services = new();
+        
+        private readonly ConcurrentQueue<MemoryBuffer> pool = new();
+
+        private long idGenerator;
+
+        public override void Dispose()
+        {
+            if (this.IsDisposed())
+            {
+                return;
+            }
+            
+            base.Dispose();
+            
+            foreach (var kv in this.services)
+            {
+                kv.Value.Dispose();
+            }
+        }
+     
+        public MemoryBuffer FetchMemoryBuffer()
+        {
+            MemoryBuffer memoryBuffer;
+            if (this.pool.TryDequeue(out memoryBuffer))
+            {
+                return memoryBuffer;
+            }
+
+            memoryBuffer = new(128) { IsFromPool = true };
+            return memoryBuffer;
+        }
+
+        public void RecycleMemoryBuffer(MemoryBuffer memoryBuffer)
+        {
+            if (memoryBuffer == null)
+            {
+                return;
+            }
+            
+            if (!memoryBuffer.IsFromPool)
+            {
+                return;
+            }
+            if (memoryBuffer.Capacity > 128) // 太大的不回收,GC
+            {
+                return;
+            }
+
+            if (this.pool.Count > 1000)
+            {
+                return;
+            }
+
+            memoryBuffer.SetLength(0);
+            memoryBuffer.Seek(0, SeekOrigin.Begin);
+            this.pool.Enqueue(memoryBuffer);
+        }
+
+        public void Add(AService aService)
+        {
+            aService.Id = Interlocked.Increment(ref this.idGenerator);
+            this.services[aService.Id] = aService;
+        }
+
+        public AService Get(long id)
+        {
+            AService aService;
+            this.services.TryGetValue(id, out aService);
+            return aService;
+        }
+
+        public void Remove(long id)
+        {
+            this.services.Remove(id, out AService _);
+        }
+
+        // 这个因为是NetClientComponent中使用,不会与Accept冲突
+        public uint CreateConnectChannelId()
+        {
+            return RandomGenerator.RandUInt32();
+        }
+
+        // 防止与内网进程号的ChannelId冲突,所以设置为一个大的随机数
+        private uint acceptIdGenerator = uint.MaxValue;
+
+        public uint CreateAcceptChannelId()
+        {
+            return --this.acceptIdGenerator;
+        }
+    }
+}

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/NetServices.cs.meta → Unity/Assets/Scripts/Core/Network/NetServices.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/OpcodeHelper.cs → Unity/Assets/Scripts/Core/Network/OpcodeHelper.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/OpcodeHelper.cs.meta → Unity/Assets/Scripts/Core/Network/OpcodeHelper.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/OpcodeRangeDefine.cs → Unity/Assets/Scripts/Core/Network/OpcodeRangeDefine.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/OpcodeRangeDefine.cs.meta → Unity/Assets/Scripts/Core/Network/OpcodeRangeDefine.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/OpcodeType.cs → Unity/Assets/Scripts/Core/Network/OpcodeType.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/OpcodeType.cs.meta → Unity/Assets/Scripts/Core/Network/OpcodeType.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/PacketParser.cs → Unity/Assets/Scripts/Core/Network/PacketParser.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/PacketParser.cs.meta → Unity/Assets/Scripts/Core/Network/PacketParser.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/ResponseTypeAttribute.cs → Unity/Assets/Scripts/Core/Network/ResponseTypeAttribute.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/ResponseTypeAttribute.cs.meta → Unity/Assets/Scripts/Core/Network/ResponseTypeAttribute.cs.meta


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/RpcException.cs → Unity/Assets/Scripts/Core/Network/RpcException.cs


+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/RpcException.cs.meta → Unity/Assets/Scripts/Core/Network/RpcException.cs.meta


+ 2 - 2
Unity/Assets/Scripts/Core/World/Module/Network/TChannel.cs → Unity/Assets/Scripts/Core/Network/TChannel.cs

@@ -384,7 +384,7 @@ namespace ET
 						break;
 					}
 				}
-				NetServices.Instance.OnRead(this.Service.Id, channelId, actorId, message);
+				this.Service.ReadCallback(channelId, actorId, message);
 			}
 			catch (Exception e)
 			{
@@ -402,7 +402,7 @@ namespace ET
 			
 			this.Service.Remove(channelId);
 			
-			NetServices.Instance.OnError(this.Service.Id, channelId, error);
+			this.Service.ErrorCallback(channelId, error);
 		}
 	}
 }

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/TChannel.cs.meta → Unity/Assets/Scripts/Core/Network/TChannel.cs.meta


+ 5 - 6
Unity/Assets/Scripts/Core/World/Module/Network/TService.cs → Unity/Assets/Scripts/Core/Network/TService.cs

@@ -24,13 +24,13 @@ namespace ET
 	
 	public sealed class TService : AService
 	{
-		private readonly Dictionary<long, TChannel> idChannels = new Dictionary<long, TChannel>();
+		private readonly Dictionary<long, TChannel> idChannels = new();
 
-		private readonly SocketAsyncEventArgs innArgs = new SocketAsyncEventArgs();
+		private readonly SocketAsyncEventArgs innArgs = new();
 		
 		private Socket acceptor;
 
-		public ConcurrentQueue<TArgs> Queue = new ConcurrentQueue<TArgs>();
+		public ConcurrentQueue<TArgs> Queue = new();
 
 		public TService(AddressFamily addressFamily, ServiceType serviceType)
 		{
@@ -40,7 +40,6 @@ namespace ET
 		public TService(IPEndPoint ipEndPoint, ServiceType serviceType)
 		{
 			this.ServiceType = serviceType;
-			
 			this.acceptor = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 			// 容易出问题,先注释掉,按需开启
 			//this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
@@ -91,7 +90,7 @@ namespace ET
 				this.idChannels.Add(channel.Id, channel);
 				long channelId = channel.Id;
 				
-				NetServices.Instance.OnAccept(this.Id, channelId, channel.RemoteAddress);
+				this.AcceptCallback(channelId, channel.RemoteAddress);
 			}
 			catch (Exception exception)
 			{
@@ -168,7 +167,7 @@ namespace ET
 				TChannel aChannel = this.Get(channelId);
 				if (aChannel == null)
 				{
-					NetServices.Instance.OnError(this.Id, channelId, ErrorCore.ERR_SendMessageNotFoundTChannel);
+					this.ErrorCallback(channelId, ErrorCore.ERR_SendMessageNotFoundTChannel);
 					return;
 				}
 				

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/TService.cs.meta → Unity/Assets/Scripts/Core/Network/TService.cs.meta


+ 2 - 2
Unity/Assets/Scripts/Core/World/Module/Network/WChannel.cs → Unity/Assets/Scripts/Core/Network/WChannel.cs

@@ -230,7 +230,7 @@ namespace ET
                         break;
                     }
                 }
-                NetServices.Instance.OnRead(this.Service.Id, channelId, new ActorId(), message);
+                this.Service.ReadCallback(channelId, new ActorId(), message);
             }
             catch (Exception e)
             {
@@ -248,7 +248,7 @@ namespace ET
 			
             this.Service.Remove(channelId);
 			
-            NetServices.Instance.OnError(this.Service.Id, channelId, error);
+            this.Service.ErrorCallback(channelId, error);
         }
     }
 }

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/WChannel.cs.meta → Unity/Assets/Scripts/Core/Network/WChannel.cs.meta


+ 1 - 1
Unity/Assets/Scripts/Core/World/Module/Network/WService.cs → Unity/Assets/Scripts/Core/Network/WService.cs

@@ -105,7 +105,7 @@ namespace ET
 
                         this.channels[channel.Id] = channel;
 
-                        NetServices.Instance.OnAccept(this.Id, channel.Id, channel.RemoteAddress);
+                        this.AcceptCallback(channel.Id, channel.RemoteAddress);
                     }
                     catch (Exception e)
                     {

+ 0 - 0
Unity/Assets/Scripts/Core/World/Module/Network/WService.cs.meta → Unity/Assets/Scripts/Core/Network/WService.cs.meta


+ 0 - 239
Unity/Assets/Scripts/Core/World/Module/Network/NetServices.cs

@@ -1,239 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Net;
-
-
-
-namespace ET
-{
-    public enum NetworkProtocol
-    {
-        TCP,
-        KCP,
-        Websocket,
-    }
-
-    [ComponentOf(typeof(Fiber))]
-    public class NetServices: Entity, IAwake
-    {
-        private readonly Dictionary<int, Action<long, IPEndPoint>> acceptCallback = new();
-        private readonly Dictionary<int, Action<long, ActorId, object>> readCallback = new();
-        private readonly Dictionary<int, Action<long, int>> errorCallback = new();
-        
-        private readonly Dictionary<int, AService> services = new();
-        private readonly Queue<int> queue = new();
-
-        private readonly Queue<MemoryBuffer> pool = new();
-
-        private int serviceIdGenerator;
-
-        public override void Dispose()
-        {
-            if (this.IsDisposed)
-            {
-                return;
-            }
-            base.Dispose();
-
-            foreach (var kv in this.services)
-            {
-                kv.Value.Dispose();
-            }
-        }
-
-        public (uint, uint) GetChannelConn(int serviceId, long channelId)
-        {
-            AService service = this.Get(serviceId);
-            if (service == null)
-            {
-                return (0, 0);
-            }
-
-            return service.GetChannelConn(channelId);
-        }
-
-        public void ChangeAddress(int serviceId, long channelId, IPEndPoint ipEndPoint)
-        {
-            AService service = this.Get(serviceId);
-            if (service == null)
-            {
-                return;
-            }
-
-            service.ChangeAddress(channelId, ipEndPoint);
-        }
-
-        public void SendMessage(int serviceId, long channelId, ActorId actorId, MessageObject message)
-        {
-            AService service = this.Get(serviceId);
-            if (service != null)
-            {
-                service.Send(channelId, actorId, message);
-            }
-        }
-
-        public int AddService(AService aService)
-        {
-            aService.Id = ++this.serviceIdGenerator;
-            this.Add(aService);
-            return aService.Id;
-        }
-
-        public void RemoveService(int serviceId)
-        {
-            this.Remove(serviceId);
-        }
-
-        public void RemoveChannel(int serviceId, long channelId, int error)
-        {
-            AService service = this.Get(serviceId);
-            if (service != null)
-            {
-                service.Remove(channelId, error);
-            }
-        }
-
-        public void CreateChannel(int serviceId, long channelId, IPEndPoint address)
-        {
-            AService service = this.Get(serviceId);
-            if (service != null)
-            {
-                service.Create(channelId, address);
-            }
-        }
-
-        public void RegisterAcceptCallback(int serviceId, Action<long, IPEndPoint> action)
-        {
-            this.acceptCallback.Add(serviceId, action);
-        }
-
-        public void RegisterReadCallback(int serviceId, Action<long, ActorId, object> action)
-        {
-            this.readCallback.Add(serviceId, action);
-        }
-
-        public void RegisterErrorCallback(int serviceId, Action<long, int> action)
-        {
-            this.errorCallback.Add(serviceId, action);
-        }
-        
-
-        public MemoryBuffer FetchMemoryBuffer()
-        {
-            if (this.pool.Count > 0)
-            {
-                return this.pool.Dequeue();
-            }
-
-            MemoryBuffer memoryBuffer = new(128) { IsFromPool = true };
-            return memoryBuffer;
-        }
-
-        public void RecycleMemoryBuffer(MemoryBuffer memoryBuffer)
-        {
-            if (memoryBuffer == null)
-            {
-                return;
-            }
-            
-            if (!memoryBuffer.IsFromPool)
-            {
-                return;
-            }
-            if (memoryBuffer.Capacity > 128) // 太大的不回收,GC
-            {
-                return;
-            }
-
-            if (this.pool.Count > 1000)
-            {
-                return;
-            }
-
-            memoryBuffer.SetLength(0);
-            memoryBuffer.Seek(0, SeekOrigin.Begin);
-            this.pool.Enqueue(memoryBuffer);
-        }
-
-        private void Add(AService aService)
-        {
-            this.services[aService.Id] = aService;
-            this.queue.Enqueue(aService.Id);
-        }
-
-        public AService Get(int id)
-        {
-            AService aService;
-            this.services.TryGetValue(id, out aService);
-            return aService;
-        }
-
-        private void Remove(int id)
-        {
-            if (this.services.Remove(id, out AService service))
-            {
-                service.Dispose();
-            }
-        }
-
-        public void Update()
-        {
-            int count = this.queue.Count;
-            while (count-- > 0)
-            {
-                int serviceId = this.queue.Dequeue();
-                if (!this.services.TryGetValue(serviceId, out AService service))
-                {
-                    continue;
-                }
-
-                this.queue.Enqueue(serviceId);
-                service.Update();
-            }
-        }
-
-        public void OnAccept(int serviceId, long channelId, IPEndPoint ipEndPoint)
-        {
-            if (!this.acceptCallback.TryGetValue(serviceId, out var action))
-            {
-                return;
-            }
-
-            action.Invoke(channelId, ipEndPoint);
-        }
-
-        public void OnRead(int serviceId, long channelId, ActorId actorId, object message)
-        {
-            if (!this.readCallback.TryGetValue(serviceId, out var action))
-            {
-                return;
-            }
-
-            action.Invoke(channelId, actorId, message);
-        }
-
-        public void OnError(int serviceId, long channelId, int error)
-        {
-            if (!this.errorCallback.TryGetValue(serviceId, out Action<long, int> action))
-            {
-                return;
-            }
-            action.Invoke(channelId, error);
-        }
-
-        // 这个因为是NetClientComponent中使用,不会与Accept冲突
-        public uint CreateConnectChannelId()
-        {
-            return RandomGenerator.RandUInt32();
-        }
-
-        // 防止与内网进程号的ChannelId冲突,所以设置为一个大的随机数
-        private uint acceptIdGenerator = uint.MaxValue;
-
-        public uint CreateAcceptChannelId()
-        {
-            return --this.acceptIdGenerator;
-        }
-    }
-}

+ 2 - 3
Unity/Assets/Scripts/Hotfix/Client/Demo/Router/RouterCheckComponentSystem.cs

@@ -16,7 +16,6 @@ namespace ET.Client
             Session session = self.GetParent<Session>();
             long instanceId = self.InstanceId;
             Fiber fiber = self.Fiber();
-            
             while (true)
             {
                 if (self.InstanceId != instanceId)
@@ -42,7 +41,7 @@ namespace ET.Client
                 {
                     long sessionId = session.Id;
 
-                    (uint localConn, uint remoteConn) = NetServices.Instance.GetChannelConn(session.ServiceId, sessionId);
+                    (uint localConn, uint remoteConn) = session.AService.GetChannelConn(sessionId);
                     
                     IPEndPoint realAddress = self.GetParent<Session>().RemoteAddress;
                     Log.Info($"get recvLocalConn start: {self.ClientScene().Id} {realAddress} {localConn} {remoteConn}");
@@ -58,7 +57,7 @@ namespace ET.Client
                     
                     session.LastRecvTime = TimeHelper.ClientNow();
                     
-                    NetServices.Instance.ChangeAddress(session.ServiceId, sessionId, routerAddress);
+                    session.AService.ChangeAddress(sessionId, routerAddress);
                 }
                 catch (Exception e)
                 {

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

@@ -10,21 +10,15 @@ namespace ET.Client
         [EntitySystem]
         private static void Awake(this NetClientComponent self, AddressFamily addressFamily)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            self.ServiceId = netServices.AddService(new KService(addressFamily, ServiceType.Outer));
-            netServices.RegisterReadCallback(self.ServiceId, self.OnRead);
-            netServices.RegisterErrorCallback(self.ServiceId, self.OnError);
+            self.AService = new KService(addressFamily, ServiceType.Outer);
+            self.AService.ReadCallback = self.OnRead;
+            self.AService.ErrorCallback = self.OnError;
         }
         
         [EntitySystem]
         private static void Destroy(this NetClientComponent self)
         {
-            if (self.Fiber().IsDisposed)
-            {
-                return;
-            }
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            netServices.RemoveService(self.ServiceId);
+            self.AService.Dispose();
         }
 
         private static void OnRead(this NetClientComponent self, long channelId, ActorId actorId, object message)
@@ -56,30 +50,28 @@ namespace ET.Client
 
         public static Session Create(this NetClientComponent self, IPEndPoint realIPEndPoint)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            long channelId = netServices.CreateConnectChannelId();
-            Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
+            long channelId = NetServices.Instance.CreateConnectChannelId();
+            Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = realIPEndPoint;
             if (self.IScene.SceneType != SceneType.Benchmark)
             {
                 session.AddComponent<SessionIdleCheckerComponent>();
             }
-            netServices.CreateChannel(self.ServiceId, session.Id, realIPEndPoint);
+            self.AService.Create(session.Id, realIPEndPoint);
 
             return session;
         }
         
         public static Session Create(this NetClientComponent self, IPEndPoint routerIPEndPoint, IPEndPoint realIPEndPoint, uint localConn)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
             long channelId = localConn;
-            Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
+            Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = realIPEndPoint;
             if (self.IScene.SceneType != SceneType.Benchmark)
             {
                 session.AddComponent<SessionIdleCheckerComponent>();
             }
-            netServices.CreateChannel(self.ServiceId, session.Id, routerIPEndPoint);
+            self.AService.Create(session.Id, routerIPEndPoint);
             return session;
         }
     }

+ 13 - 21
Unity/Assets/Scripts/Hotfix/Server/Module/Message/NetInnerComponentSystem.cs

@@ -9,57 +9,50 @@ namespace ET.Server
         [EntitySystem]
         private static void Awake(this NetInnerComponent self)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
             switch (self.InnerProtocol)
             {
                 case NetworkProtocol.TCP:
                 {
-                    self.ServiceId = netServices.AddService(new TService(AddressFamily.InterNetwork, ServiceType.Inner));
+                    self.AService = new TService(AddressFamily.InterNetwork, ServiceType.Inner);
                     break;
                 }
                 case NetworkProtocol.KCP:
                 {
-                    self.ServiceId = netServices.AddService(new KService(AddressFamily.InterNetwork, ServiceType.Inner));
+                    self.AService = new KService(AddressFamily.InterNetwork, ServiceType.Inner);
                     break;
                 }
             }
                 
-            netServices.RegisterReadCallback(self.ServiceId, self.OnRead);
-            netServices.RegisterErrorCallback(self.ServiceId, self.OnError);
+            self.AService.ReadCallback = self.OnRead;
+            self.AService.ErrorCallback = self.OnError;
         }
 
         [EntitySystem]
         private static void Awake(this NetInnerComponent self, IPEndPoint address)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
             switch (self.InnerProtocol)
             {
                 case NetworkProtocol.TCP:
                 {
-                    self.ServiceId = netServices.AddService(new TService(address, ServiceType.Inner));
+                    self.AService = new TService(address, ServiceType.Inner);
                     break;
                 }
                 case NetworkProtocol.KCP:
                 {
-                    self.ServiceId = netServices.AddService(new KService(address, ServiceType.Inner));
+                    self.AService = new KService(address, ServiceType.Inner);
                     break;
                 }
             }
                 
-            netServices.RegisterAcceptCallback(self.ServiceId, self.OnAccept);
-            netServices.RegisterReadCallback(self.ServiceId, self.OnRead);
-            netServices.RegisterErrorCallback(self.ServiceId, self.OnError);
+            self.AService.AcceptCallback = self.OnAccept;
+            self.AService.ReadCallback = self.OnRead;
+            self.AService.ErrorCallback = self.OnError;
         }
 
         [EntitySystem]
         private static void Destroy(this NetInnerComponent self)
         {
-            if (self.Fiber().InstanceId == 0)
-            {
-                return;
-            }
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            netServices.RemoveService(self.ServiceId);
+            self.AService.Dispose();
         }
 
         private static void OnRead(this NetInnerComponent self, long channelId, ActorId actorId, object message)
@@ -96,17 +89,16 @@ namespace ET.Server
         // 这个channelId是由CreateAcceptChannelId生成的
         private static void OnAccept(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
         {
-            Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
+            Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = ipEndPoint;
             //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);
         }
 
         private static Session CreateInner(this NetInnerComponent self, long channelId, IPEndPoint ipEndPoint)
         {
-            Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
+            Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = ipEndPoint;
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            netServices.CreateChannel(self.ServiceId, channelId, ipEndPoint);
+            self.AService.Create(channelId, ipEndPoint);
 
             //session.AddComponent<InnerPingComponent>();
             //session.AddComponent<SessionIdleCheckerComponent, int, int, int>(NetThreadComponent.checkInteral, NetThreadComponent.recvMaxIdleTime, NetThreadComponent.sendMaxIdleTime);

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

@@ -9,18 +9,16 @@ namespace ET.Server
         [EntitySystem]
         private static void Awake(this NetServerComponent self, IPEndPoint address)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            self.ServiceId = netServices.AddService(new KService(address, ServiceType.Outer));
-            netServices.RegisterAcceptCallback(self.ServiceId, self.OnAccept);
-            netServices.RegisterReadCallback(self.ServiceId, self.OnRead);
-            netServices.RegisterErrorCallback(self.ServiceId, self.OnError);
+            self.AService = new KService(address, ServiceType.Outer);
+            self.AService.AcceptCallback = self.OnAccept;
+            self.AService.ReadCallback = self.OnRead;
+            self.AService.ErrorCallback = self.OnError;
         }
 
         [EntitySystem]
         private static void Destroy(this NetServerComponent self)
         {
-            NetServices netServices = self.Fiber().GetComponent<NetServices>();
-            netServices.RemoveService(self.ServiceId);
+            self.AService.Dispose();
         }
 
         private static void OnError(this NetServerComponent self, long channelId, int error)
@@ -38,7 +36,7 @@ namespace ET.Server
         // 这个channelId是由CreateAcceptChannelId生成的
         private static void OnAccept(this NetServerComponent self, long channelId, IPEndPoint ipEndPoint)
         {
-            Session session = self.AddChildWithId<Session, int>(channelId, self.ServiceId);
+            Session session = self.AddChildWithId<Session, AService>(channelId, self.AService);
             session.RemoteAddress = ipEndPoint;
 
             if (self.IScene.SceneType != SceneType.BenchmarkServer)

+ 1 - 2
Unity/Assets/Scripts/Hotfix/Server/Module/Router/RouterComponentSystem.cs

@@ -247,8 +247,7 @@ namespace ET.Server
                     self.ConnectIdNodes.TryGetValue(connectId, out routerNode);
                     if (routerNode == null)
                     {
-                        NetServices netServices = self.Fiber().GetComponent<NetServices>();
-                        outerConn = netServices.CreateConnectChannelId();
+                        outerConn = NetServices.Instance.CreateConnectChannelId();
                         routerNode = self.New(realAddress, connectId, outerConn, innerConn, self.CloneAddress());
                         Log.Info($"router create: {realAddress} {connectId} {outerConn} {innerConn} {routerNode.SyncIpEndPoint}");
                         self.OuterNodes.Add(routerNode.OuterConn, routerNode);

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

@@ -6,16 +6,20 @@ namespace ET.Client
     [Event(SceneType.Process)]
     public class EntryEvent3_InitClient: AEvent<Fiber, ET.EventType.EntryEvent3>
     {
-        protected override async ETTask Run(Fiber scene, ET.EventType.EntryEvent3 args)
+        protected override async ETTask Run(Fiber fiber, ET.EventType.EntryEvent3 args)
         {
+            World.Instance.AddSingleton<UIEventComponent>();
+            
             // 加载配置
-            scene.AddComponent<ResourcesComponent>();
+            ResourcesComponent resourcesComponent = fiber.AddComponent<ResourcesComponent>();
+
+            await resourcesComponent.LoadBundleAsync("unit.unity3d");
 
-            await ResourcesComponent.Instance.LoadBundleAsync("unit.unity3d");
+            GlobalComponent globalComponent = fiber.GetComponent<GlobalComponent>();
 
-            SceneType sceneType = EnumHelper.FromString<SceneType>(GlobalComponent.Instance.GlobalConfig.AppType.ToString());
+            SceneType sceneType = EnumHelper.FromString<SceneType>(globalComponent.GlobalConfig.AppType.ToString());
             
-            Scene clientScene = await SceneFactory.CreateClientScene(scene, 1, sceneType, sceneType.ToString());
+            Scene clientScene = await SceneFactory.CreateClientScene(fiber, 1, sceneType, sceneType.ToString());
             
             await EventSystem.Instance.PublishAsync(clientScene, new EventType.AppStartInitFinish());
         }

+ 0 - 1
Unity/Assets/Scripts/HotfixView/Client/Demo/Scene/AfterCreateClientScene_AddComponent.cs

@@ -5,7 +5,6 @@ namespace ET.Client
     {
         protected override async ETTask Run(Scene scene, EventType.AfterCreateClientScene args)
         {
-            scene.AddComponent<UIEventComponent>();
             scene.AddComponent<UIComponent>();
             scene.AddComponent<ResourcesLoaderComponent>();
             await ETTask.CompletedTask;

+ 3 - 1
Unity/Assets/Scripts/HotfixView/Client/Demo/Scene/SceneChangeStart_AddComponent.cs

@@ -8,9 +8,11 @@ namespace ET.Client
         protected override async ETTask Run(Scene scene, EventType.SceneChangeStart args)
         {
             Scene currentScene = scene.CurrentScene();
+
+            ResourcesComponent resourcesComponent = scene.Fiber().GetComponent<ResourcesComponent>();
             
             // 加载场景资源
-            await ResourcesComponent.Instance.LoadBundleAsync($"{currentScene.Name}.unity3d");
+            await resourcesComponent.LoadBundleAsync($"{currentScene.Name}.unity3d");
             // 切换到map场景
 
             await SceneManager.LoadSceneAsync(currentScene.Name);

+ 3 - 2
Unity/Assets/Scripts/HotfixView/Client/Demo/UI/UIHelp/UIHelpEvent.cs

@@ -10,8 +10,9 @@ namespace ET.Client
         {
 	        try
 	        {
-		        await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UIHelp.StringToAB());
-		        GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UIHelp.StringToAB(), UIType.UIHelp);
+		        ResourcesComponent resourcesComponent = uiComponent.Fiber().GetComponent<ResourcesComponent>();
+		        await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(resourcesComponent.StringToAB(UIType.UIHelp));
+		        GameObject bundleGameObject = (GameObject) resourcesComponent.GetAsset(resourcesComponent.StringToAB(UIType.UIHelp), UIType.UIHelp);
 		        GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
 		        UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UIHelp, gameObject);
 

+ 3 - 3
Unity/Assets/Scripts/HotfixView/Client/Demo/UI/UILobby/UILobbyEvent.cs

@@ -8,8 +8,9 @@ namespace ET.Client
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
             await ETTask.CompletedTask;
-            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILobby.StringToAB());
-            GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILobby.StringToAB(), UIType.UILobby);
+            ResourcesComponent resourcesComponent = uiComponent.Fiber().GetComponent<ResourcesComponent>();
+            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(resourcesComponent.StringToAB(UIType.UILobby));
+            GameObject bundleGameObject = (GameObject) resourcesComponent.GetAsset(resourcesComponent.StringToAB(UIType.UILobby), UIType.UILobby);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILobby, gameObject);
 
@@ -19,7 +20,6 @@ namespace ET.Client
 
         public override void OnRemove(UIComponent uiComponent)
         {
-            ResourcesComponent.Instance.UnloadBundle(UIType.UILobby.StringToAB());
         }
     }
 }

+ 3 - 3
Unity/Assets/Scripts/HotfixView/Client/Demo/UI/UILogin/UILoginEvent.cs

@@ -8,8 +8,9 @@ namespace ET.Client
     {
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
-            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILogin.StringToAB());
-            GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILogin.StringToAB(), UIType.UILogin);
+            ResourcesComponent resourcesComponent = uiComponent.Fiber().GetComponent<ResourcesComponent>();
+            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(resourcesComponent.StringToAB(UIType.UILogin));
+            GameObject bundleGameObject = (GameObject) resourcesComponent.GetAsset(resourcesComponent.StringToAB(UIType.UILogin), UIType.UILogin);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILogin, gameObject);
             ui.AddComponent<UILoginComponent>();
@@ -18,7 +19,6 @@ namespace ET.Client
 
         public override void OnRemove(UIComponent uiComponent)
         {
-            ResourcesComponent.Instance.UnloadBundle(UIType.UILogin.StringToAB());
         }
     }
 }

+ 5 - 3
Unity/Assets/Scripts/HotfixView/Client/Demo/Unit/AfterUnitCreate_CreateUnitView.cs

@@ -10,10 +10,12 @@ namespace ET.Client
             Unit unit = args.Unit;
             // Unit View层
             // 这里可以改成异步加载,demo就不搞了
-            GameObject bundleGameObject = (GameObject)ResourcesComponent.Instance.GetAsset("Unit.unity3d", "Unit");
+            ResourcesComponent resourcesComponent = scene.Fiber().GetComponent<ResourcesComponent>();
+            GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset("Unit.unity3d", "Unit");
             GameObject prefab = bundleGameObject.Get<GameObject>("Skeleton");
-	        
-            GameObject go = UnityEngine.Object.Instantiate(prefab, GlobalComponent.Instance.Unit, true);
+
+            GlobalComponent globalComponent = scene.Fiber().GetComponent<GlobalComponent>();
+            GameObject go = UnityEngine.Object.Instantiate(prefab, globalComponent.Unit, true);
             go.transform.position = unit.Position;
             unit.AddComponent<GameObjectComponent>().GameObject = go;
             unit.AddComponent<AnimatorComponent>();

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

@@ -18,7 +18,8 @@ namespace ET.Client
             await UIHelper.Create(args.Room, UIType.UILSRoom, UILayer.Low);
             
             // 加载场景资源
-            await ResourcesComponent.Instance.LoadBundleAsync($"{room.Name}.unity3d");
+            ResourcesComponent resourcesComponent = clientScene.Fiber().GetComponent<ResourcesComponent>();
+            await resourcesComponent.LoadBundleAsync($"{room.Name}.unity3d");
             // 切换到map场景
 
             await SceneManager.LoadSceneAsync(room.Name);

+ 4 - 2
Unity/Assets/Scripts/HotfixView/Client/LockStep/LSUnitViewComponentSystem.cs

@@ -12,10 +12,12 @@ namespace ET.Client
             foreach (long playerId in room.PlayerIds)
             {
                 LSUnit lsUnit = lsUnitComponent.GetChild<LSUnit>(playerId);
-                GameObject bundleGameObject = (GameObject)ResourcesComponent.Instance.GetAsset("Unit.unity3d", "Unit");
+                ResourcesComponent resourcesComponent = self.Fiber().GetComponent<ResourcesComponent>();
+                GameObject bundleGameObject = (GameObject)resourcesComponent.GetAsset("Unit.unity3d", "Unit");
                 GameObject prefab = bundleGameObject.Get<GameObject>("Skeleton");
 
-                GameObject unitGo = UnityEngine.Object.Instantiate(prefab, GlobalComponent.Instance.Unit, true);
+                GlobalComponent globalComponent = self.Fiber().GetComponent<GlobalComponent>();
+                GameObject unitGo = UnityEngine.Object.Instantiate(prefab, globalComponent.Unit, true);
                 unitGo.transform.position = lsUnit.Position.ToVector();
 
                 LSUnitView lsUnitView = self.AddChildWithId<LSUnitView, GameObject>(lsUnit.Id, unitGo);

+ 0 - 1
Unity/Assets/Scripts/HotfixView/Client/LockStep/Scene/AfterCreateClientScene_LSAddComponent.cs

@@ -5,7 +5,6 @@ namespace ET.Client
     {
         protected override async ETTask Run(Scene scene, EventType.AfterCreateClientScene args)
         {
-            scene.AddComponent<UIEventComponent>();
             scene.AddComponent<UIComponent>();
             scene.AddComponent<ResourcesLoaderComponent>();
             await ETTask.CompletedTask;

+ 3 - 3
Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSLobby/UILSLobbyEvent.cs

@@ -8,8 +8,9 @@ namespace ET.Client
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
             await ETTask.CompletedTask;
-            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILSLobby.StringToAB());
-            GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILSLobby.StringToAB(), UIType.UILSLobby);
+            ResourcesComponent resourcesComponent = uiComponent.Fiber().GetComponent<ResourcesComponent>();
+            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(resourcesComponent.StringToAB(UIType.UILSLobby));
+            GameObject bundleGameObject = (GameObject) resourcesComponent.GetAsset(resourcesComponent.StringToAB(UIType.UILSLobby), UIType.UILSLobby);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILSLobby, gameObject);
 
@@ -19,7 +20,6 @@ namespace ET.Client
 
         public override void OnRemove(UIComponent uiComponent)
         {
-            ResourcesComponent.Instance.UnloadBundle(UIType.UILSLobby.StringToAB());
         }
     }
 }

+ 3 - 3
Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSLogin/UILSLoginEvent.cs

@@ -8,8 +8,9 @@ namespace ET.Client
     {
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
-            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILSLogin.StringToAB());
-            GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILSLogin.StringToAB(), UIType.UILSLogin);
+            ResourcesComponent resourcesComponent = uiComponent.Fiber().GetComponent<ResourcesComponent>();
+            await uiComponent.Scene().GetComponent<ResourcesLoaderComponent>().LoadAsync(resourcesComponent.StringToAB(UIType.UILSLogin));
+            GameObject bundleGameObject = (GameObject) resourcesComponent.GetAsset(resourcesComponent.StringToAB(UIType.UILSLogin), UIType.UILSLogin);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILSLogin, gameObject);
             ui.AddComponent<UILSLoginComponent>();
@@ -18,7 +19,6 @@ namespace ET.Client
 
         public override void OnRemove(UIComponent uiComponent)
         {
-            ResourcesComponent.Instance.UnloadBundle(UIType.UILSLogin.StringToAB());
         }
     }
 }

+ 3 - 3
Unity/Assets/Scripts/HotfixView/Client/LockStep/UI/UILSRoom/UILSRoomEvent.cs

@@ -8,8 +8,9 @@ namespace ET.Client
     {
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
-            await uiComponent.Room().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILSRoom.StringToAB());
-            GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILSRoom.StringToAB(), UIType.UILSRoom);
+            ResourcesComponent resourcesComponent = uiComponent.Fiber().GetComponent<ResourcesComponent>();
+            await uiComponent.Room().GetComponent<ResourcesLoaderComponent>().LoadAsync(resourcesComponent.StringToAB(UIType.UILSRoom));
+            GameObject bundleGameObject = (GameObject) resourcesComponent.GetAsset(resourcesComponent.StringToAB(UIType.UILSRoom), UIType.UILSRoom);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILSRoom, gameObject);
             ui.AddComponent<UILSRoomComponent>();
@@ -18,7 +19,6 @@ namespace ET.Client
 
         public override void OnRemove(UIComponent uiComponent)
         {
-            ResourcesComponent.Instance.UnloadBundle(UIType.UILSRoom.StringToAB());
         }
     }
 }

+ 0 - 69
Unity/Assets/Scripts/HotfixView/Client/Module/UI/UIEventComponentSystem.cs

@@ -1,69 +0,0 @@
-using System;
-using System.Collections.Generic;
-using UnityEngine;
-
-namespace ET.Client
-{
-    /// <summary>
-    /// 管理所有UI GameObject 以及UI事件
-    /// </summary>
-    [FriendOf(typeof (UIEventComponent))]
-    public static partial class UIEventComponentSystem
-    {
-        [EntitySystem]
-        private static void Awake(this UIEventComponent self)
-        {
-            GameObject uiRoot = GameObject.Find("/Global/UI");
-            ReferenceCollector referenceCollector = uiRoot.GetComponent<ReferenceCollector>();
-
-            self.UILayers.Add((int)UILayer.Hidden, referenceCollector.Get<GameObject>(UILayer.Hidden.ToString()).transform);
-            self.UILayers.Add((int)UILayer.Low, referenceCollector.Get<GameObject>(UILayer.Low.ToString()).transform);
-            self.UILayers.Add((int)UILayer.Mid, referenceCollector.Get<GameObject>(UILayer.Mid.ToString()).transform);
-            self.UILayers.Add((int)UILayer.High, referenceCollector.Get<GameObject>(UILayer.High.ToString()).transform);
-
-            var uiEvents = EventSystem.Instance.GetTypes(typeof (UIEventAttribute));
-            foreach (Type type in uiEvents)
-            {
-                object[] attrs = type.GetCustomAttributes(typeof (UIEventAttribute), false);
-                if (attrs.Length == 0)
-                {
-                    continue;
-                }
-
-                UIEventAttribute uiEventAttribute = attrs[0] as UIEventAttribute;
-                AUIEvent aUIEvent = Activator.CreateInstance(type) as AUIEvent;
-                self.UIEvents.Add(uiEventAttribute.UIType, aUIEvent);
-            }
-        }
-
-        public static async ETTask<UI> OnCreate(this UIEventComponent self, UIComponent uiComponent, string uiType, UILayer uiLayer)
-        {
-            try
-            {
-                UI ui = await self.UIEvents[uiType].OnCreate(uiComponent, uiLayer);
-                return ui;
-            }
-            catch (Exception e)
-            {
-                throw new Exception($"on create ui error: {uiType}", e);
-            }
-        }
-
-        public static Transform GetLayer(this UIEventComponent self, int layer)
-        {
-            return self.UILayers[layer];
-        }
-
-        public static void OnRemove(this UIEventComponent self, UIComponent uiComponent, string uiType)
-        {
-            try
-            {
-                self.UIEvents[uiType].OnRemove(uiComponent);
-            }
-            catch (Exception e)
-            {
-                throw new Exception($"on remove ui error: {uiType}", e);
-            }
-        }
-    }
-}

+ 0 - 11
Unity/Assets/Scripts/HotfixView/Client/Module/UI/UIEventComponentSystem.cs.meta

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

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

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

+ 1 - 1
Unity/Assets/Scripts/Model/Client/Module/Message/NetClientComponent.cs

@@ -11,6 +11,6 @@ namespace ET.Client
     [ComponentOf(typeof(Scene))]
     public class NetClientComponent: Entity, IAwake<AddressFamily>, IDestroy
     {
-        public int ServiceId;
+        public AService AService;
     }
 }

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Message/NetInnerComponent.cs

@@ -6,7 +6,7 @@ namespace ET.Server
     [ComponentOf(typeof(Fiber))]
     public class NetInnerComponent: Entity, IAwake<IPEndPoint>, IAwake, IDestroy
     {
-        public int ServiceId;
+        public AService AService;
         
         public NetworkProtocol InnerProtocol = NetworkProtocol.KCP;
     }

+ 1 - 1
Unity/Assets/Scripts/Model/Server/Module/Message/NetServerComponent.cs

@@ -11,6 +11,6 @@ namespace ET.Server
     [ComponentOf(typeof(Scene))]
     public class NetServerComponent: Entity, IAwake<IPEndPoint>, IDestroy
     {
-        public int ServiceId;
+        public AService AService;
     }
 }

+ 0 - 2
Unity/Assets/Scripts/Model/Share/Entry.cs

@@ -62,8 +62,6 @@ namespace ET
             fiber.AddComponent<MainThreadSynchronizationContext>();
             fiber.AddComponent<TimerComponent>();
             fiber.AddComponent<CoroutineLockComponent>();
-            
-            fiber.AddComponent<NetServices>();
 
             await World.Instance.AddSingleton<ConfigComponent>().LoadAsync();
 

+ 8 - 8
Unity/Assets/Scripts/Model/Share/Module/Message/Session.cs

@@ -21,9 +21,9 @@ namespace ET
     public static partial class SessionSystem
     {
         [EntitySystem]
-        private static void Awake(this Session self, int serviceId)
+        private static void Awake(this Session self, AService aService)
         {
-            self.ServiceId = serviceId;
+            self.AService = aService;
             long timeNow = TimeHelper.ClientNow();
             self.LastRecvTime = timeNow;
             self.LastSendTime = timeNow;
@@ -36,7 +36,7 @@ namespace ET
         [EntitySystem]
         private static void Destroy(this Session self)
         {
-            self.Fiber().GetComponent<NetServices>().RemoveChannel(self.ServiceId, self.Id, self.Error);
+            self.AService.Remove(self.Id, self.Error);
             
             foreach (RpcInfo responseCallback in self.requestCallbacks.Values.ToArray())
             {
@@ -103,7 +103,7 @@ namespace ET
         public static async ETTask<IResponse> Call(this Session self, IRequest request)
         {
             int rpcId = ++Session.RpcId;
-            RpcInfo rpcInfo = new RpcInfo(request);
+            RpcInfo rpcInfo = new(request);
             self.requestCallbacks[rpcId] = rpcInfo;
             request.RpcId = rpcId;
             self.Send(request);
@@ -119,14 +119,14 @@ namespace ET
         {
             self.LastSendTime = TimeHelper.ClientNow();
             Log.Debug(message.ToString());
-            self.Fiber().GetComponent<NetServices>().SendMessage(self.ServiceId, self.Id, actorId, message as MessageObject);
+            self.AService.Send(self.Id, actorId, message as MessageObject);
         }
     }
 
     [ChildOf]
-    public sealed class Session: Entity, IAwake<int>, IDestroy
+    public sealed class Session: Entity, IAwake<AService>, IDestroy
     {
-        public int ServiceId { get; set; }
+        public AService AService { get; set; }
         
         public static int RpcId
         {
@@ -134,7 +134,7 @@ namespace ET
             set;
         }
 
-        public readonly Dictionary<int, RpcInfo> requestCallbacks = new Dictionary<int, RpcInfo>();
+        public readonly Dictionary<int, RpcInfo> requestCallbacks = new();
         
         public long LastRecvTime
         {

+ 22 - 21
Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesComponent.cs

@@ -57,47 +57,48 @@ namespace ET.Client
     [FriendOf(typeof (ResourcesComponent))]
     public static class AssetBundleHelper
     {
-        public static string IntToString(this int value)
+        public static string IntToString(this ResourcesComponent resourcesComponent, int value)
         {
             string result;
-            if (ResourcesComponent.Instance.IntToStringDict.TryGetValue(value, out result))
+            if (resourcesComponent.IntToStringDict.TryGetValue(value, out result))
             {
                 return result;
             }
 
             result = value.ToString();
-            ResourcesComponent.Instance.IntToStringDict[value] = result;
+            resourcesComponent.IntToStringDict[value] = result;
             return result;
         }
 
-        public static string StringToAB(this string value)
+        public static string StringToAB(this ResourcesComponent resourcesComponent, string value)
         {
             string result;
-            if (ResourcesComponent.Instance.StringToABDict.TryGetValue(value, out result))
+            if (resourcesComponent.StringToABDict.TryGetValue(value, out result))
             {
                 return result;
             }
 
             result = value + ".unity3d";
-            ResourcesComponent.Instance.StringToABDict[value] = result;
+            resourcesComponent.StringToABDict[value] = result;
             return result;
         }
 
-        public static string IntToAB(this int value)
+        public static string IntToAB(this ResourcesComponent resourcesComponent, int value)
         {
-            return value.IntToString().StringToAB();
+            string s = resourcesComponent.IntToString(value);
+            return resourcesComponent.StringToAB(s);
         }
 
-        public static string BundleNameToLower(this string value)
+        public static string BundleNameToLower(this ResourcesComponent resourcesComponent, string value)
         {
             string result;
-            if (ResourcesComponent.Instance.BundleNameToLowerDict.TryGetValue(value, out result))
+            if (resourcesComponent.BundleNameToLowerDict.TryGetValue(value, out result))
             {
                 return result;
             }
 
             result = value.ToLower();
-            ResourcesComponent.Instance.BundleNameToLowerDict[value] = result;
+            resourcesComponent.BundleNameToLowerDict[value] = result;
             return result;
         }
     }
@@ -206,7 +207,7 @@ namespace ET.Client
         public static UnityEngine.Object GetAsset(this ResourcesComponent self, string bundleName, string prefab)
         {
             Dictionary<string, UnityEngine.Object> dict;
-            if (!self.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
+            if (!self.resourceCache.TryGetValue(self.BundleNameToLower(bundleName), out dict))
             {
                 throw new Exception($"not found asset: {bundleName} {prefab}");
             }
@@ -223,7 +224,7 @@ namespace ET.Client
         // 一帧卸载一个包,避免卡死
         public static async ETTask UnloadBundleAsync(this ResourcesComponent self, string assetBundleName, bool unload = true)
         {
-            assetBundleName = assetBundleName.BundleNameToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
 
             string[] dependencies = self.GetSortedDependencies(assetBundleName);
 
@@ -243,7 +244,7 @@ namespace ET.Client
         // 只允许场景设置unload为false
         public static void UnloadBundle(this ResourcesComponent self, string assetBundleName, bool unload = true)
         {
-            assetBundleName = assetBundleName.BundleNameToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
 
             string[] dependencies = self.GetSortedDependencies(assetBundleName);
 
@@ -258,7 +259,7 @@ namespace ET.Client
 
         private static void UnloadOneBundle(this ResourcesComponent self, string assetBundleName, bool unload = true)
         {
-            assetBundleName = assetBundleName.BundleNameToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
 
             ABInfo abInfo;
             if (!self.bundles.TryGetValue(assetBundleName, out abInfo))
@@ -289,7 +290,7 @@ namespace ET.Client
         /// <returns></returns>
         public static void LoadBundle(this ResourcesComponent self, string assetBundleName)
         {
-            assetBundleName = assetBundleName.ToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
 
             string[] dependencies = self.GetSortedDependencies(assetBundleName);
             //Log.Debug($"-----------dep load start {assetBundleName} dep: {dependencies.ToList().ListToString()}");
@@ -309,7 +310,7 @@ namespace ET.Client
         private static void AddResource(this ResourcesComponent self, string bundleName, string assetName, UnityEngine.Object resource)
         {
             Dictionary<string, UnityEngine.Object> dict;
-            if (!self.resourceCache.TryGetValue(bundleName.BundleNameToLower(), out dict))
+            if (!self.resourceCache.TryGetValue(self.BundleNameToLower(bundleName), out dict))
             {
                 dict = new Dictionary<string, UnityEngine.Object>();
                 self.resourceCache[bundleName] = dict;
@@ -320,7 +321,7 @@ namespace ET.Client
 
         private static void LoadOneBundle(this ResourcesComponent self, string assetBundleName)
         {
-            assetBundleName = assetBundleName.BundleNameToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
             ABInfo abInfo;
             if (self.bundles.TryGetValue(assetBundleName, out abInfo))
             {
@@ -397,7 +398,7 @@ namespace ET.Client
         /// </summary>
         public static async ETTask LoadBundleAsync(this ResourcesComponent self, string assetBundleName)
         {
-            assetBundleName = assetBundleName.BundleNameToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
 
             string[] dependencies = self.GetSortedDependencies(assetBundleName);
             //Log.Debug($"-----------dep load async start {assetBundleName} dep: {dependencies.ToList().ListToString()}");
@@ -442,7 +443,7 @@ namespace ET.Client
 
         private static async ETTask<ABInfo> LoadOneBundleAsync(this ResourcesComponent self, string assetBundleName)
         {
-            assetBundleName = assetBundleName.BundleNameToLower();
+            assetBundleName = self.BundleNameToLower(assetBundleName);
             ABInfo abInfo;
             if (self.bundles.TryGetValue(assetBundleName, out abInfo))
             {
@@ -552,7 +553,7 @@ namespace ET.Client
     }
 
     [ComponentOf(typeof(Fiber))]
-    public class ResourcesComponent: SingletonEntity<ResourcesComponent>, IAwake, IDestroy
+    public class ResourcesComponent: Entity, IAwake, IDestroy
     {
         public AssetBundleManifest AssetBundleManifestObject { get; set; }
 

+ 3 - 3
Unity/Assets/Scripts/ModelView/Client/Module/Resource/ResourcesLoaderComponent.cs

@@ -30,12 +30,12 @@ namespace ET.Client
                     using CoroutineLock coroutineLock =
                             await coroutineLockComponent.Wait(CoroutineLockType.ResourcesLoader, abName.GetHashCode(), 0);
                     {
-                        if (ResourcesComponent.Instance == null)
+                        if (self.Fiber().IsDisposed)
                         {
                             return;
                         }
 
-                        await ResourcesComponent.Instance.UnloadBundleAsync(abName);
+                        await self.Fiber().GetComponent<ResourcesComponent>().UnloadBundleAsync(abName);
                     }
                 }
             }
@@ -59,7 +59,7 @@ namespace ET.Client
             }
 
             self.LoadedResource.Add(ab);
-            await ResourcesComponent.Instance.LoadBundleAsync(ab);
+            await self.Fiber().GetComponent<ResourcesComponent>().LoadBundleAsync(ab);
         }
     }
 

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

@@ -7,11 +7,65 @@ namespace ET.Client
 	/// <summary>
 	/// 管理所有UI GameObject
 	/// </summary>
-	[ComponentOf(typeof(Fiber))]
-	public class UIEventComponent: SingletonEntity<UIEventComponent>, IAwake
+	public class UIEventComponent: Singleton<UIEventComponent>, ISingletonAwake
 	{
 		public Dictionary<string, AUIEvent> UIEvents = new Dictionary<string, AUIEvent>();
 		
 		public Dictionary<int, Transform> UILayers = new Dictionary<int, Transform>();
+		
+        public void Awake()
+        {
+            GameObject uiRoot = GameObject.Find("/Global/UI");
+            ReferenceCollector referenceCollector = uiRoot.GetComponent<ReferenceCollector>();
+
+            this.UILayers.Add((int)UILayer.Hidden, referenceCollector.Get<GameObject>(UILayer.Hidden.ToString()).transform);
+            this.UILayers.Add((int)UILayer.Low, referenceCollector.Get<GameObject>(UILayer.Low.ToString()).transform);
+            this.UILayers.Add((int)UILayer.Mid, referenceCollector.Get<GameObject>(UILayer.Mid.ToString()).transform);
+            this.UILayers.Add((int)UILayer.High, referenceCollector.Get<GameObject>(UILayer.High.ToString()).transform);
+
+            var uiEvents = EventSystem.Instance.GetTypes(typeof (UIEventAttribute));
+            foreach (Type type in uiEvents)
+            {
+                object[] attrs = type.GetCustomAttributes(typeof (UIEventAttribute), false);
+                if (attrs.Length == 0)
+                {
+                    continue;
+                }
+
+                UIEventAttribute uiEventAttribute = attrs[0] as UIEventAttribute;
+                AUIEvent aUIEvent = Activator.CreateInstance(type) as AUIEvent;
+                this.UIEvents.Add(uiEventAttribute.UIType, aUIEvent);
+            }
+        }
+
+        public async ETTask<UI> OnCreate(UIComponent uiComponent, string uiType, UILayer uiLayer)
+        {
+            try
+            {
+                UI ui = await this.UIEvents[uiType].OnCreate(uiComponent, uiLayer);
+                return ui;
+            }
+            catch (Exception e)
+            {
+                throw new Exception($"on create ui error: {uiType}", e);
+            }
+        }
+
+        public Transform GetLayer(int layer)
+        {
+            return this.UILayers[layer];
+        }
+
+        public void OnRemove(UIComponent uiComponent, string uiType)
+        {
+            try
+            {
+                this.UIEvents[uiType].OnRemove(uiComponent);
+            }
+            catch (Exception e)
+            {
+                throw new Exception($"on remove ui error: {uiType}", e);
+            }
+        }
 	}
 }