Files
Modrinth-plus/apps/labrinth/src/validate/resourcepack.rs

80 lines
2.2 KiB
Rust
Raw Normal View History

2024-10-18 16:07:35 -07:00
use crate::validate::{
MaybeProtectedZipFile, PLAUSIBLE_PACK_REGEX, SupportedGameVersions,
ValidationError, ValidationResult,
2024-10-18 16:07:35 -07:00
};
use chrono::DateTime;
2022-06-26 10:39:38 -07:00
use std::io::Cursor;
use zip::ZipArchive;
pub struct PackValidator;
2022-06-26 10:39:38 -07:00
impl super::Validator for PackValidator {
2022-06-26 10:39:38 -07:00
fn get_file_extensions(&self) -> &[&str] {
&["zip"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["minecraft"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
// Time since release of 13w24a which replaced texture packs with resource packs
2024-10-18 16:07:35 -07:00
SupportedGameVersions::PastDate(
DateTime::from_timestamp(1371137542, 0).unwrap(),
)
2022-06-26 10:39:38 -07:00
}
fn validate_maybe_protected_zip(
2022-06-26 10:39:38 -07:00
&self,
file: &mut MaybeProtectedZipFile,
2022-06-26 10:39:38 -07:00
) -> Result<ValidationResult, ValidationError> {
if match file {
MaybeProtectedZipFile::Unprotected(archive) => {
archive.by_name("pack.mcmeta").is_ok()
}
MaybeProtectedZipFile::MaybeProtected { data, .. } => {
PLAUSIBLE_PACK_REGEX.is_match(data)
}
} {
Ok(ValidationResult::Pass)
} else {
Ok(ValidationResult::Warning(
"No pack.mcmeta present for resourcepack file. Tip: Make sure pack.mcmeta is in the root directory of your pack!",
))
}
}
}
pub struct TexturePackValidator;
impl super::Validator for TexturePackValidator {
fn get_file_extensions(&self) -> &[&str] {
&["zip"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["minecraft"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
// a1.2.2a to 13w23b
SupportedGameVersions::Range(
DateTime::from_timestamp(1289339999, 0).unwrap(),
DateTime::from_timestamp(1370651522, 0).unwrap(),
)
}
fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
if archive.by_name("pack.txt").is_err() {
return Ok(ValidationResult::Warning(
"No pack.txt present for pack file.",
));
}
2022-06-26 10:39:38 -07:00
Ok(ValidationResult::Pass)
}
}