Skip to content

Commit 2b72e14

Browse files
committed
Change some AntiXrayProcessor comments
1 parent 1f3622e commit 2b72e14

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

common/src/main/java/dev/booky/betterview/common/antixray/AntiXrayProcessor.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,19 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
135135
obfuscatedPalette = BitSet.valueOf(EMPTY_LONG_ARRAY);
136136
obfuscatedPalette.set(0);
137137
presetPalette = new int[presetCount];
138-
// search for the palette value in our presets
138+
// search for the palette value in our presets to determine
139+
// whether the value may also be a preset block
139140
int presetIndex = Arrays.binarySearch(presets, value);
140141
if (presetIndex < 0) {
141-
// construct new palette
142+
// construct new palette, value not in presets
142143
newPalette = new int[1 + presetCount];
143144
System.arraycopy(presets, 0, newPalette, 1, presetCount);
144145
// specify indices of preset blockstate in the palette
145146
for (int i = 0; i < presetCount; i++) {
146147
presetPalette[i] = i + 1;
147148
}
148149
} else {
149-
// construct new palette
150+
// construct new palette, value is in presets
150151
newPalette = new int[presetCount];
151152
// copy the preset blockstate ids around the value into the new palette array
152153
System.arraycopy(presets, 0,
@@ -177,18 +178,22 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
177178
case 5, 6, 7, 8: {
178179
int paletteSize = VarIntUtil.readVarInt(buf);
179180
int[] palette = new int[paletteSize];
181+
// the count of the preset states not included in the existing palette,
182+
// to figure out whether we need to resize the palette or not
180183
int extraPaletteSize = presetCount;
181184
for (int i = 0; i < paletteSize; i++) {
182185
int value = VarIntUtil.readVarInt(buf);
183186
palette[i] = value;
184187
// check if this blockstate needs to be obfuscated
185188
if (this.obfuscatedStates.get(value)
186-
// don't obfuscate if this is the only replacement state
189+
// don't obfuscate if this value is the only replacement state anyway
187190
&& (presetCount != 1 || presets[0] != value)) {
191+
// lazy-load obfuscated palette bitset
188192
if (obfuscatedPalette == null) {
189193
obfuscatedPalette = BitSet.valueOf(EMPTY_LONG_ARRAY);
190194
obfuscatedPalette.set(paletteSize); // expand
191195
}
196+
// mark current palette entry for obfuscation
192197
obfuscatedPalette.set(i);
193198
}
194199
// check if this blockstate is present in our presets array
@@ -198,7 +203,7 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
198203
// save index of palette entry
199204
if (presetPalette == null) {
200205
presetPalette = new int[presetCount];
201-
if (paletteSize != 1) { // don't fill if this is only one slot anyway
206+
if (paletteSize != 1) { // don't fill array if there is only one preset anyway
202207
Arrays.fill(presetPalette, -1);
203208
}
204209
}
@@ -209,31 +214,33 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
209214
return; // nothing to obfuscate, cancel processing
210215
}
211216
// check if we need to modify the palette; if extraPaletteSize
212-
// is zero, all presets are already present in the existing palette and
217+
// is zero, all presets are already present in the existing palette, and
213218
// we don't need to do anything
214219
if (extraPaletteSize > 0) {
215220
newPalette = new int[paletteSize + extraPaletteSize];
216221
// copy in original palette, don't modify original indices
217222
System.arraycopy(palette, 0, newPalette, 0, paletteSize);
218-
// check whether any preset blockstates are present in the existing palette
223+
// check whether any of the preset blockstates are present in the existing palette
219224
if (presetPalette != null) {
220-
// some presets present in existing palette, copy over
221-
// remaining presets
225+
// some presets present in existing palette,
226+
// copy over remaining presets
222227
for (int i = 0, j = paletteSize; i < presetCount; i++) {
223228
if (presetPalette[i] == -1) {
224229
newPalette[j] = presets[i]; // push into new palette
225230
presetPalette[i] = j++; // save index in new palette
226231
}
227232
}
228233
} else {
229-
// no presets present, simply copy the preset blockstates
234+
// no presets present at all, just append our preset blockstates into the new palette...
230235
System.arraycopy(presets, 0, newPalette, paletteSize, presetCount);
236+
// ... and calculate indices of preset blockstates
231237
presetPalette = new int[presetCount];
232238
for (int i = 0; i < presetCount; i++) {
233239
presetPalette[i] = i + paletteSize;
234240
}
235241
}
236-
// update bit sizes, but respect that linear palettes always have at least 4 bits
242+
// update bits-per-entry of new palette, but don't allow shrinking the bits-per-entry;
243+
// the linear palette has a hard-coded bits-per-entry of 4
237244
int predictedBits = MathUtil.ceilLog2(paletteSize + extraPaletteSize);
238245
newPaletteBits = Math.max(predictedBits, newPaletteBits);
239246
}
@@ -263,7 +270,7 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
263270
entryMask = 0;
264271
valuesPerWord = 0; // dummy value
265272
if (storageLength) {
266-
// verify storage length if available in buffer
273+
// verify storage length in buffer
267274
int bufWordCount = VarIntUtil.readVarInt(buf);
268275
assert bufWordCount == 0;
269276
}
@@ -275,7 +282,7 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
275282
valuesPerWord = (char) (Long.SIZE / paletteStorageBits);
276283
int wordCount = (STORAGE_SIZE_3D + valuesPerWord - 1) / valuesPerWord;
277284
if (storageLength) {
278-
// verify storage length if available in buffer
285+
// verify storage length in buffer
279286
int bufWordCount = VarIntUtil.readVarInt(buf);
280287
assert bufWordCount == wordCount;
281288
}
@@ -297,7 +304,7 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
297304
newValuesPerWord = valuesPerWord;
298305
newStorage = storage;
299306
} else {
300-
// create new storage array
307+
// create new empty storage array
301308
newValuesPerWord = (char) (Long.SIZE / newPaletteBits);
302309
int newWordCount = (STORAGE_SIZE_3D + newValuesPerWord - 1) / newValuesPerWord;
303310
newStorage = new long[newWordCount];
@@ -339,7 +346,8 @@ public synchronized void process(ByteBuf buf, int sectionY, boolean storageLengt
339346
}
340347
// replace value with value from strategy, after passing through palette
341348
newValue = presetPalette[strategy.get()];
342-
// check if we need to write, or if we need to resize anyway
349+
// check if we need to write here; if we need to resize, our new value will get written
350+
// anyway; if the new value is the same as the old value, we can just skip handling
343351
if (!resize && newValue != value) {
344352
storage[wordIndex] = word
345353
// remove bits from word at position of value

0 commit comments

Comments
 (0)