Skip to content

Commit

Permalink
Fix validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Apr 22, 2024
1 parent 33a27db commit b596b1c
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 123 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ use tokio::sync::RwLock;

extern crate clickhouse as clickhouse_crate;
use clickhouse_crate::Client;
use governor::{Quota, RateLimiter};
use governor::middleware::StateInformationMiddleware;
use governor::{Quota, RateLimiter};
use util::cors::default_cors;

use crate::queue::moderation::AutomatedModerationQueue;
use crate::util::ratelimit::KeyedRateLimiter;
use crate::{
queue::payouts::process_payout,
search::indexing::index_projects,
util::env::{parse_strings_from_var, parse_var},
};
use crate::util::ratelimit::KeyedRateLimiter;

pub mod auth;
pub mod clickhouse;
Expand Down
8 changes: 2 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use actix_web::{App, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use env_logger::Env;
use governor::middleware::StateInformationMiddleware;
use governor::{Quota, RateLimiter};
use labrinth::database::redis::RedisPool;
use labrinth::file_hosting::S3Host;
use labrinth::search;
use labrinth::util::ratelimit::{KeyedRateLimiter, RateLimit};
use labrinth::util::ratelimit::RateLimit;
use labrinth::{check_env_vars, clickhouse, database, file_hosting, queue};
use log::{error, info};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::time::Duration;

#[cfg(feature = "jemalloc")]
#[global_allocator]
Expand Down Expand Up @@ -98,7 +94,7 @@ async fn main() -> std::io::Result<()> {

let search_config = search::SearchConfig::new(None);

let mut labrinth_config = labrinth::app_setup(
let labrinth_config = labrinth::app_setup(
pool.clone(),
redis_pool.clone(),
search_config.clone(),
Expand Down
8 changes: 4 additions & 4 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ impl Scheduler {
}

pub fn run<F, R>(&mut self, interval: std::time::Duration, mut task: F)
where
F: FnMut() -> R + Send + 'static,
R: std::future::Future<Output = ()> + Send + 'static,
where
F: FnMut() -> R + Send + 'static,
R: std::future::Future<Output = ()> + Send + 'static,
{
let future = IntervalStream::new(actix_rt::time::interval(interval))
.for_each_concurrent(2, move |_| task());
Expand Down Expand Up @@ -207,4 +207,4 @@ async fn update_versions(
}

Ok(())
}
}
4 changes: 0 additions & 4 deletions src/validate/datapack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ impl super::Validator for DataPackValidator {
&["zip"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["datapack"]
}
Expand Down
4 changes: 0 additions & 4 deletions src/validate/fabric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ impl super::Validator for FabricValidator {
&["jar", "zip"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["fabric"]
}
Expand Down
22 changes: 6 additions & 16 deletions src/validate/forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ impl super::Validator for ForgeValidator {
&["jar", "zip"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["forge"]
}
Expand Down Expand Up @@ -47,10 +43,6 @@ impl super::Validator for LegacyForgeValidator {
&["jar", "zip"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["forge"]
}
Expand All @@ -71,15 +63,13 @@ 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!",
));
};

//TODO: Check if file is a dev JAR?
// if archive.by_name("mcmod.info").is_err() {
// return Ok(ValidationResult::Warning(
// "Forge mod file does not contain mcmod.info!",
// ));
// };

Ok(ValidationResult::Pass)
}
Expand Down
4 changes: 0 additions & 4 deletions src/validate/liteloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ impl super::Validator for LiteLoaderValidator {
&["litemod", "jar"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["liteloader"]
}
Expand Down
78 changes: 40 additions & 38 deletions src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ pub enum SupportedGameVersions {

pub trait Validator: Sync {
fn get_file_extensions(&self) -> &[&str];
fn get_project_types(&self) -> &[&str];
fn get_supported_loaders(&self) -> &[&str];
fn get_supported_game_versions(&self) -> SupportedGameVersions;
fn validate(
Expand Down Expand Up @@ -118,41 +117,28 @@ pub async fn validate_file(
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
redis: &RedisPool,
) -> Result<ValidationResult, ValidationError> {
// TODO: This needs to be revisited or removed with v3.
// Currently, it checks if the loader is the modpack loader, and extracts the pack data from it.
// This (and the funnction that calls this) should be refactored such that
// - validators are removed (or altogether reworked)
// - if a mrpack is uploaded, the pack data is extracted and usable to extract dependencies automatically

// TODO: A test needs to be written for this.
match loaders {
loaders if loaders == vec![Loader("mrpack".to_string())] => {
let game_versions = version_fields
.into_iter()
.find_map(|v| MinecraftGameVersion::try_from_version_field(&v).ok())
.unwrap_or_default();
let all_game_versions =
MinecraftGameVersion::list(None, None, &mut *transaction, redis).await?;
validate_minecraft_file(
data,
file_extension,
"modpack".to_string(),
loaders,
game_versions,
all_game_versions,
file_type,
)
.await
}
_ => Ok(ValidationResult::Pass),
}
let game_versions = version_fields
.into_iter()
.find_map(|v| MinecraftGameVersion::try_from_version_field(&v).ok())
.unwrap_or_default();
let all_game_versions =
MinecraftGameVersion::list(None, None, &mut *transaction, redis).await?;

validate_minecraft_file(
data,
file_extension,
loaders,
game_versions,
all_game_versions,
file_type,
)
.await
}

async fn validate_minecraft_file(
data: bytes::Bytes,
file_extension: String,
mut project_type: String,
mut loaders: Vec<Loader>,
loaders: Vec<Loader>,
game_versions: Vec<MinecraftGameVersion>,
all_game_versions: Vec<MinecraftGameVersion>,
file_type: Option<FileType>,
Expand All @@ -164,33 +150,49 @@ async fn validate_minecraft_file(
if let Some(file_type) = file_type {
match file_type {
FileType::RequiredResourcePack | FileType::OptionalResourcePack => {
project_type = "resourcepack".to_string();
loaders = vec![Loader("minecraft".to_string())];
return PackValidator.validate(&mut zip);
}
FileType::Unknown => {}
}
}

let mut visited = false;
let mut saved_result = None;
for validator in VALIDATORS {
if validator.get_project_types().contains(&&*project_type)
&& loaders
.iter()
.any(|x| validator.get_supported_loaders().contains(&&*x.0))
if loaders
.iter()
.any(|x| validator.get_supported_loaders().contains(&&*x.0))
&& game_version_supported(
&game_versions,
&all_game_versions,
validator.get_supported_game_versions(),
)
{
if validator.get_file_extensions().contains(&&*file_extension) {
return validator.validate(&mut zip);
let result = validator.validate(&mut zip)?;
match result {
ValidationResult::PassWithPackDataAndFiles { .. } => {
saved_result = Some(result);
}
ValidationResult::Pass => {
if saved_result.is_none() {
saved_result = Some(result);
}
}
ValidationResult::Warning(_) => {
return Ok(result);
}
}
} else {
visited = true;
}
}
}

if let Some(result) = saved_result {
return Ok(result);
}

if visited {
if ALWAYS_ALLOWED_EXT.contains(&&*file_extension) {
Ok(ValidationResult::Warning(
Expand Down
6 changes: 1 addition & 5 deletions src/validate/modpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ impl super::Validator for ModpackValidator {
&["mrpack"]
}

fn get_project_types(&self) -> &[&str] {
&["modpack"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["forge", "fabric", "quilt", "mrpack"]
&["mrpack"]
}

fn get_supported_game_versions(&self) -> SupportedGameVersions {
Expand Down
16 changes: 0 additions & 16 deletions src/validate/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ impl super::Validator for PluginYmlValidator {
&["zip", "jar"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["bukkit", "spigot", "paper", "purpur"]
}
Expand Down Expand Up @@ -45,10 +41,6 @@ impl super::Validator for BungeeCordValidator {
&["zip", "jar"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["bungeecord", "waterfall"]
}
Expand Down Expand Up @@ -81,10 +73,6 @@ impl super::Validator for VelocityValidator {
&["zip", "jar"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["velocity"]
}
Expand Down Expand Up @@ -114,10 +102,6 @@ impl super::Validator for SpongeValidator {
&["zip", "jar"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["sponge"]
}
Expand Down
4 changes: 0 additions & 4 deletions src/validate/quilt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ impl super::Validator for QuiltValidator {
&["jar", "zip"]
}

fn get_project_types(&self) -> &[&str] {
&["mod"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["quilt"]
}
Expand Down
8 changes: 0 additions & 8 deletions src/validate/resourcepack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ impl super::Validator for PackValidator {
&["zip"]
}

fn get_project_types(&self) -> &[&str] {
&["resourcepack"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["minecraft"]
}
Expand Down Expand Up @@ -47,10 +43,6 @@ impl super::Validator for TexturePackValidator {
&["zip"]
}

fn get_project_types(&self) -> &[&str] {
&["resourcepack"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["minecraft"]
}
Expand Down
12 changes: 0 additions & 12 deletions src/validate/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ impl super::Validator for ShaderValidator {
&["zip"]
}

fn get_project_types(&self) -> &[&str] {
&["shader"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["optifine", "iris"]
}
Expand Down Expand Up @@ -42,10 +38,6 @@ impl super::Validator for CanvasShaderValidator {
&["zip"]
}

fn get_project_types(&self) -> &[&str] {
&["shader"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["canvas"]
}
Expand Down Expand Up @@ -81,10 +73,6 @@ impl super::Validator for CoreShaderValidator {
&["zip"]
}

fn get_project_types(&self) -> &[&str] {
&["shader"]
}

fn get_supported_loaders(&self) -> &[&str] {
&["vanilla"]
}
Expand Down

0 comments on commit b596b1c

Please sign in to comment.