yyl
2025-06-17 007fbd542c30f5fa8308128aac26ce6584b3067a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.IO;
using System;
 
 
public class ConfigBase<U, T> where T : ConfigBase<U, T>, new()
{
    public static Dictionary<U, T> dic = new Dictionary<U, T>();
 
    public static bool isInit = false;
 
    public static T Get(U id)
    {
        if (!isInit)
        {
            Debug.LogError("ConfigBase 没有初始化");
            return null; // 或者抛出异常,视情况而定
        }
 
        if (dic.ContainsKey(id))
        {
            return dic[id];
        }
 
        return null;
    }
 
    public static void ForceInit()
    {
        ConfigManager.Instance.LoadConfigByType(typeof(T));
    }
 
    public static List<U> GetKeys()
    {
        if (!isInit)
        {
            Debug.LogError(typeof(U).Name + " 没有初始化 GetKeys");
            return null; // 或者抛出异常,视情况而定
        }
        List<U> result = new List<U>();
        result.AddRange(dic.Keys);
        return result;
    }
 
 
    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);
        return result;
    }
 
    public static bool HasKey(U key)
    {
        if (!isInit)
        {
            Debug.LogError(typeof(T).Name + " 没有初始化 HasKey");
            return false; // 或者抛出异常,视情况而定
        }
 
        return dic.ContainsKey(key);
    }
 
    public static void Init(string[] lines)
    {
        dic.Clear();
        
        for (int i = 3; i < lines.Length; i++)
        {
            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
            dic.Add(key, config);
            #if UNITY_EDITOR
            }
            catch (ArgumentException exception)
            {
                Debug.LogError(typeof(T).Name  + " 重复的key " + key + " " + exception.Message);
            }
            #endif
        }
 
        foreach (var cfg in dic.Values)
        {
            cfg.AllConfigLoadFinish();
        }
 
        isInit = true;
    }
 
    public virtual U LoadKey(string line)
    {
        return default(U);
    }
 
    protected virtual void AllConfigLoadFinish()
    {
        
    }
 
    public virtual void LoadConfig(string line)
    {
        
    }
 
    protected virtual void OnConfigParseCompleted()
    {
        
    }
    
    protected int ParseInt(string str)
    {
        int result = 0;
        int.TryParse(str, out result);
        return result;
    }
 
    protected float ParseFloat(string str)
    {
        float result = 0f;
        float.TryParse(str, out result);
        return result;
    }
 
    protected string[] Split(string str, char split)
    {
        return str.Split(split);
    }
 
    protected List<string> ParseStrList(string str, char split)
    {
        List<string> result = new List<string>();
        string[] strs = Split(str, split);
        for (int i = 0; i < strs.Length; i++)
        {
            result.Add(strs[i]);
        }
 
        return result;
    }
 
    protected List<int> ParseIntList(string str, char split)
    {
        List<int> result = new List<int>();
        string[] strs = Split(str, split);
        for (int i = 0; i < strs.Length; i++)
        {
            result.Add(ParseInt(strs[i]));
        }
        return result;
    }
 
    protected U GetKey(string key)
    {
        if (typeof(U) == typeof(string))
        {
            return (U)(object)key;
        }
        else if (typeof(U) == typeof(int))
        {
            return (U)(object)ParseInt(key);
        }
        else
        {
            Debug.LogError("GetKey 意外的key类型 类型错误 " + typeof(U).Name);
            return default(U);
        }
    }
}