From cc85a444c9f342e95a422341c7df91764ffff983 Mon Sep 17 00:00:00 2001 From: NotNite Date: Fri, 8 Nov 2024 10:52:46 -0500 Subject: [PATCH 1/2] Change line numbers to token indexes --- GDWeave/Godot/GodotScriptFile.cs | 1 + GDWeave/Script/ScriptModder.cs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/GDWeave/Godot/GodotScriptFile.cs b/GDWeave/Godot/GodotScriptFile.cs index 28600af..fb0e774 100644 --- a/GDWeave/Godot/GodotScriptFile.cs +++ b/GDWeave/Godot/GodotScriptFile.cs @@ -8,6 +8,7 @@ public class GodotScriptFile { public const uint Magic = 0x43534447; // 'G', 'D', 'S', 'C' public const uint Version = 13; // Godot 3.5.2 public const byte UselessXorKey = 0xB6; // What + public const uint TokenLineMask = (1 << 24) - 1; public List Identifiers = new(); public List Constants = new(); diff --git a/GDWeave/Script/ScriptModder.cs b/GDWeave/Script/ScriptModder.cs index 18d3e57..bf51d14 100644 --- a/GDWeave/Script/ScriptModder.cs +++ b/GDWeave/Script/ScriptModder.cs @@ -22,6 +22,7 @@ public bool Run(GodotScriptFile file, string path) { // Translate special tokens back into tokens file.Tokens.Clear(); + file.Lines.Clear(); foreach (var token in tokens) { switch (token) { case ConstantToken constant: { @@ -48,6 +49,8 @@ public bool Run(GodotScriptFile file, string path) { } file.Tokens.Add(token); + var idx = (uint) file.Tokens.Count - 1; + file.Lines.Add((idx, idx & GodotScriptFile.TokenLineMask)); } return true; From 32ee3e560743de0346dba1ac906ec9caffa8b5ee Mon Sep 17 00:00:00 2001 From: NotNite Date: Sun, 10 Nov 2024 15:07:16 -0500 Subject: [PATCH 2/2] Remove loader lock by initializing GDWeave in a hook --- Cargo.lock | 112 +++++++++++++++++++++++++++++++++++++++++++++- loader/Cargo.toml | 6 +++ loader/src/lib.rs | 41 ++++++++++++++++- 3 files changed, 156 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 133059c..ab6a080 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -835,6 +835,16 @@ dependencies = [ "libc", ] +[[package]] +name = "libudis86-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "139bbf9ddb1bfc90c1ac64dd2923d9c957cd433cee7315c018125d72ab08a6b0" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "libz-sys" version = "1.1.20" @@ -855,8 +865,10 @@ dependencies = [ "isahc", "netcorehost", "proxy-dll", + "retour", "thiserror", "win-msgbox", + "windows", ] [[package]] @@ -881,6 +893,15 @@ dependencies = [ "crc", ] +[[package]] +name = "mach2" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19b955cdeb2a02b9117f121ce63aa52d08ade45de53e48fe6a38b39c10f6f709" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.7.4" @@ -914,6 +935,16 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "mmap-fixed-fixed" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0681853891801e4763dc252e843672faf32bcfee27a0aa3b19733902af450acc" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "netcorehost" version = "0.17.0" @@ -1217,6 +1248,18 @@ dependencies = [ "getrandom", ] +[[package]] +name = "region" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6b6ebd13bc009aef9cd476c1310d49ac354d36e240cf1bd753290f3dc7199a7" +dependencies = [ + "bitflags", + "libc", + "mach2", + "windows-sys 0.52.0", +] + [[package]] name = "reqwest" version = "0.12.8" @@ -1260,6 +1303,22 @@ dependencies = [ "windows-registry", ] +[[package]] +name = "retour" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9af44d40e2400b44d491bfaf8eae111b09f23ac4de6e92728e79d93e699c527" +dependencies = [ + "cfg-if", + "generic-array", + "libc", + "libudis86-sys", + "mmap-fixed-fixed", + "once_cell", + "region", + "slice-pool2", +] + [[package]] name = "ring" version = "0.17.8" @@ -1424,6 +1483,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "slice-pool2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a3d689654af89bdfeba29a914ab6ac0236d382eb3b764f7454dde052f2821f8" + [[package]] name = "sluice" version = "0.5.5" @@ -1828,6 +1893,51 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-strings", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "windows-registry" version = "0.2.0" diff --git a/loader/Cargo.toml b/loader/Cargo.toml index fa78fa7..c68573b 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -11,5 +11,11 @@ anyhow = "1.0.89" isahc = "1.7.2" netcorehost = "0.17.0" proxy-dll = "0.2.5" +retour = { version = "0.3.1", features = ["static-detour"] } thiserror = "1.0.64" win-msgbox = "0.2.1" +windows = { version = "0.58.0", features = [ + "Win32_System_Threading", + "Win32_System_ProcessStatus", + "Win32_System_LibraryLoader", +] } diff --git a/loader/src/lib.rs b/loader/src/lib.rs index 5fc1701..a429f65 100644 --- a/loader/src/lib.rs +++ b/loader/src/lib.rs @@ -1,8 +1,17 @@ use isahc::ReadResponseExt; use netcorehost::{error::HostingError, nethost, pdcstr, pdcstring::PdCString}; use proxy_dll::proxy; +use retour::static_detour; use thiserror::Error; use win_msgbox::{Okay, YesNo}; +use windows::{ + core::PCWSTR, + Win32::System::{ + LibraryLoader::GetModuleHandleW, + ProcessStatus::{GetModuleInformation, MODULEINFO}, + Threading::GetCurrentProcess, + }, +}; #[derive(Error, Debug)] pub enum LoaderError { @@ -89,8 +98,11 @@ fn install_net() -> anyhow::Result<()> { Ok(()) } -#[proxy] -pub fn main() { +static_detour! { + static MainHook: fn() -> i32; +} + +pub fn main_detour() -> i32 { if let Err(e) = init() { match e { LoaderError::LoadHostfxrError(_) @@ -123,4 +135,29 @@ pub fn main() { } } } + + MainHook.call() +} + +#[proxy] +pub fn main() { + unsafe { + let process = GetCurrentProcess(); + let module = GetModuleHandleW(PCWSTR::null()).unwrap(); + let mut lpmodinfo = MODULEINFO::default(); + GetModuleInformation( + process, + module, + &mut lpmodinfo, + size_of::() as u32, + ) + .unwrap(); + + let entry = lpmodinfo.EntryPoint; + + MainHook + .initialize(std::mem::transmute(entry), main_detour) + .unwrap(); + MainHook.enable().unwrap(); + } }