Skip to content

Commit

Permalink
implement watchdog, support switch on/off
Browse files Browse the repository at this point in the history
  • Loading branch information
shevernitskiy committed Oct 1, 2023
1 parent 126e713 commit bed75df
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 26 deletions.
139 changes: 138 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde_derive = "1.0.164"
serde = "1.0.185"
static_init = "1.0.3"
dlopen2 = "0.6.1"
rdev = "0.5.3"

[target.'cfg(target_os = "windows")'.dependencies]
exe = "0.5.6"
Expand Down
10 changes: 9 additions & 1 deletion src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ use crate::config::CONFIG;
#[static_init::dynamic]
pub static DICTIONARY: Dictionary = Dictionary::new(&CONFIG.settings.dictionary);

#[allow(dead_code)]
pub struct Dictionary {
map: HashMap<String, Vec<u8>>,
path: &'static str,
}

impl Dictionary {
pub fn new(path: &String) -> Self {
pub fn new(path: &'static String) -> Self {
Self {
map: Dictionary::load(path).unwrap(),
path,
}
}

Expand All @@ -29,6 +32,11 @@ impl Dictionary {
self.map.capacity()
}

pub fn _reload(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.map = Self::load(self.path)?;
Ok(())
}

fn load(path: &str) -> Result<HashMap<String, Vec<u8>>, Box<dyn std::error::Error>> {
let file = std::fs::File::open(path)?;
let mut reader = DecodeReaderBytesBuilder::new().encoding(Some(WINDOWS_1251)).build(file);
Expand Down
50 changes: 33 additions & 17 deletions src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,39 @@ pub unsafe fn attach_all() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

pub unsafe fn detach_all() -> Result<(), Box<dyn std::error::Error>> {
if CONFIG.settings.enable_translation {
detach_string_copy_n()?;
detach_string_append_n()?;
detach_std_string_append()?;
detach_addst()?;
detach_addst_top()?;
detach_addst_flag()?;
}
if CONFIG.settings.enable_search {
detach_standardstringentry()?;
detach_simplify_string()?;
detach_upper_case_string()?;
detach_lower_case_string()?;
detach_capitalize_string_words()?;
detach_capitalize_string_first_word()?;
}
pub unsafe fn enable_all() -> Result<(), Box<dyn std::error::Error>> {
enable_string_copy_n()?;
enable_string_append_n()?;
enable_std_string_append()?;
enable_addst()?;
enable_addst_top()?;
enable_addst_flag()?;

enable_standardstringentry()?;
enable_simplify_string()?;
enable_upper_case_string()?;
enable_lower_case_string()?;
enable_capitalize_string_words()?;
enable_capitalize_string_first_word()?;

Ok(())
}

pub unsafe fn disable_all() -> Result<(), Box<dyn std::error::Error>> {
disable_string_copy_n()?;
disable_string_append_n()?;
disable_std_string_append()?;
disable_addst()?;
disable_addst_top()?;
disable_addst_flag()?;

disable_standardstringentry()?;
disable_simplify_string()?;
disable_upper_case_string()?;
disable_lower_case_string()?;
disable_capitalize_string_words()?;
disable_capitalize_string_first_word()?;

Ok(())
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod cxxstring;
mod dictionary;
mod hooks;
mod utils;
mod watchdog;

use log::LevelFilter;
use log::{error, info, trace};
Expand All @@ -24,6 +25,7 @@ extern "C" fn attach() {
// unsafe {
// crash::install();
// }
watchdog::install();
simple_logging::log_to_file(&CONFIG.settings.log_file, LevelFilter::Trace).unwrap();
if CONFIG.metadata.name != "dfint localization hook" {
error!("unable to find config file");
Expand All @@ -47,7 +49,7 @@ extern "C" fn attach() {
#[no_mangle]
extern "C" fn detach() {
unsafe {
match hooks::detach_all() {
match hooks::disable_all() {
_ => (),
};
}
Expand Down
28 changes: 22 additions & 6 deletions src/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ pub fn hook(args: TokenStream, input: TokenStream) -> TokenStream {
} = function;

let attach_ident = format_ident!("attach_{}", ident);
let detach_ident = format_ident!("detach_{}", ident);
let handle_ident = format_ident!("handle_{}", ident);
let enable_ident = format_ident!("enable_{}", ident);
let disable_ident = format_ident!("disable_{}", ident);
let ret_type = quote!(#output).to_string();
let inputs_unnamed = quote!(#inputs)
.to_string()
Expand All @@ -80,7 +81,18 @@ pub fn hook(args: TokenStream, input: TokenStream) -> TokenStream {
let mut attach = quote!(
pub unsafe fn #attach_ident() -> Result<(), Box<dyn std::error::Error>> {
let target = target();
#handle_ident.initialize(target, #ident)?.enable()?;
#handle_ident.initialize(target, #ident)?;
#enable_ident()?;
Ok(())
}

pub unsafe fn #enable_ident() -> Result<(), Box<dyn std::error::Error>> {
#handle_ident.enable()?;
Ok(())
}

pub unsafe fn #disable_ident() -> Result<(), Box<dyn std::error::Error>> {
#handle_ident.disable()?;
Ok(())
}
)
Expand Down Expand Up @@ -138,15 +150,19 @@ pub fn hook(args: TokenStream, input: TokenStream) -> TokenStream {
pub unsafe fn #attach_ident() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

pub unsafe fn #enable_ident() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

pub unsafe fn #disable_ident() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
)
.to_string();
}

let result = quote!(
pub unsafe fn #detach_ident() -> Result<(), Box<dyn std::error::Error>> {
#handle_ident.disable()?;
Ok(())
}
static_detour! { static #handle_ident: unsafe #abi fn() #output; }
#vis #unsafety #constness fn #ident(#inputs) #output #block
);
Expand Down
Loading

0 comments on commit bed75df

Please sign in to comment.