diff --git a/Cargo.lock b/Cargo.lock index 6f47e5c..7db77bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -925,10 +925,8 @@ dependencies = [ "ctor", "dll-syringe", "mod_util", - "semver", "thiserror", "windows", - "zip", ] [[package]] @@ -966,12 +964,6 @@ dependencies = [ "syn 2.0.74", ] -[[package]] -name = "semver" -version = "1.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" - [[package]] name = "serde" version = "1.0.206" diff --git a/Cargo.toml b/Cargo.toml index f3927e0..9474cb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,12 @@ nursery = { level = "warn", priority = -1 } pedantic = { level = "warn", priority = -1 } [dependencies] -anyhow = "1.0.86" -semver = "1.0.23" -thiserror = "1.0.63" -configparser = "3.1.0" +thiserror = "1.0" +configparser = "3.1" mod_util = { git = "https://github.com/fgardt/factorio-scanner" } [target.'cfg(windows)'.dependencies] -zip = "2.1.6" +anyhow = "1.0" dll-syringe = "0.15.2" windows = { version = "0.58.0", features = [ "Win32", diff --git a/src/windows.rs b/src/windows.rs index 99e7705..e97b95a 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -4,7 +4,6 @@ use crate::common; use anyhow::{anyhow, bail, Context, Result}; use dll_syringe::process::{BorrowedProcess, ProcessModule}; use dll_syringe::{process::OwnedProcess, Syringe}; -use semver::Version; use std::ffi::CString; use std::fs; use std::fs::File; @@ -21,89 +20,6 @@ use windows::Win32::System::Threading::{ CreateProcessA, ResumeThread, TerminateProcess, CREATE_SUSPENDED, PROCESS_INFORMATION, STARTUPINFOA, }; -use zip::read::ZipArchive; - -fn unzip_specific_file( - file_name: &str, - zip_path: impl AsRef, - output_path: impl AsRef, -) -> Result<()> { - let file = File::open(zip_path)?; - let mut archive = ZipArchive::new(file).context("Failed to open rivets mod ZIP archive")?; - - let mut zip_file = archive - .by_name(file_name) - .context("Failed to find rivets.dll in ZIP archive")?; - - let err = format!( - "Failed to create output file {}", - output_path - .as_ref() - .to_str() - .ok_or(anyhow!("Failed to unzip rivets.dll"))? - ); - let mut output_file = File::create(output_path).context(err)?; - - io::copy(&mut zip_file, &mut output_file) - .context("Failed to write ZIP entry to output file")?; - - Ok(()) -} - -fn find_latest_rivets_version(write_path: impl AsRef) -> Result { - let mut latest_version: Option = None; - let mut latest_version_file: Option = None; - - for entry in fs::read_dir(write_path.as_ref().join("mods"))? { - let entry = entry?; - let file_name = entry.file_name(); - if let Some(file_name_str) = file_name.to_str() { - if file_name_str.starts_with("rivets_") && file_name_str.ends_with(".zip") { - let version_str = file_name_str - .trim_start_matches("rivets_") - .trim_end_matches(".zip"); - if let Ok(version) = Version::parse(version_str) { - if let Some(current_version) = &latest_version { - if version > *current_version { - latest_version = Some(version); - latest_version_file = Some(entry.path()); - } - } else { - latest_version = Some(version); - latest_version_file = Some(entry.path()); - } - } - } - } - } - - if let Some(file_path) = latest_version_file { - if let Some(file_name) = file_path.file_name() { - if let Some(file_name_str) = file_name.to_str() { - return Ok(file_name_str.to_string()); - } - } - } - - Err(anyhow!("No rivets mod found in the mods folder")) -} - -fn extract_dll(write_path: impl AsRef) -> Result { - const DLL_NAME: &str = "rivets/rivets.dll"; - - let latest_rivets_version = find_latest_rivets_version(write_path.as_ref())?; - println!("Found rivets version: {latest_rivets_version} Injecting...",); - - let tmp_folder = write_path.as_ref().join("temp/rivets"); - fs::create_dir_all(&tmp_folder)?; - - let output_path = tmp_folder.join(format!("{latest_rivets_version}.dll")); - let zip_path = write_path.as_ref().join("mods").join(latest_rivets_version); - - unzip_specific_file(DLL_NAME, zip_path, &output_path)?; - - Ok(output_path) -} fn get_syringe() -> Result { let Some(process) = OwnedProcess::find_first_by_name("factorio") else { @@ -188,19 +104,19 @@ fn start_factorio(factorio_path: PCSTR) -> Result { } pub fn run() -> Result<()> { - let mut factorio_path = std::env::current_dir()?; - let (read_path, write_path) = common::get_data_dirs(&factorio_path)?; + let bin_path = common::get_bin_folder()?; + let (read_path, write_path) = common::get_data_dirs(&bin_path)?; let (stdout_read, _) = create_pipe()?; let mut reader = unsafe { std::fs::File::from_raw_handle(stdout_read.0) }; - factorio_path.push("factorio.exe"); + let factorio_path = bin_path.join("factorio.exe"); let factorio_path = CString::new(factorio_path.as_os_str().to_string_lossy().into_owned())?; println!("Factorio path: {factorio_path:?}"); let factorio_path = PCSTR(factorio_path.as_ptr().cast()); - let dll_path = extract_dll(&write_path)?; + let dll_path = common::extract_rivets_lib(read_path, write_path)?; let factorio_process_information: PROCESS_INFORMATION = start_factorio(factorio_path)?; println!("Factorio process started.");