private void TryExecuteLauncherUpdate(string absoluteTarget, int processId) { // Setup default values string? absoluteSourcePath = Path.GetDirectoryName(AppContext.BaseDirectory); SelfUpdateStatus status = SelfUpdateStatus.Success; // Execute update if (!string.IsNullOrEmpty(absoluteSourcePath) && !string.IsNullOrEmpty(absoluteTarget) && processId != 0 /*&& Path.IsPathFullyQualified(absoluteTarget)*/) { try { UpdateLauncher(absoluteSourcePath, absoluteTarget, processId); } catch (Exception e) { Error(e.Message); status = e switch { { } ex when ex is InsufficientDeletePermissionsException => SelfUpdateStatus.DeletePermissionFailure, { } ex when ex is InsufficientMovePermissionsException => SelfUpdateStatus.MovePermissionFailure, { } ex when ex is MoveDirectoryMissingException => SelfUpdateStatus.DirectoryMissingFailure, { } ex when ex is CannotKillProcessException => SelfUpdateStatus.KillFailure, _ => SelfUpdateStatus.UnhandledException }; } } else { if (string.IsNullOrEmpty(absoluteSourcePath) || processId != 0) { Error("pid, or sourcePath was null or empty."); status = SelfUpdateStatus.InvalidArguments; } else { string errorMessage = $"The argument supplied by --target was null, empty or not absolute: {absoluteTarget}"; throw new ArgumentException(errorMessage); } } // Startup new launcher; failure is irresolvable Directory.SetCurrentDirectory(absoluteTarget); Process.Start($"{absoluteTarget}/{LauncherExeFilename}", $"--patch-result={status} --application-log={_applicationLogFilePath}"); }
/** * Executes a launcher update by attempting to apply a launcher update, then restarting the launcher * * @param sourcePath Path of the new launcher binaries * @param targetPath Path of the current launcher binaries * @param processId Process ID of the launcher which blocks us from updating */ static void execute(string sourcePath, string targetPath, int processId) { string backupPath = targetPath + "_removeme"; // Apply launcher self-update SelfUpdateStatus status = SelfUpdateStatus.UnhandledException; try { status = apply(sourcePath, targetPath, backupPath, processId); if (status == SelfUpdateStatus.Success) { // Patch applied successfully; delete backup deleteDirectory(backupPath); } else if (status == SelfUpdateStatus.MovePermissionFailure) { // Patch failed to apply; try to move backup back (if it exists) SelfUpdateStatus restoreBackupResult = moveDirectory(backupPath, targetPath); if (restoreBackupResult != SelfUpdateStatus.Success && // Backup failed to restore restoreBackupResult != SelfUpdateStatus.DirectoryMissingFailure) // Backup existed { // The launcher is stuck in the backup directory. We're up shit creek, and won't be able to restart the launcher. This shouldn't ever happen. throw new Exception("Backup failed to restore; this should never happen"); } } } catch (Exception e) { Error("Unhandled exception: " + e.ToString()); return; } // Startup new launcher; failure is irresolvable Process.Start(targetPath + LauncherExeFilename, "--patch-result=" + status); }