Skip to content

Commit

Permalink
refactor: use common functions in windows impl
Browse files Browse the repository at this point in the history
  • Loading branch information
fgardt committed Aug 12, 2024
1 parent a3fd881 commit b14315e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 101 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
92 changes: 4 additions & 88 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Path>,
output_path: impl AsRef<Path>,
) -> 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<Path>) -> Result<String> {
let mut latest_version: Option<Version> = None;
let mut latest_version_file: Option<PathBuf> = 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<Path>) -> Result<PathBuf> {
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<Syringe> {
let Some(process) = OwnedProcess::find_first_by_name("factorio") else {
Expand Down Expand Up @@ -188,19 +104,19 @@ fn start_factorio(factorio_path: PCSTR) -> Result<PROCESS_INFORMATION> {
}

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.");
Expand Down

0 comments on commit b14315e

Please sign in to comment.