Writing of player nick bytes
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user