themelios_archive/
dirdat.rs1use gospel::read::{Reader, Le as _};
4use gospel::write::{Writer, Le as _};
5
6#[derive(Clone, Copy, PartialEq, Eq)]
11pub struct Name([u8; 12]);
12
13impl std::ops::Deref for Name {
14 type Target = [u8; 12];
15
16 fn deref(&self) -> &Self::Target {
17 &self.0
18 }
19}
20
21impl Default for Name {
22 fn default() -> Self {
23 Self(*b"/_______.___")
24 }
25}
26
27impl std::fmt::Debug for Name {
28 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
29 write!(f, "Name({:?})", self.to_string())
30 }
31}
32
33impl std::fmt::Display for Name {
34 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
35 let name = cp932::decode_lossy(&self.0);
36 let name = name.to_lowercase();
37 if let Some((name, ext)) = name.split_once('.') {
38 write!(f, "{}.{}", name.trim_end_matches(' '), ext.trim_end_matches(' '))
39 } else {
40 f.write_str(name.trim_end_matches(' '))
41 }
42 }
43}
44
45impl TryFrom<String> for Name {
46 type Error = NameError;
47
48 fn try_from(value: String) -> Result<Self, Self::Error> {
49 Name::try_from(value.as_str())
50 }
51}
52
53impl TryFrom<&str> for Name {
54 type Error = NameError;
55
56 fn try_from(name: &str) -> Result<Self, Self::Error> {
57 let (_, name) = name.rsplit_once(['/', '\\']).unwrap_or(("", name));
58 let name = name.to_uppercase();
59 let (name, ext) = name.split_once('.').unwrap_or((&name, ""));
60 let name = cp932::encode(name).map_err(|_| NameError)?;
61 let ext = cp932::encode(ext).map_err(|_| NameError)?;
62 if name.len() > 8 || ext.len() > 3 { return Err(NameError); }
63 let mut o = *b" . ";
64 o[..name.len()].copy_from_slice(&name);
65 o[9..][..ext.len()].copy_from_slice(&ext);
66 Ok(Name(o))
67 }
68}
69
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub struct NameError;
73
74impl std::fmt::Display for NameError {
75 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
76 f.write_str("cannot convert to archive name")
77 }
78}
79
80impl std::error::Error for NameError {}
81
82#[derive(Debug, Clone, PartialEq, Eq, Default)]
86pub struct DirEntry {
87 pub name: Name,
89 pub unk1: u32,
92 pub size: usize,
94 pub unk2: usize,
97 pub reserved_size: usize,
99 pub timestamp: u32,
101 pub offset: usize,
103}
104
105pub fn read_dir(data: &[u8]) -> Result<Vec<DirEntry>, gospel::read::Error> {
110 let mut f = Reader::new(data);
111 f.check(b"LB DIR\x1A\0")?;
112 let count = f.u64()? as usize;
113
114 let mut items = Vec::with_capacity(count);
115
116 for _ in 0..count {
117 items.push(DirEntry {
118 name: Name(f.array::<12>()?),
119 unk1: f.u32()?,
120 size: f.u32()? as usize,
121 unk2: f.u32()? as usize,
122 reserved_size: f.u32()? as usize,
123 timestamp: f.u32()?,
124 offset: f.u32()? as usize,
125 });
126 }
127
128 Ok(items)
129}
130
131pub fn write_dir(entries: &[DirEntry]) -> Vec<u8> {
133 let mut f = Writer::new();
134 f.slice(b"LB DIR\x1A\0");
135 f.u64(entries.len() as u64);
136
137 for e in entries {
138 f.array::<12>(e.name.0);
139 f.u32(e.unk1);
140 f.u32(e.size as u32);
141 f.u32(e.unk2 as u32);
142 f.u32(e.reserved_size as u32);
143 f.u32(e.timestamp);
144 f.u32(e.offset as u32);
145 }
146
147 f.finish().unwrap()
148}