tangxu
2024-10-22 6a07c4c846ffbb1e93afdf0260e123e4c145f419
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DPumpHydr.WinFrmUI.WenSkin.DLL;
using DPumpHydr.WinFrmUI.WenSkin.Json;
using DPumpHydr.WinFrmUI.WenSkin.Json.Linq;
 
namespace DPumpHydr.WinFrmUI.WenSkin.WenJsonConfig
{
    public class JsonConfig
    {
        public JsonConfig() : this("WenAutoConfig")
        {
        }
        public JsonConfig(string jsonFileName)
        {
            Path = Application.StartupPath;
            JsonFileName = jsonFileName;
            Load();
        }
        
        public JsonConfig(string path,string jsonFileName)
        {
            this.Path = path;
            JsonFileName = jsonFileName;
            Load();
        }
 
        #region 私有属性
        private JsonConfigGroupList groups;
        #endregion
 
        #region 公有属性
        public string JsonFileName { get;private set; }
        public string Path { get;private set; }
        public JsonConfigGroupList Groups => groups ??= new JsonConfigGroupList();
        public JsonConfigGroup this[string str] => Groups[str];
        public string this[string group, string val] { get => Groups[group, val]; set => Groups[group, val] = value; }
        #endregion
 
        //打开配置文件
        public void Load()
        {
            string path = $"{this.Path}\\{JsonFileName}.json";
            Load(path);
        }
        public void Load(string path)
        {
            if (!File.Exists(path))
                Save();
            string str = File.ReadAllText(path);
            groups = JsonConvert.DeserializeObject<JsonConfigGroupList>(str);
 
            //将加密数据解密
            Enc(groups);
 
            void Enc(JsonConfigGroupList groups)
            {
                foreach (var g in groups)
                {
                    if (g.Groups.Count > 0)
                        Enc(g.Groups);
                    foreach (var item in g.Items)
                    {
                        if (item.Ecc)
                        {
                            item.Value = item.Value?.ToAESDecrypt();
                        }
                    }
                }
            }
        }
        //保存文件
        public void Save()
        {
            string path = $"{this.Path}\\{JsonFileName}.json";
            Save(path);
        }
        public void Save(string path)
        {
            var js = new JsonConfigGroupList();
 
            //加密存储数据
            OutData(this.Groups, js);
 
            static void OutData(JsonConfigGroupList groups, JsonConfigGroupList owner)
            {
                foreach (var g in groups)
                {
                    JsonConfigGroup jsonConfigGroup = new JsonConfigGroup(owner)
                    {
                        Name = g.Name,
                        AllowAdd = g.AllowAdd,
                        Info = g.Info,
                        Visible = g.Visible,
                    };
                    owner.Add(jsonConfigGroup);
 
                    foreach (var item in g.Items)
                    {
                        JsonConfigItem jsonConfigItem = new JsonConfigItem(jsonConfigGroup.Items)
                        {
                            ComboBox = item.ComboBox,
                            Delete = item.Delete,
                            Info = item.Info,
                            Name = item.Name,
                            ReadOnly = item.ReadOnly,
                            Visible = item.Visible,
                            Tip = item.Tip,
                            Ecc = item.Ecc,
                        };
                        jsonConfigItem.Items.AddRange(item.Items.ToArray());
 
                        if (item.Ecc)
                        {
                            jsonConfigItem.Value = item.Value?.ToAESEncrypt();
                        }
                        else
                            jsonConfigItem.Value = item.Value;
                        jsonConfigGroup.Items.Add(jsonConfigItem);
                    }
 
                    if (g.Groups.Count > 0)
                        OutData(g.Groups, jsonConfigGroup.Groups);
                }
            }
 
            var json = JsonConvert.SerializeObject(js, Formatting.Indented);
 
            if (!Directory.Exists(System.IO.Path.GetDirectoryName(path)))
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(path));
            File.WriteAllText(path, json);
        }
 
        public void Show()
        {
            new WenConfigForm(this).Show();
        } 
        public void ShowDialog()
        {
            new WenConfigForm(this).ShowDialog();
        }
 
        public class JsonConfigGroupList : List<JsonConfigGroup>
        {
            public JsonConfigGroup this[string str] => Find(a => a.Name.ToUpper() == str.ToUpper());
 
            public string this[string group, string val]
            {
                get => Find(a => a.Name.ToUpper() == group.ToUpper())?.Items.Find(a => a.Name.ToUpper() == val.ToUpper())?.Value;
                set
                {
                    if (Find(a => a.Name.ToUpper() == group.ToUpper()) is JsonConfigGroup item && item.Items.Find(a => a.Name.ToUpper() == val.ToUpper()) is JsonConfigItem im)
                    {
                        im.Value = value;
                    }                 
                }
            }
 
            /// <summary>
            /// 新增组
            /// </summary>
            /// <param name="groupName">名称</param>
            /// <param name="info">描述</param>
            /// <param name="add">若存在是否继续新增</param>
            /// <returns></returns>
            public JsonConfigGroup Add(string groupName, string info = "组", bool add = false)
            {
                if (Find(a => a.Name == groupName) is JsonConfigGroup group && !add)
                {
                    return group;
                }
                else
                {
                    group = new JsonConfigGroup(this)
                    {
                        Name = groupName,
                        Info = info
                    };
                    Add(group);
                    return group;
                }
            }
        }
 
        public class JsonConfigGroup
        {
            private JsonConfigGroupList groups;
            private JsonConfigItemList items;
            private readonly JsonConfigGroupList owner;
 
            public JsonConfigGroup(JsonConfigGroupList owner)
            {
                this.owner = owner;
                Name = "";
                Info = "";
                AllowAdd = false;
                Visible = true;
            }
 
            public string Name { get; set; }
            public string Info { get; set; }
            public bool AllowAdd { get; set; }
            public bool Visible { get; set; }
            public JsonConfigItemList Items => items ??= new JsonConfigItemList();
            public JsonConfigGroupList Groups => groups ??= new JsonConfigGroupList();
 
            public void Remove()
            {
                owner.Remove(this);                
            }
        }
 
        public class JsonConfigItem
        {
            private List<string> items;
            private readonly JsonConfigItemList owner;
 
            public JsonConfigItem(JsonConfigItemList owner)
            {
                this.owner = owner;
                Name = "";
                Info = "";
                Ecc = false;
                ReadOnly = false;
                Delete = false;
                Value = "";
                Tip = "";
                Visible = true;
            }
 
            public string Name { get; set; }
            public string Info { get; set; }
            public bool Ecc { get; set; }
            public bool ReadOnly { get; set; }
            public bool Delete { get; set; }
            public string Value { get; set; }
            public string Tip { get; set; }
            public bool Visible { get; set; }
            public bool ComboBox { get; set; }
            public List<string> Items => items ??= new List<string>();
            public void Remove()
            {
                owner.Remove(this);
            }
        }
 
        public class JsonConfigItemList : List<JsonConfigItem>
        {
            public JsonConfigItemList()
            {
            }
            public JsonConfigItem this[string str] => Find(a => a.Name.ToUpper() == str.ToUpper());
 
            public JsonConfigItem Add(string itemName,string value,string info="项",bool ecc=false,bool readOnly=false)
            {
                JsonConfigItem item = new JsonConfigItem(this)
                {
                    Name = itemName,
                    Info = info,
                    Ecc = ecc,
                    ReadOnly = readOnly,
                    Value = value,
                };
                this.Add(item);
                return item;
            }
        }
    }
}