/
Config.cs
244 lines (212 loc) · 9.2 KB
/
Config.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Linq;
using TrexMinerGUI.Forms;
using Newtonsoft.Json;
using System.Reflection;
namespace TrexMinerGUI
{
public class Config
{
private static readonly string InvalidMinerArgsMessage = "Invalid MinerArgs!";
private static readonly string InvalidDefaultProfileNameMessage = "Invalid DefaultProfileName!";
private static readonly string InvalidProfileNamesMessage = "Invalid Profile Names!";
private string _activeProfileName;
private static string ConfigFilePath { get => Program.ExecutionPath + "config.json"; }
public class Profile
{
public string Name { get; set; }
private string _minerArgs;
public string MinerArgs
{
get
{
return _minerArgs;
}
set
{
string[] fields = value.Split(" ");
if (
fields[0] == "t-rex" ||
(!fields.Contains("-a") && !fields.Contains("--algo")) ||
(!fields.Contains("-o") && !fields.Contains("--url"))
)
{
throw new ArgumentException(InvalidMinerArgsMessage);
}
_minerArgs = value;
}
}
public bool ApplyAfterburnerProfileOnMinerStart { get; set; }
public bool ApplyAfterburnerProfileOnMinerClose { get; set; }
public string ProfileToApplyOnMinerStart { get; set; }
public string ProfileToApplyOnMinerClose { get; set; }
}
public class EmailSetting
{
public bool SendMail { get; set; } = false;
public string Host { get; set; } = "smtp.gmail.com";
public int Port { get; set; } = 587;
public string UserName { get; set; }
public string Password { get; set; }
public string SendFrom { get; set; }
public string SendTo { get; set; }
public string Subject { get; set; } = "TrexMinerGUI: Error";
}
public bool StartMiningOnAppStart { get; set; }
public bool TryToCloseMSIAfterburnerIfItIsNotRunningAlready { get; set; }
public List<Profile> Profiles { get; set; }
[JsonIgnore]
public Profile ActiveProfile { get; private set; }
[JsonProperty]
public string ActiveProfileName
{
private get => _activeProfileName;
set
{
ActiveProfile = Profiles.Find(x => x.Name == value);
if (ActiveProfile == null) throw new ArgumentException(InvalidDefaultProfileNameMessage);
_activeProfileName = value;
}
}
[JsonIgnore]
public bool IsConfigHasBeenReset { get; private set; } = false;
public EmailSetting TheEmailSetting { get; set; }
public void SaveConfigToFile()
{
string TheFileContent = JsonConvert.SerializeObject(this, Formatting.Indented);
ExternalMethods.WriteAllTextWithBackup(ConfigFilePath, TheFileContent);
}
public bool DeleteProfile(Profile TheProfile)
{
if (TheProfile == ActiveProfile)
return false;
if (Profiles.Remove(TheProfile))
{
SaveConfigToFile();
return true;
}
else
{
return false;
}
}
private static Profile GenerateNewProfile(String ProfileName = null)
{
string NewProfileName = ProfileName != null ? ProfileName : "default";
return new Profile
{
// https://github.com/trexminer/T-Rex#examples
MinerArgs = "-a ethash -o stratum+tcp://eth.2miners.com:2020 -u 0x1f75eccd8fbddf057495b96669ac15f8e296c2cd -p x -w rigTrexTest",
Name = NewProfileName,
ApplyAfterburnerProfileOnMinerClose = false,
ApplyAfterburnerProfileOnMinerStart = false,
ProfileToApplyOnMinerClose = "1",
ProfileToApplyOnMinerStart = "1"
};
}
public int CreateProfile()
{
string NewProfileName = "NewProfile";
int prefix = 1;
while (Profiles.Find(x => x.Name == NewProfileName + prefix) != null)
{
prefix++;
}
Profiles.Add(GenerateNewProfile(NewProfileName + prefix));
SaveConfigToFile();
return Profiles.Count - 1;
}
public static Config LoadConfigFile()
{
try
{
if (File.Exists(Program.ExecutionPath + "trex_gui.conf"))
{
Config newConfig = new Config
{
Profiles = new List<Profile>
{
GenerateNewProfile()
}
};
newConfig.ActiveProfileName = newConfig.Profiles[0].Name;
var Lines = File.ReadAllLines(Program.ExecutionPath + "trex_gui.conf");
foreach (string Line in Lines)
{
string[] Sections = Line.Split("=");
switch (Sections[0])
{
case "MinerArgs":
newConfig.Profiles[0].MinerArgs = Sections[1];
break;
case "StartMiningOnAppStart":
newConfig.StartMiningOnAppStart = Sections[1] == "True";
break;
case "ApplyAfterburnerProfileOnMinerStart":
newConfig.Profiles[0].ApplyAfterburnerProfileOnMinerStart = Sections[1] == "True";
break;
case "ApplyAfterburnerProfileOnMinerClose":
newConfig.Profiles[0].ApplyAfterburnerProfileOnMinerClose = Sections[1] == "True";
break;
case "TryToCloseMSIAfterburnerIfItIsNotRunningAlready":
newConfig.TryToCloseMSIAfterburnerIfItIsNotRunningAlready = Sections[1] == "True";
break;
case "ProfileToApplyOnMinerStart":
newConfig.Profiles[0].ProfileToApplyOnMinerStart = Sections[1];
break;
case "ProfileToApplyOnMinerClose":
newConfig.Profiles[0].ProfileToApplyOnMinerClose = Sections[1];
break;
}
}
if (String.IsNullOrEmpty(newConfig.Profiles[0].MinerArgs) ||
String.IsNullOrEmpty(newConfig.Profiles[0].ProfileToApplyOnMinerStart) ||
String.IsNullOrEmpty(newConfig.Profiles[0].ProfileToApplyOnMinerClose))
{
throw new ArgumentException();
}
newConfig.SaveConfigToFile();
File.Delete(Program.ExecutionPath + "trex_gui.conf");
File.Delete(Program.ExecutionPath + "trex_gui.conf.backup");
return newConfig;
}
var JsonString = File.ReadAllText(ConfigFilePath);
var jsonSerializerSettings = new JsonSerializerSettings
{
CheckAdditionalContent = true,
MissingMemberHandling = MissingMemberHandling.Ignore
};
var LoadedConfig = JsonConvert.DeserializeObject<Config>(JsonString, jsonSerializerSettings);
if (LoadedConfig == null)
throw new FileNotFoundException();
if (LoadedConfig.Profiles.Select(s => s.Name).Distinct().Count() != LoadedConfig.Profiles.Select(s => s.Name).Count() ||
LoadedConfig.ActiveProfile == null)
throw new ArgumentException(InvalidProfileNamesMessage);
if (LoadedConfig.TheEmailSetting == null)
LoadedConfig.TheEmailSetting = new EmailSetting();
return LoadedConfig;
}
catch (FileNotFoundException)
{
List<Profile> NewProfileList = new List<Profile>
{
GenerateNewProfile()
};
return new Config
{
StartMiningOnAppStart = false,
TryToCloseMSIAfterburnerIfItIsNotRunningAlready = true,
Profiles = NewProfileList,
ActiveProfileName = NewProfileList[0].Name,
IsConfigHasBeenReset = true,
TheEmailSetting = new EmailSetting()
};
}
}
}
}