Skip to content

Commit

Permalink
feat: Add sanity check for package install
Browse files Browse the repository at this point in the history
  • Loading branch information
GeckoEidechse committed Jul 20, 2023
1 parent 8ae71ef commit d335e74
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src-tauri/src/mod_management/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::constants::{BLACKLISTED_MODS, CORE_MODS};
use async_recursion::async_recursion;
use thermite::prelude::ThermiteError;

use crate::NorthstarMod;
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -459,6 +460,41 @@ fn delete_older_versions(
Ok(())
}

/// Checks whether some mod is correctly formatted
/// Currently checks whether
/// - Some `mod.json` exists under `mods/*/mod.json`
fn fc_sanity_check(input: &&fs::File) -> bool {
let mut archive = match zip::read::ZipArchive::new(*input) {
Ok(archive) => archive,
Err(_) => return false,
};

let mut has_mods = false;
let mut mod_json_exists = false;

// Checks for `mods/*/mod.json`
for i in 0..archive.len() {
let file = match archive.by_index(i) {
Ok(file) => file,
Err(_) => continue,
};
let file_path = file.mangled_name();
if file_path.starts_with("mods/") {
has_mods = true;
if let Some(name) = file_path.file_name() {
if name == "mod.json" {
let parent_path = file_path.parent().unwrap();
if parent_path.parent().unwrap().to_str().unwrap() == "mods" {
mod_json_exists = true;
}
}
}
}
}

has_mods && mod_json_exists
}

// Copied from `libtermite` source code and modified
// Should be replaced with a library call to libthermite in the future
/// Download and install mod to the specified target.
Expand Down Expand Up @@ -544,14 +580,21 @@ pub async fn fc_download_mod_and_install(
);

// Extract the mod to the mods directory
match thermite::core::manage::install_mod(
match thermite::core::manage::install_with_sanity(
temp_file.file(),
std::path::Path::new(&install_directory),
fc_sanity_check,
) {
Ok(_) => (),
Err(err) => {
log::warn!("libthermite couldn't install mod {thunderstore_mod_string} due to {err:?}",);
return Err(err.to_string());
return match err {
ThermiteError::SanityError => Err(
"Mod failed sanity check during install. It's probably not correctly formatted"
.to_string(),
),
_ => Err(err.to_string()),
};
}
};

Expand Down

0 comments on commit d335e74

Please sign in to comment.