feat(m5): client-server sync, offline queue and replay playback
This commit is contained in:
@@ -10,5 +10,6 @@ description = "CDylib bridge between Flutter and the deterministic simulation"
|
||||
crate-type = ["cdylib", "staticlib"]
|
||||
|
||||
[dependencies]
|
||||
sim.workspace = true
|
||||
sim = { workspace = true, features = ["serde"] }
|
||||
content.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
@@ -64,6 +64,50 @@ mod tests {
|
||||
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 {
|
||||
|
||||
+121
-1
@@ -4,7 +4,12 @@
|
||||
//! fixed-step loop and reads a snapshot for rendering. No data is pushed from
|
||||
//! Rust; the renderer pulls.
|
||||
|
||||
use sim::state::{Command, Event, Phase, Sim};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::c_char;
|
||||
|
||||
use sim::replay::{CommandRecord, ReplayV1};
|
||||
use sim::state::{Command, Event, Phase, Sim, TankSpec};
|
||||
use sim::terrain::WORLD_W;
|
||||
|
||||
/// Opaque match handle owned by Dart.
|
||||
pub struct MatchHandle {
|
||||
@@ -12,6 +17,8 @@ pub struct MatchHandle {
|
||||
terrain_version: u32,
|
||||
impacts: u32,
|
||||
last_impact: [i32; 2],
|
||||
/// When set, due commands are applied automatically while ticking.
|
||||
playback: Option<(Vec<CommandRecord>, usize)>,
|
||||
}
|
||||
|
||||
impl MatchHandle {
|
||||
@@ -32,6 +39,7 @@ pub extern "C" fn iron_arc_new_match(seed: u64) -> *mut MatchHandle {
|
||||
terrain_version: 0,
|
||||
impacts: 0,
|
||||
last_impact: [0, 0],
|
||||
playback: None,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -97,6 +105,20 @@ pub unsafe extern "C" fn iron_arc_tick(handle: *mut MatchHandle, ticks: u32) ->
|
||||
};
|
||||
let mut impacts = 0;
|
||||
for _ in 0..ticks {
|
||||
// In playback mode, apply every command that is due at this tick.
|
||||
if h.playback.is_some() {
|
||||
let tick = h.sim.tick;
|
||||
let mut pending: Vec<Command> = Vec::new();
|
||||
if let Some((records, index)) = h.playback.as_mut() {
|
||||
while *index < records.len() && records[*index].t <= tick {
|
||||
pending.push(records[*index].c.to_command());
|
||||
*index += 1;
|
||||
}
|
||||
}
|
||||
for command in pending {
|
||||
let _ = h.sim_mut().submit(command);
|
||||
}
|
||||
}
|
||||
for event in h.sim_mut().tick() {
|
||||
if let Event::Impact { x, y, .. } = event {
|
||||
impacts += 1;
|
||||
@@ -268,3 +290,101 @@ pub unsafe extern "C" fn iron_arc_last_impact(handle: *const MatchHandle, out: *
|
||||
}
|
||||
h.impacts
|
||||
}
|
||||
|
||||
/// Serialises the recorded match as replay JSON (`ReplayV1`).
|
||||
///
|
||||
/// The returned string must be released with [`iron_arc_string_free`].
|
||||
///
|
||||
/// # Safety
|
||||
/// `handle` must be a valid pointer from [`iron_arc_new_match`].
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn iron_arc_replay_json(handle: *const MatchHandle) -> *mut c_char {
|
||||
let Some(h) = handle.as_ref() else {
|
||||
return std::ptr::null_mut();
|
||||
};
|
||||
let replay = ReplayV1::capture(&h.sim, "hotseat", h.sim.tick);
|
||||
match serde_json::to_string(&replay) {
|
||||
Ok(json) => match CString::new(json) {
|
||||
Ok(value) => value.into_raw(),
|
||||
Err(_) => std::ptr::null_mut(),
|
||||
},
|
||||
Err(_) => std::ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Frees a string returned by [`iron_arc_replay_json`].
|
||||
///
|
||||
/// # Safety
|
||||
/// `value` must come from this library and must not be used afterwards.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn iron_arc_string_free(value: *mut c_char) {
|
||||
if !value.is_null() {
|
||||
drop(CString::from_raw(value));
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a handle that replays the given replay JSON automatically.
|
||||
///
|
||||
/// Returns null when the JSON is invalid, the simulation version differs or the
|
||||
/// content hash does not match the build.
|
||||
///
|
||||
/// # Safety
|
||||
/// `json` must be a valid, null-terminated UTF-8 string.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn iron_arc_new_match_playback(json: *const c_char) -> *mut MatchHandle {
|
||||
if json.is_null() {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
let Ok(text) = CStr::from_ptr(json).to_str() else {
|
||||
return std::ptr::null_mut();
|
||||
};
|
||||
let Ok(replay) = serde_json::from_str::<ReplayV1>(text) else {
|
||||
return std::ptr::null_mut();
|
||||
};
|
||||
if replay.players.len() != 2 {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
let specs = [
|
||||
TankSpec {
|
||||
x: 220,
|
||||
loadout: replay.players[0].loadout,
|
||||
},
|
||||
TankSpec {
|
||||
x: WORLD_W - 220,
|
||||
loadout: replay.players[1].loadout,
|
||||
},
|
||||
];
|
||||
let content = content::Content::builtin();
|
||||
if content.hash() != replay.content_hash {
|
||||
return std::ptr::null_mut();
|
||||
}
|
||||
let sim = Sim::new(replay.seed, specs, content);
|
||||
Box::into_raw(Box::new(MatchHandle {
|
||||
sim,
|
||||
terrain_version: 0,
|
||||
impacts: 0,
|
||||
last_impact: [0, 0],
|
||||
playback: Some((replay.commands.clone(), 0)),
|
||||
}))
|
||||
}
|
||||
|
||||
/// True (1) when this handle replays a recorded match.
|
||||
///
|
||||
/// # Safety
|
||||
/// `handle` must be a valid pointer from this library.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn iron_arc_is_playback(handle: *const MatchHandle) -> i32 {
|
||||
match handle.as_ref() {
|
||||
Some(h) => i32::from(h.playback.is_some()),
|
||||
None => 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Current state hash (used by the client to detect desync with the server).
|
||||
///
|
||||
/// # Safety
|
||||
/// `handle` must be a valid pointer from this library.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn iron_arc_state_hash(handle: *const MatchHandle) -> u64 {
|
||||
handle.as_ref().map(|h| h.sim.state_hash()).unwrap_or(0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user