Skip to content

Commit 2f404c3

Browse files
authored
Expose all the comparisons in the C API (#811)
This exposes all the comparisons in the C API and makes it possible to set the comparison via a string. This will enable LiveSplit One to show a list of comparisons to choose from.
1 parent cc188ff commit 2f404c3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

capi/src/run.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ pub extern "C" fn Run_custom_comparison(this: &Run, index: usize) -> *const c_ch
279279
output_str(&this.custom_comparisons()[index])
280280
}
281281

282+
/// Returns the amount of total comparisons stored in this Run.
283+
#[no_mangle]
284+
pub extern "C" fn Run_comparisons_len(this: &Run) -> usize {
285+
this.custom_comparisons().len() + this.comparison_generators().len()
286+
}
287+
288+
/// Accesses a comparison stored in this Run by its index. This includes both
289+
/// custom comparisons as well as all the Comparison Generators. You may not
290+
/// provide an out of bounds index.
291+
#[no_mangle]
292+
pub extern "C" fn Run_comparison(this: &Run, index: usize) -> *const c_char {
293+
let custom_len = this.custom_comparisons().len();
294+
output_str(if index < custom_len {
295+
&this.custom_comparisons()[index]
296+
} else {
297+
this.comparison_generators()[index - custom_len].name()
298+
})
299+
}
300+
282301
/// Accesses the Auto Splitter Settings that are encoded as XML.
283302
#[no_mangle]
284303
pub extern "C" fn Run_auto_splitter_settings(this: &Run) -> *const c_char {

capi/src/timer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ pub extern "C" fn Timer_current_comparison(this: &Timer) -> *const c_char {
217217
output_str(this.current_comparison())
218218
}
219219

220+
/// Tries to set the current comparison to the comparison specified. If the
221+
/// comparison doesn't exist <FALSE> is returned.
222+
#[no_mangle]
223+
pub unsafe extern "C" fn Timer_set_current_comparison(
224+
this: &mut Timer,
225+
comparison: *const c_char,
226+
) -> bool {
227+
this.set_current_comparison(str(comparison)).is_ok()
228+
}
229+
220230
/// Switches the current comparison to the next comparison in the list.
221231
#[no_mangle]
222232
pub extern "C" fn Timer_switch_to_next_comparison(this: &mut Timer) {

0 commit comments

Comments
 (0)