use std::time::Duration;
use windows::Win32::Foundation::HANDLE;
use crate::memory::MemoryError;
use crate::memory_lock::MemoryLock;
use crate::memory_resolver::{AddressSource, ParseError};
use super::super::register::ModifierHandler;
#[derive(Clone, Debug)]
pub struct LockConfig {
pub name: String,
pub value: Vec<u8>,
pub scan_interval: Duration,
}
pub struct LockHandler {
config: LockConfig,
address_source: AddressSource,
instance: Option<MemoryLock>,
last_handle: Option<HANDLE>,
last_pid: Option<u32>,
}
impl LockHandler {
fn new(config: LockConfig, address_source: AddressSource) -> Self {
Self {
config,
address_source,
instance: None,
last_handle: None,
last_pid: None,
}
}
pub fn new_with_address(
name: impl Into<String>,
address_source: AddressSource,
value: Vec<u8>,
scan_interval: Duration,
) -> Box<dyn ModifierHandler> {
let config = LockConfig {
name: name.into(),
value,
scan_interval,
};
Box::new(Self::new(config, address_source))
}
pub fn new_with_address_typed<T: crate::memory_lock::AsBytes>(
name: impl Into<String>,
address_source: AddressSource,
value: T,
scan_interval: Duration,
) -> Box<dyn ModifierHandler> {
Self::new_with_address(name, address_source, value.as_bytes().to_vec(), scan_interval)
}
pub fn new_lock_x86(
name: impl Into<String>,
pattern: &str,
value: Vec<u8>,
scan_interval: Duration,
) -> Result<Box<dyn ModifierHandler>, ParseError> {
let addr = AddressSource::from_pattern_x86(pattern)?;
Ok(Self::new_with_address(name, addr, value, scan_interval))
}
pub fn new_lock_x86_typed<T: crate::memory_lock::AsBytes>(
name: impl Into<String>,
pattern: &str,
value: T,
scan_interval: Duration,
) -> Result<Box<dyn ModifierHandler>, ParseError> {
let addr = AddressSource::from_pattern_x86(pattern)?;
Ok(Self::new_with_address_typed(name, addr, value, scan_interval))
}
pub fn new_lock_x64(
name: impl Into<String>,
pattern: &str,
value: Vec<u8>,
scan_interval: Duration,
) -> Result<Box<dyn ModifierHandler>, ParseError> {
let addr = AddressSource::from_pattern_x64(pattern)?;
Ok(Self::new_with_address(name, addr, value, scan_interval))
}
pub fn new_lock_x64_typed<T: crate::memory_lock::AsBytes>(
name: impl Into<String>,
pattern: &str,
value: T,
scan_interval: Duration,
) -> Result<Box<dyn ModifierHandler>, ParseError> {
let addr = AddressSource::from_pattern_x64(pattern)?;
Ok(Self::new_with_address_typed(name, addr, value, scan_interval))
}
pub fn new_lock_aob(
name: impl Into<String>,
pattern: &str,
value: Vec<u8>,
scan_interval: Duration,
) -> Result<Box<dyn ModifierHandler>, String> {
let addr = AddressSource::from_aob(pattern)?;
Ok(Self::new_with_address(name, addr, value, scan_interval))
}
pub fn new_lock_aob_typed<T: crate::memory_lock::AsBytes>(
name: impl Into<String>,
pattern: &str,
value: T,
scan_interval: Duration,
) -> Result<Box<dyn ModifierHandler>, String> {
let addr = AddressSource::from_aob(pattern)?;
Ok(Self::new_with_address_typed(name, addr, value, scan_interval))
}
}
impl ModifierHandler for LockHandler {
fn name(&self) -> &str { &self.config.name }
fn activate(&mut self, handle: HANDLE, pid: u32) -> Result<(), MemoryError> {
let context_changed = self.last_handle != Some(handle) || self.last_pid != Some(pid);
if self.instance.is_some() && !context_changed {
return Ok(());
}
if self.instance.is_some() {
self.deactivate()?;
}
let target_address = self.address_source.resolve(handle, pid)
.map_err(|e| MemoryError::InvalidAddress(format!("Failed to resolve address: {}", e)))?;
let mut lock = MemoryLock::builder()
.handle(handle)
.pid(pid)
.address(target_address)
.bytes(self.config.value.clone())
.scan_interval(self.config.scan_interval)
.build()?;
lock.lock_bytes(&self.config.value)?;
self.instance = Some(lock);
self.last_handle = Some(handle);
self.last_pid = Some(pid);
Ok(())
}
fn deactivate(&mut self) -> Result<(), MemoryError> {
if let Some(_lock) = self.instance.take() {
}
self.last_handle = None;
self.last_pid = None;
Ok(())
}
fn is_active(&self) -> bool {
self.instance.is_some()
}
}
unsafe impl Send for LockHandler {}