From 0bd0602655c1c121bd700bce2042771780b4cdf6 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 8 Jun 2018 10:02:15 -0400 Subject: [PATCH] Fixed build errors --- build.gradle | 2 +- .../io/anuke/mindustry/core/NetServer.java | 66 +++++++++++++++++-- .../io/anuke/mindustry/net/NetConnection.java | 7 ++ .../src/io/anuke/mindustry/net/NetEvents.java | 12 ++++ core/src/io/anuke/mindustry/net/Packets.java | 2 +- 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 62edd576a5..0f9d78ad27 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,7 @@ allprojects { gdxVersion = '1.9.8' roboVMVersion = '2.3.0' aiVersion = '1.8.1' - uCoreVersion = '32c8405' + uCoreVersion = '382d7b2' getVersionString = { String buildVersion = getBuildVersion() diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index 3d492cf7f0..c6b6d3fb4f 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -7,6 +7,8 @@ import io.anuke.annotations.Annotations.Remote; import io.anuke.mindustry.content.Mechs; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.entities.Player; +import io.anuke.mindustry.entities.traits.SyncTrait; +import io.anuke.mindustry.gen.Call; import io.anuke.mindustry.gen.RemoteReadServer; import io.anuke.mindustry.io.Version; import io.anuke.mindustry.net.*; @@ -15,12 +17,18 @@ import io.anuke.mindustry.net.Packets.*; import io.anuke.ucore.core.Timers; import io.anuke.ucore.entities.Entities; import io.anuke.ucore.entities.EntityGroup; +import io.anuke.ucore.entities.trait.Entity; +import io.anuke.ucore.io.delta.ByteDeltaEncoder; +import io.anuke.ucore.io.delta.ByteMatcherHash; +import io.anuke.ucore.io.delta.DEZEncoder; import io.anuke.ucore.modules.Module; import io.anuke.ucore.util.Log; import io.anuke.ucore.util.Timer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; import static io.anuke.mindustry.Vars.*; @@ -37,6 +45,13 @@ public class NetServer extends Module{ private boolean closing = false; private Timer timer = new Timer(5); + /**Stream for writing player sync data to.*/ + private ByteArrayOutputStream syncStream = new ByteArrayOutputStream(); + /**Data stream for writing player sync data to.*/ + private DataOutputStream dataStream = new DataOutputStream(syncStream); + /**Encoder for computing snapshot deltas.*/ + private DEZEncoder encoder = new DEZEncoder(); + public NetServer(){ Net.handleServer(Connect.class, (id, connect) -> { @@ -170,19 +185,56 @@ public class NetServer extends Module{ } void sync(){ - //TODO implement snapshot packets w/ delta compression + try { + //TODO implement snapshot packets w/ delta compression + //iterate through each player + for (Player player : connections.values()) { + //reset stream to begin writing + syncStream.reset(); + int totalGroups = 0; - //iterate through each player - for(Player player : connections.values()){ + for (EntityGroup group : Entities.getAllGroups()) { + if (!group.isEmpty() && (group.all().get(0) instanceof SyncTrait)) totalGroups ++; + } - //check for syncable groups. - for(EntityGroup group : Entities.getAllGroups()){ - if(group.isEmpty()) continue; + //write total amount of serializable groups + dataStream.writeByte(totalGroups); + + //check for syncable groups + for (EntityGroup group : Entities.getAllGroups()) { + //TODO range-check sync positions? + if (group.isEmpty() || !(group.all().get(0) instanceof SyncTrait)) continue; + + //write group ID + group size + dataStream.writeByte(group.getID()); + dataStream.writeShort(group.size()); + + for(Entity entity : group.all()){ + //write all entities now + ((SyncTrait)entity).write(dataStream); + } + } + + NetConnection connection = Net.getConnection(player.clientid); + + byte[] bytes = syncStream.toByteArray(); + if(connection.lastSnapshot == null){ + //no snapshot to diff, send it all + Call.onSnapshot(connection.id, bytes, -1); + }else{ + //send diff otherwise + byte[] diff = ByteDeltaEncoder.toDiff(new ByteMatcherHash(connection.lastSnapshot, bytes), encoder); + Call.onSnapshot(connection.id, diff, connection.lastSnapshotID); + + connection.lastSentSnapshot = bytes; + } } - } + }catch (IOException e){ + e.printStackTrace(); + } } @Remote(server = false) diff --git a/core/src/io/anuke/mindustry/net/NetConnection.java b/core/src/io/anuke/mindustry/net/NetConnection.java index ba6bfbf80c..c326e862af 100644 --- a/core/src/io/anuke/mindustry/net/NetConnection.java +++ b/core/src/io/anuke/mindustry/net/NetConnection.java @@ -6,6 +6,13 @@ public abstract class NetConnection { public final int id; public final String address; + /**ID of last snapshot this connection is guaranteed to have recieved.*/ + public int lastSnapshotID; + /**Byte array of last sent snapshot data that is confirmed to be recieved..*/ + public byte[] lastSnapshot; + /**Byte array of last sent snapshot.*/ + public byte[] lastSentSnapshot; + public NetConnection(int id, String address){ this.id = id; this.address = address; diff --git a/core/src/io/anuke/mindustry/net/NetEvents.java b/core/src/io/anuke/mindustry/net/NetEvents.java index 2b338fc885..9e33132648 100644 --- a/core/src/io/anuke/mindustry/net/NetEvents.java +++ b/core/src/io/anuke/mindustry/net/NetEvents.java @@ -1,5 +1,17 @@ package io.anuke.mindustry.net; +import io.anuke.annotations.Annotations.Remote; +import io.anuke.mindustry.entities.Player; + public class NetEvents { + @Remote(one = true, all = false, unreliable = true) + public static void onSnapshot(byte[] snapshot, int snapshotID){ + + } + + @Remote(unreliable = true, server = false) + public static void onRecievedSnapshot(Player player, int snapshotID){ + Net.getConnection(player.clientid).lastSnapshotID = snapshotID; + } } diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index 80478adce6..bef9a2c79a 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -9,7 +9,7 @@ import io.anuke.mindustry.net.Packet.ImportantPacket; import io.anuke.mindustry.net.Packet.UnimportantPacket; import io.anuke.ucore.io.ByteBufferOutput; import io.anuke.ucore.io.IOUtils; -import io.anuke.ucore.io.delta.ByteBufferInput; +import io.anuke.ucore.io.ByteBufferInput; import java.nio.ByteBuffer;