Skip to content

Commit 5d24c77

Browse files
committed
🚧 Add .mcb file compression.
1 parent d567389 commit 5d24c77

File tree

7 files changed

+625
-3
lines changed

7 files changed

+625
-3
lines changed

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"exporters/",
1616
"tools/",
1717
"svelte.config.ts",
18-
"src/util/bufferGeometryUtils.ts"
18+
"vitest.config.ts",
19+
"src/util/bufferGeometryUtils.ts",
20+
"tests/**/*"
1921
],
2022
"overrides": [
2123
{

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,17 @@
7878
"build:scripts": "esbuild --bundle --platform=node --outfile=dist/build.cjs --packages=external ./tools/esbuild.ts",
7979
"dev": "yarn build:scripts && node ./dist/build.cjs --mode=dev",
8080
"prod": "node ./tools/cleanupDist.cjs && yarn build:scripts && node ./dist/build.cjs",
81-
"format": "prettier --write ."
81+
"format": "prettier --write .",
82+
"test": "yarn build:scripts && vitest run",
83+
"coverage": "yarn build:scripts && vitest run --coverage"
8284
},
8385
"devDependencies": {
8486
"@novacbn/svelte-codejar": "^0.1.2",
8587
"@types/download": "^8.0.5",
8688
"@types/eslint": "^8.21.1",
8789
"@types/js-yaml": "^4.0.5",
8890
"@types/node": "^17.0.21",
91+
"@types/websocket": "^1.0.10",
8992
"@typescript-eslint/eslint-plugin": "^5.54.0",
9093
"@typescript-eslint/parser": "^5.54.0",
9194
"blockbench-types": "https://github.com/SnaveSutit/blockbench-types.git",
@@ -102,7 +105,8 @@
102105
"svelte-awesome-color-picker": "^3.0.0-beta.7",
103106
"svelte-preprocess": "^5.0.1",
104107
"svelte-preprocess-esbuild": "^3.0.1",
105-
"typescript": "^4.5.5"
108+
"typescript": "^4.5.5",
109+
"vitest": "^2.1.8"
106110
},
107111
"dependencies": {
108112
"deepslate": "^0.19.2",

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { exportProject } from './systems/exporter'
4242
import { openBlueprintLoadingDialog } from './interface/blueprintLoadingPopup'
4343
import { openInstallPopup } from './interface/installedPopup'
4444
import { cleanupExportedFiles } from './systems/cleaner'
45+
import mcbFiles from './systems/datapackCompiler/mcbFiles'
4546

4647
// @ts-ignore
4748
globalThis.AnimatedJava = {
@@ -83,6 +84,7 @@ globalThis.AnimatedJava = {
8384
Undo.finishEdit('Remove Cubes Associated With Texture')
8485
},
8586
cleanupExportedFiles,
87+
mcbFiles,
8688
},
8789
}
8890

tools/esbuild.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ImportGlobPlugin from 'esbuild-plugin-import-glob'
1818
import packagerPlugin from './plugins/packagerPlugin'
1919
import inlineWorkerPlugin from './plugins/workerPlugin'
2020
import assetOverridePlugin from './plugins/assetOverridePlugin'
21+
import mcbCompressionPlugin from './plugins/mcbCompressionPlugin'
2122
import * as path from 'path'
2223
const PACKAGE = JSON.parse(fs.readFileSync('./package.json', 'utf-8'))
2324

@@ -216,6 +217,7 @@ const devConfig: esbuild.BuildOptions = {
216217
packagerPlugin(),
217218
inlineWorkerPlugin(devWorkerConfig),
218219
assetOverridePlugin(),
220+
mcbCompressionPlugin(),
219221
DEPENDENCY_QUARKS,
220222
],
221223
format: 'iife',
@@ -243,6 +245,7 @@ const prodConfig: esbuild.BuildOptions = {
243245
packagerPlugin(),
244246
inlineWorkerPlugin({}),
245247
assetOverridePlugin(),
248+
mcbCompressionPlugin(),
246249
DEPENDENCY_QUARKS,
247250
],
248251
keepNames: true,

tools/plugins/mcbCompressionPlugin.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Plugin } from 'esbuild'
2+
import * as fs from 'fs/promises'
3+
import * as pathjs from 'path'
4+
import * as fflate from 'fflate'
5+
6+
function zip(data: fflate.AsyncZippable): Promise<Uint8Array> {
7+
return new Promise((resolve, reject) => {
8+
fflate.zip(data, { level: 9 }, (err, data) => {
9+
if (err) reject(err)
10+
else resolve(data)
11+
})
12+
})
13+
}
14+
15+
export default function plugin(): Plugin {
16+
return {
17+
name: 'mcbCompressionPlugin',
18+
setup(build) {
19+
const mcbFiles = new Map<string, fflate.AsyncZippableFile>()
20+
21+
build.onLoad({ filter: /\.mcb$/ }, async ({ path }) => {
22+
const localPath = pathjs.relative(process.cwd(), path).replace(/\\/g, '/')
23+
const data = await fs.readFile(path)
24+
mcbFiles.set(localPath, data)
25+
26+
return {
27+
contents: `
28+
import getZipFile from '__MCB_ZIP_DATA'
29+
export default getZipFile('${localPath}')
30+
`,
31+
loader: 'js',
32+
}
33+
})
34+
35+
build.onResolve({ filter: /^__MCB_ZIP_DATA$/ }, ({ path }) => {
36+
return {
37+
path,
38+
namespace: 'mcbZipData',
39+
}
40+
})
41+
42+
build.onLoad({ filter: /.*/, namespace: 'mcbZipData' }, async ({ path }) => {
43+
const zipped = await zip(Object.fromEntries(mcbFiles.entries()))
44+
const data = Buffer.from(zipped).toString('base64')
45+
return {
46+
contents: `
47+
import * as fflate from 'fflate'
48+
const unzipped = fflate.unzipSync(Uint8Array.from(atob('${data}'), c => c.charCodeAt(0)))
49+
export default function getFile(path) {
50+
return Buffer.from(unzipped[path]).toString('utf-8')
51+
}
52+
`,
53+
resolveDir: process.cwd(),
54+
loader: 'js',
55+
}
56+
})
57+
},
58+
}
59+
}

vitest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
dir: 'tests',
6+
},
7+
})

0 commit comments

Comments
 (0)