Skip to content

Commit 2d2e16c

Browse files
committed
add checking game config for keybinds in bf2042
1 parent b6aa5b2 commit 2d2e16c

File tree

3 files changed

+140
-56
lines changed

3 files changed

+140
-56
lines changed

Cargo.lock

Lines changed: 47 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
[package]
2-
name = "battlefield-anti-afk-script"
3-
description = ""
4-
version = "0.1.0"
5-
edition = "2018"
6-
authors = ["iiTzArcur <[email protected]>"]
7-
8-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9-
10-
[dependencies]
11-
serde = "1.0.152"
12-
serde_derive = "1.0.152"
13-
winapi = { version = "0.3.9", features = ["winuser"] }
14-
log = "0.4"
15-
env_logger = "0.10.0"
16-
chrono = "*"
17-
confy = "0.5.1"
18-
19-
[[bin]]
20-
name = "bf4"
21-
path = "src/bf4.rs"
22-
23-
[[bin]]
24-
name = "bf1"
25-
path = "src/bf1.rs"
26-
27-
[[bin]]
28-
name = "bfv"
29-
path = "src/bfv.rs"
30-
31-
[[bin]]
32-
name = "bf2042"
33-
path = "src/bf2042.rs"
34-
35-
[[bin]]
36-
name = "test_msg"
1+
[package]
2+
name = "battlefield-anti-afk-script"
3+
description = ""
4+
version = "0.1.0"
5+
edition = "2018"
6+
authors = ["iiTzArcur <[email protected]>"]
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
serde = "1.0.163"
12+
serde_derive = "1.0.163"
13+
winapi = { version = "0.3.9", features = ["winuser"] }
14+
log = "0.4"
15+
env_logger = "0.10.0"
16+
chrono = "*"
17+
confy = "0.5.1"
18+
dirs-next = "2.0.0"
19+
20+
[[bin]]
21+
name = "bf4"
22+
path = "src/bf4.rs"
23+
24+
[[bin]]
25+
name = "bf1"
26+
path = "src/bf1.rs"
27+
28+
[[bin]]
29+
name = "bfv"
30+
path = "src/bfv.rs"
31+
32+
[[bin]]
33+
name = "bf2042"
34+
path = "src/bf2042.rs"
35+
36+
[[bin]]
37+
name = "test_msg"
3738
path = "src/test_msg.rs"

src/actions.rs

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::ffi::OsStr;
2+
use std::fs::File;
3+
use std::io::{BufRead, BufReader};
24
use std::iter::once;
35
use std::os::windows::prelude::OsStrExt;
46
use std::ptr;
@@ -101,6 +103,44 @@ fn bf2042_message_action(message_to_send: &str) {
101103
}
102104
}
103105

106+
fn get_config_key(chat: &str, defaults: u16) -> u16 {
107+
let docs = dirs_next::document_dir();
108+
match docs {
109+
Some(docs) => {
110+
match File::open(format!(
111+
"{}\\Battlefield 2042\\settings\\PROFSAVE_profile",
112+
docs.to_str().unwrap()
113+
)) {
114+
Ok(f) => {
115+
let f = BufReader::new(f);
116+
for line in f.lines() {
117+
let line = line.unwrap_or_default();
118+
if line.starts_with(&format!("GstKeyBinding.default.{}.0.button", chat)) {
119+
return match line.split(' ').last().unwrap_or("").parse() {
120+
Ok(key_code) => key_code,
121+
Err(_) => {
122+
log::error!("Keycode in config invalid, using defaults...");
123+
defaults
124+
}
125+
};
126+
}
127+
}
128+
log::error!("Keybind not found in config of game, using defaults...");
129+
defaults
130+
}
131+
Err(_) => {
132+
log::error!("Cant open profsave of bf2042 at \"{}\\Battlefield 2042\\settings\\PROFSAVE_profile\", using defaults...", docs.to_str().unwrap());
133+
defaults
134+
}
135+
}
136+
}
137+
None => {
138+
log::error!("Can't find documents folder, using defaults...");
139+
defaults
140+
}
141+
}
142+
}
143+
104144
// https://gist.github.com/dretax/fe37b8baf55bc30e9d63
105145
pub fn send_message(
106146
cfg: &structs::SeederConfig,
@@ -116,6 +156,14 @@ pub fn send_message(
116156
SetForegroundWindow(game_info.game_process);
117157
ShowWindow(game_info.game_process, 9);
118158
sleep(Duration::from_millis(1808));
159+
let squad_key = match game_name == "Battlefield™ 2042" {
160+
true => get_config_key("GstKeyBinding.default.ConceptSquadChat.0.button", 0x26),
161+
false => 0x26,
162+
};
163+
let team_key = match game_name == "Battlefield™ 2042" {
164+
true => get_config_key("GstKeyBinding.default.ConceptTeamChat.0.button", 0x25),
165+
false => 0x25,
166+
};
119167

120168
match cfg.chat_type {
121169
structs::ChatType::Announce => {
@@ -127,15 +175,17 @@ pub fn send_message(
127175
}
128176
structs::ChatType::Public => {
129177
if game_name == "Battlefield™ 2042" {
130-
send_keys::key_enter(0x26, 50);
131-
sleep(Duration::from_secs(1));
132-
message_action(current_message, 0x0F);
178+
send_keys::key_enter(squad_key, 50);
179+
sleep(Duration::from_secs(3));
180+
let tab_key =
181+
get_config_key("GstKeyBinding.default.ConceptChat.1.button", 0x0F);
182+
message_action(current_message, tab_key);
133183
} else {
134-
message_action(current_message, 0x24);
184+
message_action(current_message, squad_key);
135185
}
136186
}
137-
structs::ChatType::Team => message_action(current_message, 0x25),
138-
structs::ChatType::Squad => message_action(current_message, 0x26),
187+
structs::ChatType::Team => message_action(current_message, team_key),
188+
structs::ChatType::Squad => message_action(current_message, squad_key),
139189
}
140190

141191
ShowWindow(game_info.game_process, 6);

0 commit comments

Comments
 (0)