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

Add timer_current_split_index #747

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ log = { version = "0.4.14", default-features = false, optional = true }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
# WebAssembly in the Web
js-sys = { version = "0.3.55", optional = true }
js-sys = { version = "0.3.65", optional = true }
wasm-bindgen = { version = "0.2.78", optional = true }
wasm-bindgen-futures = { version = "0.4.28", optional = true }
web-sys = { version = "0.3.28", default-features = false, features = [
web-sys = { version = "0.3.65", default-features = false, features = [
"Document",
"Performance",
"VisibilityState",
Expand Down
6 changes: 6 additions & 0 deletions crates/livesplit-auto-splitting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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 @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions crates/livesplit-auto-splitting/src/runtime/api/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ pub fn bind<T: Timer>(linker: &mut Linker<Context<T>>) -> Result<(), CreationErr
source,
name: "timer_reset",
})?
.func_wrap("env", "timer_current_split_index", {
|caller: Caller<'_, Context<T>>| {
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<T>>,
name_ptr: u32,
Expand Down
6 changes: 6 additions & 0 deletions crates/livesplit-auto-splitting/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ pub trait Timer: Send {
fn undo_split(&mut self);
/// Resets the timer.
fn reset(&mut self);
/// Accesses the index of the split the attempt is currently on. If there's
/// no attempt in progress, `None` 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.
fn current_split_index(&self) -> Option<usize>;
/// Sets the game time.
fn set_game_time(&mut self, time: time::Duration);
/// Pauses the game time. This does not pause the timer, only the automatic
Expand Down
3 changes: 3 additions & 0 deletions crates/livesplit-auto-splitting/tests/sandboxing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ impl Timer for DummyTimer {
fn skip_split(&mut self) {}
fn undo_split(&mut self) {}
fn reset(&mut self) {}
fn current_split_index(&self) -> Option<usize> {
None
}
fn set_game_time(&mut self, _time: time::Duration) {}
fn pause_game_time(&mut self) {}
fn resume_game_time(&mut self) {}
Expand Down
4 changes: 2 additions & 2 deletions crates/livesplit-hotkey/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ x11-dl = { version = "2.20.0", optional = true }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
wasm-bindgen = { version = "0.2.54", optional = true }
web-sys = { version = "0.3.28", default-features = false, features = [
web-sys = { version = "0.3.65", default-features = false, features = [
"EventTarget",
"Gamepad",
"GamepadButton",
"KeyboardEvent",
"Navigator",
"Window",
], optional = true }
js-sys = { version = "0.3.28", default-features = false, optional = true }
js-sys = { version = "0.3.65", default-features = false, optional = true }

[dependencies]
cfg-if = "1.0.0"
Expand Down
10 changes: 10 additions & 0 deletions src/auto_splitting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -807,6 +813,10 @@ impl<E: event::CommandSink + TimerQuery + Send> AutoSplitTimer for Timer<E> {
drop(self.0.reset(None));
}

fn current_split_index(&self) -> Option<usize> {
self.0.get_timer().current_split_index()
}

fn set_game_time(&mut self, time: time::Duration) {
drop(self.0.set_game_time(time.into()));
}
Expand Down
Loading