From e497af4c268a26f4d7d8d2a1c49c1a6be98549f4 Mon Sep 17 00:00:00 2001 From: Geometrically <18202329+Geometrically@users.noreply.github.com> Date: Sun, 5 Jun 2022 10:42:33 -0700 Subject: [PATCH] Add deps list for override mods, fix version editing for packs (#363) --- src/routes/version_creation.rs | 46 +++++++++++++++++++++------------- src/util/validate.rs | 5 ++-- src/validate/mod.rs | 7 ++++-- src/validate/pack.rs | 33 ++++++++++++++++-------- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/routes/version_creation.rs b/src/routes/version_creation.rs index 4db932263..97f11f41f 100644 --- a/src/routes/version_creation.rs +++ b/src/routes/version_creation.rs @@ -696,9 +696,13 @@ pub async fn upload_file( ) .await?; - if let ValidationResult::PassWithPackData(ref data) = validation_result { + if let ValidationResult::PassWithPackDataAndFiles { + ref format, + ref files, + } = validation_result + { if dependencies.is_empty() { - let hashes: Vec> = data + let hashes: Vec> = format .files .iter() .filter_map(|x| x.hashes.get(&PackFileHash::Sha1)) @@ -716,7 +720,7 @@ pub async fn upload_file( ) .fetch_all(&mut *transaction).await?; - for file in &data.files { + for file in &format.files { if let Some(dep) = res.iter().find(|x| { x.hash.as_deref() == file @@ -735,24 +739,30 @@ pub async fn upload_file( }); } } - } else { - if let Some(first_download) = file.downloads.first() { - dependencies.push(DependencyBuilder { - project_id: None, - version_id: None, - file_name: Some( - first_download - .rsplit('/') - .next() - .unwrap_or(first_download) - .to_string(), - ), - dependency_type: DependencyType::Required + } else if let Some(first_download) = file.downloads.first() { + dependencies.push(DependencyBuilder { + project_id: None, + version_id: None, + file_name: Some( + first_download + .rsplit('/') + .next() + .unwrap_or(first_download) .to_string(), - }); - } + ), + dependency_type: DependencyType::Required.to_string(), + }); } } + + for file in files { + dependencies.push(DependencyBuilder { + project_id: None, + version_id: None, + file_name: Some(file.to_string()), + dependency_type: DependencyType::Required.to_string(), + }); + } } } diff --git a/src/util/validate.rs b/src/util/validate.rs index 34ad7fcd1..5dc4912a8 100644 --- a/src/util/validate.rs +++ b/src/util/validate.rs @@ -72,11 +72,12 @@ pub fn validate_deps( .iter() .duplicates_by(|x| { format!( - "{}-{}", + "{}-{}-{}", x.version_id .unwrap_or(crate::models::projects::VersionId(0)), x.project_id - .unwrap_or(crate::models::projects::ProjectId(0)) + .unwrap_or(crate::models::projects::ProjectId(0)), + x.file_name.as_deref().unwrap_or_default() ) }) .next() diff --git a/src/validate/mod.rs b/src/validate/mod.rs index b46d9f4ea..86fde2838 100644 --- a/src/validate/mod.rs +++ b/src/validate/mod.rs @@ -33,7 +33,10 @@ pub enum ValidationError { #[derive(Eq, PartialEq)] pub enum ValidationResult { /// File should be marked as primary with pack file data - PassWithPackData(PackFormat), + PassWithPackDataAndFiles { + format: PackFormat, + files: Vec, + }, /// File should be marked as primary Pass, /// File should not be marked primary, the reason for which is inside the String @@ -43,7 +46,7 @@ pub enum ValidationResult { impl ValidationResult { pub fn is_passed(&self) -> bool { match self { - ValidationResult::PassWithPackData(_) => true, + ValidationResult::PassWithPackDataAndFiles { .. } => true, ValidationResult::Pass => true, ValidationResult::Warning(_) => false, } diff --git a/src/validate/pack.rs b/src/validate/pack.rs index 6b1c10836..ff79daff3 100644 --- a/src/validate/pack.rs +++ b/src/validate/pack.rs @@ -31,17 +31,19 @@ impl super::Validator for PackValidator { &self, archive: &mut ZipArchive>, ) -> Result { - let mut file = - archive.by_name("modrinth.index.json").map_err(|_| { - ValidationError::InvalidInput( - "Pack manifest is missing.".into(), - ) - })?; + let pack: PackFormat = { + let mut file = + archive.by_name("modrinth.index.json").map_err(|_| { + ValidationError::InvalidInput( + "Pack manifest is missing.".into(), + ) + })?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; - let pack: PackFormat = serde_json::from_str(&contents)?; + serde_json::from_str(&contents)? + }; pack.validate().map_err(|err| { ValidationError::InvalidInput( @@ -87,6 +89,17 @@ impl super::Validator for PackValidator { }; } - Ok(ValidationResult::PassWithPackData(pack)) + Ok(ValidationResult::PassWithPackDataAndFiles { + format: pack, + files: archive + .file_names() + .filter(|x| { + x.starts_with("overrides/mods") + || x.starts_with("client-overrides/mods") + || x.starts_with("server-overrides/mods") + }) + .flat_map(|x| x.rsplit('/').next().map(|x| x.to_string())) + .collect::>(), + }) } }