Przeglądaj źródła

1.服务端只需要加载该AppType需要的配置文件,由ConfigAttribute参数控制
2.HttpAttribute之前漏掉了继承BaseAttribute

tanghai 7 lat temu
rodzic
commit
d124502c4d

+ 1 - 1
Server/Hotfix/Handler/Actor_TransferHandler.cs

@@ -26,7 +26,7 @@ namespace ETHotfix
 				
 				int mapIndex = message.MapIndex;
 
-				StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
+				StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
 
 				// 考虑AllServer情况
 				if (startConfigComponent.Count == 1)

+ 1 - 1
Server/Hotfix/Handler/M2A_ReloadHandler.cs

@@ -17,7 +17,7 @@ namespace ETHotfix
 			catch (Exception e)
 			{
 				response.Error = ErrorCode.ERR_ReloadFail;
-				StartConfig myStartConfig = Game.Scene.GetComponent<StartConfigComponent>().StartConfig;
+				StartConfig myStartConfig = StartConfigComponent.Instance.StartConfig;
 				InnerConfig innerConfig = myStartConfig.GetComponent<InnerConfig>();
 				response.Message = $"{innerConfig.IPEndPoint} reload fail, {e}";
 				reply(response);

+ 1 - 1
Server/Hotfix/Module/Actor/ActorMessageDispatherComponentSystem.cs

@@ -35,7 +35,7 @@ namespace ETHotfix
 
 		public static void Load(this ActorMessageDispatherComponent self)
 		{
-			AppType appType = self.Entity.GetComponent<StartConfigComponent>().StartConfig.AppType;
+			AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
 
 			self.ActorMessageHandlers.Clear();
 			self.ActorTypeHandlers.Clear();

+ 2 - 2
Server/Hotfix/Module/Actor/ActorMessageSenderSystem.cs

@@ -44,7 +44,7 @@ namespace ETHotfix
 				self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
 			}
 
-			self.Address = Game.Scene.GetComponent<StartConfigComponent>()
+			self.Address = StartConfigComponent.Instance
 					.Get(IdGenerater.GetAppIdFromId(self.ActorId))
 					.GetComponent<InnerConfig>().IPEndPoint;
 
@@ -170,7 +170,7 @@ namespace ETHotfix
 					// 等待1s再发送
 					await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
 					self.ActorId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(self.Id);
-					self.Address = Game.Scene.GetComponent<StartConfigComponent>()
+					self.Address = StartConfigComponent.Instance
 							.Get(IdGenerater.GetAppIdFromId(self.ActorId))
 							.GetComponent<InnerConfig>().IPEndPoint;
 					self.AllowGet();

+ 1 - 1
Server/Hotfix/Module/BigWorld/LockComponentSystem.cs

@@ -71,7 +71,7 @@ namespace ETHotfix
 			try
 			{
 				Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(self.address);
-				string serverAddress = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.ServerIP;
+				string serverAddress = StartConfigComponent.Instance.StartConfig.ServerIP;
 				G2G_LockRequest request = new G2G_LockRequest { Id = self.Entity.Id, Address = serverAddress };
 				await session.Call(request);
 

+ 116 - 0
Server/Hotfix/Module/Config/ConfigComponentSystem.cs

@@ -0,0 +1,116 @@
+using System;
+using System.Collections.Generic;
+using ETModel;
+
+namespace ETHotfix
+{
+    [ObjectSystem]
+    public class ConfigAwakeSystem : AwakeSystem<ConfigComponent>
+    {
+        public override void Awake(ConfigComponent self)
+        {
+            self.Awake();
+        }
+    }
+
+    [ObjectSystem]
+    public class ConfigLoadSystem : LoadSystem<ConfigComponent>
+    {
+        public override void Load(ConfigComponent self)
+        {
+            self.Load();
+        }
+    }
+    
+    public static class ConfigComponentHelper
+	{
+		public static void Awake(this ConfigComponent self)
+		{
+			self.Load();
+		}
+
+		public static void Load(this ConfigComponent self)
+		{
+			AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
+			
+			self.AllConfig.Clear();
+			List<Type> types = Game.EventSystem.GetTypes(typeof(ConfigAttribute));
+
+			foreach (Type type in types)
+			{
+				object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
+				if (attrs.Length == 0)
+				{
+					continue;
+				}
+				
+				ConfigAttribute configAttribute = attrs[0] as ConfigAttribute;
+				// 只加载指定的配置
+				if (!configAttribute.Type.Is(appType))
+				{
+					continue;
+				}
+				
+				object obj = Activator.CreateInstance(type);
+
+				ACategory iCategory = obj as ACategory;
+				if (iCategory == null)
+				{
+					throw new Exception($"class: {type.Name} not inherit from ACategory");
+				}
+				iCategory.BeginInit();
+				iCategory.EndInit();
+
+				self.AllConfig[iCategory.ConfigType] = iCategory;
+			}
+		}
+
+		public static IConfig GetOne(this ConfigComponent self, Type type)
+		{
+			ACategory configCategory;
+			if (!self.AllConfig.TryGetValue(type, out configCategory))
+			{
+				throw new Exception($"ConfigComponent not found key: {type.FullName}");
+			}
+			return configCategory.GetOne();
+		}
+
+		public static IConfig Get(this ConfigComponent self, Type type, int id)
+		{
+			ACategory configCategory;
+			if (!self.AllConfig.TryGetValue(type, out configCategory))
+			{
+				throw new Exception($"ConfigComponent not found key: {type.FullName}");
+			}
+
+			return configCategory.TryGet(id);
+		}
+
+		public static IConfig TryGet(this ConfigComponent self, Type type, int id)
+		{
+			ACategory configCategory;
+			if (!self.AllConfig.TryGetValue(type, out configCategory))
+			{
+				return null;
+			}
+			return configCategory.TryGet(id);
+		}
+
+		public static IConfig[] GetAll(this ConfigComponent self, Type type)
+		{
+			ACategory configCategory;
+			if (!self.AllConfig.TryGetValue(type, out configCategory))
+			{
+				throw new Exception($"ConfigComponent not found key: {type.FullName}");
+			}
+			return configCategory.GetAll();
+		}
+
+		public static ACategory GetCategory(this ConfigComponent self, Type type)
+		{
+			ACategory configCategory;
+			bool ret = self.AllConfig.TryGetValue(type, out configCategory);
+			return ret ? configCategory : null;
+		}
+	}
+}

+ 1 - 1
Server/Hotfix/Module/DB/DBProxyComponentSystem.cs

@@ -27,7 +27,7 @@ namespace ETHotfix
 	{
 		public static void Awake(this DBProxyComponent self)
 		{
-			StartConfig dbStartConfig = Game.Scene.GetComponent<StartConfigComponent>().DBConfig;
+			StartConfig dbStartConfig = StartConfigComponent.Instance.DBConfig;
 			self.dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
 		}
 

+ 1 - 1
Server/Hotfix/Module/FrameSync/C2G_EnterMapHandler.cs

@@ -14,7 +14,7 @@ namespace ETHotfix
 			{
 				Player player = session.GetComponent<SessionPlayerComponent>().Player;
 				// 在map服务器上创建战斗Unit
-				IPEndPoint mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
+				IPEndPoint mapAddress = StartConfigComponent.Instance.MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
 				Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
 				M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId });
 				player.UnitId = createUnit.UnitId;

+ 1 - 1
Server/Hotfix/Module/FrameSync/RealmGateAddressComponentSystem.cs

@@ -15,7 +15,7 @@ namespace ETHotfix
 	{
 		public static void Start(this RealmGateAddressComponent component)
 		{
-			StartConfig[] startConfigs = component.Entity.GetComponent<StartConfigComponent>().GetAll();
+			StartConfig[] startConfigs = StartConfigComponent.Instance.GetAll();
 			foreach (StartConfig config in startConfigs)
 			{
 				if (!config.AppType.Is(AppType.Gate))

+ 1 - 1
Server/Hotfix/Module/Location/LocationProxyComponentSystem.cs

@@ -16,7 +16,7 @@ namespace ETHotfix
 	{
 		public static void Awake(this LocationProxyComponent self)
 		{
-			StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
+			StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
 
 			StartConfig startConfig = startConfigComponent.LocationConfig;
 			self.LocationAddress = startConfig.GetComponent<InnerConfig>().IPEndPoint;

+ 1 - 1
Server/Hotfix/Module/Message/MessageDispatherComponentSystem.cs

@@ -31,7 +31,7 @@ namespace ETHotfix
 		{
 			self.Handlers.Clear();
 
-			AppType appType = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.AppType;
+			AppType appType = StartConfigComponent.Instance.StartConfig.AppType;
 
 			List<Type> types = Game.EventSystem.GetTypes(typeof(MessageHandlerAttribute));
 

+ 2 - 2
Server/Hotfix/Module/Message/NetInnerComponentSystem.cs

@@ -37,7 +37,7 @@ namespace ETHotfix
 			self.Awake(NetworkProtocol.TCP);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
-			self.AppType = self.Entity.GetComponent<StartConfigComponent>().StartConfig.AppType;
+			self.AppType = StartConfigComponent.Instance.StartConfig.AppType;
 		}
 
 		public static void Awake(this NetInnerComponent self, IPEndPoint ipEndPoint)
@@ -45,7 +45,7 @@ namespace ETHotfix
 			self.Awake(NetworkProtocol.TCP, ipEndPoint);
 			self.MessagePacker = new MongoPacker();
 			self.MessageDispatcher = new InnerMessageDispatcher();
-			self.AppType = self.Entity.GetComponent<StartConfigComponent>().StartConfig.AppType;
+			self.AppType = StartConfigComponent.Instance.StartConfig.AppType;
 		}
 
 		public static void Update(this NetInnerComponent self)

+ 2 - 2
Server/Model/Component/AppManagerComponent.cs

@@ -22,7 +22,7 @@ namespace ETModel
 		public void Start()
 		{
 			string[] ips = NetHelper.GetAddressIPs();
-			StartConfig[] startConfigs = Game.Scene.GetComponent<StartConfigComponent>().GetAll();
+			StartConfig[] startConfigs = StartConfigComponent.Instance.GetAll();
 			
 			foreach (StartConfig startConfig in startConfigs)
 			{
@@ -47,7 +47,7 @@ namespace ETModel
 		private void StartProcess(int appId)
 		{
 			OptionComponent optionComponent = Game.Scene.GetComponent<OptionComponent>();
-			StartConfigComponent startConfigComponent = Game.Scene.GetComponent<StartConfigComponent>();
+			StartConfigComponent startConfigComponent = StartConfigComponent.Instance;
 			string configFile = optionComponent.Options.Config;
 			StartConfig startConfig = startConfigComponent.Get(appId);
 			const string exe = "dotnet";

+ 15 - 0
Server/Model/Component/StartConfigComponent.cs

@@ -16,6 +16,8 @@ namespace ETModel
 	
 	public class StartConfigComponent: Component
 	{
+		public static StartConfigComponent Instance { get; private set; }
+		
 		private Dictionary<int, StartConfig> configDict;
 		
 		public StartConfig StartConfig { get; private set; }
@@ -32,6 +34,8 @@ namespace ETModel
 
 		public void Awake(string path, int appId)
 		{
+			Instance = this;
+			
 			this.configDict = new Dictionary<int, StartConfig>();
 			this.MapConfigs = new List<StartConfig>();
 			this.GateConfigs = new List<StartConfig>();
@@ -83,6 +87,17 @@ namespace ETModel
 			this.StartConfig = this.Get(appId);
 		}
 
+		public override void Dispose()
+		{
+			if (this.IsDisposed)
+			{
+				return;
+			}
+			base.Dispose();
+			
+			Instance = null;
+		}
+
 		public StartConfig Get(int id)
 		{
 			try

+ 13 - 0
Server/Model/Module/Config/ConfigComponent.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+
+namespace ETModel
+{
+	/// <summary>
+	/// Config组件会扫描所有的有ConfigAttribute标签的配置,加载进来
+	/// </summary>
+	public class ConfigComponent: Component
+	{
+		public Dictionary<Type, ACategory> AllConfig = new Dictionary<Type, ACategory>();
+	}
+}

+ 1 - 1
Server/Model/Module/Http/HttpComponent.cs

@@ -53,7 +53,7 @@ namespace ETModel
 
 		public void Awake()
 		{
-			StartConfig startConfig = Game.Scene.GetComponent<StartConfigComponent>().StartConfig;
+			StartConfig startConfig = StartConfigComponent.Instance.StartConfig;
 			this.appType = startConfig.AppType;
 			this.HttpConfig = startConfig.GetComponent<HttpConfig>();
 

+ 1 - 1
Server/Model/Module/Http/HttpHandlerAttribute.cs

@@ -2,7 +2,7 @@
 
 namespace ETModel
 {
-	public class HttpHandlerAttribute : Attribute
+	public class HttpHandlerAttribute : BaseAttribute
 	{
 		public AppType AppType { get; }
 

+ 0 - 1
Server/Model/Server.Model.csproj

@@ -74,7 +74,6 @@
     <Compile Include="..\..\Unity\Assets\Scripts\Component\TimerComponent.cs" Link="Component\TimerComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Config\ACategory.cs" Link="Module\Config\ACategory.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Config\ConfigAttribute.cs" Link="Module\Config\ConfigAttribute.cs" />
-    <Compile Include="..\..\Unity\Assets\Scripts\Module\Config\ConfigComponent.cs" Link="Module\Config\ConfigComponent.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\Config\IConfig.cs" Link="Module\Config\IConfig.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\FrameSync\FrameMessage.cs" Link="Module\FrameSync\FrameMessage.cs" />
     <Compile Include="..\..\Unity\Assets\Scripts\Module\FrameSync\FrameOpcode.cs" Link="Module\FrameSync\FrameOpcode.cs" />

+ 2 - 7
Unity/Assets/Scripts/Base/OneThreadSynchronizationContext.cs

@@ -6,18 +6,13 @@ namespace ETModel
 {
 	public class OneThreadSynchronizationContext : SynchronizationContext
 	{
-		public static OneThreadSynchronizationContext Instance = new OneThreadSynchronizationContext();
+		public static OneThreadSynchronizationContext Instance { get; } = new OneThreadSynchronizationContext();
 
 		// 线程同步队列,发送接收socket回调都放到该队列,由poll线程统一执行
 		private readonly ConcurrentQueue<Action> queue = new ConcurrentQueue<Action>();
 
 		private Action a;
 
-		private void Add(Action action)
-		{
-			this.queue.Enqueue(action);
-		}
-
 		public void Update()
 		{
 			while (true)
@@ -32,7 +27,7 @@ namespace ETModel
 
 		public override void Post(SendOrPostCallback callback, object state)
 		{
-			this.Add(() => { callback(state); });
+			this.queue.Enqueue(() => { callback(state); });
 		}
 	}
 }

+ 1 - 0
Unity/Assets/Scripts/Module/Config/ConfigComponent.cs

@@ -45,6 +45,7 @@ namespace ETModel
 				{
 					continue;
 				}
+				
 				object obj = Activator.CreateInstance(type);
 
 				ACategory iCategory = obj as ACategory;