use std::ffi::{CStr, CString};
use after_effects_sys::{A_char, PF_Err, PF_Err_BAD_CALLBACK_PARAM, PF_Err_INTERNAL_STRUCT_DAMAGED, PF_Err_NONE};
#[cfg(feature = "diagnostics")]
use crate::core::diagnostics::DiagnosticBuilder;
macro_rules! impl_math_sys {
($name:ident, $func:ident, $arg:literal, $diag_path:literal) => {
#[inline(always)]
pub(crate) extern "C" fn $name(x: f64) -> f64 {
let result = x.$func();
#[cfg(feature = "diagnostics")]
DiagnosticBuilder::new()
.set_name($diag_path)
.add_arg($arg, x)
.set_result(result)
.emit();
result
}
};
($name:ident, $func:ident, $arg1:literal, $arg2:literal, $diag_path:literal) => {
#[inline(always)]
pub(crate) extern "C" fn $name(a: f64, b: f64) -> f64 {
let result = a.$func(b);
#[cfg(feature = "diagnostics")]
DiagnosticBuilder::new()
.set_name($diag_path)
.add_arg($arg1, a)
.add_arg($arg2, b)
.set_result(result)
.emit();
result
}
};
}
impl_math_sys!(atan_sys, atan, "x", "InData/utils/ansi/atan");
impl_math_sys!(atan2_sys, atan2, "y", "x", "InData/utils/ansi/atan2");
impl_math_sys!(ceil_sys, ceil, "x", "InData/utils/ansi/ceil");
impl_math_sys!(cos_sys, cos, "x", "InData/utils/ansi/cos");
impl_math_sys!(exp_sys, exp, "x", "InData/utils/ansi/exp");
impl_math_sys!(fabs_sys, abs, "x", "InData/utils/ansi/fabs");
impl_math_sys!(floor_sys, floor, "x", "InData/utils/ansi/floor");
impl_math_sys!(fmod_sys, rem_euclid, "x", "y", "InData/utils/ansi/fmod");
impl_math_sys!(hypot_sys, hypot, "x", "y", "InData/utils/ansi/hypot");
impl_math_sys!(log_sys, ln, "x", "InData/utils/ansi/log");
impl_math_sys!(log10_sys, log10, "x", "InData/utils/ansi/log10");
impl_math_sys!(pow_sys, powf, "x", "y", "InData/utils/ansi/pow");
impl_math_sys!(sin_sys, sin, "x", "InData/utils/ansi/sin");
impl_math_sys!(sqrt_sys, sqrt, "x", "InData/utils/ansi/sqrt");
impl_math_sys!(tan_sys, tan, "x", "InData/utils/ansi/tan");
impl_math_sys!(asin_sys, asin, "x", "InData/utils/ansi/asin");
impl_math_sys!(acos_sys, acos, "x", "InData/utils/ansi/acos");
pub(crate) unsafe extern "C" fn sprintf_sys(arg1: *mut A_char, arg2: *const A_char, mut args: ...) -> PF_Err {
crate::core::ffi::guard("sprintf_sys", || {
const SPRINTF_BUFFER_SIZE: usize = 256;
if arg1.is_null() || arg2.is_null() {
return PF_Err_BAD_CALLBACK_PARAM as PF_Err;
}
let format_str = match unsafe { CStr::from_ptr(arg2) }.to_str() {
Ok(s) => s,
Err(_) => {
return PF_Err_INTERNAL_STRUCT_DAMAGED as PF_Err;
}
};
let mut result = String::new();
let mut chars = format_str.chars().peekable();
#[cfg(feature = "diagnostics")]
let mut diagnostics = DiagnosticBuilder::new();
#[cfg(feature = "diagnostics")]
diagnostics
.set_name("UtilityCallbacks/ansi/sprintf")
.add_arg("format", format!("{:?}", format_str));
while let Some(c) = chars.next() {
if c == '%' {
if let Some(next) = chars.next() {
match next {
'd' => {
let arg = unsafe { args.next_arg::<i32>() };
#[cfg(feature = "diagnostics")]
diagnostics.add_arg("arg_int", format!("{:?}", arg));
result.push_str(&arg.to_string());
}
's' => {
let ptr = unsafe { args.next_arg::<*const i8>() };
if !ptr.is_null() {
match unsafe { CStr::from_ptr(ptr) }.to_str() {
Ok(s) => {
result.push_str(s);
#[cfg(feature = "diagnostics")]
diagnostics.add_arg("arg_str", format!("{:?}", s));
}
Err(_) => result.push_str("(invalid)"),
}
} else {
result.push_str("(null)");
}
}
'%' => {
result.push('%');
}
_ => {
result.push('%');
result.push(next);
}
}
} else {
result.push('%');
}
} else {
result.push(c);
}
}
let c_result = match CString::new(result) {
Ok(s) => s,
Err(_) => {
return PF_Err_INTERNAL_STRUCT_DAMAGED as PF_Err;
}
};
let bytes = c_result.as_bytes_with_nul();
let copy_len = bytes.len().min(SPRINTF_BUFFER_SIZE);
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), arg1 as *mut u8, copy_len) };
if copy_len == SPRINTF_BUFFER_SIZE && SPRINTF_BUFFER_SIZE > 0 {
unsafe { *((arg1 as *mut u8).add(SPRINTF_BUFFER_SIZE - 1)) = 0 };
}
#[cfg(feature = "diagnostics")]
diagnostics.set_result(format!("{:?}", c_result)).emit();
PF_Err_NONE as PF_Err
})
}
pub unsafe extern "C" fn strcpy_sys(arg1: *mut A_char, arg2: *const A_char) -> *mut A_char {
if arg1.is_null() || arg2.is_null() {
return std::ptr::null_mut();
}
let src = match unsafe { CStr::from_ptr(arg2) }.to_str() {
Ok(s) => s,
Err(_) => return std::ptr::null_mut(),
};
let cstring = match CString::new(src) {
Ok(s) => s,
Err(_) => return std::ptr::null_mut(),
};
let bytes = cstring.as_bytes_with_nul();
unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), arg1 as *mut u8, bytes.len()) };
arg1
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
#[test]
fn atan_test() {
let x = 1.0;
let result = atan_sys(x);
assert!(
(result - std::f64::consts::FRAC_PI_4).abs() < 1e-10,
"Expected atan(1.0) to be close to π/4, got {}",
result
);
}
#[test]
fn atan2_test() {
let y = 1.0;
let x = 1.0;
let result = atan2_sys(y, x);
assert!(
(result - std::f64::consts::FRAC_PI_4).abs() < 1e-10,
"Expected atan2(1.0, 1.0) to be close to π/4, got {}",
result
);
}
#[test]
fn ceil_test() {
let x = 1.5;
let result = ceil_sys(x);
assert!(
(result - 2.0).abs() < 1e-10,
"Expected ceil(1.5) to be close to 2.0, got {}",
result
);
}
#[test]
fn cos_test() {
let x = 0.0;
let result = cos_sys(x);
assert!(
(result - 1.0).abs() < 1e-10,
"Expected cos(0.0) to be close to 1.0, got {}",
result
);
}
#[test]
fn exp_test() {
let x = 1.0;
let result = exp_sys(x);
assert!(
(result - std::f64::consts::E).abs() < 1e-10,
"Expected exp(1.0) to be close to e, got {}",
result
);
}
#[test]
fn fabs_test() {
let x = -3.5;
let result = fabs_sys(x);
assert!(
(result - 3.5).abs() < 1e-10,
"Expected fabs(-3.5) to be close to 3.5, got {}",
result
);
}
#[test]
fn floor_test() {
let x = 1.5;
let result = floor_sys(x);
assert!(
(result - 1.0).abs() < 1e-10,
"Expected floor(1.5) to be close to 1.0, got {}",
result
);
}
#[test]
fn fmod_test() {
let x = 5.5;
let y = 2.0;
let result = fmod_sys(x, y);
assert!(
(result - 1.5).abs() < 1e-10,
"Expected fmod(5.5, 2.0) to be close to 1.5, got {}",
result
);
}
#[test]
fn hypot_test() {
let x = 3.0;
let y = 4.0;
let result = hypot_sys(x, y);
assert!(
(result - 5.0).abs() < 1e-10,
"Expected hypot(3.0, 4.0) to be close to 5.0, got {}",
result
);
}
#[test]
fn log_test() {
let x = std::f64::consts::E;
let result = log_sys(x);
assert!(
(result - 1.0).abs() < 1e-10,
"Expected log(e) to be close to 1.0, got {}",
result
);
}
#[test]
fn log10_test() {
let x = 100.0;
let result = log10_sys(x);
assert!(
(result - 2.0).abs() < 1e-10,
"Expected log10(100) to be close to 2.0, got {}",
result
);
}
#[test]
fn pow_test() {
let x = 2.0;
let y = 3.0;
let result = pow_sys(x, y);
assert!(
(result - 8.0).abs() < 1e-10,
"Expected pow(2.0, 3.0) to be close to 8.0, got {}",
result
);
}
#[test]
fn sin_test() {
let x = std::f64::consts::FRAC_PI_2; let result = sin_sys(x);
assert!(
(result - 1.0).abs() < 1e-10,
"Expected sin(π/2) to be close to 1.0, got {}",
result
);
}
#[test]
fn sqrt_test() {
let x = 16.0;
let result = sqrt_sys(x);
assert!(
(result - 4.0).abs() < 1e-10,
"Expected sqrt(16) to be close to 4.0, got {}",
result
);
}
#[test]
fn tan_test() {
let x = std::f64::consts::FRAC_PI_4; let result = tan_sys(x);
assert!(
(result - 1.0).abs() < 1e-10,
"Expected tan(π/4) to be close to 1.0, got {}",
result
);
}
macro_rules! sprintf {
($fmt:expr $(, $arg:expr)* $(,)?) => {{
let fmt = CString::new($fmt).unwrap();
let mut buffer = [0 as A_char; 256];
let err = unsafe { sprintf_sys(buffer.as_mut_ptr(), fmt.as_ptr() $(, $arg)*) };
assert_eq!(err, PF_Err_NONE as PF_Err, "sprintf_sys returned error {err}");
unsafe { CStr::from_ptr(buffer.as_ptr()) }.to_str().unwrap().to_owned()
}};
}
#[test]
fn sprintf_no_specifier_copies_verbatim() {
assert_eq!(sprintf!("Hello, world!"), "Hello, world!");
}
#[test]
fn sprintf_single_int() {
assert_eq!(sprintf!("%d", 42i32), "42");
}
#[test]
fn sprintf_several_ints() {
assert_eq!(sprintf!("%d + %d = %d", 2i32, 3i32, 5i32), "2 + 3 = 5");
}
#[test]
fn sprintf_negative_int() {
assert_eq!(sprintf!("%d", -7i32), "-7");
}
#[test]
fn sprintf_string() {
let s = CString::new("aexlo").unwrap();
assert_eq!(sprintf!("%s", s.as_ptr()), "aexlo");
}
#[test]
fn sprintf_null_string_arg() {
assert_eq!(sprintf!("%s", std::ptr::null::<A_char>()), "(null)");
}
#[test]
fn sprintf_mixed_specifiers() {
let name = CString::new("frame").unwrap();
assert_eq!(sprintf!("%s=%d", name.as_ptr(), 7i32), "frame=7");
}
#[test]
fn sprintf_literal_percent() {
assert_eq!(sprintf!("100%% done"), "100% done");
}
#[test]
fn sprintf_unsupported_specifier_passthrough() {
assert_eq!(sprintf!("%q"), "%q");
}
#[test]
fn sprintf_trailing_percent() {
assert_eq!(sprintf!("done%"), "done%");
}
#[test]
fn sprintf_null_buffer_is_bad_callback_param() {
let fmt = CString::new("%d").unwrap();
let err = unsafe { sprintf_sys(std::ptr::null_mut(), fmt.as_ptr(), 1i32) };
assert_eq!(err, PF_Err_BAD_CALLBACK_PARAM as PF_Err);
}
#[test]
fn sprintf_null_format_is_bad_callback_param() {
let mut buffer = [0 as A_char; 256];
let err = unsafe { sprintf_sys(buffer.as_mut_ptr(), std::ptr::null()) };
assert_eq!(err, PF_Err_BAD_CALLBACK_PARAM as PF_Err);
}
#[test]
fn strcpy_test() {
let src = std::ffi::CString::new("Hello, world!").unwrap();
let mut buffer = [0i8; 256];
let result_ptr = unsafe { strcpy_sys(buffer.as_mut_ptr(), src.as_ptr()) };
assert!(!result_ptr.is_null(), "Expected strcpy to return a non-null pointer");
let result_str = unsafe { CStr::from_ptr(result_ptr).to_str().unwrap() };
assert!(
result_str == src.to_str().unwrap(),
"Expected strcpy to copy the string correctly, got {:?}",
result_str
);
}
#[test]
fn asin_test() {
let x = 0.5;
let result = asin_sys(x);
assert!(
(result - std::f64::consts::FRAC_PI_6).abs() < 1e-10,
"Expected asin(0.5) to be close to π/6, got {}",
result
);
}
#[test]
fn acos_test() {
let x = 0.5;
let result = acos_sys(x);
assert!(
(result - std::f64::consts::FRAC_PI_3).abs() < 1e-10,
"Expected acos(0.5) to be close to π/3, got {}",
result
);
}
}