Skip to content

Commit c41215d

Browse files
committed
Allow uninstaller to fail
1 parent 55c731a commit c41215d

1 file changed

Lines changed: 43 additions & 25 deletions

File tree

MigrationInstaller/Program.cs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ namespace OpenShock.ShockOSC.MigrationInstaller;
1212
public static class Program
1313
{
1414
private const string AppDisplayName = "ShockOSC";
15-
private const string DownloadUrl = "https://github.com/OpenShock/Desktop/releases/latest/download/OpenShock_Desktop_Setup.exe";
15+
16+
private const string DownloadUrl =
17+
"https://github.com/OpenShock/Desktop/releases/latest/download/OpenShock_Desktop_Setup.exe";
18+
1619
private static string TempInstallerPath => Path.Combine(Path.GetTempPath(), "OpenShock_Desktop_Setup.exe");
17-
18-
private static readonly string LocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
20+
21+
private static readonly string LocalAppData =
22+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
23+
1924
private static readonly string DesktopProgramPath = Path.Combine(LocalAppData, "OpenShock", "Desktop");
2025
private static readonly string DesktopExePath = Path.Combine(DesktopProgramPath, "OpenShock.Desktop.exe");
2126

@@ -29,7 +34,7 @@ public static async Task Main()
2934
{
3035
Console.WriteLine("❌ Error: " + ex.Message);
3136
}
32-
37+
3338
Console.WriteLine("Press enter to exit.");
3439
Console.ReadLine();
3540
}
@@ -55,15 +60,15 @@ private static bool RelaunchAsAdmin()
5560
return false;
5661
}
5762
}
58-
63+
5964
private static async Task<bool> RunMainLogic()
6065
{
61-
if(!IsRunAsAdmin())
66+
if (!IsRunAsAdmin())
6267
{
6368
Console.WriteLine("❌ Please run this program as administrator.");
6469
return RelaunchAsAdmin();
6570
}
66-
71+
6772
try
6873
{
6974
await MigrateConfig();
@@ -72,7 +77,7 @@ private static async Task<bool> RunMainLogic()
7277
{
7378
Console.WriteLine($"⚠️ Config migration failed: {ex.Message}");
7479
}
75-
80+
7681
Console.WriteLine("ℹ️ Killing ShockOSC process if running...");
7782
var processes = Process.GetProcessesByName("OpenShock.ShockOsc");
7883
if (processes.Length > 0)
@@ -89,27 +94,36 @@ private static async Task<bool> RunMainLogic()
8994
{
9095
Console.WriteLine("ℹ️ No ShockOSC processes found.");
9196
}
92-
97+
9398
Console.WriteLine("🔍 Searching for uninstaller...");
9499
var uninstallerPath = FindUninstaller(AppDisplayName);
95100
if (string.IsNullOrEmpty(uninstallerPath))
96101
{
97102
Console.WriteLine("❌ Uninstaller not found.");
98-
return false;
99103
}
100-
101-
Console.WriteLine($"🗑 Running uninstaller: {uninstallerPath}");
102-
await RunProcess(uninstallerPath, "/S");
104+
else
105+
{
106+
Console.WriteLine($"🗑 Running uninstaller: {uninstallerPath}");
107+
try
108+
{
109+
await RunProcess(uninstallerPath, "/S");
110+
}
111+
catch (Exception ex)
112+
{
113+
Console.WriteLine($"❌ Failed to run uninstaller: {ex.Message}");
114+
}
115+
}
103116

104117
Console.WriteLine("🌐 Downloading new installer...");
105118
await DownloadFile(DownloadUrl, TempInstallerPath);
106119

107-
Console.WriteLine("🚀 Launching new installer... Please wait a few seconds for the installation to complete....");
120+
Console.WriteLine(
121+
"🚀 Launching new installer... Please wait a few seconds for the installation to complete....");
108122
await RunProcess(TempInstallerPath, "/S");
109123
Console.WriteLine("✅ Update process complete.");
110-
124+
111125
Console.WriteLine($"🔄 Relaunching OpenShock Desktop... ({DesktopExePath})");
112-
126+
113127
var proc = new Process();
114128
proc.StartInfo.WorkingDirectory = DesktopProgramPath;
115129
proc.StartInfo.FileName = DesktopExePath;
@@ -146,7 +160,7 @@ private static async Task<bool> RunMainLogic()
146160
if (!string.IsNullOrEmpty(result)) return result;
147161
}
148162
}
149-
163+
150164
return null;
151165
}
152166

@@ -156,9 +170,9 @@ private static async Task<bool> RunMainLogic()
156170
foreach (var subkeyName in key.GetSubKeyNames())
157171
{
158172
using var subkey = key.OpenSubKey(subkeyName);
159-
if(subkey?.GetValue("DisplayName") is not string regDisplayName) continue;
173+
if (subkey?.GetValue("DisplayName") is not string regDisplayName) continue;
160174
if (!regDisplayName.Equals(displayName, StringComparison.InvariantCulture)) continue;
161-
if(subkey.GetValue("UninstallString") is not string regUninstallString) continue;
175+
if (subkey.GetValue("UninstallString") is not string regUninstallString) continue;
162176
return regUninstallString;
163177
}
164178

@@ -186,7 +200,7 @@ private static async Task RunProcess(string file, string args)
186200
proc.Start();
187201
await proc.WaitForExitAsync();
188202
}
189-
203+
190204
private static bool IsRunAsAdmin()
191205
{
192206
using var identity = WindowsIdentity.GetCurrent();
@@ -199,17 +213,20 @@ private static bool IsRunAsAdmin()
199213
private static readonly string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
200214
private static readonly string OldConfigFolder = Path.Combine(AppData, "OpenShock", "ShockOSC");
201215
private static readonly string OldConfigPath = Path.Combine(OldConfigFolder, "config.json");
202-
private static readonly string ModuleDataFolder = Path.Combine(AppData, "OpenShock", "Desktop", "moduleData", "openshock.shockosc");
216+
217+
private static readonly string ModuleDataFolder =
218+
Path.Combine(AppData, "OpenShock", "Desktop", "moduleData", "openshock.shockosc");
219+
203220
private static readonly string ModuleConfig = Path.Combine(ModuleDataFolder, "config.json");
204-
221+
205222
private static async Task MigrateConfig()
206223
{
207224
if (!File.Exists(OldConfigPath))
208225
{
209226
Console.WriteLine($"ℹ️ No old config found to migrate. Checked at {OldConfigPath}");
210227
return;
211228
}
212-
229+
213230
Directory.CreateDirectory(ModuleDataFolder);
214231

215232
if (File.Exists(ModuleConfig))
@@ -225,7 +242,8 @@ private static async Task MigrateConfig()
225242
OldSchema.ShockOscConfig? oldConfig;
226243
try
227244
{
228-
oldConfig = JsonSerializer.Deserialize<OldSchema.ShockOscConfig>(json, OldSchemaSourceGenerationContext.Default.ShockOscConfig);
245+
oldConfig = JsonSerializer.Deserialize<OldSchema.ShockOscConfig>(json,
246+
OldSchemaSourceGenerationContext.Default.ShockOscConfig);
229247
}
230248
catch (Exception ex)
231249
{
@@ -245,6 +263,6 @@ private static async Task MigrateConfig()
245263
await File.WriteAllTextAsync(ModuleConfig, newJson);
246264
Console.WriteLine("✅ Config migration complete.");
247265
}
248-
266+
249267
#endregion
250268
}

0 commit comments

Comments
 (0)