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
use crate::{Eu4Error, Eu4Save, Eu4SaveMeta, FailedResolveStrategy, GameState, Meta, TokenLookup};
use jomini::{BinaryDeserializerBuilder, TextDeserializer, TextTape};
use serde::de::DeserializeOwned;
use std::fmt;
use std::io::{Read, Seek, SeekFrom};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Encoding {
    Text,
    TextZip,
    BinZip,
}

impl Encoding {
    pub fn as_str(&self) -> &'static str {
        match self {
            Encoding::Text => "text",
            Encoding::TextZip => "textzip",
            Encoding::BinZip => "binzip",
        }
    }
}

impl fmt::Display for Encoding {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[derive(Debug, Clone, Copy)]
pub enum Extraction {
    InMemory,
    #[cfg(feature = "mmap")]
    MmapTemporaries,
}

#[derive(Debug, Clone)]
pub struct Eu4ExtractorBuilder {
    extraction: Extraction,
    on_failed_resolve: FailedResolveStrategy,
}

impl Default for Eu4ExtractorBuilder {
    fn default() -> Self {
        Eu4ExtractorBuilder::new()
    }
}

impl Eu4ExtractorBuilder {
    pub fn new() -> Self {
        Eu4ExtractorBuilder {
            extraction: Extraction::InMemory,
            on_failed_resolve: FailedResolveStrategy::Ignore,
        }
    }

    pub fn with_extraction(mut self, extraction: Extraction) -> Self {
        self.extraction = extraction;
        self
    }

    pub fn with_on_failed_resolve(mut self, strategy: FailedResolveStrategy) -> Self {
        self.on_failed_resolve = strategy;
        self
    }

    pub fn build(self) -> Eu4Extractor {
        Eu4Extractor {
            extraction: self.extraction,
            on_failed_resolve: self.on_failed_resolve,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Eu4Extractor {
    extraction: Extraction,
    on_failed_resolve: FailedResolveStrategy,
}

impl Default for Eu4Extractor {
    fn default() -> Self {
        Eu4ExtractorBuilder::new().build()
    }
}

impl Eu4Extractor {
    pub fn extract_meta<R>(&self, mut reader: R) -> Result<(Meta, Encoding), Eu4Error>
    where
        R: Read + Seek,
    {
        let mut header = [0; "EU4txt".len()];
        reader.read_exact(&mut header).map_err(Eu4Error::IoErr)?;

        let mut buffer = Vec::with_capacity(0);
        if is_text(&header).is_some() {
            reader.read_to_end(&mut buffer).map_err(Eu4Error::IoErr)?;
            let meta = TextDeserializer::from_slice(&buffer)?;
            Ok((meta, Encoding::Text))
        } else if is_zip(&header) {
            reader.seek(SeekFrom::Start(0)).map_err(Eu4Error::IoErr)?;
            let mut zip = zip::ZipArchive::new(reader).map_err(Eu4Error::ZipCentralDirectory)?;
            match self.extraction {
                Extraction::InMemory => {
                    melt_in_memory(&mut buffer, "meta", &mut zip, self.on_failed_resolve)
                }

                #[cfg(feature = "mmap")]
                Extraction::MmapTemporaries => {
                    melt_with_temporary("meta", &mut zip, self.on_failed_resolve)
                }
            }
        } else {
            Err(Eu4Error::UnknownHeader)
        }
    }

    pub fn extract_save<R>(&self, mut reader: R) -> Result<(Eu4Save, Encoding), Eu4Error>
    where
        R: Read + Seek,
    {
        let mut header = [0; "EU4txt".len()];
        reader.read_exact(&mut header).map_err(Eu4Error::IoErr)?;

        let mut buffer = Vec::with_capacity(0);
        if is_text(&header).is_some() {
            reader.read_to_end(&mut buffer).map_err(Eu4Error::IoErr)?;
            let tape = TextTape::from_slice(&buffer)?;
            let meta: Meta = TextDeserializer::from_tape(&tape)?;
            let game: GameState = TextDeserializer::from_tape(&tape)?;
            Ok((Eu4Save { meta, game }, Encoding::Text))
        } else if is_zip(&header) {
            reader.seek(SeekFrom::Start(0)).map_err(Eu4Error::IoErr)?;
            let mut zip = zip::ZipArchive::new(reader).map_err(Eu4Error::ZipCentralDirectory)?;
            let (meta, encoding) = match self.extraction {
                Extraction::InMemory => {
                    melt_in_memory(&mut buffer, "meta", &mut zip, self.on_failed_resolve)
                }

                #[cfg(feature = "mmap")]
                Extraction::MmapTemporaries => {
                    melt_with_temporary("meta", &mut zip, self.on_failed_resolve)
                }
            }?;

            let (game, _) = match self.extraction {
                Extraction::InMemory => {
                    melt_in_memory(&mut buffer, "gamestate", &mut zip, self.on_failed_resolve)
                }

                #[cfg(feature = "mmap")]
                Extraction::MmapTemporaries => {
                    melt_with_temporary("gamestate", &mut zip, self.on_failed_resolve)
                }
            }?;

            Ok((Eu4Save { meta, game }, encoding))
        } else {
            Err(Eu4Error::UnknownHeader)
        }
    }

    // For the times where all you want is the metadata but will accept the game state too save on
    // future needless double parsing.
    pub fn extract_meta_optimistic<R>(
        &self,
        mut reader: R,
    ) -> Result<(Eu4SaveMeta, Encoding), Eu4Error>
    where
        R: Read + Seek,
    {
        let mut header = [0; "EU4txt".len()];
        reader.read_exact(&mut header).map_err(Eu4Error::IoErr)?;

        let mut buffer = Vec::with_capacity(0);

        // If we're encountering text then since we have to read through the whole document anyways
        // to extract the metadata we might as well extract the game state too.
        if is_text(&header).is_some() {
            reader.read_to_end(&mut buffer).map_err(Eu4Error::IoErr)?;
            let tape = TextTape::from_slice(&buffer)?;
            let meta: Meta = TextDeserializer::from_tape(&tape)?;
            let game: Option<GameState> = TextDeserializer::from_tape(&tape).map(Some)?;
            Ok((Eu4SaveMeta { meta, game }, Encoding::Text))
        } else if is_zip(&header) {
            reader.seek(SeekFrom::Start(0)).map_err(Eu4Error::IoErr)?;
            let mut zip = zip::ZipArchive::new(reader).map_err(Eu4Error::ZipCentralDirectory)?;
            let (meta, encoding) = match self.extraction {
                Extraction::InMemory => {
                    melt_in_memory(&mut buffer, "meta", &mut zip, self.on_failed_resolve)
                }

                #[cfg(feature = "mmap")]
                Extraction::MmapTemporaries => {
                    melt_with_temporary("meta", &mut zip, self.on_failed_resolve)
                }
            }?;

            Ok((Eu4SaveMeta { meta, game: None }, encoding))
        } else {
            Err(Eu4Error::UnknownHeader)
        }
    }
}

fn melt_in_memory<T, R>(
    mut buffer: &mut Vec<u8>,
    name: &'static str,
    zip: &mut zip::ZipArchive<R>,
    on_failed_resolve: FailedResolveStrategy,
) -> Result<(T, Encoding), Eu4Error>
where
    R: Read + Seek,
    T: DeserializeOwned,
{
    buffer.clear();
    let mut zip_file = zip
        .by_name(name)
        .map_err(|e| Eu4Error::ZipMissingEntry(name, e))?;

    // protect against excessively large uncompressed data
    if zip_file.size() > 1024 * 1024 * 200 {
        return Err(Eu4Error::ZipSize(name));
    }

    buffer.reserve(zip_file.size() as usize);
    zip_file
        .read_to_end(&mut buffer)
        .map_err(|e| Eu4Error::ZipExtraction(name, e))?;

    if let Some(data) = is_bin(&buffer) {
        let res = BinaryDeserializerBuilder::new()
            .on_failed_resolve(on_failed_resolve)
            .from_slice(data, TokenLookup)
            .map_err(|e| Eu4Error::Deserialize {
                part: Some(name.to_string()),
                err: e,
            })?;
        Ok((res, Encoding::BinZip))
    } else if let Some(data) = is_text(&buffer) {
        let res = TextDeserializer::from_slice(data)?;
        Ok((res, Encoding::TextZip))
    } else {
        Err(Eu4Error::UnknownHeader)
    }
}

#[cfg(feature = "mmap")]
fn melt_with_temporary<T, R>(
    name: &'static str,
    zip: &mut zip::ZipArchive<R>,
    on_failed_resolve: FailedResolveStrategy,
) -> Result<(T, Encoding), Eu4Error>
where
    R: Read + Seek,
    T: DeserializeOwned,
{
    use std::io::{BufWriter, Write};

    let mut zip_file = zip
        .by_name(name)
        .map_err(|e| Eu4Error::ZipMissingEntry(name, e))?;

    // protect against excessively large uncompressed data
    if zip_file.size() > 1024 * 1024 * 200 {
        return Err(Eu4Error::ZipSize(name));
    }

    let file = tempfile::tempfile().map_err(Eu4Error::IoErr)?;
    let mut writer = BufWriter::new(file);
    std::io::copy(&mut zip_file, &mut writer).map_err(|e| Eu4Error::ZipExtraction(name, e))?;
    writer.flush().map_err(Eu4Error::IoErr)?;
    let file = writer.into_inner().unwrap();
    let mmap = unsafe {
        memmap::MmapOptions::new()
            .map(&file)
            .map_err(Eu4Error::IoErr)?
    };
    let buffer = &mmap[..];

    if let Some(data) = is_bin(&buffer) {
        let res = BinaryDeserializerBuilder::new()
            .on_failed_resolve(on_failed_resolve)
            .from_slice(data, TokenLookup)
            .map_err(|e| Eu4Error::Deserialize {
                part: Some(name.to_string()),
                err: e,
            })?;
        Ok((res, Encoding::BinZip))
    } else if let Some(data) = is_text(&buffer) {
        let res = TextDeserializer::from_slice(data)?;
        Ok((res, Encoding::TextZip))
    } else {
        Err(Eu4Error::UnknownHeader)
    }
}

fn is_text(data: &[u8]) -> Option<&[u8]> {
    let sentry = b"EU4txt";
    if data.get(..sentry.len()).map_or(false, |x| x == sentry) {
        Some(&data[sentry.len()..])
    } else {
        None
    }
}

fn is_bin(data: &[u8]) -> Option<&[u8]> {
    let sentry = b"EU4bin";
    if data.get(..sentry.len()).map_or(false, |x| x == sentry) {
        Some(&data[sentry.len()..])
    } else {
        None
    }
}

fn is_zip(data: &[u8]) -> bool {
    let sentry = [0x50, 0x4b, 0x03, 0x04];
    data.get(..sentry.len()).map_or(false, |x| x == sentry)
}