Better IO
This commit is contained in:
@@ -125,7 +125,7 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
if(tile.entity != null){
|
||||
writeChunk(stream, true, out -> {
|
||||
out.writeByte(tile.entity.version());
|
||||
tile.entity.write(out);
|
||||
tile.entity.write(Writes.get(out));
|
||||
});
|
||||
}else{
|
||||
//write consecutive non-entity blocks
|
||||
@@ -188,7 +188,7 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
try{
|
||||
readChunk(stream, true, in -> {
|
||||
byte revision = in.readByte();
|
||||
tile.entity.read(in, revision);
|
||||
tile.entity.read(Reads.get(in), revision);
|
||||
});
|
||||
}catch(Exception e){
|
||||
throw new IOException("Failed to read tile entity of block: " + block, e);
|
||||
@@ -231,7 +231,7 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
|
||||
writeChunk(stream, true, out -> {
|
||||
out.writeByte(entity.classId());
|
||||
entity.write(out);
|
||||
entity.write(Writes.get(out));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -253,7 +253,7 @@ public abstract class SaveVersion extends SaveFileReader{
|
||||
readChunk(stream, true, in -> {
|
||||
byte typeid = in.readByte();
|
||||
Syncc sync = (Syncc)EntityMapping.map(typeid).get();
|
||||
sync.read(in);
|
||||
sync.read(Reads.get(in));
|
||||
sync.add();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mindustry.io;
|
||||
|
||||
import arc.graphics.*;
|
||||
import arc.util.io.*;
|
||||
import mindustry.annotations.Annotations.*;
|
||||
import mindustry.ctype.*;
|
||||
import mindustry.entities.bullet.*;
|
||||
@@ -19,60 +20,53 @@ import static mindustry.Vars.*;
|
||||
|
||||
/** Class for specifying read/write methods for code generation. */
|
||||
@SuppressWarnings("unused")
|
||||
@TypeIOHandler
|
||||
public class TypeIO{
|
||||
|
||||
@WriteClass(Entityc.class)
|
||||
public static void writeEntity(ByteBuffer buffer, Entityc entity){
|
||||
buffer.putInt(entity == null ? -1 : entity.id());
|
||||
public static void writeEntity(Writes write, Entityc entity){
|
||||
write.i(entity == null ? -1 : entity.id());
|
||||
}
|
||||
|
||||
@ReadClass(Entityc.class)
|
||||
public static <T extends Entityc> T readEntity(ByteBuffer buffer){
|
||||
return (T)Groups.all.getByID(buffer.getInt());
|
||||
public static <T extends Entityc> T readEntity(Reads read){
|
||||
return (T)Groups.all.getByID(read.i());
|
||||
}
|
||||
|
||||
@WriteClass(Tile.class)
|
||||
public static void writeTile(ByteBuffer buffer, Tile tile){
|
||||
buffer.putInt(tile == null ? Pos.get(-1, -1) : tile.pos());
|
||||
public static void writeTile(Writes write, Tile tile){
|
||||
write.i(tile == null ? Pos.get(-1, -1) : tile.pos());
|
||||
}
|
||||
|
||||
@ReadClass(Tile.class)
|
||||
public static Tile readTile(ByteBuffer buffer){
|
||||
return world.tile(buffer.getInt());
|
||||
public static Tile readTile(Reads read){
|
||||
return world.tile(read.i());
|
||||
}
|
||||
|
||||
@WriteClass(Block.class)
|
||||
public static void writeBlock(ByteBuffer buffer, Block block){
|
||||
buffer.putShort(block.id);
|
||||
public static void writeBlock(Writes write, Block block){
|
||||
write.s(block.id);
|
||||
}
|
||||
|
||||
@ReadClass(Block.class)
|
||||
public static Block readBlock(ByteBuffer buffer){
|
||||
return content.block(buffer.getShort());
|
||||
public static Block readBlock(Reads read){
|
||||
return content.block(read.s());
|
||||
}
|
||||
|
||||
@WriteClass(BuildRequest[].class)
|
||||
public static void writeRequests(ByteBuffer buffer, BuildRequest[] requests){
|
||||
buffer.putShort((short)requests.length);
|
||||
public static void writeRequests(Writes write, BuildRequest[] requests){
|
||||
write.s((short)requests.length);
|
||||
for(BuildRequest request : requests){
|
||||
buffer.put(request.breaking ? (byte)1 : 0);
|
||||
buffer.putInt(Pos.get(request.x, request.y));
|
||||
write.b(request.breaking ? (byte)1 : 0);
|
||||
write.i(Pos.get(request.x, request.y));
|
||||
if(!request.breaking){
|
||||
buffer.putShort(request.block.id);
|
||||
buffer.put((byte)request.rotation);
|
||||
buffer.put(request.hasConfig ? (byte)1 : 0);
|
||||
buffer.putInt(request.config);
|
||||
write.s(request.block.id);
|
||||
write.b((byte)request.rotation);
|
||||
write.b(request.hasConfig ? (byte)1 : 0);
|
||||
write.i(request.config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ReadClass(BuildRequest[].class)
|
||||
public static BuildRequest[] readRequests(ByteBuffer buffer){
|
||||
short reqamount = buffer.getShort();
|
||||
public static BuildRequest[] readRequests(Reads read){
|
||||
short reqamount = read.s();
|
||||
BuildRequest[] reqs = new BuildRequest[reqamount];
|
||||
for(int i = 0; i < reqamount; i++){
|
||||
byte type = buffer.get();
|
||||
int position = buffer.getInt();
|
||||
byte type = read.b();
|
||||
int position = read.i();
|
||||
BuildRequest currentRequest;
|
||||
|
||||
if(world.tile(position) == null){
|
||||
@@ -82,10 +76,10 @@ public class TypeIO{
|
||||
if(type == 1){ //remove
|
||||
currentRequest = new BuildRequest(Pos.x(position), Pos.y(position));
|
||||
}else{ //place
|
||||
short block = buffer.getShort();
|
||||
byte rotation = buffer.get();
|
||||
boolean hasConfig = buffer.get() == 1;
|
||||
int config = buffer.getInt();
|
||||
short block = read.s();
|
||||
byte rotation = read.b();
|
||||
boolean hasConfig = read.b() == 1;
|
||||
int config = read.i();
|
||||
currentRequest = new BuildRequest(Pos.x(position), Pos.y(position), rotation, content.block(block));
|
||||
if(hasConfig){
|
||||
currentRequest.configure(config);
|
||||
@@ -98,163 +92,152 @@ public class TypeIO{
|
||||
return reqs;
|
||||
}
|
||||
|
||||
@WriteClass(KickReason.class)
|
||||
public static void writeKick(ByteBuffer buffer, KickReason reason){
|
||||
buffer.put((byte)reason.ordinal());
|
||||
public static void writeKick(Writes write, KickReason reason){
|
||||
write.b((byte)reason.ordinal());
|
||||
}
|
||||
|
||||
@ReadClass(KickReason.class)
|
||||
public static KickReason readKick(ByteBuffer buffer){
|
||||
return KickReason.values()[buffer.get()];
|
||||
public static KickReason readKick(Reads read){
|
||||
return KickReason.values()[read.b()];
|
||||
}
|
||||
|
||||
@WriteClass(Rules.class)
|
||||
public static void writeRules(ByteBuffer buffer, Rules rules){
|
||||
public static void writeRules(Writes write, Rules rules){
|
||||
String string = JsonIO.write(rules);
|
||||
byte[] bytes = string.getBytes(charset);
|
||||
buffer.putInt(bytes.length);
|
||||
buffer.put(bytes);
|
||||
write.i(bytes.length);
|
||||
write.b(bytes);
|
||||
}
|
||||
|
||||
@ReadClass(Rules.class)
|
||||
public static Rules readRules(ByteBuffer buffer){
|
||||
int length = buffer.getInt();
|
||||
byte[] bytes = new byte[length];
|
||||
buffer.get(bytes);
|
||||
String string = new String(bytes, charset);
|
||||
public static Rules readRules(Reads read){
|
||||
int length = read.i();
|
||||
String string = new String(read.b(new byte[length]), charset);
|
||||
return JsonIO.read(Rules.class, string);
|
||||
}
|
||||
|
||||
@WriteClass(Team.class)
|
||||
public static void writeTeam(ByteBuffer buffer, Team reason){
|
||||
buffer.put((byte) (int)reason.id);
|
||||
public static void writeTeam(Writes write, Team reason){
|
||||
write.b((byte) (int)reason.id);
|
||||
}
|
||||
|
||||
@ReadClass(Team.class)
|
||||
public static Team readTeam(ByteBuffer buffer){
|
||||
return Team.get(buffer.get());
|
||||
public static Team readTeam(Reads read){
|
||||
return Team.get(read.b());
|
||||
}
|
||||
|
||||
@WriteClass(UnitCommand.class)
|
||||
public static void writeUnitCommand(ByteBuffer buffer, UnitCommand reason){
|
||||
buffer.put((byte)reason.ordinal());
|
||||
public static void writeUnitCommand(Writes write, UnitCommand reason){
|
||||
write.b((byte)reason.ordinal());
|
||||
}
|
||||
|
||||
@ReadClass(UnitCommand.class)
|
||||
public static UnitCommand readUnitCommand(ByteBuffer buffer){
|
||||
return UnitCommand.all[buffer.get()];
|
||||
public static UnitCommand readUnitCommand(Reads read){
|
||||
return UnitCommand.all[read.b()];
|
||||
}
|
||||
|
||||
@WriteClass(AdminAction.class)
|
||||
public static void writeAction(ByteBuffer buffer, AdminAction reason){
|
||||
buffer.put((byte)reason.ordinal());
|
||||
public static void writeAction(Writes write, AdminAction reason){
|
||||
write.b((byte)reason.ordinal());
|
||||
}
|
||||
|
||||
@ReadClass(AdminAction.class)
|
||||
public static AdminAction readAction(ByteBuffer buffer){
|
||||
return AdminAction.values()[buffer.get()];
|
||||
public static AdminAction readAction(Reads read){
|
||||
return AdminAction.values()[read.b()];
|
||||
}
|
||||
|
||||
@WriteClass(UnitType.class)
|
||||
public static void writeUnitDef(ByteBuffer buffer, UnitType effect){
|
||||
buffer.putShort(effect.id);
|
||||
public static void writeUnitDef(Writes write, UnitType effect){
|
||||
write.s(effect.id);
|
||||
}
|
||||
|
||||
@ReadClass(UnitType.class)
|
||||
public static UnitType readUnitDef(ByteBuffer buffer){
|
||||
return content.getByID(ContentType.unit, buffer.getShort());
|
||||
public static UnitType readUnitDef(Reads read){
|
||||
return content.getByID(ContentType.unit, read.s());
|
||||
}
|
||||
|
||||
@WriteClass(Color.class)
|
||||
public static void writeColor(ByteBuffer buffer, Color color){
|
||||
buffer.putInt(Color.rgba8888(color));
|
||||
public static void writeColor(Writes write, Color color){
|
||||
write.i(Color.rgba8888(color));
|
||||
}
|
||||
|
||||
@ReadClass(Color.class)
|
||||
public static Color readColor(ByteBuffer buffer){
|
||||
return new Color(buffer.getInt());
|
||||
public static Color readColor(Reads read){
|
||||
return new Color(read.i());
|
||||
}
|
||||
|
||||
@WriteClass(Liquid.class)
|
||||
public static void writeLiquid(ByteBuffer buffer, Liquid liquid){
|
||||
buffer.putShort(liquid == null ? -1 : liquid.id);
|
||||
public static void writeLiquid(Writes write, Liquid liquid){
|
||||
write.s(liquid == null ? -1 : liquid.id);
|
||||
}
|
||||
|
||||
@ReadClass(Liquid.class)
|
||||
public static Liquid readLiquid(ByteBuffer buffer){
|
||||
short id = buffer.getShort();
|
||||
public static Liquid readLiquid(Reads read){
|
||||
short id = read.s();
|
||||
return id == -1 ? null : content.liquid(id);
|
||||
}
|
||||
|
||||
@WriteClass(BulletType.class)
|
||||
public static void writeBulletType(ByteBuffer buffer, BulletType type){
|
||||
buffer.putShort(type.id);
|
||||
public static void writeBulletType(Writes write, BulletType type){
|
||||
write.s(type.id);
|
||||
}
|
||||
|
||||
@ReadClass(BulletType.class)
|
||||
public static BulletType readBulletType(ByteBuffer buffer){
|
||||
return content.getByID(ContentType.bullet, buffer.getShort());
|
||||
public static BulletType readBulletType(Reads read){
|
||||
return content.getByID(ContentType.bullet, read.s());
|
||||
}
|
||||
|
||||
@WriteClass(Item.class)
|
||||
public static void writeItem(ByteBuffer buffer, Item item){
|
||||
buffer.putShort(item == null ? -1 : item.id);
|
||||
public static void writeItem(Writes write, Item item){
|
||||
write.s(item == null ? -1 : item.id);
|
||||
}
|
||||
|
||||
@ReadClass(Item.class)
|
||||
public static Item readItem(ByteBuffer buffer){
|
||||
short id = buffer.getShort();
|
||||
public static Item readItem(Reads read){
|
||||
short id = read.s();
|
||||
return id == -1 ? null : content.item(id);
|
||||
}
|
||||
|
||||
@WriteClass(String.class)
|
||||
public static void writeString(ByteBuffer buffer, String string){
|
||||
public static void writeString(Writes write, String string){
|
||||
if(string != null){
|
||||
byte[] bytes = string.getBytes(charset);
|
||||
buffer.putShort((short)bytes.length);
|
||||
buffer.put(bytes);
|
||||
write.s((short)bytes.length);
|
||||
write.b(bytes);
|
||||
}else{
|
||||
buffer.putShort((short)-1);
|
||||
write.s((short)-1);
|
||||
}
|
||||
}
|
||||
|
||||
@ReadClass(String.class)
|
||||
public static String readString(ByteBuffer buffer){
|
||||
short slength = buffer.getShort();
|
||||
public static String readString(Reads read){
|
||||
short slength = read.s();
|
||||
if(slength != -1){
|
||||
return new String(read.b(new byte[slength]), charset);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeString(ByteBuffer write, String string){
|
||||
if(string != null){
|
||||
byte[] bytes = string.getBytes(charset);
|
||||
write.putShort((short)bytes.length);
|
||||
write.put(bytes);
|
||||
}else{
|
||||
write.putShort((short)-1);
|
||||
}
|
||||
}
|
||||
|
||||
public static String readString(ByteBuffer read){
|
||||
short slength = read.getShort();
|
||||
if(slength != -1){
|
||||
byte[] bytes = new byte[slength];
|
||||
buffer.get(bytes);
|
||||
read.get(bytes);
|
||||
return new String(bytes, charset);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@WriteClass(byte[].class)
|
||||
public static void writeBytes(ByteBuffer buffer, byte[] bytes){
|
||||
buffer.putShort((short)bytes.length);
|
||||
buffer.put(bytes);
|
||||
public static void writeBytes(Writes write, byte[] bytes){
|
||||
write.s((short)bytes.length);
|
||||
write.b(bytes);
|
||||
}
|
||||
|
||||
@ReadClass(byte[].class)
|
||||
public static byte[] readBytes(ByteBuffer buffer){
|
||||
short length = buffer.getShort();
|
||||
byte[] bytes = new byte[length];
|
||||
buffer.get(bytes);
|
||||
return bytes;
|
||||
public static byte[] readBytes(Reads read){
|
||||
short length = read.s();
|
||||
return read.b(new byte[length]);
|
||||
}
|
||||
|
||||
@WriteClass(TraceInfo.class)
|
||||
public static void writeTraceInfo(ByteBuffer buffer, TraceInfo trace){
|
||||
writeString(buffer, trace.ip);
|
||||
writeString(buffer, trace.uuid);
|
||||
buffer.put(trace.modded ? (byte)1 : 0);
|
||||
buffer.put(trace.mobile ? (byte)1 : 0);
|
||||
public static void writeTraceInfo(Writes write, TraceInfo trace){
|
||||
writeString(write, trace.ip);
|
||||
writeString(write, trace.uuid);
|
||||
write.b(trace.modded ? (byte)1 : 0);
|
||||
write.b(trace.mobile ? (byte)1 : 0);
|
||||
}
|
||||
|
||||
@ReadClass(TraceInfo.class)
|
||||
public static TraceInfo readTraceInfo(ByteBuffer buffer){
|
||||
return new TraceInfo(readString(buffer), readString(buffer), buffer.get() == 1, buffer.get() == 1);
|
||||
public static TraceInfo readTraceInfo(Reads read){
|
||||
return new TraceInfo(readString(read), readString(read), read.b() == 1, read.b() == 1);
|
||||
}
|
||||
|
||||
public static void writeStringData(DataOutput buffer, String string) throws IOException{
|
||||
|
||||
Reference in New Issue
Block a user