-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
501 lines (440 loc) · 21.1 KB
/
MainForm.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using Microsoft.Win32;
using System.Data;
using System.IO;
namespace PlaytimeUpdater
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.updateButton = new System.Windows.Forms.Button();
this.fileSelectionGroup = new System.Windows.Forms.GroupBox();
this.selectFile = new System.Windows.Forms.Button();
this.filePathLabel = new Label();
this.SuspendLayout();
// updateButton
this.updateButton.Location = new System.Drawing.Point(100, 200);
this.updateButton.Name = "updateButton";
this.updateButton.Size = new System.Drawing.Size(200, 50);
this.updateButton.TabIndex = 0;
this.updateButton.Text = "Update Playtime";
this.updateButton.UseVisualStyleBackColor = true;
this.updateButton.Click += new System.EventHandler(this.UpdateButton_Click);
// filePathLabel
this.filePathLabel.Location = new System.Drawing.Point(10, 20);
this.filePathLabel.Name = "filePath";
this.filePathLabel.Text = "File Path: " + file_path;
Size textSize = TextRenderer.MeasureText(filePathLabel.Text, filePathLabel.Font);
this.filePathLabel.Size = new System.Drawing.Size(textSize.Width, textSize.Height);
// selectFile
this.selectFile.Location = new System.Drawing.Point(textSize.Width + 10, 20);
this.selectFile.Name = "selectFile";
this.selectFile.Size = new System.Drawing.Size(50, 30);
this.selectFile.TabIndex = 1;
this.selectFile.Text = "...";
this.selectFile.UseVisualStyleBackColor = true;
this.selectFile.Click += new System.EventHandler(this.SelectFile);
// fileSelectionGroup
this.fileSelectionGroup.Location = new System.Drawing.Point(100, 250);
this.fileSelectionGroup.Name = "fileSelectionGroup";
this.fileSelectionGroup.Size = new System.Drawing.Size(filePathLabel.Size.Width + selectFile.Size.Width + 30, selectFile.Size.Height + 30);
this.fileSelectionGroup.Controls.Add(this.filePathLabel);
this.fileSelectionGroup.Controls.Add(this.selectFile);
// MainForm
this.ClientSize = new System.Drawing.Size(this.fileSelectionGroup.Size.Width+10, 300);
this.Controls.Add(this.updateButton);
this.Controls.Add(this.fileSelectionGroup);
this.Name = "MainForm";
this.Text = "Playtime Updater";
this.ResumeLayout(false);
// Initialize group boxes
InitializeGroupBoxes();
UpdateRadioButtonsAndGroups();
// Set visibility and position of group boxes based on gameList
SetGroupBoxVisibilityAndPosition();
GetPlaytimes();
}
private void InitializeGroupBoxes()
{
genshinGroup = CreateGroupBox("Genshin Impact", genshin_registry_path, file_path, 0, out genshinRegistryRadioButton, out genshinTextRadioButton);
this.Controls.Add(genshinGroup);
starrailGroup = CreateGroupBox("Star Rail", starrail_registry_path, file_path, 1, out starrailRegistryRadioButton, out starrailTextRadioButton);
this.Controls.Add(starrailGroup);
zzzGroup = CreateGroupBox("Zenless Zone Zero", zzz_registry_path, file_path, 2, out zzzRegistryRadioButton, out zzzTextRadioButton);
this.Controls.Add(zzzGroup);
honkaiGroup = CreateGroupBox("Honkai Impact 3rd", honkai_registry_path, file_path, 3, out honkaiRegistryRadioButton, out honkaiTextRadioButton);
this.Controls.Add(honkaiGroup);
}
private GroupBox CreateGroupBox(string gameName, string registryPath, string filePath, int line, out RadioButton registryRadioButton, out RadioButton textRadioButton)
{
GroupBox groupBox = new GroupBox();
groupBox.Name = gameName;
groupBox.Text = gameName;
groupBox.Size = new Size(250, 110);
registryRadioButton = new RadioButton();
registryRadioButton.Location = new Point(10, 30);
// registryRadioButton.Text = RegistryHelper.GetRegistryPlaytimeDisplay(registryPath);
// Size registryTextSize = MeasureTextSize(registryRadioButton.Text, registryRadioButton.Font);
// registryRadioButton.Size = new Size(registryTextSize.Width + 100, registryTextSize.Height + 10);
registryRadioButton.UseVisualStyleBackColor = true;
registryRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
textRadioButton = new RadioButton();
textRadioButton.Location = new Point(10, 70);
// textRadioButton.Text = FileHelper.GetTextFilePlaytimeDisplay(line, filePath);
// Size textSize = MeasureTextSize(textRadioButton.Text, textRadioButton.Font);
// textRadioButton.Size = new Size(textSize.Width + 100, textSize.Height + 10);
textRadioButton.UseVisualStyleBackColor = true;
textRadioButton.CheckedChanged += new EventHandler(RadioButton_CheckedChanged);
// if (registryTextSize.Width > textSize.Width)
// {
// groupBox.Size = new Size(registryRadioButton.Size.Width + 120, 110);
// }
// else
// {
// groupBox.Size = new Size(textRadioButton.Size.Width + 120, 110);
// }
groupBox.Controls.Add(registryRadioButton);
groupBox.Controls.Add(textRadioButton);
return groupBox;
}
private void SetGroupBoxVisibilityAndPosition()
{
int yOffset = 50; // Initial Y offset
int ySpacing = 110; // Spacing between group boxes
// Set visibility and position for Genshin Impact
if (gameList.Any(game => game.Item1 == "Genshin Impact"))
{
genshinGroup.Visible = true;
genshinGroup.Location = new System.Drawing.Point(100, yOffset);
yOffset += ySpacing;
}
else
{
genshinGroup.Visible = false;
}
// Set visibility and position for Star Rail
if (gameList.Any(game => game.Item1 == "Star Rail"))
{
starrailGroup.Visible = true;
starrailGroup.Location = new System.Drawing.Point(100, yOffset);
yOffset += ySpacing;
}
else
{
starrailGroup.Visible = false;
}
// Set visibility and position for Zenless Zone Zero
if (gameList.Any(game => game.Item1 == "Zenless Zone Zero"))
{
zzzGroup.Visible = true;
zzzGroup.Location = new System.Drawing.Point(100, yOffset);
yOffset += ySpacing;
}
else
{
zzzGroup.Visible = false;
}
// Set visibility and position for Honkai Impact 3rd
if (gameList.Any(game => game.Item1 == "Honkai Impact 3rd"))
{
honkaiGroup.Visible = true;
honkaiGroup.Location = new System.Drawing.Point(100, yOffset);
yOffset += ySpacing;
}
else
{
honkaiGroup.Visible = false;
}
UpdateRadioButtonsAndGroups();
// Set position for Update Button
updateButton.Location = new System.Drawing.Point(100, yOffset+10);
yOffset += updateButton.Height + 10;
// Set position for File Selection Group
fileSelectionGroup.Location = new System.Drawing.Point(100, yOffset);
// Set form height based on number of visible group boxes
this.ClientSize = new System.Drawing.Size(fileSelectionGroup.Size.Width+200, yOffset + 100);
}
void RadioButton_CheckedChanged(object sender, EventArgs e)
{
string selected = "";
if (sender is not RadioButton rb)
{
MessageBox.Show("Sender is not a RadioButton");
return;
}
// Ensure that the RadioButton.Checked property
// changed to true.
if (rb.Checked)
{
string rbtext = rb.Text.Split(' ')[0];
if (rbtext == "Registry")
{
selected = "R";
}
if (rbtext == "File")
{
selected = "T";
}
if (rb.Parent.Name == "Genshin Impact")
{
genshin_selected = selected;
}
else if (rb.Parent.Name == "Star Rail")
{
starrail_selected = selected;
}
else if (rb.Parent.Name == "Zenless Zone Zero")
{
zzz_selected = selected;
}
else if (rb.Parent.Name == "Honkai Impact 3rd")
{
honkai_selected = selected;
}
}
}
private void UpdateButton_Click(object sender, EventArgs e)
{
if (genshin_selected == "R")
{
FileHelper.SavePlaytimeToTextFile(genshin_registry_playtime, 0, file_path);
}
else if (genshin_selected == "T")
{
RegistryHelper.SavePlaytimeToRegistry(genshin_registry_path, genshin_text_playtime);
}
if (starrail_selected == "R")
{
FileHelper.SavePlaytimeToTextFile(starrail_registry_playtime, 1, file_path);
}
else if (starrail_selected == "T")
{
RegistryHelper.SavePlaytimeToRegistry(starrail_registry_path, starrail_text_playtime);
}
if (zzz_selected == "R")
{
FileHelper.SavePlaytimeToTextFile(zzz_registry_playtime, 2, file_path);
}
else if (zzz_selected == "T")
{
RegistryHelper.SavePlaytimeToRegistry(zzz_registry_path, zzz_text_playtime);
}
if (honkai_selected == "R")
{
FileHelper.SavePlaytimeToTextFile(honkai_registry_playtime, 3, file_path);
}
else if (honkai_selected == "T")
{
RegistryHelper.SavePlaytimeToRegistry(honkai_registry_path, honkai_text_playtime);
}
UpdateRadioButtonsAndGroups();
GetPlaytimes();
}
public void UpdateRadioButtonsAndGroups()
{
foreach (var group in this.Controls.OfType<GroupBox>())
{
if (group.Controls.Count < 2) continue; // Ensure there are at least two controls (radio buttons) in the group
RadioButton registryRadioButton = group.Controls[0] as RadioButton;
RadioButton textRadioButton = group.Controls[1] as RadioButton;
if (registryRadioButton == null || textRadioButton == null) continue; // Ensure the controls are radio buttons
// Update the text of the radio buttons
if (group.Name == "Genshin Impact" && gameList.Any(game => game.Item1 == "Genshin Impact"))
{
registryRadioButton.Text = RegistryHelper.GetRegistryPlaytimeDisplay(genshin_registry_path);
textRadioButton.Text = FileHelper.GetTextFilePlaytimeDisplay(0, file_path);
}
else if (group.Name == "Star Rail" && gameList.Any(game => game.Item1 == "Star Rail"))
{
registryRadioButton.Text = RegistryHelper.GetRegistryPlaytimeDisplay(starrail_registry_path);
textRadioButton.Text = FileHelper.GetTextFilePlaytimeDisplay(1, file_path);
}
else if (group.Name == "Zenless Zone Zero" && gameList.Any(game => game.Item1 == "Zenless Zone Zero"))
{
registryRadioButton.Text = RegistryHelper.GetRegistryPlaytimeDisplay(zzz_registry_path);
textRadioButton.Text = FileHelper.GetTextFilePlaytimeDisplay(2, file_path);
}
else if (group.Name == "Honkai Impact 3rd" && gameList.Any(game => game.Item1 == "Honkai Impact 3rd"))
{
registryRadioButton.Text = RegistryHelper.GetRegistryPlaytimeDisplay(honkai_registry_path);
textRadioButton.Text = FileHelper.GetTextFilePlaytimeDisplay(3, file_path);
}
if (gameList.Any(game => game.Item1 == group.Text))
{
int registryPlaytime = RegistryHelper.ReadPlaytimeFromRegistry(gameList.First(game => game.Item1 == group.Text).Item2);
int textPlaytime = FileHelper.ReadPlaytimeFromTextFile(gameList.First(game => game.Item1 == group.Text).Item3, file_path);
// Update the colors based on playtime comparison
if (registryPlaytime > textPlaytime)
{
registryRadioButton.ForeColor = Color.Green;
textRadioButton.ForeColor = Color.Black;
}
else if (registryPlaytime < textPlaytime)
{
textRadioButton.ForeColor = Color.Green;
registryRadioButton.ForeColor = Color.Black;
}
else
{
registryRadioButton.ForeColor = Color.Black;
textRadioButton.ForeColor = Color.Black;
}
}
// Measure the text size and update the size of the radio buttons
Size registryTextSize = MeasureTextSize(registryRadioButton.Text, registryRadioButton.Font);
Size textTextSize = MeasureTextSize(textRadioButton.Text, textRadioButton.Font);
registryRadioButton.Size = new Size(registryTextSize.Width + 100, registryTextSize.Height + 10);
textRadioButton.Size = new Size(textTextSize.Width + 100, textTextSize.Height + 10);
// Update the size of the group box based on the larger radio button
int maxWidth = Math.Max(registryTextSize.Width, textTextSize.Width);
group.Size = new Size(maxWidth + 120, group.Height);
}
}
public static List<(string, string, int)> CreateGameList()
{
List<(string, string, int)> gameList = new();
if (RegistryHelper.CheckRegistryEntry(genshin_registry_path))
{
gameList.Add(("Genshin Impact", genshin_registry_path, 0));
Console.WriteLine("Genshin Impact found");
}
if (RegistryHelper.CheckRegistryEntry(starrail_registry_path))
{
gameList.Add(("Star Rail", starrail_registry_path, 1));
Console.WriteLine("Star Rail found");
}
if (RegistryHelper.CheckRegistryEntry(zzz_registry_path))
{
gameList.Add(("Zenless Zone Zero", zzz_registry_path, 2));
Console.WriteLine("Zenless Zone Zero found");
}
if (RegistryHelper.CheckRegistryEntry(honkai_registry_path))
{
gameList.Add(("Honkai Impact 3rd", honkai_registry_path, 3));
Console.WriteLine("Honkai Impact 3rd found");
}
return gameList;
}
private void SelectFile(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = file_path;
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
file_path = openFileDialog.FileName;
SaveFilePath(file_path);
filePathLabel.Text = "File Path: " + file_path;
filePathLabel.Size = TextRenderer.MeasureText(filePathLabel.Text, filePathLabel.Font);
selectFile.Location = new Point(filePathLabel.Size.Width + 10, 20);
fileSelectionGroup.Size = new Size(filePathLabel.Size.Width + selectFile.Size.Width + 30, selectFile.Size.Height + 30);
SetGroupBoxVisibilityAndPosition();
GetPlaytimes();
UpdateRadioButtonsAndGroups();
}
}
private void SaveFilePath(string filePath)
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(RegistryKeyPath);
key.SetValue(RegistryValueName, filePath);
key.Close();
}
public static string GetFilePath()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(RegistryKeyPath);
if (key != null)
{
string filePath = key.GetValue(RegistryValueName).ToString();
key.Close();
if (!File.Exists(filePath))
{
return default_file_path;
}
return filePath;
}
return default_file_path;
}
public static void GetPlaytimes()
{
foreach (var game in gameList)
{
if (game.Item1 == "Genshin Impact")
{
genshin_registry_playtime = RegistryHelper.ReadPlaytimeFromRegistry(genshin_registry_path);
genshin_text_playtime = FileHelper.ReadPlaytimeFromTextFile(0, file_path);
}
else if (game.Item1 == "Star Rail")
{
starrail_registry_playtime = RegistryHelper.ReadPlaytimeFromRegistry(starrail_registry_path);
starrail_text_playtime = FileHelper.ReadPlaytimeFromTextFile(1, file_path);
}
else if (game.Item1 == "Zenless Zone Zero")
{
zzz_registry_playtime = RegistryHelper.ReadPlaytimeFromRegistry(zzz_registry_path);
zzz_text_playtime = FileHelper.ReadPlaytimeFromTextFile(2, file_path);
}
else if (game.Item1 == "Honkai Impact 3rd")
{
honkai_registry_playtime = RegistryHelper.ReadPlaytimeFromRegistry(honkai_registry_path);
honkai_text_playtime = FileHelper.ReadPlaytimeFromTextFile(3, file_path);
}
}
}
private Size MeasureTextSize(string text, Font font)
{
using (Graphics g = this.CreateGraphics())
{
return g.MeasureString(text, font).ToSize();
}
}
private System.Windows.Forms.GroupBox genshinGroup;
private System.Windows.Forms.RadioButton genshinRegistryRadioButton;
private System.Windows.Forms.RadioButton genshinTextRadioButton;
private System.Windows.Forms.GroupBox starrailGroup;
private System.Windows.Forms.RadioButton starrailRegistryRadioButton;
private System.Windows.Forms.RadioButton starrailTextRadioButton;
private System.Windows.Forms.GroupBox zzzGroup;
private System.Windows.Forms.RadioButton zzzRegistryRadioButton;
private System.Windows.Forms.RadioButton zzzTextRadioButton;
private System.Windows.Forms.GroupBox honkaiGroup;
private System.Windows.Forms.RadioButton honkaiRegistryRadioButton;
private System.Windows.Forms.RadioButton honkaiTextRadioButton;
private System.Windows.Forms.Button updateButton;
private System.Windows.Forms.GroupBox fileSelectionGroup;
private System.Windows.Forms.Button selectFile;
private System.Windows.Forms.Label filePathLabel;
private const string RegistryKeyPath = @"SOFTWARE\PlaytimeUpdater";
private const string RegistryValueName = "FilePath";
public static string genshin_registry_path = @"Software\miHoYo\Genshin Impact";
public static string starrail_registry_path = @"Software\Cognosphere\Star Rail";
public static string zzz_registry_path = @"Software\miHoYo\ZenlessZoneZero";
public static string honkai_registry_path = @"Software\miHoYo\Honkai Impact 3rd";
public static string default_text_folder_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\PlaytimeUpdater\";
public static string default_file_path = default_text_folder_path + "playtime.txt";
public static string file_path = GetFilePath();
public static List<(string, string, int)> gameList = CreateGameList();
public static int genshin_registry_playtime;
public static int genshin_text_playtime;
public static int starrail_registry_playtime;
public static int starrail_text_playtime;
public static int zzz_registry_playtime;
public static int zzz_text_playtime;
public static int honkai_registry_playtime;
public static int honkai_text_playtime;
private string genshin_selected="";
private string starrail_selected="";
private string zzz_selected="";
private string honkai_selected="";
}
}