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

读取配置模块更新,更加自动化

tanghai 11 лет назад
Родитель
Сommit
a3d0b31a38

+ 4 - 4
CSharp/Game/BehaviorTree/BehaviorTreeFactory.cs

@@ -37,12 +37,12 @@ namespace BehaviorTree
 
         private Node CreateNode(NodeConfig config)
         {
-            if (!this.dictionary.ContainsKey((NodeType) config.Type))
+            if (!this.dictionary.ContainsKey((NodeType) config.Id))
             {
-                throw new KeyNotFoundException(string.Format("CreateNode cannot found: {0}",
-                        config.Type));
+                throw new KeyNotFoundException(
+                    string.Format("CreateNode cannot found: {0}", config.Id));
             }
-            return this.dictionary[(NodeType) config.Type](config);
+            return this.dictionary[(NodeType) config.Id](config);
         }
 
         private Node CreateTreeNode(NodeConfig config)

+ 2 - 2
CSharp/Game/BehaviorTree/NodeAttribute.cs

@@ -5,8 +5,8 @@ namespace BehaviorTree
     [AttributeUsage(AttributeTargets.Class)]
     public class NodeAttribute : Attribute
     {
-        public NodeType NodeType { get; set; }
-        public Type ClassType { get; set; }
+        public NodeType NodeType { get; private set; }
+        public Type ClassType { get; private set; }
 
         public NodeAttribute(NodeType nodeType, Type classType)
         {

+ 7 - 23
CSharp/Game/BehaviorTree/NodeConfig.cs

@@ -1,38 +1,22 @@
 using System.Collections.Generic;
+using Common.Config;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace BehaviorTree
 {
-    public class NodeConfig
+    public class NodeConfig: IConfig
     {
-        public uint Id { get; set; }
-
-        public int Type { get; set; }
+        public int Id { get; set; }
 
         [BsonIgnoreIfNull]
         public List<string> Args { get; set; }
 
         [BsonIgnoreIfNull]
         public List<NodeConfig> SubConfigs { get; set; }
+    }
 
-        public void AddArgs(string arg)
-        {
-            if (this.Args == null)
-            {
-                this.Args = new List<string>();
-            }
-
-            this.Args.Add(arg);
-        }
-
-        public void AddSubConfig(NodeConfig subConfig)
-        {
-            if (this.SubConfigs == null)
-            {
-                this.SubConfigs = new List<NodeConfig>();
-            }
-
-            this.SubConfigs.Add(subConfig);
-        }
+    [Config]
+    public class NodeCategory : ACategory<NodeConfig>
+    {
     }
 }

+ 1 - 1
CSharp/Game/BehaviorTree/NodeType.cs

@@ -2,7 +2,7 @@
 {
     public enum NodeType
     {
-        Select = 1,
+        Selector = 1,
         Sequence = 2,
         Not = 10,
     }

+ 1 - 1
CSharp/Game/BehaviorTree/Selector.cs

@@ -1,6 +1,6 @@
 namespace BehaviorTree
 {
-    [NodeAttribute(NodeType.Select, typeof(Selector))]
+    [NodeAttribute(NodeType.Selector, typeof(Selector))]
     public class Selector: Node
     {
         public Selector(NodeConfig config): base(config)

+ 6 - 4
CSharp/Game/World/Config/GlobalConfig.cs

@@ -1,12 +1,14 @@
-namespace Model.Config
+using Common.Config;
+
+namespace World.Config
 {
-    public class GlobalConfig: IType
+    public class GlobalConfig: IConfig
     {
-        public int Type { get; set; }
+        public int Id { get; set; }
     }
 
     [Config]
-    public class GlobalCategory: ConfigCategory<GlobalConfig>
+    public class GlobalCategory: ACategory<GlobalConfig>
     {
     }
 }

+ 4 - 2
CSharp/Game/World/World.cs

@@ -1,4 +1,5 @@
-using Model;
+using Common.Config;
+using Model;
 
 namespace World
 {
@@ -6,7 +7,7 @@ namespace World
     {
         private static readonly World instance = new World();
 
-        private readonly ConfigManager configManager = new ConfigManager(typeof(World).Assembly);
+        private readonly ConfigManager configManager = new ConfigManager();
 
         private readonly GameObjectManager gameObjectManager = new GameObjectManager();
 
@@ -20,6 +21,7 @@ namespace World
 
         private World()
         {
+            configManager.Load(typeof(World).Assembly);
         }
 
         public ConfigManager ConfigManager

+ 3 - 2
CSharp/Platform/Common/Common.csproj

@@ -58,15 +58,16 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Config\ICategory.cs" />
     <Compile Include="Event\EventTrigger.cs" />
     <Compile Include="Event\Env.cs" />
     <Compile Include="Event\IEventAttribute.cs" />
     <Compile Include="Event\IEvent.cs" />
     <Compile Include="Base\Object.cs" />
     <Compile Include="Config\ConfigAttribute.cs" />
-    <Compile Include="Config\ConfigCategory.cs" />
+    <Compile Include="Config\ACategory.cs" />
     <Compile Include="Config\ConfigManager.cs" />
-    <Compile Include="Config\IType.cs" />
+    <Compile Include="Config\IConfig.cs" />
     <Compile Include="Helper\BigIntegerHelper.cs" />
     <Compile Include="Helper\ByteHelper.cs" />
     <Compile Include="Helper\EnumHelper.cs" />

+ 13 - 17
CSharp/Platform/Common/Config/ConfigCategory.cs → CSharp/Platform/Common/Config/ACategory.cs

@@ -1,18 +1,18 @@
 using System;
 using System.Collections.Generic;
-using System.ComponentModel;
 using System.IO;
+using System.Linq;
 using Common.Helper;
 
-namespace Model
+namespace Common.Config
 {
-    public abstract class ConfigCategory<T>: ISupportInitialize where T : IType
+    public abstract class ACategory<T>: ICategory where T : IConfig
     {
         protected readonly Dictionary<int, T> dict = new Dictionary<int, T>();
 
-        protected ConfigCategory()
+        public void BeginInit()
         {
-            string path = Path.Combine(@"./Config/", this.ConfigName);
+            string path = Path.Combine(@"./Config/", this.Name);
 
             if (!Directory.Exists(path))
             {
@@ -22,10 +22,14 @@ namespace Model
             foreach (var file in Directory.GetFiles(path))
             {
                 var t = MongoHelper.FromJson<T>(File.ReadAllText(file));
-                this.dict.Add(t.Type, t);
+                this.dict.Add(t.Id, t);
             }
         }
 
+        public void EndInit()
+        {
+        }
+
         public T this[int type]
         {
             get
@@ -34,7 +38,7 @@ namespace Model
             }
         }
 
-        public string ConfigName
+        public string Name
         {
             get
             {
@@ -42,17 +46,9 @@ namespace Model
             }
         }
 
-        public Dictionary<int, T> GetAll()
-        {
-            return this.dict;
-        }
-
-        public void BeginInit()
-        {
-        }
-
-        public void EndInit()
+        public T[] GetAll()
         {
+            return this.dict.Values.ToArray();
         }
     }
 }

+ 1 - 1
CSharp/Platform/Common/Config/ConfigAttribute.cs

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

+ 15 - 17
CSharp/Platform/Common/Config/ConfigManager.cs

@@ -1,19 +1,13 @@
 using System;
 using System.Collections.Generic;
-using System.ComponentModel;
 using System.Reflection;
 
-namespace Model
+namespace Common.Config
 {
     public class ConfigManager
     {
         public Dictionary<string, object> allConfig;
 
-        public ConfigManager(Assembly assembly)
-        {
-            Load(assembly);
-        }
-
         public void Load(Assembly assembly)
         {
             var localAllConfig = new Dictionary<string, object>();
@@ -28,33 +22,37 @@ namespace Model
                 
                 object obj = (Activator.CreateInstance(type));
 
-                var iSupportInitialize = obj as ISupportInitialize;
-
-                if (iSupportInitialize != null)
+                ICategory iCategory = obj as ICategory;
+                if (iCategory == null)
                 {
-                    iSupportInitialize.EndInit();
+                    throw new Exception(string.Format("class: {0} not inherit from ACategory", type.Name));
                 }
+                iCategory.BeginInit();
+                iCategory.EndInit();
 
                 localAllConfig[type.Name] = obj;
             }
             this.allConfig = localAllConfig;
         }
 
-        public T Get<T>(int type) where T : IType
+        public T Get<T>(int type) where T : IConfig
         {
-            var configCategory = (ConfigCategory<T>)this.allConfig[typeof(T).Name];
+            var configCategory = (ACategory<T>)this.allConfig[typeof(T).Name];
             return configCategory[type];
         }
 
-        public Dictionary<int, T> GetAll<T>() where T : IType
+        public T[] GetAll<T>() where T : IConfig
         {
-            var configCategory = (ConfigCategory<T>)this.allConfig[typeof(T).Name];
+            var configCategory = (ACategory<T>)this.allConfig[typeof(T).Name];
             return configCategory.GetAll();
         }
 
-        public ConfigCategory<T> GetConfigCategory<T>() where T : IType
+        public T GetCategory<T>() where T : class, ICategory, new()
         {
-            return (ConfigCategory<T>) this.allConfig[typeof (T).Name];
+            T t = new T();
+            object category;
+            bool ret = this.allConfig.TryGetValue(t.Name, out category);
+            return ret? (T) category : null;
         }
     }
 }

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

@@ -0,0 +1,9 @@
+using System.ComponentModel;
+
+namespace Common.Config
+{
+    public interface ICategory : ISupportInitialize
+    {
+        string Name { get; }
+    }
+}

+ 7 - 0
CSharp/Platform/Common/Config/IConfig.cs

@@ -0,0 +1,7 @@
+namespace Common.Config
+{
+    public interface IConfig
+    {
+        int Id { get; }
+    }
+}

+ 0 - 7
CSharp/Platform/Common/Config/IType.cs

@@ -1,7 +0,0 @@
-namespace Model
-{
-    public interface IType
-    {
-        int Type { get; }
-    }
-}