1 |
|
2 |
|
3 |
|
4 |
|
5 | export class ResourceManager {
|
6 | constructor(list = []) {
|
7 | this.list = list;
|
8 | this.cache = {};
|
9 | }
|
10 | get allResourcePacks() { return this.list.map((l) => l.info); }
|
11 | |
12 |
|
13 |
|
14 | async addResourcePack(resourcePack) {
|
15 | let info;
|
16 | try {
|
17 | info = await resourcePack.info();
|
18 | }
|
19 | catch (_a) {
|
20 | info = { pack_format: -1, description: "" };
|
21 | }
|
22 | const domains = await resourcePack.domains();
|
23 | const wrapper = { info, source: resourcePack, domains };
|
24 | this.list.push(wrapper);
|
25 | }
|
26 | |
27 |
|
28 |
|
29 | clearCache() { this.cache = {}; }
|
30 | |
31 |
|
32 |
|
33 | clearAll() {
|
34 | this.cache = {};
|
35 | this.list.splice(0, this.list.length);
|
36 | }
|
37 | |
38 |
|
39 |
|
40 | swap(first, second) {
|
41 | if (first >= this.list.length || first < 0 || second >= this.list.length || second < 0) {
|
42 | throw new Error("Illegal index");
|
43 | }
|
44 | const fir = this.list[first];
|
45 | this.list[first] = this.list[second];
|
46 | this.list[second] = fir;
|
47 | this.clearCache();
|
48 | }
|
49 | |
50 |
|
51 |
|
52 | invalidate(location) {
|
53 | delete this.cache[`${location.domain}:${location.path}`];
|
54 | }
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 | async load(location, urlOnly = false) {
|
61 | const cached = this.cache[`${location.domain}:${location.path}`];
|
62 | if (cached) {
|
63 | return cached;
|
64 | }
|
65 | for (const src of this.list) {
|
66 | const loaded = await src.source.load(location, urlOnly);
|
67 | if (!loaded) {
|
68 | continue;
|
69 | }
|
70 | this.putCache(loaded);
|
71 | return loaded;
|
72 | }
|
73 | return undefined;
|
74 | }
|
75 | putCache(res) {
|
76 | this.cache[`${res.location.domain}:${res.location.path}`] = res;
|
77 | }
|
78 | }
|
79 | export * from "./model-loader";
|