提交 a3d0b31a 编写于 作者: T tanghai

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

上级 d2d40eb4
......@@ -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)
......
......@@ -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)
{
......
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>
{
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
{
public enum NodeType
{
Select = 1,
Selector = 1,
Sequence = 2,
Not = 10,
}
......
namespace BehaviorTree
{
[NodeAttribute(NodeType.Select, typeof(Selector))]
[NodeAttribute(NodeType.Selector, typeof(Selector))]
public class Selector: Node
{
public Selector(NodeConfig config): base(config)
......
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>
{
}
}
\ No newline at end of file
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
......
......@@ -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" />
......
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();
}
}
}
\ No newline at end of file
using System;
namespace Model
namespace Common.Config
{
[AttributeUsage(AttributeTargets.Class)]
public class ConfigAttribute: Attribute
......
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;
}
}
}
\ No newline at end of file
using System.ComponentModel;
namespace Common.Config
{
public interface ICategory : ISupportInitialize
{
string Name { get; }
}
}
namespace Common.Config
{
public interface IConfig
{
int Id { get; }
}
}
\ No newline at end of file
namespace Model
{
public interface IType
{
int Type { get; }
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册