From eb39a905761a36f733a456110e6b49d655bf5c16 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Mon, 29 Apr 2024 00:02:20 +0300 Subject: [PATCH] fix: toJson / fromJson didn't use maxBitsPerBlock (#238) * fix: JSON is losing maxBitsPerBlock * add test * forgot commit --- .gitignore | 1 + src/pc/common/PaletteContainer.js | 14 ++++++++++---- test/ChunkColumn.test.js | 10 ++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 115aa8c2..a7d5e77e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ dist/ benchmarks/results/ package-lock.json .vscode +.DS_Store diff --git a/src/pc/common/PaletteContainer.js b/src/pc/common/PaletteContainer.js index a2b80919..f5de8b8a 100644 --- a/src/pc/common/PaletteContainer.js +++ b/src/pc/common/PaletteContainer.js @@ -113,6 +113,7 @@ class IndirectPaletteContainer { type: 'indirect', palette: this.palette, maxBits: this.maxBits, + maxBitsPerBlock: this.maxBitsPerBlock, data: this.data.toJson() }) } @@ -122,6 +123,7 @@ class IndirectPaletteContainer { return new IndirectPaletteContainer({ palette: parsed.palette, maxBits: parsed.maxBits, + maxBitsPerBlock: parsed.maxBitsPerBlock, data: BitArray.fromJson(parsed.data) }) } @@ -171,7 +173,8 @@ class SingleValueContainer { value: this.value, bitsPerValue: this.bitsPerValue, capacity: this.capacity, - maxBits: this.maxBits + maxBits: this.maxBits, + maxBitsPerBlock: this.maxBitsPerBlock }) } @@ -181,7 +184,8 @@ class SingleValueContainer { value: parsed.value, bitsPerValue: parsed.bitsPerValue, capacity: parsed.capacity, - maxBits: parsed.maxBits + maxBits: parsed.maxBits, + maxBitsPerBlock: parsed.maxBitsPerBlock }) } } @@ -196,14 +200,16 @@ function containerFromJson (j) { return new IndirectPaletteContainer({ palette: parsed.palette, maxBits: parsed.maxBits, - data: BitArray.fromJson(parsed.data) + data: BitArray.fromJson(parsed.data), + maxBitsPerBlock: parsed.maxBitsPerBlock }) } else if (parsed.type === 'single') { return new SingleValueContainer({ value: parsed.value, bitsPerValue: parsed.bitsPerValue, capacity: parsed.capacity, - maxBits: parsed.maxBits + maxBits: parsed.maxBits, + maxBitsPerBlock: parsed.maxBitsPerBlock }) } return undefined diff --git a/test/ChunkColumn.test.js b/test/ChunkColumn.test.js index fc23b776..d4ba089b 100644 --- a/test/ChunkColumn.test.js +++ b/test/ChunkColumn.test.js @@ -145,6 +145,16 @@ for (const version of allVersions) { assert.strictEqual(4, cc2.getSkyLight(new Vec3(0, 4, 0))) assert.strictEqual(cc.toJson(), cc2.toJson()) }) + it('to/from JSON work and keep maxBitsPerBlock', () => { + const cc = new ChunkColumn() + const cc2 = ChunkColumn.fromJson(cc.toJson()) + + for (let i = 0; i < 4096; i++) { + cc2.setBlockStateId(new Vec3(0, 0, 0), i) // Decides to switch to Direct pallete at some point + const blockStateId = cc2.getBlockStateId(new Vec3(0, 0, 0)) + if (blockStateId !== i) throw new Error(`Expected ${i} but got ${blockStateId}`) + } + }) //