-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConfiguration.cs
More file actions
87 lines (75 loc) · 2.31 KB
/
Configuration.cs
File metadata and controls
87 lines (75 loc) · 2.31 KB
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace EthminerGUI
{
public class Configuration
{
string localMachineName;
public string LocalMachineName
{
get => localMachineName;
set
{
localMachineName = Regex.Replace(value, @"[^a-zA-Z0-9-_]", string.Empty).Trim();
}
}
public int SelectedIndex { get; set; }
Miner[] miners;
public Miner CurrentMiner
{
get => miners[SelectedIndex];
set => miners[SelectedIndex] = value;
}
string path;
public Configuration(string path)
{
this.path = path;
if (File.Exists(path))
{
var json = File.ReadAllText(path);
var jobj = JObject.Parse(json);
LocalMachineName = (string)jobj["machineName"];
SelectedIndex = (int)jobj["index"];
var miners = (JArray)jobj["miners"];
this.miners = new Miner[miners.Count];
for (int i = 0; i < miners.Count; i++)
{
this.miners[i] = JsonConvert.DeserializeObject<Miner>(miners[i].ToString());
}
}
else
{
LocalMachineName = Environment.MachineName;
SelectedIndex = 0;
miners = new Miner[1];
miners[0] = new Miner();
Save();
}
}
public string[] GetMinerNames()
{
string[] names = new string[miners.Length];
for (int i = 0; i < miners.Length; i++)
{
names[i] = miners[i].name.ToString();
}
return names;
}
public void Save()
{
var jobj = new JObject();
jobj["machineName"] = LocalMachineName;
jobj["index"] = SelectedIndex;
var miners = new JArray();
foreach (var miner in this.miners)
{
miners.Add(JObject.Parse(JsonConvert.SerializeObject(miner)));
}
jobj["miners"] = miners;
File.WriteAllText(path, jobj.ToString());
}
}
}