Fixed some network crashes and block config problems

This commit is contained in:
Anuken
2018-01-10 17:22:19 -05:00
parent f09394c0ea
commit 1820abe14f
12 changed files with 94 additions and 37 deletions

View File

@@ -200,31 +200,37 @@ public class NetClient extends Module {
Net.handle(BlockUpdatePacket.class, packet -> { Net.handle(BlockUpdatePacket.class, packet -> {
Tile tile = Vars.world.tile(packet.position % Vars.world.width(), packet.position / Vars.world.width()); 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; tile.entity.health = packet.health;
} }
}); });
Net.handle(BlockSyncPacket.class, packet -> { Net.handle(BlockSyncPacket.class, packet -> {
if(!gotEntities) return;
DataInputStream stream = new DataInputStream(packet.stream); DataInputStream stream = new DataInputStream(packet.stream);
try{ Gdx.app.postRunnable(() -> {
while(stream.available() > 0){ try {
int pos = stream.readInt(); 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 ++){ for (int i = 0; i < times; i++) {
tile.entity.timer.getTimes()[i] = stream.readFloat(); tile.entity.timer.getTimes()[i] = stream.readFloat();
}
tile.entity.read(stream);
} }
} catch (IOException e) {
tile.entity.read(stream); throw new RuntimeException(e);
} }
}catch (IOException e){ });
e.printStackTrace();
}
}); });
Net.handle(DisconnectPacket.class, packet -> { Net.handle(DisconnectPacket.class, packet -> {
@@ -247,17 +253,24 @@ public class NetClient extends Module {
}); });
Net.handle(WeaponSwitchPacket.class, packet -> { 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.weaponLeft = (Weapon)Upgrade.getByID(packet.left);
player.weaponRight = (Weapon)Upgrade.getByID(packet.right); player.weaponRight = (Weapon)Upgrade.getByID(packet.right);
});
}); });
Net.handle(BlockTapPacket.class, packet -> { Net.handle(BlockTapPacket.class, packet -> {
Tile tile = Vars.world.tile(packet.position); 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){ public void handleBlockTap(Tile tile){
BlockTapPacket packet = new BlockTapPacket(); BlockTapPacket packet = new BlockTapPacket();
packet.position = tile.packedPosition(); packet.position = tile.packedPosition();

View File

@@ -168,6 +168,13 @@ public class NetServer extends Module{
Net.sendExcept(Net.getLastConnection(), packet, SendMode.tcp); 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){ public void sendMessage(String message){

View File

@@ -186,7 +186,7 @@ public abstract class InputHandler extends InputAdapter{
placeBlockInternal(x, y, result, rotation, effects, sound); placeBlockInternal(x, y, result, rotation, effects, sound);
if(Net.active()){ if(Net.active() && result != ProductionBlocks.core){
Vars.netClient.handlePlace(x, y, result, rotation); Vars.netClient.handlePlace(x, y, result, rotation);
} }
} }

View File

@@ -74,6 +74,7 @@ public class SaveIO{
} }
} }
/**Returns whether or not conversion was succesful.*/
public static boolean checkConvert(int slot){ public static boolean checkConvert(int slot){
try{ try{
@@ -88,12 +89,12 @@ public class SaveIO{
target.read(stream); target.read(stream);
stream.close(); stream.close();
saveToSlot(slot); saveToSlot(slot);
return true;
} }
return false;
}catch (IOException e){ return true;
throw new RuntimeException(e);
}catch (Exception e){
return false;
} }
} }

View File

@@ -35,9 +35,15 @@ public class Saves {
} }
public void convertSaves(){ public void convertSaves(){
Array<SaveSlot> invalid = new Array<>();
for(SaveSlot slot : saves){ for(SaveSlot slot : saves){
SaveIO.checkConvert(slot.index); if(!SaveIO.checkConvert(slot.index)){
invalid.add(slot);
}
} }
saves.removeAll(invalid, true);
} }
public SaveSlot getCurrent() { public SaveSlot getCurrent() {

View File

@@ -127,4 +127,9 @@ public class Packets {
public static class BlockTapPacket{ public static class BlockTapPacket{
public int position; public int position;
} }
public static class BlockConfigPacket{
public int position;
public byte data;
}
} }

View File

@@ -37,6 +37,7 @@ public class Registrator {
UpgradePacket.class, UpgradePacket.class,
WeaponSwitchPacket.class, WeaponSwitchPacket.class,
BlockTapPacket.class, BlockTapPacket.class,
BlockConfigPacket.class,
Class.class, Class.class,
byte[].class, byte[].class,

View File

@@ -109,6 +109,11 @@ public class Block{
public void tapped(Tile tile){} public void tapped(Tile tile){}
public void buildTable(Tile tile, Table table) {} 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){ public boolean isConfigurable(Tile tile){
return false; return false;

View File

@@ -90,6 +90,14 @@ public class Sorter extends Junction{
return to; return to;
} }
@Override
public void configure(Tile tile, byte data) {
SorterEntity entity = tile.entity();
if(entity != null){
entity.sortItem = Item.getByID(data);
}
}
@Override @Override
public boolean isConfigurable(Tile tile){ public boolean isConfigurable(Tile tile){
return true; return true;
@@ -113,6 +121,7 @@ public class Sorter extends Junction{
final int f = i; final int f = i;
ImageButton button = cont.addImageButton("white", "toggle", 24, () -> { ImageButton button = cont.addImageButton("white", "toggle", 24, () -> {
entity.sortItem = items.get(f); entity.sortItem = items.get(f);
setConfigure(tile, (byte)f);
}).size(38, 42).padBottom(-5.1f).group(group).get(); }).size(38, 42).padBottom(-5.1f).group(group).get();
button.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(Draw.region("icon-"+items.get(i).name))); button.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(Draw.region("icon-"+items.get(i).name)));
button.setChecked(entity.sortItem.id == f); button.setChecked(entity.sortItem.id == f);

View File

@@ -46,6 +46,14 @@ public class Teleporter extends PowerBlock{
powerCapacity = 30f; powerCapacity = 30f;
} }
@Override
public void configure(Tile tile, byte data) {
TeleporterEntity entity = tile.entity();
if(entity != null){
entity.color = data;
}
}
@Override @Override
public void getStats(Array<String> list){ public void getStats(Array<String> list){
super.getStats(list); super.getStats(list);
@@ -104,6 +112,7 @@ public class Teleporter extends PowerBlock{
ImageButton button = cont.addImageButton("white", "toggle", 24, () -> { ImageButton button = cont.addImageButton("white", "toggle", 24, () -> {
entity.color = (byte)f; entity.color = (byte)f;
lastColor = (byte)f; lastColor = (byte)f;
setConfigure(tile, (byte)f);
}).size(34, 38).padBottom(-5.1f).group(group).get(); }).size(34, 38).padBottom(-5.1f).group(group).get();
button.getStyle().imageUpColor = colorArray[f]; button.getStyle().imageUpColor = colorArray[f];
button.setChecked(entity.color == f); button.setChecked(entity.color == f);

View File

@@ -53,8 +53,7 @@ public class KryoClient implements ClientProvider{
try{ try{
Net.handleClientReceived(c); Net.handleClientReceived(c);
}catch (Exception e){ }catch (Exception e){
Gdx.app.exit(); Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
throw new RuntimeException(e);
} }
} }
@@ -65,8 +64,7 @@ public class KryoClient implements ClientProvider{
try{ try{
Net.handleClientReceived(c); Net.handleClientReceived(c);
}catch (Exception e){ }catch (Exception e){
Gdx.app.exit(); Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
throw new RuntimeException(e);
} }
} }
@@ -77,8 +75,7 @@ public class KryoClient implements ClientProvider{
try{ try{
Net.handleClientReceived(object); Net.handleClientReceived(object);
}catch (Exception e){ }catch (Exception e){
Gdx.app.exit(); Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
throw new RuntimeException(e);
} }
} }

View File

@@ -29,7 +29,7 @@ public class KryoServer implements ServerProvider {
IntArray connections = new IntArray(); IntArray connections = new IntArray();
public KryoServer(){ public KryoServer(){
server = new Server(4096, 1024); //TODO tweak server = new Server(4096*2, 2048); //TODO tweak
server.setDiscoveryHandler(new ServerDiscoveryHandler() { server.setDiscoveryHandler(new ServerDiscoveryHandler() {
private ByteBuffer buffer = ByteBuffer.allocate(4); private ByteBuffer buffer = ByteBuffer.allocate(4);
@@ -61,8 +61,7 @@ public class KryoServer implements ServerProvider {
Net.handleServerReceived(c, c.id); Net.handleServerReceived(c, c.id);
connections.add(c.id); connections.add(c.id);
}catch (Exception e){ }catch (Exception e){
Gdx.app.exit(); Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
throw new RuntimeException(e);
} }
} }
@@ -76,8 +75,7 @@ public class KryoServer implements ServerProvider {
try{ try{
Net.handleServerReceived(c, c.id); Net.handleServerReceived(c, c.id);
}catch (Exception e){ }catch (Exception e){
Gdx.app.exit(); Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
throw new RuntimeException(e);
} }
} }
@@ -88,8 +86,7 @@ public class KryoServer implements ServerProvider {
try{ try{
Net.handleServerReceived(object, connection.getID()); Net.handleServerReceived(object, connection.getID());
}catch (Exception e){ }catch (Exception e){
Gdx.app.exit(); Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
throw new RuntimeException(e);
} }
} }
}); });