Skip to content

Commit

Permalink
feat: All versions now are available offline! (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy authored Aug 31, 2024
1 parent 0dc2612 commit eb0bc02
Show file tree
Hide file tree
Showing 20 changed files with 1,781 additions and 145 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const compareRenderedFlatWorld = () => {
}

const testWorldLoad = () => {
return cy.document().then({ timeout: 25_000 }, doc => {
return cy.document().then({ timeout: 35_000 }, doc => {
return new Cypress.Promise(resolve => {
doc.addEventListener('cypress-world-ready', resolve)
})
Expand Down
1 change: 1 addition & 0 deletions experiments/decode.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="decode.ts" type="module"></script>
26 changes: 26 additions & 0 deletions experiments/decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Include the pako library
import pako from 'pako';
import compressedJsRaw from './compressed.js?raw'

function decompressFromBase64(input) {
// Decode the Base64 string
const binaryString = atob(input);
const len = binaryString.length;
const bytes = new Uint8Array(len);

// Convert the binary string to a byte array
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}

// Decompress the byte array
const decompressedData = pako.inflate(bytes, { to: 'string' });

return decompressedData;
}

// Use the function
console.time('decompress');
const decompressedData = decompressFromBase64(compressedJsRaw);
console.timeEnd('decompress')
console.log(decompressedData)
1,011 changes: 1,007 additions & 4 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions prismarine-viewer/esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ import * as esbuild from 'esbuild'
import { polyfillNode } from 'esbuild-plugin-polyfill-node'
import path, { dirname, join } from 'path'
import { fileURLToPath } from 'url'
import childProcess from 'child_process'
import supportedVersions from '../src/supportedVersions.mjs'

const dev = process.argv.includes('-w')

const __dirname = path.dirname(fileURLToPath(new URL(import.meta.url)))

const mcDataPath = join(__dirname, '../dist/mc-data')
const mcDataPath = join(__dirname, '../generated/minecraft-data-optimized.json')
if (!fs.existsSync(mcDataPath)) {
// shouldn't it be in the viewer instead?
await import('../scripts/prepareData.mjs')
childProcess.execSync('tsx ../scripts/makeOptimizedMcData.mjs', { stdio: 'inherit', cwd: __dirname })
}

fs.copyFileSync(join(__dirname, 'playground.html'), join(__dirname, 'public/index.html'))
fsExtra.copySync(mcDataPath, join(__dirname, 'public/mc-data'))
const availableVersions = fs.readdirSync(mcDataPath).map(ver => ver.replace('.js', ''))

/** @type {import('esbuild').BuildOptions} */
const buildOptions = {
Expand All @@ -37,7 +36,7 @@ const buildOptions = {
],
keepNames: true,
banner: {
js: `globalThis.global = globalThis;globalThis.includedVersions = ${JSON.stringify(availableVersions)};`,
js: `globalThis.global = globalThis;globalThis.includedVersions = ${JSON.stringify(supportedVersions)};`,
},
alias: {
events: 'events',
Expand All @@ -63,13 +62,14 @@ const buildOptions = {
}, () => {
const defaultVersionsObj = {}
return {
contents: `window.mcData ??= ${JSON.stringify(defaultVersionsObj)};module.exports = { pc: window.mcData }`,
loader: 'js',
contents: fs.readFileSync(join(__dirname, '../src/shims/minecraftData.ts'), 'utf8'),
loader: 'ts',
resolveDir: join(__dirname, '../src/shims'),
}
})
build.onEnd((e) => {
if (e.errors.length) return
// fs.writeFileSync(join(__dirname, 'dist/metafile.json'), JSON.stringify(e.metafile), 'utf8')
fs.writeFileSync(join(__dirname, './public/metafile.json'), JSON.stringify(e.metafile), 'utf8')
})
}
},
Expand Down
29 changes: 15 additions & 14 deletions prismarine-viewer/examples/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { loadScript } from '../viewer/lib/utils'
import { TWEEN_DURATION } from '../viewer/lib/entities'
import { EntityMesh } from '../viewer/lib/entity/EntityMesh'
import { WorldDataEmitter, Viewer } from '../viewer'
import '../../src/getCollisionShapes'
import { toMajorVersion } from '../../src/utils'

window.THREE = THREE
Expand Down Expand Up @@ -65,21 +66,21 @@ async function main () {
let continuousRender = false

const { version } = params
await window._LOAD_MC_DATA()
// temporary solution until web worker is here, cache data for faster reloads
const globalMcData = window['mcData']
if (!globalMcData['version']) {
const major = toMajorVersion(version)
const sessionKey = `mcData-${major}`
if (sessionStorage[sessionKey]) {
Object.assign(globalMcData, JSON.parse(sessionStorage[sessionKey]))
} else {
if (sessionStorage.length > 1) sessionStorage.clear()
await loadScript(`./mc-data/${major}.js`)
try {
sessionStorage[sessionKey] = JSON.stringify(Object.fromEntries(Object.entries(globalMcData).filter(([ver]) => ver.startsWith(major))))
} catch { }
}
}
// const globalMcData = window['mcData']
// if (!globalMcData['version']) {
// const major = toMajorVersion(version)
// const sessionKey = `mcData-${major}`
// if (sessionStorage[sessionKey]) {
// Object.assign(globalMcData, JSON.parse(sessionStorage[sessionKey]))
// } else {
// if (sessionStorage.length > 1) sessionStorage.clear()
// try {
// sessionStorage[sessionKey] = JSON.stringify(Object.fromEntries(Object.entries(globalMcData).filter(([ver]) => ver.startsWith(major))))
// } catch { }
// }
// }

const mcData: IndexedData = require('minecraft-data')(version)
window['loadedData'] = mcData
Expand Down
3 changes: 3 additions & 0 deletions prismarine-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@
"optionalDependencies": {
"canvas": "^2.11.2",
"node-canvas-webgl": "^0.3.0"
},
"devDependencies": {
"live-server": "^1.2.2"
}
}
10 changes: 7 additions & 3 deletions prismarine-viewer/viewer/lib/worldrendererCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { EventEmitter } from 'events'
import { Vec3 } from 'vec3'
import * as THREE from 'three'
import mcDataRaw from 'minecraft-data/data.js' // handled correctly in esbuild plugin
import mcDataRaw from 'minecraft-data/data.js' // note: using alias
import blocksAtlases from 'mc-assets/dist/blocksAtlases.json'
import blocksAtlasLatest from 'mc-assets/dist/blocksAtlasLatest.png'
import blocksAtlasLegacy from 'mc-assets/dist/blocksAtlasLegacy.png'
Expand Down Expand Up @@ -223,8 +223,12 @@ export abstract class WorldRendererCommon<WorkerSend = any, WorkerReceive = any>

sendMesherMcData () {
const allMcData = mcDataRaw.pc[this.version] ?? mcDataRaw.pc[toMajorVersion(this.version)]
const mcData = Object.fromEntries(Object.entries(allMcData).filter(([key]) => dynamicMcDataFiles.includes(key)))
mcData.version = JSON.parse(JSON.stringify(mcData.version))
const mcData = {
version: JSON.parse(JSON.stringify(allMcData.version))
}
for (const key of dynamicMcDataFiles) {
mcData[key] = allMcData[key]
}

for (const worker of this.workers) {
worker.postMessage({ type: 'mcData', mcData, config: this.mesherConfig })
Expand Down
8 changes: 4 additions & 4 deletions rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ export default defineConfig({
const prep = async () => {
console.time('total-prep')
fs.mkdirSync('./generated', { recursive: true })
if (!fs.existsSync('./generated/minecraft-data-data.js')) {
childProcess.execSync('tsx ./scripts/genShims.ts', { stdio: 'inherit' })
if (!fs.existsSync('./generated/minecraft-data-optimized.json') || require('./generated/minecraft-data-optimized.json').versionKey !== require('minecraft-data/package.json').version) {
childProcess.execSync('tsx ./scripts/makeOptimizedMcData.mjs', { stdio: 'inherit' })
}
childProcess.execSync('tsx ./scripts/genShims.ts', { stdio: 'inherit' })
if (!fs.existsSync('./generated/latestBlockCollisionsShapes.json')) {
childProcess.execSync('tsx ./scripts/optimizeBlockCollisions.ts', { stdio: 'inherit' })
}
Expand All @@ -117,7 +118,6 @@ export default defineConfig({
configJson.defaultProxy = ':8080'
}
fs.writeFileSync('./dist/config.json', JSON.stringify(configJson), 'utf8')
childProcess.execSync('node ./scripts/prepareData.mjs', { stdio: 'inherit' })
// childProcess.execSync('./scripts/prepareSounds.mjs', { stdio: 'inherit' })
// childProcess.execSync('tsx ./scripts/genMcDataTypes.ts', { stdio: 'inherit' })
// childProcess.execSync('tsx ./scripts/genPixelartTypes.ts', { stdio: 'inherit' })
Expand Down Expand Up @@ -164,7 +164,7 @@ export default defineConfig({
// throw new Error(`${resource.request} was requested by ${resource.contextInfo.issuer}`)
}
if (absolute.endsWith('/minecraft-data/data.js')) {
resource.request = path.join(__dirname, './generated/minecraft-data-data.js')
resource.request = path.join(__dirname, './src/shims/minecraftData.ts')
}
}))
addRules([
Expand Down
1 change: 0 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ exports.getSwAdditionalEntries = () => {
// need to be careful with this
const filesToCachePatterns = [
'index.html',
`mc-data/${defaultLocalServerOptions.versionMajor}.js`,
'background/**',
// todo-low copy from assets
'*.mp3',
Expand Down
17 changes: 0 additions & 17 deletions scripts/genShims.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import fs from 'fs'
import MinecraftData from 'minecraft-data'
import MCProtocol from 'minecraft-protocol'
import { appReplacableResources } from '../src/resourcesSource'

const { supportedVersions, defaultVersion } = MCProtocol

// gen generated/minecraft-data-data.js

const data = MinecraftData(defaultVersion)
const defaultVersionObj = {
[defaultVersion]: {
version: data.version,
protocol: data.protocol,
}
}

const mcDataContents = `window.mcData ??= ${JSON.stringify(defaultVersionObj)};module.exports = { pc: window.mcData }`

fs.mkdirSync('./generated', { recursive: true })
fs.writeFileSync('./generated/minecraft-data-data.js', mcDataContents, 'utf8')

// app resources

Expand Down
Loading

0 comments on commit eb0bc02

Please sign in to comment.