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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Lodestone client — injected into Minecraft, drawn inside its own window.
//!
//! Flow:
//! DllMain spawns a thread so the loader lock is released at once
//! init waits for glfw.dll, then hooks glfwSwapBuffers
//! swap_buffers runs once per frame, on the game's render thread:
//! - first call sets up the window hook, GL and JNI
//! - applies the enabled modules through JNI
//! - draws the menu into the frame the game just built
//! - calls the real glfwSwapBuffers
//!
//! Running on the render thread is what makes the JNI side simple: it is a
//! Java thread, already attached, and the game is not concurrently mutating the
//! objects we touch.
#![cfg(windows)]
pub mod blocks;
pub mod capture;
pub mod config;
pub mod cheats;
pub mod hook;
pub mod input;
pub mod jni;
pub mod mc;
pub mod overlay;
pub mod state;
use std::ffi::c_void;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::OnceLock;
use windows_sys::Win32::Foundation::{HMODULE, HWND};
use windows_sys::Win32::System::LibraryLoader::{
DisableThreadLibraryCalls, GetModuleHandleA, GetProcAddress,
};
use windows_sys::Win32::System::Threading::CreateThread;
use crate::jni::Jni;
use crate::mc::{Mc, World};
static HOOK: OnceLock<hook::Hook> = OnceLock::new();
static MODULE: std::sync::atomic::AtomicIsize = std::sync::atomic::AtomicIsize::new(0);
static UNLOADING: AtomicBool = AtomicBool::new(false);
/// How many threads are currently executing inside our detour. The module must
/// not be freed while this is above zero.
static IN_DETOUR: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
static FRAMES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
// ---------------------------------------------------------------------------
// entry point
// ---------------------------------------------------------------------------
#[no_mangle]
pub extern "system" fn DllMain(module: HMODULE, reason: u32, _reserved: *mut c_void) -> i32 {
const DLL_PROCESS_ATTACH: u32 = 1;
if reason == DLL_PROCESS_ATTACH {
MODULE.store(module as isize, Ordering::SeqCst);
// SAFETY: both calls are documented as safe from DllMain, and the
// thread body does all the real work outside the loader lock.
unsafe {
DisableThreadLibraryCalls(module);
CreateThread(
std::ptr::null(),
0,
Some(init),
std::ptr::null_mut(),
0,
std::ptr::null_mut(),
);
}
}
1
}
pub fn log(msg: &str) {
use std::io::Write;
if let Ok(mut f) = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open("C:\\lodestone\\client.log")
{
let _ = writeln!(f, "{msg}");
}
}
unsafe extern "system" fn init(_: *mut c_void) -> u32 {
log("--- lodestone client starting ---");
// A panic inside the render hook would otherwise vanish into catch_unwind.
std::panic::set_hook(Box::new(|info| {
log(&format!("PANIC: {info}"));
}));
// The game loads GLFW early, but not necessarily before us.
let mut glfw = std::ptr::null_mut();
for _ in 0..600 {
glfw = GetModuleHandleA(c"glfw.dll".as_ptr() as *const u8);
if !glfw.is_null() {
break;
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
if glfw.is_null() {
log("glfw.dll never appeared — giving up");
return 1;
}
let Some(swap) = GetProcAddress(glfw, c"glfwSwapBuffers".as_ptr() as *const u8) else {
log("glfw.dll does not export glfwSwapBuffers");
return 1;
};
log(&format!("glfwSwapBuffers at {:p}", swap as *const c_void));
let hooked = match hook::install(swap as *mut c_void, swap_buffers as *const c_void) {
Ok(h) => {
let _ = HOOK.set(h);
log("hook installed");
state::with(|s| s.status = "hooked".into());
true
}
Err(e) => {
log(&format!("hook failed: {e}"));
state::with(|s| s.status = format!("hook failed: {e}"));
false
}
};
// Watchdog: the loader drops a marker file to ask us to leave, which is how
// a rebuild gets the old copy out of the way without restarting the game.
loop {
std::thread::sleep(std::time::Duration::from_millis(250));
if std::path::Path::new(UNLOAD_MARKER).exists() {
if hooked {
// Let the render thread unwind the hooks in the right order.
state::with(|s| s.eject = true);
} else {
log("unload marker seen; nothing was hooked");
}
return 0;
}
}
}
const UNLOAD_MARKER: &str = "C:\\lodestone\\unload";
// ---------------------------------------------------------------------------
// per-frame
// ---------------------------------------------------------------------------
/// Everything that may only be touched from the render thread.
struct Render {
overlay: overlay::Overlay,
jni: Jni,
mc: Option<Mc>,
world: Option<World>,
blocks: Option<blocks::Blocks>,
last_base_scan: std::time::Instant,
saved: cheats::Saved,
window: *mut c_void,
hwnd: HWND,
menu_was_open: bool,
saved_cursor_mode: i32,
}
/// Only ever read or written inside `swap_buffers`, which the game calls from a
/// single thread.
static mut RENDER: Option<Render> = None;
unsafe extern "C" fn swap_buffers(window: *mut c_void) {
IN_DETOUR.fetch_add(1, Ordering::SeqCst);
if !UNLOADING.load(Ordering::SeqCst) {
let n = FRAMES.fetch_add(1, Ordering::Relaxed);
if n % 600 == 0 {
log(&format!("frame {n}"));
}
// A panic must never unwind into the game's C frame.
let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| frame(window)));
}
if let Some(h) = HOOK.get() {
let original: unsafe extern "C" fn(*mut c_void) = std::mem::transmute(h.trampoline);
original(window);
}
IN_DETOUR.fetch_sub(1, Ordering::SeqCst);
}
unsafe fn frame(window: *mut c_void) {
let slot = &raw mut RENDER;
if (*slot).is_none() {
match setup(window) {
Ok(r) => {
*slot = Some(r);
log("render state ready");
}
Err(e) => {
log(&format!("setup failed: {e}"));
state::with(|s| s.status = e.clone());
// Try again next frame: the world may not exist yet.
return;
}
}
}
let Some(r) = (*slot).as_mut() else { return };
// Cursor: while the menu is open the game must release the mouse, and get
// it back exactly as it was when the menu closes.
let open = state::with(|s| s.menu_open).unwrap_or(false);
if open != r.menu_was_open {
if open {
r.saved_cursor_mode = glfw::get_input_mode(r.window, glfw::CURSOR);
glfw::set_input_mode(r.window, glfw::CURSOR, glfw::CURSOR_NORMAL);
} else if r.saved_cursor_mode != 0 {
glfw::set_input_mode(r.window, glfw::CURSOR, r.saved_cursor_mode);
}
r.menu_was_open = open;
}
// Modules, inside a local frame so nothing leaks.
if let Some(mcx) = r.mc.as_ref() {
if let Some(_guard) = r.jni.frame(256) {
let cfg = state::with(|s| s.cfg.clone());
if let Some(cfg) = cfg {
let mut game = cheats::apply(
&r.jni,
mcx,
r.world.as_ref(),
r.blocks.as_ref(),
&cfg,
&mut r.saved,
);
// Blocks are scanned incrementally, so this only costs a slice
// of the volume per frame.
if let (Some(scan), true) = (r.blocks.as_mut(), game.in_world) {
if let Some(instance) = mcx.instance(&r.jni) {
if let Some(level) = mcx.level(&r.jni, instance) {
// Ores have to be swept; a slice per frame.
if cfg.esp.xray {
scan.step_ores(
&r.jni,
level,
game.pos,
cfg.esp.block_radius as i32,
&cfg.esp.xray_selected,
// Each position is a Java allocation and
// three calls; this is a per-frame budget,
// so keep it modest.
1024,
);
game.blocks = scan.ores.clone();
}
// Block entities come from a list the client keeps,
// so this is cheap enough to redo periodically
// rather than continuously.
if cfg.esp.containers || cfg.esp.base_finder {
let due = r.last_base_scan.elapsed().as_secs_f32() > 1.0;
if due {
r.last_base_scan = std::time::Instant::now();
scan.scan_block_entities(
&r.jni,
level,
game.pos,
cfg.esp.base_chunk_radius as i32,
cfg.esp.base_finder && !cfg.esp.containers,
);
}
game.base_hits = scan.entities.clone();
}
}
}
}
state::with(|s| s.game = game);
}
}
}
let (w, h) = glfw::framebuffer_size(r.window);
if w > 0 && h > 0 {
let fps = r.overlay.fps();
state::with(|s| {
s.game.fps = fps;
r.overlay.draw(w, h, s);
});
}
if state::with(|s| s.eject).unwrap_or(false) {
log("unload requested");
unload(r);
}
}
unsafe fn setup(window: *mut c_void) -> Result<Render, String> {
let hwnd = glfw::win32_window(window);
if hwnd == 0 {
return Err("glfwGetWin32Window returned null".into());
}
capture::set_window(hwnd as HWND);
if !input::install(hwnd as HWND) {
log("window procedure hook failed");
}
let overlay = overlay::Overlay::new()?;
log("gl painter created");
let vm = jni::java_vm().ok_or("no JVM in this process")?;
let jni = Jni::current(vm).ok_or("this thread is not attached to the JVM")?;
log("jni environment acquired");
let mut world = None;
let mut block_scan = None;
let mc = match Mc::resolve(&jni) {
Ok(m) => {
if m.missing.is_empty() {
log("all minecraft names resolved");
} else {
log(&format!("unresolved: {}", m.missing.join(", ")));
}
let mut missing = m.missing.clone();
world = World::resolve(&jni, &m, &mut missing);
block_scan = blocks::Blocks::resolve(&jni, &mut missing);
if block_scan.is_none() {
log("block scanning unavailable: x-ray and container esp are off");
}
if world.is_none() {
log("world bindings unavailable: combat and ESP are off");
}
if !missing.is_empty() {
log(&format!("unresolved: {}", missing.join(", ")));
}
state::with(|s| {
s.missing = missing;
s.status = "ready".into();
});
Some(m)
}
Err(e) => {
log(&format!("minecraft bindings failed: {e}"));
state::with(|s| s.status = format!("bindings: {e}"));
None
}
};
// Pick up settings from the last session, if there are any.
match config::load_into_shared() {
Ok(true) => log("config loaded"),
Ok(false) => {}
Err(e) => log(&format!("config load failed: {e}")),
}
Ok(Render {
overlay,
jni,
mc,
world,
blocks: block_scan,
last_base_scan: std::time::Instant::now()
- std::time::Duration::from_secs(5),
saved: cheats::Saved::default(),
window,
hwnd: hwnd as HWND,
menu_was_open: false,
saved_cursor_mode: 0,
})
}
/// Stop the client: hooks out, game state restored, nothing drawn.
///
/// It deliberately does **not** unmap the module. A DLL that patched a window
/// procedure, installed a panic hook and left Rust thread-locals behind cannot
/// prove that nothing in the process still points into its code, and unmapping
/// it while one pointer survives crashes the game — which is exactly what an
/// earlier version of this did. A few megabytes of idle address space is a
/// much better trade. Restart the game to be rid of it entirely.
unsafe fn unload(r: &mut Render) {
UNLOADING.store(true, Ordering::SeqCst);
if let Some(mcx) = r.mc.as_ref() {
if let Some(_guard) = r.jni.frame(32) {
cheats::restore(&r.jni, mcx, r.world.as_ref(), &mut r.saved);
}
}
capture::apply(false);
let restored = input::remove(r.hwnd);
if r.saved_cursor_mode != 0 {
glfw::set_input_mode(r.window, glfw::CURSOR, r.saved_cursor_mode);
}
if let Some(h) = HOOK.get() {
h.remove();
}
state::with(|s| {
s.eject = false;
s.menu_open = false;
s.status = "unloaded".into();
});
log(&format!(
"unloaded: hooks removed, settings restored, window procedure {} — module stays resident",
if restored { "restored" } else { "kept (something subclassed after us)" }
));
}
// ---------------------------------------------------------------------------
// the few GLFW entry points we need
// ---------------------------------------------------------------------------
mod glfw {
use std::ffi::c_void;
use windows_sys::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
pub const CURSOR: i32 = 0x0003_3001;
pub const CURSOR_NORMAL: i32 = 0x0003_4001;
unsafe fn proc(name: &[u8]) -> Option<unsafe extern "system" fn() -> isize> {
let glfw = GetModuleHandleA(c"glfw.dll".as_ptr() as *const u8);
if glfw.is_null() {
return None;
}
GetProcAddress(glfw, name.as_ptr())
}
pub unsafe fn win32_window(window: *mut c_void) -> isize {
match proc(b"glfwGetWin32Window\0") {
Some(f) => {
let f: unsafe extern "C" fn(*mut c_void) -> isize = std::mem::transmute(f);
f(window)
}
None => 0,
}
}
pub unsafe fn framebuffer_size(window: *mut c_void) -> (i32, i32) {
match proc(b"glfwGetFramebufferSize\0") {
Some(f) => {
let f: unsafe extern "C" fn(*mut c_void, *mut i32, *mut i32) =
std::mem::transmute(f);
let (mut w, mut h) = (0i32, 0i32);
f(window, &mut w, &mut h);
(w, h)
}
None => (0, 0),
}
}
pub unsafe fn get_input_mode(window: *mut c_void, mode: i32) -> i32 {
match proc(b"glfwGetInputMode\0") {
Some(f) => {
let f: unsafe extern "C" fn(*mut c_void, i32) -> i32 = std::mem::transmute(f);
f(window, mode)
}
None => 0,
}
}
pub unsafe fn set_input_mode(window: *mut c_void, mode: i32, value: i32) {
if let Some(f) = proc(b"glfwSetInputMode\0") {
let f: unsafe extern "C" fn(*mut c_void, i32, i32) = std::mem::transmute(f);
f(window, mode, value);
}
}
}