-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
393 lines (324 loc) · 13.6 KB
/
MainForm.cs
File metadata and controls
393 lines (324 loc) · 13.6 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
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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Win32;
using Newtonsoft.Json;
namespace o_manager
{
public partial class MainForm : Form
{
string osuPath;
string otdSettingsPath;
string otdExePath;
string currentWindowsUser;
string configsRoot;
string backupRoot;
string indexFile;
ConfigIndex configIndex;
public MainForm()
{
InitializeComponent();
configsRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs");
backupRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Backups");
indexFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "configs.json");
Directory.CreateDirectory(configsRoot);
Directory.CreateDirectory(backupRoot);
currentWindowsUser = Environment.UserName;
Log("Detecting osu! stable...");
osuPath = DetectOsuStable();
Log("Detecting OpenTabletDriver settings...");
otdSettingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OpenTabletDriver");
if (!Directory.Exists(otdSettingsPath))
otdSettingsPath = null;
Log("Searching for OpenTabletDriver executable...");
otdExePath = DetectOtdExe();
Log($"osu! path: {(osuPath ?? "NOT FOUND")}");
Log($"OTD settings path: {(otdSettingsPath ?? "NOT FOUND")}");
Log($"OTD executable path: {(otdExePath ?? "NOT FOUND")}");
LoadConfigIndex();
RefreshDropdown();
}
private void Log(string msg)
{
txtLog.AppendText($"{DateTime.Now:HH:mm:ss} - {msg}{Environment.NewLine}");
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.ScrollToCaret();
}
// ================== osu! stable detection ==================
private string DetectOsuStable()
{
string path = null;
Process[] processes = Process.GetProcessesByName("osu!");
foreach (var proc in processes)
{
try
{
string exePath = proc.MainModule.FileName;
string dir = Path.GetDirectoryName(exePath);
if (IsOsuStableDirectory(dir))
return dir;
}
catch { }
}
try
{
string key = "HKEY_CURRENT_USER\\Software\\ppy\\osu!";
string installPath = (string)Registry.GetValue(key, "InstallPath", null);
if (!string.IsNullOrEmpty(installPath) && IsOsuStableDirectory(installPath))
return installPath;
}
catch { }
string local = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "osu!");
if (Directory.Exists(local) && IsOsuStableDirectory(local))
return local;
return null;
}
private bool IsOsuStableDirectory(string dir)
{
string userCfg = $"osu!.{currentWindowsUser}.cfg";
return File.Exists(Path.Combine(dir, "osu!.db")) &&
File.Exists(Path.Combine(dir, userCfg));
}
// ================== OpenTabletDriver executable detection ==================
private string DetectOtdExe()
{
string[] exeNames = {
"OpenTabletDriver.Console.exe",
"OpenTabletDriver.Daemon.exe",
"OpenTabletDriver.UX.Wpf.exe"
};
List<string> roots = new List<string>
{
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), // settings folder
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads"),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
};
// Include all other fixed drives
foreach (var drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Fixed)
roots.Add(drive.RootDirectory.FullName);
}
foreach (var root in roots)
{
string found = SearchSubfolders(root, exeNames, 3);
if (!string.IsNullOrEmpty(found))
return found;
}
return null;
}
private string SearchSubfolders(string root, string[] exeNames, int maxDepth, int currentDepth = 0)
{
if (currentDepth > maxDepth) return null;
try
{
foreach (var file in Directory.GetFiles(root))
{
if (exeNames.Contains(Path.GetFileName(file), StringComparer.OrdinalIgnoreCase))
return file;
}
foreach (var dir in Directory.GetDirectories(root))
{
string found = SearchSubfolders(dir, exeNames, maxDepth, currentDepth + 1);
if (!string.IsNullOrEmpty(found))
return found;
}
}
catch { }
return null;
}
// ================== Config Handling ==================
private void LoadConfigIndex()
{
if (File.Exists(indexFile))
{
string json = File.ReadAllText(indexFile);
configIndex = JsonConvert.DeserializeObject<ConfigIndex>(json);
}
else
{
configIndex = new ConfigIndex { Configs = new List<string>() };
SaveConfigIndex();
}
}
private void SaveConfigIndex()
{
string json = JsonConvert.SerializeObject(configIndex, Formatting.Indented);
File.WriteAllText(indexFile, json);
}
private void RefreshDropdown()
{
cmbConfigs.Items.Clear();
foreach (var c in configIndex.Configs)
cmbConfigs.Items.Add(c);
if (cmbConfigs.Items.Count > 0)
cmbConfigs.SelectedIndex = 0;
}
private void btnExport_Click(object sender, EventArgs e)
{
string configName = Microsoft.VisualBasic.Interaction.InputBox("Enter a name for this config:", "Export Config", "MyConfig");
if (string.IsNullOrWhiteSpace(configName)) return;
string configDir = Path.Combine(configsRoot, configName);
if (Directory.Exists(configDir))
{
var result = MessageBox.Show($"Config '{configName}' already exists. Overwrite?", "Confirm Overwrite", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes) return;
Directory.Delete(configDir, true);
}
Directory.CreateDirectory(Path.Combine(configDir, "osu"));
Directory.CreateDirectory(Path.Combine(configDir, "otd"));
CopyOsuFiles(osuPath, Path.Combine(configDir, "osu"));
CopyOtdFiles(otdSettingsPath, Path.Combine(configDir, "otd"));
if (!configIndex.Configs.Contains(configName))
configIndex.Configs.Add(configName);
SaveConfigIndex();
RefreshDropdown();
Log($"Exported config '{configName}'.");
}
private void btnLoad_Click(object sender, EventArgs e)
{
if (cmbConfigs.SelectedItem == null) { Log("Select a config first."); return; }
string configName = cmbConfigs.SelectedItem.ToString();
string configDir = Path.Combine(configsRoot, configName);
if (!Directory.Exists(configDir)) { Log("Config not found."); return; }
string backupTime = DateTime.Now.ToString("yyyyMMdd_HHmmss");
string backupDir = Path.Combine(backupRoot, backupTime);
Directory.CreateDirectory(Path.Combine(backupDir, "osu"));
Directory.CreateDirectory(Path.Combine(backupDir, "otd"));
BackupOsu(backupDir);
BackupOtd(backupDir);
// Update osu CFG filename to current user
string oldCfg = Path.Combine(configDir, "osu", $"osu!.{currentWindowsUser}.cfg");
string[] existingCfgs = Directory.GetFiles(Path.Combine(configDir, "osu"), "osu!.*.cfg");
if (existingCfgs.Length > 0)
{
string srcCfg = existingCfgs[0];
string dstCfg = oldCfg;
SafeFileCopy(srcCfg, dstCfg);
}
CopyOsuFiles(Path.Combine(configDir, "osu"), osuPath);
CopyOtdFiles(Path.Combine(configDir, "otd"), otdSettingsPath);
RestartOTD();
Log($"Loaded config '{configName}'. Backup saved.");
}
private void btnRestore_Click(object sender, EventArgs e)
{
var backups = Directory.GetDirectories(backupRoot);
if (backups.Length == 0) { Log("No backups available."); return; }
Array.Sort(backups);
string latestBackup = backups[^1];
CopyOsuFiles(Path.Combine(latestBackup, "osu"), osuPath);
CopyOtdFiles(Path.Combine(latestBackup, "otd"), otdSettingsPath);
RestartOTD();
Log($"Restored backup from {Path.GetFileName(latestBackup)}.");
}
// ================== Helpers ==================
private void CopyOsuFiles(string srcDir, string dstDir)
{
if (string.IsNullOrEmpty(srcDir) || string.IsNullOrEmpty(dstDir)) return;
Directory.CreateDirectory(dstDir);
string userCfg = $"osu!.{currentWindowsUser}.cfg";
string[] osuFiles = {userCfg};
foreach (var f in osuFiles)
{
string src = Path.Combine(srcDir, f);
if (File.Exists(src))
{
string dst = Path.Combine(dstDir, f);
SafeFileCopy(src, dst);
}
}
}
private void CopyOtdFiles(string srcDir, string dstDir)
{
if (string.IsNullOrEmpty(srcDir) || string.IsNullOrEmpty(dstDir)) return;
Directory.CreateDirectory(dstDir);
string[] otdFiles = { "settings.json", "tablet-data.txt" };
foreach (var f in otdFiles)
{
string src = Path.Combine(srcDir, f);
if (File.Exists(src))
{
string dst = Path.Combine(dstDir, f);
SafeFileCopy(src, dst);
}
}
}
private void BackupOsu(string backupDir)
{
if (osuPath != null)
{
string backupOsu = Path.Combine(backupDir, "osu");
CopyOsuFiles(osuPath, backupOsu);
}
}
private void BackupOtd(string backupDir)
{
if (otdSettingsPath != null)
{
string backupOtd = Path.Combine(backupDir, "otd");
CopyOtdFiles(otdSettingsPath, backupOtd);
}
}
private void RestartOTD()
{
if (string.IsNullOrEmpty(otdExePath)) return;
string daemonExe = Path.Combine(Path.GetDirectoryName(otdExePath), "OpenTabletDriver.Daemon.exe");
foreach (var proc in Process.GetProcessesByName("OpenTabletDriver.Console")
.Concat(Process.GetProcessesByName("OpenTabletDriver.UX.Wpf"))
.Concat(Process.GetProcessesByName("OpenTabletDriver.Daemon")))
{
try { proc.Kill(); proc.WaitForExit(); } catch { }
}
if (!File.Exists(daemonExe))
{
Log("OpenTabletDriver Daemon not found.");
return;
}
try
{
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = daemonExe,
UseShellExecute = false,
WorkingDirectory = Path.GetDirectoryName(daemonExe)
};
Process.Start(psi);
Log("OpenTabletDriver restarted successfully (daemon).");
}
catch (Exception ex)
{
Log($"Failed to restart OpenTabletDriver: {ex.Message}");
}
}
// Safe file copy to prevent locks
private void SafeFileCopy(string src, string dst)
{
try
{
using (var sourceStream = new FileStream(src, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var destStream = new FileStream(dst, FileMode.Create, FileAccess.Write, FileShare.None))
{
sourceStream.CopyTo(destStream);
}
}
catch (Exception ex)
{
Log($"Failed to copy '{Path.GetFileName(src)}': {ex.Message}");
}
}
// Designer required empty handlers
private void txtLog_TextChanged(object sender, EventArgs e) { }
private void cmbConfigs_SelectedIndexChanged(object sender, EventArgs e) { }
}
public class ConfigIndex
{
public List<string> Configs { get; set; }
}
}