win-auto-utils 0.2.2

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
//! Memory Lock implementation
//!
//! Provides functionality to continuously monitor and restore memory values.

use crate::memory::{read_memory_bytes, write_memory_bytes, MemoryError};
use crate::memory_resolver::{MemoryAddress, AddressSource};
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Arc,
};
use std::thread;
use std::time::Duration;
use windows::Win32::Foundation::HANDLE;

/// Trait to convert types to bytes for memory writing
pub trait AsBytes {
    fn as_bytes(&self) -> &[u8];
}

impl AsBytes for u8 {
    fn as_bytes(&self) -> &[u8] {
        std::slice::from_ref(self)
    }
}

impl AsBytes for u16 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const u16 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<u16>()) }
    }
}

impl AsBytes for u32 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const u32 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<u32>()) }
    }
}

impl AsBytes for u64 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const u64 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<u64>()) }
    }
}

impl AsBytes for f32 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const f32 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<f32>()) }
    }
}

impl AsBytes for f64 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const f64 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<f64>()) }
    }
}

impl AsBytes for i8 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const i8 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<i8>()) }
    }
}

impl AsBytes for i16 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const i16 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<i16>()) }
    }
}

impl AsBytes for i32 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const i32 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<i32>()) }
    }
}

impl AsBytes for i64 {
    fn as_bytes(&self) -> &[u8] {
        let ptr = self as *const i64 as *const u8;
        unsafe { std::slice::from_raw_parts(ptr, std::mem::size_of::<i64>()) }
    }
}

/// Builder for MemoryLock
pub struct MemoryLockBuilder {
    handle: Option<HANDLE>,
    address_source: Option<AddressSource>,
    pid: Option<u32>,
    locked_value: Option<Vec<u8>>,
    scan_interval: Duration,
}

impl MemoryLockBuilder {
    /// Create a new builder with all parameters unset
    pub fn new() -> Self {
        Self {
            handle: None,
            address_source: None,
            pid: None,
            locked_value: None,
            scan_interval: Duration::from_millis(100), // Default interval
        }
    }

    /// Set the process handle
    pub fn handle(mut self, handle: HANDLE) -> Self {
        self.handle = Some(handle);
        self
    }

    /// Set a static address directly
    pub fn address(mut self, address: usize) -> Self {
        // Convert static address to MemoryAddress::Absolute
        let mem_addr = MemoryAddress {
            base: crate::memory_resolver::AddressBase::Absolute(address),
            operations: vec![],
            pointer_size: crate::memory_resolver::PointerSize::default_architecture(),
        };
        self.address_source = Some(AddressSource::from_static(mem_addr));
        self
    }

    /// Set the dynamic address source (MemoryAddress pattern)
    pub fn address_from_resolver(mut self, addr: MemoryAddress) -> Self {
        self.address_source = Some(AddressSource::from_static(addr));
        self
    }

    /// Set an AOB pattern for dynamic address resolution
    pub fn address_from_aob(mut self, pattern: &str) -> Result<Self, String> {
        self.address_source = Some(AddressSource::from_aob_scan(pattern)?);
        Ok(self)
    }

    /// Set the process ID (required for dynamic addresses)
    pub fn pid(mut self, pid: u32) -> Self {
        self.pid = Some(pid);
        self
    }

    /// Set the value to lock (generic type)
    pub fn value<T: AsBytes>(mut self, val: T) -> Self {
        self.locked_value = Some(val.as_bytes().to_vec());
        self
    }

    /// Set the raw bytes to lock
    pub fn bytes(mut self, data: Vec<u8>) -> Self {
        self.locked_value = Some(data);
        self
    }

    /// Set the scan interval
    pub fn scan_interval(mut self, interval: Duration) -> Self {
        self.scan_interval = interval;
        self
    }

    /// Build the MemoryLock instance
    pub fn build(self) -> Result<MemoryLock, MemoryError> {
        // Validate required parameters
        let handle = self.handle.ok_or_else(|| {
            MemoryError::WriteFailed(
                "handle must be set. Call .handle(handle) before build().".to_string(),
            )
        })?;

        let address_source = self.address_source.ok_or_else(|| {
            MemoryError::WriteFailed(
                "address must be set. Call .address(addr), .address_from_resolver(addr), or .address_from_aob(pattern) before build().".to_string()
            )
        })?;

        let locked_value = self.locked_value.ok_or_else(|| {
            MemoryError::WriteFailed(
                "locked_value must be set. Call .value(val) or .bytes(data) before build()."
                    .to_string(),
            )
        })?;

        let size = locked_value.len();

        // For module-based or AOB addresses, PID is recommended
        if self.pid.is_none() {
            // Check if we need PID
            let needs_pid = match &address_source {
                AddressSource::Static(addr) => {
                    matches!(addr.base, crate::memory_resolver::AddressBase::Module { .. })
                }
                AddressSource::AobScan { .. } => true,
            };
            
            if needs_pid {
                return Err(MemoryError::WriteFailed(
                    "PID must be set when using module-relative or AOB addresses. Call .pid(pid) before build().".to_string()
                ));
            }
        }

        Ok(MemoryLock {
            handle: Some(handle),  // Store HANDLE directly
            pid: self.pid,
            address_source: Some(address_source),
            size,
            locked_value,
            scan_interval: self.scan_interval,
            stop_flag: Arc::new(AtomicBool::new(false)),
            worker_thread: None,
        })
    }
}

/// Memory lock controller for continuous value monitoring and restoration
///
/// # Example
/// ```no_run
/// use win_auto_utils::memory_lock::MemoryLock;
/// use windows::Win32::Foundation::HANDLE;
///
/// let handle = HANDLE::default(); // Replace with actual handle
/// let mut lock = MemoryLock::builder()
///     .handle(handle)
///     .address(0x7FF6A1B2C3D4)
///     .value(100u32)
///     .build()?;
///
/// lock.lock_value(100u32)?; // Start locking
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// ```
pub struct MemoryLock {
    handle: Option<HANDLE>,  // Store HANDLE directly for type consistency
    address_source: Option<AddressSource>,
    pid: Option<u32>,
    size: usize,
    locked_value: Vec<u8>,
    scan_interval: Duration,
    stop_flag: Arc<AtomicBool>,
    worker_thread: Option<thread::JoinHandle<()>>,
}

impl MemoryLock {
    /// Create a new builder with all parameters unset
    pub fn builder() -> MemoryLockBuilder {
        MemoryLockBuilder::new()
    }

    /// Set the scan interval for checking memory changes
    ///
    /// # Arguments
    /// * `interval` - How often to check and restore the value
    ///
    /// # Note
    /// Shorter intervals provide tighter locking but consume more CPU
    pub fn set_scan_interval(&mut self, interval: Duration) {
        self.scan_interval = interval;
    }

    /// Lock a specific value at the target address
    ///
    /// # Arguments
    /// * `value` - The value to lock (will be serialized to bytes)
    ///
    /// # Type Support
    /// Supports any type that implements `AsRef<[u8]>` or can be converted to bytes
    ///
    /// # Example
    /// ```no_run
    /// # use win_auto_utils::memory_lock::MemoryLock;
    /// # use windows::Win32::Foundation::HANDLE;
    /// # let handle = HANDLE::default();
    /// let mut lock = MemoryLock::builder()
    ///     .handle(handle)
    ///     .address(0x1000)
    ///     .value(100u32)
    ///     .build()?;
    /// lock.lock_value(100u32)?; // Start locking with the value
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn lock_value<T: Copy + AsBytes>(&mut self, value: T) -> Result<(), MemoryError> {
        // Validate required parameters
        let handle = self.handle.ok_or_else(|| {
            MemoryError::WriteFailed(
                "handle must be set. Call .handle(handle) before lock_value().".to_string(),
            )
        })?;

        let address_source = self.address_source.as_ref().ok_or_else(|| {
            MemoryError::WriteFailed("address must be set. Call .address(addr), .address_from_resolver(addr), or .address_from_aob(pattern) before lock_value().".to_string())
        })?;

        let pid = self.pid.unwrap_or(0);

        // Resolve the actual address (tolerate errors if configured)
        let address = match address_source.resolve(handle, pid) {
            Ok(addr) => addr,
            Err(_) => {
                // For dynamic addresses, we can tolerate initial resolution failure
                // The background thread will retry on each cycle

                // Start monitoring anyway - the thread will handle retries
                let bytes = value.as_bytes();
                self.size = bytes.len();
                self.locked_value = bytes.to_vec();
                return self.start_monitoring(); // Thread will re-resolve address
            }
        };

        let bytes = value.as_bytes();
        self.size = bytes.len();
        self.locked_value = bytes.to_vec();

        // Write the initial value
        write_memory_bytes(handle, address, &self.locked_value)?;

        // Start the monitoring thread
        self.start_monitoring()?;

        Ok(())
    }

    /// Lock raw bytes at the target address
    ///
    /// # Arguments
    /// * `bytes` - Raw byte sequence to lock
    pub fn lock_bytes(&mut self, bytes: &[u8]) -> Result<(), MemoryError> {
        // Validate required parameters
        let handle = self.handle.ok_or_else(|| {
            MemoryError::WriteFailed(
                "handle must be set. Call .handle(handle) before lock_bytes().".to_string(),
            )
        })?;

        let address_source = self.address_source.as_ref().ok_or_else(|| {
            MemoryError::WriteFailed("address must be set. Call .address(addr), .address_from_resolver(addr), or .address_from_aob(pattern) before lock_bytes().".to_string())
        })?;

        let pid = self.pid.unwrap_or(0);

        // Resolve the actual address (tolerate errors if configured)
        let address = match address_source.resolve(handle, pid) {
            Ok(addr) => addr,
            Err(_) => {
                // For dynamic addresses, we can tolerate initial resolution failure
                // The background thread will retry on each cycle

                // Start monitoring anyway - the thread will handle retries
                self.size = bytes.len();
                self.locked_value = bytes.to_vec();
                return self.start_monitoring(); // Thread will re-resolve address
            }
        };

        self.size = bytes.len();
        self.locked_value = bytes.to_vec();

        // Write the initial value
        write_memory_bytes(handle, address, &self.locked_value)?;

        // Start the monitoring thread
        self.start_monitoring()?;

        Ok(())
    }

    /// Start the monitoring thread
    fn start_monitoring(&mut self) -> Result<(), MemoryError> {
        let handle = self.handle.unwrap();
        let address_source = self.address_source.clone().unwrap();
        let pid = self.pid.unwrap_or(0);
        let size = self.size;
        let locked_value = self.locked_value.clone();
        let scan_interval = self.scan_interval;
        let stop_flag = self.stop_flag.clone();

        // Convert HANDLE to usize for thread-safe passing
        let handle_usize = handle.0 as usize;

        self.worker_thread = Some(thread::spawn(move || {
            // Convert usize back to HANDLE inside the thread
            let handle = HANDLE(handle_usize as *mut std::ffi::c_void);

            while !stop_flag.load(Ordering::SeqCst) {
                thread::sleep(scan_interval);

                // Resolve the actual address (tolerate errors if configured)
                let address = match address_source.resolve(handle, pid) {
                    Ok(addr) => addr,
                    Err(_) => continue,
                };

                // Read the current value
                let current_value = match read_memory_bytes(handle, address, size) {
                    Ok(val) => val,
                    Err(_) => continue,
                };

                // Compare and write if necessary
                if current_value != locked_value {
                    if let Err(_) = write_memory_bytes(handle, address, &locked_value) {
                        continue;
                    }
                }
            }
        }));

        Ok(())
    }

    /// Stop the monitoring thread
    ///
    /// This method gracefully stops the background monitoring thread.
    /// After calling this, you can call `lock_value()` or `lock_bytes()` again
    /// to restart monitoring with new parameters.
    ///
    /// # Example
    /// ```no_run
    /// # use win_auto_utils::memory_lock::MemoryLock;
    /// # use windows::Win32::Foundation::HANDLE;
    /// # let handle = HANDLE::default();
    /// let mut lock = MemoryLock::builder()
    ///     .handle(handle)
    ///     .address(0x1000)
    ///     .value(100u32)
    ///     .build()?;
    /// lock.lock_value(100u32)?; // Start locking
    /// 
    /// // ... some time later ...
    /// lock.stop(); // Stop the monitoring thread
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn stop(&mut self) {
        // Signal the worker thread to stop
        self.stop_flag.store(true, Ordering::SeqCst);
        
        // Wait for the thread to finish (with timeout to avoid hanging)
        if let Some(handle) = self.worker_thread.take() {
            // Give the thread up to 1 second to finish
            let _ = handle.join();
        }
    }
}

impl Drop for MemoryLock {
    fn drop(&mut self) {
        self.stop();
    }
}