Skip to content

Commit

Permalink
add test method for testing sending messages, and fix bf2042 message …
Browse files Browse the repository at this point in the history
…sender
  • Loading branch information
zefanjajobse committed Mar 19, 2023
1 parent f685041 commit f2d94ce
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 35 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ path = "src/bfv.rs"
[[bin]]
name = "bf2042"
path = "src/bf2042.rs"

[[bin]]
name = "test_msg"
path = "src/test_msg.rs"
67 changes: 32 additions & 35 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,34 @@ 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)
}

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
}

Expand All @@ -40,12 +42,14 @@ fn message_action(cfg: &structs::SeederConfig) {
sleep(Duration::from_secs(3));
let mut message: Vec<DXCode> = 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));
}
}

Expand All @@ -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));
Expand All @@ -78,22 +84,17 @@ fn bf2042_message_action(cfg: &structs::SeederConfig) {
sleep(Duration::from_secs(1));
let mut message: Vec<DXCode> = 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));
}
}
Expand All @@ -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);
Expand All @@ -116,18 +117,14 @@ pub fn send_message(cfg: &structs::SeederConfig, game_name: &str) {
}
}


pub fn is_running(game_name: &str) -> structs::GameInfo {
unsafe {
let window: Vec<u16> = OsStr::new(game_name)
.encode_wide()
.chain(once(0))
.collect();
let window: Vec<u16> = 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 {
is_running: window_handle != no_game,
game_process: window_handle,
}
}
}
}
24 changes: 24 additions & 0 deletions src/test_msg.rs
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit f2d94ce

Please sign in to comment.