Skip to content

Commit

Permalink
Ignore not found errors on uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Nov 11, 2024
1 parent 66520cc commit a339bb5
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions crates/autostart-installer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::{env, io, process::ExitCode};
use std::{
env,
io::{self, ErrorKind},
process::ExitCode,
};

use shared::popup::{display_popup, fatal_popup, MessageBoxIcon};
use winreg::{
enums::{HKEY_LOCAL_MACHINE, KEY_ALL_ACCESS},
enums::{HKEY_LOCAL_MACHINE, KEY_SET_VALUE},
RegKey,
};

Expand Down Expand Up @@ -81,49 +85,50 @@ fn install() -> io::Result<()> {
}

fn uninstall() {
let mut errors_bg3 = String::new();
let mut errors_bg3_dx11 = String::new();

let delete_from_key = |key, errors: &mut String| {
let key = HKLM.open_subkey_with_flags(key, KEY_ALL_ACCESS);
match key {
Ok(k) => match k.get_value::<String, _>("debugger") {
Ok(_) => {
if let Err(e) = k.delete_value("debugger") {
errors.push_str(&e.to_string());
let (mut error_bg3, mut error_bg3_dx11) = (Ok(()), Ok(()));

let try_delete_value = |key, error: &mut io::Result<()>| {
match HKLM.open_subkey_with_flags(key, KEY_SET_VALUE) {
Ok(k) => {
if let Err(e) = k.delete_value("debugger") {
if e.kind() != ErrorKind::NotFound {
*error = Err(e);
}
}
}

Err(e) => errors.push_str(&e.to_string()),
},

Err(e) => errors.push_str(&e.to_string()),
Err(e) => {
// it's ok if it doesn't exist
if e.kind() != ErrorKind::NotFound {
*error = Err(e);
}
}
}
};

delete_from_key(R_BG3, &mut errors_bg3);
delete_from_key(R_BG3_DX11, &mut errors_bg3_dx11);
try_delete_value(R_BG3, &mut error_bg3);
try_delete_value(R_BG3_DX11, &mut error_bg3_dx11);

let bg3_ok = errors_bg3.is_empty();
let bg3_dx11_ok = errors_bg3_dx11.is_empty();
if error_bg3.is_err() || error_bg3_dx11.is_err() {
let bg3_ok = error_bg3.is_ok();
let bg3_dx11_ok = error_bg3_dx11.is_ok();

if !bg3_ok || !bg3_dx11_ok {
let mut errors = String::new();

if !errors_bg3.is_empty() {
errors.push_str(&format!("\nErrors (bg3 key)\n{errors_bg3}\n"));
if error_bg3.is_err() {
errors.push_str(&format!("\nErrors (bg3 key)\n{}\n", error_bg3.unwrap_err()));
}

if !errors_bg3_dx11.is_empty() {
errors.push_str(&format!("\nErrors (bg3_dx11 key)\n{errors_bg3_dx11}\n"));
if error_bg3_dx11.is_err() {
errors.push_str(&format!(
"\nErrors (bg3_dx11 key)\n{}\n",
error_bg3_dx11.unwrap_err()
));
}

fatal_popup(
"uninstall failed",
format!(
r#"If the error is "cannot find the file specified", you can ignore it; it simply means there was nothing to uninstall.
If you'd like to try manually uninstalling, delete the `debugger` value from both:
r#"If you'd like to try manually uninstalling, delete the `debugger` value from both:
(uninstalled: {bg3_ok}) HKLM\{R_BG3}
(uninstalled: {bg3_dx11_ok}) HKLM\{R_BG3_DX11}
Expand Down

0 comments on commit a339bb5

Please sign in to comment.