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
use binread::{derive_binread, BinRead, BinReaderExt, NullString, io::{self, SeekFrom}};
use binread::file_ptr::{FilePtr, IntoSeekFrom};
use core::fmt;
#[derive(BinRead, Debug)]
#[br(big)]
struct GcmTop (
#[br(seek_before = SeekFrom::Start(0x1c))]
pub GcmFile,
);
#[derive(BinRead)]
pub struct GameId(pub [u8; 6]);
#[derive_binread]
#[derive(Debug)]
#[br(magic = 0xc2339f3d_u32)]
pub struct GcmFile {
#[br(seek_before = SeekFrom::Start(0))]
pub game_id: GameId,
pub disc_number: u8,
pub revision: u8,
#[br(seek_before = SeekFrom::Start(0x20))]
#[br(map = NullString::into_string)]
pub internal_name: String,
#[br(seek_before = SeekFrom::Start(0x420))]
pub dol_offset: u32,
fs_offset: u32,
fs_size: u32,
max_fs_size: u32,
#[br(seek_before = SeekFrom::Start(fs_offset as u64))]
#[br(args(fs_offset, fs_size))]
pub filesystem: FileSystem,
}
#[derive(BinRead, Debug)]
#[br(import(offset: u32, size: u32))]
pub struct FileSystem {
pub root: RootNode,
#[br(args(
offset as u64,
(offset + (root.total_node_count * FsNode::SIZE)) as u64
))]
#[br(count = root.total_node_count - 1)]
pub files: Vec<FsNode>,
}
#[derive(BinRead, Debug)]
#[br(magic = 1u8)]
pub struct RootNode {
#[br(map = U24::into)]
pub name_offset: u32,
pub node_start_index: u32,
pub total_node_count: u32,
}
type FilePtr24<T> = FilePtr<U24, T>;
#[br(import(root_offset: u64, name_offset: u64))]
#[derive(BinRead, Debug)]
pub enum FsNode {
#[br(magic = 0u8)]
File {
#[br(offset = name_offset)]
#[br(parse_with = FilePtr24::parse)]
#[br(map = NullString::into_string)]
name: String,
offset: u32,
size: u32,
},
#[br(magic = 1u8)]
Directory {
#[br(offset = name_offset)]
#[br(parse_with = FilePtr24::parse)]
#[br(map = NullString::into_string)]
name: String,
parent_index: u32,
end_index: u32,
},
}
impl FsNode {
const SIZE: u32 = 0xC;
}
#[derive(BinRead, Clone, Copy)]
struct U24([u8; 3]);
impl IntoSeekFrom for U24 {
fn into_seek_from(self) -> SeekFrom {
u32::from(self).into_seek_from()
}
}
impl From<U24> for u32 {
fn from(U24(x): U24) -> Self {
u32::from_be_bytes([0, x[0], x[1], x[2]])
}
}
impl fmt::Debug for GameId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match std::str::from_utf8(&self.0[..]) {
Ok(id) => fmt::Debug::fmt(id, f),
Err(_) => write!(f, "GameId({:02x?})", &self.0[..])
}
}
}
mod error;
mod dir_listing;
pub use error::GcmError;
pub use dir_listing::*;
impl GcmFile {
pub fn from_reader<R>(reader: &mut R) -> Result<Self, GcmError>
where R: io::Read + io::Seek,
{
Ok(reader.read_be::<GcmTop>()?.0)
}
}
use std::path::Path;
impl GcmFile {
pub fn open<P>(path: P) -> Result<Self, GcmError>
where P: AsRef<Path>,
{
let mut reader = std::io::BufReader::new(std::fs::File::open(path)?);
Ok(reader.read_be::<GcmTop>()?.0)
}
}