| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace Base
- {
- /// <summary>
- /// 管理该所有的配置
- /// </summary>
- /// <typeparam name="T"></typeparam>
- public abstract class ACategory<T>: ICategory where T : AConfig
- {
- protected Dictionary<long, T> dict;
- public virtual void BeginInit()
- {
- this.dict = new Dictionary<long, T>();
- string path = $@"Config/{typeof (T).Name}";
- string configStr;
- try
- {
- configStr = File.ReadAllText(path);
- }
- catch (Exception)
- {
- throw new Exception($"load config file fail, path: {path}");
- }
- foreach (string str in configStr.Split(new[] { "\r\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);
- }
- }
- }
- public Type ConfigType
- {
- get
- {
- return typeof (T);
- }
- }
- public virtual void EndInit()
- {
- }
- public T this[long type]
- {
- get
- {
- T t;
- if (!this.dict.TryGetValue(type, out t))
- {
- throw new Exception($"{typeof (T)} 没有找到配置, key: {type}");
- }
- return t;
- }
- }
- public T TryGet(int type)
- {
- T t;
- if (!this.dict.TryGetValue(type, out t))
- {
- return null;
- }
- return t;
- }
- public T[] GetAll()
- {
- return this.dict.Values.ToArray();
- }
- public T GetOne()
- {
- return this.dict.Values.First();
- }
- }
- }
|