-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyIniFile.cs
312 lines (282 loc) · 11.8 KB
/
MyIniFile.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System.Text;
namespace Resolutioner;
public class MyIniFile
{
public const string Filename = ".resolutioner.ini";
public static readonly string Home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
public static readonly string Filepath = Path.Combine(Home, Filename);
public FileStream FileStream;
public Config Conf;
public class Config
{
public int ConfMigration;
public decimal DesiredWidth;
public decimal DesiredHeight;
public decimal RestoreWidth;
public decimal RestoreHeight;
public bool OnLogin;
public bool OnLogoff;
public bool DontDoThings;
public bool OnSessionUnlock;
public bool OnSessionLock;
public bool OnSessionLogon;
public bool OnSessionLogoff;
public bool OnConsoleConnect;
public bool OnConsoleDisconnect;
public bool OnRemoteConnect;
public bool OnRemoteDisconnect;
public bool OnRemoteOn;
public bool OnRemoteOff;
public Config(
int confMigration,
decimal desiredWidth, decimal desiredHeight, decimal restoreWidth, decimal restoreHeight,
bool onLogin, bool onLogoff,
bool dontDoThings,
bool onSessionUnlock, bool onSessionLock,
bool onSessionLogon, bool onSessionLogoff,
bool onConsoleConnect, bool onConsoleDisconnect,
bool onRemoteConnect, bool onRemoteDisconnect,
bool onRemoteOn, bool onRemoteOff)
{
ConfMigration = confMigration;
DesiredWidth = desiredWidth;
DesiredHeight = desiredHeight;
RestoreWidth = restoreWidth;
RestoreHeight = restoreHeight;
OnLogin = onLogin;
OnLogoff = onLogoff;
DontDoThings = dontDoThings;
OnSessionUnlock = onSessionUnlock;
OnSessionLock = onSessionLock;
OnSessionLogon = onSessionLogon;
OnSessionLogoff = onSessionLogoff;
OnConsoleConnect = onConsoleConnect;
OnConsoleDisconnect = onConsoleDisconnect;
OnRemoteConnect = onRemoteConnect;
OnRemoteDisconnect = onRemoteDisconnect;
OnRemoteOn = onRemoteOn;
OnRemoteOff = onRemoteOff;
}
public Config(int confMigration)
{
ConfMigration = confMigration;
}
public Config()
{
ConfMigration = 2;
DesiredWidth = 0;
DesiredHeight = 0;
RestoreWidth = 0;
RestoreHeight = 0;
}
public Config(int confMigration, bool allBools)
{
ConfMigration = confMigration;
OnLogin = allBools;
OnLogoff = allBools;
DontDoThings = allBools;
OnSessionUnlock = allBools;
OnSessionLock = allBools;
OnSessionLogon = allBools;
OnConsoleConnect = allBools;
OnSessionLogoff = allBools;
OnConsoleDisconnect = allBools;
OnRemoteConnect = allBools;
OnRemoteDisconnect = allBools;
OnRemoteOn = allBools;
OnRemoteOff = allBools;
}
public class Fields
{
public const string OnLogin = "On login";
public const string OnLogoff = "On logoff";
public const string DontDoThings = "Don't do things.";
public const string OnSessionUnlock = "On session unlock";
public const string OnSessionLock = "On session lock";
public const string OnSessionLogon = "On session logon";
public const string OnSessionLogoff = "On session logoff";
public const string OnConsoleConnect = "On console connect";
public const string OnConsoleDisconnect = "On console disconnect";
public const string OnRemoteConnect = "On remote connect";
public const string OnRemoteDisconnect = "On remote disconnect";
public const string OnRemoteOn = "On remote control on";
public const string OnRemoteOff = "On remote control off";
}
public string BoolsToString()
{
StringBuilder sb = new();
const string format = "\n{0}";
if (OnLogin) sb.AppendFormat(format, Fields.OnLogin);
if (OnLogoff) sb.AppendFormat(format, Fields.OnLogoff);
if (DontDoThings) sb.AppendFormat(format, Fields.DontDoThings);
if (OnSessionUnlock) sb.AppendFormat(format, Fields.OnSessionUnlock);
if (OnSessionLock) sb.AppendFormat(format, Fields.OnSessionLock);
if (OnSessionLogon) sb.AppendFormat(format, Fields.OnSessionLogon);
if (OnSessionLogoff) sb.AppendFormat(format, Fields.OnSessionLogoff);
if (OnConsoleConnect) sb.AppendFormat(format, Fields.OnConsoleConnect);
if (OnConsoleDisconnect) sb.AppendFormat(format, Fields.OnConsoleDisconnect);
if (OnRemoteConnect) sb.AppendFormat(format, Fields.OnRemoteConnect);
if (OnRemoteDisconnect) sb.AppendFormat(format, Fields.OnRemoteDisconnect);
if (OnRemoteOn) sb.AppendFormat(format, Fields.OnRemoteOn);
if (OnRemoteOff) sb.AppendFormat(format, Fields.OnRemoteOff);
return sb.ToString();
}
}
public MyIniFile()
{
try
{
FileStream = File.Open(Filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (FileNotFoundException)
{
FileStream = File.Open(Filepath, FileMode.CreateNew, FileAccess.Write, FileShare.None);
}
try
{
Read();
}
catch (FileFormatException)
{
Conf = new Config(2, 0, 0, 0, 0,
false, false, false, true, false, false, false, false, false, false, false, false, false);
}
catch (ArgumentException)
{
Conf = new Config(-1);
throw;
}
if (Conf == null) throw new Exception("aaaaaa");
}
public void Read()
{
if (FileStream.Length != 0)
{
try
{
var streamReader = new StreamReader(FileStream);
var line = streamReader.ReadLine();
var confMigration = Int32.Parse(line ?? throw new InvalidOperationException());
if (confMigration == 2)
{
var builder = new Config(confMigration, false);
do
{
line = streamReader.ReadLine() ?? throw new Exception("ugh no");
var strings = line.Split("/");
switch (strings[0])
{
case "Resolutions":
for (int i = 1; i <= 4; i++)
{
var x = Int32.Parse(strings[i]);
switch (i)
{
case 1:
builder.DesiredWidth = x;
break;
case 2:
builder.DesiredHeight = x;
break;
case 3:
builder.RestoreWidth = x;
break;
case 4:
builder.RestoreHeight = x;
break;
}
}
break;
case Config.Fields.OnLogin:
builder.OnLogin = true;
break;
case Config.Fields.OnLogoff:
builder.OnLogoff = true;
break;
case Config.Fields.DontDoThings:
builder.DontDoThings = true;
break;
case Config.Fields.OnSessionUnlock:
builder.OnSessionUnlock = true;
break;
case Config.Fields.OnSessionLock:
builder.OnSessionLock = true;
break;
case Config.Fields.OnSessionLogon:
builder.OnSessionLogon = true;
break;
case Config.Fields.OnSessionLogoff:
builder.OnSessionLogoff = true;
break;
case Config.Fields.OnConsoleConnect:
builder.OnConsoleConnect = true;
break;
case Config.Fields.OnConsoleDisconnect:
builder.OnConsoleDisconnect = true;
break;
case Config.Fields.OnRemoteConnect:
builder.OnRemoteConnect = true;
break;
case Config.Fields.OnRemoteDisconnect:
builder.OnRemoteDisconnect = true;
break;
case Config.Fields.OnRemoteOn:
builder.OnRemoteOn = true;
break;
case Config.Fields.OnRemoteOff:
builder.OnRemoteOff = true;
break;
case "":
break;
default:
StringBuilder builder_here = new StringBuilder();
foreach (var s in strings)
builder_here.Append(s);
throw new ArgumentException(builder_here.ToString());
}
} while (streamReader.Peek() >= 0);
streamReader.Dispose();
// FileStream.Seek(0, SeekOrigin.Begin);
Conf = builder;
}
else
{
streamReader.Dispose();
// FileStream.Seek(0, SeekOrigin.Begin);
throw new ArgumentException();
}
}
catch (UnauthorizedAccessException)
{
throw new ArgumentException();
}
finally
{
FileStream = File.Open(Filepath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
}
else
{
throw new FileFormatException();
}
}
public void Write()
{
FileStream.Close();
FileStream = File.Open(Filepath, FileMode.Truncate, FileAccess.Write, FileShare.None);
StreamWriter streamWriter = new(FileStream);
streamWriter.WriteLine(Conf.ConfMigration);
streamWriter.WriteLine("Resolutions/{0}/{1}/{2}/{3}",
Conf.DesiredWidth, Conf.DesiredHeight, Conf.RestoreWidth, Conf.RestoreHeight);
streamWriter.Write(Conf.BoolsToString());
streamWriter.Flush();
streamWriter.Dispose();
// FileStream.Seek(0, SeekOrigin.Begin);
FileStream = File.Open(Filepath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
}
public void Write(Config what)
{
Conf = what;
Write();
}
}