use crate::memory::MemoryError;
use crate::memory_aobscan::pattern::Pattern;
use crate::memory_aobscan::scanner::aob_scan_internal;
use windows::Win32::Foundation::HANDLE;
pub struct AobScanBuilder {
handle: HANDLE,
pattern: Option<Pattern>,
start_address: usize,
length: usize,
find_all: bool,
use_cache: bool,
}
impl AobScanBuilder {
pub fn new(handle: HANDLE) -> Self {
Self {
handle,
pattern: None,
start_address: 0,
length: 0,
find_all: false,
use_cache: true,
}
}
pub fn use_cache(mut self, use_cache: bool) -> Self {
self.use_cache = use_cache;
self
}
pub fn pattern_str(mut self, pattern: &str) -> Result<Self, String> {
self.pattern = Some(Pattern::from_str(pattern)?);
Ok(self)
}
pub fn pattern_bytes(mut self, bytes: Vec<u8>) -> Self {
use crate::memory_aobscan::pattern::anchor::find_best_anchor_sequence;
let mask = vec![true; bytes.len()];
let mask_bytes = vec![0xFF; bytes.len()];
let anchor_sequence = find_best_anchor_sequence(&bytes, &mask);
self.pattern = Some(Pattern {
bytes,
mask,
mask_bytes,
anchor_sequence,
});
self
}
pub fn start_address(mut self, addr: usize) -> Self {
self.start_address = addr;
self
}
pub fn length(mut self, len: usize) -> Self {
self.length = len;
self
}
pub fn find_all(mut self, all: bool) -> Self {
self.find_all = all;
self
}
pub fn scan(self) -> Result<Vec<usize>, MemoryError> {
let pattern = self
.pattern
.ok_or_else(|| MemoryError::InvalidAddress("Pattern not set".to_string()))?;
aob_scan_internal(
self.handle,
&pattern,
self.start_address,
self.length,
self.find_all,
self.use_cache,
)
}
}