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
//! Bundles searched together: the way the game resolves a resource id or a name, which it looks up in every bundle
//! loaded. A bundle with a program database holds the permutations it adds and names the stock ones, which lie in
//! another bundle (the game's configuration bundle in `globals.toc`, a level's root).
use std::collections::HashMap;
use crate::inputlayout;
use crate::listing::{Bundle, Listed};
use crate::{binding, bytecode, lookup, permutation};
/// Bundles that ids and names are looked up in, with the index of what they hold built once: the permutations by type
/// and id, the CompiledBytecode resources by name. Where two bundles hold the same id or name, the earlier wins.
#[derive(Debug)]
pub struct Scope<'a> {
bundles: &'a [Bundle],
/// (type, id) -> (bundle, resource index in its manifest).
permutations: HashMap<(u32, u64), (usize, usize)>,
bytecode: HashMap<&'a str, (usize, usize)>,
}
impl<'a> Scope<'a> {
/// Index `bundles` (in lookup order); where two hold the same id or name the earlier wins.
pub fn new(bundles: &'a [Bundle]) -> Self {
let (mut permutations, mut bytecode) = (HashMap::new(), HashMap::new());
for (b, bundle) in bundles.iter().enumerate() {
for (i, a) in bundle.manifest.res.iter().enumerate() {
match a.rtype {
permutation::RASTER
| permutation::COMPUTE
| permutation::SHARED_RASTER
| permutation::SHARED_COMPUTE
| binding::BINDING_SET
| binding::PASS_BINDING
| inputlayout::RTYPE
| lookup::STREAMABLE_TEXTURE_SET => {
permutations
.entry((a.rtype, permutation::resource_id(&a.rmeta)))
.or_insert((b, i));
}
bytecode::RTYPE => {
bytecode.entry(a.name.as_str()).or_insert((b, i));
}
_ => {}
}
}
}
Scope {
bundles,
permutations,
bytecode,
}
}
/// The bundles, in lookup order.
pub fn bundles(&self) -> &'a [Bundle] {
self.bundles
}
/// The entry of the resource of type `rtype` with `id` in the first bundle of the scope that holds one (a
/// permutation or a streamable texture set).
pub fn resource_by_id(&self, rtype: u32, id: u64) -> Option<Listed> {
let &(b, i) = self.permutations.get(&(rtype, id))?;
self.bundles.get(b)?.resource(i)
}
/// The bundle and entry of the resource of type `rtype` with `id`.
pub fn resource_with_home(&self, rtype: u32, id: u64) -> Option<(&'a Bundle, Listed)> {
let &(b, i) = self.permutations.get(&(rtype, id))?;
let bundle = self.bundles.get(b)?;
Some((bundle, bundle.resource(i)?))
}
/// The bundle and entry of the permutation with `id`, a raster one first.
pub fn permutation(&self, id: u64) -> Option<(&'a Bundle, Listed)> {
let &(b, i) = [permutation::RASTER, permutation::COMPUTE]
.iter()
.find_map(|t| self.permutations.get(&(*t, id)))?;
let bundle = self.bundles.get(b)?;
Some((bundle, bundle.resource(i)?))
}
/// The bundle and entry of the CompiledBytecode resource named `name`.
pub fn bytecode(&self, name: &str) -> Option<(&'a Bundle, Listed)> {
let &(b, i) = self.bytecode.get(name)?;
let bundle = self.bundles.get(b)?;
Some((bundle, bundle.resource(i)?))
}
/// The bundle `entry` was listed from, among these.
pub fn bundle_of(&self, entry: &Listed) -> Option<&'a Bundle> {
self.bundles
.iter()
.find(|b| b.toc_path == entry.toc && b.name == entry.bundle)
}
}