File size: 1,894 Bytes
229ceef
 
 
 
 
 
 
 
 
e5f8547
229ceef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! OpenWA immediate-mode debug UI.
//!
//! Spawns a floating egui window (backed by OpenGL via eframe) in a background
//! thread. The window is in the same process as WA.exe, so it can read game
//! memory directly through the `openwa-core` typed structs.
//!
//! # Usage
//!
//! ```rust
//! // In openwa-dll lib.rs, after rebase::init() and hook installation:
//! if std::env::var("OPENWA_DEBUG_UI").is_ok() {
//!     openwa_debugui::spawn();
//! }
//! ```
//!
//! # Log integration
//!
//! Any crate that depends on `openwa-debugui` can push events to the log panel:
//!
//! ```rust
//! openwa_debugui::log::push("some hook fired");
//! ```

mod app;
pub mod log;

use app::DebugApp;

/// Spawn the debug UI window in a background thread.
///
/// Returns immediately. The window runs until closed by the user.
/// This must be called *after* `openwa_core::rebase::init()`.
pub fn spawn() {
    std::thread::spawn(|| {
        if let Err(e) = run_window() {
            // Not much we can do here; the window just won't appear.
            eprintln!("[openwa-debugui] fatal: {e:?}");
        }
    });
}

fn run_window() -> Result<(), eframe::Error> {
    let mut options = eframe::NativeOptions {
        viewport: eframe::egui::ViewportBuilder::default()
            .with_title("OpenWA Debug")
            .with_inner_size([760.0, 560.0]),
        ..Default::default()
    };

    // On Windows we run in a background thread (DLL injection), so we need
    // to allow the event loop to be created on a non-main thread.
    #[cfg(target_os = "windows")]
    {
        options.event_loop_builder = Some(Box::new(|builder| {
            use winit::platform::windows::EventLoopBuilderExtWindows;
            builder.with_any_thread(true);
        }));
    }

    eframe::run_native(
        "OpenWA Debug",
        options,
        Box::new(|_cc| Ok(Box::new(DebugApp::default()))),
    )
}