darkomen 0.5.2

Warhammer: Dark Omen library and CLI in Rust
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use std::{
    fs::File,
    io::{Read as _, Write as _},
    path::PathBuf,
};

use clap::{Args, Subcommand, ValueEnum};
use darkomen::portrait::heads;
use darkomen::portrait::keyframes;
use darkomen::portrait::sequences;

use super::io::{read_input_to_string, write_output_string};

#[derive(Args)]
pub struct PortraitArgs {
    #[command(subcommand)]
    pub subcommand: Option<PortraitSubcommands>,
}

#[derive(Subcommand)]
pub enum PortraitSubcommands {
    EditHeads(EditHeadsArgs),
    EditKeyframes(EditKeyframesArgs),
    EditSequences(EditSequencesArgs),
    DumpHeads(DumpHeadsArgs),
    DumpKeyframes(DumpKeyframesArgs),
    DumpSequences(DumpSequencesArgs),
    WriteHeads(WriteHeadsArgs),
    WriteKeyframes(WriteKeyframesArgs),
    WriteSequences(WriteSequencesArgs),
}

#[derive(Args)]
pub struct EditHeadsArgs {
    /// The path to the heads database file to edit, e.g., ".../HEADS.DB".
    #[arg(index = 1)]
    pub heads_file: String,

    /// The name of the text editor to use.
    #[arg(short, long, default_value = "code --wait")]
    pub editor: String,

    /// The format to edit the heads database file in.
    #[arg(short, long, default_value_t=Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct EditKeyframesArgs {
    /// The path to the keyframes file to edit, e.g., ".../0.KEY".
    #[arg(index = 1)]
    pub keyframes_file: String,

    /// The name of the text editor to use.
    #[arg(short, long, default_value = "code --wait")]
    pub editor: String,

    /// The format to edit the keyframes file in.
    #[arg(short, long, default_value_t=Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct EditSequencesArgs {
    /// The path to the sequences file to edit, e.g., ".../0.SEQ".
    #[arg(index = 1)]
    pub sequences_file: String,

    /// The name of the text editor to use.
    #[arg(short, long, default_value = "code --wait")]
    pub editor: String,

    /// The format to edit the sequences file in.
    #[arg(short, long, default_value_t=Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct DumpHeadsArgs {
    /// The path to the heads database file to read, e.g., ".../HEADS.DB".
    #[arg(index = 1)]
    pub heads_file: String,

    /// Output path. Use "-" or omit for stdout.
    #[arg(short, long)]
    pub output: Option<String>,

    #[arg(short, long, default_value_t = Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct DumpKeyframesArgs {
    /// The path to the keyframes file to read, e.g., ".../0.KEY".
    #[arg(index = 1)]
    pub keyframes_file: String,

    /// Output path. Use "-" or omit for stdout.
    #[arg(short, long)]
    pub output: Option<String>,

    #[arg(short, long, default_value_t = Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct DumpSequencesArgs {
    /// The path to the sequences file to read, e.g., ".../0.SEQ".
    #[arg(index = 1)]
    pub sequences_file: String,

    /// Output path. Use "-" or omit for stdout.
    #[arg(short, long)]
    pub output: Option<String>,

    #[arg(short, long, default_value_t = Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct WriteHeadsArgs {
    /// The destination binary heads database file to write.
    #[arg(index = 1)]
    pub heads_file: String,

    /// Input path containing serialized data. Use "-" for stdin.
    #[arg(short, long)]
    pub input: String,

    #[arg(short, long, default_value_t = Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct WriteKeyframesArgs {
    /// The destination binary keyframes file to write.
    #[arg(index = 1)]
    pub keyframes_file: String,

    /// Input path containing serialized data. Use "-" for stdin.
    #[arg(short, long)]
    pub input: String,

    #[arg(short, long, default_value_t = Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Args)]
pub struct WriteSequencesArgs {
    /// The destination binary sequences file to write.
    #[arg(index = 1)]
    pub sequences_file: String,

    /// Input path containing serialized data. Use "-" for stdin.
    #[arg(short, long)]
    pub input: String,

    #[arg(short, long, default_value_t = Format::Json)]
    #[clap(value_enum)]
    pub format: Format,
}

#[derive(Clone, ValueEnum)]
pub enum Format {
    Json,
    Ron,
}

impl std::fmt::Display for Format {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Format::Json => f.write_str("json"),
            Format::Ron => f.write_str("ron"),
        }
    }
}

pub fn run(args: &PortraitArgs) -> anyhow::Result<()> {
    match &args.subcommand {
        Some(PortraitSubcommands::EditHeads(edit_args)) => edit_heads_file(edit_args)?,
        Some(PortraitSubcommands::EditKeyframes(edit_args)) => edit_keyframes_file(edit_args)?,
        Some(PortraitSubcommands::EditSequences(edit_args)) => edit_sequences_file(edit_args)?,
        Some(PortraitSubcommands::DumpHeads(dump_args)) => dump_heads_file(dump_args)?,
        Some(PortraitSubcommands::DumpKeyframes(dump_args)) => dump_keyframes_file(dump_args)?,
        Some(PortraitSubcommands::DumpSequences(dump_args)) => dump_sequences_file(dump_args)?,
        Some(PortraitSubcommands::WriteHeads(write_args)) => write_heads_file(write_args)?,
        Some(PortraitSubcommands::WriteKeyframes(write_args)) => write_keyframes_file(write_args)?,
        Some(PortraitSubcommands::WriteSequences(write_args)) => write_sequences_file(write_args)?,
        None => {}
    }

    Ok(())
}

fn dump_heads_file(args: &DumpHeadsArgs) -> anyhow::Result<()> {
    let file = File::open(&args.heads_file)?;
    let heads_db = heads::Decoder::new(file).decode()?;

    let as_string = match args.format {
        Format::Ron => ron::ser::to_string_pretty(&heads_db, ron::ser::PrettyConfig::default())?,
        Format::Json => serde_json::to_string_pretty(&heads_db)?,
    };

    write_output_string(args.output.as_deref(), &as_string)
}

fn dump_keyframes_file(args: &DumpKeyframesArgs) -> anyhow::Result<()> {
    let file = File::open(&args.keyframes_file)?;
    let keyframes_db = keyframes::Decoder::new(file).decode()?;

    let as_string = match args.format {
        Format::Ron => {
            ron::ser::to_string_pretty(&keyframes_db, ron::ser::PrettyConfig::default())?
        }
        Format::Json => serde_json::to_string_pretty(&keyframes_db)?,
    };

    write_output_string(args.output.as_deref(), &as_string)
}

fn dump_sequences_file(args: &DumpSequencesArgs) -> anyhow::Result<()> {
    let file = File::open(&args.sequences_file)?;
    let sequences_db = sequences::Decoder::new(file).decode()?;

    let as_string = match args.format {
        Format::Ron => {
            ron::ser::to_string_pretty(&sequences_db, ron::ser::PrettyConfig::default())?
        }
        Format::Json => serde_json::to_string_pretty(&sequences_db)?,
    };

    write_output_string(args.output.as_deref(), &as_string)
}

fn write_heads_file(args: &WriteHeadsArgs) -> anyhow::Result<()> {
    let s = read_input_to_string(&args.input)?;
    let heads_db: heads::HeadsDatabase = match args.format {
        Format::Ron => ron::de::from_str(&s)?,
        Format::Json => serde_json::from_str(&s)?,
    };

    let file = File::create(&args.heads_file)?;
    heads::Encoder::new(file).encode(&heads_db)?;

    Ok(())
}

fn write_keyframes_file(args: &WriteKeyframesArgs) -> anyhow::Result<()> {
    let s = read_input_to_string(&args.input)?;
    let keyframes_db: keyframes::Keyframes = match args.format {
        Format::Ron => ron::de::from_str(&s)?,
        Format::Json => serde_json::from_str(&s)?,
    };

    let file = File::create(&args.keyframes_file)?;
    keyframes::Encoder::new(file).encode(&keyframes_db)?;

    Ok(())
}

fn write_sequences_file(args: &WriteSequencesArgs) -> anyhow::Result<()> {
    let s = read_input_to_string(&args.input)?;
    let sequences_db: sequences::Sequences = match args.format {
        Format::Ron => ron::de::from_str(&s)?,
        Format::Json => serde_json::from_str(&s)?,
    };

    let file = File::create(&args.sequences_file)?;
    sequences::Encoder::new(file).encode(&sequences_db)?;

    Ok(())
}

fn edit_heads_file(args: &EditHeadsArgs) -> anyhow::Result<()> {
    let heads_file: PathBuf = args.heads_file.clone().into();

    // Load the heads database file.
    let file = File::open(heads_file.clone())?;
    let heads_db = heads::Decoder::new(file).decode()?;

    // Serialize the heads database to a human-readable string.
    let (as_string, extension) = match args.format {
        Format::Ron => (
            ron::ser::to_string_pretty(&heads_db, ron::ser::PrettyConfig::default())?,
            "ron",
        ),
        Format::Json => (serde_json::to_string_pretty(&heads_db)?, "json"),
    };

    // Write the human-readable string to a temporary file.
    let prefix = format!(
        "{}.",
        heads_file
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("heads"),
    );
    let suffix = format!(".{extension}");
    let mut temp_file = tempfile::Builder::new()
        .prefix(&prefix)
        .suffix(&suffix)
        .tempfile()?;
    temp_file.write_all(as_string.as_bytes())?;
    temp_file.flush()?;

    // Open the temporary file in the editor.
    let (editor, editor_args) = {
        let mut parts = args.editor.split_whitespace();
        let editor = parts.next().unwrap();
        let editor_args = parts.collect::<Vec<_>>();
        (editor, editor_args)
    };
    let mut command = std::process::Command::new(editor);
    command.args(editor_args);

    // This call blocks until the editor process exits.
    println!("Waiting for editor to close...");
    command.arg(temp_file.path()).status()?;
    println!("Editor closed");

    // Read the modified human-readable string from the temporary file.
    let mut modified_string = String::new();
    temp_file.reopen()?.read_to_string(&mut modified_string)?;

    // Deserialize the modified string to a heads database.
    let modified_heads_db = match args.format {
        Format::Ron => ron::de::from_str(&modified_string)?,
        Format::Json => serde_json::from_str(&modified_string)?,
    };

    // Write the modified heads database to the original file.
    let file = File::create(heads_file)?;
    heads::Encoder::new(file).encode(&modified_heads_db)?;

    println!("Heads database file successfully edited");

    Ok(())
}

fn edit_keyframes_file(args: &EditKeyframesArgs) -> anyhow::Result<()> {
    let keyframes_file: PathBuf = args.keyframes_file.clone().into();

    // Load the keyframes file.
    let file = File::open(keyframes_file.clone())?;
    let keyframes_db = keyframes::Decoder::new(file).decode()?;

    // Serialize the keyframes database to a human-readable string.
    let (as_string, extension) = match args.format {
        Format::Ron => (
            ron::ser::to_string_pretty(&keyframes_db, ron::ser::PrettyConfig::default())?,
            "ron",
        ),
        Format::Json => (serde_json::to_string_pretty(&keyframes_db)?, "json"),
    };

    // Write the human-readable string to a temporary file.
    let prefix = format!(
        "{}.",
        keyframes_file
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("keyframes"),
    );
    let suffix = format!(".{extension}");
    let mut temp_file = tempfile::Builder::new()
        .prefix(&prefix)
        .suffix(&suffix)
        .tempfile()?;
    temp_file.write_all(as_string.as_bytes())?;
    temp_file.flush()?;

    // Open the temporary file in the editor.
    let (editor, editor_args) = {
        let mut parts = args.editor.split_whitespace();
        let editor = parts.next().unwrap();
        let editor_args = parts.collect::<Vec<_>>();
        (editor, editor_args)
    };
    let mut command = std::process::Command::new(editor);
    command.args(editor_args);

    // This call blocks until the editor process exits.
    println!("Waiting for editor to close...");
    command.arg(temp_file.path()).status()?;
    println!("Editor closed");

    // Read the modified human-readable string from the temporary file.
    let mut modified_string = String::new();
    temp_file.reopen()?.read_to_string(&mut modified_string)?;

    // Deserialize the modified string to a keyframes database.
    let modified_keyframes_db = match args.format {
        Format::Ron => ron::de::from_str(&modified_string)?,
        Format::Json => serde_json::from_str(&modified_string)?,
    };

    // Write the modified keyframes database to the original file.
    let file = File::create(keyframes_file)?;
    keyframes::Encoder::new(file).encode(&modified_keyframes_db)?;

    println!("Keyframes file successfully edited");

    Ok(())
}

fn edit_sequences_file(args: &EditSequencesArgs) -> anyhow::Result<()> {
    let sequences_file: PathBuf = args.sequences_file.clone().into();

    // Load the sequences file.
    let file = File::open(sequences_file.clone())?;
    let sequences_db = sequences::Decoder::new(file).decode()?;

    // Serialize the sequences database to a human-readable string.
    let (as_string, extension) = match args.format {
        Format::Ron => (
            ron::ser::to_string_pretty(&sequences_db, ron::ser::PrettyConfig::default())?,
            "ron",
        ),
        Format::Json => (serde_json::to_string_pretty(&sequences_db)?, "json"),
    };

    // Write the human-readable string to a temporary file.
    let prefix = format!(
        "{}.",
        sequences_file
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("sequences"),
    );
    let suffix = format!(".{extension}");
    let mut temp_file = tempfile::Builder::new()
        .prefix(&prefix)
        .suffix(&suffix)
        .tempfile()?;
    temp_file.write_all(as_string.as_bytes())?;
    temp_file.flush()?;

    // Open the temporary file in the editor.
    let (editor, editor_args) = {
        let mut parts = args.editor.split_whitespace();
        let editor = parts.next().unwrap();
        let editor_args = parts.collect::<Vec<_>>();
        (editor, editor_args)
    };
    let mut command = std::process::Command::new(editor);
    command.args(editor_args);

    // This call blocks until the editor process exits.
    println!("Waiting for editor to close...");
    command.arg(temp_file.path()).status()?;
    println!("Editor closed");

    // Read the modified human-readable string from the temporary file.
    let mut modified_string = String::new();
    temp_file.reopen()?.read_to_string(&mut modified_string)?;

    // Deserialize the modified string to a sequences database.
    let modified_sequences_db = match args.format {
        Format::Ron => ron::de::from_str(&modified_string)?,
        Format::Json => serde_json::from_str(&modified_string)?,
    };

    // Write the modified sequences database to the original file.
    let file = File::create(sequences_file)?;
    sequences::Encoder::new(file).encode(&modified_sequences_db)?;

    println!("Sequences file successfully edited");

    Ok(())
}