Skip to content

Commit

Permalink
Extensive network refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Sep 7, 2019
1 parent 38fe99f commit e90c8c4
Show file tree
Hide file tree
Showing 48 changed files with 780 additions and 886 deletions.
31 changes: 7 additions & 24 deletions android/src/io/anuke/mindustry/AndroidLauncher.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package io.anuke.mindustry;

import android.*;
import android.app.*;
import android.content.*;
import android.content.pm.*;
import android.net.*;
import android.os.*;
import android.os.Build.*;
import android.os.*;
import android.provider.Settings.*;
import android.telephony.*;
import io.anuke.arc.*;
Expand All @@ -18,13 +17,12 @@
import io.anuke.arc.util.serialization.*;
import io.anuke.mindustry.game.Saves.*;
import io.anuke.mindustry.io.*;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.net.*;
import io.anuke.mindustry.net.Net.*;
import io.anuke.mindustry.ui.dialogs.*;

import java.io.*;
import java.lang.System;
import java.util.*;

import static io.anuke.mindustry.Vars.*;

Expand All @@ -41,10 +39,13 @@ protected void onCreate(Bundle savedInstanceState){
UnitScl.dp.addition = 0.5f;
}

Net.setClientProvider(new ArcNetClient());
Net.setServerProvider(new ArcNetServer());
initialize(new ClientLauncher(){

@Override
public NetProvider getNet(){
return new ArcNetImpl();
}

@Override
public void hide(){
moveTaskToBack(true);
Expand All @@ -68,24 +69,6 @@ public String getUUID(){
}
}

@Override
public void requestExternalPerms(Runnable callback){
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M || (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
callback.run();
}else{
permCallback = callback;
ArrayList<String> perms = new ArrayList<>();
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
perms.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
perms.add(Manifest.permission.READ_EXTERNAL_STORAGE);
}
requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE);
}
}

@Override
public void shareFile(FileHandle file){
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void writeMethodVariant(TypeSpec.Builder classBuilder, MethodEntry metho
if(!forwarded && methodEntry.local != Loc.none){
//add in local checks
if(methodEntry.local != Loc.both){
method.beginControlFlow("if(" + getCheckString(methodEntry.local) + " || !io.anuke.mindustry.net.Net.active())");
method.beginControlFlow("if(" + getCheckString(methodEntry.local) + " || !io.anuke.mindustry.Vars.net.active())");
}

//concatenate parameters
Expand Down Expand Up @@ -159,7 +159,7 @@ private void writeMethodVariant(TypeSpec.Builder classBuilder, MethodEntry metho
boolean writePlayerSkipCheck = methodEntry.where == Loc.both && i == 0;

if(writePlayerSkipCheck){ //write begin check
method.beginControlFlow("if(io.anuke.mindustry.net.Net.server())");
method.beginControlFlow("if(io.anuke.mindustry.Vars.net.server())");
}

if(Utils.isPrimitive(typeName)){ //check if it's a primitive, and if so write it
Expand Down Expand Up @@ -205,7 +205,7 @@ private void writeMethodVariant(TypeSpec.Builder classBuilder, MethodEntry metho
}

//send the actual packet
method.addStatement("io.anuke.mindustry.net.Net." + sendString + "packet, " +
method.addStatement("io.anuke.mindustry.Vars.net." + sendString + "packet, " +
(methodEntry.unreliable ? "io.anuke.mindustry.net.Net.SendMode.udp" : "io.anuke.mindustry.net.Net.SendMode.tcp") + ")");


Expand All @@ -217,8 +217,8 @@ private void writeMethodVariant(TypeSpec.Builder classBuilder, MethodEntry metho
}

private String getCheckString(Loc loc){
return loc.isClient && loc.isServer ? "io.anuke.mindustry.net.Net.server() || io.anuke.mindustry.net.Net.client()" :
loc.isClient ? "io.anuke.mindustry.net.Net.client()" :
loc.isServer ? "io.anuke.mindustry.net.Net.server()" : "false";
return loc.isClient && loc.isServer ? "io.anuke.mindustry.Vars.net.server() || io.anuke.mindustry.Vars.net.client()" :
loc.isClient ? "io.anuke.mindustry.Vars.net.client()" :
loc.isServer ? "io.anuke.mindustry.Vars.net.server()" : "false";
}
}
2 changes: 2 additions & 0 deletions core/src/io/anuke/mindustry/ClientLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.anuke.mindustry.gen.*;
import io.anuke.mindustry.graphics.*;
import io.anuke.mindustry.maps.*;
import io.anuke.mindustry.net.Net;

import static io.anuke.arc.Core.*;
import static io.anuke.mindustry.Vars.*;
Expand Down Expand Up @@ -40,6 +41,7 @@ public void setup(){
assets = new AssetManager();
assets.setLoader(Texture.class, "." + mapExtension, new MapPreviewLoader());
atlas = TextureAtlas.blankAtlas();
Vars.net = new Net(platform.getNet());

UI.loadSystemCursors();

Expand Down
3 changes: 2 additions & 1 deletion core/src/io/anuke/mindustry/Vars.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public class Vars implements Loadable{
/** list of all locales that can be switched to */
public static Locale[] locales;

public static Net net;
public static ContentLoader content;
public static GameState state;
public static GlobalData data;
Expand Down Expand Up @@ -231,7 +232,7 @@ public static void init(){

for(EntityGroup<?> group : entities.all()){
group.setRemoveListener(entity -> {
if(entity instanceof SyncTrait && Net.client()){
if(entity instanceof SyncTrait && net.client()){
netClient.addRemovedEntity((entity).getID());
}
});
Expand Down
4 changes: 2 additions & 2 deletions core/src/io/anuke/mindustry/ai/Pathfinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Pathfinder{
public Pathfinder(){
Events.on(WorldLoadEvent.class, event -> clear());
Events.on(TileChangeEvent.class, event -> {
if(Net.client()) return;
if(net.client()) return;

for(Team team : Team.all){
TeamData data = state.teams.get(team);
Expand All @@ -43,7 +43,7 @@ public void updateSolid(Tile tile){
}

public void update(){
if(Net.client() || paths == null) return;
if(net.client() || paths == null) return;

for(Team team : Team.all){
if(state.teams.isActive(team)){
Expand Down
2 changes: 1 addition & 1 deletion core/src/io/anuke/mindustry/ai/WaveSpawner.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void eachFlyerSpawn(PositionConsumer cons){
}

public boolean isSpawning(){
return spawning && !Net.client();
return spawning && !net.client();
}

private void reset(){
Expand Down
16 changes: 8 additions & 8 deletions core/src/io/anuke/mindustry/core/Control.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.anuke.mindustry.gen.*;
import io.anuke.mindustry.input.*;
import io.anuke.mindustry.maps.Map;
import io.anuke.mindustry.net.Net;
import io.anuke.mindustry.type.*;
import io.anuke.mindustry.ui.dialogs.*;
import io.anuke.mindustry.world.*;
Expand All @@ -29,6 +28,7 @@

import static io.anuke.arc.Core.*;
import static io.anuke.mindustry.Vars.*;
import static io.anuke.mindustry.Vars.net;

/**
* Control module.
Expand Down Expand Up @@ -67,7 +67,7 @@ public Control(){

Events.on(WorldLoadEvent.class, event -> {
Core.app.post(() -> Core.app.post(() -> {
if(Net.active() && player.getClosestCore() != null){
if(net.active() && player.getClosestCore() != null){
//set to closest core since that's where the player will probably respawn; prevents camera jumps
Core.camera.position.set(player.isDead() ? player.getClosestCore() : player);
}else{
Expand Down Expand Up @@ -99,7 +99,7 @@ public Control(){
Effects.shake(5, 6, Core.camera.position.x, Core.camera.position.y);
//the restart dialog can show info for any number of scenarios
Call.onGameOver(event.winner);
if(state.rules.zone != null && !Net.client()){
if(state.rules.zone != null && !net.client()){
//remove zone save on game over
if(saves.getZoneSlot() != null && !state.rules.tutorial){
saves.getZoneSlot().delete();
Expand All @@ -109,9 +109,9 @@ public Control(){

//autohost for pvp maps
Events.on(WorldLoadEvent.class, event -> {
if(state.rules.pvp && !Net.active()){
if(state.rules.pvp && !net.active()){
try{
Net.host(port);
net.host(port);
player.isAdmin = true;
}catch(IOException e){
ui.showError(Core.bundle.format("server.error", Strings.parseException(e, true)));
Expand Down Expand Up @@ -210,7 +210,7 @@ public void playMap(Map map, Rules rules){
public void playZone(Zone zone){
ui.loadAnd(() -> {
logic.reset();
Net.reset();
net.reset();
world.loadGenerator(zone.generator);
zone.rules.accept(state.rules);
state.rules.zone = zone;
Expand All @@ -229,7 +229,7 @@ public void playTutorial(){
Zone zone = Zones.groundZero;
ui.loadAnd(() -> {
logic.reset();
Net.reset();
net.reset();

world.beginMapLoad();

Expand Down Expand Up @@ -284,7 +284,7 @@ public boolean isHighScore(){
@Override
public void dispose(){
content.dispose();
Net.dispose();
net.dispose();
Musics.dispose();
Sounds.dispose();
ui.editor.dispose();
Expand Down
16 changes: 7 additions & 9 deletions core/src/io/anuke/mindustry/core/GameState.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.anuke.mindustry.core;

import io.anuke.arc.Events;
import io.anuke.mindustry.entities.type.BaseUnit;
import io.anuke.mindustry.entities.type.base.BaseDrone;
import io.anuke.mindustry.game.EventType.StateChangeEvent;
import io.anuke.arc.*;
import io.anuke.mindustry.entities.type.*;
import io.anuke.mindustry.entities.type.base.*;
import io.anuke.mindustry.game.EventType.*;
import io.anuke.mindustry.game.*;
import io.anuke.mindustry.net.Net;

import static io.anuke.mindustry.Vars.unitGroups;
import static io.anuke.mindustry.Vars.waveTeam;
import static io.anuke.mindustry.Vars.*;

public class GameState{
/** Current wave number, can be anything in non-wave modes. */
Expand All @@ -29,7 +27,7 @@ public class GameState{
private State state = State.menu;

public int enemies(){
return Net.client() ? enemies : unitGroups[waveTeam.ordinal()].count(b -> !(b instanceof BaseDrone));
return net.client() ? enemies : unitGroups[waveTeam.ordinal()].count(b -> !(b instanceof BaseDrone));
}

public BaseUnit boss(){
Expand All @@ -46,7 +44,7 @@ public boolean isEditor(){
}

public boolean isPaused(){
return (is(State.paused) && !Net.active()) || (gameOver && !Net.active());
return (is(State.paused) && !net.active()) || (gameOver && !net.active());
}

public boolean is(State astate){
Expand Down
4 changes: 2 additions & 2 deletions core/src/io/anuke/mindustry/core/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void update(){
}
}

if(!Net.client() && state.wavetime <= 0 && state.rules.waves){
if(!net.client() && state.wavetime <= 0 && state.rules.waves){
runWave();
}

Expand Down Expand Up @@ -237,7 +237,7 @@ public void update(){
pathfinder.update();
}

if(!Net.client() && !world.isInvalidMap() && !state.isEditor()){
if(!net.client() && !world.isInvalidMap() && !state.isEditor()){
checkGameOver();
}
}
Expand Down
Loading

0 comments on commit e90c8c4

Please sign in to comment.