From 246564d00f032ba75ec1de7a9d3377d4c65df529 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Fri, 24 Nov 2023 13:54:19 +0100 Subject: [PATCH 1/4] Fetch file version from Nexus When importing an archive file downloaded from Nexus, the file name does include a version field. But, presumably for compatibility reasons, Nexus replaces special characters with `-`, so that this field doesn't match common schemes like `1.0.0`. So instead we use the also included update timestamp to find the corresponding file info from Nexus and use the version data from that. Closes #131. --- crates/dtmm/src/controller/import.rs | 20 ++++++++++++++++--- lib/nexusmods/src/lib.rs | 24 +++++++++++++++++++++++ lib/nexusmods/src/types.rs | 29 ++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/crates/dtmm/src/controller/import.rs b/crates/dtmm/src/controller/import.rs index 131d01f..522d94a 100644 --- a/crates/dtmm/src/controller/import.rs +++ b/crates/dtmm/src/controller/import.rs @@ -381,7 +381,7 @@ pub(crate) async fn import_mod(state: ActionState, info: FileInfo) -> Result Result version, + Err(err) => { + let err = Report::new(err); + tracing::warn!( + "Failed to fetch version for Nexus download. \ + Falling back to file name:\n{:?}", + err + ); + version + } + }; + + let info = NexusInfo::from(mod_info); + tracing::debug!(version, ?info); + Some((info, version)) } else { None diff --git a/lib/nexusmods/src/lib.rs b/lib/nexusmods/src/lib.rs index 145435d..0cca768 100644 --- a/lib/nexusmods/src/lib.rs +++ b/lib/nexusmods/src/lib.rs @@ -39,6 +39,8 @@ pub enum Error { Infallible(#[from] Infallible), #[error("invalid NXM URL '{}': {0}", .1.as_str())] InvalidNXM(&'static str, Url), + #[error("{0}")] + Custom(String), } pub type Result = std::result::Result; @@ -102,6 +104,28 @@ impl Api { self.send(req).await } + #[tracing::instrument(skip(self))] + pub async fn file_version(&self, id: u64, timestamp: T) -> Result + where + T: std::fmt::Debug, + OffsetDateTime: PartialEq, + { + let url = BASE_URL_GAME.join(&format!("mods/{id}/files.json"))?; + let req = self.client.get(url); + let files: FileList = self.send(req).await?; + + let Some(file) = files + .files + .into_iter() + .find(|file| file.updated_timestamp == timestamp) + else { + let err = Error::Custom("Timestamp does not match any file".into()); + return Err(err); + }; + + Ok(file.version) + } + pub fn parse_file_name>( name: S, ) -> Option<(String, u64, String, OffsetDateTime)> { diff --git a/lib/nexusmods/src/types.rs b/lib/nexusmods/src/types.rs index b0dffd5..b88911e 100644 --- a/lib/nexusmods/src/types.rs +++ b/lib/nexusmods/src/types.rs @@ -64,6 +64,35 @@ pub struct Mod { // pub contains_adult_content: bool, } +#[derive(Debug, Deserialize)] +pub struct File { + pub id: Vec, + pub uid: u64, + pub file_id: u64, + pub name: String, + pub version: String, + pub category_id: u64, + pub category_name: String, + pub is_primary: bool, + pub size: u64, + pub file_name: String, + #[serde(with = "time::serde::timestamp")] + pub updated_timestamp: OffsetDateTime, + pub mod_version: String, + pub external_virus_scan_url: String, + pub description: String, + pub size_kb: u64, + pub size_in_bytes: u64, + pub changelog_html: String, + pub content_preview_link: String, +} + +#[derive(Debug, Deserialize)] +pub struct FileList { + pub files: Vec, + // pub file_updates: Vec, +} + #[derive(Debug, Deserialize)] pub struct DownloadLink { pub name: String, -- 2.45.3 From 440d0f505bf4390c3d70c46eea3ae6f3fb821fb9 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Mon, 27 Nov 2023 15:38:40 +0100 Subject: [PATCH 2/4] Add changelog entry --- CHANGELOG.adoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 4d1861c..31910d3 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -16,6 +16,7 @@ - dtmt: add utility to migrate mod projects - dtmm: reset dtkit-patch installations - sdk: implement decompiling Lua files +- dtmm: fetch file version for Nexus mods === Fixed -- 2.45.3 From 2ad3fd0fc1195bd094b79c731fe123c99e0d1e13 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 29 Nov 2023 15:21:07 +0100 Subject: [PATCH 3/4] dtmm: Fix importing from `.mod` file --- crates/dtmm/src/controller/import.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/dtmm/src/controller/import.rs b/crates/dtmm/src/controller/import.rs index 522d94a..68f2b05 100644 --- a/crates/dtmm/src/controller/import.rs +++ b/crates/dtmm/src/controller/import.rs @@ -234,6 +234,8 @@ fn extract_mod_config(archive: &mut ZipArchive) -> Result<(Mo None }; + tracing::debug!(?legacy_mod_data); + if let Some(name) = find_archive_file(archive, "dtmt.cfg") { let mut f = archive .by_name(&name) @@ -266,6 +268,24 @@ fn extract_mod_config(archive: &mut ZipArchive) -> Result<(Mo Ok((cfg, root)) } + } else if let Some((mod_id, resources, root)) = legacy_mod_data { + let cfg = ModConfig { + bundled: false, + dir: PathBuf::new(), + id: mod_id.clone(), + name: mod_id, + summary: "A mod for the game Warhammer 40,000: Darktide".into(), + version: "N/A".into(), + description: None, + author: None, + image: None, + categories: Vec::new(), + packages: Vec::new(), + resources, + depends: Vec::new(), + }; + + Ok((cfg, root)) } else { eyre::bail!( "Mod needs a config file or `.mod` file. \ -- 2.45.3 From a0fe5d3f816396d586f443dfc5e881f6e374ee9d Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Wed, 29 Nov 2023 15:37:37 +0100 Subject: [PATCH 4/4] nexusmods: Fix File type --- lib/nexusmods/src/lib.rs | 2 +- lib/nexusmods/src/types.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/nexusmods/src/lib.rs b/lib/nexusmods/src/lib.rs index 0cca768..06c1f01 100644 --- a/lib/nexusmods/src/lib.rs +++ b/lib/nexusmods/src/lib.rs @@ -117,7 +117,7 @@ impl Api { let Some(file) = files .files .into_iter() - .find(|file| file.updated_timestamp == timestamp) + .find(|file| file.uploaded_timestamp == timestamp) else { let err = Error::Custom("Timestamp does not match any file".into()); return Err(err); diff --git a/lib/nexusmods/src/types.rs b/lib/nexusmods/src/types.rs index b88911e..db0f624 100644 --- a/lib/nexusmods/src/types.rs +++ b/lib/nexusmods/src/types.rs @@ -77,13 +77,13 @@ pub struct File { pub size: u64, pub file_name: String, #[serde(with = "time::serde::timestamp")] - pub updated_timestamp: OffsetDateTime, + pub uploaded_timestamp: OffsetDateTime, pub mod_version: String, pub external_virus_scan_url: String, pub description: String, pub size_kb: u64, pub size_in_bytes: u64, - pub changelog_html: String, + pub changelog_html: Option, pub content_preview_link: String, } -- 2.45.3