Skip to main content
Verticopolis engine

verticopolis_engine/tdt/
mod.rs

1//! The 1994 `.TDT` save format: a port of `src/storage/tdt*.ts`, held to
2//! `conformance/tdt-cases.json` by the `tdt` referee binary and the test in
3//! `referee.rs`. Bytes in, the serialized game out (`import_tdt`); the
4//! serialized game in, bytes out (`export_tdt`). The engine never depends on
5//! this module; a tower that never touches a 1994 file carries none of it.
6
7pub mod byte_reader;
8pub mod byte_writer;
9#[cfg(test)]
10mod cathedral_tests;
11pub mod encoder;
12pub mod export;
13pub mod export_gather;
14pub mod export_parking;
15pub mod export_report;
16pub mod export_tables;
17#[cfg(any(test, feature = "conformance"))]
18pub mod fixtures;
19#[cfg(test)]
20mod fixtures_tests;
21pub mod format;
22pub mod import;
23pub mod import_report;
24pub mod pacing;
25pub mod parse;
26pub mod part_merge;
27#[cfg(any(test, feature = "conformance"))]
28pub mod referee;
29pub mod resync;
30pub mod retail;
31pub mod stair_scan;
32pub mod stamp;
33pub mod tables;
34pub mod tail;
35#[cfg(test)]
36mod theater_flush_tests;
37#[cfg(test)]
38mod theatre_tests;
39pub mod transports;
40pub mod types;
41pub mod venue_export;
42pub mod venue_people;
43pub mod venues;
44
45/// A `.TDT` file that cannot be read; the message is player-readable.
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub struct LegacyImportError(pub String);
48
49/// A tower the 1994 format cannot hold; the message is player-readable.
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub struct LegacyExportError(pub String);
52
53impl std::fmt::Display for LegacyImportError {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        f.write_str(&self.0)
56    }
57}
58
59impl std::fmt::Display for LegacyExportError {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        f.write_str(&self.0)
62    }
63}
64
65/// Reads a 1994 SimTower `.TDT` save into this engine's save JSON, plus
66/// warnings about anything it could not carry over. Load the JSON with
67/// [`deserialize_tdt`](crate::load::deserialize_tdt) after parsing it. The
68/// error is a sentence a player can read.
69///
70/// ```
71/// use verticopolis_engine::clock::GameMode;
72/// use verticopolis_engine::sim::Simulation;
73/// use verticopolis_engine::{export_tdt, import_tdt};
74///
75/// let sim = Simulation::new_game(1, GameMode::Classic);
76/// let bytes = export_tdt(&sim.serialize().to_string()).unwrap();
77/// let (json, _warnings) = import_tdt(&bytes, "TOWER.TDT").unwrap();
78/// let save: serde_json::Value = serde_json::from_str(&json).unwrap();
79/// let tower = verticopolis_engine::load::deserialize_tdt(&save).unwrap();
80/// assert_eq!(tower.mode, GameMode::Classic);
81/// ```
82pub fn import_tdt(bytes: &[u8], filename: &str) -> Result<(String, Vec<String>), String> {
83    let parsed = parse::parse_tdt(bytes, filename).map_err(|e| e.0)?;
84    Ok((parsed.save.to_string(), parsed.warnings))
85}
86
87/// Writes a Classic game's save JSON (the text of
88/// [`Simulation::serialize`](crate::sim::Simulation::serialize)) as a 1994
89/// SimTower `.TDT` file. The error is a sentence a player can read, or says
90/// what made the JSON unusable.
91pub fn export_tdt(save_json: &str) -> Result<Vec<u8>, String> {
92    let save: serde_json::Value =
93        crate::load::parse_save_json(save_json.as_bytes()).map_err(|e| format!("save: {e}"))?;
94    export::build_tdt(&save)
95        .map(|built| built.bytes)
96        .map_err(|e| e.0)
97}