diff --git a/annotations/src/main/java/mindustry/annotations/Annotations.java b/annotations/src/main/java/mindustry/annotations/Annotations.java index 2c0dd7d0a5..e286ce6694 100644 --- a/annotations/src/main/java/mindustry/annotations/Annotations.java +++ b/annotations/src/main/java/mindustry/annotations/Annotations.java @@ -218,21 +218,9 @@ public class Annotations{ * This method must return void and have two parameters, the first being of type {@link java.nio.ByteBuffer} and the second * being the type returned by {@link #value()}. */ - @Target(ElementType.METHOD) + @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) - public @interface WriteClass{ - Class value(); - } - - /** - * Specifies that this method will be used to read classes of the type returned by {@link #value()}.
- * This method must return the type returned by {@link #value()}, - * and have one parameter, being of type {@link java.nio.ByteBuffer}. - */ - @Target(ElementType.METHOD) - @Retention(RetentionPolicy.SOURCE) - public @interface ReadClass{ - Class value(); + public @interface TypeIOHandler{ } //endregion diff --git a/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java b/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java index 036bcb248a..997da6bd98 100644 --- a/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java +++ b/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java @@ -1,6 +1,5 @@ package mindustry.annotations.entity; -import arc.util.*; import com.squareup.javapoet.*; import com.squareup.javapoet.MethodSpec.*; import mindustry.annotations.*; @@ -19,14 +18,14 @@ public class EntityIO{ void io(TypeName type, String field) throws Exception{ if(type.isPrimitive()){ - s(type.toString(), field); + s(type == TypeName.BOOLEAN ? "bool" : type.toString().charAt(0) + "", field); }else if(type.toString().equals("java.lang.String")){ - s("UTF", field); + s("str", field); }else if(instanceOf(type.toString(), "mindustry.ctype.Content")){ if(write){ - s("short", field + ".id"); + s("s", field + ".id"); }else{ - st(field + " = mindustry.Vars.content.getByID(mindustry.ctype.ContentType.$L, input.readShort())", BaseProcessor.simpleName(type.toString()).toLowerCase().replace("type", "")); + st(field + " = mindustry.Vars.content.getByID(mindustry.ctype.ContentType.$L, read.s())", BaseProcessor.simpleName(type.toString()).toLowerCase().replace("type", "")); } } } @@ -45,9 +44,9 @@ public class EntityIO{ private void s(String type, String field){ if(write){ - builder.addStatement("output.write$L($L)", Strings.capitalize(type), field); + builder.addStatement("write.$L($L)", type, field); }else{ - builder.addStatement("$L = input.read$L()", field, Strings.capitalize(type)); + builder.addStatement("$L = read.$L()", field, type); } } } diff --git a/annotations/src/main/java/mindustry/annotations/remote/IOFinder.java b/annotations/src/main/java/mindustry/annotations/remote/IOFinder.java deleted file mode 100644 index b5f09fdba3..0000000000 --- a/annotations/src/main/java/mindustry/annotations/remote/IOFinder.java +++ /dev/null @@ -1,88 +0,0 @@ -package mindustry.annotations.remote; - -import mindustry.annotations.Annotations.*; -import mindustry.annotations.*; - -import javax.annotation.processing.*; -import javax.lang.model.element.*; -import javax.lang.model.type.*; -import java.util.*; - -/** - * This class finds reader and writer methods annotated by the {@link WriteClass} - * and {@link ReadClass} annotations. - */ -public class IOFinder{ - - /** - * Finds all class serializers for all types and returns them. Logs errors when necessary. - * Maps fully qualified class names to their serializers. - */ - public HashMap findSerializers(RoundEnvironment env){ - HashMap result = new HashMap<>(); - - //get methods with the types - Set writers = env.getElementsAnnotatedWith(WriteClass.class); - Set readers = env.getElementsAnnotatedWith(ReadClass.class); - - //look for writers first - for(Element writer : writers){ - WriteClass writean = writer.getAnnotation(WriteClass.class); - String typeName = getValue(writean); - - //make sure there's only one read method - if(readers.stream().filter(elem -> getValue(elem.getAnnotation(ReadClass.class)).equals(typeName)).count() > 1){ - BaseProcessor.err("Multiple writer methods for type '" + typeName + "'", writer); - } - - //make sure there's only one write method - long count = readers.stream().filter(elem -> getValue(elem.getAnnotation(ReadClass.class)).equals(typeName)).count(); - if(count == 0){ - BaseProcessor.err("Writer method does not have an accompanying reader: ", writer); - }else if(count > 1){ - BaseProcessor.err("Writer method has multiple reader for type: ", writer); - } - - Element reader = readers.stream().filter(elem -> getValue(elem.getAnnotation(ReadClass.class)).equals(typeName)).findFirst().get(); - - //add to result list - result.put(typeName, new ClassSerializer(BaseProcessor.getMethodName(reader), BaseProcessor.getMethodName(writer), typeName)); - } - - return result; - } - - private String getValue(WriteClass write){ - try{ - Class type = write.value(); - return type.getName(); - }catch(MirroredTypeException e){ - return e.getTypeMirror().toString(); - } - } - - private String getValue(ReadClass read){ - try{ - Class type = read.value(); - return type.getName(); - }catch(MirroredTypeException e){ - return e.getTypeMirror().toString(); - } - } - - /** Information about read/write methods for a specific class type. */ - public static class ClassSerializer{ - /** Fully qualified method name of the reader. */ - public final String readMethod; - /** Fully qualified method name of the writer. */ - public final String writeMethod; - /** Fully qualified class type name. */ - public final String classType; - - public ClassSerializer(String readMethod, String writeMethod, String classType){ - this.readMethod = readMethod; - this.writeMethod = writeMethod; - this.classType = classType; - } - } -} diff --git a/annotations/src/main/java/mindustry/annotations/remote/RemoteProcess.java b/annotations/src/main/java/mindustry/annotations/remote/RemoteProcess.java index f405f44a07..6a23ef45a7 100644 --- a/annotations/src/main/java/mindustry/annotations/remote/RemoteProcess.java +++ b/annotations/src/main/java/mindustry/annotations/remote/RemoteProcess.java @@ -3,11 +3,10 @@ package mindustry.annotations.remote; import com.squareup.javapoet.*; import mindustry.annotations.*; import mindustry.annotations.Annotations.*; -import mindustry.annotations.remote.IOFinder.*; +import mindustry.annotations.remote.TypeIOResolver.*; import javax.annotation.processing.*; import javax.lang.model.element.*; -import javax.tools.Diagnostic.*; import java.util.*; import java.util.stream.*; @@ -32,7 +31,7 @@ public class RemoteProcess extends BaseProcessor{ private static final String callLocation = "Call"; //class serializers - private HashMap serializers; + private ClassSerializer serializer; //all elements with the Remote annotation private Set elements; //map of all classes to generate by name @@ -51,7 +50,7 @@ public class RemoteProcess extends BaseProcessor{ //round 1: find all annotations, generate *writers* if(round == 1){ //get serializers - serializers = new IOFinder().findSerializers(roundEnv); + serializer = TypeIOResolver.resolve(this); //last method ID used int lastMethodID = 0; //find all elements with the Remote annotation @@ -98,12 +97,12 @@ public class RemoteProcess extends BaseProcessor{ } //create read/write generators - RemoteWriteGenerator writegen = new RemoteWriteGenerator(serializers); + RemoteWriteGenerator writegen = new RemoteWriteGenerator(serializer); //generate the methods to invoke (write) writegen.generateFor(classes, packageName); }else if(round == 2){ //round 2: generate all *readers* - RemoteReadGenerator readgen = new RemoteReadGenerator(serializers); + RemoteReadGenerator readgen = new RemoteReadGenerator(serializer); //generate server readers readgen.generateFor(methods.stream().filter(method -> method.where.isClient).collect(Collectors.toList()), readServerName, packageName, true); diff --git a/annotations/src/main/java/mindustry/annotations/remote/RemoteReadGenerator.java b/annotations/src/main/java/mindustry/annotations/remote/RemoteReadGenerator.java index d2d102887f..9a7e2f8546 100644 --- a/annotations/src/main/java/mindustry/annotations/remote/RemoteReadGenerator.java +++ b/annotations/src/main/java/mindustry/annotations/remote/RemoteReadGenerator.java @@ -1,21 +1,21 @@ package mindustry.annotations.remote; +import arc.util.io.*; import com.squareup.javapoet.*; import mindustry.annotations.*; -import mindustry.annotations.remote.IOFinder.*; +import mindustry.annotations.remote.TypeIOResolver.*; import javax.lang.model.element.Modifier; import javax.lang.model.element.*; import java.lang.reflect.*; -import java.nio.*; import java.util.*; /** Generates code for reading remote invoke packets on the client and server. */ public class RemoteReadGenerator{ - private final HashMap serializers; + private final ClassSerializer serializers; /** Creates a read generator that uses the supplied serializer setup. */ - public RemoteReadGenerator(HashMap serializers){ + public RemoteReadGenerator(ClassSerializer serializers){ this.serializers = serializers; } @@ -34,7 +34,7 @@ public class RemoteReadGenerator{ //create main method builder MethodSpec.Builder readMethod = MethodSpec.methodBuilder("readPacket") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) - .addParameter(ByteBuffer.class, "buffer") //buffer to read form + .addParameter(Reads.class, "read") //buffer to read form .addParameter(int.class, "id") //ID of method type to read .returns(void.class); @@ -76,26 +76,22 @@ public class RemoteReadGenerator{ //name of parameter String varName = var.getSimpleName().toString(); //captialized version of type name for reading primitives - String capName = typeName.equals("byte") ? "" : Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1); + String pname = typeName.equals("boolean") ? "bool" : typeName.charAt(0) + ""; //write primitives automatically if(BaseProcessor.isPrimitive(typeName)){ - if(typeName.equals("boolean")){ - readBlock.addStatement("boolean " + varName + " = buffer.get() == 1"); - }else{ - readBlock.addStatement(typeName + " " + varName + " = buffer.get" + capName + "()"); - } + readBlock.addStatement("$L $L = read.$L()", typeName, varName, pname); }else{ //else, try and find a serializer - ClassSerializer ser = serializers.getOrDefault(typeName, SerializerResolver.locate(entry.element, var.asType())); + String ser = serializers.readers.get(typeName, SerializerResolver.locate(entry.element, var.asType(), false)); if(ser == null){ //make sure a serializer exists! - BaseProcessor.err("No @ReadClass method to read class type '" + typeName + "' in method " + entry.targetMethod, var); + BaseProcessor.err("No read method to read class type '" + typeName + "' in method " + entry.targetMethod + "; " + serializers.readers, var); return; } //add statement for reading it - readBlock.addStatement(typeName + " " + varName + " = " + ser.readMethod + "(buffer)"); + readBlock.addStatement(typeName + " " + varName + " = " + ser + "(read)"); } //append variable name to string builder diff --git a/annotations/src/main/java/mindustry/annotations/remote/RemoteWriteGenerator.java b/annotations/src/main/java/mindustry/annotations/remote/RemoteWriteGenerator.java index bf6330bc51..b8a3509f63 100644 --- a/annotations/src/main/java/mindustry/annotations/remote/RemoteWriteGenerator.java +++ b/annotations/src/main/java/mindustry/annotations/remote/RemoteWriteGenerator.java @@ -1,22 +1,22 @@ package mindustry.annotations.remote; import arc.struct.*; +import arc.util.io.*; import com.squareup.javapoet.*; import mindustry.annotations.Annotations.*; import mindustry.annotations.*; -import mindustry.annotations.remote.IOFinder.*; +import mindustry.annotations.remote.TypeIOResolver.*; import javax.lang.model.element.*; import java.io.*; -import java.nio.*; import java.util.*; /** Generates code for writing remote invoke packets on the client and server. */ public class RemoteWriteGenerator{ - private final HashMap serializers; + private final ClassSerializer serializers; /** Creates a write generator that uses the supplied serializer setup. */ - public RemoteWriteGenerator(HashMap serializers){ + public RemoteWriteGenerator(ClassSerializer serializers){ this.serializers = serializers; } @@ -29,8 +29,12 @@ public class RemoteWriteGenerator{ classBuilder.addJavadoc(RemoteProcess.autogenWarning); //add temporary write buffer - classBuilder.addField(FieldSpec.builder(ByteBuffer.class, "TEMP_BUFFER", Modifier.STATIC, Modifier.PRIVATE, Modifier.FINAL) - .initializer("ByteBuffer.allocate($1L)", RemoteProcess.maxPacketSize).build()); + classBuilder.addField(FieldSpec.builder(ReusableByteOutStream.class, "OUT", Modifier.STATIC, Modifier.PRIVATE, Modifier.FINAL) + .initializer("new ReusableByteOutStream($L)", RemoteProcess.maxPacketSize).build()); + + //add writer for that buffer + classBuilder.addField(FieldSpec.builder(Writes.class, "WRITE", Modifier.STATIC, Modifier.PRIVATE, Modifier.FINAL) + .initializer("new Writes(new $T(OUT))", DataOutputStream.class).build()); //go through each method entry in this class for(MethodEntry methodEntry : entry.methods){ @@ -128,14 +132,12 @@ public class RemoteWriteGenerator{ //add statement to create packet from pool method.addStatement("$1N packet = $2N.obtain($1N.class, $1N::new)", "mindustry.net.Packets.InvokePacket", "arc.util.pooling.Pools"); - //assign buffer - method.addStatement("packet.writeBuffer = TEMP_BUFFER"); //assign priority method.addStatement("packet.priority = (byte)" + methodEntry.priority.ordinal()); //assign method ID method.addStatement("packet.type = (byte)" + methodEntry.id); - //rewind buffer - method.addStatement("TEMP_BUFFER.position(0)"); + //reset stream + method.addStatement("OUT.reset()"); method.addTypeVariables(Array.with(elem.getTypeParameters()).map(BaseProcessor::getTVN)); @@ -169,15 +171,10 @@ public class RemoteWriteGenerator{ } if(BaseProcessor.isPrimitive(typeName)){ //check if it's a primitive, and if so write it - if(typeName.equals("boolean")){ //booleans are special - method.addStatement("TEMP_BUFFER.put(" + varName + " ? (byte)1 : 0)"); - }else{ - method.addStatement("TEMP_BUFFER.put" + - capName + "(" + varName + ")"); - } + method.addStatement("WRITE.$L($L)", typeName.equals("boolean") ? "bool" : typeName.charAt(0) + "", varName); }else{ //else, try and find a serializer - ClassSerializer ser = serializers.getOrDefault(typeName, SerializerResolver.locate(elem, var.asType())); + String ser = serializers.writers.get(typeName, SerializerResolver.locate(elem, var.asType(), true)); if(ser == null){ //make sure a serializer exists! BaseProcessor.err("No @WriteClass method to write class type: '" + typeName + "'", var); @@ -185,7 +182,7 @@ public class RemoteWriteGenerator{ } //add statement for writing it - method.addStatement(ser.writeMethod + "(TEMP_BUFFER, " + varName + ")"); + method.addStatement(ser + "(WRITE, " + varName + ")"); } if(writePlayerSkipCheck){ //write end check @@ -193,8 +190,10 @@ public class RemoteWriteGenerator{ } } + //assign packet bytes + method.addStatement("packet.bytes = OUT.getBytes()"); //assign packet length - method.addStatement("packet.writeLength = TEMP_BUFFER.position()"); + method.addStatement("packet.length = OUT.size()"); String sendString; diff --git a/annotations/src/main/java/mindustry/annotations/remote/SerializerResolver.java b/annotations/src/main/java/mindustry/annotations/remote/SerializerResolver.java index d1668bdfc2..6431bfea49 100644 --- a/annotations/src/main/java/mindustry/annotations/remote/SerializerResolver.java +++ b/annotations/src/main/java/mindustry/annotations/remote/SerializerResolver.java @@ -1,24 +1,17 @@ package mindustry.annotations.remote; import arc.struct.*; -import mindustry.annotations.remote.IOFinder.*; import javax.lang.model.element.*; import javax.lang.model.type.*; public class SerializerResolver{ - private static final ClassSerializer entitySerializer = new ClassSerializer("mindustry.io.TypeIO.readEntity", "mindustry.io.TypeIO.writeEntity", "Entityc"); - public static ClassSerializer locate(ExecutableElement elem, TypeMirror mirror){ + public static String locate(ExecutableElement elem, TypeMirror mirror, boolean write){ //generic type - if(mirror.toString().equals("T")){ - TypeParameterElement param = elem.getTypeParameters().get(0); - if(Array.with(param.getBounds()).contains(SerializerResolver::isEntity)){ - return entitySerializer; - } - } - if(isEntity(mirror)){ - return entitySerializer; + if((mirror.toString().equals("T") && Array.with(elem.getTypeParameters().get(0).getBounds()).contains(SerializerResolver::isEntity)) || + isEntity(mirror)){ + return write ? "mindustry.io.TypeIO.writeEntity" : "mindustry.io.TypeIO.readEntity"; } return null; } diff --git a/annotations/src/main/java/mindustry/annotations/remote/TypeIOResolver.java b/annotations/src/main/java/mindustry/annotations/remote/TypeIOResolver.java new file mode 100644 index 0000000000..ffa98dae22 --- /dev/null +++ b/annotations/src/main/java/mindustry/annotations/remote/TypeIOResolver.java @@ -0,0 +1,50 @@ +package mindustry.annotations.remote; + +import arc.struct.*; +import mindustry.annotations.Annotations.*; +import mindustry.annotations.*; +import mindustry.annotations.util.*; + +import javax.lang.model.element.*; + +/** + * This class finds reader and writer methods. + */ +public class TypeIOResolver{ + + /** + * Finds all class serializers for all types and returns them. Logs errors when necessary. + * Maps fully qualified class names to their serializers. + */ + public static ClassSerializer resolve(BaseProcessor processor){ + ClassSerializer out = new ClassSerializer(new ObjectMap<>(), new ObjectMap<>()); + for(Stype type : processor.types(TypeIOHandler.class)){ + //look at all TypeIOHandler methods + Array methods = type.methods(); + for(Smethod meth : methods){ + if(meth.is(Modifier.PUBLIC) && meth.is(Modifier.STATIC)){ + Array params = meth.params(); + //2 params, second one is type, first is writer + if(params.size == 2 && params.first().tname().toString().equals("arc.util.io.Writes")){ + out.writers.put(params.get(1).tname().toString(), type.fullName() + "." + meth.name()); + }else if(params.size == 1 && params.first().tname().toString().equals("arc.util.io.Reads") && !meth.isVoid()){ + //1 param, one is reader, returns type + out.readers.put(meth.retn().toString(), type.fullName() + "." + meth.name()); + } + } + } + } + + return out; + } + + /** Information about read/write methods for class types. */ + public static class ClassSerializer{ + public final ObjectMap writers, readers; + + public ClassSerializer(ObjectMap writers, ObjectMap readers){ + this.writers = writers; + this.readers = readers; + } + } +} diff --git a/core/src/mindustry/core/NetClient.java b/core/src/mindustry/core/NetClient.java index babcd3d4b3..c402dc1dd6 100644 --- a/core/src/mindustry/core/NetClient.java +++ b/core/src/mindustry/core/NetClient.java @@ -121,8 +121,7 @@ public class NetClient implements ApplicationListener{ }); net.handleClient(InvokePacket.class, packet -> { - packet.writeBuffer.position(0); - RemoteReadClient.readPacket(packet.writeBuffer, packet.type); + RemoteReadClient.readPacket(packet.reader(), packet.type); }); } @@ -377,7 +376,7 @@ public class NetClient implements ApplicationListener{ } //read the entity - entity.read(input); + entity.read(Reads.get(input)); if(created && entity.interpolator().target != null){ //set initial starting position @@ -407,7 +406,7 @@ public class NetClient implements ApplicationListener{ Log.warn("Missing entity at {0}. Skipping block snapshot.", tile); break; } - tile.entity.read(input, tile.entity.version()); + tile.entity.read(Reads.get(input), tile.entity.version()); } }catch(Exception e){ e.printStackTrace(); @@ -435,9 +434,9 @@ public class NetClient implements ApplicationListener{ Tile tile = world.tile(pos); if(tile != null && tile.entity != null){ - tile.entity.items().read(input); + tile.entity.items().read(Reads.get(input)); }else{ - new ItemModule().read(input); + new ItemModule().read(Reads.get(input)); } } diff --git a/core/src/mindustry/core/NetServer.java b/core/src/mindustry/core/NetServer.java index 6d575e1307..77d24bdc4c 100644 --- a/core/src/mindustry/core/NetServer.java +++ b/core/src/mindustry/core/NetServer.java @@ -61,8 +61,8 @@ public class NetServer implements ApplicationListener{ private boolean closing = false; private Interval timer = new Interval(); - private ByteBuffer writeBuffer = ByteBuffer.allocate(127); - private ByteBufferOutput outputBuffer = new ByteBufferOutput(writeBuffer); + private ReusableByteOutStream writeBuffer = new ReusableByteOutStream(127); + private Writes outputBuffer = new Writes(new DataOutputStream(writeBuffer)); /** Stream for writing player sync data to. */ private ReusableByteOutStream syncStream = new ReusableByteOutStream(); @@ -214,7 +214,7 @@ public class NetServer implements ApplicationListener{ } try{ - writeBuffer.position(0); + writeBuffer.reset(); player.write(outputBuffer); }catch(Throwable t){ t.printStackTrace(); @@ -237,7 +237,7 @@ public class NetServer implements ApplicationListener{ net.handleServer(InvokePacket.class, (con, packet) -> { if(con.player == null) return; try{ - RemoteReadServer.readPacket(packet.writeBuffer, packet.type, con.player); + RemoteReadServer.readPacket(packet.reader(), packet.type, con.player); }catch(ValidateException e){ Log.debug("Validation failed for '{0}': {1}", e.player, e.getMessage()); }catch(RuntimeException e){ @@ -704,7 +704,7 @@ public class NetServer implements ApplicationListener{ sent ++; dataStream.writeInt(entity.tile().pos()); - entity.write(dataStream); + entity.write(Writes.get(dataStream)); if(syncStream.size() > maxSnapshotSize){ dataStream.close(); @@ -730,7 +730,7 @@ public class NetServer implements ApplicationListener{ for(CoreEntity entity : cores){ dataStream.writeInt(entity.tile().pos()); - entity.items().write(dataStream); + entity.items().write(Writes.get(dataStream)); } dataStream.close(); @@ -749,7 +749,7 @@ public class NetServer implements ApplicationListener{ //write all entities now dataStream.writeInt(entity.id()); //write id dataStream.writeByte(entity.classId()); //write type ID - entity.write(dataStream); //write entity + entity.write(Writes.get(dataStream)); //write entity sent++; diff --git a/core/src/mindustry/entities/def/EntityComp.java b/core/src/mindustry/entities/def/EntityComp.java index a9520bf3b8..0ba8fc13df 100644 --- a/core/src/mindustry/entities/def/EntityComp.java +++ b/core/src/mindustry/entities/def/EntityComp.java @@ -1,12 +1,11 @@ package mindustry.entities.def; import arc.func.*; +import arc.util.io.*; import mindustry.annotations.Annotations.*; import mindustry.entities.*; import mindustry.gen.*; -import java.io.*; - import static mindustry.Vars.player; @Component @@ -53,11 +52,11 @@ abstract class EntityComp{ abstract boolean serialize(); @MethodPriority(1) - void read(DataInput input) throws IOException{ + void read(Reads read){ afterRead(); } - void write(DataOutput output) throws IOException{ + void write(Writes write){ } diff --git a/core/src/mindustry/entities/def/MassComp.java b/core/src/mindustry/entities/def/MassComp.java index 13ec0c2b9d..aef77bfa6a 100644 --- a/core/src/mindustry/entities/def/MassComp.java +++ b/core/src/mindustry/entities/def/MassComp.java @@ -5,7 +5,7 @@ import mindustry.gen.*; @Component abstract class MassComp implements Velc{ - float mass = 1f; + transient float mass = 1f; public void impulse(float x, float y){ vel().add(x / mass, y / mass); diff --git a/core/src/mindustry/entities/def/TileComp.java b/core/src/mindustry/entities/def/TileComp.java index 8fa16401b2..611aafae7d 100644 --- a/core/src/mindustry/entities/def/TileComp.java +++ b/core/src/mindustry/entities/def/TileComp.java @@ -5,6 +5,7 @@ import arc.math.geom.*; import arc.struct.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; import mindustry.annotations.Annotations.*; import mindustry.game.EventType.*; import mindustry.game.*; @@ -13,8 +14,6 @@ import mindustry.world.*; import mindustry.world.consumers.*; import mindustry.world.modules.*; -import java.io.*; - import static mindustry.Vars.*; @EntityDef(value = {Tilec.class}, isFinal = false, genio = false, serialize = false) @@ -63,30 +62,30 @@ abstract class TileComp implements Posc, Teamc, Healthc, Tilec, Timerc{ } @CallSuper - public void write(DataOutput output) throws IOException{ - output.writeFloat(health()); - output.writeByte(tile.rotation()); - output.writeByte(tile.getTeamID()); - if(items != null) items.write(output); - if(power != null) power.write(output); - if(liquids != null) liquids.write(output); - if(cons != null) cons.write(output); + public void write(Writes write){ + write.f(health()); + write.b(tile.rotation()); + write.b(tile.getTeamID()); + if(items != null) items.write(write); + if(power != null) power.write(write); + if(liquids != null) liquids.write(write); + if(cons != null) cons.write(write); } @CallSuper @Override - public void read(DataInput input, byte revision) throws IOException{ - health(input.readFloat()); - byte rotation = input.readByte(); - byte team = input.readByte(); + public void read(Reads read, byte revision){ + health(read.f()); + byte rotation = read.b(); + byte team = read.b(); tile.setTeam(Team.get(team)); tile.rotation(rotation); - if(items != null) items.read(input); - if(power != null) power.read(input); - if(liquids != null) liquids.read(input); - if(cons != null) cons.read(input); + if(items != null) items.read(read); + if(power != null) power.read(read); + if(liquids != null) liquids.read(read); + if(cons != null) cons.read(read); } @Override diff --git a/core/src/mindustry/entities/def/VelComp.java b/core/src/mindustry/entities/def/VelComp.java index fdc0f22f04..a704ad0398 100644 --- a/core/src/mindustry/entities/def/VelComp.java +++ b/core/src/mindustry/entities/def/VelComp.java @@ -10,7 +10,7 @@ abstract class VelComp implements Posc{ @Import float x, y; final Vec2 vel = new Vec2(); - float drag = 0f; + transient float drag = 0f; //velocity needs to be called first, as it affects delta and lastPosition @MethodPriority(-1) diff --git a/core/src/mindustry/io/SaveVersion.java b/core/src/mindustry/io/SaveVersion.java index 1ca1197db3..eed215ec1a 100644 --- a/core/src/mindustry/io/SaveVersion.java +++ b/core/src/mindustry/io/SaveVersion.java @@ -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(); }); } diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index c9cfe60b40..f41716a727 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -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 readEntity(ByteBuffer buffer){ - return (T)Groups.all.getByID(buffer.getInt()); + public static 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{ diff --git a/core/src/mindustry/net/NetworkIO.java b/core/src/mindustry/net/NetworkIO.java index 10504e9143..0c6a435343 100644 --- a/core/src/mindustry/net/NetworkIO.java +++ b/core/src/mindustry/net/NetworkIO.java @@ -1,9 +1,10 @@ package mindustry.net; import arc.util.*; +import arc.util.io.*; import mindustry.core.*; -import mindustry.gen.*; import mindustry.game.*; +import mindustry.gen.*; import mindustry.io.*; import mindustry.maps.Map; import mindustry.net.Administration.*; @@ -26,7 +27,7 @@ public class NetworkIO{ stream.writeFloat(state.wavetime); stream.writeInt(player.id()); - player.write(stream); + player.write(Writes.get(stream)); SaveIO.getSaveWriter().writeContentHeader(stream); SaveIO.getSaveWriter().writeMap(stream); @@ -48,7 +49,7 @@ public class NetworkIO{ Groups.all.clear(); int id = stream.readInt(); player.reset(); - player.read(stream); + player.read(Reads.get(stream)); player.id(id); player.add(); diff --git a/core/src/mindustry/net/Packets.java b/core/src/mindustry/net/Packets.java index e24d18db6f..d9d49c37e0 100644 --- a/core/src/mindustry/net/Packets.java +++ b/core/src/mindustry/net/Packets.java @@ -2,10 +2,12 @@ package mindustry.net; import arc.*; import arc.struct.*; +import arc.util.io.*; import arc.util.serialization.*; import mindustry.core.*; import mindustry.io.*; +import java.io.*; import java.nio.*; /** @@ -65,31 +67,29 @@ public class Packets{ } public static class InvokePacket implements Packet{ + private static ReusableByteInStream bin; + private static Reads read = new Reads(new DataInputStream(bin = new ReusableByteInStream())); + public byte type, priority; - public ByteBuffer writeBuffer; - public int writeLength; + public byte[] bytes; + public int length; @Override public void read(ByteBuffer buffer){ type = buffer.get(); priority = buffer.get(); - writeLength = buffer.getShort(); + short writeLength = buffer.getShort(); byte[] bytes = new byte[writeLength]; buffer.get(bytes); - writeBuffer = ByteBuffer.wrap(bytes); } @Override public void write(ByteBuffer buffer){ buffer.put(type); buffer.put(priority); - buffer.putShort((short)writeLength); - - writeBuffer.position(0); - for(int i = 0; i < writeLength; i++){ - buffer.put(writeBuffer.get()); - } + buffer.putShort((short)length); + buffer.put(bytes, 0, length); } @Override @@ -106,6 +106,11 @@ public class Packets{ public boolean isUnimportant(){ return priority == 2; } + + public Reads reader(){ + bin.setBytes(bytes); + return read; + } } /** Marks the beginning of a stream. */ diff --git a/core/src/mindustry/world/DirectionalItemBuffer.java b/core/src/mindustry/world/DirectionalItemBuffer.java index 5c57376fa1..a44e34c6af 100644 --- a/core/src/mindustry/world/DirectionalItemBuffer.java +++ b/core/src/mindustry/world/DirectionalItemBuffer.java @@ -1,9 +1,10 @@ package mindustry.world; -import mindustry.annotations.Annotations.Struct; -import arc.util.Time; -import mindustry.gen.BufferItem; -import mindustry.type.Item; +import arc.util.*; +import arc.util.io.*; +import mindustry.annotations.Annotations.*; +import mindustry.gen.*; +import mindustry.type.*; import java.io.*; @@ -46,22 +47,22 @@ public class DirectionalItemBuffer{ indexes[buffer] --; } - public void write(DataOutput stream) throws IOException{ + public void write(Writes write){ for(int i = 0; i < 4; i++){ - stream.writeByte(indexes[i]); - stream.writeByte(buffers[i].length); + write.b(indexes[i]); + write.b(buffers[i].length); for(long l : buffers[i]){ - stream.writeLong(l); + write.l(l); } } } - public void read(DataInput stream) throws IOException{ + public void read(Reads read){ for(int i = 0; i < 4; i++){ - indexes[i] = stream.readByte(); - byte length = stream.readByte(); + indexes[i] = read.b(); + byte length = read.b(); for(int j = 0; j < length; j++){ - long value = stream.readLong(); + long value = read.l(); if(j < buffers[i].length){ buffers[i][j] = value; } diff --git a/core/src/mindustry/world/ItemBuffer.java b/core/src/mindustry/world/ItemBuffer.java index 20f414403f..107d42bb08 100644 --- a/core/src/mindustry/world/ItemBuffer.java +++ b/core/src/mindustry/world/ItemBuffer.java @@ -1,9 +1,8 @@ package mindustry.world; import arc.util.*; -import mindustry.type.Item; - -import java.io.*; +import arc.util.io.*; +import mindustry.type.*; import static mindustry.Vars.content; @@ -60,19 +59,19 @@ public class ItemBuffer{ index--; } - public void write(DataOutput stream) throws IOException{ - stream.writeByte((byte)index); - stream.writeByte((byte)buffer.length); + public void write(Writes write){ + write.b((byte)index); + write.b((byte)buffer.length); for(long l : buffer){ - stream.writeLong(l); + write.l(l); } } - public void read(DataInput stream) throws IOException{ - index = stream.readByte(); - byte length = stream.readByte(); + public void read(Reads read){ + index = read.b(); + byte length = read.b(); for(int i = 0; i < length; i++){ - long l = stream.readLong(); + long l = read.l(); if(i < buffer.length){ buffer[i] = l; } diff --git a/core/src/mindustry/world/blocks/BuildBlock.java b/core/src/mindustry/world/blocks/BuildBlock.java index 2c71e03994..24fe3aeeac 100644 --- a/core/src/mindustry/world/blocks/BuildBlock.java +++ b/core/src/mindustry/world/blocks/BuildBlock.java @@ -7,6 +7,7 @@ import arc.graphics.g2d.*; import arc.math.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; import mindustry.annotations.Annotations.*; import mindustry.content.*; import mindustry.entities.*; @@ -347,37 +348,37 @@ public class BuildBlock extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(progress); - stream.writeShort(previous == null ? -1 : previous.id); - stream.writeShort(cblock == null ? -1 : cblock.id); + public void write(Writes write){ + super.write(write); + write.f(progress); + write.s(previous == null ? -1 : previous.id); + write.s(cblock == null ? -1 : cblock.id); if(accumulator == null){ - stream.writeByte(-1); + write.b(-1); }else{ - stream.writeByte(accumulator.length); + write.b(accumulator.length); for(int i = 0; i < accumulator.length; i++){ - stream.writeFloat(accumulator[i]); - stream.writeFloat(totalAccumulator[i]); + write.f(accumulator[i]); + write.f(totalAccumulator[i]); } } } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - progress = stream.readFloat(); - short pid = stream.readShort(); - short rid = stream.readShort(); - byte acsize = stream.readByte(); + public void read(Reads read, byte revision){ + super.read(read, revision); + progress = read.f(); + short pid = read.s(); + short rid = read.s(); + byte acsize = read.b(); if(acsize != -1){ accumulator = new float[acsize]; totalAccumulator = new float[acsize]; for(int i = 0; i < acsize; i++){ - accumulator[i] = stream.readFloat(); - totalAccumulator[i] = stream.readFloat(); + accumulator[i] = read.f(); + totalAccumulator[i] = read.f(); } } diff --git a/core/src/mindustry/world/blocks/defense/Door.java b/core/src/mindustry/world/blocks/defense/Door.java index b8753f45e6..d38d2931c1 100644 --- a/core/src/mindustry/world/blocks/defense/Door.java +++ b/core/src/mindustry/world/blocks/defense/Door.java @@ -1,6 +1,7 @@ package mindustry.world.blocks.defense; import arc.*; +import arc.util.io.*; import mindustry.annotations.Annotations.*; import arc.Graphics.*; import arc.Graphics.Cursor.*; @@ -92,15 +93,15 @@ public class Door extends Wall{ public boolean open = false; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeBoolean(open); + public void write(Writes write){ + super.write(write); + write.bool(open); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - open = stream.readBoolean(); + public void read(Reads read, byte revision){ + super.read(read, revision); + open = read.bool(); } } diff --git a/core/src/mindustry/world/blocks/defense/ForceProjector.java b/core/src/mindustry/world/blocks/defense/ForceProjector.java index b7b06d49e8..e95a27aa7e 100644 --- a/core/src/mindustry/world/blocks/defense/ForceProjector.java +++ b/core/src/mindustry/world/blocks/defense/ForceProjector.java @@ -7,6 +7,7 @@ import arc.graphics.g2d.*; import arc.math.*; import arc.math.geom.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.gen.*; import mindustry.graphics.*; @@ -171,23 +172,23 @@ public class ForceProjector extends Block{ float phaseHeat; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeBoolean(broken); - stream.writeFloat(buildup); - stream.writeFloat(radscl); - stream.writeFloat(warmup); - stream.writeFloat(phaseHeat); + public void write(Writes write){ + super.write(write); + write.bool(broken); + write.f(buildup); + write.f(radscl); + write.f(warmup); + write.f(phaseHeat); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - broken = stream.readBoolean(); - buildup = stream.readFloat(); - radscl = stream.readFloat(); - warmup = stream.readFloat(); - phaseHeat = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + broken = read.bool(); + buildup = read.f(); + radscl = read.f(); + warmup = read.f(); + phaseHeat = read.f(); } } diff --git a/core/src/mindustry/world/blocks/defense/MendProjector.java b/core/src/mindustry/world/blocks/defense/MendProjector.java index 74f40c7ac0..84565b866e 100644 --- a/core/src/mindustry/world/blocks/defense/MendProjector.java +++ b/core/src/mindustry/world/blocks/defense/MendProjector.java @@ -6,6 +6,7 @@ import arc.graphics.Color; import arc.graphics.g2d.*; import arc.math.Mathf; import arc.util.*; +import arc.util.io.*; import mindustry.content.Fx; import mindustry.gen.*; import mindustry.graphics.*; @@ -126,17 +127,17 @@ public class MendProjector extends Block{ float phaseHeat; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(heat); - stream.writeFloat(phaseHeat); + public void write(Writes write){ + super.write(write); + write.f(heat); + write.f(phaseHeat); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - heat = stream.readFloat(); - phaseHeat = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + heat = read.f(); + phaseHeat = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/defense/OverdriveProjector.java b/core/src/mindustry/world/blocks/defense/OverdriveProjector.java index e717c060d5..0f8d34ef0f 100644 --- a/core/src/mindustry/world/blocks/defense/OverdriveProjector.java +++ b/core/src/mindustry/world/blocks/defense/OverdriveProjector.java @@ -6,6 +6,7 @@ import arc.graphics.Color; import arc.graphics.g2d.*; import arc.math.Mathf; import arc.util.Time; +import arc.util.io.*; import mindustry.gen.*; import mindustry.graphics.*; import mindustry.world.*; @@ -124,17 +125,17 @@ public class OverdriveProjector extends Block{ float phaseHeat; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(heat); - stream.writeFloat(phaseHeat); + public void write(Writes write){ + super.write(write); + write.f(heat); + write.f(phaseHeat); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - heat = stream.readFloat(); - phaseHeat = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + heat = read.f(); + phaseHeat = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/defense/turrets/ItemTurret.java b/core/src/mindustry/world/blocks/defense/turrets/ItemTurret.java index 4a7505efa8..a7e046bc12 100644 --- a/core/src/mindustry/world/blocks/defense/turrets/ItemTurret.java +++ b/core/src/mindustry/world/blocks/defense/turrets/ItemTurret.java @@ -3,6 +3,7 @@ package mindustry.world.blocks.defense.turrets; import arc.*; import arc.struct.*; import arc.scene.ui.layout.*; +import arc.util.io.*; import mindustry.*; import mindustry.content.*; import mindustry.entities.bullet.*; @@ -151,23 +152,23 @@ public class ItemTurret extends CooledTurret{ public class ItemTurretEntity extends TurretEntity{ @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeByte(ammo.size); + public void write(Writes write){ + super.write(write); + write.b(ammo.size); for(AmmoEntry entry : ammo){ ItemEntry i = (ItemEntry)entry; - stream.writeByte(i.item.id); - stream.writeShort(i.amount); + write.b(i.item.id); + write.s(i.amount); } } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - byte amount = stream.readByte(); + public void read(Reads read, byte revision){ + super.read(read, revision); + byte amount = read.b(); for(int i = 0; i < amount; i++){ - Item item = Vars.content.item(stream.readByte()); - short a = stream.readShort(); + Item item = Vars.content.item(read.b()); + short a = read.s(); totalAmmo += a; ammo.add(new ItemEntry(item, a)); } diff --git a/core/src/mindustry/world/blocks/defense/turrets/Turret.java b/core/src/mindustry/world/blocks/defense/turrets/Turret.java index 4e70eed14c..4b2eaf4171 100644 --- a/core/src/mindustry/world/blocks/defense/turrets/Turret.java +++ b/core/src/mindustry/world/blocks/defense/turrets/Turret.java @@ -12,6 +12,7 @@ import arc.math.Angles; import arc.math.Mathf; import arc.math.geom.Vec2; import arc.util.Time; +import arc.util.io.*; import mindustry.content.Fx; import mindustry.entities.*; import mindustry.entities.bullet.BulletType; @@ -320,17 +321,17 @@ public abstract class Turret extends Block{ public Posc target; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(reload); - stream.writeFloat(rotation); + public void write(Writes write){ + super.write(write); + write.f(reload); + write.f(rotation); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - reload = stream.readFloat(); - rotation = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + reload = read.f(); + rotation = read.f(); } @Override diff --git a/core/src/mindustry/world/blocks/distribution/BufferedItemBridge.java b/core/src/mindustry/world/blocks/distribution/BufferedItemBridge.java index 87e7ce7e1e..6171141034 100644 --- a/core/src/mindustry/world/blocks/distribution/BufferedItemBridge.java +++ b/core/src/mindustry/world/blocks/distribution/BufferedItemBridge.java @@ -1,6 +1,7 @@ package mindustry.world.blocks.distribution; import arc.math.*; +import arc.util.io.*; import mindustry.type.*; import mindustry.world.*; @@ -41,15 +42,15 @@ public class BufferedItemBridge extends ExtendingItemBridge{ ItemBuffer buffer = new ItemBuffer(bufferCapacity, speed); @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - buffer.write(stream); + public void write(Writes write){ + super.write(write); + buffer.write(write); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - buffer.read(stream); + public void read(Reads read, byte revision){ + super.read(read, revision); + buffer.read(read); } } } diff --git a/core/src/mindustry/world/blocks/distribution/Conveyor.java b/core/src/mindustry/world/blocks/distribution/Conveyor.java index 77eb3aa098..0d5e980351 100644 --- a/core/src/mindustry/world/blocks/distribution/Conveyor.java +++ b/core/src/mindustry/world/blocks/distribution/Conveyor.java @@ -8,6 +8,7 @@ import arc.math.geom.*; import arc.struct.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.gen.*; import mindustry.entities.units.*; @@ -351,23 +352,23 @@ public class Conveyor extends Block implements Autotiler{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeInt(len); + public void write(Writes write){ + super.write(write); + write.i(len); for(int i = 0; i < len; i++){ - stream.writeInt(Pack.intBytes((byte)ids[i].id, (byte)(xs[i] * 127), (byte)(ys[i] * 255 - 128), (byte)0)); + write.i(Pack.intBytes((byte)ids[i].id, (byte)(xs[i] * 127), (byte)(ys[i] * 255 - 128), (byte)0)); } } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - int amount = stream.readInt(); + public void read(Reads read, byte revision){ + super.read(read, revision); + int amount = read.i(); len = Math.min(amount, capacity); for(int i = 0; i < amount; i++){ - int val = stream.readInt(); + int val = read.i(); byte id = (byte)(val >> 24); float x = (float)((byte)(val >> 16)) / 127f; float y = ((float)((byte)(val >> 8)) + 128f) / 255f; diff --git a/core/src/mindustry/world/blocks/distribution/ItemBridge.java b/core/src/mindustry/world/blocks/distribution/ItemBridge.java index 79b063d53f..9fbe9a4779 100644 --- a/core/src/mindustry/world/blocks/distribution/ItemBridge.java +++ b/core/src/mindustry/world/blocks/distribution/ItemBridge.java @@ -8,6 +8,7 @@ import arc.graphics.g2d.*; import arc.math.*; import arc.math.geom.*; import arc.util.*; +import arc.util.io.*; import mindustry.gen.*; import mindustry.entities.units.*; import mindustry.graphics.*; @@ -375,27 +376,27 @@ public class ItemBridge extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeInt(link); - stream.writeFloat(uptime); - stream.writeByte(incoming.size); + public void write(Writes write){ + super.write(write); + write.i(link); + write.f(uptime); + write.b(incoming.size); IntSetIterator it = incoming.iterator(); while(it.hasNext){ - stream.writeInt(it.next()); + write.i(it.next()); } } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - link = stream.readInt(); - uptime = stream.readFloat(); - byte links = stream.readByte(); + public void read(Reads read, byte revision){ + super.read(read, revision); + link = read.i(); + uptime = read.f(); + byte links = read.b(); for(int i = 0; i < links; i++){ - incoming.add(stream.readInt()); + incoming.add(read.i()); } } } diff --git a/core/src/mindustry/world/blocks/distribution/Junction.java b/core/src/mindustry/world/blocks/distribution/Junction.java index 60fb690cec..cb26a2e936 100644 --- a/core/src/mindustry/world/blocks/distribution/Junction.java +++ b/core/src/mindustry/world/blocks/distribution/Junction.java @@ -1,6 +1,7 @@ package mindustry.world.blocks.distribution; import arc.util.Time; +import arc.util.io.*; import mindustry.gen.*; import mindustry.gen.BufferItem; import mindustry.type.Item; @@ -9,8 +10,6 @@ import mindustry.world.DirectionalItemBuffer; import mindustry.world.Tile; import mindustry.world.meta.BlockGroup; -import java.io.DataInput; -import java.io.DataOutput; import java.io.IOException; import static mindustry.Vars.content; @@ -89,15 +88,15 @@ public class Junction extends Block{ DirectionalItemBuffer buffer = new DirectionalItemBuffer(capacity, speed); @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - buffer.write(stream); + public void write(Writes write){ + super.write(write); + buffer.write(write); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - buffer.read(stream); + public void read(Reads read, byte revision){ + super.read(read, revision); + buffer.read(read); } } } diff --git a/core/src/mindustry/world/blocks/distribution/MassDriver.java b/core/src/mindustry/world/blocks/distribution/MassDriver.java index bf54163b1d..1da475939a 100644 --- a/core/src/mindustry/world/blocks/distribution/MassDriver.java +++ b/core/src/mindustry/world/blocks/distribution/MassDriver.java @@ -5,6 +5,7 @@ import arc.struct.*; import arc.graphics.g2d.*; import arc.math.*; import arc.util.*; +import arc.util.io.*; import arc.util.pooling.Pool.*; import arc.util.pooling.*; import mindustry.content.*; @@ -332,19 +333,19 @@ public class MassDriver extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeInt(link); - stream.writeFloat(rotation); - stream.writeByte((byte)state.ordinal()); + public void write(Writes write){ + super.write(write); + write.i(link); + write.f(rotation); + write.b((byte)state.ordinal()); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - link = stream.readInt(); - rotation = stream.readFloat(); - state = DriverState.values()[stream.readByte()]; + public void read(Reads read, byte revision){ + super.read(read, revision); + link = read.i(); + rotation = read.f(); + state = DriverState.values()[read.b()]; } } diff --git a/core/src/mindustry/world/blocks/distribution/OverflowGate.java b/core/src/mindustry/world/blocks/distribution/OverflowGate.java index 4de9226f58..56334a39e0 100644 --- a/core/src/mindustry/world/blocks/distribution/OverflowGate.java +++ b/core/src/mindustry/world/blocks/distribution/OverflowGate.java @@ -2,6 +2,7 @@ package mindustry.world.blocks.distribution; import arc.math.*; import arc.util.*; +import arc.util.io.*; import mindustry.gen.*; import mindustry.type.*; import mindustry.world.*; @@ -124,10 +125,10 @@ public class OverflowGate extends Block{ float time; @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); + public void read(Reads read, byte revision){ + super.read(read, revision); if(revision == 1){ - new DirectionalItemBuffer(25, 50f).read(stream); + new DirectionalItemBuffer(25, 50f).read(read); } } } diff --git a/core/src/mindustry/world/blocks/distribution/Sorter.java b/core/src/mindustry/world/blocks/distribution/Sorter.java index d686b3ca8b..73de1a36d4 100644 --- a/core/src/mindustry/world/blocks/distribution/Sorter.java +++ b/core/src/mindustry/world/blocks/distribution/Sorter.java @@ -5,6 +5,7 @@ import arc.math.*; import arc.scene.ui.layout.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; import mindustry.gen.*; import mindustry.entities.units.*; import mindustry.type.*; @@ -156,18 +157,18 @@ public class Sorter extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeShort(sortItem == null ? -1 : sortItem.id); + public void write(Writes write){ + super.write(write); + write.s(sortItem == null ? -1 : sortItem.id); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - sortItem = content.item(stream.readShort()); + public void read(Reads read, byte revision){ + super.read(read, revision); + sortItem = content.item(read.s()); if(revision == 1){ - new DirectionalItemBuffer(20, 45f).read(stream); + new DirectionalItemBuffer(20, 45f).read(read); } } } diff --git a/core/src/mindustry/world/blocks/logic/MessageBlock.java b/core/src/mindustry/world/blocks/logic/MessageBlock.java index 3a8c4b6080..51f1fc088f 100644 --- a/core/src/mindustry/world/blocks/logic/MessageBlock.java +++ b/core/src/mindustry/world/blocks/logic/MessageBlock.java @@ -1,7 +1,6 @@ package mindustry.world.blocks.logic; import arc.*; -import mindustry.annotations.Annotations.*; import arc.Input.*; import arc.graphics.*; import arc.graphics.g2d.*; @@ -9,7 +8,9 @@ import arc.math.geom.*; import arc.scene.ui.*; import arc.scene.ui.layout.*; import arc.util.*; +import arc.util.io.*; import arc.util.pooling.*; +import mindustry.annotations.Annotations.*; import mindustry.entities.*; import mindustry.gen.*; import mindustry.net.*; @@ -17,8 +18,6 @@ import mindustry.ui.*; import mindustry.ui.dialogs.*; import mindustry.world.*; -import java.io.*; - import static mindustry.Vars.*; public class MessageBlock extends Block{ @@ -150,15 +149,15 @@ public class MessageBlock extends Block{ public String[] lines = {""}; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeUTF(message); + public void write(Writes write){ + super.write(write); + write.str(message); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - message = stream.readUTF(); + public void read(Reads read, byte revision){ + super.read(read, revision); + message = read.str(); } } } diff --git a/core/src/mindustry/world/blocks/power/ImpactReactor.java b/core/src/mindustry/world/blocks/power/ImpactReactor.java index 3893f87a02..f323c15687 100644 --- a/core/src/mindustry/world/blocks/power/ImpactReactor.java +++ b/core/src/mindustry/world/blocks/power/ImpactReactor.java @@ -5,6 +5,7 @@ import arc.graphics.*; import arc.graphics.g2d.*; import arc.math.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.entities.*; import mindustry.game.EventType.*; @@ -165,15 +166,15 @@ public class ImpactReactor extends PowerGenerator{ public float warmup; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(warmup); + public void write(Writes write){ + super.write(write); + write.f(warmup); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - warmup = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + warmup = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/power/LightBlock.java b/core/src/mindustry/world/blocks/power/LightBlock.java index 9cfe3fd1b5..c8861c4a32 100644 --- a/core/src/mindustry/world/blocks/power/LightBlock.java +++ b/core/src/mindustry/world/blocks/power/LightBlock.java @@ -4,6 +4,7 @@ import arc.graphics.*; import arc.graphics.g2d.*; import arc.scene.ui.layout.*; import arc.util.*; +import arc.util.io.*; import mindustry.gen.*; import mindustry.graphics.*; import mindustry.world.*; @@ -80,15 +81,15 @@ public class LightBlock extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeInt(color); + public void write(Writes write){ + super.write(write); + write.i(color); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - color = stream.readInt(); + public void read(Reads read, byte revision){ + super.read(read, revision); + color = read.i(); } } } diff --git a/core/src/mindustry/world/blocks/power/NuclearReactor.java b/core/src/mindustry/world/blocks/power/NuclearReactor.java index 8a1ab3f1e8..e1c9d20339 100644 --- a/core/src/mindustry/world/blocks/power/NuclearReactor.java +++ b/core/src/mindustry/world/blocks/power/NuclearReactor.java @@ -6,6 +6,7 @@ import arc.graphics.g2d.*; import arc.math.*; import arc.math.geom.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.entities.*; import mindustry.game.EventType.*; @@ -186,15 +187,15 @@ public class NuclearReactor extends PowerGenerator{ public float flash; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(heat); + public void write(Writes write){ + super.write(write); + write.f(heat); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - heat = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + heat = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/power/PowerGenerator.java b/core/src/mindustry/world/blocks/power/PowerGenerator.java index 835c925894..ac35364e0f 100644 --- a/core/src/mindustry/world/blocks/power/PowerGenerator.java +++ b/core/src/mindustry/world/blocks/power/PowerGenerator.java @@ -3,6 +3,7 @@ package mindustry.world.blocks.power; import arc.Core; import arc.struct.EnumSet; import arc.util.Strings; +import arc.util.io.*; import mindustry.gen.*; import mindustry.graphics.Pal; import mindustry.ui.Bar; @@ -59,15 +60,15 @@ public class PowerGenerator extends PowerDistributor{ public float productionEfficiency = 0.0f; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(productionEfficiency); + public void write(Writes write){ + super.write(write); + write.f(productionEfficiency); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - productionEfficiency = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + productionEfficiency = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/production/Cultivator.java b/core/src/mindustry/world/blocks/production/Cultivator.java index e8dd4d05b4..26cd4c61ed 100644 --- a/core/src/mindustry/world/blocks/production/Cultivator.java +++ b/core/src/mindustry/world/blocks/production/Cultivator.java @@ -5,6 +5,7 @@ import arc.graphics.*; import arc.graphics.g2d.*; import arc.math.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.gen.*; import mindustry.graphics.*; @@ -120,15 +121,15 @@ public class Cultivator extends GenericCrafter{ public float boost; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(warmup); + public void write(Writes write){ + super.write(write); + write.f(warmup); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - warmup = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + warmup = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/production/GenericCrafter.java b/core/src/mindustry/world/blocks/production/GenericCrafter.java index 8dacbb6710..657e100ec5 100644 --- a/core/src/mindustry/world/blocks/production/GenericCrafter.java +++ b/core/src/mindustry/world/blocks/production/GenericCrafter.java @@ -4,6 +4,7 @@ import arc.func.*; import arc.graphics.g2d.*; import arc.math.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.entities.*; import mindustry.gen.*; @@ -151,17 +152,17 @@ public class GenericCrafter extends Block{ public float warmup; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(progress); - stream.writeFloat(warmup); + public void write(Writes write){ + super.write(write); + write.f(progress); + write.f(warmup); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - progress = stream.readFloat(); - warmup = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + progress = read.f(); + warmup = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/sandbox/ItemSource.java b/core/src/mindustry/world/blocks/sandbox/ItemSource.java index c03cbe5b6f..f68653e2df 100644 --- a/core/src/mindustry/world/blocks/sandbox/ItemSource.java +++ b/core/src/mindustry/world/blocks/sandbox/ItemSource.java @@ -4,6 +4,7 @@ import arc.*; import arc.graphics.g2d.*; import arc.scene.ui.layout.*; import arc.util.*; +import arc.util.io.*; import mindustry.gen.*; import mindustry.entities.units.*; import mindustry.type.*; @@ -101,15 +102,15 @@ public class ItemSource extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeShort(outputItem == null ? -1 : outputItem.id); + public void write(Writes write){ + super.write(write); + write.s(outputItem == null ? -1 : outputItem.id); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - outputItem = content.item(stream.readShort()); + public void read(Reads read, byte revision){ + super.read(read, revision); + outputItem = content.item(read.s()); } } } diff --git a/core/src/mindustry/world/blocks/sandbox/LiquidSource.java b/core/src/mindustry/world/blocks/sandbox/LiquidSource.java index 11ebd2f9ce..31740856ea 100644 --- a/core/src/mindustry/world/blocks/sandbox/LiquidSource.java +++ b/core/src/mindustry/world/blocks/sandbox/LiquidSource.java @@ -5,6 +5,7 @@ import arc.graphics.g2d.*; import arc.scene.ui.layout.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; import mindustry.entities.units.*; import mindustry.gen.*; import mindustry.type.*; @@ -97,15 +98,15 @@ public class LiquidSource extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeByte(source == null ? -1 : source.id); + public void write(Writes write){ + super.write(write); + write.b(source == null ? -1 : source.id); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - byte id = stream.readByte(); + public void read(Reads read, byte revision){ + super.read(read, revision); + byte id = read.b(); source = id == -1 ? null : content.liquid(id); } } diff --git a/core/src/mindustry/world/blocks/storage/Unloader.java b/core/src/mindustry/world/blocks/storage/Unloader.java index 7b2f0d95c9..15d503079b 100644 --- a/core/src/mindustry/world/blocks/storage/Unloader.java +++ b/core/src/mindustry/world/blocks/storage/Unloader.java @@ -4,6 +4,7 @@ import arc.graphics.*; import arc.graphics.g2d.*; import arc.scene.ui.layout.*; import arc.util.*; +import arc.util.io.*; import mindustry.gen.*; import mindustry.entities.units.*; import mindustry.type.*; @@ -138,15 +139,15 @@ public class Unloader extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeByte(sortItem == null ? -1 : sortItem.id); + public void write(Writes write){ + super.write(write); + write.b(sortItem == null ? -1 : sortItem.id); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - byte id = stream.readByte(); + public void read(Reads read, byte revision){ + super.read(read, revision); + byte id = read.b(); sortItem = id == -1 ? null : content.items().get(id); } } diff --git a/core/src/mindustry/world/blocks/units/CommandCenter.java b/core/src/mindustry/world/blocks/units/CommandCenter.java index d31215b765..b4f62c5942 100644 --- a/core/src/mindustry/world/blocks/units/CommandCenter.java +++ b/core/src/mindustry/world/blocks/units/CommandCenter.java @@ -8,6 +8,7 @@ import arc.scene.ui.*; import arc.scene.ui.layout.*; import arc.struct.*; import arc.util.*; +import arc.util.io.*; import mindustry.content.*; import mindustry.entities.*; import mindustry.gen.*; @@ -126,15 +127,15 @@ public class CommandCenter extends Block{ } @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeByte(command.ordinal()); + public void write(Writes write){ + super.write(write); + write.b(command.ordinal()); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - command = UnitCommand.all[stream.readByte()]; + public void read(Reads read, byte revision){ + super.read(read, revision); + command = UnitCommand.all[read.b()]; } } } diff --git a/core/src/mindustry/world/blocks/units/MechPad.java b/core/src/mindustry/world/blocks/units/MechPad.java index 752ab283f5..4567ce39f4 100644 --- a/core/src/mindustry/world/blocks/units/MechPad.java +++ b/core/src/mindustry/world/blocks/units/MechPad.java @@ -6,6 +6,7 @@ import arc.math.geom.*; import arc.struct.*; import arc.util.ArcAnnotate.*; import arc.util.*; +import arc.util.io.*; import mindustry.annotations.Annotations.*; import mindustry.content.*; import mindustry.gen.*; @@ -140,19 +141,19 @@ public class MechPad extends Block{ float heat; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(progress); - stream.writeFloat(time); - stream.writeFloat(heat); + public void write(Writes write){ + super.write(write); + write.f(progress); + write.f(time); + write.f(heat); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - progress = stream.readFloat(); - time = stream.readFloat(); - heat = stream.readFloat(); + public void read(Reads read, byte revision){ + super.read(read, revision); + progress = read.f(); + time = read.f(); + heat = read.f(); } } } diff --git a/core/src/mindustry/world/blocks/units/UnitFactory.java b/core/src/mindustry/world/blocks/units/UnitFactory.java index 412adcd0d5..7f09a66ff4 100644 --- a/core/src/mindustry/world/blocks/units/UnitFactory.java +++ b/core/src/mindustry/world/blocks/units/UnitFactory.java @@ -4,6 +4,7 @@ import arc.*; import arc.graphics.g2d.*; import arc.math.*; import arc.struct.*; +import arc.util.io.*; import mindustry.*; import mindustry.annotations.Annotations.*; import mindustry.content.*; @@ -189,17 +190,17 @@ public class UnitFactory extends Block{ int spawned; @Override - public void write(DataOutput stream) throws IOException{ - super.write(stream); - stream.writeFloat(buildTime); - stream.writeInt(spawned); + public void write(Writes write){ + super.write(write); + write.f(buildTime); + write.i(spawned); } @Override - public void read(DataInput stream, byte revision) throws IOException{ - super.read(stream, revision); - buildTime = stream.readFloat(); - spawned = stream.readInt(); + public void read(Reads read, byte revision){ + super.read(read, revision); + buildTime = read.f(); + spawned = read.i(); } } } diff --git a/core/src/mindustry/world/modules/BlockModule.java b/core/src/mindustry/world/modules/BlockModule.java index 6d00713944..974f43656f 100644 --- a/core/src/mindustry/world/modules/BlockModule.java +++ b/core/src/mindustry/world/modules/BlockModule.java @@ -1,10 +1,9 @@ package mindustry.world.modules; -import java.io.*; +import arc.util.io.*; /** A class that represents compartmentalized tile entity state. */ public abstract class BlockModule{ - public abstract void write(DataOutput stream) throws IOException; - - public abstract void read(DataInput stream) throws IOException; + public abstract void write(Writes write); + public abstract void read(Reads read); } diff --git a/core/src/mindustry/world/modules/ConsumeModule.java b/core/src/mindustry/world/modules/ConsumeModule.java index efbc7bec17..5ea2899b38 100644 --- a/core/src/mindustry/world/modules/ConsumeModule.java +++ b/core/src/mindustry/world/modules/ConsumeModule.java @@ -1,10 +1,9 @@ package mindustry.world.modules; +import arc.util.io.*; import mindustry.gen.*; import mindustry.world.consumers.Consume; -import java.io.*; - public class ConsumeModule extends BlockModule{ private boolean valid, optionalValid; private final Tilec entity; @@ -59,12 +58,12 @@ public class ConsumeModule extends BlockModule{ } @Override - public void write(DataOutput stream) throws IOException{ - stream.writeBoolean(valid); + public void write(Writes write){ + write.bool(valid); } @Override - public void read(DataInput stream) throws IOException{ - valid = stream.readBoolean(); + public void read(Reads read){ + valid = read.bool(); } } diff --git a/core/src/mindustry/world/modules/ItemModule.java b/core/src/mindustry/world/modules/ItemModule.java index cd30730d2d..df2c58fc9d 100644 --- a/core/src/mindustry/world/modules/ItemModule.java +++ b/core/src/mindustry/world/modules/ItemModule.java @@ -1,9 +1,9 @@ package mindustry.world.modules; +import arc.util.io.*; import mindustry.type.Item; import mindustry.type.ItemStack; -import java.io.*; import java.util.Arrays; import static mindustry.Vars.content; @@ -121,32 +121,32 @@ public class ItemModule extends BlockModule{ } @Override - public void write(DataOutput stream) throws IOException{ + public void write(Writes write){ byte amount = 0; for(int item : items){ if(item > 0) amount++; } - stream.writeByte(amount); //amount of items + write.b(amount); //amount of items for(int i = 0; i < items.length; i++){ if(items[i] > 0){ - stream.writeByte(i); //item ID - stream.writeInt(items[i]); //item amount + write.b(i); //item ID + write.i(items[i]); //item amount } } } @Override - public void read(DataInput stream) throws IOException{ + public void read(Reads read){ //just in case, reset items Arrays.fill(items, 0); - byte count = stream.readByte(); + byte count = read.b(); total = 0; for(int j = 0; j < count; j++){ - int itemid = stream.readByte(); - int itemamount = stream.readInt(); + int itemid = read.b(); + int itemamount = read.i(); items[content.item(itemid).id] = itemamount; total += itemamount; } diff --git a/core/src/mindustry/world/modules/LiquidModule.java b/core/src/mindustry/world/modules/LiquidModule.java index eaaa22a07f..7615aad033 100644 --- a/core/src/mindustry/world/modules/LiquidModule.java +++ b/core/src/mindustry/world/modules/LiquidModule.java @@ -1,9 +1,9 @@ package mindustry.world.modules; import arc.math.*; +import arc.util.io.*; import mindustry.type.Liquid; -import java.io.*; import java.util.Arrays; import static mindustry.Vars.content; @@ -83,31 +83,31 @@ public class LiquidModule extends BlockModule{ } @Override - public void write(DataOutput stream) throws IOException{ + public void write(Writes write){ byte amount = 0; for(float liquid : liquids){ if(liquid > 0) amount++; } - stream.writeByte(amount); //amount of liquids + write.b(amount); //amount of liquids for(int i = 0; i < liquids.length; i++){ if(liquids[i] > 0){ - stream.writeByte(i); //liquid ID - stream.writeFloat(liquids[i]); //item amount + write.b(i); //liquid ID + write.f(liquids[i]); //item amount } } } @Override - public void read(DataInput stream) throws IOException{ + public void read(Reads read){ Arrays.fill(liquids, 0); total = 0f; - byte count = stream.readByte(); + byte count = read.b(); for(int j = 0; j < count; j++){ - int liquidid = stream.readByte(); - float amount = stream.readFloat(); + int liquidid = read.b(); + float amount = read.f(); liquids[liquidid] = amount; if(amount > 0){ current = content.liquid(liquidid); diff --git a/core/src/mindustry/world/modules/PowerModule.java b/core/src/mindustry/world/modules/PowerModule.java index e1500f85e7..ac503ab825 100644 --- a/core/src/mindustry/world/modules/PowerModule.java +++ b/core/src/mindustry/world/modules/PowerModule.java @@ -1,12 +1,9 @@ package mindustry.world.modules; import arc.struct.IntArray; +import arc.util.io.*; import mindustry.world.blocks.power.PowerGraph; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; - public class PowerModule extends BlockModule{ /** * In case of unbuffered consumers, this is the percentage (1.0f = 100%) of the demanded power which can be supplied. @@ -18,22 +15,22 @@ public class PowerModule extends BlockModule{ public IntArray links = new IntArray(); @Override - public void write(DataOutput stream) throws IOException{ - stream.writeShort(links.size); + public void write(Writes write){ + write.s(links.size); for(int i = 0; i < links.size; i++){ - stream.writeInt(links.get(i)); + write.i(links.get(i)); } - stream.writeFloat(status); + write.f(status); } @Override - public void read(DataInput stream) throws IOException{ + public void read(Reads read){ links.clear(); - short amount = stream.readShort(); + short amount = read.s(); for(int i = 0; i < amount; i++){ - links.add(stream.readInt()); + links.add(read.i()); } - status = stream.readFloat(); + status = read.f(); if(Float.isNaN(status) || Float.isInfinite(status)) status = 0f; } } diff --git a/gradle.properties b/gradle.properties index 61f26a68e4..3c2c6f7a16 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ org.gradle.daemon=true org.gradle.jvmargs=-Xms256m -Xmx1024m -archash=d271474a36e1b68887bdb7520bf683c5aa832e79 +archash=9a946c98476bfbae80840608862e934f4c66b6cd diff --git a/tests/src/test/java/IOTests.java b/tests/src/test/java/IOTests.java index 79f7a213cf..e105bffe3d 100644 --- a/tests/src/test/java/IOTests.java +++ b/tests/src/test/java/IOTests.java @@ -1,14 +1,23 @@ import arc.util.*; +import arc.util.io.*; +import mindustry.content.*; import mindustry.game.*; +import mindustry.gen.*; import mindustry.io.*; import org.junit.jupiter.api.*; +import java.io.*; import java.nio.*; import static org.junit.jupiter.api.Assertions.*; public class IOTests{ + @Test + void writeEntities(){ + //TODO + } + @Test void writeEnglish(){ ByteBuffer buffer = ByteBuffer.allocate(500);