Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cross platform support #21

Merged
merged 10 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"rust-analyzer.linkedProjects": [
".\\src\\macro\\Cargo.toml",
".\\Cargo.toml"
".\\Cargo.toml",
"src/macro/Cargo.toml",
"Cargo.toml",
]
}
79 changes: 55 additions & 24 deletions Cargo.lock

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

24 changes: 15 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "hook"
name = "dfint_hook"
version = "0.1.0"
edition = "2021"

Expand All @@ -13,20 +13,26 @@ macro = { path = "src/macro" }
retour = { version = "0.3.1", features = ["static-detour"] }
log = "0.4.20"
simple-logging = "2.0.2"
winapi = { version = "^0.3.9", features = [
"minwindef",
"winnt",
"libloaderapi",
"windef",
"winuser",
] }
encoding_rs = "*"
encoding_rs_io = "*"
regex = "*"
toml = "0.8.0"
exe = "0.5.6"
backtrace = "0.3.69"
chrono = "0.4.26"
serde_derive = "1.0.164"
serde = "1.0.185"
static_init = "1.0.3"
dlopen2 = "0.6.1"

[target.'cfg(target_os = "windows")'.dependencies]
exe = "0.5.6"
winapi = { version = "^0.3.9", features = [
"minwindef",
"winnt",
"libloaderapi",
"windef",
"winuser",
] }

[target.'cfg(target_os = "linux")'.dependencies]
checksum = "0.2.1"
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# df-steam-hook-rs

[![Build](https://github.com/dfint/df-steam-hook-rs/actions/workflows/build.yml/badge.svg)](https://github.com/dfint/df-steam-hook-rs/actions/workflows/build.yml)

Re-implementation of [df-steam-hook](https://github.com/dfint/df-steam-hook) in Rust.
Re-implementation of [df-steam-hook](https://github.com/dfint/df-steam-hook) in
Rust.

Supports:

- Windows (classic/steam/itch.io)
- Linux (classic/steam/itch.io)

Implemented:

- config/offsets files
- crash reports
- dictionary from csv
- translation hooks
- enter string hooks (search)
Expand Down
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tab_spaces = 2
chain_width = 100
chain_width = 120
max_width = 120
120 changes: 66 additions & 54 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ use std::error::Error;
use std::path::Path;

use crate::utils;
use exe::{VecPE, PE};
use static_init::dynamic;

static EXE_FILE: &str = "./Dwarf Fortress.exe";
static CONFIG_FILE: &str = "./dfint_data/dfint_config.toml";
static OFFSETS_DIR: &str = "./dfint_data/offsets/";
#[cfg(target_os = "windows")]
const EXE_FILE: &'static str = "./Dwarf Fortress.exe";

#[dynamic]
pub static CONFIG: Config = Config::new().unwrap();
#[cfg(target_os = "linux")]
const EXE_FILE: &'static str = "./dwarfort";

const CONFIG_FILE: &'static str = "./dfint_data/dfint_config.toml";
const OFFSETS_DIR: &'static str = "./dfint_data/offsets/";

#[static_init::dynamic]
pub static CONFIG: Config = Config::new().unwrap();

pub struct Config {
pub metadata: ConfigMetadata,
pub settings: Settings,
pub offset: OffsetsValues,
pub offset_metadata: OffsetsMetadata,
pub symbol: Option<SymbolsValues>,
pub hook_version: String,
}

Expand Down Expand Up @@ -49,6 +52,7 @@ pub struct Settings {
pub struct Offsets {
pub metadata: OffsetsMetadata,
pub offsets: OffsetsValues,
pub symbols: Option<SymbolsValues>,
}

#[derive(Deserialize)]
Expand All @@ -60,87 +64,95 @@ pub struct OffsetsMetadata {

#[derive(Deserialize)]
pub struct OffsetsValues {
pub string_copy: usize,
pub string_copy_n: usize,
pub string_append: usize,
pub string_append_0: usize,
pub string_append_n: usize,
pub convert_ulong_to_string: usize,
pub addst: usize,
pub addst_top: usize,
pub addcoloredst: usize,
pub addst_flag: usize,
pub addst_template: Option<usize>,
pub standardstringentry: usize,
pub simplify_string: usize,
pub upper_case_string: usize,
pub lower_case_string: usize,
pub capitalize_string_words: usize,
pub capitalize_string_first_word: usize,
pub addchar: usize,
pub addchar_top: usize,
pub add_texture: usize,
pub gps_allocate: usize,
pub cleanup_arrays: usize,
pub screen_to_texid: usize,
pub screen_to_texid_top: usize,
pub loading_world_new_game_loop: usize,
pub loading_world_continuing_game_loop: usize,
pub loading_world_start_new_game_loop: usize,
pub menu_interface_loop: usize,
pub keybinding: Option<u32>,
pub string_copy_n: Option<usize>,
pub string_append_n: Option<usize>,
pub addst: Option<usize>,
pub addst_top: Option<usize>,
pub addst_flag: Option<usize>,
pub standardstringentry: Option<usize>,
pub simplify_string: Option<usize>,
pub upper_case_string: Option<usize>,
pub lower_case_string: Option<usize>,
pub capitalize_string_words: Option<usize>,
pub capitalize_string_first_word: Option<usize>,
pub utf_input: Option<usize>,
}

#[derive(Deserialize)]
pub struct SymbolsValues {
pub addst: Option<Vec<String>>,
pub addst_top: Option<Vec<String>>,
pub addst_flag: Option<Vec<String>>,
pub standardstringentry: Option<Vec<String>>,
pub simplify_string: Option<Vec<String>>,
pub upper_case_string: Option<Vec<String>>,
pub lower_case_string: Option<Vec<String>>,
pub capitalize_string_words: Option<Vec<String>>,
pub capitalize_string_first_word: Option<Vec<String>>,
pub std_string_append: Option<Vec<String>>,
pub enabler: Option<Vec<String>>,
}

impl Config {
pub fn new() -> Result<Self, Box<dyn Error>> {
let pe_timestamp = Self::pe_timestamp(Path::new(EXE_FILE))?;
let checksum = Self::checksum(Path::new(EXE_FILE))?;
let main_config = Self::parse_toml::<MainConfig>(Path::new(CONFIG_FILE))?;
match Self::walk_offsets(Path::new(OFFSETS_DIR), pe_timestamp) {
match Self::walk_offsets(Path::new(OFFSETS_DIR), checksum) {
Ok(offsets) => Ok(Self {
metadata: main_config.metadata,
settings: main_config.settings,
offset: offsets.offsets,
offset_metadata: offsets.metadata,
symbol: offsets.symbols,
hook_version: match option_env!("HOOK_VERSION") {
Some(version) => String::from(version),
None => String::from("not-defined"),
},
}),
Err(_) => Err("Config Error".into()),
Err(e) => Err(format!("Config error {:?}", e).into()),
}
}

fn pe_timestamp(path: &Path) -> Result<u32, Box<dyn Error>> {
#[cfg(target_os = "windows")]
fn checksum(path: &Path) -> Result<u32, Box<dyn Error>> {
use exe::{VecPE, PE};
let pefile = VecPE::from_disk_file(path)?;
Ok(pefile.get_nt_headers_64()?.file_header.time_date_stamp)
}

fn walk_offsets(path: &Path, target_timestamp: u32) -> Result<Offsets, Box<dyn Error>> {
#[cfg(target_os = "linux")]
fn checksum(path: &Path) -> Result<u32, Box<dyn Error>> {
let mut crc = checksum::crc::Crc::new(path.to_str().unwrap());
match crc.checksum() {
Ok(checksum) => Ok(checksum.crc32),
Err(e) => Err(format!("Checksum error {:?}", e).into()),
}
}

fn walk_offsets(path: &Path, target_checksum: u32) -> Result<Offsets, Box<dyn Error>> {
for entry in std::fs::read_dir(path)? {
let entry = entry?;
let pentry = entry.path();
if !pentry.is_file() {
continue;
}
let offsets = Self::parse_toml::<Offsets>(&pentry)?;
if offsets.metadata.checksum == target_timestamp {
if offsets.metadata.checksum == target_checksum {
return Ok(offsets);
}
}

unsafe {
utils::message_box(
format!(
"unable to find offsets file for current version of DF\nchecksum: 0x{:x}",
target_timestamp
)
.as_str(),
"dfint hook error",
utils::MessageIconType::Error,
);
}
utils::message_box(
format!(
"unable to find offsets file for current version of DF\nchecksum: 0x{:x}",
target_checksum
)
.as_str(),
"dfint hook error",
utils::MessageIconType::Error,
);

std::process::exit(2);
// Err("Unable to find offsets file".into())
}

fn parse_toml<T: for<'de> serde::Deserialize<'de>>(path: &Path) -> Result<T, Box<dyn Error>> {
Expand Down
2 changes: 2 additions & 0 deletions src/cxxset.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use std::alloc::{dealloc, Layout};

#[repr(C)]
Expand Down
Loading