Skip to content

Commit

Permalink
feat(devtools): downloadFile global function
Browse files Browse the repository at this point in the history
fix: local server wasnt saving the time of the world
feat(advanced): add a way to specify local server options
  • Loading branch information
zardoy committed Sep 6, 2024
1 parent d6964b8 commit 8e330c0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Instead I recommend setting `options.debugLogNotFrequentPackets`. Also you can u
- `debugChangedOptions` - See what options are changed. Don't change options here.
- `localServer`/`server` - Only for singleplayer mode/host. Flying Squid server instance, see it's documentation for more.
- `localServer.overworld.storageProvider.regions` - See ALL LOADED region files with all raw data.
- `localServer.levelData.LevelName = 'test'; localServer.writeLevelDat()` - Change name of the world

- `nbt.simplify(someNbt)` - Simplifies nbt data, so it's easier to read.

Expand Down Expand Up @@ -141,6 +142,7 @@ Single player specific:
- `?singleplayer=1` - Create empty world on load. Nothing will be saved
- `?version=<version>` - Set the version for the singleplayer world (when used with `?singleplayer=1`)
- `?noSave=true` - Disable auto save on unload / disconnect / export whenever a world is loaded. Only manual save with `/save` command will work.
- `?serverSetting=<key>:<value>` - Set local server [options](https://github.com/zardoy/space-squid/tree/everything/src/modules.ts#L51). For example `?serverSetting=noInitialChunksSend:true` will disable initial chunks loading on the loading screen.
- `?map=<map_url>` - Load the map from ZIP. You can use any url, but it must be **CORS enabled**.
- `?mapDir=<index_file_url>` - Load the map from a file descriptor. It's recommended and the fastest way to load world but requires additional setup. The file must be in the following format:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"esbuild-plugin-polyfill-node": "^0.3.0",
"express": "^4.18.2",
"filesize": "^10.0.12",
"flying-squid": "npm:@zardoy/flying-squid@^0.0.38",
"flying-squid": "npm:@zardoy/flying-squid@^0.0.40",
"fs-extra": "^11.1.1",
"google-drive-browserfs": "github:zardoy/browserfs#google-drive",
"jszip": "^3.10.1",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/builtinCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const commands: Array<{
command: ['/save'],
async invoke () {
await saveServer(false)
writeText('Saved to browser memory')
}
}
]
Expand Down
23 changes: 11 additions & 12 deletions src/devtools.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// global variables useful for debugging

import fs from 'fs'
import { WorldRendererThree } from 'prismarine-viewer/viewer/lib/worldrendererThree'
import { getEntityCursor } from './worldInteractions'

// Object.defineProperty(window, 'cursorBlock', )

window.cursorBlockRel = (x = 0, y = 0, z = 0) => {
const newPos = bot.blockAtCursor(5)?.position.offset(x, y, z)
if (!newPos) return
Expand Down Expand Up @@ -44,14 +43,14 @@ window.inspectPacket = (packetName, full = false) => {
return returnobj
}

// for advanced debugging, use with watch expression

let stats_ = {}
window.addStatHit = (key) => {
stats_[key] ??= 0
stats_[key]++
window.downloadFile = async (path: string) => {
if (!path.startsWith('/') && localServer) path = `${localServer.options.worldFolder}/${path}`
const data = await fs.promises.readFile(path)
const blob = new Blob([data], { type: 'application/octet-stream' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = path.split('/').at(-1)!
a.click()
URL.revokeObjectURL(url)
}
setInterval(() => {
window.stats = stats_
stats_ = {}
}, 1000)
2 changes: 1 addition & 1 deletion src/flyingSquidUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const saveServer = async (autoSave = true) => {
if (!localServer || fsState.isReadonly) return
// todo
const worlds = [(localServer as any).overworld] as Array<import('prismarine-world').world.World>
await Promise.all([savePlayers(autoSave), ...worlds.map(async world => world.saveNow())])
await Promise.all([localServer.writeLevelDat(), savePlayers(autoSave), ...worlds.map(async world => world.saveNow())])
}
export const disconnect = async () => {
if (localServer) {
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ function hideCurrentScreens () {
}

const loadSingleplayer = (serverOverrides = {}, flattenedServerOverrides = {}) => {
void connect({ singleplayer: true, username: options.localUsername, serverOverrides, serverOverridesFlat: flattenedServerOverrides })
const serverSettingsQsRaw = new URLSearchParams(window.location.search).getAll('serverSetting')
const serverSettingsQs = serverSettingsQsRaw.map(x => x.split(':')).reduce<Record<string, string>>((acc, [key, value]) => {
acc[key] = JSON.parse(value)
return acc
}, {})
void connect({ singleplayer: true, username: options.localUsername, serverOverrides, serverOverridesFlat: { ...flattenedServerOverrides, ...serverSettingsQs } })
}
function listenGlobalEvents () {
window.addEventListener('connect', e => {
Expand Down
2 changes: 2 additions & 0 deletions src/topRightStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export const statsEnd = () => {
statsGl.end()
}

// for advanced debugging, use with watch expression

window.statsPerSec = {}
let statsPerSec = {}
window.addStatPerSec = (name) => {
Expand Down

0 comments on commit 8e330c0

Please sign in to comment.