| 1 | using Core.Event; |
| 2 | using Core.Librarys; |
| 3 | using Core.Models.Config; |
| 4 | using Core.Servicers.Interfaces; |
| 5 | using Newtonsoft.Json; |
| 6 | using System; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Diagnostics; |
| 9 | using System.IO; |
| 10 | using System.Linq; |
| 11 | using System.Reflection; |
| 12 | using System.Text; |
| 13 | using System.Threading.Tasks; |
| 14 | |
| 15 | namespace 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 | } |