From 4cc0e0d23e899e5d410e630b42a6bbc5d01a28fa Mon Sep 17 00:00:00 2001 From: Dark Date: Sat, 8 Jan 2022 01:52:28 -0500 Subject: [PATCH] Support Auto Splitting in the C API --- capi/Cargo.toml | 1 + capi/bind_gen/src/c.rs | 2 +- capi/bind_gen/src/swift/code.rs | 2 +- capi/cdylib/Cargo.toml | 1 + capi/src/auto_splitting_runtime.rs | 72 ++++++++++++++++++++++++++++++ capi/src/lib.rs | 1 + capi/staticlib/Cargo.toml | 1 + 7 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 capi/src/auto_splitting_runtime.rs diff --git a/capi/Cargo.toml b/capi/Cargo.toml index 138cfc3f..6f55727a 100644 --- a/capi/Cargo.toml +++ b/capi/Cargo.toml @@ -14,3 +14,4 @@ default = ["image-shrinking"] image-shrinking = ["livesplit-core/image-shrinking"] software-rendering = ["livesplit-core/software-rendering"] wasm-web = ["livesplit-core/wasm-web"] +auto-splitting = ["livesplit-core/auto-splitting"] diff --git a/capi/bind_gen/src/c.rs b/capi/bind_gen/src/c.rs index 24802142..c2ea0e14 100644 --- a/capi/bind_gen/src/c.rs +++ b/capi/bind_gen/src/c.rs @@ -16,7 +16,7 @@ fn get_type(ty: &Type) -> Cow<'_, str> { "u32" => "uint32_t", "u64" => "uint64_t", "usize" => "size_t", - "isize" => "ssize_t", + "isize" => "ptrdiff_t", "f32" => "float", "f64" => "double", "bool" => "bool", diff --git a/capi/bind_gen/src/swift/code.rs b/capi/bind_gen/src/swift/code.rs index bf0afcde..890c9f5b 100644 --- a/capi/bind_gen/src/swift/code.rs +++ b/capi/bind_gen/src/swift/code.rs @@ -37,7 +37,7 @@ fn get_ll_type(ty: &Type) -> &str { "u32" => "UInt32", "u64" => "UInt64", "usize" => "size_t", - "isize" => "ssize_t", + "isize" => "ptrdiff_t", "f32" => "Float", "f64" => "Double", "bool" => "Bool", diff --git a/capi/cdylib/Cargo.toml b/capi/cdylib/Cargo.toml index 0717648d..8aaf3db6 100644 --- a/capi/cdylib/Cargo.toml +++ b/capi/cdylib/Cargo.toml @@ -16,3 +16,4 @@ default = ["image-shrinking"] image-shrinking = ["livesplit-core-capi/image-shrinking"] software-rendering = ["livesplit-core-capi/software-rendering"] wasm-web = ["livesplit-core-capi/wasm-web"] +auto-splitting = ["livesplit-core-capi/auto-splitting"] diff --git a/capi/src/auto_splitting_runtime.rs b/capi/src/auto_splitting_runtime.rs new file mode 100644 index 00000000..7b4c26c0 --- /dev/null +++ b/capi/src/auto_splitting_runtime.rs @@ -0,0 +1,72 @@ +//! With an Auto Splitting Runtime, the runner can use an Auto Splitter to +//! automatically control the timer on systems that are supported. + +use super::str; +use crate::shared_timer::OwnedSharedTimer; +use std::os::raw::c_char; +use std::path::PathBuf; + +#[cfg(feature = "auto-splitting")] +use livesplit_core::auto_splitting::Runtime as AutoSplittingRuntime; + +#[cfg(not(feature = "auto-splitting"))] +use livesplit_core::SharedTimer; + +#[cfg(not(feature = "auto-splitting"))] +#[allow(missing_docs)] +pub struct AutoSplittingRuntime; + +#[allow(missing_docs)] +#[cfg(not(feature = "auto-splitting"))] +impl AutoSplittingRuntime { + pub fn new(_: SharedTimer) -> Self { + Self + } + + pub fn unload_script(&self) -> Result<(), ()> { + Err(()) + } + + pub fn load_script(&self, _: PathBuf) -> Result<(), ()> { + Err(()) + } +} + +/// type +pub type OwnedAutoSplittingRuntime = Box; +/// type +pub type NullableOwnedAutoSplittingRuntime = Option; + +/// Creates a new Auto Splitting Runtime for a Timer. +#[no_mangle] +pub extern "C" fn AutoSplittingRuntime_new( + shared_timer: OwnedSharedTimer, +) -> OwnedAutoSplittingRuntime { + Box::new(AutoSplittingRuntime::new(*shared_timer)) +} + +/// Attempts to load an auto splitter. Returns true if successful. +#[no_mangle] +pub unsafe extern "C" fn AutoSplittingRuntime_load_script( + this: &AutoSplittingRuntime, + path: *const c_char, +) -> bool { + let path = str(path); + if !path.is_empty() { + this.load_script(PathBuf::from(path)).is_ok() + } else { + false + } +} + +/// Attempts to unload the auto splitter. Returns true if successful. +#[no_mangle] +pub extern "C" fn AutoSplittingRuntime_unload_script(this: &AutoSplittingRuntime) -> bool { + this.unload_script().is_ok() +} + +/// drop +#[no_mangle] +pub extern "C" fn AutoSplittingRuntime_drop(this: OwnedAutoSplittingRuntime) { + drop(this); +} diff --git a/capi/src/lib.rs b/capi/src/lib.rs index 5247151a..e3437728 100644 --- a/capi/src/lib.rs +++ b/capi/src/lib.rs @@ -12,6 +12,7 @@ use std::{ }; pub mod analysis; +pub mod auto_splitting_runtime; pub mod atomic_date_time; pub mod attempt; pub mod blank_space_component; diff --git a/capi/staticlib/Cargo.toml b/capi/staticlib/Cargo.toml index 7b52fcf0..2e5defcc 100644 --- a/capi/staticlib/Cargo.toml +++ b/capi/staticlib/Cargo.toml @@ -16,3 +16,4 @@ default = ["image-shrinking"] image-shrinking = ["livesplit-core-capi/image-shrinking"] software-rendering = ["livesplit-core-capi/software-rendering"] wasm-web = ["livesplit-core-capi/wasm-web"] +auto-splitting = ["livesplit-core-capi/auto-splitting"]