From 2d2e16cd93642b9a483ee1d9f78828f08a0cfaa8 Mon Sep 17 00:00:00 2001 From: Zefanja Jobse Date: Tue, 6 Jun 2023 13:44:14 +0200 Subject: [PATCH] add checking game config for keybinds in bf2042 --- Cargo.lock | 61 +++++++++++++++++++++++++++++++---------- Cargo.toml | 73 +++++++++++++++++++++++++------------------------- src/actions.rs | 62 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 140 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43dabf7..02698a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,7 @@ version = "0.1.0" dependencies = [ "chrono", "confy", + "dirs-next", "env_logger", "log", "serde", @@ -130,7 +131,7 @@ dependencies = [ "proc-macro2", "quote", "scratch", - "syn", + "syn 1.0.107", ] [[package]] @@ -147,7 +148,7 @@ checksum = "3e7e2adeb6a0d4a282e581096b06e1791532b7d576dcde5ccd9382acf55db8e6" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -159,6 +160,16 @@ dependencies = [ "dirs-sys", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + [[package]] name = "dirs-sys" version = "0.3.7" @@ -170,6 +181,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "env_logger" version = "0.10.0" @@ -348,18 +370,18 @@ checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" [[package]] name = "proc-macro2" -version = "1.0.49" +version = "1.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +checksum = "6aeca18b86b413c660b781aa319e4e2648a3e6f9eadc9b47e9038e6fe9f3451b" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.23" +version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488" dependencies = [ "proc-macro2", ] @@ -423,19 +445,19 @@ checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" [[package]] name = "serde" -version = "1.0.152" +version = "1.0.163" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2" [[package]] name = "serde_derive" -version = "1.0.152" +version = "1.0.163" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.18", ] [[package]] @@ -449,6 +471,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "termcolor" version = "1.1.3" @@ -475,7 +508,7 @@ checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", ] [[package]] @@ -543,7 +576,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 1.0.107", "wasm-bindgen-shared", ] @@ -565,7 +598,7 @@ checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.107", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 351b8e2..aa84cb0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,37 +1,38 @@ -[package] -name = "battlefield-anti-afk-script" -description = "" -version = "0.1.0" -edition = "2018" -authors = ["iiTzArcur "] - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -serde = "1.0.152" -serde_derive = "1.0.152" -winapi = { version = "0.3.9", features = ["winuser"] } -log = "0.4" -env_logger = "0.10.0" -chrono = "*" -confy = "0.5.1" - -[[bin]] -name = "bf4" -path = "src/bf4.rs" - -[[bin]] -name = "bf1" -path = "src/bf1.rs" - -[[bin]] -name = "bfv" -path = "src/bfv.rs" - -[[bin]] -name = "bf2042" -path = "src/bf2042.rs" - -[[bin]] -name = "test_msg" +[package] +name = "battlefield-anti-afk-script" +description = "" +version = "0.1.0" +edition = "2018" +authors = ["iiTzArcur "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = "1.0.163" +serde_derive = "1.0.163" +winapi = { version = "0.3.9", features = ["winuser"] } +log = "0.4" +env_logger = "0.10.0" +chrono = "*" +confy = "0.5.1" +dirs-next = "2.0.0" + +[[bin]] +name = "bf4" +path = "src/bf4.rs" + +[[bin]] +name = "bf1" +path = "src/bf1.rs" + +[[bin]] +name = "bfv" +path = "src/bfv.rs" + +[[bin]] +name = "bf2042" +path = "src/bf2042.rs" + +[[bin]] +name = "test_msg" path = "src/test_msg.rs" \ No newline at end of file diff --git a/src/actions.rs b/src/actions.rs index a2d808e..cb704dc 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,4 +1,6 @@ use std::ffi::OsStr; +use std::fs::File; +use std::io::{BufRead, BufReader}; use std::iter::once; use std::os::windows::prelude::OsStrExt; use std::ptr; @@ -101,6 +103,44 @@ fn bf2042_message_action(message_to_send: &str) { } } +fn get_config_key(chat: &str, defaults: u16) -> u16 { + let docs = dirs_next::document_dir(); + match docs { + Some(docs) => { + match File::open(format!( + "{}\\Battlefield 2042\\settings\\PROFSAVE_profile", + docs.to_str().unwrap() + )) { + Ok(f) => { + let f = BufReader::new(f); + for line in f.lines() { + let line = line.unwrap_or_default(); + if line.starts_with(&format!("GstKeyBinding.default.{}.0.button", chat)) { + return match line.split(' ').last().unwrap_or("").parse() { + Ok(key_code) => key_code, + Err(_) => { + log::error!("Keycode in config invalid, using defaults..."); + defaults + } + }; + } + } + log::error!("Keybind not found in config of game, using defaults..."); + defaults + } + Err(_) => { + log::error!("Cant open profsave of bf2042 at \"{}\\Battlefield 2042\\settings\\PROFSAVE_profile\", using defaults...", docs.to_str().unwrap()); + defaults + } + } + } + None => { + log::error!("Can't find documents folder, using defaults..."); + defaults + } + } +} + // https://gist.github.com/dretax/fe37b8baf55bc30e9d63 pub fn send_message( cfg: &structs::SeederConfig, @@ -116,6 +156,14 @@ pub fn send_message( SetForegroundWindow(game_info.game_process); ShowWindow(game_info.game_process, 9); sleep(Duration::from_millis(1808)); + let squad_key = match game_name == "Battlefield™ 2042" { + true => get_config_key("GstKeyBinding.default.ConceptSquadChat.0.button", 0x26), + false => 0x26, + }; + let team_key = match game_name == "Battlefield™ 2042" { + true => get_config_key("GstKeyBinding.default.ConceptTeamChat.0.button", 0x25), + false => 0x25, + }; match cfg.chat_type { structs::ChatType::Announce => { @@ -127,15 +175,17 @@ pub fn send_message( } structs::ChatType::Public => { if game_name == "Battlefield™ 2042" { - send_keys::key_enter(0x26, 50); - sleep(Duration::from_secs(1)); - message_action(current_message, 0x0F); + send_keys::key_enter(squad_key, 50); + sleep(Duration::from_secs(3)); + let tab_key = + get_config_key("GstKeyBinding.default.ConceptChat.1.button", 0x0F); + message_action(current_message, tab_key); } else { - message_action(current_message, 0x24); + message_action(current_message, squad_key); } } - structs::ChatType::Team => message_action(current_message, 0x25), - structs::ChatType::Squad => message_action(current_message, 0x26), + structs::ChatType::Team => message_action(current_message, team_key), + structs::ChatType::Squad => message_action(current_message, squad_key), } ShowWindow(game_info.game_process, 6);