-
Notifications
You must be signed in to change notification settings - Fork 10
/
lib.rs
216 lines (184 loc) · 6.79 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// johndisandonato's Elden Ring Practice Tool
// Copyright (C) 2022-2024 johndisandonato <https://github.com/veeenu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
mod config;
mod practice_tool;
mod widgets;
pub mod update;
pub mod util;
use std::ffi::c_void;
use std::time::{Duration, Instant};
use std::{mem, ptr, thread};
use hudhook::hooks::dx12::ImguiDx12Hooks;
use hudhook::tracing::error;
use hudhook::{eject, Hudhook};
use libeldenring::codegen::base_addresses::BaseAddresses;
use libeldenring::version;
use once_cell::sync::Lazy;
use practice_tool::PracticeTool;
use windows::core::{s, w, GUID, HRESULT, PCWSTR};
use windows::Win32::Foundation::{HINSTANCE, MAX_PATH};
use windows::Win32::System::LibraryLoader::{GetModuleHandleW, GetProcAddress, LoadLibraryW};
use windows::Win32::System::Memory::{
VirtualProtect, PAGE_EXECUTE_READWRITE, PAGE_PROTECTION_FLAGS,
};
use windows::Win32::System::SystemInformation::GetSystemDirectoryW;
use windows::Win32::System::SystemServices::DLL_PROCESS_ATTACH;
use windows::Win32::UI::Input::KeyboardAndMouse::{GetAsyncKeyState, VK_RSHIFT};
type FDirectInput8Create = unsafe extern "stdcall" fn(
hinst: HINSTANCE,
dwversion: u32,
riidltf: *const GUID,
ppvout: *mut *mut c_void,
punkouter: HINSTANCE,
) -> HRESULT;
static DIRECTINPUT8CREATE: Lazy<FDirectInput8Create> = Lazy::new(|| unsafe {
let mut dinput8_path = [0u16; MAX_PATH as usize];
let count = GetSystemDirectoryW(Some(&mut dinput8_path)) as usize;
// If count == 0, this will be fun
ptr::copy_nonoverlapping(w!("\\dinput8.dll").0, dinput8_path[count..].as_mut_ptr(), 12);
let dinput8 = LoadLibraryW(PCWSTR(dinput8_path.as_ptr())).unwrap();
let directinput8create = mem::transmute::<
Option<unsafe extern "system" fn() -> isize>,
unsafe extern "stdcall" fn(
HINSTANCE,
u32,
*const GUID,
*mut *mut c_void,
HINSTANCE,
) -> HRESULT,
>(GetProcAddress(dinput8, s!("DirectInput8Create")));
apply_no_logo();
directinput8create
});
#[no_mangle]
unsafe extern "stdcall" fn DirectInput8Create(
hinst: HINSTANCE,
dwversion: u32,
riidltf: *const GUID,
ppvout: *mut *mut c_void,
punkouter: HINSTANCE,
) -> HRESULT {
(DIRECTINPUT8CREATE)(hinst, dwversion, riidltf, ppvout, punkouter)
}
unsafe fn apply_no_logo() {
let module_base = GetModuleHandleW(None).unwrap();
let offset = BaseAddresses::from(version::get_version()).func_remove_intro_screens;
let ptr = (module_base.0 as usize + offset) as *mut [u8; 2];
let mut old = PAGE_PROTECTION_FLAGS(0);
if *ptr == [0x74, 0x53] && VirtualProtect(ptr as _, 2, PAGE_EXECUTE_READWRITE, &mut old).is_ok()
{
(*ptr) = [0x90, 0x90];
VirtualProtect(ptr as _, 2, old, &mut old).ok();
}
}
unsafe fn apply_event_patch() {
let module_base = GetModuleHandleW(None).unwrap();
let offset_1 = BaseAddresses::from(version::get_version()).event_patch1;
let offset_2 = BaseAddresses::from(version::get_version()).event_patch2;
let ptr_1 = (module_base.0 as usize + offset_1) as *mut [u8; 2];
let mut old_1 = PAGE_PROTECTION_FLAGS(0);
if *ptr_1 == [0x32, 0xC0]
&& VirtualProtect(ptr_1 as _, 2, PAGE_EXECUTE_READWRITE, &mut old_1).is_ok()
{
(*ptr_1) = [0xB0, 0x01];
VirtualProtect(ptr_1 as _, 2, old_1, &mut old_1).ok();
}
let ptr_2 = (module_base.0 as usize + offset_2) as *mut [u8; 2];
let mut old_2 = PAGE_PROTECTION_FLAGS(0);
if *ptr_2 == [0x32, 0xC0]
&& VirtualProtect(ptr_2 as _, 2, PAGE_EXECUTE_READWRITE, &mut old_2).is_ok()
{
(*ptr_2) = [0xB0, 0x01];
VirtualProtect(ptr_2 as _, 2, old_2, &mut old_2).ok();
}
}
unsafe fn apply_font_patch() {
let module_base = GetModuleHandleW(None).unwrap();
let offset = BaseAddresses::from(version::get_version()).font_patch;
let ptr = (module_base.0 as usize + offset) as *mut u8;
let mut old = PAGE_PROTECTION_FLAGS(0);
if *ptr == 0x48 && VirtualProtect(ptr as _, 1, PAGE_EXECUTE_READWRITE, &mut old).is_ok() {
(*ptr) = 0xC3;
VirtualProtect(ptr as _, 1, old, &mut old).ok();
}
}
fn start_practice_tool(hmodule: HINSTANCE) {
let practice_tool = PracticeTool::new();
unsafe {
apply_event_patch(); // Needed for event draw
apply_font_patch(); // Needed for event draw & altimeter
}
if let Err(e) = Hudhook::builder()
.with::<ImguiDx12Hooks>(practice_tool)
.with_hmodule(hmodule)
.build()
.apply()
{
error!("Couldn't apply hooks: {e:?}");
eject();
}
}
fn await_rshift() -> bool {
let duration_threshold = Duration::from_secs(2);
let check_window = Duration::from_secs(10);
let poll_interval = Duration::from_millis(100);
let start_time = Instant::now();
let mut key_down_start: Option<Instant> = None;
while start_time.elapsed() < check_window {
let state = unsafe { GetAsyncKeyState(VK_RSHIFT.0 as i32) };
let key_down = state < 0;
match (key_down, key_down_start) {
(true, None) => {
key_down_start = Some(Instant::now());
},
(true, Some(start)) => {
if start.elapsed() >= duration_threshold {
return true;
}
},
(false, _) => {
key_down_start = None;
},
}
thread::sleep(poll_interval);
}
false
}
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "stdcall" fn DllMain(hmodule: HINSTANCE, reason: u32, _: *mut c_void) -> bool {
if reason == DLL_PROCESS_ATTACH {
if version::check_version().is_err() {
return false;
}
thread::spawn(move || {
apply_no_logo();
if util::get_dll_path()
.and_then(|path| {
path.file_name().map(|s| s.to_string_lossy().to_lowercase() == "dinput8.dll")
})
.unwrap_or(false)
{
if await_rshift() {
start_practice_tool(hmodule)
}
} else {
start_practice_tool(hmodule)
}
});
}
true
}