From f2d94cec609464d72c633e287a7a90ad1d15bd38 Mon Sep 17 00:00:00 2001 From: Zefanja Jobse Date: Sun, 19 Mar 2023 14:06:41 +0100 Subject: [PATCH] add test method for testing sending messages, and fix bf2042 message sender --- Cargo.toml | 4 +++ src/actions.rs | 67 +++++++++++++++++++++++-------------------------- src/test_msg.rs | 24 ++++++++++++++++++ 3 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 src/test_msg.rs diff --git a/Cargo.toml b/Cargo.toml index b28d0e2..351b8e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,7 @@ 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 05a78e8..6b474d9 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -5,10 +5,12 @@ use std::ptr; use std::thread::sleep; use std::time::Duration; use winapi::shared::windef::HWND__; -use winapi::um::winuser::{FindWindowW, SetForegroundWindow, ShowWindow, SendMessageW, GetForegroundWindow}; +use winapi::um::winuser::{ + FindWindowW, GetForegroundWindow, SendMessageW, SetForegroundWindow, ShowWindow, +}; +use crate::chars::{char_to_dxcodes, DXCode}; use crate::{send_keys, structs}; -use crate::chars::{DXCode, char_to_dxcodes}; fn make_l_param(lo_word: i32, hi_word: i32) -> i32 { (hi_word << 16) | (lo_word & 0xffff) @@ -16,21 +18,21 @@ fn make_l_param(lo_word: i32, hi_word: i32) -> i32 { pub fn anti_afk(game_name: &str, mut run_once_no_game: bool) -> bool { let game_info = is_running(game_name); - if game_info.is_running { - unsafe { - let current_forground_window = GetForegroundWindow(); - let l_param = make_l_param(20, 20); - SendMessageW(game_info.game_process, 0x201, 0, l_param as isize); - SendMessageW(game_info.game_process, 0x202, 0, l_param as isize); - SetForegroundWindow(current_forground_window); - // reset no game check - run_once_no_game = true; - } - log::info!("Running anti-idle for {}.", game_name); - } else if run_once_no_game { - log::info!("No game found, idleing..."); - run_once_no_game = false; + if game_info.is_running { + unsafe { + let current_forground_window = GetForegroundWindow(); + let l_param = make_l_param(20, 20); + SendMessageW(game_info.game_process, 0x201, 0, l_param as isize); + SendMessageW(game_info.game_process, 0x202, 0, l_param as isize); + SetForegroundWindow(current_forground_window); + // reset no game check + run_once_no_game = true; } + log::info!("Running anti-idle for {}.", game_name); + } else if run_once_no_game { + log::info!("No game found, idleing..."); + run_once_no_game = false; + } run_once_no_game } @@ -40,12 +42,14 @@ fn message_action(cfg: &structs::SeederConfig) { sleep(Duration::from_secs(3)); let mut message: Vec = Vec::new(); for char in cfg.message.chars() { - if let Some(dx) = char_to_dxcodes(char) { message.push(dx) } + if let Some(dx) = char_to_dxcodes(char) { + message.push(dx) + } } send_keys::send_string(message); sleep(Duration::from_secs(1)); send_keys::key_enter(0x1C, 8); // ENTER - sleep(Duration::from_secs(1)); + sleep(Duration::from_secs(1)); } } @@ -66,10 +70,12 @@ fn bf2042_message_action(cfg: &structs::SeederConfig) { // println!("Click!"); send_keys::key_enter(0x39, 80); // SPACE sleep(Duration::from_secs(1)); + // println!("Move to top item for broadcast menu!"); + send_keys::spam_keys(0xC8, 80, 5); // UP + sleep(Duration::from_secs(1)); // println!("broadcast menu"); send_keys::key_enter(0x39, 80); // SPACE sleep(Duration::from_secs(1)); - // println!("fill in"); send_keys::spam_keys(0xC8, 8, 2); // UP sleep(Duration::from_secs(1)); @@ -78,22 +84,17 @@ fn bf2042_message_action(cfg: &structs::SeederConfig) { sleep(Duration::from_secs(1)); let mut message: Vec = Vec::new(); for char in cfg.message.chars() { - if let Some(dx) = char_to_dxcodes(char) { message.push(dx) } + if let Some(dx) = char_to_dxcodes(char) { + message.push(dx) + } } send_keys::send_string(message); sleep(Duration::from_secs(1)); // println!("done with message"); send_keys::key_enter(0x1C, 80); // ENTER sleep(Duration::from_secs(1)); - // println!("done button"); - send_keys::key_enter(0xD0, 80); // DOWN - sleep(Duration::from_secs(1)); - // println!("broadcast!"); - send_keys::key_enter(0x39, 80); // SPACE - sleep(Duration::from_secs(1)); - // println!("Back to spawn screen"); - send_keys::spam_keys(0x01, 8, 2); // ESC + send_keys::spam_keys(0x01, 8, 3); // ESC sleep(Duration::from_secs(1)); } } @@ -106,7 +107,7 @@ pub fn send_message(cfg: &structs::SeederConfig, game_name: &str) { SetForegroundWindow(game_info.game_process); ShowWindow(game_info.game_process, 9); sleep(Duration::from_millis(1808)); - if game_name.is_empty() { + if game_name == "Battlefield™ 2042" { bf2042_message_action(cfg); } else { message_action(cfg); @@ -116,13 +117,9 @@ pub fn send_message(cfg: &structs::SeederConfig, game_name: &str) { } } - pub fn is_running(game_name: &str) -> structs::GameInfo { unsafe { - let window: Vec = OsStr::new(game_name) - .encode_wide() - .chain(once(0)) - .collect(); + let window: Vec = OsStr::new(game_name).encode_wide().chain(once(0)).collect(); let window_handle = FindWindowW(std::ptr::null_mut(), window.as_ptr()); let no_game: *mut HWND__ = ptr::null_mut(); structs::GameInfo { @@ -130,4 +127,4 @@ pub fn is_running(game_name: &str) -> structs::GameInfo { game_process: window_handle, } } -} \ No newline at end of file +} diff --git a/src/test_msg.rs b/src/test_msg.rs new file mode 100644 index 0000000..0e5c060 --- /dev/null +++ b/src/test_msg.rs @@ -0,0 +1,24 @@ +mod actions; +mod chars; +mod send_keys; +mod shared_main; +mod structs; + +fn main() { + let cfg: structs::SeederConfig = match confy::load_path("config.txt") { + Ok(config) => config, + Err(e) => { + println!("error in config.txt: {}", e); + println!("changing back to default.."); + structs::SeederConfig { + send_messages: true, + message: "testmessage1".into(), + message_start_time_utc: "12:00".into(), + message_stop_time_utc: "23:00".into(), + message_timeout_mins: 8, + } + } + }; + confy::store_path("config.txt", cfg.clone()).unwrap(); + actions::send_message(&cfg, "Battlefield™ 2042"); +}