Skip to content

Commit

Permalink
Update validators again (#925)
Browse files Browse the repository at this point in the history
* Update validators again

* fix tests + clippy
  • Loading branch information
Geometrically authored Jun 12, 2024
1 parent beaaed6 commit b8963d2
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/database/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions src/validate/fabric.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
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;

pub struct FabricValidator;

impl super::Validator for FabricValidator {
fn get_file_extensions(&self) -> &[&str] {
&["jar", "zip"]
&["jar"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["fabric"]
}

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(
Expand All @@ -32,6 +27,8 @@ impl super::Validator for FabricValidator {
));
}

filter_out_packs(archive)?;

Ok(ValidationResult::Pass)
}
}
38 changes: 23 additions & 15 deletions src/validate/forge.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
}
Expand All @@ -51,25 +55,29 @@ 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,
),
)
}

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)
}
Expand Down
4 changes: 3 additions & 1 deletion src/validate/liteloader.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -27,6 +27,8 @@ impl super::Validator for LiteLoaderValidator {
));
}

filter_out_packs(archive)?;

Ok(ValidationResult::Pass)
}
}
25 changes: 25 additions & 0 deletions src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions src/validate/modpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 37 additions & 0 deletions src/validate/neoforge.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion src/validate/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion src/validate/quilt.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,6 +32,8 @@ impl super::Validator for QuiltValidator {
));
}

filter_out_packs(archive)?;

Ok(ValidationResult::Pass)
}
}
34 changes: 34 additions & 0 deletions src/validate/rift.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
26 changes: 25 additions & 1 deletion tests/common/dummy_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl TestFile {
)
.unwrap();
zip.write_all(fabric_mod_json.as_bytes()).unwrap();

zip.start_file(
"META-INF/mods.toml",
FileOptions::default().compression_method(CompressionMethod::Stored),
)
.unwrap();
zip.write_all(fabric_mod_json.as_bytes()).unwrap();

zip.finish().unwrap();
}
let bytes = cursor.into_inner();
Expand All @@ -117,7 +125,23 @@ impl TestFile {
"game": "minecraft",
"versionId": "1.20.1-9.6",
"name": filename,
"files": [],
"files": [
{
"path": "mods/animatica-0.6+1.20.jar",
"hashes": {
"sha1": "3bcb19c759f313e69d3f7848b03c48f15167b88d",
"sha512": "7d50f3f34479f8b052bfb9e2482603b4906b8984039777dc2513ecf18e9af2b599c9d094e88cec774f8525345859e721a394c8cd7c14a789c9538d2533c71d65"
},
"env": {
"client": "required",
"server": "required"
},
"downloads": [
"https://cdn.modrinth.com/data/PRN43VSY/versions/uNgEPb10/animatica-0.6%2B1.20.jar"
],
"fileSize": 69810
}
],
"dependencies": {
"fabric-loader": "0.14.22",
"minecraft": "1.20.1"
Expand Down

0 comments on commit b8963d2

Please sign in to comment.