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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
mod decoder;
mod encoder;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::prelude::*;
use serde::{Deserialize, Serialize};
pub use decoder::{DecodeError, Decoder};
pub use encoder::{EncodeError, Encoder};
#[derive(Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Default, Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Gameflow {
/// The paths that the gameflow follows.
pub paths: Vec<Path>,
/// Always 103 in the original game.
pub(crate) unknown1: u32,
/// The animation frame interval in milliseconds, multiplied by 2. Divided
/// by 2 at runtime to get the actual frame time (e,g., 40 / 2 = 20ms per
/// frame).
///
/// Used for animating the points along the path.
///
/// Always 40 in the original game.
pub animation_frame_interval_millis_x2: u16,
pub(crate) unknown3: u16,
/// Notes is probably a relic from the gameflow editor. In `CH1_ALL.DOT.ron`
/// it looks like the first note is truncated, so this field probably
/// suffers the same nul-termination issue seen in other game files.
pub(crate) notes: Vec<String>,
/// The name of the map file that this gameflow is associated with. This is
/// not used in the original game, but is probably used by the gameflow
/// editor.
pub map_file_name: String,
pub(crate) unknown4: Vec<u8>,
}
#[derive(Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Default, Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Path {
/// The control points used to make a curve that represents the path.
pub control_points: Vec<Point>,
/// The number of animation frames between revealing each point along this
/// path. With the default 20ms frame time, a value of 5 means points appear
/// every 100ms.
///
/// Always 5 in the original game.
pub frames_per_point: i32,
/// Distance in pixels between interpolated points along the path's curve.
///
/// Used by the curve generation algorithm to determine rendering
/// granularity.
///
/// Always 10 in the original game.
pub curve_point_spacing: i32,
/// Always 0.
pub unknown3: i32,
/// Always 1.
pub unknown4: i32,
/// Optional index of the previous path in the gameflow. Set to -1 if there
/// is no previous path.
///
/// This is used to link paths together in the gameflow. The first path in
/// the gameflow has a previous path index of -1. The last path in the
/// gameflow has a next path index of -1.
///
/// This doesn't seem to be used in the original game, but is probably used
/// by the gameflow editor.
pub previous_path_index: i32,
/// Optional index of the next path in the gameflow. Set to -1 if there is
/// no next path.
///
/// This is used to link paths together in the gameflow. The first path in
/// the gameflow has a previous path index of -1. The last path in the
/// gameflow has a next path index of -1.
///
/// This doesn't seem to be used in the original game, but is probably used
/// by the gameflow editor.
pub next_path_index: i32,
/// Always 0.
pub unknown7: i32,
pub unknown8: Vec<u8>,
}
#[derive(Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Default, Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "bevy_reflect", feature = "debug"), reflect(Debug))]
pub struct Point {
/// The x-coordinate of the point.
pub x: u32,
/// The y-coordinate of the point.
pub y: u32,
/// Sometimes 1, usually 0. Each gameflow file has one point with a value of
/// 1 for this field.
///
/// - In CH1_ALL.DOT, this point is located near Altdorf.
/// - In CH2_ALL.DOT, this point is located near Altdorf.
/// - In CH3_ALL.DOT, this point is located between Altdorf and Kislev.
/// - In CH4_ALL.DOT, this point is located near Paravon.
///
/// This doesn't seem to be used at runtime. It might be editor metadata,
/// e.g., it may mark the currently selected point in the gameflow editor.
pub(crate) unknown1: u32,
/// Always 0 in the game files.
pub(crate) unknown2: u32,
}
#[cfg(test)]
mod tests {
use std::{
ffi::{OsStr, OsString},
fs::File,
path::{Path, PathBuf},
};
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn test_encode_too_many_control_points() {
let gameflow = Gameflow {
paths: vec![super::Path {
control_points: vec![
Point::default();
11 // more than MAX_CONTROL_POINTS
],
..Default::default()
}],
..Default::default()
};
let mut encoded_bytes = Vec::new();
let result = Encoder::new(&mut encoded_bytes).encode(&gameflow);
assert!(result.is_err());
match result {
Err(EncodeError::TooManyControlPoints) => (),
_ => panic!("Expected TooManyControlPoints error"),
}
}
fn roundtrip_test(original_bytes: &[u8], gameflow: &Gameflow) {
let mut encoded_bytes = Vec::new();
Encoder::new(&mut encoded_bytes).encode(gameflow).unwrap();
let original_bytes = original_bytes
.chunks(16)
.map(|chunk| {
chunk
.iter()
.map(|b| format!("{b:02X}"))
.collect::<Vec<_>>()
.join(" ")
})
.collect::<Vec<_>>()
.join("\n");
let encoded_bytes = encoded_bytes
.chunks(16)
.map(|chunk| {
chunk
.iter()
.map(|b| format!("{b:02X}"))
.collect::<Vec<_>>()
.join(" ")
})
.collect::<Vec<_>>()
.join("\n");
assert_eq!(original_bytes, encoded_bytes);
}
#[test]
fn test_decode_ch1_all() {
let d: PathBuf = [
std::env::var("DARKOMEN_PATH").unwrap().as_str(),
"DARKOMEN",
"GAMEDATA",
"GAMEFLOW",
"CH1_ALL.DOT",
]
.iter()
.collect();
let original_bytes = std::fs::read(d.clone()).unwrap();
let file = File::open(d).unwrap();
let gameflow = Decoder::new(file).decode().unwrap();
assert_eq!(gameflow.paths.len(), 11);
assert_eq!(gameflow.unknown1, 103);
assert_eq!(gameflow.animation_frame_interval_millis_x2, 40);
assert_eq!(gameflow.unknown3, 6);
assert_eq!(
gameflow.notes,
vec![
"_allz.dot]".to_string(),
"\\SrcCode\\public\\MP_Dots\\editor\\dots\\M1_ENG.bmp".to_string()
]
);
assert_eq!(gameflow.map_file_name, "M1_ENG.bmp".to_string());
assert_eq!(gameflow.paths.first().unwrap().control_points.len(), 5);
assert_eq!(
gameflow
.paths
.first()
.unwrap()
.control_points
.first()
.unwrap()
.x,
89
);
assert_eq!(
gameflow
.paths
.first()
.unwrap()
.control_points
.first()
.unwrap()
.y,
71
);
roundtrip_test(&original_bytes, &gameflow);
}
#[test]
fn test_decode_all() {
let d: PathBuf = [
std::env::var("DARKOMEN_PATH").unwrap().as_str(),
"DARKOMEN",
"GAMEDATA",
"GAMEFLOW",
]
.iter()
.collect();
let root_output_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "decoded", "gameflows"]
.iter()
.collect();
std::fs::create_dir_all(&root_output_dir).unwrap();
fn visit_dirs(dir: &Path, cb: &mut dyn FnMut(&Path)) {
println!("Reading dir {:?}", dir.display());
let mut paths = std::fs::read_dir(dir)
.unwrap()
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()
.unwrap();
paths.sort();
for path in paths {
if path.is_dir() {
visit_dirs(&path, cb);
} else {
cb(&path);
}
}
}
visit_dirs(&d, &mut |path| {
let Some(ext) = path.extension() else {
return;
};
if ext.to_string_lossy().to_uppercase() != "DOT" {
return;
}
println!("Decoding {:?}", path.file_name().unwrap());
let original_bytes = std::fs::read(path).unwrap();
let file = File::open(path).unwrap();
let gameflow = Decoder::new(file).decode().unwrap();
roundtrip_test(&original_bytes, &gameflow);
let parent_dir = path
.components()
.collect::<Vec<_>>()
.iter()
.rev()
.skip(1) // skip the file name
.take_while(|c| c.as_os_str() != "DARKOMEN")
.collect::<Vec<_>>()
.iter()
.rev()
.collect::<PathBuf>();
let output_dir = root_output_dir.join(parent_dir);
std::fs::create_dir_all(&output_dir).unwrap();
let output_path = append_ext("ron", output_dir.join(path.file_name().unwrap()));
let mut buffer = String::new();
ron::ser::to_writer_pretty(&mut buffer, &gameflow, Default::default()).unwrap();
std::fs::write(output_path, buffer).unwrap();
});
}
fn append_ext(ext: impl AsRef<OsStr>, path: PathBuf) -> PathBuf {
let mut os_string: OsString = path.into();
os_string.push(".");
os_string.push(ext.as_ref());
os_string.into()
}
}