跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

AppConfig.cs

145 行 · 4.0 KB历史下载
1using Core.Event;
2using Core.Librarys;
3using Core.Models.Config;
4using Core.Servicers.Interfaces;
5using Newtonsoft.Json;
6using System;
7using System.Collections.Generic;
8using System.Diagnostics;
9using System.IO;
10using System.Linq;
11using System.Reflection;
12using System.Text;
13using System.Threading.Tasks;
14
15namespace Core.Servicers.Instances
16{
17 public class AppConfig : IAppConfig
18 {
19 private string fileName;
20 private ConfigModel config;
21
22 public event AppConfigEventHandler ConfigChanged;
23 private ConfigModel oldConfig;
24
25 public AppConfig()
26 {
27 fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
28 "Data",
29 "AppConfig.json");
30 }
31 public void Load()
32 {
33 string configText = string.Empty;
34 try
35 {
36 if (File.Exists(fileName))
37 {
38 configText = File.ReadAllText(fileName);
39 config = JsonConvert.DeserializeObject<ConfigModel>(configText);
40 }
41 else
42 {
43 // 创建基础配置
44 CreateDefaultConfig();
45
46 Save();
47 }
48
49 CopyToOldConfig();
50
51 CheckOption(config);
52 }
53 catch (JsonSerializationException ex)
54 {
55 HandleLoadFail(ex.Message, configText);
56 }
57 catch (JsonReaderException ex)
58 {
59 HandleLoadFail(ex.Message, configText);
60 }
61 catch (Exception ex)
62 {
63 Logger.Error(ex.Message);
64 }
65 }
66
67 private void CopyToOldConfig()
68 {
69 string configStr = JsonConvert.SerializeObject(config);
70 oldConfig = JsonConvert.DeserializeObject<ConfigModel>(configStr);
71 }
72
73 private void HandleLoadFail(string err, string configText)
74 {
75 // 创建基础配置
76 CreateDefaultConfig();
77
78 Save();
79
80 Logger.Error(err + "\r\nConfig content: " + configText);
81 }
82 private void CreateDefaultConfig()
83 {
84 config = new ConfigModel();
85 config.Links = new List<Models.Config.Link.LinkModel>();
86
87 config.General = new GeneralModel();
88 config.General.IsStartatboot = false;
89
90 config.Behavior = new BehaviorModel();
91 }
92
93 public ConfigModel GetConfig()
94 {
95 return config;
96 }
97
98
99 public void Save()
100 {
101 try
102 {
103 string dir = Path.GetDirectoryName(fileName);
104 if (!Directory.Exists(dir))
105 {
106 Directory.CreateDirectory(dir);
107 }
108
109 File.WriteAllText(fileName, JsonConvert.SerializeObject(config));
110
111 ConfigChanged?.Invoke(oldConfig ?? config, config);
112
113 CopyToOldConfig();
114 }
115 catch (Exception ex)
116 {
117 Logger.Error(ex.Message);
118 }
119 }
120
121 private void CheckOption(object obj)
122 {
123 System.Reflection.PropertyInfo[] properties = obj.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
124
125 foreach (System.Reflection.PropertyInfo item in properties)
126 {
127 string name = item.Name;
128 object value = item.GetValue(obj, null);
129 if (value == null)
130 {
131 //配置项不存在时创建
132 Type[] types = new Type[0];
133 object[] objs = new object[0];
134
135 ConstructorInfo ctor = item.PropertyType.GetConstructor(types);
136 if (ctor != null)
137 {
138 object instance = ctor.Invoke(objs);
139 item.SetValue(obj, instance);
140 }
141 }
142 }
143 }
144 }
145}