Procházet zdrojové kódy

ConfigAttribute增加ServerType选项,不同的Server可以搭配不同的配置文件

tanghai před 11 roky
rodič
revize
87bb8e0156

+ 2 - 3
CSharp/Game/Controller/ConfigCategory/BuffCategory.cs

@@ -1,9 +1,8 @@
-using Common.Config;
-using Model;
+using Model;
 
 namespace Controller
 {
-	[Config]
+	[Config(ServerType.Realm, ServerType.Gate, ServerType.City)]
 	public class BuffCategory: ACategory<BuffConfig>
 	{
 	}

+ 1 - 2
CSharp/Game/Controller/ConfigCategory/GlobalCategory.cs

@@ -1,5 +1,4 @@
-using Common.Config;
-using Model;
+using Model;
 
 namespace Controller
 {

+ 2 - 3
CSharp/Game/Controller/ConfigCategory/NodeCategory.cs

@@ -1,9 +1,8 @@
-using Common.Config;
-using Model;
+using Model;
 
 namespace Controller
 {
-	[Config]
+	[Config(ServerType.All)]
 	public class NodeCategory: ACategory<NodeConfig>
 	{
 	}

+ 29 - 0
CSharp/Game/Controller/ConfigCategory/ServerInfoCategory.cs

@@ -0,0 +1,29 @@
+using System.Collections.Generic;
+using Model;
+
+namespace Controller
+{
+	[Config(ServerType.All)]
+	public class ServerInfoCategory : ACategory<ServerInfoConfig>
+	{
+		public Dictionary<string, ServerInfoConfig> NameServerInfoConfigs = new Dictionary<string, ServerInfoConfig>();
+
+		public override void EndInit()
+		{
+			base.EndInit();
+
+			foreach (ServerInfoConfig serverInfoConfig in this.GetAll())
+			{
+				this.NameServerInfoConfigs[serverInfoConfig.Name] = serverInfoConfig;
+			}
+		}
+
+		public ServerInfoConfig this[string name]
+		{
+			get
+			{
+				return this.NameServerInfoConfigs[name];
+			}
+		}
+	}
+}

+ 2 - 3
CSharp/Game/Controller/ConfigCategory/UnitCategory.cs

@@ -1,9 +1,8 @@
-using Common.Config;
-using Model;
+using Model;
 
 namespace Controller
 {
-	[Config]
+	[Config(ServerType.All)]
 	public class UnitCategory: ACategory<UnitConfig>
 	{
 	}

+ 1 - 0
CSharp/Game/Controller/Controller.csproj

@@ -45,6 +45,7 @@
     <Compile Include="ConfigCategory\BuffCategory.cs" />
     <Compile Include="ConfigCategory\GlobalCategory.cs" />
     <Compile Include="ConfigCategory\NodeCategory.cs" />
+    <Compile Include="ConfigCategory\ServerInfoCategory.cs" />
     <Compile Include="ConfigCategory\UnitCategory.cs" />
     <Compile Include="Event\AfterAddBuff.cs" />
     <Compile Include="Factory\UnitFactory.cs" />

+ 2 - 2
CSharp/Platform/Common/Config/ACategory.cs → CSharp/Game/Model/ACategory.cs

@@ -4,11 +4,11 @@ using System.IO;
 using System.Linq;
 using Common.Helper;
 
-namespace Common.Config
+namespace Model
 {
 	public abstract class ACategory<T>: ICategory where T : AConfig
 	{
-		protected Dictionary<int, T> dict;
+		private Dictionary<int, T> dict;
 
 		public virtual void BeginInit()
 		{

+ 1 - 1
CSharp/Platform/Common/Config/AConfig.cs → CSharp/Game/Model/AConfig.cs

@@ -1,7 +1,7 @@
 using System.ComponentModel;
 using MongoDB.Bson.Serialization.Attributes;
 
-namespace Common.Config
+namespace Model
 {
 	public abstract class AConfig: ISupportInitialize
 	{

+ 9 - 2
CSharp/Game/Model/Component/ConfigComponent.cs

@@ -2,18 +2,20 @@
 using System.Collections.Generic;
 using System.Reflection;
 using Common.Base;
-using Common.Config;
 
 namespace Model
 {
 	public class ConfigComponent: Component<World>, IAssemblyLoader
 	{
-		public Dictionary<Type, ICategory> allConfig;
+		private Dictionary<Type, ICategory> allConfig;
 
 		public void Load(Assembly assembly)
 		{
 			this.allConfig = new Dictionary<Type, ICategory>();
 			Type[] types = assembly.GetTypes();
+
+			ServerType serverType = World.Instance.Options.ServerType;
+
 			foreach (Type type in types)
 			{
 				object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
@@ -21,6 +23,11 @@ namespace Model
 				{
 					continue;
 				}
+				ConfigAttribute configAttribute = (ConfigAttribute) attrs[0];
+				if (!configAttribute.Contain(serverType))
+				{
+					continue;
+				}
 
 				object obj = (Activator.CreateInstance(type));
 

+ 1 - 0
CSharp/Game/Model/Component/NetworkComponent.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Reflection;
 using Common.Base;
 using Common.Event;
 using Common.Network;

+ 0 - 1
CSharp/Game/Model/Config/BuffConfig.cs

@@ -1,5 +1,4 @@
 using System.Collections.Generic;
-using Common.Config;
 
 namespace Model
 {

+ 1 - 3
CSharp/Game/Model/Config/GlobalConfig.cs

@@ -1,6 +1,4 @@
-using Common.Config;
-
-namespace Model
+namespace Model
 {
 	public class GlobalConfig: AConfig
 	{

+ 0 - 1
CSharp/Game/Model/Config/NodeConfig.cs

@@ -1,5 +1,4 @@
 using System.Collections.Generic;
-using Common.Config;
 
 namespace Model
 {

+ 11 - 0
CSharp/Game/Model/Config/ServerInfoConfig.cs

@@ -0,0 +1,11 @@
+namespace Model
+{
+	public class ServerInfoConfig : AConfig
+	{
+		public string Zone { get; set; }
+		public string Name { get; set; }
+		public string Host { get; set; }
+		public int Port { get; set; }
+		public ServerType ServerType { get; set; }
+	}
+}

+ 1 - 3
CSharp/Game/Model/Config/UnitConfig.cs

@@ -1,6 +1,4 @@
-using Common.Config;
-
-namespace Model
+namespace Model
 {
 	public class UnitConfig: AConfig
 	{

+ 27 - 0
CSharp/Game/Model/ConfigAttribute.cs

@@ -0,0 +1,27 @@
+using System;
+
+namespace Model
+{
+	[AttributeUsage(AttributeTargets.Class)]
+	public class ConfigAttribute: Attribute
+	{
+		private int ServerType { get; set; }
+
+		public ConfigAttribute(params ServerType[] serverTypes)
+		{
+			foreach (ServerType serverType in serverTypes)
+			{
+				this.ServerType |= (int)serverType;
+			}
+		}
+
+		public bool Contain(ServerType serverType)
+		{
+			if ((this.ServerType & (int)serverType) == 0)
+			{
+				return false;
+			}
+			return true;
+		}
+	}
+}

+ 1 - 1
CSharp/Platform/Common/Config/ICategory.cs → CSharp/Game/Model/ICategory.cs

@@ -1,7 +1,7 @@
 using System;
 using System.ComponentModel;
 
-namespace Common.Config
+namespace Model
 {
 	public interface ICategory: ISupportInitialize
 	{

+ 9 - 0
CSharp/Game/Model/IConfigLoader.cs

@@ -0,0 +1,9 @@
+using System.Reflection;
+
+namespace Model
+{
+	interface IConfigLoader
+	{
+		void Load(Assembly assembly);
+	}
+}

+ 7 - 0
CSharp/Game/Model/Model.csproj

@@ -43,6 +43,8 @@
     <Reference Include="System.Core" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="ACategory.cs" />
+    <Compile Include="AConfig.cs" />
     <Compile Include="BehaviorTree\BehaviorTree.cs" />
     <Compile Include="BehaviorTree\BehaviorTreeFactory.cs" />
     <Compile Include="BehaviorTree\BlackBoard.cs" />
@@ -60,21 +62,26 @@
     <Compile Include="Component\NetworkComponent.cs" />
     <Compile Include="Component\TimerComponent.cs" />
     <Compile Include="Component\UnitComponent.cs" />
+    <Compile Include="ConfigAttribute.cs" />
     <Compile Include="Config\BuffConfig.cs" />
     <Compile Include="Config\GlobalConfig.cs" />
     <Compile Include="Config\NodeConfig.cs" />
+    <Compile Include="Config\ServerInfoConfig.cs" />
     <Compile Include="Config\UnitConfig.cs" />
     <Compile Include="EnvKey.cs" />
     <Compile Include="EventAttribute.cs" />
     <Compile Include="EventType.cs" />
     <Compile Include="FactoryAttribute.cs" />
     <Compile Include="IAssemblyLoader.cs" />
+    <Compile Include="ICategory.cs" />
+    <Compile Include="IConfigLoader.cs" />
     <Compile Include="IFactory.cs" />
     <Compile Include="IStart.cs" />
     <Compile Include="IUpdate.cs" />
     <Compile Include="MessageAttribute.cs" />
     <Compile Include="Options.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="ServerType.cs" />
     <Compile Include="Unit.cs" />
     <Compile Include="UnitType.cs" />
     <Compile Include="World.cs" />

+ 8 - 2
CSharp/Game/Model/Options.cs

@@ -2,10 +2,16 @@
 using CommandLine;
 using Common.Network;
 
-namespace Common.Base
+namespace Model
 {
 	public class Options
 	{
+		[Option('s', "serverType", Required = true, HelpText = "ServerType.")]
+		public ServerType ServerType { get; set; }
+
+		[Option('n', "name", Required = true, HelpText = "Name.")]
+		public string Name { get; set; }
+
 		[Option('h', "host", Required = true, HelpText = "Host.")]
 		public string Host { get; set; }
 
@@ -15,7 +21,7 @@ namespace Common.Base
 		[Option("protocol", Required = true, HelpText = "Protocol, tcp or udp.")]
 		public NetworkProtocol Protocol { get; set; }
 
-		[Option('v', null, HelpText = "Print details during execution.")]
+		[Option('v', HelpText = "Print details during execution.")]
 		public bool Verbose { get; set; }
 
 		[HelpOption]

+ 14 - 0
CSharp/Game/Model/ServerType.cs

@@ -0,0 +1,14 @@
+using System;
+
+namespace Model
+{
+	[Flags]
+	public enum ServerType
+	{
+		None = 0,
+		Realm = 1,
+		Gate = 2,
+		City = 4,
+		All = int.MaxValue,
+	}
+}

+ 0 - 4
CSharp/Platform/Common/Common.csproj

@@ -54,10 +54,6 @@
     <Compile Include="Base\Object.cs" />
     <Compile Include="Base\QueueDictionary.cs" />
     <Compile Include="Base\TimerManager.cs" />
-    <Compile Include="Config\ACategory.cs" />
-    <Compile Include="Config\AConfig.cs" />
-    <Compile Include="Config\ConfigAttribute.cs" />
-    <Compile Include="Config\ICategory.cs" />
     <Compile Include="Event\AEventAttribute.cs" />
     <Compile Include="Event\Env.cs" />
     <Compile Include="Event\IEventAsync.cs" />

+ 0 - 9
CSharp/Platform/Common/Config/ConfigAttribute.cs

@@ -1,9 +0,0 @@
-using System;
-
-namespace Common.Config
-{
-	[AttributeUsage(AttributeTargets.Class)]
-	public class ConfigAttribute: Attribute
-	{
-	}
-}