Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow auto splitters to query the size of modules #602

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/livesplit-auto-splitting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
//! name_ptr: *const u8,
//! name_len: usize,
//! ) -> Option<NonZeroAddress>;
//! /// Gets the size of a module in a process.
//! pub fn process_get_module_size(
//! process: ProcessId,
//! name_ptr: *const u8,
//! name_len: usize,
//! ) -> Option<NonZeroU64>;
//!
//! /// Sets the tick rate of the runtime. This influences the amount of
//! /// times the `update` function is called per second.
Expand Down
20 changes: 20 additions & 0 deletions crates/livesplit-auto-splitting/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ impl Process {
.map(|m| m.start() as u64)
}

pub fn module_size(&mut self, module: &str) -> Result<u64, ModuleError> {
let now = Instant::now();
if now - self.last_check >= Duration::from_secs(1) {
self.modules = match proc_maps::get_process_maps(self.pid) {
Ok(m) => m,
Err(source) => {
self.modules.clear();
return Err(ModuleError::ListModules { source });
}
};
self.last_check = now;
}
Ok(self
.modules
.iter()
.filter(|m| m.filename().map_or(false, |f| f.ends_with(module)))
.map(|m| m.size() as u64)
.sum())
}

pub fn read_mem(&self, address: Address, buf: &mut [u8]) -> io::Result<()> {
self.handle.copy_address(address as usize, buf)
}
Expand Down
15 changes: 15 additions & 0 deletions crates/livesplit-auto-splitting/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,21 @@ fn bind_interface<T: Timer>(linker: &mut Linker<Context<T>>) -> Result<(), Creat
.context(LinkFunction {
name: "process_get_module_address",
})?
.func_wrap("env", "process_get_module_size", {
|mut caller: Caller<'_, Context<T>>, process: u64, ptr: u32, len: u32| {
let (memory, context) = memory_and_context(&mut caller);
let module_name = read_str(memory, ptr, len)?;
Ok(context
.processes
.get_mut(ProcessKey::from(KeyData::from_ffi(process as u64)))
.ok_or_else(|| Trap::new(format!("Invalid process handle: {process}")))?
.module_size(module_name)
.unwrap_or_default())
}
})
.context(LinkFunction {
name: "process_get_module_size",
})?
.func_wrap("env", "process_read", {
|mut caller: Caller<'_, Context<T>>,
process: u64,
Expand Down
6 changes: 6 additions & 0 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
//! name_ptr: *const u8,
//! name_len: usize,
//! ) -> Option<NonZeroAddress>;
//! /// Gets the size of a module in a process.
//! pub fn process_get_module_size(
//! process: ProcessId,
//! name_ptr: *const u8,
//! name_len: usize,
//! ) -> Option<NonZeroU64>;
//!
//! /// Sets the tick rate of the runtime. This influences the amount of
//! /// times the `update` function is called per second.
Expand Down