Update validators again (#925)

* Update validators again

* fix tests + clippy
This commit is contained in:
Geometrically
2024-06-12 14:38:35 -07:00
committed by GitHub
parent beaaed6613
commit b8963d272a
11 changed files with 161 additions and 28 deletions

View File

@@ -217,7 +217,7 @@ impl RedisPool {
let mut cached_values = cached_values_raw
.into_iter()
.filter_map(|(key, val)| {
if Utc.timestamp(val.iat + ACTUAL_EXPIRY, 0) < current_time {
if Utc.timestamp_opt(val.iat + ACTUAL_EXPIRY, 0).unwrap() < current_time {
expired_values.insert(val.key.to_string(), val);
None

View File

@@ -1,5 +1,4 @@
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
use chrono::{DateTime, NaiveDateTime, Utc};
use crate::validate::{filter_out_packs, SupportedGameVersions, ValidationError, ValidationResult};
use std::io::Cursor;
use zip::ZipArchive;
@@ -7,7 +6,7 @@ pub struct FabricValidator;
impl super::Validator for FabricValidator {
fn get_file_extensions(&self) -> &[&str] {
&["jar", "zip"]
&["jar"]
}
fn get_supported_loaders(&self) -> &[&str] {
@@ -15,11 +14,7 @@ impl super::Validator for FabricValidator {
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
// Time since release of 18w49a, the first fabric version
SupportedGameVersions::PastDate(DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(1543969469, 0).unwrap(),
Utc,
))
SupportedGameVersions::All
}
fn validate(
@@ -32,6 +27,8 @@ impl super::Validator for FabricValidator {
));
}
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}
}

View File

@@ -1,4 +1,4 @@
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
use crate::validate::{filter_out_packs, SupportedGameVersions, ValidationError, ValidationResult};
use chrono::{DateTime, NaiveDateTime, Utc};
use std::io::Cursor;
use zip::ZipArchive;
@@ -24,13 +24,17 @@ impl super::Validator for ForgeValidator {
fn validate(
&self,
_archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
// if archive.by_name("META-INF/mods.toml").is_err() {
// return Ok(ValidationResult::Warning(
// "No mods.toml present for Forge file.",
// ));
// }
if archive.by_name("META-INF/mods.toml").is_err()
&& !archive.file_names().any(|x| x.ends_with(".class"))
{
return Ok(ValidationResult::Warning(
"No mods.toml or valid class files present for Forge file.",
));
}
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}
@@ -51,11 +55,11 @@ impl super::Validator for LegacyForgeValidator {
// Times between versions 1.5.2 to 1.12.2, which all use the legacy way of defining mods
SupportedGameVersions::Range(
DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(1366818300, 0).unwrap(),
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
),
DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_opt(1505810340, 0).unwrap(),
NaiveDateTime::from_timestamp_opt(1540122066, 0).unwrap(),
Utc,
),
)
@@ -63,13 +67,17 @@ impl super::Validator for LegacyForgeValidator {
fn validate(
&self,
_archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
// if archive.by_name("mcmod.info").is_err() {
// return Ok(ValidationResult::Warning(
// "Forge mod file does not contain mcmod.info!",
// ));
// };
if archive.by_name("mcmod.info").is_err()
&& !archive.file_names().any(|x| x.ends_with(".class"))
{
return Ok(ValidationResult::Warning(
"Forge mod file does not contain mcmod.info or valid class files!",
));
};
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}

View File

@@ -1,4 +1,4 @@
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
use crate::validate::{filter_out_packs, SupportedGameVersions, ValidationError, ValidationResult};
use std::io::Cursor;
use zip::ZipArchive;
@@ -27,6 +27,8 @@ impl super::Validator for LiteLoaderValidator {
));
}
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}
}

View File

@@ -9,9 +9,11 @@ use crate::validate::fabric::FabricValidator;
use crate::validate::forge::{ForgeValidator, LegacyForgeValidator};
use crate::validate::liteloader::LiteLoaderValidator;
use crate::validate::modpack::ModpackValidator;
use crate::validate::neoforge::NeoForgeValidator;
use crate::validate::plugin::*;
use crate::validate::quilt::QuiltValidator;
use crate::validate::resourcepack::{PackValidator, TexturePackValidator};
use crate::validate::rift::RiftValidator;
use crate::validate::shader::{CanvasShaderValidator, CoreShaderValidator, ShaderValidator};
use chrono::{DateTime, Utc};
use std::io::Cursor;
@@ -23,9 +25,11 @@ mod fabric;
mod forge;
mod liteloader;
mod modpack;
mod neoforge;
pub mod plugin;
mod quilt;
mod resourcepack;
mod rift;
mod shader;
#[derive(Error, Debug)]
@@ -104,6 +108,8 @@ static VALIDATORS: &[&dyn Validator] = &[
&ShaderValidator,
&CoreShaderValidator,
&DataPackValidator,
&RiftValidator,
&NeoForgeValidator,
];
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
@@ -239,3 +245,22 @@ fn game_version_supported(
}
}
}
pub fn filter_out_packs(
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
if (archive.by_name("modlist.html").is_ok() && archive.by_name("manifest.json").is_ok())
|| archive
.file_names()
.any(|x| x.starts_with("mods/") && x.ends_with(".jar"))
|| archive
.file_names()
.any(|x| x.starts_with("override/mods/") && x.ends_with(".jar"))
{
return Ok(ValidationResult::Warning(
"Invalid modpack file. You must upload a valid .MRPACK file.",
));
}
Ok(ValidationResult::Pass)
}

View File

@@ -48,6 +48,10 @@ impl super::Validator for ModpackValidator {
));
}
if pack.files.is_empty() && !archive.file_names().any(|x| x.starts_with("overrides/")) {
return Err(ValidationError::InvalidInput("Pack has no files!".into()));
}
for file in &pack.files {
if !file.hashes.contains_key(&PackFileHash::Sha1) {
return Err(ValidationError::InvalidInput(

37
src/validate/neoforge.rs Normal file
View File

@@ -0,0 +1,37 @@
use crate::validate::{filter_out_packs, SupportedGameVersions, ValidationError, ValidationResult};
use std::io::Cursor;
use zip::ZipArchive;
pub struct NeoForgeValidator;
impl super::Validator for NeoForgeValidator {
fn get_file_extensions(&self) -> &[&str] {
&["jar", "zip"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["forge"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
SupportedGameVersions::All
}
fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
if archive.by_name("META-INF/mods.toml").is_err()
&& archive.by_name("META-INF/neoforge.mods.toml").is_err()
&& !archive.file_names().any(|x| x.ends_with(".class"))
{
return Ok(ValidationResult::Warning(
"No neoforge.mods.toml, mods.toml, or valid class files present for NeoForge file.",
));
}
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}
}

View File

@@ -10,7 +10,7 @@ impl super::Validator for PluginYmlValidator {
}
fn get_supported_loaders(&self) -> &[&str] {
&["bukkit", "spigot", "paper", "purpur"]
&["bukkit", "spigot", "paper", "purpur", "folia"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {

View File

@@ -1,4 +1,4 @@
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
use crate::validate::{filter_out_packs, SupportedGameVersions, ValidationError, ValidationResult};
use chrono::{DateTime, NaiveDateTime, Utc};
use std::io::Cursor;
use zip::ZipArchive;
@@ -32,6 +32,8 @@ impl super::Validator for QuiltValidator {
));
}
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}
}

34
src/validate/rift.rs Normal file
View File

@@ -0,0 +1,34 @@
use crate::validate::{filter_out_packs, SupportedGameVersions, ValidationError, ValidationResult};
use std::io::Cursor;
use zip::ZipArchive;
pub struct RiftValidator;
impl super::Validator for RiftValidator {
fn get_file_extensions(&self) -> &[&str] {
&["jar"]
}
fn get_supported_loaders(&self) -> &[&str] {
&["rift"]
}
fn get_supported_game_versions(&self) -> SupportedGameVersions {
SupportedGameVersions::All
}
fn validate(
&self,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
if archive.by_name("riftmod.json").is_err() {
return Ok(ValidationResult::Warning(
"No riftmod.json present for Rift file.",
));
}
filter_out_packs(archive)?;
Ok(ValidationResult::Pass)
}
}