From 018003e7dd429ead90fbb6ce25c058b2204d3a1c Mon Sep 17 00:00:00 2001 From: Lauriethefish Date: Sun, 18 Aug 2024 13:44:02 +0100 Subject: [PATCH] Detect already uninstalled apk and do not crash (closes #169) --- QuestPatcher.Core/AndroidDebugBridge.cs | 27 +++++++++++++++++-- QuestPatcher.Core/InstallManager.cs | 5 +++- QuestPatcher.Core/Patching/PatchingManager.cs | 5 +++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/QuestPatcher.Core/AndroidDebugBridge.cs b/QuestPatcher.Core/AndroidDebugBridge.cs index 122127d..2feecf7 100644 --- a/QuestPatcher.Core/AndroidDebugBridge.cs +++ b/QuestPatcher.Core/AndroidDebugBridge.cs @@ -485,9 +485,32 @@ public async Task DownloadApk(string packageId, string destination) await DownloadFile(appPath, destination); } - public async Task UninstallApp(string packageId) + /// + /// Uninstalls the app with package ID , if an app with this ID exists. + /// + /// The package ID of the app to uninstall. + /// True if the app existed and was uninstalled, false otherwise. + public async Task UninstallApp(string packageId) { - await RunCommand($"uninstall {packageId}"); + // Allow exit code 1 as this is returned in the case of the app not being installed in the first place. + var output = await RunCommand($"uninstall {packageId}", 1); + if(output.ExitCode == 1) + { + // Check if the failure was due to the app being uninstalled already (this causes DELETE_FAILED_INTERNAL_ERROR, which is terrible design) + if (output.AllOutput.Contains("[DELETE_FAILED_INTERNAL_ERROR]")) + { + return false; + } + else + { + throw new AdbException("Received exit code 1 when uninstalling and was not because app was already uninstalled: " + + output.AllOutput); + } + } + else + { + return true; + } } public async Task IsPackageInstalled(string packageId) diff --git a/QuestPatcher.Core/InstallManager.cs b/QuestPatcher.Core/InstallManager.cs index 471c5de..58059e9 100644 --- a/QuestPatcher.Core/InstallManager.cs +++ b/QuestPatcher.Core/InstallManager.cs @@ -277,7 +277,10 @@ public async Task RestoreObbBackup(string backupPath) /// public async Task UninstallApp() { - await _debugBridge.UninstallApp(_config.AppId); + if(!await _debugBridge.UninstallApp(_config.AppId)) + { + Log.Warning("APK was already uninstalled. QuestPatcher is (was) out of sync."); + } InstalledApp = null; _quit(); } diff --git a/QuestPatcher.Core/Patching/PatchingManager.cs b/QuestPatcher.Core/Patching/PatchingManager.cs index 730b967..a27f3a7 100644 --- a/QuestPatcher.Core/Patching/PatchingManager.cs +++ b/QuestPatcher.Core/Patching/PatchingManager.cs @@ -706,7 +706,10 @@ public async Task PatchApp() PatchingStage = PatchingStage.UninstallingOriginal; try { - await _debugBridge.UninstallApp(_config.AppId); + if(!await _debugBridge.UninstallApp(_config.AppId)) + { + Log.Warning("APK was already uninstalled"); + } } catch (AdbException) {