@nkduy/mc-asset-loader 0.0.0-stage → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +373 -0
- package/README.md +453 -2
- package/dist/mc-asset-loader.min.js +11 -0
- package/index.d.ts +344 -0
- package/package.json +52 -4
- package/src/bedrock.js +54 -0
- package/src/cache.js +127 -0
- package/src/index.js +471 -0
- package/src/inflate.js +168 -0
- package/src/jar.js +267 -0
- package/src/local.js +62 -0
- package/src/manifest.js +442 -0
- package/src/objects.js +18 -0
- package/src/opfs-cache.js +153 -0
- package/src/source.js +68 -0
- package/src/store.js +69 -0
- package/src/util.js +105 -0
- package/src/version.js +160 -0
- package/src/zip.js +253 -0
- package/src/zlib-node.js +15 -0
package/src/manifest.js
ADDED
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import { isNode, hashFromUrl, define, memoMap, pool } from "./util.js"
|
|
2
|
+
|
|
3
|
+
export const MANIFEST_URL = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"
|
|
4
|
+
const BEDROCK_RELEASES = "https://api.github.com/repos/Mojang/bedrock-samples/releases"
|
|
5
|
+
export const LEGACY_ASSETS_BEFORE = Date.parse("2013-06-13T15:32:23+00:00")
|
|
6
|
+
const DEFAULT_TTL = 10 * 60 * 1000
|
|
7
|
+
const ASSET_INDEXES = "asset_indexes"
|
|
8
|
+
|
|
9
|
+
const MAIN_EXTRA = ["1.20.4", "1.20.6", "1.21.3", "1.21.4", "1.21.5", "1.21.8", "1.21.10", "1.21.11"]
|
|
10
|
+
|
|
11
|
+
function lineOf(id, bedrock) {
|
|
12
|
+
if (bedrock) {
|
|
13
|
+
const [major, minor, patch] = id.split(".")
|
|
14
|
+
return `${major}.${minor}.${Math.floor((parseInt(patch) || 0) / 10) * 10}`
|
|
15
|
+
}
|
|
16
|
+
const m = /^(\d+)\.(\d+)/.exec(id)
|
|
17
|
+
return m ? m[1] + "." + m[2] : id
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function bedrockVersion(release) {
|
|
21
|
+
const asset = release.assets?.find(a => a.name.endsWith("-full.zip"))
|
|
22
|
+
return {
|
|
23
|
+
id: release.tag_name.replace(/^v/, ""),
|
|
24
|
+
type: release.prerelease ? "snapshot" : "release",
|
|
25
|
+
releaseTime: release.published_at,
|
|
26
|
+
tag: release.tag_name,
|
|
27
|
+
zip: asset
|
|
28
|
+
? { url: asset.browser_download_url, size: asset.size }
|
|
29
|
+
: { url: `https://github.com/Mojang/bedrock-samples/archive/refs/tags/${release.tag_name}.zip`, size: null, archive: true }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function main(all) {
|
|
34
|
+
const bedrock = all.some(v => v.zip)
|
|
35
|
+
const releases = all.filter(v => v.type === "release")
|
|
36
|
+
const keep = new Set()
|
|
37
|
+
const lines = new Set()
|
|
38
|
+
for (const v of releases) {
|
|
39
|
+
const line = lineOf(v.id, bedrock)
|
|
40
|
+
if (!lines.has(line)) {
|
|
41
|
+
lines.add(line)
|
|
42
|
+
keep.add(v)
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!bedrock) for (const v of releases) if (MAIN_EXTRA.includes(v.id)) keep.add(v)
|
|
46
|
+
const snapshot = all.find(v => v.type === "snapshot")
|
|
47
|
+
if (snapshot && (!releases[0] || Date.parse(snapshot.releaseTime) > Date.parse(releases[0].releaseTime))) keep.add(snapshot)
|
|
48
|
+
return all.filter(v => keep.has(v))
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const VersionType = {
|
|
52
|
+
RELEASE: "release",
|
|
53
|
+
SNAPSHOT: "snapshot",
|
|
54
|
+
BETA: "old_beta",
|
|
55
|
+
ALPHA: "old_alpha",
|
|
56
|
+
MAIN: main,
|
|
57
|
+
MODERN: all => all.filter(v => !v.legacyLayout)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function applyFilter(all, filter) {
|
|
61
|
+
if (filter == null) return all.slice()
|
|
62
|
+
if (typeof filter === "string") return all.filter(v => v.type === filter)
|
|
63
|
+
if (typeof filter === "function") return filter(all)
|
|
64
|
+
if (Array.isArray(filter)) {
|
|
65
|
+
const keep = new Set()
|
|
66
|
+
for (const f of filter) for (const v of applyFilter(all, f)) keep.add(v)
|
|
67
|
+
return all.filter(v => keep.has(v))
|
|
68
|
+
}
|
|
69
|
+
throw new TypeError("Unsupported version filter")
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function ttlFromHeaders(headers) {
|
|
73
|
+
if (!headers) return DEFAULT_TTL
|
|
74
|
+
const control = headers.get("cache-control")
|
|
75
|
+
const m = control && /max-age=(\d+)/i.exec(control)
|
|
76
|
+
if (m) {
|
|
77
|
+
const age = parseInt(headers.get("age") ?? "0", 10) || 0
|
|
78
|
+
return Math.max(0, (Number(m[1]) - age) * 1000)
|
|
79
|
+
}
|
|
80
|
+
const expires = headers.get("expires")
|
|
81
|
+
if (expires) {
|
|
82
|
+
const until = Date.parse(expires)
|
|
83
|
+
const date = Date.parse(headers.get("date") ?? "")
|
|
84
|
+
if (!Number.isNaN(until)) return Math.max(0, until - (Number.isNaN(date) ? Date.now() : date))
|
|
85
|
+
}
|
|
86
|
+
return DEFAULT_TTL
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const versionKey = row => row.sha1 ?? (row.url ? hashFromUrl(row.url) : row.id)
|
|
90
|
+
|
|
91
|
+
const BOUND = {
|
|
92
|
+
search: "arg", file: "arg", read: "arg",
|
|
93
|
+
getTexture: "arg", getModel: "arg", getBlockstate: "arg", getItemDefinition: "arg", getSound: "arg", getLang: "arg", getStructure: "arg",
|
|
94
|
+
loadJar: "opts", loadObjects: "opts", export: "opts"
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const raws = new WeakMap()
|
|
98
|
+
const STATE = Symbol("state")
|
|
99
|
+
|
|
100
|
+
class ManifestVersion {
|
|
101
|
+
constructor(manifest, row) {
|
|
102
|
+
Object.assign(this, row)
|
|
103
|
+
this.legacyLayout = Date.parse(row.releaseTime) < LEGACY_ASSETS_BEFORE
|
|
104
|
+
this[STATE] = Object.freeze({ manifest })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
details() {
|
|
108
|
+
return this[STATE].manifest.details(this)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
list(a, b) {
|
|
112
|
+
const mc = this[STATE].manifest.mc
|
|
113
|
+
return mc._isFolderArg(a) ? mc.list(a, { ...b, version: this }) : mc.list({ ...a, version: this })
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
for (const [name, kind] of Object.entries(BOUND)) {
|
|
118
|
+
ManifestVersion.prototype[name] = kind === "arg"
|
|
119
|
+
? function (x, opts) { return this[STATE].manifest.mc[name](x, { ...opts, version: this }) }
|
|
120
|
+
: function (opts) { return this[STATE].manifest.mc[name]({ ...opts, version: this }) }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function sameEntries(a, b) {
|
|
124
|
+
if (a.entries.length !== b.entries.length) return false
|
|
125
|
+
for (let i = 0; i < a.entries.length; i++) {
|
|
126
|
+
if (a.entries[i] !== b.entries[i]) return false
|
|
127
|
+
}
|
|
128
|
+
return true
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export class Manifest {
|
|
132
|
+
constructor(mc, { manifest, manifestExpiry, onProgress, onUpdate } = {}) {
|
|
133
|
+
this.mc = mc
|
|
134
|
+
this._expiry = manifestExpiry
|
|
135
|
+
this._onProgress = onProgress
|
|
136
|
+
this._onUpdate = onUpdate
|
|
137
|
+
this._state = null
|
|
138
|
+
this._owned = false
|
|
139
|
+
this._expiresAt = 0
|
|
140
|
+
this._pending = null
|
|
141
|
+
this._persisted = null
|
|
142
|
+
this._details = new Map()
|
|
143
|
+
this._source = null
|
|
144
|
+
if (manifest != null) this._adopt(manifest)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
get _cacheKey() {
|
|
148
|
+
if (this.mc._type === "bedrock") return "bedrock_manifest"
|
|
149
|
+
if (this.mc._type === "assets") return "assets_manifest"
|
|
150
|
+
return "manifest"
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async _current() {
|
|
154
|
+
if (this._owned) return this._state
|
|
155
|
+
if (this._state && Date.now() < this._expiresAt) return this._state
|
|
156
|
+
let cached
|
|
157
|
+
if (!this._state) {
|
|
158
|
+
cached = await this.mc._store.get("meta", this._cacheKey)
|
|
159
|
+
if (!(cached?.json?.versions && typeof cached.time === "number")) cached = undefined
|
|
160
|
+
else {
|
|
161
|
+
const expiresAt = cached.time + this._effectiveTtl(cached.ttl)
|
|
162
|
+
if (Date.now() < expiresAt || !isNode) {
|
|
163
|
+
this._state = this._build(cached.json)
|
|
164
|
+
this._owned = false
|
|
165
|
+
this._expiresAt = expiresAt
|
|
166
|
+
if (Date.now() < expiresAt) return this._state
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (this._state && !isNode && this._onUpdate) {
|
|
171
|
+
if (!this._pending) {
|
|
172
|
+
const served = this._state
|
|
173
|
+
this._pending = this._fetch()
|
|
174
|
+
.then(state => { if (!sameEntries(served, state)) this._onUpdate(this) })
|
|
175
|
+
.catch(() => {})
|
|
176
|
+
.finally(() => { this._pending = null })
|
|
177
|
+
}
|
|
178
|
+
return this._state
|
|
179
|
+
}
|
|
180
|
+
if (!this._pending) {
|
|
181
|
+
this._pending = this._fetch().finally(() => { this._pending = null })
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
return await this._pending
|
|
185
|
+
} catch (e) {
|
|
186
|
+
if (this._state) return this._state
|
|
187
|
+
if (cached) {
|
|
188
|
+
this._state = this._build(cached.json)
|
|
189
|
+
this._owned = false
|
|
190
|
+
this._expiresAt = 0
|
|
191
|
+
return this._state
|
|
192
|
+
}
|
|
193
|
+
throw e
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
_effectiveTtl(headerTtl) {
|
|
198
|
+
return this._expiry === undefined ? headerTtl : (this._expiry == null ? Infinity : this._expiry)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async _fetch() {
|
|
202
|
+
const { json, headers } = this.mc._type === "bedrock" ? await this._fetchBedrock()
|
|
203
|
+
: this.mc._type === "assets" ? await this._fetchAssets()
|
|
204
|
+
: await this._fetchJava()
|
|
205
|
+
const state = this._build(json)
|
|
206
|
+
const headerTtl = ttlFromHeaders(headers)
|
|
207
|
+
this._state = state
|
|
208
|
+
this._owned = false
|
|
209
|
+
this._expiresAt = Date.now() + this._effectiveTtl(headerTtl)
|
|
210
|
+
this._persisted = this.mc._store.set("meta", this._cacheKey, { time: Date.now(), ttl: headerTtl, json })
|
|
211
|
+
return state
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async _fetchJava() {
|
|
215
|
+
const res = await this.mc._request(MANIFEST_URL)
|
|
216
|
+
return { json: await res.json(), headers: res.headers }
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async _fetchBedrock() {
|
|
220
|
+
const versions = []
|
|
221
|
+
let headers
|
|
222
|
+
let page = 1
|
|
223
|
+
while (true) {
|
|
224
|
+
const res = await this.mc._request(`${BEDROCK_RELEASES}?per_page=100&page=${page++}`)
|
|
225
|
+
const batch = await res.json()
|
|
226
|
+
for (const r of batch) versions.push(bedrockVersion(r))
|
|
227
|
+
headers = res.headers
|
|
228
|
+
if (batch.length < 100) break
|
|
229
|
+
}
|
|
230
|
+
return { json: { versions }, headers }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async _fetchAssets() {
|
|
234
|
+
const res = this._source ? null : await this.mc._request(MANIFEST_URL)
|
|
235
|
+
const json = this._source ?? await res.json()
|
|
236
|
+
const rows = json.versions
|
|
237
|
+
const store = this.mc._store
|
|
238
|
+
const known = new Array(rows.length)
|
|
239
|
+
const probes = new Map()
|
|
240
|
+
const mc = this.mc
|
|
241
|
+
const onProgress = this._onProgress
|
|
242
|
+
let done = 0
|
|
243
|
+
let total = 0
|
|
244
|
+
let last = -1
|
|
245
|
+
const step = () => {
|
|
246
|
+
const ratio = total ? ++done / total : 1
|
|
247
|
+
const percent = Math.round(ratio * 100)
|
|
248
|
+
if (percent === last && done < total) return
|
|
249
|
+
last = percent
|
|
250
|
+
onProgress?.(ratio)
|
|
251
|
+
}
|
|
252
|
+
const seen = await store.get("meta", ASSET_INDEXES) ?? {}
|
|
253
|
+
let added = 0
|
|
254
|
+
const flush = () => added ? store.set("meta", ASSET_INDEXES, seen).then(() => { added = 0 }) : null
|
|
255
|
+
function probe(i) {
|
|
256
|
+
return memoMap(probes, i, async () => {
|
|
257
|
+
const key = hashFromUrl(rows[i].url)
|
|
258
|
+
if (!(key in seen)) {
|
|
259
|
+
const d = await (await mc._request(rows[i].url)).json()
|
|
260
|
+
seen[key] = d.assetIndex ?? null
|
|
261
|
+
added++
|
|
262
|
+
}
|
|
263
|
+
return known[i] = seen[key]
|
|
264
|
+
})
|
|
265
|
+
}
|
|
266
|
+
// April window: april fools builds are the only versions with a unique single-use index
|
|
267
|
+
function april(row) {
|
|
268
|
+
const d = new Date(row.releaseTime)
|
|
269
|
+
const m = d.getUTCMonth()
|
|
270
|
+
return (m === 2 && d.getUTCDate() >= 25) || (m === 3 && d.getUTCDate() <= 7)
|
|
271
|
+
}
|
|
272
|
+
const anchors = []
|
|
273
|
+
for (let i = 0; i < rows.length; i++) {
|
|
274
|
+
if (i === 0 || i === rows.length - 1 || rows[i].type === "release" || april(rows[i])) anchors.push(i)
|
|
275
|
+
}
|
|
276
|
+
const gaps = []
|
|
277
|
+
const edges = new Set()
|
|
278
|
+
for (let a = 1; a < anchors.length; a++) {
|
|
279
|
+
const lo = anchors[a - 1] + 1
|
|
280
|
+
const hi = anchors[a] - 1
|
|
281
|
+
if (hi < lo) continue
|
|
282
|
+
gaps.push([lo, hi])
|
|
283
|
+
edges.add(lo).add(hi)
|
|
284
|
+
}
|
|
285
|
+
total = anchors.length + edges.size + gaps.length
|
|
286
|
+
last = 0
|
|
287
|
+
onProgress?.(0)
|
|
288
|
+
await pool(anchors, 32, async i => { await probe(i); step() })
|
|
289
|
+
flush()
|
|
290
|
+
await pool(Array.from(edges), 32, async i => { await probe(i); step() })
|
|
291
|
+
flush()
|
|
292
|
+
async function solve(lo, hi) {
|
|
293
|
+
if (hi - lo < 1) return
|
|
294
|
+
if (hi - lo - 1 <= 2) {
|
|
295
|
+
await Promise.all(Array.from({ length: hi - lo + 1 }, (_, k) => probe(lo + k)))
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
const [a, b] = await Promise.all([probe(lo), probe(hi)])
|
|
299
|
+
if (a?.id === b?.id) return
|
|
300
|
+
const mid = (lo + hi) >> 1
|
|
301
|
+
await probe(mid)
|
|
302
|
+
await Promise.all([solve(lo, mid), solve(mid, hi)])
|
|
303
|
+
}
|
|
304
|
+
await Promise.all(gaps.map(async ([lo, hi]) => { await solve(lo, hi); step() }))
|
|
305
|
+
await flush()
|
|
306
|
+
for (let i = 1; i < rows.length; i++) if (known[i] === undefined) known[i] = known[i - 1]
|
|
307
|
+
const byId = new Map()
|
|
308
|
+
for (let i = rows.length - 1; i >= 0; i--) {
|
|
309
|
+
const row = rows[i]
|
|
310
|
+
const index = known[i]
|
|
311
|
+
if (!index) continue
|
|
312
|
+
const cur = byId.get(index.id)
|
|
313
|
+
if (!cur) {
|
|
314
|
+
byId.set(index.id, {
|
|
315
|
+
id: index.id,
|
|
316
|
+
type: row.type === "release" ? "release" : "snapshot",
|
|
317
|
+
releaseTime: row.releaseTime,
|
|
318
|
+
sha1: index.sha1,
|
|
319
|
+
url: index.url,
|
|
320
|
+
size: index.size,
|
|
321
|
+
totalSize: index.totalSize,
|
|
322
|
+
first: row.id,
|
|
323
|
+
last: row.id
|
|
324
|
+
})
|
|
325
|
+
} else {
|
|
326
|
+
if (row.type === "release") cur.type = "release"
|
|
327
|
+
cur.last = row.id
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const versions = Array.from(byId.values()).sort((a, b) => Date.parse(b.releaseTime) - Date.parse(a.releaseTime))
|
|
331
|
+
return { json: { versions }, headers: res?.headers }
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
_adopt(json) {
|
|
335
|
+
if (this.mc._type === "bedrock") {
|
|
336
|
+
if (!Array.isArray(json) || json.some(r => typeof r?.tag_name !== "string")) throw new TypeError("Not a releases array")
|
|
337
|
+
this._state = this._build({ versions: json.map(bedrockVersion) })
|
|
338
|
+
this._owned = true
|
|
339
|
+
this._expiresAt = Infinity
|
|
340
|
+
return
|
|
341
|
+
}
|
|
342
|
+
if (!json || typeof json !== "object" || !Array.isArray(json.versions)) throw new TypeError("Not a version manifest")
|
|
343
|
+
const copy = JSON.parse(JSON.stringify(json))
|
|
344
|
+
if (this.mc._type === "assets") {
|
|
345
|
+
this._source = copy
|
|
346
|
+
return
|
|
347
|
+
}
|
|
348
|
+
this._state = this._build(copy)
|
|
349
|
+
this._owned = true
|
|
350
|
+
this._expiresAt = Infinity
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
_build(json) {
|
|
354
|
+
if (!json || !Array.isArray(json.versions)) throw new TypeError("Not a version manifest")
|
|
355
|
+
const previous = new Map()
|
|
356
|
+
if (this._state) for (const e of this._state.entries) previous.set(versionKey(e), e)
|
|
357
|
+
const entries = json.versions.map(row => {
|
|
358
|
+
const old = previous.get(versionKey(row))
|
|
359
|
+
if (old && raws.get(old) === JSON.stringify(row)) return old
|
|
360
|
+
return this._enrich(row)
|
|
361
|
+
})
|
|
362
|
+
const byId = new Map()
|
|
363
|
+
for (const e of entries) if (!byId.has(e.id)) byId.set(e.id, e)
|
|
364
|
+
return { entries, byId }
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
_enrich(row) {
|
|
368
|
+
const entry = new ManifestVersion(this, row)
|
|
369
|
+
raws.set(entry, JSON.stringify(row))
|
|
370
|
+
return entry
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async versions(filter) {
|
|
374
|
+
const { entries } = await this._current()
|
|
375
|
+
return applyFilter(entries, filter)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
async version(id) {
|
|
379
|
+
const state = await this._current()
|
|
380
|
+
if (id === "release" || id === "snapshot" || id === "newest") return this._latest(state)[id]
|
|
381
|
+
return state.byId.get(id) ?? null
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
async latest() {
|
|
385
|
+
return this._latest(await this._current())
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
_latest({ entries }) {
|
|
389
|
+
return {
|
|
390
|
+
release: entries.find(v => v.type === "release") ?? null,
|
|
391
|
+
snapshot: entries.find(v => v.type === "snapshot") ?? null,
|
|
392
|
+
newest: entries[0] ?? null
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
peek(version) {
|
|
397
|
+
const state = this._state
|
|
398
|
+
if (!state) return null
|
|
399
|
+
if (version === "release" || version === "snapshot" || version === "newest") return this._latest(state)[version]
|
|
400
|
+
const id = typeof version === "object" && version !== null ? version.id : version
|
|
401
|
+
return state.byId.get(id) ?? null
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async update(json) {
|
|
405
|
+
if (json !== undefined) {
|
|
406
|
+
this._adopt(json)
|
|
407
|
+
if (this.mc._type !== "assets") return
|
|
408
|
+
}
|
|
409
|
+
await this._fetch()
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
async resolve(version) {
|
|
413
|
+
if (version === "release" || version === "snapshot" || version === "newest") {
|
|
414
|
+
const entry = (await this.latest())[version]
|
|
415
|
+
if (!entry) throw new Error(`No ${version} version in the manifest`)
|
|
416
|
+
return entry
|
|
417
|
+
}
|
|
418
|
+
const id = typeof version === "object" && version !== null ? version.id : version
|
|
419
|
+
const entry = await this.version(id)
|
|
420
|
+
if (entry) return entry
|
|
421
|
+
if (typeof version === "object" && version !== null && version.url) return version
|
|
422
|
+
throw new Error(`Unknown version "${id}"`)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
async details(version) {
|
|
426
|
+
const entry = await this.resolve(version)
|
|
427
|
+
const key = versionKey(entry)
|
|
428
|
+
return memoMap(this._details, key, () => this._fetchDetails(entry, key))
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
async _fetchDetails(entry, key) {
|
|
432
|
+
const store = this.mc._store
|
|
433
|
+
const type = this.mc._type
|
|
434
|
+
const cacheKey = (type === "bedrock" ? "bedrock_details_" : type === "assets" ? "index_" : "details_") + key
|
|
435
|
+
const cached = await store.get("meta", cacheKey)
|
|
436
|
+
if (cached && typeof cached === "object") return cached
|
|
437
|
+
const res = await this.mc._request(type === "bedrock" ? `${BEDROCK_RELEASES}/tags/${entry.tag}` : entry.url)
|
|
438
|
+
const json = await res.json()
|
|
439
|
+
store.set("meta", cacheKey, json)
|
|
440
|
+
return json
|
|
441
|
+
}
|
|
442
|
+
}
|
package/src/objects.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const OBJECTS_URL = "https://resources.download.minecraft.net/"
|
|
2
|
+
|
|
3
|
+
export function rootIndex(index) {
|
|
4
|
+
const objects = index?.objects ?? {}
|
|
5
|
+
const keys = Object.keys(objects)
|
|
6
|
+
const namespaced = keys.some(k => k.startsWith("minecraft/"))
|
|
7
|
+
const root = namespaced ? "assets/" : "assets/minecraft/"
|
|
8
|
+
const map = new Map()
|
|
9
|
+
for (const key of keys) {
|
|
10
|
+
const { hash, size } = objects[key]
|
|
11
|
+
if (typeof hash !== "string") continue
|
|
12
|
+
const path = key === "pack.mcmeta" ? key : root + key
|
|
13
|
+
map.set(path, { hash, size })
|
|
14
|
+
}
|
|
15
|
+
return map
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const objectUrl = hash => OBJECTS_URL + hash.slice(0, 2) + "/" + hash
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { encoder, decoder } from "./util.js"
|
|
2
|
+
|
|
3
|
+
const STORES = ["meta", "blobs"]
|
|
4
|
+
const SAFE = /[^A-Za-z0-9_.%-]/g
|
|
5
|
+
const encodeKey = key => encodeURIComponent(key).replace(SAFE, c => "%" + c.charCodeAt(0).toString(16).toUpperCase().padStart(2, "0"))
|
|
6
|
+
const decodeKey = name => decodeURIComponent(name)
|
|
7
|
+
|
|
8
|
+
export class OpfsCache {
|
|
9
|
+
constructor(dir, { maxSize = 1_000_000_000, key } = {}) {
|
|
10
|
+
this.name = dir || "mc-asset-loader"
|
|
11
|
+
this.key = key
|
|
12
|
+
this.maxSize = maxSize == null ? Infinity : maxSize
|
|
13
|
+
this._root = null
|
|
14
|
+
this._index = null
|
|
15
|
+
this._scan = null
|
|
16
|
+
this._touched = new Map()
|
|
17
|
+
this._dirs = new Map()
|
|
18
|
+
this._evicting = null
|
|
19
|
+
this._rootDir().catch(() => {})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
_rootDir() {
|
|
23
|
+
return this._root ??= navigator.storage.getDirectory()
|
|
24
|
+
.then(r => r.getDirectoryHandle(this.name, { create: true }))
|
|
25
|
+
.then(r => this.key ? r.getDirectoryHandle(this.key, { create: true }) : r)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_dir(store) {
|
|
29
|
+
this._rootDir()
|
|
30
|
+
let dir = this._dirs.get(store)
|
|
31
|
+
if (!dir) {
|
|
32
|
+
dir = this._root.then(r => r.getDirectoryHandle(store, { create: true }))
|
|
33
|
+
this._dirs.set(store, dir)
|
|
34
|
+
dir.catch(() => this._dirs.delete(store))
|
|
35
|
+
}
|
|
36
|
+
return dir
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async _ready() {
|
|
40
|
+
if (this._index) return this._index
|
|
41
|
+
if (!this._scan) this._scan = this._load().then(index => this._index = index)
|
|
42
|
+
return this._scan
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async _load() {
|
|
46
|
+
const index = new Map()
|
|
47
|
+
for (const store of STORES) {
|
|
48
|
+
const dir = await this._dir(store)
|
|
49
|
+
for await (const [name, handle] of dir.entries()) {
|
|
50
|
+
if (handle.kind !== "file") continue
|
|
51
|
+
const file = await handle.getFile()
|
|
52
|
+
const id = store + "/" + name
|
|
53
|
+
index.set(id, { size: file.size, mtime: this._touched.get(id) ?? file.lastModified })
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return index
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_touch(id, size) {
|
|
60
|
+
const now = Date.now()
|
|
61
|
+
this._touched.set(id, now)
|
|
62
|
+
if (this._index) {
|
|
63
|
+
const rec = this._index.get(id)
|
|
64
|
+
if (rec) rec.mtime = now
|
|
65
|
+
else this._index.set(id, { size, mtime: now })
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async get(store, key) {
|
|
70
|
+
const name = encodeKey(key)
|
|
71
|
+
let file
|
|
72
|
+
try {
|
|
73
|
+
file = await (await (await this._dir(store)).getFileHandle(name)).getFile()
|
|
74
|
+
} catch {
|
|
75
|
+
return undefined
|
|
76
|
+
}
|
|
77
|
+
const bytes = new Uint8Array(await file.arrayBuffer())
|
|
78
|
+
this._touch(store + "/" + name, bytes.length)
|
|
79
|
+
return store === "meta" ? JSON.parse(decoder.decode(bytes)) : bytes
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async set(store, key, value) {
|
|
83
|
+
const name = encodeKey(key)
|
|
84
|
+
const bytes = store === "meta" ? encoder.encode(JSON.stringify(value)) : value
|
|
85
|
+
const handle = await (await this._dir(store)).getFileHandle(name, { create: true })
|
|
86
|
+
const writable = await handle.createWritable()
|
|
87
|
+
try {
|
|
88
|
+
await writable.write(bytes)
|
|
89
|
+
} finally {
|
|
90
|
+
await writable.close()
|
|
91
|
+
}
|
|
92
|
+
const id = store + "/" + name
|
|
93
|
+
this._touched.set(id, Date.now())
|
|
94
|
+
if (this._index) this._index.set(id, { size: bytes.length, mtime: Date.now() })
|
|
95
|
+
this._evictLater()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
_evictLater() {
|
|
99
|
+
if (!isFinite(this.maxSize) || this._evicting) return
|
|
100
|
+
this._evicting = new Promise(resolve => setTimeout(resolve, 0))
|
|
101
|
+
.then(() => this._ready())
|
|
102
|
+
.then(() => this._evict())
|
|
103
|
+
.catch(() => {})
|
|
104
|
+
.finally(() => { this._evicting = null })
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async delete(store, key) {
|
|
108
|
+
const name = encodeKey(key)
|
|
109
|
+
this._index?.delete(store + "/" + name)
|
|
110
|
+
this._touched.delete(store + "/" + name)
|
|
111
|
+
await (await this._dir(store)).removeEntry(name).catch(() => {})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async list() {
|
|
115
|
+
const index = await this._ready()
|
|
116
|
+
return Array.from(index.entries()).map(([id, rec]) => {
|
|
117
|
+
const slash = id.indexOf("/")
|
|
118
|
+
return { key: id.slice(0, slash + 1) + decodeKey(id.slice(slash + 1)), size: rec.size }
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async clear() {
|
|
123
|
+
this._index = null
|
|
124
|
+
this._scan = null
|
|
125
|
+
this._touched.clear()
|
|
126
|
+
this._dirs.clear()
|
|
127
|
+
const root = await this._rootDir()
|
|
128
|
+
for (const store of STORES) await root.removeEntry(store, { recursive: true }).catch(() => {})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async setMaxSize(maxSize) {
|
|
132
|
+
this.maxSize = maxSize == null ? Infinity : maxSize
|
|
133
|
+
if (!isFinite(this.maxSize)) return
|
|
134
|
+
await this._ready()
|
|
135
|
+
await this._evict()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async _evict() {
|
|
139
|
+
if (!isFinite(this.maxSize)) return
|
|
140
|
+
const index = this._index
|
|
141
|
+
let total = 0
|
|
142
|
+
for (const rec of index.values()) total += rec.size
|
|
143
|
+
if (total <= this.maxSize) return
|
|
144
|
+
const order = Array.from(index.entries()).sort((a, b) => a[1].mtime - b[1].mtime)
|
|
145
|
+
for (const [id, rec] of order) {
|
|
146
|
+
if (total <= this.maxSize) break
|
|
147
|
+
index.delete(id)
|
|
148
|
+
total -= rec.size
|
|
149
|
+
const [store, name] = id.split("/")
|
|
150
|
+
await (await this._dir(store)).removeEntry(name).catch(() => {})
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
package/src/source.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { decodeEntry, decodeEntrySync, inflateRaw } from "./zip.js"
|
|
2
|
+
import { memo, memoMap } from "./util.js"
|
|
3
|
+
|
|
4
|
+
export class ZipSource {
|
|
5
|
+
constructor({ url, size, request, store }) {
|
|
6
|
+
this.url = url
|
|
7
|
+
this.size = size
|
|
8
|
+
this.request = request
|
|
9
|
+
this.store = store
|
|
10
|
+
this._listing = null
|
|
11
|
+
this._list = null
|
|
12
|
+
this._buffer = null
|
|
13
|
+
this._held = null
|
|
14
|
+
this._persisted = null
|
|
15
|
+
this._persistedMeta = null
|
|
16
|
+
this._progress = null
|
|
17
|
+
this._reads = new Map()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
buffer() {
|
|
21
|
+
return memo(this, "_buffer", async () => this._held = await this._loadBuffer())
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
listing() {
|
|
25
|
+
return memo(this, "_listing", async () => this._list = await this._loadListing())
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async load(onProgress) {
|
|
29
|
+
this._progress = onProgress
|
|
30
|
+
try {
|
|
31
|
+
const [, held] = await Promise.all([this.listing(), this.buffer()])
|
|
32
|
+
if (onProgress) {
|
|
33
|
+
const total = Array.isArray(held) ? held.reduce((n, c) => n + c.bytes.length, 0) : held.length
|
|
34
|
+
onProgress(total, total)
|
|
35
|
+
}
|
|
36
|
+
} finally {
|
|
37
|
+
this._progress = null
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async settled() {
|
|
42
|
+
await this._persisted
|
|
43
|
+
await this._list?.sorting
|
|
44
|
+
await this._persistedMeta
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async extract(path) {
|
|
48
|
+
const hit = await this.raw(path)
|
|
49
|
+
return hit ? decodeEntry(hit.data, hit.entry) : null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
read(path) {
|
|
53
|
+
const hit = this._reads.get(path)
|
|
54
|
+
if (hit) return hit
|
|
55
|
+
if (!this._list || !this._held) return memoMap(this._reads, path, () => this.extract(path))
|
|
56
|
+
let p
|
|
57
|
+
try {
|
|
58
|
+
const raw = this.rawSync(path)
|
|
59
|
+
const bytes = raw && decodeEntrySync(raw.data, raw.entry)
|
|
60
|
+
p = !raw ? Promise.resolve(null) : bytes ? Promise.resolve(bytes) : inflateRaw(raw.data)
|
|
61
|
+
} catch (e) {
|
|
62
|
+
p = Promise.reject(e)
|
|
63
|
+
}
|
|
64
|
+
this._reads.set(path, p)
|
|
65
|
+
p.catch(() => { if (this._reads.get(path) === p) this._reads.delete(path) })
|
|
66
|
+
return p
|
|
67
|
+
}
|
|
68
|
+
}
|