hch
2025-09-18 2159b415a8d445a6602ff708c110bcbb4944a25a
Main/Config/ConfigBase.cs
@@ -7,84 +7,116 @@
public class ConfigBase<U, T> where T : ConfigBase<U, T>, new()
{
    public static Dictionary<U, T> dic = new Dictionary<U, T>();
    /// <summary>
    /// 是否访问过静态构造函数
    /// </summary>
    public static bool visit = false;
    static ConfigBase()
    {
        if (isInit)
        {
            return;
        }
        LazyInit();
    }
    private static Dictionary<U, T> m_dic = new Dictionary<U, T>();
    public static Dictionary<U, T> dic
    {
        get
        {
            return m_dic;
        }
    }
    public static bool isInit = false;
    public static T Get(U id)
    {
        if (!isInit)
        if (m_dic.ContainsKey(id))
        {
            Debug.LogError("ConfigBase 没有初始化");
            return null; // 或者抛出异常,视情况而定
        }
        if (dic.ContainsKey(id))
        {
            return dic[id];
            return m_dic[id];
        }
        return null;
    }
    public static void ForceInit()
    // public static void ForceRelease()
    // {
    //     m_dic.Clear();
    //     isInit = false;
    // }
    public static List<U> GetKeys()
    {
        ConfigManager.Instance.LoadConfigByType(typeof(T));
        List<U> result = new List<U>();
        result.AddRange(m_dic.Keys);
        return result;
    }
    public List<T> GetValues()
    public static List<T> GetValues()
    {
        if (!isInit)
        {
            Debug.LogError(typeof(T).Name + " 没有初始化 GetValues");
            return null; // 或者抛出异常,视情况而定
        }
        List<T> result = new List<T>();
        result.AddRange(dic.Values);
        result.AddRange(m_dic.Values);
        return result;
    }
    public static bool HasKey(U key)
    {
        return m_dic.ContainsKey(key);
    }
    public static void LazyInit()
    {
        if (!isInit)
        {
            Debug.LogError(typeof(T).Name + " 没有初始化 HasKey");
            return false; // 或者抛出异常,视情况而定
            //  实际上是同步的
            ConfigManager.Instance.LoadConfigByType(typeof(T));
        }
        return dic.ContainsKey(key);
    }
    public static void Init(string[] lines)
    {
        dic.Clear();
        for (int i = 3; i < lines.Length; i++)
        if (isInit)
        {
            string line = lines[i];
            var index = line.IndexOf("\t");
            if (index == -1)
            return;
        }
        for (int i = 3; i < lines.Length; i++)
            {
                continue;
                string line = lines[i];
                var index = line.IndexOf("\t");
                if (index == -1)
                {
                    continue;
                }
                string strKey = line.Substring(0, index);
                T config = new T();
                U key = config.LoadKey(strKey);
                config.LoadConfig(line);
                config.OnConfigParseCompleted();
#if UNITY_EDITOR
                try
                {
#endif
                    m_dic.Add(key, config);
#if UNITY_EDITOR
                }
                catch (ArgumentException exception)
                {
                    Debug.LogError(typeof(T).Name + " 重复的key " + key + " " + exception.Message);
                }
#endif
            }
            string strKey = line.Substring(0, index);
            T config = new T();
            U key = config.LoadKey(strKey);
            config.LoadConfig(line);
            #if UNITY_EDITOR
            try
            {
            #endif
            dic.Add(key, config);
            #if UNITY_EDITOR
            }
            catch (ArgumentException exception)
            {
                Debug.LogError(typeof(T).Name  + " 重复的key " + key + " " + exception.Message);
            }
            #endif
        foreach (var cfg in m_dic.Values)
        {
            cfg.AllConfigLoadFinish();
        }
        isInit = true;
@@ -95,10 +127,20 @@
        return default(U);
    }
    protected virtual void AllConfigLoadFinish()
    {
    }
    public virtual void LoadConfig(string line)
    {
        
    }
    protected virtual void OnConfigParseCompleted()
    {
    }
    
    protected int ParseInt(string str)
    {