Skip to content

Commit eb39a90

Browse files
authored
fix: toJson / fromJson didn't use maxBitsPerBlock (#238)
* fix: JSON is losing maxBitsPerBlock * add test * forgot commit
1 parent 0631db2 commit eb39a90

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist/
33
benchmarks/results/
44
package-lock.json
55
.vscode
6+
.DS_Store

src/pc/common/PaletteContainer.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class IndirectPaletteContainer {
113113
type: 'indirect',
114114
palette: this.palette,
115115
maxBits: this.maxBits,
116+
maxBitsPerBlock: this.maxBitsPerBlock,
116117
data: this.data.toJson()
117118
})
118119
}
@@ -122,6 +123,7 @@ class IndirectPaletteContainer {
122123
return new IndirectPaletteContainer({
123124
palette: parsed.palette,
124125
maxBits: parsed.maxBits,
126+
maxBitsPerBlock: parsed.maxBitsPerBlock,
125127
data: BitArray.fromJson(parsed.data)
126128
})
127129
}
@@ -171,7 +173,8 @@ class SingleValueContainer {
171173
value: this.value,
172174
bitsPerValue: this.bitsPerValue,
173175
capacity: this.capacity,
174-
maxBits: this.maxBits
176+
maxBits: this.maxBits,
177+
maxBitsPerBlock: this.maxBitsPerBlock
175178
})
176179
}
177180

@@ -181,7 +184,8 @@ class SingleValueContainer {
181184
value: parsed.value,
182185
bitsPerValue: parsed.bitsPerValue,
183186
capacity: parsed.capacity,
184-
maxBits: parsed.maxBits
187+
maxBits: parsed.maxBits,
188+
maxBitsPerBlock: parsed.maxBitsPerBlock
185189
})
186190
}
187191
}
@@ -196,14 +200,16 @@ function containerFromJson (j) {
196200
return new IndirectPaletteContainer({
197201
palette: parsed.palette,
198202
maxBits: parsed.maxBits,
199-
data: BitArray.fromJson(parsed.data)
203+
data: BitArray.fromJson(parsed.data),
204+
maxBitsPerBlock: parsed.maxBitsPerBlock
200205
})
201206
} else if (parsed.type === 'single') {
202207
return new SingleValueContainer({
203208
value: parsed.value,
204209
bitsPerValue: parsed.bitsPerValue,
205210
capacity: parsed.capacity,
206-
maxBits: parsed.maxBits
211+
maxBits: parsed.maxBits,
212+
maxBitsPerBlock: parsed.maxBitsPerBlock
207213
})
208214
}
209215
return undefined

test/ChunkColumn.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ for (const version of allVersions) {
145145
assert.strictEqual(4, cc2.getSkyLight(new Vec3(0, 4, 0)))
146146
assert.strictEqual(cc.toJson(), cc2.toJson())
147147
})
148+
it('to/from JSON work and keep maxBitsPerBlock', () => {
149+
const cc = new ChunkColumn()
150+
const cc2 = ChunkColumn.fromJson(cc.toJson())
151+
152+
for (let i = 0; i < 4096; i++) {
153+
cc2.setBlockStateId(new Vec3(0, 0, 0), i) // Decides to switch to Direct pallete at some point
154+
const blockStateId = cc2.getBlockStateId(new Vec3(0, 0, 0))
155+
if (blockStateId !== i) throw new Error(`Expected ${i} but got ${blockStateId}`)
156+
}
157+
})
148158

149159
//
150160

0 commit comments

Comments
 (0)