Skip to content

Commit aec843f

Browse files
committed
Add timer_segment_splitted
1 parent bb0c5a9 commit aec843f

6 files changed

Lines changed: 33 additions & 1 deletion

File tree

crates/livesplit-auto-splitting/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ unsafe extern "C" {
106106
/// So you need to be careful when using this value for indexing.
107107
/// Same index does not imply same split on undo and then split.
108108
pub safe fn timer_current_split_index() -> i32;
109+
/// Whether the segment at `idx` was splitted this attempt.
110+
pub safe fn timer_segment_splitted(idx: i32) -> bool;
109111

110112
/// Starts the timer.
111113
pub safe fn timer_start();

crates/livesplit-auto-splitting/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
//! /// So you need to be careful when using this value for indexing.
107107
//! /// Same index does not imply same split on undo and then split.
108108
//! pub safe fn timer_current_split_index() -> i32;
109+
//! /// Whether the segment at `idx` was splitted this attempt.
110+
//! pub safe fn timer_segment_splitted(idx: i32) -> bool;
109111
//!
110112
//! /// Starts the timer.
111113
//! pub safe fn timer_start();

crates/livesplit-auto-splitting/src/runtime/api/timer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::Result;
22
use wasmtime::{Caller, Linker};
33

4-
use crate::{runtime::Context, CreationError, Timer};
4+
use crate::{CreationError, Timer, runtime::Context};
55

66
use super::{get_str, memory_and_context};
77

@@ -27,6 +27,18 @@ pub fn bind<T: Timer>(linker: &mut Linker<Context<T>>) -> Result<(), CreationErr
2727
source,
2828
name: "timer_current_split_index",
2929
})?
30+
.func_wrap("env", "timer_segment_splitted", {
31+
|caller: Caller<'_, Context<T>>, index: i32| {
32+
let Ok(idx) = usize::try_from(index) else {
33+
return Ok(0u32);
34+
};
35+
Ok(caller.data().timer.segment_splitted(idx) as u32)
36+
}
37+
})
38+
.map_err(|source| CreationError::LinkFunction {
39+
source,
40+
name: "timer_segment_splitted",
41+
})?
3042
.func_wrap(
3143
"env",
3244
"timer_start",

crates/livesplit-auto-splitting/src/timer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub trait Timer: Send {
4242
/// So you need to be careful when using this value for indexing.
4343
/// Same index does not imply same split on undo and then split.
4444
fn current_split_index(&self) -> Option<usize>;
45+
/// Whether the segment at `idx` was splitted this attempt.
46+
fn segment_splitted(&self, idx: usize) -> bool;
4547
/// Starts the timer.
4648
fn start(&mut self);
4749
/// Splits the current segment.

crates/livesplit-auto-splitting/tests/sandboxing.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ impl Timer for DummyTimer {
1717
fn current_split_index(&self) -> Option<usize> {
1818
None
1919
}
20+
fn segment_splitted(&self, _idx: usize) -> bool {
21+
false
22+
}
2023
fn start(&mut self) {}
2124
fn split(&mut self) {}
2225
fn skip_split(&mut self) {}

src/auto_splitting/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
//! /// So you need to be careful when using this value for indexing.
107107
//! /// Same index does not imply same split on undo and then split.
108108
//! pub safe fn timer_current_split_index() -> i32;
109+
//! /// Whether the segment given by `idx` was splitted this attempt.
110+
//! pub safe fn timer_segment_splitted(idx: i32) -> bool;
109111
//!
110112
//! /// Starts the timer.
111113
//! pub safe fn timer_start();
@@ -798,6 +800,15 @@ impl<E: event::CommandSink + TimerQuery + Send> AutoSplitTimer for Timer<E> {
798800
self.0.get_timer().current_split_index()
799801
}
800802

803+
fn segment_splitted(&self, idx: usize) -> bool {
804+
self.0
805+
.get_timer()
806+
.run()
807+
.segments()
808+
.get(idx)
809+
.is_some_and(|segment| segment.split_time().real_time.is_some())
810+
}
811+
801812
fn start(&mut self) {
802813
drop(self.0.start());
803814
}

0 commit comments

Comments
 (0)