Skip to content

Commit

Permalink
Fixed crash when connnecting, improved error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Jan 13, 2018
1 parent 77d8464 commit 5e2173f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 12 deletions.
4 changes: 2 additions & 2 deletions android/AndroidManifest.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.anuke.mindustry"
android:versionCode="53"
android:versionName="3.3b5" >
android:versionCode="54"
android:versionName="3.3b6" >

<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
1 change: 1 addition & 0 deletions core/assets/bundles/bundle.properties
Expand Up @@ -17,6 +17,7 @@ text.name=Name:
text.public=Public
text.players={0} players online
text.players.single={0} player online
text.server.mismatch=Packet error: possible client/server version mismatch.\nMake sure you and the host have the\nlatest version of Mindustry!
text.server.closing=[accent]Closing server...
text.server.kicked.kick=You have been kicked from the server!
text.server.kicked.invalidPassword=Invalid password!
Expand Down
2 changes: 2 additions & 0 deletions core/src/io/anuke/mindustry/core/NetClient.java
Expand Up @@ -47,6 +47,7 @@ public class NetClient extends Module {
public NetClient(){

Net.handle(Connect.class, packet -> {
Net.setClientLoaded(false);
requests.clear();
connecting = true;
gotData = false;
Expand Down Expand Up @@ -99,6 +100,7 @@ public NetClient(){

Net.send(new ConnectConfirmPacket(), SendMode.tcp);
GameState.set(State.playing);
Net.setClientLoaded(true);
});
});

Expand Down
4 changes: 4 additions & 0 deletions core/src/io/anuke/mindustry/core/UI.java
Expand Up @@ -180,6 +180,10 @@ public void showInfo(String info){
}

public void showError(String text){
if(hasDialog()){
Dialog dialog = scene.getScrollFocus() instanceof Dialog ? (Dialog)scene.getScrollFocus() : (Dialog)scene.getKeyboardFocus();
dialog.hide();
}
new Dialog("$text.error.title", "dialog"){{
content().margin(15).add(text);
buttons().addButton("$text.ok", this::hide).size(90, 50).pad(4);
Expand Down
4 changes: 4 additions & 0 deletions core/src/io/anuke/mindustry/io/Saves.java
Expand Up @@ -51,6 +51,10 @@ public SaveSlot getCurrent() {
}

public void update(){
if(GameState.is(State.menu)){
current = null;
}

if(!GameState.is(State.menu) && !Vars.control.isGameOver() && current != null && current.isAutosave()){
time += Timers.delta();
if(time > Settings.getInt("saveinterval")*60) {
Expand Down
7 changes: 6 additions & 1 deletion core/src/io/anuke/mindustry/net/Net.java
Expand Up @@ -18,6 +18,7 @@
public class Net{
private static boolean server;
private static boolean active;
private static boolean clientLoaded;
private static ObjectMap<Class<?>, Consumer> clientListeners = new ObjectMap<>();
private static ObjectMap<Class<?>, Consumer> serverListeners = new ObjectMap<>();
private static ClientProvider clientProvider;
Expand All @@ -26,6 +27,10 @@ public class Net{
private static int lastConnection = -1;
private static IntMap<StreamBuilder> streams = new IntMap<>();
private static AsyncExecutor executor = new AsyncExecutor(4);

public static void setClientLoaded(boolean loaded){
clientLoaded = loaded;
}

/**Connect to an address.*/
public static void connect(String ip, int port) throws IOException{
Expand Down Expand Up @@ -141,7 +146,7 @@ public static void handleClientReceived(Object object){
handleClientReceived(builder.build());
}
}else if(clientListeners.get(object.getClass()) != null){
clientListeners.get(object.getClass()).accept(object);
if(clientLoaded) clientListeners.get(object.getClass()).accept(object);
}else{
Gdx.app.error("Mindustry::Net", "Unhandled packet type: '" + object.getClass() + "'!");
}
Expand Down
5 changes: 0 additions & 5 deletions core/src/io/anuke/mindustry/net/Registrator.java
Expand Up @@ -7,7 +7,6 @@
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.net.Streamable.StreamBegin;
import io.anuke.mindustry.net.Streamable.StreamChunk;
import io.anuke.mindustry.resource.Mech;
import io.anuke.ucore.entities.Entity;

public class Registrator {
Expand Down Expand Up @@ -47,15 +46,11 @@ public static Class<?>[] getClasses(){
int[].class,
int[][].class,
Entity[].class,
Player[].class,
Array.class,
Vector2.class,
EnemySpawnPacket[].class,

Entity.class,
Player.class,
Mech.class,

Enemy.class
};
}
Expand Down
1 change: 1 addition & 0 deletions core/src/io/anuke/mindustry/ui/dialogs/HostDialog.java
Expand Up @@ -25,6 +25,7 @@ public HostDialog(){
Vars.player.name = text;
Settings.put("name", text);
Settings.save();
Vars.ui.listfrag.rebuild();
}).grow().pad(8);
}).width(w).height(70f).pad(4);

Expand Down
Expand Up @@ -48,8 +48,7 @@ public boolean acceptItem(Item item, Tile tile, Tile source){
public void handleItem(Item item, Tile tile, Tile source){
Tile to = getTileTarget(item, tile, source, true);

Timers.run(15, ()->{
if(to == null || to.entity == null) return;
Timers.run(15, () -> {
to.block().handleItem(item, to, tile);
});

Expand Down
33 changes: 31 additions & 2 deletions kryonet/src/io/anuke/kryonet/KryoClient.java
Expand Up @@ -42,7 +42,7 @@ public void onFinally() {

}
});
client.start();

client.addListener(new Listener(){
@Override
public void connected (Connection connection) {
Expand Down Expand Up @@ -75,7 +75,13 @@ public void received (Connection connection, Object object) {
try{
Net.handleClientReceived(object);
}catch (Exception e){
Gdx.app.postRunnable(() -> {throw new RuntimeException(e);});
if(e instanceof KryoNetException && e.getMessage() != null && e.getMessage().toLowerCase().contains("incorrect")) {
UCore.log("Mismatch!");
}else{
Gdx.app.postRunnable(() -> {
throw new RuntimeException(e);
});
}
}

}
Expand All @@ -86,6 +92,19 @@ public void received (Connection connection, Object object) {

@Override
public void connect(String ip, int port) throws IOException {
//just in case
client.stop();

Thread updateThread = new Thread(() -> {
try{
client.run();
}catch (Exception e){
handleException(e);
}
}, "Kryonet Client");
updateThread.setDaemon(true);
updateThread.start();

client.connect(5000, ip, port, port);
}

Expand Down Expand Up @@ -145,4 +164,14 @@ public void dispose(){
}
}

private void handleException(Exception e){
e.printStackTrace();
if(e instanceof KryoNetException){
Gdx.app.postRunnable(() -> Vars.ui.showError("$text.server.mismatch"));
}else{
//TODO better exception handling.
disconnect();
}
}

}

0 comments on commit 5e2173f

Please sign in to comment.