enfusion_pak 0.1.0

A library/cli for reading Enfusion game engine `.pak` files.
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
use std::ops::Range;

use crate::error::PakError;
use jiff::civil::DateTime;
use kinded::Kinded;
use log::debug;
use variantly::Variantly;
pub use winnow::LocatingSlice;
use winnow::ModalResult as WResult;
use winnow::Parser;
pub use winnow::Partial;
use winnow::binary::be_u32;
use winnow::binary::le_u16;
use winnow::binary::le_u32;
use winnow::binary::u8;
use winnow::combinator::alt;
use winnow::error::ErrMode;
use winnow::error::StrContext;
use winnow::stream::Offset;
use winnow::stream::Stream as _;
use winnow::token::take;

/// Represents some type of a file or directory
#[derive(Debug, Clone)]
pub struct FileEntry {
    name: String,
    meta: FileEntryMeta,
}

#[cfg(feature = "arc")]
pub type RcFileEntry = std::sync::Arc<FileEntry>;

#[cfg(not(feature = "arc"))]
pub type RcFileEntry = std::rc::Rc<FileEntry>;

impl FileEntry {
    /// Entry's name
    pub fn name(&self) -> &str {
        self.name.as_str()
    }

    /// What kind of entry this is
    pub fn kind(&self) -> FileEntryKind {
        self.meta.kind()
    }

    /// Entry metadata. For a directory this will contain its children,
    /// and for a file this will contain file metadata.
    pub fn meta(&self) -> &FileEntryMeta {
        &self.meta
    }

    /// Merges `other` into this node.
    pub fn merge(&mut self, other: Self) {
        let FileEntryMeta::Folder { children: self_children } = &mut self.meta else {
            panic!("merge should only be called on directories");
        };

        let FileEntryMeta::Folder { children: other_children } = other.meta else {
            panic!("merge should only be called on directories");
        };

        for other_child in other_children {
            if let Some(self_child) =
                self_children.iter_mut().find(|self_child| self_child.name == other_child.name)
            {
                if other_child.kind() == FileEntryKind::File {
                    debug!("{:#?}, {:#?}", &self_child, &other_child);
                }
                assert_eq!(other_child.kind(), self_child.kind());
                assert_ne!(
                    other_child.kind(),
                    FileEntryKind::File,
                    "File was duplicated across PAK files"
                );
                RcFileEntry::get_mut(self_child)
                    .expect("couldn't get self_child as mut")
                    .merge(RcFileEntry::try_unwrap(other_child).expect("couldn't unwrap child"));
            } else {
                self_children.push(other_child);
            }
        }
    }

    /// Merges refcounted children from `other` into this node.
    pub fn merge_ref(&mut self, other: RcFileEntry) {
        let FileEntryMeta::Folder { children: self_children } = &mut self.meta else {
            panic!("merge should only be called on directories");
        };

        let FileEntryMeta::Folder { children: other_children } = &other.meta else {
            panic!("merge should only be called on directories");
        };

        for other_child in other_children {
            if let Some(self_child) =
                self_children.iter_mut().find(|self_child| self_child.name == other_child.name)
            {
                if other_child.kind() == FileEntryKind::File {
                    debug!("{:#?}, {:#?}", &self_child, &other_child);
                }
                assert_eq!(other_child.kind(), self_child.kind());
                assert_ne!(
                    other_child.kind(),
                    FileEntryKind::File,
                    "File was duplicated across PAK files"
                );
                RcFileEntry::get_mut(self_child)
                    .expect("couldn't get self_child as mut")
                    .merge_ref(RcFileEntry::clone(other_child));
            } else {
                self_children.push(RcFileEntry::clone(other_child));
            }
        }
    }
}

/// An entry's metadata containing either its children or file metadata
#[derive(Debug, Clone, Kinded, Variantly)]
#[kinded(kind = FileEntryKind)]
#[non_exhaustive]
pub enum FileEntryMeta {
    Folder {
        children: Vec<RcFileEntry>,
    },
    File {
        offset: u32,
        compressed_len: u32,
        decompressed_len: u32,
        unk: u32,
        unk2: u16,
        compressed: u8,
        compression_level: u8,
        timestamp: u32,
    },
}

impl FileEntryMeta {
    /// Adds a child to this file entry. No-op if this is a folder
    pub fn push_child(&mut self, child: FileEntry) {
        if let FileEntryMeta::Folder { children } = self {
            children.push(RcFileEntry::new(child));
        }
    }

    /// Returns this file's timestamp. For directories there is no timestamp information
    /// and this will return `None`. For Files, this returns the date/time at which the file
    /// was modified(?). Note: there is no time zone information recorded.
    pub fn parsed_timestamp(&self) -> Option<jiff::civil::DateTime> {
        match self {
            FileEntryMeta::Folder { .. } => None,
            FileEntryMeta::File { timestamp, .. } => {
                let year = (timestamp >> 26) + 2000;
                let month = (timestamp >> 22) & 0xf;
                let day = (timestamp >> 17) & 0x1f;
                let hour = (timestamp >> 12) & 0x1f;
                let minute = (timestamp >> 6) & 0x3f;
                let second = timestamp & 0x3f;

                DateTime::new(
                    year as i16,
                    month as i8,
                    day as i8,
                    hour as i8,
                    minute as i8,
                    second as i8,
                    0,
                )
                .ok()
            }
        }
    }
}

impl TryFrom<u8> for FileEntryKind {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        let result = match value {
            0 => Self::Folder,
            1 => Self::File,
            _ => {
                panic!("unknown file entry kind: {value:#X}");
            }
        };

        Ok(result)
    }
}

#[derive(Debug)]
pub struct PakFile {
    chunks: Vec<Chunk>,
}

impl PakFile {
    /// Returns an immutable slice of the [`Chunk`]s contained in this `PakFile`.
    pub fn chunks(&self) -> &[Chunk] {
        &self.chunks
    }

    /// Returns a mutable `Vec` for this `PakFile`'s chunks.
    pub fn chunks_mut(&mut self) -> &mut Vec<Chunk> {
        &mut self.chunks
    }

    /// Finds and returns an immutable reference to the [`Chunk::File`] chunk contained in this `PakFile`.
    /// May return `None` if no such chunk exists.
    pub fn file_chunk(&self) -> Option<&Chunk> {
        self.chunks.iter().find(|chunk| chunk.is_file())
    }

    /// Finds and returns a mutable reference to the [`Chunk::File`] chunk contained in this `PakFile`.
    /// May return `None` if no such chunk exists.
    pub fn file_chunk_mut(&mut self) -> Option<&mut Chunk> {
        self.chunks.iter_mut().find(|chunk| chunk.is_file())
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum PakType {
    PAC1,
}

#[derive(Debug, Kinded, Variantly)]
#[non_exhaustive]
pub enum Chunk {
    Form {
        file_size: u32,
        pak_file_type: PakType,
    },
    Head {
        version: u32,
        unknown_data: Range<usize>,
    },
    /// Contains the
    Data {
        data: Range<usize>,
    },
    File {
        fs: RcFileEntry,
    },
    Unknown(u32),
}

impl PakFile {
    pub fn parse(data: &[u8]) -> Result<PakFile, PakError> {
        let mut parser = PakParser::new();

        let mut curr_data = data;
        loop {
            let mut input = Stream::new(curr_data);
            let start = input.checkpoint();
            // For mmap the parser should never raise an error or require state transitions
            match parser.parse(&mut input) {
                Ok(ParserStateMachine::Done(pak_file)) => {
                    return Ok(pak_file);
                }
                Ok(ParserStateMachine::Skip { from, count, parser: next_parser }) => {
                    let parsed = input.checkpoint().offset_from(&start);
                    curr_data = &curr_data[parsed..];

                    debug!(
                        "Skipping {count:#X} bytes from {from:#X} (offset is now: {:#X})",
                        next_parser.bytes_parsed()
                    );
                    curr_data = &curr_data[count..];
                    parser = next_parser;
                }
                Ok(state) => {
                    panic!("Unexpected state: {:?}", state.kind());
                }
                Err(winnow::error::ErrMode::Cut(e)) => {
                    return Err(PakError::ParserError(e));
                }
                Err(e) => {
                    panic!("Unknown error occurred: {e:?}");
                }
            }
        }
    }
}

pub struct PakParser {
    state: PakParserState,
    chunks: Vec<Chunk>,
    pak_len: Option<usize>,
    bytes_parsed: usize,
}

pub type Stream<'i> = Partial<&'i [u8]>;

impl PakParser {
    pub fn new() -> Self {
        PakParser {
            state: PakParserState::ParsingChunk,
            chunks: Vec::with_capacity(4),
            pak_len: None,
            bytes_parsed: 0,
        }
    }

    pub fn bytes_parsed(&self) -> usize {
        self.bytes_parsed
    }

    fn next_state(&mut self, bytes_consumed: usize, override_state: Option<PakParserState>) {
        debug!("Consumed {:#X} bytes from offset {:#X}", bytes_consumed, self.bytes_parsed);
        self.bytes_parsed += bytes_consumed;

        if let Some(override_state) = override_state {
            self.state = override_state
        } else {
            let old_state = std::mem::replace(&mut self.state, PakParserState::ParsingChunk);
            self.state = match old_state {
                PakParserState::ParsingChunk => PakParserState::ParsingChunk,
                PakParserState::ParsingFileChunk {
                    parsed_root,
                    mut parents,
                    bytes_processed,
                    chunk_len,
                } => {
                    if bytes_processed + bytes_consumed == chunk_len {
                        assert_eq!(parents.len(), 1);
                        self.chunks.push(Chunk::File {
                            fs: RcFileEntry::new(parents.pop().unwrap().entry),
                        });
                        PakParserState::Done
                    } else {
                        PakParserState::ParsingFileChunk {
                            parsed_root,
                            parents,
                            bytes_processed: bytes_processed + bytes_consumed,
                            chunk_len,
                        }
                    }
                }
                PakParserState::Done => PakParserState::Done,
            };
        }

        // If we've reached the end of the file, we're done. This check must come last
        // to ensure all of the above state transitions can handle anything they need
        // to do.
        if let Some(pak_len) = self.pak_len
            && self.bytes_parsed == pak_len
        {
            self.state = PakParserState::Done;
        }
    }

    pub fn parse_impl(mut self, input: &mut Stream) -> WResult<ParserStateMachine> {
        let start = input.checkpoint();
        debug!("Beginning read from {:#X}", self.bytes_parsed);

        match &self.state {
            PakParserState::ParsingChunk => {
                debug!("Reading a chunk");
                let res = parse_chunk(input);
                let parsed = match res {
                    Ok(parsed) => parsed,
                    Err(ErrMode::Incomplete(_)) => {
                        input.reset(&start);
                        return Ok(ParserStateMachine::Continue(self));
                    }
                    Err(e) => {
                        return Err(e);
                    }
                };
                debug!("Read complete! Result: {:#?}", parsed.kind());

                // Parse a single chunk
                let (skip, chunk, state) = match parsed {
                    Parsed::Chunk(chunk) => (0, Some(chunk), None),
                    Parsed::ChunkAndSkip(skip, chunk) => (skip, Some(chunk), None),
                    Parsed::FileChunkHeader { chunk_len } => {
                        debug!(
                            "We have {:#X} bytes to read starting at {:#X}",
                            chunk_len, self.bytes_parsed
                        );
                        let override_state = PakParserState::ParsingFileChunk {
                            parsed_root: false,
                            parents: Vec::with_capacity(4),
                            chunk_len,
                            bytes_processed: 0,
                        };

                        (0, None, Some(override_state))
                    }
                };

                let bytes_consumed = input.checkpoint().offset_from(&start);
                self.next_state(input.checkpoint().offset_from(&start) + skip, state);

                let skip_from = self.bytes_parsed - skip;

                if let Some(chunk) = chunk
                    && let Chunk::Form { file_size, .. } = &chunk
                {
                    // TODO: we shouldn't read the PAC1 data here
                    self.pak_len = Some((*file_size as usize) + (bytes_consumed - 4));
                    self.chunks.push(chunk);
                }

                if skip > 0 {
                    return Ok(ParserStateMachine::Skip {
                        from: skip_from,
                        count: skip,
                        parser: self,
                    });
                }
            }
            PakParserState::ParsingFileChunk { .. } => {
                debug!("Reading the file chunk");
                // Continue parsing file chunk
                match self.parse_file_entry(input) {
                    Ok(_) => {
                        // do nothing
                    }
                    Err(ErrMode::Incomplete(_)) => {
                        debug!("incomplete while reading file entry");
                        input.reset(&start);
                        return Ok(ParserStateMachine::Continue(self));
                    }
                    Err(e) => {
                        debug!("hard error while reading file entry");
                        return Err(e);
                    }
                }
                self.next_state(input.checkpoint().offset_from(&start), None);
            }
            PakParserState::Done => {
                debug!("Done");
                unreachable!("Loop should exist before PakParserState::Done is ever reached")
            }
        }

        Ok(ParserStateMachine::Loop(self))
    }

    pub fn parse(mut self, input: &mut Stream) -> WResult<ParserStateMachine> {
        // Parse as much data as we can. We either complete, or have to forward a state
        // machine quest up the stack.
        while !matches!(self.state, PakParserState::Done) {
            let next_state = self.parse_impl(input)?;

            if let ParserStateMachine::Loop(this) = next_state {
                self = this;
                continue;
            }

            return Ok(next_state);
        }

        Ok(ParserStateMachine::Done(self.complete()))
    }

    fn parse_file_entry(&mut self, input: &mut Stream) -> WResult<()> {
        let PakParserState::ParsingFileChunk { parsed_root, parents, .. } = &mut self.state else {
            panic!("Ended up in parse_file_entry in the wrong state")
        };

        let (entry, children) = parse_file_entry(input)?;

        match entry.meta.kind() {
            FileEntryKind::Folder => {
                parents.push(Directory {
                    is_root: !*parsed_root,
                    children_remaining: children,
                    entry,
                });

                *parsed_root = true;
            }
            FileEntryKind::File => {
                let parent = parents.last_mut().expect("bug: no parent for this file");
                parent.children_remaining = parent
                    .children_remaining
                    .checked_sub(1)
                    .expect("encountered more children than expected for a folder");

                parent.entry.meta.push_child(entry);
            }
        }

        // Check to see if the parents can be coalesced
        while let Some(dir) =
            parents.pop_if(|parent| parent.children_remaining == 0 && !parent.is_root)
        {
            let parent =
                parents.last_mut().expect("expected a folder to have a parent, but there is none");

            parent.children_remaining = parent
                .children_remaining
                .checked_sub(1)
                .expect("encountered more children than expected for a folder");

            parent.entry.meta.push_child(dir.entry);
        }

        Ok(())
    }

    pub fn complete(self) -> PakFile {
        PakFile { chunks: self.chunks }
    }
}

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

#[derive(Debug, Clone)]
struct Directory {
    is_root: bool,
    children_remaining: usize,
    entry: FileEntry,
}

#[derive(Debug)]
enum PakParserState {
    ParsingChunk,
    ParsingFileChunk {
        parsed_root: bool,
        parents: Vec<Directory>,
        bytes_processed: usize,
        chunk_len: usize,
    },
    Done,
}

impl PakParserState {}

#[derive(Kinded)]
enum Parsed {
    Chunk(Chunk),
    ChunkAndSkip(usize, Chunk),
    FileChunkHeader { chunk_len: usize },
}

#[derive(Kinded)]
pub enum ParserStateMachine {
    Loop(PakParser),
    Continue(PakParser),
    Skip { from: usize, count: usize, parser: PakParser },
    Done(PakFile),
}

fn parse_file_entry(input: &mut Stream) -> WResult<(FileEntry, usize)> {
    let entry_kind: FileEntryKind = u8(input)?.try_into().expect("???");
    let name_len = u8(input)?;
    let name = take(name_len).parse_next(input)?;
    let name =
        String::from_utf8(name.to_vec()).expect("name does not contain valid UTF8 characters");

    let (meta, children) = match entry_kind {
        FileEntryKind::Folder => {
            let children_count = le_u32(input)?;
            (FileEntryMeta::Folder { children: Default::default() }, children_count as usize)
        }
        FileEntryKind::File => {
            let offset = le_u32(input)?;
            let compressed_len = le_u32(input)?;
            let decompressed_len = le_u32(input)?;
            let unknown = le_u32(input)?;
            let unk2 = le_u16(input)?;
            let compressed = u8(input)?;
            let compression_level = u8(input)?;
            let timestamp = le_u32(input)?;

            assert_eq!(unknown, 0);
            assert_eq!(unk2, 0);
            assert!(matches!(compressed, 0 | 1));
            assert!(matches!(compression_level, 0 | 6));

            (
                FileEntryMeta::File {
                    offset,
                    compressed_len,
                    decompressed_len,
                    unk: unknown,
                    unk2,
                    compressed,
                    compression_level,
                    timestamp,
                },
                0,
            )
        }
    };

    Ok((FileEntry { name, meta }, children))
}

fn parse_form_chunk(input: &mut Stream) -> WResult<Parsed> {
    let file_size = be_u32(input)?;
    let pak_type_bytes: [u8; 4] = take(4usize)
        .parse_next(input)?
        .try_into()
        .expect("winnow should have returned a 4-byte buffer");
    let pak_file_type = match &pak_type_bytes {
        b"PAC1" => PakType::PAC1,
        unk => {
            panic!("unknown pak type: {unk:?}");
        }
    };

    Ok(Parsed::Chunk(Chunk::Form { file_size, pak_file_type }))
}

fn parse_head_chunk(input: &mut Stream) -> WResult<Parsed> {
    let head_start = input.checkpoint();
    let header_len = be_u32.parse_next(input)? as usize;
    assert_eq!(header_len, 0x1c);

    let mut skip_bytes = 0;

    let header_data_start = input.checkpoint();
    let version = le_u32.parse_next(input)?;
    let unknown_data_start = input.checkpoint();
    skip_bytes += unknown_data_start.offset_from(&header_data_start);

    let unknown_data_offset = unknown_data_start.offset_from(&head_start);

    let chunk = Chunk::Head {
        version,
        unknown_data: unknown_data_offset..(unknown_data_offset + skip_bytes),
    };

    Ok(Parsed::ChunkAndSkip(header_len - skip_bytes, chunk))
}

fn parse_data_chunk(input: &mut Stream) -> WResult<Parsed> {
    let data_chunk_start = input.checkpoint();
    let data_len = be_u32.parse_next(input)? as usize;

    let data_data_start = input.checkpoint();
    let data_data_offset = data_data_start.offset_from(&data_chunk_start);
    //take(data_len).void().parse_next(input)?;

    let chunk = Chunk::Data { data: data_data_offset..(data_data_offset + data_len) };

    Ok(Parsed::ChunkAndSkip(data_len, chunk))
}

fn parse_file_chunk(input: &mut Stream) -> WResult<Parsed> {
    let chunk_len = be_u32(input)? as usize;
    Ok(Parsed::FileChunkHeader { chunk_len })
}

fn parse_chunk(input: &mut Stream) -> WResult<Parsed> {
    alt((
        (b"FORM", parse_form_chunk)
            .context(StrContext::Label("chunk"))
            .context(StrContext::Expected(winnow::error::StrContextValue::Description("FORM"))),
        (b"HEAD", parse_head_chunk)
            .context(StrContext::Label("chunk"))
            .context(StrContext::Expected(winnow::error::StrContextValue::Description("HEAD"))),
        (b"DATA", parse_data_chunk)
            .context(StrContext::Label("chunk"))
            .context(StrContext::Expected(winnow::error::StrContextValue::Description("DATA"))),
        (b"FILE", parse_file_chunk)
            .context(StrContext::Label("chunk"))
            .context(StrContext::Expected(winnow::error::StrContextValue::Description("FILE"))),
    ))
    .parse_next(input)
    .map(|(_, parsed)| parsed)
}