ET框架---ConfigComponent学习笔记

ConfigComponent学习笔记

请大家关注我的微博:@NormanLin_BadPixel坏像素


这是管理配置信息的组件,我们来看一下配置信息具体是什么。

private Dictionary<Type, ICategory> allConfig;

//////
public interface ICategory: ISupportInitialize
{
    Type ConfigType { get; }
}
//////
public interface ISupportInitialize {
    void BeginInit();
    void EndInit();
}

我们找找ICategory的引用。

ACategory

如作者注释的,这是管理该所有的配置。

protected Dictionary<long, T> dict;

public virtual void BeginInit()
{
    this.dict = new Dictionary<long, T>();

    string configStr = ConfigHelper.GetText(typeof (T).Name);

    foreach (string str in configStr.Split(new[] { "\n" }, StringSplitOptions.None))
    {
        try
        {
            string str2 = str.Trim();
            if (str2 == "")
            {
                continue;
            }
            T t = MongoHelper.FromJson<T>(str2);
            this.dict.Add(t.Id, t);
        }
        catch (Exception e)
        {
            throw new Exception($"parser json fail: {str}", e);
        }
    }
}

这里很好理解,通过ConfigHelper.GetText获得到T的配置信息。然后用string.Split分离每一个配置信息,最后,把每一个具体配置信息通过Json反序列化为T对象,储存。

我们以BuffConfig为例。

[Config(AppType.Client)]
public class BuffCategory: ACategory<BuffConfig>
{
}

我们看到,BuffCategory具体存放的配置信息TBuffConfig

public class BuffConfig: AConfig
{
    public string Name { get; set; }
    public int Duration { get; set; }

    public BuffConfig()
    {
    }

    public BuffConfig(long id): base(id)
    {
    }
}

我们看看buffConfig.txt

{ "_id" : 1, "Name": "加速buff", "Duration": 1000 }
{ "_id" : 2, "Name": "增加攻击力buff", "Duration": 1500 }

好理解吧。后面则是一些获取配置信息的各种方法。

ConfigComponent

public void Load()
{
    this.allConfig = new Dictionary<Type, ICategory>();
    Type[] types = DllHelper.GetMonoTypes();

    foreach (Type type in types)
    {
        object[] attrs = type.GetCustomAttributes(typeof (ConfigAttribute), false);
        if (attrs.Length == 0)
        {
            continue;
        }
        object obj = Activator.CreateInstance(type);

        ICategory iCategory = obj as ICategory;
        if (iCategory == null)
        {
            throw new Exception($"class: {type.Name} not inherit from ACategory");
        }
        iCategory.BeginInit();
        iCategory.EndInit();

        this.allConfig[iCategory.ConfigType] = iCategory;
    }
}

知道了ACategory,这里很好解释了,就是从程序集中获取到所有需要加载配置信息的类,然后根据类加载所有的配置信息并储存。后面的各种Get,大家自己理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值