//! Flutter <-> Rust bridge. //! //! Two layers live here: //! - stateless probe functions (ABI marker, self test) used to verify the //! build pipeline and cross-target determinism, //! - [`match_api`], the stateful API the Flame client drives. #![allow(clippy::not_unsafe_ptr_arg_deref)] pub mod match_api; /// Magic marker to verify that the CDylib was loaded and symbols resolve. pub const ABI_MARKER: u32 = 0x1A2B_3C4D; /// Returns [`ABI_MARKER`]. #[no_mangle] pub extern "C" fn iron_arc_abi_marker() -> u32 { ABI_MARKER } /// Runs a fixed scripted match and returns its state hash. #[no_mangle] pub extern "C" fn iron_arc_selftest(seed: u64) -> u64 { let mut sim = sim::state::Sim::default_match(seed); let _ = sim.submit(sim::state::Command::Fire { angle_mdeg: 45_000, power: sim::fixed::from_int(1), weapon: 1, }); for _ in 0..300 { sim.tick(); } sim.state_hash() } /// Returns the simulation version as a packed `major*10000 + minor*100 + patch`. #[no_mangle] pub extern "C" fn iron_arc_sim_version_code() -> u32 { let mut parts = sim::SIM_VERSION .split('.') .filter_map(|p| p.parse::().ok()); let major = parts.next().unwrap_or(0); let minor = parts.next().unwrap_or(0); let patch = parts.next().unwrap_or(0); major * 10_000 + minor * 100 + patch } #[cfg(test)] mod tests { use super::*; #[test] fn marker_is_stable() { assert_eq!(iron_arc_abi_marker(), ABI_MARKER); } #[test] fn selftest_is_deterministic() { assert_eq!(iron_arc_selftest(42), iron_arc_selftest(42)); assert_ne!(iron_arc_selftest(42), iron_arc_selftest(43)); } #[test] fn version_code_is_packed() { assert_eq!(iron_arc_sim_version_code(), 100); } #[test] fn replay_export_reproduces_state() { unsafe { let handle = match_api::iron_arc_new_match(77); assert_eq!(match_api::iron_arc_fire(handle, 45_000, 65_536, 1), 0); match_api::iron_arc_tick(handle, 400); let hash = match_api::iron_arc_state_hash(handle); let json_ptr = match_api::iron_arc_replay_json(handle); assert!(!json_ptr.is_null(), "replay json must be produced"); let json = std::ffi::CStr::from_ptr(json_ptr) .to_str() .expect("utf8") .to_string(); match_api::iron_arc_string_free(json_ptr); assert!(json.contains(r#""commands""#)); let c_json = std::ffi::CString::new(json).expect("no nul bytes"); let playback = match_api::iron_arc_new_match_playback(c_json.as_ptr()); assert!(!playback.is_null(), "playback handle must be created"); assert_eq!(match_api::iron_arc_is_playback(playback), 1); match_api::iron_arc_tick(playback, 400); assert_eq!( match_api::iron_arc_state_hash(playback), hash, "playback must reproduce the identical state" ); match_api::iron_arc_free_match(playback); match_api::iron_arc_free_match(handle); } } #[test] fn playback_rejects_foreign_content() { unsafe { let json = r#"{"v":1,"sim_version":"0.1.0","content_hash":"fnv1a:0000000000000000","seed":1,"mode":"hotseat","players":[{"tank":"a","loadout":[1,2,5]},{"tank":"b","loadout":[1,3,4]}],"commands":[],"final_hash":0,"duration_ticks":0}"#; let c_json = std::ffi::CString::new(json).unwrap(); let handle = match_api::iron_arc_new_match_playback(c_json.as_ptr()); assert!(handle.is_null(), "content mismatch must be rejected"); } } #[test] fn match_handle_roundtrip() { unsafe { let handle = match_api::iron_arc_new_match(7); assert!(!handle.is_null()); assert_eq!(match_api::iron_arc_phase(handle), 0); assert_eq!(match_api::iron_arc_current(handle), 0); assert_eq!(match_api::iron_arc_fire(handle, 45_000, 65_536, 1), 0); assert_eq!(match_api::iron_arc_phase(handle), 1); let mut xy = [0i32; 2]; assert_eq!(match_api::iron_arc_projectile(handle, xy.as_mut_ptr()), 1); let impacts = match_api::iron_arc_tick(handle, 400); assert!(impacts >= 1, "expected an impact, got {impacts}"); assert!(!match_api::iron_arc_terrain_ptr(handle).is_null()); match_api::iron_arc_free_match(handle); } } }