livesplit-core 0.13.0

livesplit-core is a library that provides a lot of functionality for creating a speedrun timer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! The auto splitting module provides a runtime for running auto splitters that
//! can control the [`Timer`](crate::timing::Timer). These auto splitters are
//! provided as WebAssembly modules.
//!
//! # Requirements for the Auto Splitters
//!
//! The auto splitters must provide an `update` function with the following
//! signature:
//!
//! ```rust
//! #[no_mangle]
//! pub extern "C" fn update() {}
//! ```
//!
//! This function is called periodically by the runtime at the configured tick
//! rate. The tick rate is 120 Hz by default, but can be changed by the auto
//! splitter.
//!
//! In addition the WebAssembly module is expected to export a memory called
//! `memory`.
//!
//! # API exposed to the Auto Splitters
//!
//! The following functions are provided to the auto splitters in the module
//! `env`:
//!
//! ```rust
//! # use core::num::NonZeroU64;
//!
//! #[repr(transparent)]
//! pub struct Address(pub u64);
//!
//! #[repr(transparent)]
//! pub struct NonZeroAddress(pub NonZeroU64);
//!
//! #[repr(transparent)]
//! pub struct ProcessId(NonZeroU64);
//!
//! #[repr(transparent)]
//! pub struct TimerState(u32);
//!
//! impl TimerState {
//!     /// The timer is not running.
//!     pub const NOT_RUNNING: Self = Self(0);
//!     /// The timer is running.
//!     pub const RUNNING: Self = Self(1);
//!     /// The timer started but got paused. This is separate from the game
//!     /// time being paused. Game time may even always be paused.
//!     pub const PAUSED: Self = Self(2);
//!     /// The timer has ended, but didn't get reset yet.
//!     pub const ENDED: Self = Self(3);
//! }
//!
//! extern "C" {
//!     /// Gets the state that the timer currently is in.
//!     pub fn timer_get_state() -> TimerState;
//!
//!     /// Starts the timer.
//!     pub fn timer_start();
//!     /// Splits the current segment.
//!     pub fn timer_split();
//!     /// Resets the timer.
//!     pub fn timer_reset();
//!     /// Sets a custom key value pair. This may be arbitrary information that
//!     /// the auto splitter wants to provide for visualization.
//!     pub fn timer_set_variable(
//!         key_ptr: *const u8,
//!         key_len: usize,
//!         value_ptr: *const u8,
//!         value_len: usize,
//!     );
//!
//!     /// Sets the game time.
//!     pub fn timer_set_game_time(secs: i64, nanos: i32);
//!     /// Pauses the game time. This does not pause the timer, only the
//!     /// automatic flow of time for the game time.
//!     pub fn timer_pause_game_time();
//!     /// Resumes the game time. This does not resume the timer, only the
//!     /// automatic flow of time for the game time.
//!     pub fn timer_resume_game_time();
//!
//!     /// Attaches to a process based on its name.
//!     pub fn process_attach(name_ptr: *const u8, name_len: usize) -> Option<ProcessId>;
//!     /// Detaches from a process.
//!     pub fn process_detach(process: ProcessId);
//!     /// Checks whether is a process is still open. You should detach from a
//!     /// process and stop using it if this returns `false`.
//!     pub fn process_is_open(process: ProcessId) -> bool;
//!     /// Reads memory from a process at the address given. This will write
//!     /// the memory to the buffer given. Returns `false` if this fails.
//!     pub fn process_read(
//!         process: ProcessId,
//!         address: Address,
//!         buf_ptr: *mut u8,
//!         buf_len: usize,
//!     ) -> bool;
//!     /// Gets the address of a module in a process.
//!     pub fn process_get_module_address(
//!         process: ProcessId,
//!         name_ptr: *const u8,
//!         name_len: usize,
//!     ) -> Option<NonZeroAddress>;
//!     /// Gets the size of a module in a process.
//!     pub fn process_get_module_size(
//!         process: ProcessId,
//!         name_ptr: *const u8,
//!         name_len: usize,
//!     ) -> Option<NonZeroU64>;
//!
//!     /// Sets the tick rate of the runtime. This influences the amount of
//!     /// times the `update` function is called per second.
//!     pub fn runtime_set_tick_rate(ticks_per_second: f64);
//!     /// Prints a log message for debugging purposes.
//!     pub fn runtime_print_message(text_ptr: *const u8, text_len: usize);
//!
//!     /// Adds a new setting that the user can modify. This will return either
//!     /// the specified default value or the value that the user has set.
//!     pub fn user_settings_add_bool(
//!         key_ptr: *const u8,
//!         key_len: usize,
//!         description_ptr: *const u8,
//!         description_len: usize,
//!         default_value: bool,
//!     ) -> bool;
//! }
//! ```

use crate::timing::{SharedTimer, TimerPhase};
use livesplit_auto_splitting::{
    CreationError, InterruptHandle, Runtime as ScriptRuntime, SettingsStore,
    Timer as AutoSplitTimer, TimerState,
};
use snafu::Snafu;
use std::{fmt, fs, io, path::PathBuf, thread, time::Duration};
use tokio::{
    runtime,
    sync::{mpsc, oneshot, watch},
    time::{timeout_at, Instant},
};

/// An error that the [`Runtime`] can return.
#[derive(Debug, Snafu)]
pub enum Error {
    /// The runtime thread unexpectedly stopped.
    ThreadStopped,
    /// Failed loading the auto splitter.
    LoadFailed {
        /// The underlying error.
        source: CreationError,
    },
    /// Failed reading the auto splitter file.
    ReadFileFailed {
        /// The underlying error.
        source: io::Error,
    },
}

/// An auto splitter runtime that allows using an auto splitter provided as a
/// WebAssembly module to control a timer.
pub struct Runtime {
    interrupt_receiver: watch::Receiver<Option<InterruptHandle>>,
    sender: mpsc::UnboundedSender<Request>,
}

impl Drop for Runtime {
    fn drop(&mut self) {
        if let Some(handle) = &*self.interrupt_receiver.borrow() {
            handle.interrupt();
        }
    }
}

impl Runtime {
    /// Starts the runtime. Doesn't actually load an auto splitter until
    /// [`load_script`][Runtime::load_script] is called.
    pub fn new(timer: SharedTimer) -> Self {
        let (sender, receiver) = mpsc::unbounded_channel();
        let (interrupt_sender, interrupt_receiver) = watch::channel(None);
        let (timeout_sender, timeout_receiver) = watch::channel(None);

        thread::Builder::new()
            .name("Auto Splitting Runtime".into())
            .spawn(move || {
                runtime::Builder::new_current_thread()
                    .enable_time()
                    .build()
                    .unwrap()
                    .block_on(run(receiver, timer, timeout_sender, interrupt_sender))
            })
            .unwrap();

        thread::Builder::new()
            .name("Auto Splitting Watchdog".into())
            .spawn({
                let interrupt_receiver = interrupt_receiver.clone();
                move || {
                    runtime::Builder::new_current_thread()
                        .enable_time()
                        .build()
                        .unwrap()
                        .block_on(watchdog(timeout_receiver, interrupt_receiver))
                }
            })
            .unwrap();

        Self {
            interrupt_receiver,
            sender,
        }
    }

    /// Attempts to load a wasm file containing an auto splitter module. This
    /// call will block until the auto splitter has either loaded successfully
    /// or failed.
    pub async fn load_script(&self, script: PathBuf) -> Result<(), Error> {
        let (sender, receiver) = oneshot::channel();
        let script = fs::read(script).map_err(|e| Error::ReadFileFailed { source: e })?;
        self.sender
            .send(Request::LoadScript(script, sender))
            .map_err(|_| Error::ThreadStopped)?;

        receiver.await.map_err(|_| Error::ThreadStopped)??;

        Ok(())
    }

    /// Attempts to load a wasm file containing an auto splitter module. This
    /// call will block until the auto splitter has either loaded successfully
    /// or failed.
    pub fn load_script_blocking(&self, script: PathBuf) -> Result<(), Error> {
        runtime::Builder::new_current_thread()
            .enable_time()
            .build()
            .unwrap()
            .block_on(self.load_script(script))
    }

    /// Unloads the current auto splitter. This will _not_ return an error if
    /// there isn't currently an auto splitter loaded, only if the runtime
    /// thread stops unexpectedly.
    pub async fn unload_script(&self) -> Result<(), Error> {
        let (sender, receiver) = oneshot::channel();
        self.sender
            .send(Request::UnloadScript(sender))
            .map_err(|_| Error::ThreadStopped)?;

        receiver.await.map_err(|_| Error::ThreadStopped)
    }

    /// Unloads the current auto splitter. This will _not_ return an error if
    /// there isn't currently an auto splitter loaded, only if the runtime
    /// thread stops unexpectedly.
    pub fn unload_script_blocking(&self) -> Result<(), Error> {
        runtime::Builder::new_current_thread()
            .enable_time()
            .build()
            .unwrap()
            .block_on(self.unload_script())
    }
}

enum Request {
    LoadScript(Vec<u8>, oneshot::Sender<Result<(), Error>>),
    UnloadScript(oneshot::Sender<()>),
}

// This newtype is required because [`SharedTimer`](crate::timing::SharedTimer)
// is an Arc<RwLock<T>>, so we can't implement the trait directly on it.
struct Timer(SharedTimer);

impl AutoSplitTimer for Timer {
    fn state(&self) -> TimerState {
        match self.0.read().unwrap().current_phase() {
            TimerPhase::NotRunning => TimerState::NotRunning,
            TimerPhase::Running => TimerState::Running,
            TimerPhase::Paused => TimerState::Paused,
            TimerPhase::Ended => TimerState::Ended,
        }
    }

    fn start(&mut self) {
        self.0.write().unwrap().start()
    }

    fn split(&mut self) {
        self.0.write().unwrap().split()
    }

    fn reset(&mut self) {
        self.0.write().unwrap().reset(true)
    }

    fn set_game_time(&mut self, time: time::Duration) {
        self.0.write().unwrap().set_game_time(time.into());
    }

    fn pause_game_time(&mut self) {
        self.0.write().unwrap().pause_game_time()
    }

    fn resume_game_time(&mut self) {
        self.0.write().unwrap().resume_game_time()
    }

    fn set_variable(&mut self, name: &str, value: &str) {
        self.0.write().unwrap().set_custom_variable(name, value)
    }

    fn log(&mut self, message: fmt::Arguments<'_>) {
        log::info!(target: "Auto Splitter", "{message}");
    }
}

async fn run(
    mut receiver: mpsc::UnboundedReceiver<Request>,
    timer: SharedTimer,
    timeout_sender: watch::Sender<Option<Instant>>,
    interrupt_sender: watch::Sender<Option<InterruptHandle>>,
) {
    'back_to_not_having_a_runtime: loop {
        interrupt_sender.send(None).ok();
        timeout_sender.send(None).ok();

        let mut runtime = loop {
            match receiver.recv().await {
                Some(Request::LoadScript(script, ret)) => {
                    match ScriptRuntime::new(&script, Timer(timer.clone()), SettingsStore::new()) {
                        Ok(r) => {
                            ret.send(Ok(())).ok();
                            break r;
                        }
                        Err(source) => {
                            ret.send(Err(Error::LoadFailed { source })).ok();
                        }
                    };
                }
                Some(Request::UnloadScript(ret)) => {
                    log::warn!(target: "Auto Splitter", "Attempted to unload already unloaded script");
                    ret.send(()).ok();
                }
                None => {
                    return;
                }
            };
        };

        log::info!(target: "Auto Splitter", "Loaded script");
        let mut next_step = Instant::now();
        interrupt_sender.send(Some(runtime.interrupt_handle())).ok();
        timeout_sender.send(Some(next_step)).ok();

        loop {
            match timeout_at(next_step, receiver.recv()).await {
                Ok(Some(request)) => match request {
                    Request::LoadScript(script, ret) => {
                        match ScriptRuntime::new(
                            &script,
                            Timer(timer.clone()),
                            SettingsStore::new(),
                        ) {
                            Ok(r) => {
                                ret.send(Ok(())).ok();
                                runtime = r;
                                log::info!(target: "Auto Splitter", "Reloaded script");
                            }
                            Err(source) => {
                                ret.send(Err(Error::LoadFailed { source })).ok();
                                log::info!(target: "Auto Splitter", "Failed to load");
                            }
                        }
                    }
                    Request::UnloadScript(ret) => {
                        ret.send(()).ok();
                        log::info!(target: "Auto Splitter", "Unloaded script");
                        continue 'back_to_not_having_a_runtime;
                    }
                },
                Ok(None) => return,
                Err(_) => match runtime.update() {
                    Ok(tick_rate) => {
                        next_step += tick_rate;
                        timeout_sender.send(Some(next_step)).ok();
                    }
                    Err(e) => {
                        log::error!(target: "Auto Splitter", "Unloaded due to failure: {:?}", e);
                        continue 'back_to_not_having_a_runtime;
                    }
                },
            }
        }
    }
}

async fn watchdog(
    mut timeout_receiver: watch::Receiver<Option<Instant>>,
    interrupt_receiver: watch::Receiver<Option<InterruptHandle>>,
) {
    const TIMEOUT: Duration = Duration::from_secs(5);

    loop {
        let instant = *timeout_receiver.borrow();
        match instant {
            Some(time) => match timeout_at(time + TIMEOUT, timeout_receiver.changed()).await {
                Ok(Ok(_)) => {}
                Ok(Err(_)) => return,
                Err(_) => {
                    if let Some(handle) = &*interrupt_receiver.borrow() {
                        handle.interrupt();
                    }
                }
            },
            None => {
                if timeout_receiver.changed().await.is_err() {
                    return;
                }
            }
        }
    }
}