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
use crate::darksiders1::gfc;
use failure::Error;
use std::io::{Read, Seek, Write};

pub fn read(stream: impl Read + Seek) -> Result<gfc::Object, Error> {
    gfc::DSSaveGameManager::read_save(stream)
}

pub fn write(stream: impl Write + Seek, data: &gfc::Object) -> Result<(), Error> {
    gfc::DSSaveGameManager::write_save(stream, data)
}

#[cfg(test)]
mod tests {
    use crate::{darksiders1::gfc, dsav};
    use failure::Error;
    use flate2::read::ZlibDecoder;
    use std::{
        convert::{TryFrom, TryInto},
        fs,
        io,
        ops::Range,
    };

    // Note about ZlibDecoder in these tests:
    //
    // The save files contain compressed data. Darksiders uses zlib, while we use
    // flate2, so the compressed streams will not be identical. Instead, we
    // decompress them and compare the data inside.

    const INITIAL_BUFFER: usize = 16384;

    parameterized_test!(round_trip_data, [
        new_game => (&read_fixture("new-game.dsav")?),
        hundo => (&read_fixture("100%.dsav")?),
    ]);

    parameterized_test!(round_trip_json, [
        new_game => (&read_fixture("new-game.dsav")?),
        hundo => (&read_fixture("100%.dsav")?),
    ]);

    parameterized_test!(round_trip_game_info, [
        new_game => (&read_fixture("new-game.dsav")?),
        hundo => (&read_fixture("100%.dsav")?),
    ]);

    parameterized_test!(round_trip_player_save_data, [
        new_game => (&read_fixture("new-game.dsav")?),
        hundo => (&read_fixture("100%.dsav")?),
    ]);

    parameterized_test!(round_trip_world_data, [
        new_game => (&read_fixture("new-game.dsav")?),
        hundo => (&read_fixture("100%.dsav")?),
    ]);

    /// Test that when we write a save and read it back, we get an identical
    /// structure in memory.
    fn round_trip_data(dsav1: &[u8]) -> Result<(), Error> {
        let object1 = dsav::read(io::Cursor::new(dsav1))?;

        let mut dsav2 = Vec::with_capacity(INITIAL_BUFFER);
        dsav::write(io::Cursor::new(&mut dsav2), &object1)?;

        let object2 = dsav::read(io::Cursor::new(&mut dsav2))?;

        assert_eq!(format!("{:#?}", object1), format!("{:#?}", object2));
        Ok(())
    }

    /// Test that we can convert to/from JSON losslessly.
    fn round_trip_json(dsav: &[u8]) -> Result<(), Error> {
        let object1 = dsav::read(io::Cursor::new(dsav))?;
        let json = serde_json::to_string(&object1)?;
        let object2: gfc::Object = serde_json::from_str(&json)?;

        assert_eq!(format!("{:#?}", object1), format!("{:#?}", object2));
        Ok(())
    }

    /// Test that we can read and rewrite an identical copy of a DSAV's header.
    fn round_trip_game_info(dsav1: &[u8]) -> Result<(), Error> {
        let object = dsav::read(io::Cursor::new(dsav1))?;

        let mut dsav2 = Vec::with_capacity(INITIAL_BUFFER);
        dsav::write(io::Cursor::new(&mut dsav2), &object)?;

        // Implementation detail: the data before the BOD is 0x39 bytes long. Skip over
        // bytes 0x9-0xc since those are an offset that depends on the compressed data
        // length, which will be different between the two zlib implementations.
        let range1 = ..0x9;
        let range2 = 0xd..0x39;
        assert_eq!(&dsav1[range1], &dsav2[range1]);
        assert_eq!(&dsav1[range2.clone()], &dsav2[range2]);
        Ok(())
    }

    /// Test that we can read and rewrite an identical copy of a DSAV's
    /// `PlayerSaveData`.
    fn round_trip_player_save_data(dsav1: &[u8]) -> Result<(), Error> {
        let object = dsav::read(io::Cursor::new(dsav1))?;

        let mut dsav2 = Vec::with_capacity(INITIAL_BUFFER);
        dsav::write(io::Cursor::new(&mut dsav2), &object)?;

        // Implementation detail: compressed data starts at offset 0x39.
        let offset = 0x39;
        let mut bod1 = Vec::with_capacity(INITIAL_BUFFER);
        let mut bod2 = Vec::with_capacity(INITIAL_BUFFER);
        io::copy(&mut ZlibDecoder::new(&dsav1[offset..]), &mut bod1)?;
        io::copy(&mut ZlibDecoder::new(&dsav2[offset..]), &mut bod2)?;
        assert_eq!(bod1, bod2);
        Ok(())
    }

    /// Test that we can read and rewrite an identical copy of a DSAV's
    /// `WorldData`.
    fn round_trip_world_data(dsav1: &[u8]) -> Result<(), Error> {
        let object = dsav::read(io::Cursor::new(dsav1))?;

        let mut dsav2 = Vec::with_capacity(INITIAL_BUFFER);
        dsav::write(io::Cursor::new(&mut dsav2), &object)?;

        let data1 = extract_world_data(&dsav1)?;
        let data2 = extract_world_data(&dsav2)?;
        assert_eq!(data1, data2);
        Ok(())
    }

    fn extract_world_data(dsav: &[u8]) -> Result<Vec<u8>, Error> {
        // Implementation detail: world data starts at an offset pointed to in the
        // header, with 4 bytes added to skip the length prefix.
        let meta_offset: Range<usize> = 0x9..0xd;
        let offset = i32::from_le_bytes(dsav[meta_offset].try_into()?);
        let offset = usize::try_from(offset)? + 4;
        let mut buffer = Vec::with_capacity(INITIAL_BUFFER);
        io::copy(&mut ZlibDecoder::new(&dsav[offset..]), &mut buffer)?;
        Ok(buffer)
    }

    fn read_fixture(name: &str) -> io::Result<Vec<u8>> {
        let root = env!("CARGO_MANIFEST_DIR");
        let path = format!("{}/src/dsav/fixtures/{}", root, name);
        fs::read(&path)
    }
}