Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
Cleanup. Removed retour dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankvdStam committed Jul 12, 2024
1 parent e776b83 commit 770e164
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 153 deletions.
1 change: 0 additions & 1 deletion soulmemory-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
crate-type = ["lib", "cdylib"]

[dependencies]
retour = { version = "0.3.0", features = ["static-detour"] }
ilhook = "2.1.0"

mem-rs = "0.1.4"
Expand Down
5 changes: 4 additions & 1 deletion soulmemory-rs/src/games/armored_core_6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#![allow(dead_code)]
#![allow(unused_imports)]

use std::any::Any;
use std::mem;
use std::ops::Deref;
Expand Down Expand Up @@ -136,7 +139,7 @@ impl Game for ArmoredCore6
fn as_any_mut(&mut self) -> &mut dyn Any { self }
}


#[cfg(target_arch = "x86_64")]
unsafe extern "win64" fn set_event_flag_hook_fn(registers: *mut Registers, _:usize)
{
let instance = App::get_instance();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// This file is part of the soulmemory-rs distribution (https://github.com/FrankvdStam/soulmemory-rs).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/soulmemory-rs/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::sync::{Arc, Mutex};
use mem_rs::prelude::ReadWrite;
use crate::games::{DarkSouls2ScholarOfTheFirstSin};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// This file is part of the soulmemory-rs distribution (https://github.com/FrankvdStam/soulmemory-rs).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/soulmemory-rs/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#![allow(dead_code)]
#![allow(unused_imports)]

mod buffered_event_flags;

use std::any::Any;
Expand All @@ -13,6 +32,10 @@ use crate::games::dx_version::DxVersion;
use crate::games::{Game, GameExt};
use crate::games::traits::buffered_event_flags::{BufferedEventFlags, EventFlag};

#[cfg(target_arch = "x86")]//This version exists only to make things compile easily for x86
type FnGetEventFlag = unsafe extern "thiscall" fn(event_flag_man: u64, event_flag: u32) -> u8;

#[cfg(target_arch = "x86_64")]
type FnGetEventFlag = unsafe extern "win64" fn(event_flag_man: u64, event_flag: u32) -> u8;

pub struct DarkSouls2ScholarOfTheFirstSin
Expand All @@ -29,6 +52,10 @@ impl DarkSouls2ScholarOfTheFirstSin
{
pub fn new() -> Self
{
#[cfg(target_arch = "x86")]//This version exists only to make things compile easily for x86
unsafe extern "thiscall" fn empty(_: u64, _: u32) -> u8 { 0 }

#[cfg(target_arch = "x86_64")]
unsafe extern "win64" fn empty(_: u64, _: u32) -> u8 { 0 }

DarkSouls2ScholarOfTheFirstSin
Expand Down Expand Up @@ -58,27 +85,15 @@ impl Game for DarkSouls2ScholarOfTheFirstSin

self.fn_get_event_flag = mem::transmute(get_event_flag_address);

unsafe extern "win64" fn read_event_flag_hook_fn(registers: *mut Registers, _:usize)
#[cfg(target_arch = "x86_64")]
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(scholar) = GameExt::get_game_ref::<DarkSouls2ScholarOfTheFirstSin>(app.game.deref())
{
let event_flag_id = (*registers).rdx as u32;
let value = (*registers).r8 as u8;

let mut guard = scholar.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
let h = Hooker::new(set_event_flag_address, HookType::JmpBack(crate::games::dark_souls_2_scholar_of_the_first_sin::read_event_flag_hook_fn), CallbackOption::None, 0, HookFlags::empty());
self.set_event_flag_hook = Some(h.hook().unwrap());
}

let h = Hooker::new(set_event_flag_address, HookType::JmpBack(read_event_flag_hook_fn), CallbackOption::None, 0, HookFlags::empty());
self.set_event_flag_hook = Some(h.hook().unwrap());

info!("event_flag_man base address: 0x{:x}", self.event_flag_man.get_base_address());
info!("get event flag address : 0x{:x}", get_event_flag_address);
info!("get event flag address : 0x{:x}", get_event_flag_address);
info!("set event flag address : 0x{:x}", set_event_flag_address);
}
}
else
Expand All @@ -95,4 +110,20 @@ impl Game for DarkSouls2ScholarOfTheFirstSin
fn as_any(&self) -> &dyn Any { self }

fn as_any_mut(&mut self) -> &mut dyn Any { self }
}

#[cfg(target_arch = "x86_64")]
unsafe extern "win64" fn read_event_flag_hook_fn(registers: *mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(scholar) = GameExt::get_game_ref::<DarkSouls2ScholarOfTheFirstSin>(app.game.deref())
{
let event_flag_id = (*registers).rdx as u32;
let value = (*registers).r8 as u8;

let mut guard = scholar.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// This file is part of the soulmemory-rs distribution (https://github.com/FrankvdStam/soulmemory-rs).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/soulmemory-rs/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::sync::{Arc, Mutex};
use mem_rs::prelude::ReadWrite;
use crate::games::DarkSouls2Vanilla;
Expand Down
54 changes: 39 additions & 15 deletions soulmemory-rs/src/games/dark_souls_2_vanilla/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// This file is part of the soulmemory-rs distribution (https://github.com/FrankvdStam/soulmemory-rs).
// Copyright (c) 2022 Frank van der Stam.
// https://github.com/FrankvdStam/soulmemory-rs/blob/main/LICENSE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

mod buffered_event_flags;

use std::any::Any;
Expand All @@ -14,8 +30,13 @@ use crate::games::{Game, GameExt};
use crate::games::traits::buffered_event_flags::{BufferedEventFlags, EventFlag};
use crate::util::{get_stack_u32, get_stack_u8};

#[cfg(target_arch = "x86")]
type FnGetEventFlag = unsafe extern "thiscall" fn(event_flag_man: u32, event_flag: u32) -> u8;

//This version exists only to make things compile easily for x64
#[cfg(target_arch = "x86_64")]
type FnGetEventFlag = unsafe extern "win64" fn(event_flag_man: u32, event_flag: u32) -> u8;

pub struct DarkSouls2Vanilla
{
process: Process,
Expand All @@ -30,8 +51,11 @@ impl DarkSouls2Vanilla
{
pub fn new() -> Self
{
#[cfg(target_arch = "x86")]
unsafe extern "thiscall" fn empty(_: u32, _: u32) -> u8 { 0 }

#[cfg(target_arch = "x86_64")]
unsafe extern "win64" fn empty(_: u32, _: u32) -> u8 { 0 }

DarkSouls2Vanilla
{
Expand Down Expand Up @@ -60,21 +84,6 @@ impl Game for DarkSouls2Vanilla

self.fn_get_event_flag = mem::transmute(get_event_flag_address);

unsafe extern "cdecl" fn set_event_flag_hook_fn(registers: *mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(vanilla) = GameExt::get_game_ref::<DarkSouls2Vanilla>(app.game.deref())
{
let value = get_stack_u8((*registers).esp, 0x8);
let event_flag_id = get_stack_u32((*registers).esp, 0x4);

let mut guard = vanilla.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}

let h = Hooker::new(set_event_flag_address, HookType::JmpBack(set_event_flag_hook_fn), CallbackOption::None, 0, HookFlags::empty());
self.set_event_flag_hook = Some(h.hook().unwrap());

Expand All @@ -95,4 +104,19 @@ impl Game for DarkSouls2Vanilla
fn as_any(&self) -> &dyn Any { self }

fn as_any_mut(&mut self) -> &mut dyn Any { self }
}

unsafe extern "cdecl" fn set_event_flag_hook_fn(registers: *mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(vanilla) = GameExt::get_game_ref::<DarkSouls2Vanilla>(app.game.deref())
{
let value = get_stack_u8((*registers).esp, 0x8);
let event_flag_id = get_stack_u32((*registers).esp, 0x4);

let mut guard = vanilla.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}
37 changes: 21 additions & 16 deletions soulmemory-rs/src/games/dark_souls_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#![allow(dead_code)]
#![allow(unused_imports)]


use std::any::Any;
use std::mem;
use std::ops::Deref;
Expand Down Expand Up @@ -90,21 +94,6 @@ impl Game for DarkSouls3

#[cfg(target_arch = "x86_64")]
{
unsafe extern "win64" fn set_event_flag_hook_fn(registers: *mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(game) = GameExt::get_game_ref::<DarkSouls3>(app.game.deref())
{
let event_flag_id = (*registers).rdx as u32;
let value = (*registers).r8 as u8;

let mut guard = game.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}

let h = Hooker::new(set_event_flag_address, HookType::JmpBack(set_event_flag_hook_fn), CallbackOption::None, 0, HookFlags::empty());
self.set_event_flag_hook = Some(h.hook().unwrap());
}
Expand All @@ -131,4 +120,20 @@ impl Game for DarkSouls3
self
}
fn as_any_mut(&mut self) -> &mut dyn Any { self }
}
}

#[cfg(target_arch = "x86_64")]
unsafe extern "win64" fn set_event_flag_hook_fn(registers: *mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(game) = GameExt::get_game_ref::<DarkSouls3>(app.game.deref())
{
let event_flag_id = (*registers).rdx as u32;
let value = (*registers).r8 as u8;

let mut guard = game.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}
32 changes: 16 additions & 16 deletions soulmemory-rs/src/games/dark_souls_prepare_to_die_edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,6 @@ impl Game for DarkSoulsPrepareToDieEdition
self.event_flag_man = self.process.scan_abs("event flags", "56 8B F1 8B 46 1C 50 A1 ? ? ? ? 32 C9", 8, vec![0, 0, 0])?;
let set_event_flag_address = self.process.scan_abs("set_event_flag", "80 b8 14 01 00 00 00 56 8b 74 24 08 74 ? 57 51 50", 0, Vec::new())?.get_base_address();

unsafe extern "cdecl" fn capture_the_flag(reg:*mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(ptde) = GameExt::get_game_ref::<DarkSoulsPrepareToDieEdition>(app.game.deref())
{
let value = get_stack_u8((*reg).esp, 0x8);
let event_flag_id = get_stack_u32((*reg).esp, 0x4);

let mut guard = ptde.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}

let h = Hooker::new(set_event_flag_address, HookType::JmpBack(capture_the_flag), CallbackOption::None, 0, HookFlags::empty());
self.set_event_flag_hook = Some(h.hook().unwrap());

Expand Down Expand Up @@ -176,4 +161,19 @@ fn get_event_flag_offset(even_flag_id: u32) -> (usize, usize)

let mask = 0x80000000 >> (number % 32);
return (offset, mask);
}
}

unsafe extern "cdecl" fn capture_the_flag(reg:*mut Registers, _:usize)
{
let instance = App::get_instance();
let app = instance.lock().unwrap();

if let Some(ptde) = GameExt::get_game_ref::<DarkSoulsPrepareToDieEdition>(app.game.deref())
{
let value = get_stack_u8((*reg).esp, 0x8);
let event_flag_id = get_stack_u32((*reg).esp, 0x4);

let mut guard = ptde.event_flags.lock().unwrap();
guard.push(EventFlag::new(chrono::offset::Local::now(), event_flag_id, value != 0));
}
}
Loading

0 comments on commit 770e164

Please sign in to comment.