1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#![deny(
anonymous_parameters,
bare_trait_objects,
clippy::clone_on_ref_ptr,
clippy::float_cmp_const,
clippy::if_not_else,
clippy::unwrap_used
)]
#![cfg_attr(
debug_assertions,
allow(dead_code, unused_imports, unused_variables, clippy::match_single_binding,)
)]
#![cfg_attr(any(doc, not(debug_assertions)), deny(missing_docs))]
#![cfg_attr(
not(debug_assertions),
deny(
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::dbg_macro,
clippy::indexing_slicing,
)
)]
use std::convert::TryInto;
use std::io::Write;
use anyhow::Context;
use arcstr::ArcStr;
use derive_new::new;
use getset::{CopyGetters, Getters, MutGetters, Setters};
use serde::{Deserialize, Serialize};
use traffloat_types::{time, units};
use typed_builder::TypedBuilder;
use xias::Xias;
#[cfg(feature = "xy")]
pub use xylem::Xylem;
macro_rules! impl_identifiable {
($ty:ty, $scope:ty) => {
#[cfg(feature = "xy")]
impl ::xylem::Identifiable<crate::Schema> for $ty {
type Scope = $scope;
fn id(&self) -> ::xylem::Id<crate::Schema, $ty> { self.id }
}
};
($ty:ty) => {
impl_identifiable!($ty, ());
};
}
pub mod atlas;
pub mod building;
pub mod cargo;
pub mod catalyst;
pub mod crime;
pub mod edge;
pub mod feature;
pub mod gas;
pub mod lang;
pub mod liquid;
pub mod node;
pub mod skill;
pub mod vehicle;
mod tests;
pub const SCHEMA_VERSION: u32 = 1;
pub const MAGIC_HEADER: &[u8] = b"\xffTSV";
#[derive(Getters, MutGetters, TypedBuilder, Serialize, Deserialize)]
pub struct TfsaveFile {
#[getset(get = "pub")]
scenario: Scenario,
#[getset(get = "pub")]
config: Config,
#[getset(get = "pub")]
def: Vec<AnyDef>,
#[getset(get = "pub")]
#[getset(get_mut = "pub")]
state: State,
}
impl TfsaveFile {
pub fn parse(mut buf: &[u8]) -> anyhow::Result<Self> {
buf = match buf.strip_prefix(MAGIC_HEADER) {
Some(buf) => buf,
_ => anyhow::bail!("Not a traffloat scenario file"),
};
let version = match buf.get(0..4) {
Some(bytes) => u32::from_le_bytes(bytes.try_into().expect("bytes.len() == 4")),
None => anyhow::bail!("File is too short"),
};
anyhow::ensure!(version == SCHEMA_VERSION, "Incompatible scenario version");
buf = buf.get(4..).expect("Just checked above");
let flate = flate2::read::DeflateDecoder::new(buf);
rmp_serde::from_read(flate).context("Error parsing scenario file")
}
pub fn write(&self, mut w: impl Write) -> anyhow::Result<()> {
w.write_all(MAGIC_HEADER)?;
w.write_all(&SCHEMA_VERSION.to_le_bytes())?;
{
let mut flate = flate2::write::DeflateEncoder::new(&mut w, flate2::Compression::best());
self.serialize(&mut rmp_serde::Serializer::new(&mut flate))?;
flate.flush()?;
log::debug!(
"Compressed scenario file ({}%)",
flate.total_out().small_float::<f64>() / flate.total_in().small_float::<f64>()
* 100.
);
flate.finish()?;
}
Ok(())
}
}
#[derive(Debug, Clone, Getters, Serialize, Deserialize, TypedBuilder)]
pub struct Scenario {
#[getset(get = "pub")]
name: ArcStr,
#[getset(get = "pub")]
description: ArcStr,
}
#[derive(Debug, Clone, CopyGetters, Serialize, Deserialize, TypedBuilder)]
pub struct Config {
#[getset(get_copy = "pub")]
sun_speed: time::Rate<f64>,
#[getset(get_copy = "pub")]
negligible_volume: units::LiquidVolume,
}
mod schema;
#[cfg(feature = "xy")]
pub use schema::curdir;
pub use schema::{Id, IdString, Schema};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
#[cfg_attr(feature = "xy", derive(Xylem))]
#[cfg_attr(feature = "xy", xylem(expose = AnyDefXylem, derive(Deserialize), serde(tag = "type")))]
pub enum AnyDef {
LangBundle(lang::Def),
Atlas(atlas::Def),
Liquid(liquid::Def),
LiquidFormula(liquid::Formula),
DefaultLiquidFormula(liquid::DefaultFormula),
Gas(gas::Def),
CargoCategory(cargo::category::Def),
Cargo(cargo::Def),
Skill(skill::Def),
Vehicle(vehicle::Def),
BuildingCategory(building::category::Def),
Building(building::Def),
Crime(crime::Def),
}
#[derive(Default, Getters, Setters, CopyGetters, MutGetters, Serialize, Deserialize, new)]
pub struct State {
#[getset(get_copy = "pub")]
#[getset(set = "pub")]
time: time::Instant,
#[new(default)]
#[getset(get = "pub")]
#[getset(get_mut = "pub")]
nodes: Vec<node::Node>,
#[new(default)]
#[getset(get = "pub")]
#[getset(get_mut = "pub")]
edges: Vec<edge::Edge>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CustomizableName {
Original(lang::Item),
Custom(ArcStr),
}
#[cfg(feature = "yew")]
impl<'t> yew::html::ImplicitClone for CustomizableName {}
#[cfg(feature = "yew")]
impl<'t> yew::html::IntoPropValue<CustomizableName> for &'t lang::Item {
fn into_prop_value(self) -> CustomizableName { CustomizableName::Original(self.clone()) }
}
#[cfg(feature = "yew")]
impl<'t> yew::html::IntoPropValue<CustomizableName> for &'t str {
fn into_prop_value(self) -> CustomizableName { CustomizableName::Custom(ArcStr::from(self)) }
}