Skip to content

Commit

Permalink
Detect already uninstalled apk and do not crash (closes #169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriethefish committed Aug 18, 2024
1 parent 5812e6c commit 018003e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
27 changes: 25 additions & 2 deletions QuestPatcher.Core/AndroidDebugBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,32 @@ public async Task DownloadApk(string packageId, string destination)
await DownloadFile(appPath, destination);
}

public async Task UninstallApp(string packageId)
/// <summary>
/// Uninstalls the app with package ID <paramref name="packageId"/>, if an app with this ID exists.
/// </summary>
/// <param name="packageId">The package ID of the app to uninstall.</param>
/// <returns>True if the app existed and was uninstalled, false otherwise.</returns>
public async Task<bool> 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<bool> IsPackageInstalled(string packageId)
Expand Down
5 changes: 4 additions & 1 deletion QuestPatcher.Core/InstallManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ public async Task RestoreObbBackup(string backupPath)
/// <returns></returns>
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();
}
Expand Down
5 changes: 4 additions & 1 deletion QuestPatcher.Core/Patching/PatchingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 018003e

Please sign in to comment.