dfraw_json_parser 0.17.5

Library which parses Dwarf Fortress raw files into JSON
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
use encoding_rs_io::DecodeReaderBytesBuilder;
use std::{
    io::{BufRead, BufReader},
    path::Path,
};
use tracing::{debug, error, trace, warn};

use crate::{
    creature_variation::CreatureVariation,
    options::ParserOptions,
    parser::{
        entity::Entity,
        graphics::{Graphic, GraphicTypeToken, TilePage, GRAPHIC_TYPE_TOKEN_MAP},
        inorganic::Inorganic,
        material_template::MaterialTemplate,
        module_info_file::ModuleInfoFile,
        plant::Plant,
        DF_ENCODING, PARSABLE_OBJECT_TYPES, RAW_TOKEN_RE, {ObjectType, OBJECT_TOKEN_MAP},
        {RawMetadata, RawObject},
    },
    unprocessed_raw::{Modification, UnprocessedRaw},
    util::try_get_file,
    ParserError, RawModuleLocation,
};

use super::header::read_raw_file_type;

/// Results from parsing a file. Contains a list of parsed raws and a list of unprocessed raws.
///
/// The unprocessed raws need to be resolved so that they can become parsed raws. This is done
/// by calling `resolve` on an `UnprocessedRaw` object. That requires the entirety of the parsed
/// raws to be passed in, so that it can find the raws it needs to resolve against.
pub struct FileParseResults {
    /// The parsed raws from the file.
    pub parsed_raws: Vec<Box<dyn RawObject>>,
    /// The unprocessed raws from the file. These need to be resolved into parsed raws.
    pub unprocessed_raws: Vec<UnprocessedRaw>,
}

/// Parse a raw file into a list of parsed raws and a list of unprocessed raws.
///
/// This function performs the following steps:
///
/// 1. Parse the raw file into a list of parsed raws.
/// 2. Filter the parsed raws into a list of unprocessed raws.
/// 3. Return the parsed raws and unprocessed raws.
///
/// The unprocessed raws need to be resolved so that they can become parsed raws. This is done
/// by calling `resolve` on an `UnprocessedRaw` object. That requires the entirety of the parsed
/// raws to be passed in, so that it can find the raws it needs to resolve against.
///
/// # Arguments
///
/// * `raw_file_path` - The path to the raw file to parse.
/// * `options` - The parser options to use when parsing the raw file.
///
/// # Returns
///
/// * `Result<FileParseResults, ParserError>` - The results of parsing the raw file.
///
/// # Errors
///
/// * `ParserError::InvalidRawFile` - If the raw file is invalid.
/// * `ParserError::IOError` - If there is an error reading the raw file.
/// * `ParserError::ModuleInfoFileError` - If there is an error reading the module info file.
pub fn parse_raw_file<P: AsRef<Path>>(
    raw_file_path: &P,
    options: &ParserOptions,
) -> Result<FileParseResults, ParserError> {
    let mod_info_file = match ModuleInfoFile::from_raw_file_path(raw_file_path) {
        Ok(m) => m,
        Err(e) => {
            warn!(
                "parse_raw_file: Using an empty ModuleInfoFile because of error parsing the file"
            );
            debug!("{e:?}");
            ModuleInfoFile::new(
                raw_file_path
                    .as_ref()
                    .file_name()
                    .unwrap_or_default()
                    .to_str()
                    .unwrap_or(""),
                RawModuleLocation::Unknown,
                "none",
            )
        }
    };

    parse_raw_file_with_info(raw_file_path, &mod_info_file, options)
}

/// Parse a raw file into a list of parsed raws and a list of unprocessed raws.
///
/// # Arguments
///
/// * `raw_file_path` - The path to the raw file to parse.
/// * `mod_info_file` - The module info file for the raw file.
/// * `options` - The parser options to use when parsing the raw file.
///
/// # Returns
///
/// * `Result<FileParseResults, ParserError>` - The results of parsing the raw file.
///
/// # Errors
///
/// * `ParserError::InvalidRawFile` - If the raw file is invalid.
/// * `ParserError::IOError` - If there is an error reading the raw file.
/// * `ParserError::ModuleInfoFileError` - If there is an error reading the module info file.
#[allow(clippy::too_many_lines, clippy::cognitive_complexity)]
pub fn parse_raw_file_with_info<P: AsRef<Path>>(
    raw_file_path: &P,
    mod_info_file: &ModuleInfoFile,
    options: &ParserOptions,
) -> Result<FileParseResults, ParserError> {
    let mut created_raws: Vec<Box<dyn RawObject>> = Vec::new();
    let mut unprocessed_raws: Vec<UnprocessedRaw> = Vec::new();

    let file = try_get_file(raw_file_path)?;

    let decoding_reader = DecodeReaderBytesBuilder::new()
        .encoding(Some(*DF_ENCODING))
        .build(file);
    let reader = BufReader::new(decoding_reader);
    let mut started = false;
    let mut raw_filename = String::new();

    let mut temp_plant = Plant::empty();
    let mut temp_inorganic = Inorganic::empty();
    let mut temp_graphic = Graphic::empty();
    let mut temp_material_template = MaterialTemplate::empty();
    let mut temp_entity = Entity::empty();
    let mut temp_creature_variation = CreatureVariation::empty();
    let mut temp_unprocessed_raw = UnprocessedRaw::default();

    let mut last_parsed_type = ObjectType::Unknown;
    let mut last_graphic_type = GraphicTypeToken::Unknown;
    let mut temp_tile_page = TilePage::empty();
    let mut current_modification = Modification::MainRawBody { raws: Vec::new() };

    // Metadata
    let object_type = read_raw_file_type(raw_file_path)?;
    let mut raw_metadata = RawMetadata::new(
        mod_info_file,
        &object_type,
        raw_filename.as_str(),
        &raw_file_path,
        options.attach_metadata_to_raws,
    );

    // If we aren't supposed to parse this type, we should quit here
    if !options.object_types_to_parse.contains(&object_type) {
        debug!(
            "parse_raw_file_with_info: Quitting early because object type {:?} is not included in options!",
            object_type
        );
        return Ok(FileParseResults {
            parsed_raws: Vec::new(),
            unprocessed_raws: Vec::new(),
        });
    }

    // If the type of object is not in our known_list, we should quit here
    if !PARSABLE_OBJECT_TYPES.contains(&&object_type) {
        debug!(
            "parse_raw_file_with_info: Quitting early because object type {:?} is not parsable!",
            object_type
        );
        return Ok(FileParseResults {
            parsed_raws: Vec::new(),
            unprocessed_raws: Vec::new(),
        });
    }

    for (index, line) in reader.lines().enumerate() {
        if line.is_err() {
            error!(
                "parse_raw_file_with_info: Error processing {}:{}",
                raw_file_path.as_ref().display(),
                index
            );
            continue;
        }
        let line = match line {
            Ok(l) => l,
            Err(e) => {
                error!("parse_raw_file_with_info: Line-reading error\n{:?}", e);
                continue;
            }
        };

        if index == 0 {
            raw_filename = String::from(&line);
            raw_metadata = RawMetadata::new(
                mod_info_file,
                &object_type,
                raw_filename.as_str(),
                &raw_file_path,
                options.attach_metadata_to_raws,
            );
            continue;
        }
        for cap in RAW_TOKEN_RE.captures_iter(&line) {
            let captured_key = match cap.get(2) {
                Some(v) => v.as_str(),
                _ => {
                    continue;
                }
            };
            let captured_value = match cap.get(3) {
                Some(v) => v.as_str(),
                _ => {
                    continue;
                }
            };

            trace!(
                "parse_raw_file_with_info: Key: {} Value: {}",
                captured_key,
                captured_value
            );

            match captured_key {
                "OBJECT" => {
                    if !OBJECT_TOKEN_MAP.contains_key(captured_value) {
                        // We don't know what this object is, so we can't parse it.
                        // We should log this as an error.
                        error!(
                            "parse_raw_file_with_info: Unknown object type: {} Raw: {}",
                            captured_value.to_uppercase(),
                            raw_filename
                        );
                        return Err(ParserError::InvalidRawFile(format!(
                            "Unknown object type: {}",
                            captured_value.to_uppercase()
                        )));
                    }
                    // Check of object_type matches the captured_value as ObjectType.
                    // If it doesn't, we should log this as an error.
                    if &object_type
                        != OBJECT_TOKEN_MAP
                            .get(captured_value)
                            .unwrap_or(&ObjectType::Unknown)
                    {
                        error!(
                            "parse_raw_file_with_info: Object type mismatch: {} != {}",
                            object_type,
                            captured_value.to_uppercase()
                        );
                        return Err(ParserError::InvalidRawFile(format!(
                            "Object type mismatch: {} != {}",
                            object_type,
                            captured_value.to_uppercase()
                        )));
                    }
                }
                "CREATURE" | "SELECT_CREATURE" => {
                    // The entity object has a CREATURE tag
                    if started && last_parsed_type == ObjectType::Entity {
                        // We need to let the entity parse this tag.
                        temp_entity.parse_tag(captured_key, captured_value);
                        // leave before adding a fake new creature
                        continue;
                    }

                    if started
                        && (last_parsed_type == ObjectType::Creature
                            || last_parsed_type == ObjectType::CreatureCaste
                            || last_parsed_type == ObjectType::SelectCreature)
                    {
                        temp_unprocessed_raw.add_modification(current_modification.clone());
                        // We need to add the creature to the list of unprocessed raws.
                        unprocessed_raws.push(temp_unprocessed_raw.clone());
                    } else {
                        started = true;
                    }
                    // We haven't started a creature yet, so we need to start one.
                    temp_unprocessed_raw =
                        UnprocessedRaw::new(&ObjectType::Creature, &raw_metadata, captured_value);
                    current_modification = Modification::MainRawBody { raws: Vec::new() };
                    last_parsed_type = ObjectType::Creature;
                }
                "CREATURE_VARIATION" => {
                    if started && last_parsed_type == ObjectType::CreatureVariation {
                        // We need to add the creature to the list.
                        created_raws.push(Box::new(temp_creature_variation.clone()));
                    } else {
                        started = true;
                    }
                    // We haven't started a creature variation yet, so we need to start one.
                    temp_creature_variation =
                        CreatureVariation::new(captured_value, &raw_metadata.clone());
                    last_parsed_type = ObjectType::CreatureVariation;
                }
                "CASTE" | "SELECT_CASTE" => {
                    if object_type != ObjectType::Creature
                        && object_type != ObjectType::Entity
                        && object_type != ObjectType::Graphics
                    {
                        // Currently unhandled outside of these configurations.
                        continue;
                    }
                    if started && object_type == ObjectType::Entity {
                        // We need to let the entity parse this tag.
                        temp_entity.parse_tag(captured_key, captured_value);
                        // leave before adding a fake new creature
                        continue;
                    }
                    // Starting a new caste (in creature), so we can just add a caste to the last creature we started.
                    current_modification.add_raw(format!("{captured_key}:{captured_value}"));

                    last_parsed_type = ObjectType::CreatureCaste;
                }
                "PLANT" => {
                    // Starting a new plant, so we can just add a plant to the list.
                    if started {
                        // We need to add the plant to the list.
                        created_raws.push(Box::new(temp_plant.clone()));
                    } else {
                        started = true;
                    }
                    // We haven't started a plant yet, so we need to start one.
                    temp_plant = Plant::new(captured_value, &raw_metadata.clone());
                    last_parsed_type = ObjectType::Plant;
                }
                "INORGANIC" | "SELECT_INORGANIC" => {
                    if started {
                        // We've already started a raw, so we need to finish it.
                        // This is a new creature, so we need to finish the old one.
                        created_raws.push(Box::new(temp_inorganic.clone()));
                    } else {
                        started = true;
                    }
                    temp_inorganic = Inorganic::new(captured_value, &raw_metadata.clone());
                    last_parsed_type = ObjectType::Inorganic;
                }
                "MATERIAL_TEMPLATE" => {
                    // Starting a new material template, so we can just add a material template to the list.
                    if started {
                        // We need to add the material template to the list.
                        created_raws.push(Box::new(temp_material_template.clone()));
                    } else {
                        started = true;
                    }
                    // We haven't started a material template yet, so we need to start one.
                    temp_material_template =
                        MaterialTemplate::new(captured_value, &raw_metadata.clone());
                    last_parsed_type = ObjectType::MaterialTemplate;
                }
                "CREATURE_GRAPHICS"
                | "CREATURE_CASTE_GRAPHICS"
                | "TILE_GRAPHICS"
                | "PLANT_GRAPHICS" => {
                    // Starting a new graphic, so we can just add a graphic to the list.
                    if started {
                        // We need to add the graphic to the list.
                        created_raws.push(Box::new(temp_graphic.clone()));
                    } else {
                        started = true;
                    }
                    // We haven't started a graphic yet, so we need to start one.

                    last_parsed_type = ObjectType::Graphics;
                    last_graphic_type = *GRAPHIC_TYPE_TOKEN_MAP
                        .get(captured_key)
                        .unwrap_or(&GraphicTypeToken::Unknown);

                    temp_graphic =
                        Graphic::new(captured_value, &raw_metadata.clone(), last_graphic_type);
                }
                "TILE_PAGE" => {
                    if started {
                        // We need to add the tile page to the list.
                        created_raws.push(Box::new(temp_tile_page.clone()));
                    } else {
                        started = true;
                    }
                    // We haven't started a tile page yet, so we need to start one.
                    temp_tile_page = TilePage::new(captured_value, &raw_metadata.clone());
                    last_parsed_type = ObjectType::TilePage;
                }
                "ENTITY" => {
                    // Starting a new entity, so we can just add an entity to the list.
                    if started {
                        // We need to add the entity to the list.
                        created_raws.push(Box::new(temp_entity.clone()));
                    } else {
                        started = true;
                    }
                    // We haven't started an entity yet, so we need to start one.
                    temp_entity = Entity::new(captured_value, &raw_metadata.clone());
                    last_parsed_type = ObjectType::Entity;
                }
                "GO_TO_END" => {
                    debug!("began tracking AddToEnding modification");
                    // Push the current modification to the unprocessed raw
                    temp_unprocessed_raw.add_modification(current_modification.clone());
                    // Update the current modification to be an AddToEnding
                    current_modification = Modification::AddToEnding { raws: Vec::new() };
                }
                "GO_TO_START" => {
                    debug!("began tracking AddToBeginning modification");
                    // Push the current modification to the unprocessed raw
                    temp_unprocessed_raw.add_modification(current_modification.clone());
                    // Update the current modification to be an AddToBeginning
                    current_modification = Modification::AddToBeginning { raws: Vec::new() };
                }
                "GO_TO_TAG" => {
                    debug!("began tracking AddBeforeTag:{captured_value} modification");
                    // Push the current modification to the unprocessed raw
                    temp_unprocessed_raw.add_modification(current_modification.clone());
                    // Update the current modification to be an AddBeforeTag
                    current_modification = Modification::AddBeforeTag {
                        tag: captured_value.to_string(),
                        raws: Vec::new(),
                    };
                }
                "COPY_TAGS_FROM" => {
                    debug!("began tracking CopyTagsFrom:{captured_value} modification");
                    // Push the CopyTagsFrom modification to the unprocessed raw
                    temp_unprocessed_raw.add_modification(Modification::CopyTagsFrom {
                        identifier: captured_value.to_string(),
                    });
                }
                "APPLY_CREATURE_VARIATION" => {
                    debug!("began tracking ApplyCreatureVariation:{captured_value} modification");
                    // Push the ApplyCreatureVariation modification to the unprocessed raw
                    temp_unprocessed_raw.add_modification(Modification::ApplyCreatureVariation {
                        identifier: captured_value.to_string(),
                    });
                }
                _ => {
                    // This should be a tag for the current object.
                    // We should check if we have a current object, and if we do, we should add the tag to it.
                    // If we haven't started yet, we should do nothing.
                    if started {
                        match last_parsed_type {
                            ObjectType::Creature
                            | ObjectType::CreatureCaste
                            | ObjectType::SelectCreature => {
                                // We have a creature, so we can add a tag to it. We need to determine which section
                                // of the creature we are adding to.
                                current_modification
                                    .add_raw(format!("{captured_key}:{captured_value}"));
                            }
                            ObjectType::CreatureVariation => {
                                // We have a creature variation, so we can add a tag to it.
                                temp_creature_variation.parse_tag(captured_key, captured_value);
                            }
                            ObjectType::Plant => {
                                // We have a plant, so we can add a tag to it.
                                temp_plant.parse_tag(captured_key, captured_value);
                            }
                            ObjectType::Inorganic => {
                                // We have an inorganic, so we can add a tag to it.
                                temp_inorganic.parse_tag(captured_key, captured_value);
                            }
                            ObjectType::MaterialTemplate => {
                                // We have a material template, so we can add a tag to it.
                                temp_material_template.parse_tag(captured_key, captured_value);
                            }
                            ObjectType::Graphics => {
                                // We have a graphic, so we can add a tag to it.
                                if temp_graphic.get_graphic_type() == GraphicTypeToken::Tile {
                                    // Update graphic type (every line should have a graphic type tag)
                                    last_graphic_type = *GRAPHIC_TYPE_TOKEN_MAP
                                        .get(captured_key)
                                        .unwrap_or(&GraphicTypeToken::Unknown);
                                }

                                temp_graphic.parse_sprite_from_tag(
                                    captured_key,
                                    captured_value,
                                    last_graphic_type,
                                );
                            }
                            ObjectType::TilePage => {
                                // We have a tile page, so we can add a tag to it.
                                temp_tile_page.parse_tag(captured_key, captured_value);
                            }
                            ObjectType::Entity => {
                                // We have an entity, so we can add a tag to it.
                                temp_entity.parse_tag(captured_key, captured_value);
                            }
                            _ => {
                                // We don't have a known raw yet. So do nothing.
                            }
                        }
                    }
                }
            }
        }
    }
    if started {
        // If we did indeed start capture, we need to complete the final raw by adding it to the list
        if !temp_unprocessed_raw.is_empty() {
            temp_unprocessed_raw.add_modification(current_modification.clone());
            unprocessed_raws.push(temp_unprocessed_raw.clone());
        }
        if !temp_plant.is_empty() {
            created_raws.push(Box::new(temp_plant.clone()));
        }
        if !temp_inorganic.is_empty() {
            created_raws.push(Box::new(temp_inorganic.clone()));
        }
        if !temp_material_template.is_empty() {
            created_raws.push(Box::new(temp_material_template.clone()));
        }
        if !temp_graphic.is_empty() {
            created_raws.push(Box::new(temp_graphic.clone()));
        }
        if !temp_tile_page.is_empty() {
            created_raws.push(Box::new(temp_tile_page.clone()));
        }
        if !temp_entity.is_empty() {
            created_raws.push(Box::new(temp_entity.clone()));
        }
        if !temp_creature_variation.is_empty() {
            created_raws.push(Box::new(temp_creature_variation.clone()));
        }
    }

    debug!(
        "parse_raw_file_with_info: Parsed {} raws from {}",
        created_raws.len(),
        raw_filename
    );

    Ok(FileParseResults {
        parsed_raws: created_raws,
        unprocessed_raws,
    })
}