Skip to content
Open
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
7 changes: 7 additions & 0 deletions crates/livesplit-auto-splitting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ unsafe extern "C" {
/// guaranteed to be valid UTF-8 and is not nul-terminated.
/// Example values: `windows`, `linux`, `macos`
pub fn runtime_get_os(buf_ptr: *mut u8, buf_len_ptr: *mut usize) -> bool;
/// Stores the WASI path of the currently loaded splits file in the buffer
/// given. Returns `false` if there is no splits path or the buffer is too
/// small. After this call, no matter whether it was successful or not, the
/// `buf_len_ptr` will be set to the required buffer size, or `0` if there
/// is no splits path. The path is guaranteed to be valid UTF-8 and is not
/// nul-terminated.
pub fn runtime_get_splits_path(buf_ptr: *mut u8, buf_len_ptr: *mut usize) -> bool;
/// Stores the name of the architecture that the runtime is running on
/// in the buffer given. Returns `false` if the buffer is too small.
/// After this call, no matter whether it was successful or not, the
Expand Down
7 changes: 7 additions & 0 deletions crates/livesplit-auto-splitting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@
//! /// guaranteed to be valid UTF-8 and is not nul-terminated.
//! /// Example values: `windows`, `linux`, `macos`
//! pub fn runtime_get_os(buf_ptr: *mut u8, buf_len_ptr: *mut usize) -> bool;
//! /// Stores the WASI path of the currently loaded splits file in the buffer
//! /// given. Returns `false` if there is no splits path or the buffer is too
//! /// small. After this call, no matter whether it was successful or not, the
//! /// `buf_len_ptr` will be set to the required buffer size, or `0` if there
//! /// is no splits path. The path is guaranteed to be valid UTF-8 and is not
//! /// nul-terminated.
//! pub fn runtime_get_splits_path(buf_ptr: *mut u8, buf_len_ptr: *mut usize) -> bool;
//! /// Stores the name of the architecture that the runtime is running on
//! /// in the buffer given. Returns `false` if the buffer is too small.
//! /// After this call, no matter whether it was successful or not, the
Expand Down
32 changes: 32 additions & 0 deletions crates/livesplit-auto-splitting/src/runtime/api/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ pub fn bind<T: Timer>(linker: &mut Linker<Context<T>>) -> Result<(), CreationErr
source,
name: "runtime_get_os",
})?
.func_wrap("env", "runtime_get_splits_path", {
|mut caller: Caller<Context<T>>, ptr: u32, len_ptr: u32| {
let path = caller
.data()
.timer
.splits_path()
.and_then(|path| crate::wasi_path::from_native(&path));

let (memory, _) = memory_and_context(&mut caller);
let len_bytes = get_arr_mut(memory, len_ptr)?;

let Some(path) = path else {
*len_bytes = 0u32.to_le_bytes();
return Ok(0u32);
};

let needed = path.len();
let len = u32::from_le_bytes(*len_bytes) as usize;
*len_bytes = (needed as u32).to_le_bytes();

if len < needed {
return Ok(0u32);
}
let buf = get_slice_mut(memory, ptr, needed as _)?;
buf.copy_from_slice(path.as_bytes());
Ok(1u32)
}
})
.map_err(|source| CreationError::LinkFunction {
source,
name: "runtime_get_splits_path",
})?
.func_wrap("env", "runtime_get_arch", {
|mut caller: Caller<Context<T>>, ptr: u32, len_ptr: u32| {
let (memory, _) = memory_and_context(&mut caller);
Expand Down
4 changes: 4 additions & 0 deletions crates/livesplit-auto-splitting/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ pub trait Timer: Send + 'static {
fn log_auto_splitter(&mut self, message: fmt::Arguments);
/// Logs a message from the runtime.
fn log_runtime(&mut self, message: fmt::Arguments, log_level: LogLevel);
/// The path of the currently loaded splits file, if any.
fn splits_path(&self) -> Option<std::path::PathBuf> {
None
}
}
7 changes: 7 additions & 0 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@
//! /// guaranteed to be valid UTF-8 and is not nul-terminated.
//! /// Example values: `windows`, `linux`, `macos`
//! pub fn runtime_get_os(buf_ptr: *mut u8, buf_len_ptr: *mut usize) -> bool;
//! /// Stores the WASI path of the currently loaded splits file in the buffer
//! /// given. Returns `false` if there is no splits path or the buffer is too
//! /// small. After this call, no matter whether it was successful or not, the
//! /// `buf_len_ptr` will be set to the required buffer size, or `0` if there
//! /// is no splits path. The path is guaranteed to be valid UTF-8 and is not
//! /// nul-terminated.
//! pub fn runtime_get_splits_path(buf_ptr: *mut u8, buf_len_ptr: *mut usize) -> bool;
//! /// Stores the name of the architecture that the runtime is running on
//! /// in the buffer given. Returns `false` if the buffer is too small.
//! /// After this call, no matter whether it was successful or not, the
Expand Down