win-auto-utils 0.2.4

Universal Windows automation utilities with memory, window, input, and color operations
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! TrampolineHook Handler for function hooking

use super::address_strategy::AddressStrategy;
use crate::memory::MemoryError;
use crate::memory_hook::{Architecture, TrampolineHook};

use super::super::register::ModifierHandler;

// ==================== TrampolineHook Config ====================

/// TrampolineHook configuration (pure data)
#[derive(Clone, Debug)]
pub struct TrampolineHookConfig {
    pub name: String,
    pub shellcode: Vec<u8>,
    pub bytes_to_overwrite: usize,
    pub skip_trampoline: bool,
}

// ==================== TrampolineHook Handler ====================

/// TrampolineHook handler with embedded address strategy
pub struct TrampolineHookHandler {
    config: TrampolineHookConfig,
    /// Address resolution strategy
    address_strategy: AddressStrategy,
    /// Cached resolved address (avoids repeated scans)
    cached_address: Option<usize>,
    instance: Option<TrampolineHook>,
}

impl TrampolineHookHandler {
    /// Create a new TrampolineHookHandler
    fn new(config: TrampolineHookConfig, address_strategy: AddressStrategy) -> Self {
        Self {
            config,
            address_strategy,
            cached_address: None,
            instance: None,
        }
    }

    /// Create a TrampolineHookHandler with AOB scanning (recommended).
    ///
    /// Uses AOB pattern scanning to find the target address at runtime.
    /// This is the most robust approach as it works across game updates.
    ///
    /// # Performance Note
    /// - First activation: Executes AOB scan (may take 10-500ms)
    /// - Subsequent activations: Reuses cached address (instant!)
    /// - Cache auto-invalidates when PID changes
    ///
    /// # Arguments
    /// * `name` - Hook name (must be unique)
    /// * `pattern` - AOB pattern string (space-separated hex bytes, e.g., "48 89 5C 24")
    /// * `shellcode` - Your custom code bytes to execute at the hook point
    /// * `bytes_to_overwrite` - Number of bytes to overwrite (MUST equal original instruction length!)
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_manager::builtin::TrampolineHookHandler;
    ///
    /// let hook = TrampolineHookHandler::new_hook_aob(
    ///     "my_hook",
    ///     "48 89 5C 24",
    ///     vec![0x90, 0x90],  // NOP instructions
    ///     5,                  // Original instruction is 5 bytes
    /// )?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_hook_aob(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        Self::new_hook_aob_with_range(name, pattern, shellcode, bytes_to_overwrite, 0, 0)
    }

    /// Create a TrampolineHookHandler with AOB scanning and custom search range.
    ///
    /// Same as `new_hook_aob()` but allows specifying a memory range for faster scanning.
    /// Use this when you know the approximate location of the target code.
    ///
    /// # Arguments
    /// * `name` - Hook name
    /// * `pattern` - AOB pattern string
    /// * `shellcode` - Your custom code bytes
    /// * `bytes_to_overwrite` - Number of bytes to overwrite
    /// * `start_address` - Starting address for the scan (0 = from beginning)
    /// * `length` - Length of the scan range (0 = entire process memory)
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_manager::builtin::TrampolineHookHandler;
    ///
    /// // Scan only within a specific module range (much faster!)
    /// let hook = TrampolineHookHandler::new_hook_aob_with_range(
    ///     "my_hook",
    ///     "48 89 5C 24",
    ///     vec![0x90, 0x90],
    ///     5,
    ///     0x10000000,  // Start address (e.g., module base)
    ///     0x100000,    // Length (e.g., module size)
    /// )?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_hook_aob_with_range(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
        start_address: usize,
        length: usize,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        Self::new_hook_aob_with_range_and_offset(
            name,
            pattern,
            shellcode,
            bytes_to_overwrite,
            start_address,
            length,
            0,
        )
    }

    /// Create a TrampolineHookHandler with AOB scanning and offset.
    ///
    /// Same as `new_hook_aob()` but supports adding an offset to the scanned address.
    /// Useful when the AOB pattern finds a nearby signature, but you need to hook at a different location.
    ///
    /// # Arguments
    /// * `name` - Hook name
    /// * `pattern` - AOB pattern string
    /// * `shellcode` - Your custom code bytes
    /// * `bytes_to_overwrite` - Number of bytes to overwrite
    /// * `offset` - Offset to add to the scanned address (can be negative)
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_manager::builtin::TrampolineHookHandler;
    ///
    /// // Scan for function prologue, but hook 0x42 bytes later
    /// let hook = TrampolineHookHandler::new_hook_aob_with_offset(
    ///     "my_hook",
    ///     "48 89 5C 24",
    ///     vec![0x90, 0x90],
    ///     5,
    ///     0x42,
    /// )?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_hook_aob_with_offset(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
        offset: i64,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        let config = TrampolineHookConfig {
            name: name.into(),
            shellcode,
            bytes_to_overwrite,
            skip_trampoline: false,
        };

        Ok(Box::new(Self::new(
            config,
            AddressStrategy::aob_pattern_with_offset(pattern, offset),
        )))
    }

    /// Create a TrampolineHookHandler with AOB scanning, custom range and offset.
    ///
    /// Combines range-limited scanning with post-scan address correction.
    ///
    /// # Arguments
    /// * `name` - Hook name
    /// * `pattern` - AOB pattern string
    /// * `shellcode` - Your custom code bytes
    /// * `bytes_to_overwrite` - Number of bytes to overwrite
    /// * `start_address` - Starting address for the scan (0 = from beginning)
    /// * `length` - Length of the scan range (0 = entire process memory)
    /// * `offset` - Offset to add to the scanned address (can be negative)
    pub fn new_hook_aob_with_range_and_offset(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
        start_address: usize,
        length: usize,
        offset: i64,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        let config = TrampolineHookConfig {
            name: name.into(),
            shellcode,
            bytes_to_overwrite,
            skip_trampoline: false,
        };

        Ok(Box::new(Self::new(
            config,
            AddressStrategy::aob_pattern_with_range_and_offset(
                pattern,
                start_address,
                length,
                offset,
            ),
        )))
    }

    /// Create a TrampolineHookHandler with skip_trampoline mode and AOB scanning.
    ///
    /// In skip_trampoline mode, the hook jumps directly to target+bytes without creating a trampoline.
    /// Use this when you don't need to call the original function.
    ///
    /// # Arguments
    /// * `name` - Hook name
    /// * `pattern` - AOB pattern string
    /// * `shellcode` - Your custom code bytes
    /// * `bytes_to_overwrite` - Number of bytes to overwrite
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_manager::builtin::TrampolineHookHandler;
    ///
    /// let hook = TrampolineHookHandler::new_skip_trampoline_aob(
    ///     "nop_patch",
    ///     "90 90 90 90 90",
    ///     vec![0x90, 0x90, 0x90, 0x90, 0x90],
    ///     5,
    /// )?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_skip_trampoline_aob(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        Self::new_skip_trampoline_aob_with_range(name, pattern, shellcode, bytes_to_overwrite, 0, 0)
    }

    /// Create a TrampolineHookHandler with skip_trampoline mode, AOB scanning and custom range.
    pub fn new_skip_trampoline_aob_with_range(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
        start_address: usize,
        length: usize,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        Self::new_skip_trampoline_aob_with_range_and_offset(
            name,
            pattern,
            shellcode,
            bytes_to_overwrite,
            start_address,
            length,
            0,
        )
    }

    /// Create a TrampolineHookHandler with skip_trampoline mode, AOB scanning and offset.
    pub fn new_skip_trampoline_aob_with_offset(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
        offset: i64,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        let config = TrampolineHookConfig {
            name: name.into(),
            shellcode,
            bytes_to_overwrite,
            skip_trampoline: true,
        };

        Ok(Box::new(Self::new(
            config,
            AddressStrategy::aob_pattern_with_offset(pattern, offset),
        )))
    }

    /// Create a TrampolineHookHandler with skip_trampoline mode, AOB scanning, custom range and offset.
    pub fn new_skip_trampoline_aob_with_range_and_offset(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
        start_address: usize,
        length: usize,
        offset: i64,
    ) -> Result<Box<dyn ModifierHandler>, String> {
        let config = TrampolineHookConfig {
            name: name.into(),
            shellcode,
            bytes_to_overwrite,
            skip_trampoline: true,
        };

        Ok(Box::new(Self::new(
            config,
            AddressStrategy::aob_pattern_with_range_and_offset(
                pattern,
                start_address,
                length,
                offset,
            ),
        )))
    }

    /// Create a TrampolineHookHandler with static address pattern.
    ///
    /// Architecture is automatically detected at activation time from ProcessContext.
    /// Use this when the address is stable and doesn't change across game updates.
    ///
    /// # Arguments
    /// * `name` - Hook name
    /// * `pattern` - Static address pattern string (e.g., "game.exe+1000")
    /// * `shellcode` - Your custom code bytes
    /// * `bytes_to_overwrite` - Number of bytes to overwrite
    ///
    /// # Note
    /// For static addresses, include the offset directly in the pattern string.
    /// Example: "game.exe+0x44ef1" (offset is part of the pattern)
    ///
    /// # Example
    /// ```no_run
    /// use win_auto_utils::memory_manager::builtin::TrampolineHookHandler;
    ///
    /// let hook = TrampolineHookHandler::new_hook_static(
    ///     "my_hook",
    ///     "game.exe+3000",
    ///     vec![0x90, 0x90],
    ///     5,
    /// )?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn new_hook_static(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
    ) -> Result<Box<dyn ModifierHandler>, crate::memory_resolver::ParseError> {
        let config = TrampolineHookConfig {
            name: name.into(),
            shellcode,
            bytes_to_overwrite,
            skip_trampoline: false,
        };

        Ok(Box::new(Self::new(
            config,
            AddressStrategy::static_pattern(pattern),
        )))
    }

    /// Create a TrampolineHookHandler with static address in skip_trampoline mode.
    ///
    /// In skip_trampoline mode, the hook jumps directly to target+bytes without creating a trampoline.
    /// Use this when you don't need to call the original function.
    ///
    /// # Note
    /// For static addresses, include the offset directly in the pattern string.
    pub fn new_skip_trampoline_static(
        name: impl Into<String>,
        pattern: &str,
        shellcode: Vec<u8>,
        bytes_to_overwrite: usize,
    ) -> Result<Box<dyn ModifierHandler>, crate::memory_resolver::ParseError> {
        let config = TrampolineHookConfig {
            name: name.into(),
            shellcode,
            bytes_to_overwrite,
            skip_trampoline: true,
        };

        Ok(Box::new(Self::new(
            config,
            AddressStrategy::static_pattern(pattern),
        )))
    }
}

impl ModifierHandler for TrampolineHookHandler {
    fn name(&self) -> &str {
        &self.config.name
    }

    fn activate(
        &mut self,
        ctx: &crate::memory_manager::manager::ProcessContext,
    ) -> Result<(), MemoryError> {
        // Check if instance exists and if context changed (compare handle from instance)
        let context_changed = if let Some(ref hook) = self.instance {
            // SendableHandle wraps HANDLE, compare the inner HANDLE value
            hook.handle.0 != ctx.handle
        } else {
            true // No instance, need to create
        };

        // If context changed, clear address cache to force re-resolution
        if context_changed {
            self.cached_address = None;
        }

        // Resolve address (use cache if available and context unchanged)
        let target_address = if self.cached_address.is_none() {
            // Use unified AddressStrategy to resolve address (offset is already applied by the strategy)
            let addr = self
                .address_strategy
                .resolve(ctx.handle, ctx.pid, ctx.architecture)?;

            // Cache the resolved address
            self.cached_address = Some(addr);

            addr
        } else {
            // Use cached address (fast path!)
            self.cached_address.unwrap()
        };

        // If instance already exists and is installed, just return (already active)
        if !context_changed {
            if let Some(ref hook) = self.instance {
                if hook.is_installed() {
                    return Ok(());
                }
                // Hook exists but not installed (was deactivated), will reinstall below
            }
        }

        // Create new TrampolineHook instance or reinstall existing one
        if !context_changed && self.instance.is_some() {
            // Reuse existing instance - configuration is immutable, just reinstall
            if let Some(ref mut hook) = self.instance {
                hook.install()?;
            }
        } else {
            // Context changed or no instance, create new instance
            let detected_arch = ctx.architecture;

            // Create new TrampolineHook instance
            let mut hook =
                TrampolineHook::auto_new(ctx.handle, target_address, self.config.shellcode.clone());

            // Configure hook parameters with pre-detected architecture
            hook.set_architecture(detected_arch == Architecture::X64);
            hook.set_bytes_to_overwrite(self.config.bytes_to_overwrite);
            hook.set_skip_trampoline(self.config.skip_trampoline);

            // Install the hook
            hook.install()?;

            // Update state
            self.instance = Some(hook);
        }

        Ok(())
    }

    fn deactivate(&mut self) -> Result<(), MemoryError> {
        // Uninstall the hook but KEEP the instance and address cache
        // This allows fast re-activation without re-scanning
        if let Some(ref mut hook) = self.instance {
            // Only uninstall if currently installed
            if hook.is_installed() {
                hook.uninstall()?;
            }
            // Keep the instance (don't take/drop it)
        }

        // Address cache is preserved for fast re-activation
        Ok(())
    }

    fn is_active(&self) -> bool {
        self.instance.is_some()
    }
}

impl TrampolineHookHandler {
    /// Manually clear the cached address (forces re-scan on next activate)
    ///
    /// This is useful when you know the target memory layout has changed
    /// (e.g., after a game update or module reload).
    pub fn clear_cache(&mut self) {
        self.cached_address = None;
    }

    /// Check if this handler has a cached address
    pub fn has_cached_address(&self) -> bool {
        self.cached_address.is_some()
    }
}

unsafe impl Send for TrampolineHookHandler {}