1use std::sync::Arc;
7
8use crate::bundle::{self, Asset, FileRef, Kind, Manifest};
9use crate::folder::Source;
10use crate::toc::{Toc, TocChunk};
11use crate::{Error, Result, depot, hex, lookup};
12
13#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct Listed {
16 pub toc: String,
19 pub bundle: String,
21 pub kind: Kind,
23 pub index: usize,
25 pub name: String,
27 pub resource_type: Option<u32>,
29 pub resource_id: Option<u64>,
31 pub size: u32,
33 pub file: FileRef,
35}
36
37#[derive(Clone, Debug)]
39pub struct Bundle {
40 pub toc_path: String,
42 pub name: String,
44 pub(super) toc: Arc<Toc>,
45 pub(super) files: Vec<FileRef>,
46 pub(super) first: usize,
48 pub manifest: Manifest,
50}
51
52#[derive(Clone, Debug, Default)]
55pub struct Listing {
56 pub bundles: Vec<Bundle>,
58 pub unread: Vec<(String, String)>,
60}
61
62impl Listing {
63 pub fn entries(&self) -> impl Iterator<Item = Listed> + '_ {
65 self.bundles.iter().flat_map(Bundle::entries)
66 }
67}
68
69impl Bundle {
70 fn file_index(&self, kind: Kind, index: usize) -> usize {
73 let m = &self.manifest;
74 let before = match kind {
75 Kind::Ebx => 0,
76 Kind::Res => m.ebx.len(),
77 Kind::Chunk => m.ebx.len().saturating_add(m.res.len()),
78 };
79 self.first.saturating_add(before).saturating_add(index)
80 }
81
82 fn listed(&self, kind: Kind, index: usize, a: &Asset) -> Option<Listed> {
84 let res = kind == Kind::Res;
85 Some(Listed {
86 toc: self.toc_path.clone(),
87 bundle: self.name.clone(),
88 kind,
89 index,
90 name: match kind {
91 Kind::Chunk => hex(&a.guid),
92 _ => a.name.clone(),
93 },
94 resource_type: res.then_some(a.rtype),
95 resource_id: res.then_some(a.rid),
96 size: if kind == Kind::Chunk { a.lsize } else { a.size },
97 file: *self.files.get(self.file_index(kind, index))?,
98 })
99 }
100
101 pub fn entries(&self) -> impl Iterator<Item = Listed> + '_ {
103 let m = &self.manifest;
104 let ebx = (m.ebx.iter().enumerate()).filter_map(move |(i, a)| self.listed(Kind::Ebx, i, a));
105 let res = (m.res.iter().enumerate()).filter_map(move |(i, a)| self.listed(Kind::Res, i, a));
106 let chunks = (m.chunks.iter().enumerate()).filter_map(move |(i, a)| self.listed(Kind::Chunk, i, a));
107 ebx.chain(res).chain(chunks)
108 }
109
110 pub fn ebx(&self, i: usize) -> Option<Listed> {
112 self.listed(Kind::Ebx, i, self.manifest.ebx.get(i)?)
113 }
114
115 pub fn resource(&self, i: usize) -> Option<Listed> {
117 self.listed(Kind::Res, i, self.manifest.res.get(i)?)
118 }
119
120 pub fn toc_chunk(&self, src: &Source<'_>, guid: &[u8; 16]) -> Result<Option<(FileRef, usize)>> {
124 let Some(TocChunk { file, .. }) = self.toc.chunks.iter().find(|c| &c.guid == guid) else {
125 return Ok(None);
126 };
127 let file =
128 file.ok_or_else(|| Error::Missing(format!("{}: chunk {} is removed", self.name, hex(guid))))?;
129 let raw = src.raw(file)?;
130 let blocks = crate::cas::blocks(&raw)?;
131 Ok(Some((
132 file,
133 blocks
134 .iter()
135 .map(|b| b.decoded_size)
136 .fold(0usize, usize::saturating_add),
137 )))
138 }
139
140 pub fn chunk(&self, src: &Source<'_>, guid: &[u8; 16]) -> Result<(FileRef, usize)> {
144 let m = &self.manifest;
145 if let Some((i, c)) = m.chunks.iter().enumerate().find(|(_, c)| &c.guid == guid) {
146 let file = self.files.get(self.file_index(Kind::Chunk, i)).ok_or_else(|| {
147 Error::Missing(format!(
148 "{}: chunk {} has no file in the bundle's file table",
149 self.name,
150 hex(guid)
151 ))
152 })?;
153 return Ok((*file, c.lsize as usize));
154 }
155 let TocChunk { file, .. } = self.toc.chunks.iter().find(|c| &c.guid == guid).ok_or_else(|| {
156 Error::Missing(format!(
157 "{}: chunk {} is in neither the bundle nor its TOC",
158 self.name,
159 hex(guid)
160 ))
161 })?;
162 let file =
163 file.ok_or_else(|| Error::Missing(format!("{}: chunk {} is removed", self.name, hex(guid))))?;
164 let raw = src.raw(file)?;
165 let blocks = crate::cas::blocks(&raw)?;
166 Ok((
167 file,
168 blocks
169 .iter()
170 .map(|b| b.decoded_size)
171 .fold(0usize, usize::saturating_add),
172 ))
173 }
174}
175
176pub fn resource_type_name(rtype: u32) -> Option<&'static str> {
178 TYPE_NAMES
179 .iter()
180 .find(|(id, _)| *id == rtype)
181 .map(|(_, name)| *name)
182}
183
184pub const TYPE_NAMES: &[(u32, &str)] = &[
192 (crate::TEXTURE, "Texture"),
193 (crate::MESHSET, "MeshSet"),
194 (depot::RTYPE, "ShaderBlockDepotResource"),
195 (crate::bytecode::RTYPE, "CompiledBytecode"),
196 (lookup::PROGRAM_LOOKUP, "ExpressionShaderDatabase"),
197 (
198 lookup::TEXTURE_LOOKUP,
199 "ExpressionShaderStreamableTextureSetLookupTable",
200 ),
201 (0x2FEC_6526, "ShaderStateAssetId"),
202 (0xD489_FACB, "ExpressionShaderRasterPermutation"),
203 (0x95CE_62DD, "ExpressionShaderComputePermutation"),
204 (0xA19B_025D, "ExpressionShaderBindingSet"),
205 (0x7D98_F632, "ExpressionShaderInputLayout"),
206 (0x2500_47B2, "ExpressionShaderRasterPermutationSharedData"),
207 (0x879B_A711, "ExpressionShaderPermutationSharedData"),
208 (0x7A63_0735, "ExpressionShaderStreamableTextureSet"),
209 (0xA895_7ED6, "ExpressionShaderRootSlotInitData"),
210 (0x589B_B36E, "ExpressionShaderPassBinding"),
211 (0x6A5B_E5DB, "ExpressionShaderBindingSignature"),
212 (0xF434_940D, "ShaderParameterCollectionTable"),
213 (0x4175_9364, "PhysicsResource"),
214 (0x30B4_A553, "OccluderMesh"),
215 (0x1CA3_8E06, "VisualTerrain"),
216 (0x957C_32B1, "AtlasTexture"),
217 (0xDE54_0C59, "LayerGraphs"),
218 (0x428E_C9D4, "BundleRefTableResource"),
219 (0x22FE_8AC8, "TerrainStreamingTree"),
220 (0x15E1_F32E, "TerrainDecals"),
221];
222
223pub fn type_id(name: &str) -> u32 {
225 crate::djb2(&name.to_ascii_lowercase())
226}
227
228impl Source<'_> {
229 pub fn bundle_names(&self, toc: &str) -> Result<Vec<String>> {
231 Ok(self
232 .folder
233 .toc(toc)?
234 .bundles
235 .into_iter()
236 .map(|(n, _)| n)
237 .collect())
238 }
239
240 fn open_bundle(&self, toc_path: &str, toc: &Arc<Toc>, name: &str, region: &[u8]) -> Result<Bundle> {
241 let (files, inline) = bundle::read_region(region)?;
242 let (manifest, first, _) = self
243 .manifest(&files, &inline)?
244 .ok_or_else(|| Error::Missing(format!("{name} has no manifest")))?;
245 Ok(Bundle {
246 toc_path: toc_path.to_owned(),
247 name: name.to_owned(),
248 toc: Arc::clone(toc),
249 files,
250 first,
251 manifest,
252 })
253 }
254
255 pub fn bundle(&self, toc: &str, name: &str) -> Result<Bundle> {
257 let t = Arc::new(self.folder.toc(toc)?);
258 let (n, region) = t
259 .bundles
260 .iter()
261 .find(|(n, _)| n.eq_ignore_ascii_case(name))
262 .ok_or_else(|| Error::Missing(format!("{toc} has no bundle {name}")))?;
263 self.open_bundle(toc, &t, n, region)
264 }
265
266 pub fn listing(&self, toc: &str) -> Result<Listing> {
268 let t = Arc::new(self.folder.toc(toc)?);
269 let mut out = Listing::default();
270 for (name, region) in &t.bundles {
271 match self.open_bundle(toc, &t, name, region) {
272 Ok(b) => out.bundles.push(b),
273 Err(e) => out.unread.push((name.clone(), e.to_string())),
274 }
275 }
276 Ok(out)
277 }
278
279 pub fn ebx_type(&self, entry: &Listed) -> Result<String> {
281 if entry.kind != Kind::Ebx {
282 return Err(Error::Unsupported(format!("{}: not an EBX document", entry.name)));
283 }
284 let bytes = self.payload(entry.file, entry.size as usize)?;
285 Ok(crate::ebx::read(&bytes)?.root_type)
286 }
287}