Skip to content

Commit d1bdb76

Browse files
committed
S21/S26 packet static buffer thread safety
1 parent b872160 commit d1bdb76

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* ChunkAPI
3+
*
4+
* Copyright (C) 2023-2025 FalsePattern, The MEGA Team, LegacyModdingMC contributors
5+
* All Rights Reserved
6+
*
7+
* The above copyright notice and this permission notice shall be included
8+
* in all copies or substantial portions of the Software.
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Lesser General Public License as published by
12+
* the Free Software Foundation, only version 3 of the License.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Lesser General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Lesser General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*/
22+
23+
package com.falsepattern.chunk.internal.mixin.helpers;
24+
25+
import java.util.concurrent.locks.ReentrantLock;
26+
27+
public class LockHelper {
28+
public static final ReentrantLock bufferLockS21PacketChunkData = new ReentrantLock();
29+
public static final ReentrantLock bufferLockS26PacketMapChunkBulk = new ReentrantLock();
30+
}

src/main/java/com/falsepattern/chunk/internal/mixin/mixins/common/vanilla/S21PacketChunkDataMixin.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.falsepattern.chunk.internal.mixin.mixins.common.vanilla;
2424

2525
import com.falsepattern.chunk.internal.DataRegistryImpl;
26+
import com.falsepattern.chunk.internal.mixin.helpers.LockHelper;
2627
import lombok.val;
2728
import org.spongepowered.asm.mixin.Mixin;
2829
import org.spongepowered.asm.mixin.Overwrite;
@@ -90,10 +91,6 @@ public static S21PacketChunkData.Extracted func_149269_a(Chunk chunk, boolean fo
9091
ExtendedBlockStorage[] subChunks = chunk.getBlockStorageArray();
9192
S21PacketChunkData.Extracted extracted = new S21PacketChunkData.Extracted();
9293

93-
if (buffer.length < DataRegistryImpl.maxPacketSize()) {
94-
buffer = new byte[DataRegistryImpl.maxPacketSize()];
95-
}
96-
9794
if (forceUpdate) {
9895
chunk.sendUpdates = true;
9996
}
@@ -104,10 +101,21 @@ public static S21PacketChunkData.Extracted func_149269_a(Chunk chunk, boolean fo
104101
}
105102
}
106103

107-
int length = DataRegistryImpl.writeToBuffer(chunk, extracted.field_150280_b, forceUpdate, buffer);
104+
while (!LockHelper.bufferLockS21PacketChunkData.tryLock()) {
105+
Thread.yield();
106+
}
107+
try {
108+
if (buffer.length < DataRegistryImpl.maxPacketSize()) {
109+
buffer = new byte[DataRegistryImpl.maxPacketSize()];
110+
}
111+
112+
int length = DataRegistryImpl.writeToBuffer(chunk, extracted.field_150280_b, forceUpdate, buffer);
108113

109-
extracted.field_150282_a = new byte[length];
110-
System.arraycopy(buffer, 0, extracted.field_150282_a, 0, length);
114+
extracted.field_150282_a = new byte[length];
115+
System.arraycopy(buffer, 0, extracted.field_150282_a, 0, length);
116+
} finally {
117+
LockHelper.bufferLockS21PacketChunkData.unlock();
118+
}
111119
return extracted;
112120
}
113121

src/main/java/com/falsepattern/chunk/internal/mixin/mixins/common/vanilla/S26PacketMapChunkBulkMixin.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package com.falsepattern.chunk.internal.mixin.mixins.common.vanilla;
2424

25+
import com.falsepattern.chunk.internal.mixin.helpers.LockHelper;
2526
import lombok.val;
2627
import org.spongepowered.asm.mixin.Mixin;
2728
import org.spongepowered.asm.mixin.Overwrite;
@@ -90,21 +91,29 @@ public void readPacketData(PacketBuffer data) throws IOException {
9091
subChunkMSBMasks = new int[chunkCount];
9192
datas = new byte[chunkCount][];
9293

93-
if (inflaterBuffer.length < deflatedSize) {
94-
inflaterBuffer = new byte[deflatedSize];
94+
while (!LockHelper.bufferLockS26PacketMapChunkBulk.tryLock()) {
95+
Thread.yield();
9596
}
96-
97-
data.readBytes(inflaterBuffer, 0, deflatedSize);
98-
byte[] buf = new byte[S21PacketChunkData.func_149275_c() * chunkCount];
99-
Inflater inflater = new Inflater();
100-
inflater.setInput(inflaterBuffer, 0, deflatedSize);
101-
97+
byte[] buf;
10298
try {
103-
inflater.inflate(buf);
104-
} catch (DataFormatException dataformatexception) {
105-
throw new IOException("Bad compressed data format");
99+
if (inflaterBuffer.length < deflatedSize) {
100+
inflaterBuffer = new byte[deflatedSize];
101+
}
102+
103+
data.readBytes(inflaterBuffer, 0, deflatedSize);
104+
buf = new byte[S21PacketChunkData.func_149275_c() * chunkCount];
105+
Inflater inflater = new Inflater();
106+
inflater.setInput(inflaterBuffer, 0, deflatedSize);
107+
108+
try {
109+
inflater.inflate(buf);
110+
} catch (DataFormatException dataformatexception) {
111+
throw new IOException("Bad compressed data format");
112+
} finally {
113+
inflater.end();
114+
}
106115
} finally {
107-
inflater.end();
116+
LockHelper.bufferLockS26PacketMapChunkBulk.unlock();
108117
}
109118

110119
int pos = 0;

0 commit comments

Comments
 (0)