Added version check, fixed chat being open in singleplayer

This commit is contained in:
Anuken
2018-01-21 10:38:34 -05:00
parent 33dafa77d8
commit 40f7bf51c2
10 changed files with 58 additions and 47 deletions

View File

@ -22,6 +22,7 @@ text.server.mismatch=Packet error: possible client/server version mismatch.\nMak
text.server.closing=[accent]Closing server... text.server.closing=[accent]Closing server...
text.server.kicked.kick=You have been kicked from the server! text.server.kicked.kick=You have been kicked from the server!
text.server.kicked.invalidPassword=Invalid password! text.server.kicked.invalidPassword=Invalid password!
text.server.kicked.outdated=Outdated client! Update your game!
text.server.connected={0} has joined. text.server.connected={0} has joined.
text.server.disconnected={0} has disconnected. text.server.disconnected={0} has disconnected.
text.nohost=Can't host server on a custom map! text.nohost=Can't host server on a custom map!

View File

@ -477,7 +477,7 @@ public class Control extends Module{
addItem(Item.stone, 40); addItem(Item.stone, 40);
if(debug){ if(debug){
Arrays.fill(items, 999900); Arrays.fill(items, 99999);
} }
} }
@ -611,7 +611,7 @@ public class Control extends Module{
} }
if(Inputs.keyTap(Keys.G)){ if(Inputs.keyTap(Keys.G)){
world.pathfinder().benchmark(); addItem(Item.stone, 1000);
} }
if(Inputs.keyDown(Keys.I)){ if(Inputs.keyDown(Keys.I)){
@ -653,8 +653,12 @@ public class Control extends Module{
ui.paused.hide(); ui.paused.hide();
GameState.set(State.playing); GameState.set(State.playing);
}else if (!ui.restart.isShown()){ }else if (!ui.restart.isShown()){
ui.paused.show(); if(ui.chatfrag.chatOpen()) {
GameState.set(State.paused); ui.chatfrag.hide();
}else{
ui.paused.show();
GameState.set(State.paused);
}
} }
} }

View File

@ -46,6 +46,11 @@ public class NetServer extends Module{
Net.handleServer(ConnectPacket.class, (id, packet) -> { Net.handleServer(ConnectPacket.class, (id, packet) -> {
if(packet.version != Net.version){
Net.kickConnection(id, KickReason.outdated);
return;
}
UCore.log("Sending world data to client (ID=" + id + ")"); UCore.log("Sending world data to client (ID=" + id + ")");
Player player = new Player(); Player player = new Player();
@ -83,7 +88,7 @@ public class NetServer extends Module{
Player player = connections.get(packet.id); Player player = connections.get(packet.id);
if (player == null) { if (player == null) {
sendMessage("[accent]" + Bundles.format("text.server.disconnected", "<???>")); Gdx.app.error("Mindustry", "Unknown client has disconnected (ID=" + id + ")");
return; return;
} }

View File

@ -8,6 +8,7 @@ import com.badlogic.gdx.utils.async.AsyncExecutor;
import io.anuke.mindustry.Mindustry; import io.anuke.mindustry.Mindustry;
import io.anuke.mindustry.net.Packets.Connect; import io.anuke.mindustry.net.Packets.Connect;
import io.anuke.mindustry.net.Packets.Disconnect; import io.anuke.mindustry.net.Packets.Disconnect;
import io.anuke.mindustry.net.Packets.KickReason;
import io.anuke.mindustry.net.Streamable.StreamBegin; import io.anuke.mindustry.net.Streamable.StreamBegin;
import io.anuke.mindustry.net.Streamable.StreamBuilder; import io.anuke.mindustry.net.Streamable.StreamBuilder;
import io.anuke.mindustry.net.Streamable.StreamChunk; import io.anuke.mindustry.net.Streamable.StreamChunk;
@ -19,6 +20,8 @@ import io.anuke.ucore.function.Consumer;
import java.io.IOException; import java.io.IOException;
public class Net{ public class Net{
public static final int version = 9;
private static boolean server; private static boolean server;
private static boolean active; private static boolean active;
private static boolean clientLoaded; private static boolean clientLoaded;
@ -79,8 +82,8 @@ public class Net{
} }
/**Kick a specified connection from the server.*/ /**Kick a specified connection from the server.*/
public static void kickConnection(int id){ public static void kickConnection(int id, KickReason reason){
serverProvider.kick(id); serverProvider.kick(id, reason);
} }
/**Returns a list of all connections IDs.*/ /**Returns a list of all connections IDs.*/
@ -249,7 +252,7 @@ public class Net{
/**Return all connected users.*/ /**Return all connected users.*/
Array<? extends NetConnection> getConnections(); Array<? extends NetConnection> getConnections();
/**Kick a certain connection.*/ /**Kick a certain connection.*/
void kick(int connection); void kick(int connection, KickReason reason);
/**Returns the ping for a certain connection.*/ /**Returns the ping for a certain connection.*/
int getPingFor(NetConnection connection); int getPingFor(NetConnection connection);
/**Register classes to be sent.*/ /**Register classes to be sent.*/

View File

@ -19,14 +19,10 @@ import io.anuke.ucore.entities.Entities;
import java.io.*; import java.io.*;
public class NetworkIO { public class NetworkIO {
private static final int fileVersionID = 16;
public static void write(int playerID, ByteArray upgrades, OutputStream os){ public static void write(int playerID, ByteArray upgrades, OutputStream os){
try(DataOutputStream stream = new DataOutputStream(os)){ try(DataOutputStream stream = new DataOutputStream(os)){
//--META--
stream.writeInt(fileVersionID); //version id
stream.writeFloat(Timers.time()); //timer time stream.writeFloat(Timers.time()); //timer time
stream.writeLong(TimeUtils.millis()); //timestamp stream.writeLong(TimeUtils.millis()); //timestamp
@ -151,17 +147,11 @@ public class NetworkIO {
public static void load(InputStream is){ public static void load(InputStream is){
try(DataInputStream stream = new DataInputStream(is)){ try(DataInputStream stream = new DataInputStream(is)){
int version = stream.readInt();
float timerTime = stream.readFloat(); float timerTime = stream.readFloat();
long timestamp = stream.readLong(); long timestamp = stream.readLong();
Timers.resetTime(timerTime + (TimeUtils.timeSinceMillis(timestamp) / 1000f) * 60f); Timers.resetTime(timerTime + (TimeUtils.timeSinceMillis(timestamp) / 1000f) * 60f);
if(version != fileVersionID){
throw new RuntimeException("Netcode version mismatch!");
}
//general state //general state
byte mode = stream.readByte(); byte mode = stream.readByte();
byte mapid = stream.readByte(); byte mapid = stream.readByte();

View File

@ -44,11 +44,13 @@ public class Packets {
} }
public static class ConnectPacket implements Packet{ public static class ConnectPacket implements Packet{
public int version;
public String name; public String name;
public boolean android; public boolean android;
@Override @Override
public void write(ByteBuffer buffer) { public void write(ByteBuffer buffer) {
buffer.putInt(Net.version);
buffer.put((byte)name.getBytes().length); buffer.put((byte)name.getBytes().length);
buffer.put(name.getBytes()); buffer.put(name.getBytes());
buffer.put(android ? (byte)1 : 0); buffer.put(android ? (byte)1 : 0);
@ -56,6 +58,7 @@ public class Packets {
@Override @Override
public void read(ByteBuffer buffer) { public void read(ByteBuffer buffer) {
version = buffer.getInt();
byte length = buffer.get(); byte length = buffer.get();
byte[] bytes = new byte[length]; byte[] bytes = new byte[length];
buffer.get(bytes); buffer.get(bytes);
@ -394,7 +397,7 @@ public class Packets {
} }
public enum KickReason{ public enum KickReason{
kick, invalidPassword kick, invalidPassword, outdated
} }
public static class UpgradePacket implements Packet{ public static class UpgradePacket implements Packet{

View File

@ -17,7 +17,7 @@ import io.anuke.ucore.util.Strings;
public class JoinDialog extends FloatingDialog { public class JoinDialog extends FloatingDialog {
Array<Server> servers = new Array<>(); Array<Server> servers = new Array<>();
Dialog join; Dialog add;
Server renaming; Server renaming;
Table local = new Table(); Table local = new Table();
Table remote = new Table(); Table remote = new Table();
@ -31,24 +31,18 @@ public class JoinDialog extends FloatingDialog {
addCloseButton(); addCloseButton();
join = new FloatingDialog("$text.joingame.title"); add = new FloatingDialog("$text.joingame.title");
join.content().add("$text.joingame.ip").padRight(5f).left(); add.content().add("$text.joingame.ip").padRight(5f).left();
Mindustry.platforms.addDialog(join.content().addField(Settings.getString("ip"),text ->{
Mindustry.platforms.addDialog(add.content().addField(Settings.getString("ip"), text ->{
Settings.putString("ip", text); Settings.putString("ip", text);
Settings.save(); Settings.save();
}).size(340f, 54f).get(), 100); }).size(340f, 54f).get(), 100);
join.content().row(); add.content().row();
/* add.buttons().defaults().size(140f, 60f).pad(4f);
join.content().add("$text.server.port").left(); add.buttons().addButton("$text.cancel", add::hide);
Mindustry.platforms.addDialog(join.content() add.buttons().addButton("$text.ok", () -> {
.addField(Settings.getString("port"), new DigitsOnlyFilter(), text ->{
Settings.putString("port", text);
Settings.save();
}).size(240f, 54f).get());*/
join.buttons().defaults().size(140f, 60f).pad(4f);
join.buttons().addButton("$text.cancel", join::hide);
join.buttons().addButton("$text.ok", () -> {
if(renaming == null) { if(renaming == null) {
Server server = new Server(Settings.getString("ip"), Strings.parseInt(Settings.getString("port"))); Server server = new Server(Settings.getString("ip"), Strings.parseInt(Settings.getString("port")));
servers.add(server); servers.add(server);
@ -62,11 +56,11 @@ public class JoinDialog extends FloatingDialog {
setupRemote(); setupRemote();
refreshRemote(); refreshRemote();
} }
join.hide(); add.hide();
}).disabled(b -> Settings.getString("ip").isEmpty() || Net.active()); }).disabled(b -> Settings.getString("ip").isEmpty() || Net.active());
join.shown(() -> { add.shown(() -> {
join.getTitleLabel().setText(renaming != null ? "$text.server.edit" : "$text.server.add"); add.getTitleLabel().setText(renaming != null ? "$text.server.edit" : "$text.server.add");
}); });
setup(); setup();
@ -101,7 +95,7 @@ public class JoinDialog extends FloatingDialog {
inner.addImageButton("icon-pencil", "empty", 16*2, () -> { inner.addImageButton("icon-pencil", "empty", 16*2, () -> {
renaming = server; renaming = server;
join.show(); add.show();
}).margin(3f).padTop(6f).top().right(); }).margin(3f).padTop(6f).top().right();
inner.addImageButton("icon-trash-16", "empty", 16*2, () -> { inner.addImageButton("icon-trash-16", "empty", 16*2, () -> {
@ -182,7 +176,7 @@ public class JoinDialog extends FloatingDialog {
content().row(); content().row();
content().addCenteredImageTextButton("$text.server.add", "icon-add", "clear", 14*3, () -> { content().addCenteredImageTextButton("$text.server.add", "icon-add", "clear", 14*3, () -> {
renaming = null; renaming = null;
join.show(); add.show();
}).width(w).height(80f); }).width(w).height(80f);
} }
@ -219,7 +213,7 @@ public class JoinDialog extends FloatingDialog {
Vars.netClient.beginConnecting(); Vars.netClient.beginConnecting();
Net.connect(ip, port); Net.connect(ip, port);
hide(); hide();
join.hide(); add.hide();
}catch (Exception e) { }catch (Exception e) {
Throwable t = e; Throwable t = e;
while(t.getCause() != null){ while(t.getCause() != null){

View File

@ -49,6 +49,10 @@ public class ChatFragment extends Table implements Fragment{
//TODO put it in input? //TODO put it in input?
update(() -> { update(() -> {
if(!Net.active()){
hide();
}
if(Net.active() && Inputs.keyTap("chat")){ if(Net.active() && Inputs.keyTap("chat")){
toggle(); toggle();
} }

View File

@ -5,6 +5,7 @@ import io.anuke.mindustry.core.GameState;
import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.core.GameState.State;
import io.anuke.mindustry.entities.Player; import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.Packets.KickReason;
import io.anuke.mindustry.ui.BorderImage; import io.anuke.mindustry.ui.BorderImage;
import io.anuke.ucore.core.Draw; import io.anuke.ucore.core.Draw;
import io.anuke.ucore.core.Inputs; import io.anuke.ucore.core.Inputs;
@ -77,7 +78,7 @@ public class PlayerListFragment implements Fragment{
if(Net.server() && !player.isLocal){ if(Net.server() && !player.isLocal){
button.add().growY(); button.add().growY();
button.addImageButton("icon-cancel", 14*3, () -> button.addImageButton("icon-cancel", 14*3, () ->
Net.kickConnection(player.clientid) Net.kickConnection(player.clientid, KickReason.kick)
).pad(-5).padBottom(-10).size(h+10, h+14); ).pad(-5).padBottom(-10).size(h+10, h+14);
} }

View File

@ -117,11 +117,11 @@ public class KryoServer implements ServerProvider {
} }
@Override @Override
public void kick(int connection) { public void kick(int connection, KickReason reason) {
KryoConnection con = getByID(connection); KryoConnection con = getByID(connection);
KickPacket p = new KickPacket(); KickPacket p = new KickPacket();
p.reason = KickReason.kick; p.reason = reason;
con.send(p, SendMode.tcp); con.send(p, SendMode.tcp);
Timers.runTask(1f, con::close); Timers.runTask(1f, con::close);
@ -345,10 +345,16 @@ public class KryoServer implements ServerProvider {
connections.remove(this); connections.remove(this);
} }
}else if (connection != null) { }else if (connection != null) {
if (mode == SendMode.tcp) { try {
connection.sendTCP(object); if (mode == SendMode.tcp) {
} else { connection.sendTCP(object);
connection.sendUDP(object); } else {
connection.sendUDP(object);
}
}catch (Exception e){
e.printStackTrace();
UCore.log("Disconnecting invalid client!");
connection.close();
} }
} }
} }