better download ui & fix qs conflict

This commit is contained in:
Vitaly 2023-09-04 12:16:23 +03:00
commit 29e68512f4
3 changed files with 33 additions and 17 deletions

View file

@ -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 () => {

View file

@ -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

View file

@ -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`
<div class="dirt-bg"></div>
<div class="title">${this.status}${this.hasError ? '' : this._loadingDots}<p class="potential-problem">${this.hasError ? guessProblem(this.status) : ''}</p></div>
<div class="title">${this.status}${this.hasError && !this.hideDots ? '' : this._loadingDots}<p class="potential-problem">${this.hasError ? guessProblem(this.status) : ''}</p></div>
${this.hasError
? html`<div class="error-buttons"><pmui-button .hidden=${!this.maybeRecoverable} pmui-width="200px" pmui-label="Back" @pmui-click=${() => {