halbu 0.3.0

Diablo II save file parsing library.
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
#![warn(
    anonymous_parameters,
    nonstandard_style,
    single_use_lifetimes,
    trivial_casts,
    trivial_numeric_casts,
    unreachable_pub,
    unused_extern_crates,
    unused_qualifications,
    variant_size_differences
)]
//! Diablo II save-file library focused on practical editing workflows.
//!
//! `halbu` parses, edits, and writes D2R save data while preserving unknown/raw bytes
//! so saves can round-trip through the library.
//!
//! Supported top-level save layouts:
//! - [`format::FormatId::V99`]
//! - [`format::FormatId::V105`]
//!
//! Parsing modes:
//! - [`Strictness::Strict`]: fail fast on invalid/truncated data
//! - [`Strictness::Lax`]: continue parsing and collect [`ParseIssue`] values
//!
//! # Quick start
//! ```rust,no_run
//! use halbu::{CompatibilityChecks, Save, Strictness};
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let bytes = std::fs::read("Hero.d2s")?;
//!     let parsed = Save::parse(&bytes, Strictness::Strict)?;
//!     let mut save = parsed.save;
//!     let target_format = save.format();
//!
//!     save.character.name = "Demo".to_string();
//!     save.set_level(10);
//!
//!     let output = save.encode_for(target_format, CompatibilityChecks::Enforce)?;
//!     let reparsed = Save::parse(&output, Strictness::Strict)?;
//!
//!     Ok(())
//! }
//! ```
use bit::BitIndex;
use serde::{Deserialize, Serialize};

use std::fmt;

use attributes::Attributes;
use character::Character;
use npcs::Placeholder as NPCs;
use quests::Quests;
use skills::SkillPoints;
use waypoints::Waypoints;

/// Attributes/stat section model and bit-level serializer.
pub mod attributes;
/// Character section model and per-format codecs.
pub mod character;
/// Save-layout detection and top-level encode/decode glue.
pub mod format;
/// Item section placeholder/raw-preserving support.
pub mod items;
/// NPC section placeholder/raw-preserving support.
pub mod npcs;
/// Quest section model.
pub mod quests;
/// Skill section model and default D2R name mapping.
pub mod skills;
/// Internal byte utilities shared across sections.
pub mod utils;
/// Backend-owned save validation rules and issue model.
pub mod validation;
/// Waypoint section model.
pub mod waypoints;

const CHECKSUM_START: usize = 12;
const CHECKSUM_END: usize = 16;

use crate::format::FormatId;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone, Default)]
struct SaveMeta {
    /// Format id selected/observed for this save model.
    #[serde(default)]
    pub format: FormatId,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ExpansionType {
    Classic,
    #[default]
    Expansion,
    RotW,
}

impl ExpansionType {
    pub fn label(self) -> &'static str {
        match self {
            ExpansionType::Classic => "Classic",
            ExpansionType::Expansion => "Expansion",
            ExpansionType::RotW => "RotW",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GameEdition {
    D2RLegacy,
    RotW,
}

impl GameEdition {
    pub fn label(self) -> &'static str {
        match self {
            GameEdition::D2RLegacy => "D2R Legacy",
            GameEdition::RotW => "RotW",
        }
    }
}

/// Severity classification for a parse issue in lax mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IssueSeverity {
    Warning,
    Error,
}

/// High-level category for a parse issue in lax mode.
#[non_exhaustive]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum IssueKind {
    TruncatedSection,
    InvalidSignature,
    UnsupportedVersion,
    InvalidValue,
    InconsistentLayout,
    Other,
}

/// Detailed parse diagnostic emitted in lax mode.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParseIssue {
    /// Error/warning level.
    pub severity: IssueSeverity,
    /// Broad issue category.
    pub kind: IssueKind,
    /// Section name when known (`"character"`, `"attributes"`, ...).
    pub section: Option<String>,
    /// Human-readable diagnostic message.
    pub message: String,
    /// Byte offset when known.
    pub offset: Option<usize>,
    /// Expected byte count/value when known.
    pub expected: Option<usize>,
    /// Observed byte count/value when known.
    pub found: Option<usize>,
}

/// Hard parse error returned when strict validation fails.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParseHardError {
    pub message: String,
}

impl fmt::Display for ParseHardError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Parse hard error: {}", self.message)
    }
}

impl std::error::Error for ParseHardError {}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedSave {
    /// Parsed save model (possibly partial in lax mode).
    pub save: Save,
    /// Format detected from header version bytes (`V99`, `V105`, or `Unknown(n)`).
    pub detected_format: FormatId,
    /// Known layout actually used for decode (`V99` or `V105`).
    pub decoded_layout: FormatId,
    /// Optional inferred edition hint for unknown/unsupported versions.
    pub edition_hint: Option<GameEdition>,
    /// Non-fatal issues collected during parsing in lax mode.
    pub issues: Vec<ParseIssue>,
    /// Header checksum value read from bytes `12..16`, if available.
    pub header_checksum: Option<u32>,
    /// Checksum computed from payload using D2 algorithm, if available.
    pub computed_checksum: Option<u32>,
}

/// Lightweight metadata summary extracted from header + character section only.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SaveSummary {
    /// Numeric version stored in the file header when readable.
    pub version: Option<u32>,
    /// Detected format id (`FormatId`) from the header version when readable.
    pub format: Option<FormatId>,
    /// Edition family inferred from known format ids.
    pub edition: Option<GameEdition>,
    /// Character expansion mode inferred using format-specific rules.
    pub expansion_type: Option<ExpansionType>,
    /// Character name from the character section.
    pub name: Option<String>,
    /// Character class from the character section.
    pub class: Option<Class>,
    /// Character level from the character section.
    pub level: Option<u8>,
    /// Character title derived from progression/class/expansion/hardcore using default D2R rules.
    pub title: Option<String>,
    /// Non-fatal issues collected during summarize in lax mode.
    pub issues: Vec<ParseIssue>,
}

/// Controls parse behavior when malformed data is found.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum Strictness {
    /// Return an error on the first hard validation failure.
    Strict,
    /// Continue parsing when possible and accumulate [`ParseIssue`]s.
    #[default]
    Lax,
}

/// Controls whether compatibility checks are enforced during encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum CompatibilityChecks {
    /// Block encoding when compatibility checks report blocking issues.
    #[default]
    Enforce,
    /// Skip compatibility checks and encode anyway.
    Ignore,
}

/// Stable identifier for a save compatibility rule.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompatibilityCode {
    WarlockRequiresRotW,
    WarlockRequiresRotWExpansion,
    RotWExpansionRequiresRotWEdition,
    ExpansionClassRequiresExpansionMode,
    UnknownClassRequiresKnownTarget,
    MercenaryHireStateToggleUnsupported,
}

/// Compatibility finding for a specific target format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityIssue {
    pub code: CompatibilityCode,
    /// `true` means encoding should be blocked unless caller explicitly overrides policy.
    pub blocking: bool,
    pub message: String,
}

/// Full in-memory save model.
///
/// Unknown payloads for unmodeled sections are preserved in placeholder structs.
/// Invariants:
/// - `format()` and `version()` are synchronized through `set_format`.
/// - `expansion_type()` is canonical and must be changed through `set_expansion_type`.
/// - Character/attribute level must be changed through `set_level`.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct Save {
    /// Numeric version stored in the file header.
    version: u32,
    /// Canonical character expansion mode for this save model.
    #[serde(default)]
    expansion_type: ExpansionType,
    /// Character section.
    pub character: Character,
    /// Quests section.
    pub quests: Quests,
    /// Waypoints section.
    pub waypoints: Waypoints,
    /// NPC section (placeholder model).
    pub npcs: npcs::Placeholder,
    /// Attributes section.
    pub attributes: Attributes,
    /// Skills section.
    pub skills: SkillPoints,
    /// Items section (placeholder model).
    pub items: items::Placeholder,
    /// Auxiliary metadata kept by this library.
    #[serde(default)]
    meta: SaveMeta,
}

impl Default for Save {
    fn default() -> Self {
        let mut character = Character::default_class(Class::Amazon);
        character.last_played = 0;

        Save {
            version: FormatId::V99.version(),
            expansion_type: ExpansionType::Expansion,
            character,
            quests: Quests::default(),
            waypoints: Waypoints::default(),
            npcs: NPCs::default(),
            attributes: Attributes::new_save_defaults(),
            skills: SkillPoints::default(),
            items: items::Placeholder::default(),
            meta: SaveMeta { format: FormatId::V99 },
        }
    }
}

impl fmt::Display for Save {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut final_string = format!("Save:\nVersion: {0}\n", self.version);
        final_string.push_str(&format!("Expansion Type: {}\n", self.expansion_type.label()));
        final_string.push_str(&format!("Character:\n{0}\n", self.character));
        final_string.push_str(&format!("Quests:\n{0}\n", self.quests));
        final_string.push_str(&format!("Waypoints:\n{0}\n", self.waypoints));
        final_string.push_str(&format!("Attributes:\n{0}\n", self.attributes));
        final_string.push_str(&format!("Skills:\n{0}\n", self.skills));
        write!(f, "{0}", final_string)
    }
}

impl Save {
    fn apply_expansion_type_for_format(&mut self, format: FormatId, expansion_type: ExpansionType) {
        self.expansion_type = expansion_type;
        if format == FormatId::V99 {
            self.character
                .set_legacy_expansion_flag(!matches!(expansion_type, ExpansionType::Classic));
        } else if matches!(format, FormatId::V105 | FormatId::Unknown(_)) {
            // For v105, mode marker is canonical and status bit is ignored/preserved.
            character::v105::set_expansion_type(&mut self.character, expansion_type);
        }
    }

    /// Build a new blank save for a target format/class.
    ///
    /// `V99` defaults to `Expansion`; `V105` and unknown formats default to `RotW`.
    /// Call `set_expansion_type` afterward if you need `Classic`.
    pub fn new(format: FormatId, class: Class) -> Save {
        let mut character = Character::default_class(class);
        character.last_played = 0;
        character.raw_section = Vec::new();
        let expansion_type = match format.edition() {
            Some(GameEdition::RotW) => ExpansionType::RotW,
            Some(GameEdition::D2RLegacy) | None => ExpansionType::Expansion,
        };
        character.set_legacy_expansion_flag(!matches!(expansion_type, ExpansionType::Classic));

        Save {
            version: format.version(),
            expansion_type,
            character,
            quests: Quests::default(),
            waypoints: Waypoints::default(),
            npcs: NPCs::default(),
            attributes: Attributes::new_save_defaults(),
            skills: SkillPoints::default(),
            items: items::Placeholder::default(),
            meta: SaveMeta { format },
        }
    }

    pub fn format(&self) -> FormatId {
        self.meta.format
    }

    /// Header version synchronized with the save format.
    pub fn version(&self) -> u32 {
        self.version
    }

    /// Set output format and synchronize the numeric version field.
    pub fn set_format(&mut self, format: FormatId) {
        self.meta.format = format;
        self.version = format.version();
    }

    pub fn game_edition(&self) -> Option<GameEdition> {
        self.format().edition()
    }

    /// Set both character level fields kept in separate sections.
    pub fn set_level(&mut self, level: u8) {
        self.character.set_level(level);
        self.attributes.set_level(level);
    }

    pub fn expansion_type(&self) -> ExpansionType {
        self.expansion_type
    }

    pub fn set_expansion_type(&mut self, expansion_type: ExpansionType) {
        self.apply_expansion_type_for_format(self.format(), expansion_type);
    }

    /// Internal decode path: apply expansion semantics from the selected layout.
    pub(crate) fn set_expansion_type_for_format(
        &mut self,
        format: FormatId,
        expansion_type: ExpansionType,
    ) {
        self.apply_expansion_type_for_format(format, expansion_type);
    }

    /// Parse a save with explicit strictness.
    pub fn parse(byte_slice: &[u8], strictness: Strictness) -> Result<ParsedSave, ParseHardError> {
        format::decode(byte_slice, strictness)
    }

    /// Summarize only top-level header + character fields.
    pub fn summarize(
        byte_slice: &[u8],
        strictness: Strictness,
    ) -> Result<SaveSummary, ParseHardError> {
        format::summarize(byte_slice, strictness)
    }

    /// Encode to a specific output format with an explicit compatibility policy.
    pub fn encode_for(
        &self,
        format: FormatId,
        compatibility_checks: CompatibilityChecks,
    ) -> Result<Vec<u8>, EncodeError> {
        format::encode(self, format, compatibility_checks)
    }

    /// Return compatibility findings for encoding this save to `target`.
    pub fn check_compatibility(&self, target: FormatId) -> Vec<CompatibilityIssue> {
        format::compatibility_issues(self, target)
    }

    /// Return the default D2R title for this save using the canonical expansion mode.
    pub fn title_d2r(&self) -> Option<&'static str> {
        self.character.title_d2r(self.expansion_type())
    }

    /// Validate the current save using backend-owned canonical rules.
    pub fn validate(&self) -> validation::ValidationReport {
        validation::build_validation_report(self)
    }
}

/// Save encoding error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncodeError {
    message: String,
}

impl EncodeError {
    pub fn new(message: impl Into<String>) -> Self {
        Self { message: message.into() }
    }
}

impl fmt::Display for EncodeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Encoding error: {}", self.message)
    }
}

impl std::error::Error for EncodeError {}

/// In-game difficulty.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub enum Difficulty {
    #[default]
    Normal,
    Nightmare,
    Hell,
}

impl fmt::Display for Difficulty {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Difficulty::Normal => write!(f, "Normal"),
            Difficulty::Nightmare => write!(f, "Nightmare"),
            Difficulty::Hell => write!(f, "Hell"),
        }
    }
}

/// In-game act.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub enum Act {
    #[default]
    Act1,
    Act2,
    Act3,
    Act4,
    Act5,
}

impl fmt::Display for Act {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Act::Act1 => write!(f, "Act I"),
            Act::Act2 => write!(f, "Act II"),
            Act::Act3 => write!(f, "Act III"),
            Act::Act4 => write!(f, "Act IV"),
            Act::Act5 => write!(f, "Act V"),
        }
    }
}

impl TryFrom<u8> for Act {
    type Error = ParseHardError;
    fn try_from(byte: u8) -> Result<Act, ParseHardError> {
        let mut relevant_bits: u8 = 0;
        relevant_bits.set_bit_range(0..3, byte.bit_range(0..3));
        match relevant_bits {
            0x00 => Ok(Act::Act1),
            0x01 => Ok(Act::Act2),
            0x02 => Ok(Act::Act3),
            0x03 => Ok(Act::Act4),
            0x04 => Ok(Act::Act5),
            _ => Err(ParseHardError { message: format!("Found invalid act: {0:?}.", byte) }),
        }
    }
}

impl From<Act> for u8 {
    fn from(act: Act) -> u8 {
        match act {
            Act::Act1 => 0x00,
            Act::Act2 => 0x01,
            Act::Act3 => 0x02,
            Act::Act4 => 0x03,
            Act::Act5 => 0x04,
        }
    }
}

/// Character class id used by the save format.
#[derive(PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Class {
    Amazon,
    Sorceress,
    Necromancer,
    Paladin,
    Barbarian,
    Druid,
    Assassin,
    Warlock,
    Unknown(u8),
}

impl Class {
    /// Convert raw class id from save bytes.
    pub const fn from_id(class_id: u8) -> Self {
        match class_id {
            0x00 => Class::Amazon,
            0x01 => Class::Sorceress,
            0x02 => Class::Necromancer,
            0x03 => Class::Paladin,
            0x04 => Class::Barbarian,
            0x05 => Class::Druid,
            0x06 => Class::Assassin,
            0x07 => Class::Warlock,
            _ => Class::Unknown(class_id),
        }
    }

    /// Convert class to raw id used in save bytes.
    pub const fn id(self) -> u8 {
        match self {
            Class::Amazon => 0x00,
            Class::Sorceress => 0x01,
            Class::Necromancer => 0x02,
            Class::Paladin => 0x03,
            Class::Barbarian => 0x04,
            Class::Druid => 0x05,
            Class::Assassin => 0x06,
            Class::Warlock => 0x07,
            Class::Unknown(class_id) => class_id,
        }
    }
}

impl From<u8> for Class {
    fn from(class_id: u8) -> Self {
        Class::from_id(class_id)
    }
}

impl fmt::Display for Class {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let class: &'static str = match self {
            Class::Amazon => "Amazon",
            Class::Sorceress => "Sorceress",
            Class::Necromancer => "Necromancer",
            Class::Paladin => "Paladin",
            Class::Barbarian => "Barbarian",
            Class::Druid => "Druid",
            Class::Assassin => "Assassin",
            Class::Warlock => "Warlock",
            Class::Unknown(_) => "Unknown",
        };
        match self {
            Class::Unknown(class_id) => write!(f, "{0}({1})", class, class_id),
            _ => write!(f, "{0}", class),
        }
    }
}

impl From<Class> for u8 {
    fn from(class: Class) -> u8 {
        class.id()
    }
}

/// Compute save checksum using D2 save algorithm.
pub fn calc_checksum(bytes: &[u8]) -> i32 {
    let mut checksum: i32 = 0;
    for (i, byte) in bytes.iter().enumerate() {
        let mut ch = *byte as i32;
        if (CHECKSUM_START..CHECKSUM_END).contains(&i) {
            ch = 0;
        }
        checksum = (checksum << 1) + ch + ((checksum < 0) as i32);
    }
    checksum
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn test_parse_save() {
        let path: &Path = Path::new("assets/test/Joe.d2s");
        let save_file: Vec<u8> = match std::fs::read(path) {
            Ok(bytes) => bytes,
            Err(e) => panic!("File invalid: {e:?}"),
        };

        let _save = Save::parse(&save_file, Strictness::Lax).expect("save should parse");
    }

    #[test]
    fn test_set_level_syncs_character_and_attributes() {
        let mut save = Save::default();
        save.set_level(75);

        assert_eq!(save.character.level(), 75);
        assert_eq!(save.attributes.level(), 75);
    }

    #[test]
    fn new_save_sets_default_expansion_type_by_edition() {
        let v99 = Save::new(FormatId::V99, Class::Amazon);
        assert_eq!(v99.expansion_type(), ExpansionType::Expansion);

        let v105 = Save::new(FormatId::V105, Class::Amazon);
        assert_eq!(v105.expansion_type(), ExpansionType::RotW);
    }

    #[test]
    fn game_edition_reflects_format() {
        let mut save = Save::new(FormatId::V99, Class::Amazon);
        assert_eq!(save.game_edition(), Some(GameEdition::D2RLegacy));

        save.set_format(FormatId::V105);
        assert_eq!(save.game_edition(), Some(GameEdition::RotW));

        save.set_format(FormatId::Unknown(1234));
        assert_eq!(save.game_edition(), None);
    }

    #[test]
    fn save_title_uses_canonical_expansion_type() {
        let mut save = Save::new(FormatId::V105, Class::Amazon);
        save.set_expansion_type(ExpansionType::Classic);
        save.character.set_legacy_expansion_flag(true);
        save.character.progression = 4;

        assert_eq!(save.title_d2r(), Some("Dame"));
    }

    #[test]
    fn set_expansion_type_updates_v105_mode_marker_without_touching_status_bit() {
        let mut save = Save::new(FormatId::V105, Class::Amazon);
        save.character.set_legacy_expansion_flag(false);

        save.set_expansion_type(ExpansionType::Classic);
        assert_eq!(save.expansion_type(), ExpansionType::Classic);
        assert!(!save.character.status().is_expansion());
        assert_eq!(
            character::v105::mode_marker(&save.character),
            Some(character::v105::MODE_CLASSIC)
        );

        save.set_expansion_type(ExpansionType::Expansion);
        assert_eq!(save.expansion_type(), ExpansionType::Expansion);
        assert!(!save.character.status().is_expansion());
        assert_eq!(
            character::v105::mode_marker(&save.character),
            Some(character::v105::MODE_EXPANSION)
        );

        save.set_expansion_type(ExpansionType::RotW);
        assert_eq!(save.expansion_type(), ExpansionType::RotW);
        assert!(!save.character.status().is_expansion());
        assert_eq!(character::v105::mode_marker(&save.character), Some(character::v105::MODE_ROTW));
    }

    #[test]
    fn set_expansion_type_updates_v99_status_bit() {
        let mut save = Save::new(FormatId::V99, Class::Amazon);
        save.character.set_legacy_expansion_flag(false);

        save.set_expansion_type(ExpansionType::Expansion);
        assert!(save.character.status().is_expansion());

        save.set_expansion_type(ExpansionType::Classic);
        assert!(!save.character.status().is_expansion());
    }
}