diff --git a/src/downloadAndOpenWorld.ts b/src/downloadAndOpenWorld.ts index e14e4deb..61d7fbc7 100644 --- a/src/downloadAndOpenWorld.ts +++ b/src/downloadAndOpenWorld.ts @@ -15,21 +15,36 @@ window.addEventListener('load', async (e) => { const response = await fetch(mapUrl) const contentLength = +response.headers.get('Content-Length') setLoadingScreenStatus(`Downloading world ${name}: have to download ${filesize(contentLength)}...`) - // const reader = response.body!.getReader() - // let doneValue - // while (true) { - // // done is true for the last chunk - // // value is Uint8Array of the chunk bytes - // const { done, value } = await reader.read() - // if (done) { - // doneValue = value - // break - // } + let downloadedBytes = 0 + const buffer = await new Response( + new ReadableStream({ + async start(controller) { + const reader = response.body.getReader() - // setLoadingScreenStatus(`Downloading world ${name}: ${filesize(value.length)} / ${filesize(contentLength)}MB...`) - // } - await openWorldZip(await response.arrayBuffer()) + while (true) { + const { done, value } = await reader.read() + + if (done) { + controller.close() + break + } + + downloadedBytes += value.byteLength + + // Calculate download progress as a percentage + const progress = (downloadedBytes / contentLength) * 100 + + // Update your progress bar or display the progress value as needed + setLoadingScreenStatus(`Download Progress: ${Math.floor(progress)}% (${filesize(downloadedBytes, { round: 2, })} / ${filesize(contentLength, { round: 2, })}))`, false, true) + + // Pass the received data to the controller + controller.enqueue(value) + } + }, + }) + ).arrayBuffer() + await openWorldZip(buffer) }) export default async () => { diff --git a/src/loadSave.ts b/src/loadSave.ts index 5ed8b967..49ebd05d 100644 --- a/src/loadSave.ts +++ b/src/loadSave.ts @@ -50,7 +50,7 @@ export const loadSave = async (root = '/world') => { const levelDat: import('./mcTypes').LevelDat = nbt.simplify(parsedRaw).Data const qs = new URLSearchParams(window.location.search) - version = levelDat.Version?.Name ?? qs.get('version') + version = levelDat.Version?.Name ?? qs.get('mapVersion') if (!version) { const newVersion = disablePrompts ? '1.8.8' : prompt(`In 1.8 and before world save doesn\'t contain version info, please enter version you want to use to load the world.\nSupported versions ${supportedVersions.join(', ')}`, '1.8.8') if (!newVersion) return diff --git a/src/menus/loading_or_error_screen.js b/src/menus/loading_or_error_screen.js index 23aa2cd9..ab23be0b 100644 --- a/src/menus/loading_or_error_screen.js +++ b/src/menus/loading_or_error_screen.js @@ -40,6 +40,7 @@ class LoadingErrorScreen extends LitElement { constructor () { super() + this.hideDots = false this.hasError = false this.maybeRecoverable = true this.status = 'Waiting for JS load' @@ -73,7 +74,7 @@ class LoadingErrorScreen extends LitElement { return html`
-${this.hasError ? guessProblem(this.status) : ''}
${this.hasError ? guessProblem(this.status) : ''}