-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.cs
204 lines (172 loc) · 6.06 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
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
namespace CoreRation
{
public class AppConfig : BaseConfig
{
public ObservableCollection<ProfileConfig> Profiles
{
get => profiles;
set => UpdateProperty(ref profiles, value);
}
private ObservableCollection<ProfileConfig> profiles;
}
public class ProfileConfig : BaseConfig
{
public string Name
{
get => name;
set => UpdateProperty(ref name, value);
}
[DefaultValue(500)]
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public int MonitorInterval
{
get => monitorInterval;
set
{
if(value < 1)
throw new ArgumentOutOfRangeException("value", "MonitorInterval must be a positive value.");
UpdateProperty(ref monitorInterval, value);
}
}
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public string OtherCores
{
get => otherCores;
set => UpdateProperty(ref otherCores, value);
}
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public ObservableCollection<ProcessConfig> Processes
{
get => processes;
set => UpdateProperty(ref processes, value);
}
private int monitorInterval = 500;
private string name;
private string otherCores;
private ObservableCollection<ProcessConfig> processes;
[JsonIgnore]
public long otherMask { get; set; }
}
public class ProcessConfig : BaseConfig
{
public string Name
{
get => name;
set => UpdateProperty(ref name, value);
}
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public string Cores
{
get => cores;
set => UpdateProperty(ref cores, value);
}
[DefaultValue(ProcessPriority.NoChange)]
[JsonConverter(typeof(StringEnumConverter))]
[JsonProperty(DefaultValueHandling=DefaultValueHandling.IgnoreAndPopulate)]
public ProcessPriority Priority
{
get => priority;
set => UpdateProperty(ref priority, value);
}
private string name;
private string cores;
private ProcessPriority priority;
}
public abstract class BaseConfig : INotifyPropertyChanged, INotifyPropertyChanging
{
protected void UpdateProperty<T>(ref T field, T value, [CallerMemberName] string name = null)
{
if(!EqualityComparer<T>.Default.Equals(field, value))
{
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(name));
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
}
public enum ProcessPriority
{
NoChange,
Low,
BelowNormal,
Normal,
AboveNormal,
High,
Realtime,
}
public static class ConfigUtil
{
private static readonly Regex RANGE_REGEX = new Regex(@"^\s*(\d+)(?:\s*-\s*(\d+))?\s*$");
public static long ParseMask(string def, int numCores)
{
var result = 0L;
if(string.IsNullOrWhiteSpace(def))
{
return 0L;
}
var start = 0;
while(start < def.Length)
{
var end = def.IndexOf(',', start);
if(end < 0) end = def.Length;
var match = RANGE_REGEX.Match(def, start, end - start);
if(!match.Success)
{
var part = def.Substring(start, end - start);
throw new Exception($"Failed to parse core specification: \"{part}\" is not a valid core number or range.");
}
var a = int.Parse(match.Groups[1].Value);
var b = a;
if(match.Groups[2].Success)
{
b = int.Parse(match.Groups[2].Value);
}
if(a > b)
{
var c = a;
a = b;
b = c;
}
if(b >= numCores)
{
var part = def.Substring(start, end - start);
throw new Exception($"Failed to parse core specification: {b} is out of range in core specification \"{part}\".");
}
for(var x = a; x <= b; x++)
{
result |= (1L << x);
}
start = end + 1;
}
return result;
}
}
public static class ProcessPriorityExt
{
public static ProcessPriorityClass? ToPriorityClass(this ProcessPriority priority)
{
switch(priority)
{
case ProcessPriority.NoChange: return null;
case ProcessPriority.Low: return ProcessPriorityClass.Idle;
case ProcessPriority.BelowNormal: return ProcessPriorityClass.BelowNormal;
case ProcessPriority.Normal: return ProcessPriorityClass.Normal;
case ProcessPriority.AboveNormal: return ProcessPriorityClass.AboveNormal;
case ProcessPriority.High: return ProcessPriorityClass.High;
case ProcessPriority.Realtime: return ProcessPriorityClass.RealTime;
default: throw new InvalidEnumArgumentException(nameof(priority), (int)priority, typeof(ProcessPriority));
}
}
}
}