Skip to content

Commit

Permalink
fix: toJson / fromJson didn't use maxBitsPerBlock (#238)
Browse files Browse the repository at this point in the history
* fix: JSON is losing maxBitsPerBlock

* add test

* forgot commit
  • Loading branch information
zardoy authored Apr 28, 2024
1 parent 0631db2 commit eb39a90
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist/
benchmarks/results/
package-lock.json
.vscode
.DS_Store
14 changes: 10 additions & 4 deletions src/pc/common/PaletteContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class IndirectPaletteContainer {
type: 'indirect',
palette: this.palette,
maxBits: this.maxBits,
maxBitsPerBlock: this.maxBitsPerBlock,
data: this.data.toJson()
})
}
Expand All @@ -122,6 +123,7 @@ class IndirectPaletteContainer {
return new IndirectPaletteContainer({
palette: parsed.palette,
maxBits: parsed.maxBits,
maxBitsPerBlock: parsed.maxBitsPerBlock,
data: BitArray.fromJson(parsed.data)
})
}
Expand Down Expand Up @@ -171,7 +173,8 @@ class SingleValueContainer {
value: this.value,
bitsPerValue: this.bitsPerValue,
capacity: this.capacity,
maxBits: this.maxBits
maxBits: this.maxBits,
maxBitsPerBlock: this.maxBitsPerBlock
})
}

Expand All @@ -181,7 +184,8 @@ class SingleValueContainer {
value: parsed.value,
bitsPerValue: parsed.bitsPerValue,
capacity: parsed.capacity,
maxBits: parsed.maxBits
maxBits: parsed.maxBits,
maxBitsPerBlock: parsed.maxBitsPerBlock
})
}
}
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/ChunkColumn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
})

//

Expand Down

0 comments on commit eb39a90

Please sign in to comment.