Skip to content

Commit

Permalink
Support Auto Splitting in the C API
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkRTA committed Jan 8, 2022
1 parent c928d8c commit 4cc0e0d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion capi/bind_gen/src/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion capi/bind_gen/src/swift/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions capi/cdylib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
72 changes: 72 additions & 0 deletions capi/src/auto_splitting_runtime.rs
Original file line number Diff line number Diff line change
@@ -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<AutoSplittingRuntime>;
/// type
pub type NullableOwnedAutoSplittingRuntime = Option<OwnedAutoSplittingRuntime>;

/// 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);
}
1 change: 1 addition & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions capi/staticlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

0 comments on commit 4cc0e0d

Please sign in to comment.