Skip to content

Commit

Permalink
feat: Add RPC to send factorio path data into the dll
Browse files Browse the repository at this point in the history
  • Loading branch information
notnotmelon committed Aug 12, 2024
1 parent fac6119 commit 9aba1a9
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ fn inject_dll(
.map_err(|e| anyhow!("Failed to inject DLL: {e}"))
}

fn rpc(
syringe: &Syringe,
module: ProcessModule<BorrowedProcess>,
read_path: impl AsRef<Path>,
write_path: impl AsRef<Path>,
) -> Result<()> {
let rpc = unsafe {
syringe.get_payload_procedure::<fn(PathBuf, PathBuf) -> Option<String>>(module, "main")
}?
.ok_or(anyhow!("Failed to get RPC procedure"))?;
match rpc.call(&read_path.as_ref().to_path_buf(), &write_path.as_ref().to_path_buf())? {
Some(err) => bail!(err),
None => Ok(()),
}
}

fn create_pipe() -> Result<(HANDLE, HANDLE)> {
let mut stdout_read = INVALID_HANDLE_VALUE;
let mut stdout_write = INVALID_HANDLE_VALUE;
Expand Down Expand Up @@ -170,7 +186,7 @@ fn start_factorio(factorio_path: PCSTR) -> Result<PROCESS_INFORMATION> {

pub fn run() -> Result<()> {
let mut factorio_path = std::env::current_dir()?;
let (_, write_path) = common::get_data_dirs(&factorio_path)?;
let (read_path, write_path) = common::get_data_dirs(&factorio_path)?;

let (stdout_read, _) = create_pipe()?;
let mut reader = unsafe { std::fs::File::from_raw_handle(stdout_read.0) };
Expand All @@ -188,7 +204,10 @@ pub fn run() -> Result<()> {
let syringe = get_syringe().inspect_err(|_| {
attempt_kill_factorio(factorio_process_information);
})?;
inject_dll(&syringe, &dll_path).inspect_err(|_| {
let module = inject_dll(&syringe, &dll_path).inspect_err(|_| {
attempt_kill_factorio(factorio_process_information);
})?;
rpc(&syringe, module, read_path, write_path).inspect_err(|_| {
attempt_kill_factorio(factorio_process_information);
})?;
println!("DLL injected successfully.");
Expand Down

0 comments on commit 9aba1a9

Please sign in to comment.