diff --git a/Cargo.toml b/Cargo.toml index ea09ac32..05c3ef4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -100,10 +100,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", diff --git a/capi/Cargo.toml b/capi/Cargo.toml index 4db4de73..7e2a1ac7 100644 --- a/capi/Cargo.toml +++ b/capi/Cargo.toml @@ -15,7 +15,7 @@ time = { version = "0.3.4", default-features = false, features = ["formatting"] simdutf8 = { git = "https://github.com/CryZe/simdutf8", branch = "wasm-ub-panic", default-features = false } wasm-bindgen = { version = "0.2.78", optional = true } -web-sys = { version = "0.3.28", optional = true } +web-sys = { version = "0.3.65", optional = true } [features] default = ["image-shrinking"] diff --git a/capi/src/web_event_sink.rs b/capi/src/web_event_sink.rs index 94adf56d..84c39ed3 100644 --- a/capi/src/web_event_sink.rs +++ b/capi/src/web_event_sink.rs @@ -38,6 +38,7 @@ pub struct WebEventSink { resume_game_time: Option, set_custom_variable: Option, current_phase: Function, + current_split_index: Option, } #[wasm_bindgen] @@ -64,6 +65,7 @@ impl WebEventSink { resume_game_time: get_func(&obj, "resumeGameTime"), set_custom_variable: get_func(&obj, "setCustomVariable"), current_phase: get_func(&obj, "currentPhase").unwrap(), + current_split_index: get_func(&obj, "currentSplitIndex"), obj, } } @@ -216,4 +218,17 @@ impl TimerQuery for WebEventSink { _ => panic!("Unknown TimerPhase"), } } + fn current_split_index(&self) -> Option { + let i = self + .current_split_index + .as_ref()? + .call0(&self.obj) + .ok()? + .as_f64()?; + if i >= 0.0 { + Some(i as usize) + } else { + None + } + } } diff --git a/crates/livesplit-auto-splitting/src/timer.rs b/crates/livesplit-auto-splitting/src/timer.rs index d5052a5e..674acb96 100644 --- a/crates/livesplit-auto-splitting/src/timer.rs +++ b/crates/livesplit-auto-splitting/src/timer.rs @@ -30,6 +30,12 @@ pub trait Timer { 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; /// 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 diff --git a/crates/livesplit-auto-splitting/tests/sandboxing.rs b/crates/livesplit-auto-splitting/tests/sandboxing.rs index 2ce5bf5e..8d4f97a6 100644 --- a/crates/livesplit-auto-splitting/tests/sandboxing.rs +++ b/crates/livesplit-auto-splitting/tests/sandboxing.rs @@ -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 { + None + } fn set_game_time(&mut self, _time: time::Duration) {} fn pause_game_time(&mut self) {} fn resume_game_time(&mut self) {} diff --git a/crates/livesplit-hotkey/Cargo.toml b/crates/livesplit-hotkey/Cargo.toml index b4654987..3fb30868 100644 --- a/crates/livesplit-hotkey/Cargo.toml +++ b/crates/livesplit-hotkey/Cargo.toml @@ -32,7 +32,7 @@ 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", @@ -40,7 +40,7 @@ web-sys = { version = "0.3.28", default-features = false, features = [ "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" diff --git a/src/auto_splitting/mod.rs b/src/auto_splitting/mod.rs index 04c6f55e..175090e7 100644 --- a/src/auto_splitting/mod.rs +++ b/src/auto_splitting/mod.rs @@ -749,6 +749,10 @@ impl AutoSplitTimer for Timer { self.0.reset(None) } + fn current_split_index(&self) -> Option { + self.0.current_split_index() + } + fn set_game_time(&mut self, time: time::Duration) { self.0.set_game_time(time.into()); } diff --git a/src/event.rs b/src/event.rs index e8c44fa4..c3d11be0 100644 --- a/src/event.rs +++ b/src/event.rs @@ -88,6 +88,12 @@ pub trait Sink { pub trait TimerQuery { /// Returns the current Timer Phase. fn current_phase(&self) -> TimerPhase; + /// 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; } #[cfg(feature = "std")] @@ -166,6 +172,9 @@ impl TimerQuery for crate::SharedTimer { fn current_phase(&self) -> TimerPhase { self.read().unwrap().current_phase() } + fn current_split_index(&self) -> Option { + self.read().unwrap().current_split_index() + } } impl Sink for Arc { @@ -242,4 +251,7 @@ impl TimerQuery for Arc { fn current_phase(&self) -> TimerPhase { TimerQuery::current_phase(&**self) } + fn current_split_index(&self) -> Option { + TimerQuery::current_split_index(&**self) + } }