2022-05-26 10:08:19 -07:00
|
|
|
use crate::models::pack::{PackFileHash, PackFormat};
|
2021-11-13 16:35:21 -07:00
|
|
|
use crate::util::validate::validation_errors_to_string;
|
2023-04-20 16:38:30 -07:00
|
|
|
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
|
2021-05-30 15:02:07 -07:00
|
|
|
use std::io::{Cursor, Read};
|
2022-02-05 23:08:30 -07:00
|
|
|
use std::path::Component;
|
2021-11-13 15:46:08 -07:00
|
|
|
use validator::Validate;
|
2021-11-13 16:35:21 -07:00
|
|
|
use zip::ZipArchive;
|
2021-05-30 15:02:07 -07:00
|
|
|
|
2022-07-10 03:25:44 +00:00
|
|
|
pub struct ModpackValidator;
|
2021-05-30 15:02:07 -07:00
|
|
|
|
2022-07-10 03:25:44 +00:00
|
|
|
impl super::Validator for ModpackValidator {
|
2021-10-12 11:26:59 +08:00
|
|
|
fn get_file_extensions(&self) -> &[&str] {
|
2021-12-14 18:08:53 -07:00
|
|
|
&["mrpack"]
|
2021-05-30 15:02:07 -07:00
|
|
|
}
|
|
|
|
|
|
2021-10-12 11:26:59 +08:00
|
|
|
fn get_supported_loaders(&self) -> &[&str] {
|
2024-04-22 13:09:35 -07:00
|
|
|
&["mrpack"]
|
2021-05-30 15:02:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_supported_game_versions(&self) -> SupportedGameVersions {
|
|
|
|
|
SupportedGameVersions::All
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn validate(
|
|
|
|
|
&self,
|
2021-11-13 15:46:08 -07:00
|
|
|
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
|
2021-05-30 15:02:07 -07:00
|
|
|
) -> Result<ValidationResult, ValidationError> {
|
2022-06-05 10:42:33 -07:00
|
|
|
let pack: PackFormat = {
|
2023-04-20 16:38:30 -07:00
|
|
|
let mut file = if let Ok(file) = archive.by_name("modrinth.index.json") {
|
|
|
|
|
file
|
|
|
|
|
} else {
|
|
|
|
|
return Ok(ValidationResult::Warning("Pack manifest is missing."));
|
|
|
|
|
};
|
2021-05-30 15:02:07 -07:00
|
|
|
|
2022-06-05 10:42:33 -07:00
|
|
|
let mut contents = String::new();
|
|
|
|
|
file.read_to_string(&mut contents)?;
|
2021-05-30 15:02:07 -07:00
|
|
|
|
2022-06-05 10:42:33 -07:00
|
|
|
serde_json::from_str(&contents)?
|
|
|
|
|
};
|
2021-05-30 15:02:07 -07:00
|
|
|
|
2021-11-13 16:35:21 -07:00
|
|
|
pack.validate().map_err(|err| {
|
2023-04-20 16:38:30 -07:00
|
|
|
ValidationError::InvalidInput(validation_errors_to_string(err, None).into())
|
2021-11-13 16:35:21 -07:00
|
|
|
})?;
|
2021-11-13 15:46:08 -07:00
|
|
|
|
2021-10-12 11:26:59 +08:00
|
|
|
if pack.game != "minecraft" {
|
2022-03-27 19:12:42 -07:00
|
|
|
return Err(ValidationError::InvalidInput(
|
2021-10-12 11:26:59 +08:00
|
|
|
format!("Game {0} does not exist!", pack.game).into(),
|
|
|
|
|
));
|
2021-05-30 15:02:07 -07:00
|
|
|
}
|
|
|
|
|
|
2022-05-26 10:08:19 -07:00
|
|
|
for file in &pack.files {
|
|
|
|
|
if file.hashes.get(&PackFileHash::Sha1).is_none() {
|
2022-03-27 19:12:42 -07:00
|
|
|
return Err(ValidationError::InvalidInput(
|
2021-11-30 20:07:23 -07:00
|
|
|
"All pack files must provide a SHA1 hash!".into(),
|
|
|
|
|
));
|
|
|
|
|
}
|
2022-02-05 23:08:30 -07:00
|
|
|
|
2022-05-26 10:08:19 -07:00
|
|
|
if file.hashes.get(&PackFileHash::Sha512).is_none() {
|
2022-03-29 19:35:09 -07:00
|
|
|
return Err(ValidationError::InvalidInput(
|
|
|
|
|
"All pack files must provide a SHA512 hash!".into(),
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 10:08:19 -07:00
|
|
|
let path = std::path::Path::new(&file.path)
|
2022-02-05 23:08:30 -07:00
|
|
|
.components()
|
|
|
|
|
.next()
|
2023-04-20 16:38:30 -07:00
|
|
|
.ok_or_else(|| ValidationError::InvalidInput("Invalid pack file path!".into()))?;
|
2022-02-05 23:08:30 -07:00
|
|
|
|
|
|
|
|
match path {
|
|
|
|
|
Component::CurDir | Component::Normal(_) => {}
|
|
|
|
|
_ => {
|
2022-03-27 19:12:42 -07:00
|
|
|
return Err(ValidationError::InvalidInput(
|
2022-02-05 23:08:30 -07:00
|
|
|
"Invalid pack file path!".into(),
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-11-30 20:07:23 -07:00
|
|
|
}
|
|
|
|
|
|
2022-06-05 10:42:33 -07:00
|
|
|
Ok(ValidationResult::PassWithPackDataAndFiles {
|
|
|
|
|
format: pack,
|
|
|
|
|
files: archive
|
|
|
|
|
.file_names()
|
|
|
|
|
.filter(|x| {
|
2023-09-24 11:49:41 -04:00
|
|
|
(x.ends_with("jar") || x.ends_with("zip"))
|
|
|
|
|
&& (x.starts_with("overrides/mods")
|
|
|
|
|
|| x.starts_with("client-overrides/mods")
|
|
|
|
|
|| x.starts_with("server-overrides/mods"))
|
2022-06-05 10:42:33 -07:00
|
|
|
})
|
|
|
|
|
.flat_map(|x| x.rsplit('/').next().map(|x| x.to_string()))
|
|
|
|
|
.collect::<Vec<String>>(),
|
|
|
|
|
})
|
2021-05-30 15:02:07 -07:00
|
|
|
}
|
|
|
|
|
}
|