Skip to content

Commit

Permalink
Remove padding for non-file writing
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jul 24, 2023
1 parent 2921255 commit aa041a5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public CommentEntry(Player initiator, BlockPos placedAt, int messageType, String
this.imageUrl = imageUrl;
}

public CommentEntry(ResourceLocation level, FriendlyByteBuf src) {
fileOffset = src.readerIndex();
public CommentEntry(ResourceLocation level, FriendlyByteBuf src, boolean fromFile) {
if (fromFile) fileOffset = src.readerIndex();
id = src.readLong();
timestamp = src.readLong();
this.level = level;
Expand All @@ -59,15 +59,15 @@ public CommentEntry(ResourceLocation level, FriendlyByteBuf src) {
message = src.readUtf();
deleted = src.readBoolean();
imageUrl = src.readUtf();
src.skipBytes(16 - (src.readerIndex() % 16));
if (fromFile) src.skipBytes(16 - (src.readerIndex() % 16));
}

private static UUID uuidFromByteArray(byte[] bytes) {
ByteBuffer bb = ByteBuffer.wrap(bytes);
return new UUID(bb.getLong(), bb.getLong());
}

public void writeBuffer(FriendlyByteBuf dst) {
public void writeBuffer(FriendlyByteBuf dst, boolean toFile) {
dst.writeLong(id);
dst.writeLong(timestamp);
dst.writeBlockPos(location);
Expand All @@ -77,12 +77,12 @@ public void writeBuffer(FriendlyByteBuf dst) {
dst.writeUtf(message);
dst.writeBoolean(deleted);
dst.writeUtf(imageUrl);
dst.writeZero(16 - (dst.writerIndex() % 16));
if (toFile) dst.writeZero(16 - (dst.writerIndex() % 16));
}

public void writeFileStream(FileOutputStream oStream) throws IOException {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(256));
writeBuffer(buf);
writeBuffer(buf, true);
fileOffset = oStream.getChannel().position();
oStream.write(buf.array(), 0, buf.writerIndex());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void load() throws IOException {
for (Path file : files.toList()) {
long region = Long.parseUnsignedLong(file.getFileName().toString().substring(1, 9), 16);
byte[] fileContent = Files.readAllBytes(file);
loadRegion(dimension, region, fileContent);
loadRegion(dimension, region, fileContent, true);
}
}
}
Expand All @@ -50,12 +50,12 @@ public void load() throws IOException {
}
}

public void loadRegion(ResourceLocation dimension, long region, byte[] data) {
public void loadRegion(ResourceLocation dimension, long region, byte[] data, boolean fromFile) {
synchronized (this) {
List<CommentEntry> regionEntries = new ArrayList<>();
FriendlyByteBuf src = new FriendlyByteBuf(Unpooled.wrappedBuffer(data));
while (src.readerIndex() < data.length) {
CommentEntry entry = new CommentEntry(dimension, src);
CommentEntry entry = new CommentEntry(dimension, src, fromFile);
regionEntries.add(entry);
playerIndex.computeIfAbsent(entry.initiator, ignored -> new ArrayList<>())
.add(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public static void send(ServerPlayer target, ResourceLocation level, Map<ChunkPo
for (Map.Entry<ChunkPos, List<CommentEntry>> entry : data.entrySet()) {
buffer.writeChunkPos(entry.getKey());
buffer.writeInt(entry.getValue().size());
buffer.writeZero(16 - (buffer.writerIndex() % 16));
for (CommentEntry comment : entry.getValue()) {
comment.writeBuffer(buffer);
comment.writeBuffer(buffer, false);
}
}
ServerPlatform.sendPacketToPlayer(target, IDENTIFIER, buffer);
Expand All @@ -45,10 +44,9 @@ public static void handle(FriendlyByteBuf buffer) {
for (int i = 0; i < regionSize; i++) {
ChunkPos region = buffer.readChunkPos();
int commentSize = buffer.readInt();
buffer.skipBytes(16 - (buffer.readerIndex() % 16));
ArrayList<CommentEntry> comments = new ArrayList<>(commentSize);
for (int j = 0; j < commentSize; j++) {
CommentEntry comment = new CommentEntry(level, buffer);
CommentEntry comment = new CommentEntry(level, buffer, false);
comments.add(comment);
}
regions.put(region.toLong(), comments);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public static class ClientLogics {
public static void send(CommentEntry comment) {
FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer());
buffer.writeResourceLocation(comment.level);
comment.writeBuffer(buffer);
comment.writeBuffer(buffer, false);
ClientPlatform.sendPacketToServer(IDENTIFIER, buffer);
}
}

public static void handle(MinecraftServer server, ServerPlayer initiator, FriendlyByteBuf buffer) {
ResourceLocation level = buffer.readResourceLocation();
CommentEntry comment = new CommentEntry(level, buffer);
CommentEntry comment = new CommentEntry(level, buffer, false);
if (!comment.initiator.equals(Util.NIL_UUID)
&& !comment.initiator.equals(initiator.getGameProfile().getId())) {
return;
Expand Down

0 comments on commit aa041a5

Please sign in to comment.