Skip to content

Commit

Permalink
allow to load worlf from ?map= qs url
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Sep 2, 2023
1 parent a652af9 commit dd43438
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/browserfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const openWorldDirectory = async (/** @type {FileSystemDirectoryHandle?}
loadFolder()
}

export const openWorldZip = async (/** @type {File} */file) => {
export const openWorldZip = async (/** @type {File | ArrayBuffer} */file, name = file['name']) => {
await new Promise(async resolve => {
browserfs.configure({
// todo
Expand All @@ -161,8 +161,8 @@ export const openWorldZip = async (/** @type {File} */file) => {
"/world": {
fs: "ZipFS",
options: {
zipData: Buffer.from(await file.arrayBuffer()),
name: file.name
zipData: Buffer.from(file instanceof File ? (await file.arrayBuffer()) : file),
name
}
}
},
Expand Down
36 changes: 36 additions & 0 deletions src/downloadAndOpenWorld.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { openWorldZip } from './browserfs'
import { setLoadingScreenStatus } from './utils'
import { filesize } from 'filesize'

window.addEventListener('load', async (e) => {
const qs = new URLSearchParams(window.location.search)
const mapUrl = qs.get('map')
if (!mapUrl) return

const menu = document.getElementById('play-screen')
menu.style = 'display: none;'
const name = mapUrl.slice(mapUrl.lastIndexOf('/') + 1).slice(-25)
setLoadingScreenStatus(`Downloading world ${name}...`)

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
// }

// setLoadingScreenStatus(`Downloading world ${name}: ${filesize(value.length)} / ${filesize(contentLength)}MB...`)
// }
await openWorldZip(await response.arrayBuffer())
})

export default async () => {
}
18 changes: 4 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ require('./reactUi.jsx')
require('./botControls')
require('./dragndrop')
require('./browserfs')
require('./eruda')
require('./downloadAndOpenWorld')

const net = require('net')
const Stats = require('stats.js')
Expand All @@ -56,7 +58,7 @@ const Cursor = require('./cursor').default
global.THREE = require('three')
const { initVR } = require('./vr')
const { activeModalStack, showModal, hideModal, hideCurrentModal, activeModalStacks, replaceActiveModalStack, isGameActive, miscUiState, gameAdditionalState } = require('./globalState')
const { pointerLock, goFullscreen, toNumber, isCypress } = require('./utils')
const { pointerLock, goFullscreen, toNumber, isCypress, loadScript, toMajorVersion, setLoadingScreenStatus } = require('./utils')
const { notification } = require('./menus/notification')
const { removePanorama, addPanoramaCubeMap, initPanoramaOptions } = require('./panorama')
const { startLocalServer, unsupportedLocalServerFeatures } = require('./createLocalServer')
Expand Down Expand Up @@ -174,17 +176,6 @@ const hud = document.getElementById('hud')
const optionsScrn = document.getElementById('options-screen')
const pauseMenu = document.getElementById('pause-screen')

function setLoadingScreenStatus (status, isError = false) {
// todo update in component instead
showModal(loadingScreen)
if (loadingScreen.hasError) {
miscUiState.gameLoaded = false
return
}
loadingScreen.hasError = isError
loadingScreen.status = status
}

let mouseMovePostHandle = (e) => { }
let lastMouseCall
function onMouseMove (e) {
Expand Down Expand Up @@ -366,7 +357,7 @@ async function connect (connectOptions) {
})
let singlePlayerServer
try {
Object.assign(serverOptions, _.defaultsDeep({}, connectOptions.serverOverrides, options.localServerOptions, serverOptions))
Object.assign(serverOptions, _.defaultsDeep({}, connectOptions.serverOverrides ?? {}, options.localServerOptions, serverOptions))
let version = connectOptions.botVersion ?? serverOptions.version
if (version) {
setLoadingScreenStatus(`Downloading data for ${version}`)
Expand All @@ -376,7 +367,6 @@ async function connect (connectOptions) {
if (singeplayer) {
window.serverDataChannel ??= {}
window.worldLoaded = false
Object.assign(serverOptions, _.defaultsDeep({}, connectOptions.serverOverrides, options.localServerOptions, serverOptions))
singlePlayerServer = window.singlePlayerServer = startLocalServer()
// todo need just to call quit if started
// loadingScreen.maybeRecoverable = false
Expand Down
6 changes: 4 additions & 2 deletions src/loadFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const fsState = proxy({

const PROPOSE_BACKUP = true

// todo rename to loadWorld
export const loadFolder = async (root = '/world') => {
// todo do it in singleplayer as well
for (const key in forceCachedDataPaths) {
Expand Down Expand Up @@ -47,7 +48,8 @@ export const loadFolder = async (root = '/world') => {
const parsedRaw = await parseNbt(Buffer.from(levelDatContent))
const levelDat: import('./mcTypes').LevelDat = nbt.simplify(parsedRaw).Data

version = levelDat.Version?.Name
const qs = new URLSearchParams(window.location.search)
version = levelDat.Version?.Name ?? qs.get('version')
if (!version) {
const newVersion = 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
Expand All @@ -69,7 +71,7 @@ export const loadFolder = async (root = '/world') => {
if (levelDat.generatorName) {
isFlat = levelDat.generatorName === 'flat'
}
if (!isFlat) {
if (!isFlat && levelDat.generatorName !== 'default' && levelDat.generatorName !== 'customized') {
warnings.push(`Generator ${levelDat.generatorName} may not be supported yet`)
}

Expand Down

0 comments on commit dd43438

Please sign in to comment.