From 2c163a512a8bfce20e996f657fd646e797577c05 Mon Sep 17 00:00:00 2001 From: AlexKnauth Date: Wed, 12 Jun 2024 13:36:17 -0400 Subject: [PATCH] Add timer_current_split_index --- crates/livesplit-auto-splitting/README.md | 6 ++++++ crates/livesplit-auto-splitting/src/lib.rs | 6 ++++++ .../src/runtime/api/timer.rs | 13 +++++++++++++ src/auto_splitting/mod.rs | 6 ++++++ 4 files changed, 31 insertions(+) diff --git a/crates/livesplit-auto-splitting/README.md b/crates/livesplit-auto-splitting/README.md index 5c4e9d6c..de41f387 100644 --- a/crates/livesplit-auto-splitting/README.md +++ b/crates/livesplit-auto-splitting/README.md @@ -110,6 +110,12 @@ extern "C" { pub fn timer_undo_split(); /// Resets the timer. pub fn timer_reset(); + /// Accesses the index of the split the attempt is currently on. If there's + /// no attempt in progress, `-1` is returned instead. This returns an + /// index that is equal to the amount of segments when the attempt is + /// finished, but has not been reset. So you need to be careful when using + /// this value for indexing. + pub fn timer_current_split_index() -> i32; /// Sets a custom key value pair. This may be arbitrary information that the /// auto splitter wants to provide for visualization. The pointers need to /// point to valid UTF-8 encoded text with the respective given length. diff --git a/crates/livesplit-auto-splitting/src/lib.rs b/crates/livesplit-auto-splitting/src/lib.rs index 0ba8c2a5..b5758852 100644 --- a/crates/livesplit-auto-splitting/src/lib.rs +++ b/crates/livesplit-auto-splitting/src/lib.rs @@ -110,6 +110,12 @@ //! pub fn timer_undo_split(); //! /// Resets the timer. //! pub fn timer_reset(); +//! /// Accesses the index of the split the attempt is currently on. If there's +//! /// no attempt in progress, `-1` is returned instead. This returns an +//! /// index that is equal to the amount of segments when the attempt is +//! /// finished, but has not been reset. So you need to be careful when using +//! /// this value for indexing. +//! pub fn timer_current_split_index() -> i32; //! /// Sets a custom key value pair. This may be arbitrary information that the //! /// auto splitter wants to provide for visualization. The pointers need to //! /// point to valid UTF-8 encoded text with the respective given length. diff --git a/crates/livesplit-auto-splitting/src/runtime/api/timer.rs b/crates/livesplit-auto-splitting/src/runtime/api/timer.rs index 5f88e035..708c5c29 100644 --- a/crates/livesplit-auto-splitting/src/runtime/api/timer.rs +++ b/crates/livesplit-auto-splitting/src/runtime/api/timer.rs @@ -69,6 +69,19 @@ pub fn bind(linker: &mut Linker>) -> Result<(), CreationErr source, name: "timer_reset", })? + .func_wrap("env", "timer_current_split_index", { + |caller: Caller<'_, Context>| { + caller + .data() + .timer + .current_split_index() + .map_or(-1, |i| i as i32) + } + }) + .map_err(|source| CreationError::LinkFunction { + source, + name: "timer_current_split_index", + })? .func_wrap("env", "timer_set_variable", { |mut caller: Caller<'_, Context>, name_ptr: u32, diff --git a/src/auto_splitting/mod.rs b/src/auto_splitting/mod.rs index dd13f4dd..6970c2b8 100644 --- a/src/auto_splitting/mod.rs +++ b/src/auto_splitting/mod.rs @@ -110,6 +110,12 @@ //! pub fn timer_undo_split(); //! /// Resets the timer. //! pub fn timer_reset(); +//! /// Accesses the index of the split the attempt is currently on. If there's +//! /// no attempt in progress, `-1` is returned instead. This returns an +//! /// index that is equal to the amount of segments when the attempt is +//! /// finished, but has not been reset. So you need to be careful when using +//! /// this value for indexing. +//! pub fn timer_current_split_index() -> i32; //! /// Sets a custom key value pair. This may be arbitrary information that the //! /// auto splitter wants to provide for visualization. The pointers need to //! /// point to valid UTF-8 encoded text with the respective given length.