diff --git a/core/src/io/anuke/mindustry/entities/Player.java b/core/src/io/anuke/mindustry/entities/Player.java index 73a4c46b70..6d336b6fe6 100644 --- a/core/src/io/anuke/mindustry/entities/Player.java +++ b/core/src/io/anuke/mindustry/entities/Player.java @@ -16,6 +16,7 @@ import io.anuke.mindustry.game.Team; import io.anuke.mindustry.gen.Call; import io.anuke.mindustry.graphics.Palette; import io.anuke.mindustry.graphics.Trail; +import io.anuke.mindustry.io.TypeIO; import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.NetConnection; import io.anuke.mindustry.type.ContentType; @@ -847,7 +848,7 @@ public class Player extends Unit implements BuilderTrait, CarryTrait, ShooterTra @Override public void write(DataOutput buffer) throws IOException{ super.writeSave(buffer, !isLocal); - buffer.writeUTF(name); //TODO writing strings is very inefficient + TypeIO.writeStringData(buffer, name); //TODO writing strings is very inefficient buffer.writeByte(Bits.toByte(isAdmin) | (Bits.toByte(dead) << 1) | (Bits.toByte(isBoosting) << 2)); buffer.writeInt(Color.rgba8888(color)); buffer.writeByte(mech.id); @@ -862,7 +863,7 @@ public class Player extends Unit implements BuilderTrait, CarryTrait, ShooterTra public void read(DataInput buffer, long time) throws IOException{ float lastx = x, lasty = y, lastrot = rotation; super.readSave(buffer); - name = buffer.readUTF(); + name = TypeIO.readStringData(buffer); byte bools = buffer.readByte(); isAdmin = (bools & 1) != 0; dead = (bools & 2) != 0; diff --git a/core/src/io/anuke/mindustry/io/TypeIO.java b/core/src/io/anuke/mindustry/io/TypeIO.java index c16f87a165..35decc86dd 100644 --- a/core/src/io/anuke/mindustry/io/TypeIO.java +++ b/core/src/io/anuke/mindustry/io/TypeIO.java @@ -23,6 +23,9 @@ import io.anuke.ucore.core.Effects; import io.anuke.ucore.core.Effects.Effect; import io.anuke.ucore.entities.Entities; +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -372,4 +375,35 @@ public class TypeIO{ buffer.get(bytes); return bytes; } + + public static void writeStringData(DataOutput buffer, String string) throws IOException{ + if(string != null){ + Charset charset = Charset.defaultCharset(); + byte[] nameBytes = charset.toString().getBytes(StandardCharsets.UTF_8); + buffer.writeByte((byte)nameBytes.length); + buffer.write(nameBytes); + + byte[] bytes = string.getBytes(charset); + buffer.writeShort((short) bytes.length); + buffer.write(bytes); + }else{ + buffer.writeByte((byte) -1); + } + } + + public static String readStringData(DataInput buffer) throws IOException{ + byte length = buffer.readByte(); + if(length != -1){ + byte[] cbytes = new byte[length]; + buffer.readFully(cbytes); + Charset charset = Charset.forName(new String(cbytes, StandardCharsets.UTF_8)); + + short slength = buffer.readShort(); + byte[] bytes = new byte[slength]; + buffer.readByte(bytes); + return new String(bytes, charset); + }else{ + return null; + } + } }