Skip to content

Commit

Permalink
Merge pull request #57 from ethangreen-dev/fix-build-failures
Browse files Browse the repository at this point in the history
fix: build failure caused by isize, itertools deps
  • Loading branch information
ethangreen-dev authored Jul 3, 2024
2 parents f2c2d11 + 48d15c7 commit 072147c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

22 changes: 11 additions & 11 deletions crates/lovely-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ impl Lovely {
.join("Mods");

let log_dir = mod_dir.join("lovely").join("log");

log::init(&log_dir).unwrap_or_else(|e| panic!("Failed to initialize logger: {e:?}"));

let version = env!("CARGO_PKG_VERSION");
info!("Lovely {version}");

let mut is_vanilla = false;

while let Some(opt) = opts.next_arg().expect("Failed to parse argument.") {
match opt {
Arg::Long("mod-dir") => mod_dir = opts.value().map(PathBuf::from).unwrap_or(mod_dir),
Expand All @@ -97,7 +97,7 @@ impl Lovely {
// Validate that an older Lovely install doesn't already exist within the game directory.
let exe_path = env::current_exe().unwrap();
let game_dir = exe_path.parent().unwrap();
let dwmapi = game_dir.join("dwmapi.dll");
let dwmapi = game_dir.join("dwmapi.dll");

if dwmapi.is_file() {
panic!(
Expand All @@ -108,12 +108,12 @@ impl Lovely {

info!("Game directory is at {game_dir:?}");
info!("Writing logs to {log_dir:?}");

if !mod_dir.is_dir() {
info!("Creating mods directory at {mod_dir:?}");
fs::create_dir_all(&mod_dir).unwrap();
}

info!("Using mod directory at {mod_dir:?}");
let patch_table = PatchTable::load(&mod_dir)
.with_loadbuffer(loadbuffer);
Expand All @@ -124,7 +124,7 @@ impl Lovely {
fs::remove_dir_all(&dump_dir)
.unwrap_or_else(|e| panic!("Failed to recursively delete dumps directory at {dump_dir:?}: {e:?}"));
}

info!("Initialization complete in {}ms", start.elapsed().as_millis());

Lovely {
Expand All @@ -137,7 +137,7 @@ impl Lovely {
}

/// Apply patches onto the raw buffer.
///
///
/// # Safety
/// This function is unsafe because
/// - It interacts and manipulates memory directly through native pointers
Expand All @@ -162,7 +162,7 @@ impl Lovely {
}

// Prepare buffer for patching (Check and remove the last byte if it is a null terminator)
let last_byte = *buf_ptr.add(size - 1);
let last_byte = *buf_ptr.add((size - 1) as usize);
let actual_size = if last_byte == 0 { size - 1 } else { size };

// Convert the buffer from cstr ptr, to byte slice, to utf8 str.
Expand Down Expand Up @@ -263,7 +263,7 @@ impl PatchTable {
// Load n > 0 patch files from the patch directory, collecting them for later processing.
for patch_file in patch_files {
let patch_dir = patch_file.parent().unwrap();

// Determine the mod directory from the location of the lovely patch file.
let mod_dir = if patch_dir.file_name().unwrap() == "lovely" {
patch_dir.parent().unwrap()
Expand Down Expand Up @@ -452,7 +452,7 @@ impl PatchTable {
} else {
info!("Applied {patch_count} patches to '{target}'");
}

// Compute the integrity hash of the patched file.
let mut hasher = Sha256::new();
hasher.update(patched.as_bytes());
Expand Down
1 change: 1 addition & 0 deletions crates/lovely-win/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crate-type = ["cdylib"]
[dependencies]
lovely-core = { version ="0.5.0-beta7", path = "../lovely-core" }

itertools = "0.13.0"
libc = "0.2.141"
once_cell = "1.19.0"
widestring = "1.0.2"
Expand Down
9 changes: 5 additions & 4 deletions crates/lovely-win/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use lovely_core::log::*;
use lovely_core::Lovely;
use lovely_core::sys::LuaState;

use itertools::Itertools;
use once_cell::sync::OnceCell;
use retour::static_detour;
use widestring::U16CString;
Expand All @@ -32,7 +33,7 @@ unsafe extern "system" fn DllMain(_: HINSTANCE, reason: u32, _: *const c_void) -
if reason != 1 {
return 1;
}

panic::set_hook(Box::new(|x| unsafe {
let message = format!("lovely-injector has crashed: \n{x}");
error!("{message}");
Expand All @@ -47,21 +48,21 @@ unsafe extern "system" fn DllMain(_: HINSTANCE, reason: u32, _: *const c_void) -
}));

let args = env::args().collect_vec();
if !args.contains(&"--disable-console".to_string()) {
if !args.contains(&"--disable-console".to_string()) {
let _ = AllocConsole();
}

// Initialize the lovely runtime.
let rt = Lovely::init(&|a, b, c, d| LuaLoadbuffer_Detour.call(a, b, c, d));
RUNTIME.set(rt).unwrap_or_else(|_| panic!("Failed to instantiate runtime."));

// Quick and easy hook injection. Load the lua51.dll module at runtime, determine the address of the luaL_loadbuffer fn, hook it.
let handle = LoadLibraryW(w!("lua51.dll")).unwrap();
let proc = GetProcAddress(handle, s!("luaL_loadbuffer")).unwrap();
let fn_target = std::mem::transmute::<_, unsafe extern "C" fn(*mut c_void, *const u8, isize, *const u8) -> u32>(proc);

LuaLoadbuffer_Detour.initialize(
fn_target,
fn_target,
|a, b, c, d| lua_loadbuffer_detour(a, b, c, d)
)
.unwrap()
Expand Down

0 comments on commit 072147c

Please sign in to comment.