From 1820abe14f76815201e43aa9858067d4fd11322a Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 10 Jan 2018 17:22:19 -0500 Subject: [PATCH] Fixed some network crashes and block config problems --- .../io/anuke/mindustry/core/NetClient.java | 56 +++++++++++++------ .../io/anuke/mindustry/core/NetServer.java | 7 +++ .../anuke/mindustry/input/InputHandler.java | 2 +- core/src/io/anuke/mindustry/io/SaveIO.java | 9 +-- core/src/io/anuke/mindustry/io/Saves.java | 8 ++- core/src/io/anuke/mindustry/net/Packets.java | 5 ++ .../io/anuke/mindustry/net/Registrator.java | 1 + core/src/io/anuke/mindustry/world/Block.java | 5 ++ .../blocks/types/distribution/Sorter.java | 9 +++ .../blocks/types/distribution/Teleporter.java | 9 +++ kryonet/src/io/anuke/kryonet/KryoClient.java | 9 +-- kryonet/src/io/anuke/kryonet/KryoServer.java | 11 ++-- 12 files changed, 94 insertions(+), 37 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/NetClient.java b/core/src/io/anuke/mindustry/core/NetClient.java index b6ba0007f7..bb15ce2efe 100644 --- a/core/src/io/anuke/mindustry/core/NetClient.java +++ b/core/src/io/anuke/mindustry/core/NetClient.java @@ -200,31 +200,37 @@ public class NetClient extends Module { Net.handle(BlockUpdatePacket.class, packet -> { Tile tile = Vars.world.tile(packet.position % Vars.world.width(), packet.position / Vars.world.width()); - if(tile.entity != null){ + if(tile != null && tile.entity != null){ tile.entity.health = packet.health; } }); Net.handle(BlockSyncPacket.class, packet -> { + if(!gotEntities) return; + DataInputStream stream = new DataInputStream(packet.stream); - try{ - while(stream.available() > 0){ - int pos = stream.readInt(); + Gdx.app.postRunnable(() -> { + try { + while (stream.available() > 0) { + int pos = stream.readInt(); - Tile tile = Vars.world.tile(pos % Vars.world.width(), pos / Vars.world.width()); + //TODO what if there's no entity? + Tile tile = Vars.world.tile(pos % Vars.world.width(), pos / Vars.world.width()); - byte times = stream.readByte(); + byte times = stream.readByte(); - for(int i = 0; i < times; i ++){ - tile.entity.timer.getTimes()[i] = stream.readFloat(); + for (int i = 0; i < times; i++) { + tile.entity.timer.getTimes()[i] = stream.readFloat(); + } + + tile.entity.read(stream); } - - tile.entity.read(stream); + } catch (IOException e) { + throw new RuntimeException(e); } - }catch (IOException e){ - e.printStackTrace(); - } + }); + }); Net.handle(DisconnectPacket.class, packet -> { @@ -247,17 +253,24 @@ public class NetClient extends Module { }); Net.handle(WeaponSwitchPacket.class, packet -> { - Player player = Vars.control.playerGroup.getByID(packet.playerid); + Gdx.app.postRunnable(() -> { + Player player = Vars.control.playerGroup.getByID(packet.playerid); - if(player == null) return; + if(player == null) return; - player.weaponLeft = (Weapon)Upgrade.getByID(packet.left); - player.weaponRight = (Weapon)Upgrade.getByID(packet.right); + player.weaponLeft = (Weapon)Upgrade.getByID(packet.left); + player.weaponRight = (Weapon)Upgrade.getByID(packet.right); + }); }); Net.handle(BlockTapPacket.class, packet -> { Tile tile = Vars.world.tile(packet.position); - tile.block().tapped(tile); + if(tile != null) tile.block().tapped(tile); + }); + + Net.handle(BlockConfigPacket.class, packet -> { + Tile tile = Vars.world.tile(packet.position); + if(tile != null) tile.block().configure(tile, packet.data); }); } @@ -272,6 +285,13 @@ public class NetClient extends Module { } } + public void handleBlockConfig(Tile tile, byte data){ + BlockConfigPacket packet = new BlockConfigPacket(); + packet.data = data; + packet.position = tile.packedPosition(); + Net.send(packet, SendMode.tcp); + } + public void handleBlockTap(Tile tile){ BlockTapPacket packet = new BlockTapPacket(); packet.position = tile.packedPosition(); diff --git a/core/src/io/anuke/mindustry/core/NetServer.java b/core/src/io/anuke/mindustry/core/NetServer.java index 197a00eb9e..e15684ed2a 100644 --- a/core/src/io/anuke/mindustry/core/NetServer.java +++ b/core/src/io/anuke/mindustry/core/NetServer.java @@ -168,6 +168,13 @@ public class NetServer extends Module{ Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp); }); + + Net.handleServer(BlockConfigPacket.class, packet -> { + Tile tile = Vars.world.tile(packet.position); + if(tile != null) tile.block().configure(tile, packet.data); + + Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp); + }); } public void sendMessage(String message){ diff --git a/core/src/io/anuke/mindustry/input/InputHandler.java b/core/src/io/anuke/mindustry/input/InputHandler.java index 8de0abe0ea..12de513138 100644 --- a/core/src/io/anuke/mindustry/input/InputHandler.java +++ b/core/src/io/anuke/mindustry/input/InputHandler.java @@ -186,7 +186,7 @@ public abstract class InputHandler extends InputAdapter{ placeBlockInternal(x, y, result, rotation, effects, sound); - if(Net.active()){ + if(Net.active() && result != ProductionBlocks.core){ Vars.netClient.handlePlace(x, y, result, rotation); } } diff --git a/core/src/io/anuke/mindustry/io/SaveIO.java b/core/src/io/anuke/mindustry/io/SaveIO.java index 3f123e505b..135c9b4bf6 100644 --- a/core/src/io/anuke/mindustry/io/SaveIO.java +++ b/core/src/io/anuke/mindustry/io/SaveIO.java @@ -74,6 +74,7 @@ public class SaveIO{ } } + /**Returns whether or not conversion was succesful.*/ public static boolean checkConvert(int slot){ try{ @@ -88,12 +89,12 @@ public class SaveIO{ target.read(stream); stream.close(); saveToSlot(slot); - return true; } - return false; - }catch (IOException e){ - throw new RuntimeException(e); + return true; + + }catch (Exception e){ + return false; } } diff --git a/core/src/io/anuke/mindustry/io/Saves.java b/core/src/io/anuke/mindustry/io/Saves.java index ed3c3bc8fa..2fc650e2d4 100644 --- a/core/src/io/anuke/mindustry/io/Saves.java +++ b/core/src/io/anuke/mindustry/io/Saves.java @@ -35,9 +35,15 @@ public class Saves { } public void convertSaves(){ + Array invalid = new Array<>(); + for(SaveSlot slot : saves){ - SaveIO.checkConvert(slot.index); + if(!SaveIO.checkConvert(slot.index)){ + invalid.add(slot); + } } + + saves.removeAll(invalid, true); } public SaveSlot getCurrent() { diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index 08093fa60a..784cd01c19 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -127,4 +127,9 @@ public class Packets { public static class BlockTapPacket{ public int position; } + + public static class BlockConfigPacket{ + public int position; + public byte data; + } } diff --git a/core/src/io/anuke/mindustry/net/Registrator.java b/core/src/io/anuke/mindustry/net/Registrator.java index 1d100d2aa0..d1a280ee81 100644 --- a/core/src/io/anuke/mindustry/net/Registrator.java +++ b/core/src/io/anuke/mindustry/net/Registrator.java @@ -37,6 +37,7 @@ public class Registrator { UpgradePacket.class, WeaponSwitchPacket.class, BlockTapPacket.class, + BlockConfigPacket.class, Class.class, byte[].class, diff --git a/core/src/io/anuke/mindustry/world/Block.java b/core/src/io/anuke/mindustry/world/Block.java index d45857dc7f..f05756cc06 100644 --- a/core/src/io/anuke/mindustry/world/Block.java +++ b/core/src/io/anuke/mindustry/world/Block.java @@ -109,6 +109,11 @@ public class Block{ public void tapped(Tile tile){} public void buildTable(Tile tile, Table table) {} + public void configure(Tile tile, byte data){} + + public void setConfigure(Tile tile, byte data){ + Vars.netClient.handleBlockConfig(tile, data); + } public boolean isConfigurable(Tile tile){ return false; diff --git a/core/src/io/anuke/mindustry/world/blocks/types/distribution/Sorter.java b/core/src/io/anuke/mindustry/world/blocks/types/distribution/Sorter.java index e74f998b9b..b2084e40b8 100644 --- a/core/src/io/anuke/mindustry/world/blocks/types/distribution/Sorter.java +++ b/core/src/io/anuke/mindustry/world/blocks/types/distribution/Sorter.java @@ -90,6 +90,14 @@ public class Sorter extends Junction{ return to; } + @Override + public void configure(Tile tile, byte data) { + SorterEntity entity = tile.entity(); + if(entity != null){ + entity.sortItem = Item.getByID(data); + } + } + @Override public boolean isConfigurable(Tile tile){ return true; @@ -113,6 +121,7 @@ public class Sorter extends Junction{ final int f = i; ImageButton button = cont.addImageButton("white", "toggle", 24, () -> { entity.sortItem = items.get(f); + setConfigure(tile, (byte)f); }).size(38, 42).padBottom(-5.1f).group(group).get(); button.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(Draw.region("icon-"+items.get(i).name))); button.setChecked(entity.sortItem.id == f); diff --git a/core/src/io/anuke/mindustry/world/blocks/types/distribution/Teleporter.java b/core/src/io/anuke/mindustry/world/blocks/types/distribution/Teleporter.java index 0ce2733e2a..77964ee134 100644 --- a/core/src/io/anuke/mindustry/world/blocks/types/distribution/Teleporter.java +++ b/core/src/io/anuke/mindustry/world/blocks/types/distribution/Teleporter.java @@ -46,6 +46,14 @@ public class Teleporter extends PowerBlock{ powerCapacity = 30f; } + @Override + public void configure(Tile tile, byte data) { + TeleporterEntity entity = tile.entity(); + if(entity != null){ + entity.color = data; + } + } + @Override public void getStats(Array list){ super.getStats(list); @@ -104,6 +112,7 @@ public class Teleporter extends PowerBlock{ ImageButton button = cont.addImageButton("white", "toggle", 24, () -> { entity.color = (byte)f; lastColor = (byte)f; + setConfigure(tile, (byte)f); }).size(34, 38).padBottom(-5.1f).group(group).get(); button.getStyle().imageUpColor = colorArray[f]; button.setChecked(entity.color == f); diff --git a/kryonet/src/io/anuke/kryonet/KryoClient.java b/kryonet/src/io/anuke/kryonet/KryoClient.java index 8209ef8b6f..a2eb4b07e2 100644 --- a/kryonet/src/io/anuke/kryonet/KryoClient.java +++ b/kryonet/src/io/anuke/kryonet/KryoClient.java @@ -53,8 +53,7 @@ public class KryoClient implements ClientProvider{ try{ Net.handleClientReceived(c); }catch (Exception e){ - Gdx.app.exit(); - throw new RuntimeException(e); + Gdx.app.postRunnable(() -> {throw new RuntimeException(e);}); } } @@ -65,8 +64,7 @@ public class KryoClient implements ClientProvider{ try{ Net.handleClientReceived(c); }catch (Exception e){ - Gdx.app.exit(); - throw new RuntimeException(e); + Gdx.app.postRunnable(() -> {throw new RuntimeException(e);}); } } @@ -77,8 +75,7 @@ public class KryoClient implements ClientProvider{ try{ Net.handleClientReceived(object); }catch (Exception e){ - Gdx.app.exit(); - throw new RuntimeException(e); + Gdx.app.postRunnable(() -> {throw new RuntimeException(e);}); } } diff --git a/kryonet/src/io/anuke/kryonet/KryoServer.java b/kryonet/src/io/anuke/kryonet/KryoServer.java index 49143b840e..bac41dd3d9 100644 --- a/kryonet/src/io/anuke/kryonet/KryoServer.java +++ b/kryonet/src/io/anuke/kryonet/KryoServer.java @@ -29,7 +29,7 @@ public class KryoServer implements ServerProvider { IntArray connections = new IntArray(); public KryoServer(){ - server = new Server(4096, 1024); //TODO tweak + server = new Server(4096*2, 2048); //TODO tweak server.setDiscoveryHandler(new ServerDiscoveryHandler() { private ByteBuffer buffer = ByteBuffer.allocate(4); @@ -61,8 +61,7 @@ public class KryoServer implements ServerProvider { Net.handleServerReceived(c, c.id); connections.add(c.id); }catch (Exception e){ - Gdx.app.exit(); - throw new RuntimeException(e); + Gdx.app.postRunnable(() -> {throw new RuntimeException(e);}); } } @@ -76,8 +75,7 @@ public class KryoServer implements ServerProvider { try{ Net.handleServerReceived(c, c.id); }catch (Exception e){ - Gdx.app.exit(); - throw new RuntimeException(e); + Gdx.app.postRunnable(() -> {throw new RuntimeException(e);}); } } @@ -88,8 +86,7 @@ public class KryoServer implements ServerProvider { try{ Net.handleServerReceived(object, connection.getID()); }catch (Exception e){ - Gdx.app.exit(); - throw new RuntimeException(e); + Gdx.app.postRunnable(() -> {throw new RuntimeException(e);}); } } });