From 7e80a60e65a74d9a52ade726f1cce764ef0bb443 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 30 Aug 2019 17:44:28 -0400 Subject: [PATCH] More cleanup --- .../mindustry/desktop/DesktopLauncher.java | 219 ++++++++++++++++-- .../mindustry/desktop/DesktopPlatform.java | 209 ----------------- 2 files changed, 205 insertions(+), 223 deletions(-) delete mode 100644 desktop/src/io/anuke/mindustry/desktop/DesktopPlatform.java diff --git a/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java b/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java index c22d0dc7f4..ca012dbaba 100644 --- a/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java +++ b/desktop/src/io/anuke/mindustry/desktop/DesktopLauncher.java @@ -1,28 +1,42 @@ package io.anuke.mindustry.desktop; +import club.minnced.discord.rpc.*; import com.codedisaster.steamworks.*; +import io.anuke.arc.*; import io.anuke.arc.Files.*; import io.anuke.arc.backends.sdl.*; +import io.anuke.arc.backends.sdl.jni.*; +import io.anuke.arc.collection.*; +import io.anuke.arc.files.*; +import io.anuke.arc.function.*; +import io.anuke.arc.scene.event.*; +import io.anuke.arc.scene.ui.*; +import io.anuke.arc.util.*; +import io.anuke.arc.util.Log.*; +import io.anuke.arc.util.serialization.*; import io.anuke.mindustry.*; -import io.anuke.mindustry.core.*; +import io.anuke.mindustry.core.GameState.*; import io.anuke.mindustry.desktop.steam.*; +import io.anuke.mindustry.game.EventType.*; +import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.*; +import io.anuke.mindustry.ui.dialogs.*; -public class DesktopLauncher{ +import java.net.*; +import java.util.*; + +import static io.anuke.mindustry.Vars.*; + + +public class DesktopLauncher extends ClientLauncher{ + private final static String applicationId = "610508934456934412"; + + boolean useDiscord = OS.is64Bit, useSteam = true, showConsole = true; + SteamCoreNetImpl steamCore; public static void main(String[] arg){ try{ - - if(SteamAPI.isSteamRunning()){ - SteamCoreNetImpl net = DesktopPlatform.steamCore = new SteamCoreNetImpl(); - Net.setClientProvider(net); - Net.setServerProvider(net); - }else{ - Net.setClientProvider(new ArcNetClient()); - Net.setServerProvider(new ArcNetServer()); - } - - new SdlApplication(new DesktopPlatform(arg), new SdlConfig(){{ + new SdlApplication(new DesktopLauncher(arg), new SdlConfig(){{ title = "Mindustry"; maximized = true; depth = 0; @@ -32,7 +46,184 @@ public class DesktopLauncher{ setWindowIcon(FileType.Internal, "icons/icon_64.png"); }}); }catch(Throwable e){ - DesktopPlatform.handleCrash(e); + DesktopLauncher.handleCrash(e); } } + + public DesktopLauncher(String[] args){ + testMobile = Array.with(args).contains("-testMobile"); + + if(useDiscord){ + try{ + DiscordEventHandlers handlers = new DiscordEventHandlers(); + DiscordRPC.INSTANCE.Discord_Initialize(applicationId, handlers, true, "1127400"); + Log.info("Initialized Discord rich presence."); + + Runtime.getRuntime().addShutdownHook(new Thread(DiscordRPC.INSTANCE::Discord_Shutdown)); + }catch(Throwable t){ + useDiscord = false; + Log.err("Failed to initialize discord.", t); + } + } + + if(useSteam){ + if(showConsole){ + Events.on(ClientLoadEvent.class, event -> { + Label[] label = {null}; + Core.scene.table(t -> { + t.touchable(Touchable.disabled); + t.top().left(); + t.update(t::toFront); + t.table("guideDim", f -> label[0] = f.add("").get()); + }); + + Log.setLogger(new LogHandler(){ + @Override + public void print(String text, Object... args){ + super.print(text, args); + String out = Log.format(text, false, args); + + int maxlen = 2048; + + if(label[0].getText().length() > maxlen){ + label[0].setText(label[0].getText().substring(label[0].getText().length() - maxlen)); + } + + label[0].getText().append(out).append("\n"); + label[0].invalidateHierarchy(); + } + }); + }); + } + + try{ + SteamAPI.loadLibraries(); + if(!SteamAPI.init()){ + Log.info("Steam client not running. Make sure Steam is running!"); + }else{ + Vars.steam = true; + Events.on(ClientLoadEvent.class, event -> { + Core.settings.defaults("name", steamCore.friends.getPersonaName()); + //update callbacks + Core.app.addListener(new ApplicationListener(){ + @Override + public void update(){ + if(SteamAPI.isSteamRunning()){ + SteamAPI.runCallbacks(); + } + } + }); + }); + //steam shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread(SteamAPI::shutdown)); + } + }catch(Exception e){ + Log.err("Failed to load Steam native libraries."); + e.printStackTrace(); + } + } + + if(steam){ + SteamCoreNetImpl net = steamCore = new SteamCoreNetImpl(); + Net.setClientProvider(net); + Net.setServerProvider(net); + }else{ + Net.setClientProvider(new ArcNetClient()); + Net.setServerProvider(new ArcNetServer()); + } + } + + static void handleCrash(Throwable e){ + Consumer dialog = Runnable::run; + boolean badGPU = false; + + if(e.getMessage() != null && (e.getMessage().contains("Couldn't create window") || e.getMessage().contains("OpenGL 2.0 or higher"))){ + + dialog.accept(() -> message( + e.getMessage().contains("Couldn't create window") ? "A graphics initialization error has occured! Try to update your graphics drivers:\n" + e.getMessage() : + "Your graphics card does not support OpenGL 2.0!\n" + + "Try to update your graphics drivers.\n\n" + + "(If that doesn't work, your computer just doesn't support Mindustry.)")); + badGPU = true; + } + + boolean fbgp = badGPU; + + CrashSender.send(e, file -> { + if(!fbgp){ + dialog.accept(() -> message("A crash has occured. It has been saved in:\n" + file.getAbsolutePath() + "\n" + (e.getMessage() == null ? "" : "\n" + e.getMessage()))); + } + }); + } + + @Override + public void showFileChooser(String text, String content, Consumer cons, boolean open, Predicate filetype){ + new FileChooser(text, file -> filetype.test(file.extension().toLowerCase()), open, cons).show(); + } + + @Override + public void updateRPC(){ + if(!useDiscord) return; + + DiscordRichPresence presence = new DiscordRichPresence(); + + if(!state.is(State.menu)){ + String map = world.getMap() == null ? "Unknown Map" : world.isZone() ? world.getZone().localizedName : Strings.capitalize(world.getMap().name()); + String mode = state.rules.pvp ? "PvP" : state.rules.attackMode ? "Attack" : "Survival"; + String players = Net.active() && playerGroup.size() > 1 ? " | " + playerGroup.size() + " Players" : ""; + + presence.state = mode + players; + + if(!state.rules.waves){ + presence.details = map; + }else{ + presence.details = map + " | Wave " + state.wave; + presence.largeImageText = "Wave " + state.wave; + } + }else{ + if(ui.editor != null && ui.editor.isShown()){ + presence.state = "In Editor"; + }else if(ui.deploy != null && ui.deploy.isShown()){ + presence.state = "In Launch Selection"; + }else{ + presence.state = "In Menu"; + } + } + + presence.largeImageKey = "logo"; + + DiscordRPC.INSTANCE.Discord_UpdatePresence(presence); + } + + @Override + public String getUUID(){ + try{ + Enumeration e = NetworkInterface.getNetworkInterfaces(); + NetworkInterface out; + for(out = e.nextElement(); (out.getHardwareAddress() == null || !validAddress(out.getHardwareAddress())) && e.hasMoreElements(); out = e.nextElement()); + + byte[] bytes = out.getHardwareAddress(); + byte[] result = new byte[8]; + System.arraycopy(bytes, 0, result, 0, bytes.length); + + String str = new String(Base64Coder.encode(result)); + + if(str.equals("AAAAAAAAAOA=") || str.equals("AAAAAAAAAAA=")) throw new RuntimeException("Bad UUID."); + + return str; + }catch(Exception e){ + return super.getUUID(); + } + } + + private static void message(String message){ + SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MESSAGEBOX_ERROR, "oh no", message); + } + + private boolean validAddress(byte[] bytes){ + if(bytes == null) return false; + byte[] result = new byte[8]; + System.arraycopy(bytes, 0, result, 0, bytes.length); + return !new String(Base64Coder.encode(result)).equals("AAAAAAAAAOA=") && !new String(Base64Coder.encode(result)).equals("AAAAAAAAAAA="); + } } diff --git a/desktop/src/io/anuke/mindustry/desktop/DesktopPlatform.java b/desktop/src/io/anuke/mindustry/desktop/DesktopPlatform.java deleted file mode 100644 index 3bf560339b..0000000000 --- a/desktop/src/io/anuke/mindustry/desktop/DesktopPlatform.java +++ /dev/null @@ -1,209 +0,0 @@ -package io.anuke.mindustry.desktop; - -import club.minnced.discord.rpc.*; -import com.codedisaster.steamworks.*; -import io.anuke.arc.*; -import io.anuke.arc.backends.sdl.jni.*; -import io.anuke.arc.collection.*; -import io.anuke.arc.files.*; -import io.anuke.arc.function.*; -import io.anuke.arc.scene.event.*; -import io.anuke.arc.scene.ui.*; -import io.anuke.arc.util.*; -import io.anuke.arc.util.Log.*; -import io.anuke.arc.util.serialization.*; -import io.anuke.mindustry.*; -import io.anuke.mindustry.core.GameState.*; -import io.anuke.mindustry.core.*; -import io.anuke.mindustry.desktop.steam.*; -import io.anuke.mindustry.game.EventType.*; -import io.anuke.mindustry.net.Net; -import io.anuke.mindustry.net.*; -import io.anuke.mindustry.ui.dialogs.*; - -import java.net.*; -import java.util.*; - -import static io.anuke.mindustry.Vars.*; - - -public class DesktopPlatform extends Platform{ - static boolean useDiscord = OS.is64Bit, useSteam = true, showConsole = true; - static SteamCoreNetImpl steamCore; - final static String applicationId = "610508934456934412"; - String[] args; - - public DesktopPlatform(String[] args){ - this.args = args; - - testMobile = Array.with(args).contains("-testMobile"); - - if(useDiscord){ - try{ - DiscordEventHandlers handlers = new DiscordEventHandlers(); - DiscordRPC.INSTANCE.Discord_Initialize(applicationId, handlers, true, "1127400"); - Log.info("Initialized Discord rich presence."); - - Runtime.getRuntime().addShutdownHook(new Thread(DiscordRPC.INSTANCE::Discord_Shutdown)); - }catch(Throwable t){ - useDiscord = false; - Log.err("Failed to initialize discord.", t); - } - } - - if(useSteam){ - - if(showConsole){ - Events.on(ClientLoadEvent.class, event -> { - Label[] label = {null}; - Core.scene.table(t -> { - t.touchable(Touchable.disabled); - t.top().left(); - t.update(t::toFront); - t.table("guideDim", f -> label[0] = f.add("").get()); - }); - - Log.setLogger(new LogHandler(){ - @Override - public void print(String text, Object... args){ - super.print(text, args); - String out = Log.format(text, false, args); - - int maxlen = 2048; - - if(label[0].getText().length() > maxlen){ - label[0].setText(label[0].getText().substring(label[0].getText().length() - maxlen)); - } - - label[0].getText().append(out).append("\n"); - label[0].invalidateHierarchy(); - } - }); - }); - } - - try{ - SteamAPI.loadLibraries(); - if(!SteamAPI.init()){ - Log.info("Steam client not running. Make sure Steam is running!"); - }else{ - Vars.steam = true; - //run steam callbacks - Events.on(ClientLoadEvent.class, event -> { - Core.settings.defaults("name", steamCore.friends.getPersonaName()); - //update callbacks - Core.app.addListener(new ApplicationListener(){ - @Override - public void update(){ - if(SteamAPI.isSteamRunning()){ - SteamAPI.runCallbacks(); - } - } - }); - - //Core.app.post(() -> new ClientSteam()); - }); - //steam shutdown hook - Runtime.getRuntime().addShutdownHook(new Thread(SteamAPI::shutdown)); - } - }catch(Exception e){ - Log.err("Failed to load Steam native libraries."); - e.printStackTrace(); - } - } - } - - static void handleCrash(Throwable e){ - Consumer dialog = Runnable::run; - boolean badGPU = false; - - if(e.getMessage() != null && (e.getMessage().contains("Couldn't create window") || e.getMessage().contains("OpenGL 2.0 or higher"))){ - - dialog.accept(() -> message( - e.getMessage().contains("Couldn't create window") ? "A graphics initialization error has occured! Try to update your graphics drivers:\n" + e.getMessage() : - "Your graphics card does not support OpenGL 2.0!\n" + - "Try to update your graphics drivers.\n\n" + - "(If that doesn't work, your computer just doesn't support Mindustry.)")); - badGPU = true; - } - - boolean fbgp = badGPU; - - CrashSender.send(e, file -> { - if(!fbgp){ - dialog.accept(() -> message("A crash has occured. It has been saved in:\n" + file.getAbsolutePath() + "\n" + (e.getMessage() == null ? "" : "\n" + e.getMessage()))); - } - }); - } - - @Override - public void showFileChooser(String text, String content, Consumer cons, boolean open, Predicate filetype){ - new FileChooser(text, file -> filetype.test(file.extension().toLowerCase()), open, cons).show(); - } - - @Override - public void updateRPC(){ - if(!useDiscord) return; - - DiscordRichPresence presence = new DiscordRichPresence(); - - if(!state.is(State.menu)){ - String map = world.getMap() == null ? "Unknown Map" : world.isZone() ? world.getZone().localizedName : Strings.capitalize(world.getMap().name()); - String mode = state.rules.pvp ? "PvP" : state.rules.attackMode ? "Attack" : "Survival"; - String players = Net.active() && playerGroup.size() > 1 ? " | " + playerGroup.size() + " Players" : ""; - - presence.state = mode + players; - - if(!state.rules.waves){ - presence.details = map; - }else{ - presence.details = map + " | Wave " + state.wave; - presence.largeImageText = "Wave " + state.wave; - } - }else{ - if(ui.editor != null && ui.editor.isShown()){ - presence.state = "In Editor"; - }else if(ui.deploy != null && ui.deploy.isShown()){ - presence.state = "In Launch Selection"; - }else{ - presence.state = "In Menu"; - } - } - - presence.largeImageKey = "logo"; - - DiscordRPC.INSTANCE.Discord_UpdatePresence(presence); - } - - @Override - public String getUUID(){ - try{ - Enumeration e = NetworkInterface.getNetworkInterfaces(); - NetworkInterface out; - for(out = e.nextElement(); (out.getHardwareAddress() == null || !validAddress(out.getHardwareAddress())) && e.hasMoreElements(); out = e.nextElement()); - - byte[] bytes = out.getHardwareAddress(); - byte[] result = new byte[8]; - System.arraycopy(bytes, 0, result, 0, bytes.length); - - String str = new String(Base64Coder.encode(result)); - - if(str.equals("AAAAAAAAAOA=") || str.equals("AAAAAAAAAAA=")) throw new RuntimeException("Bad UUID."); - - return str; - }catch(Exception e){ - return super.getUUID(); - } - } - - private static void message(String message){ - SDL.SDL_ShowSimpleMessageBox(SDL.SDL_MESSAGEBOX_ERROR, "oh no", message); - } - - private boolean validAddress(byte[] bytes){ - if(bytes == null) return false; - byte[] result = new byte[8]; - System.arraycopy(bytes, 0, result, 0, bytes.length); - return !new String(Base64Coder.encode(result)).equals("AAAAAAAAAOA=") && !new String(Base64Coder.encode(result)).equals("AAAAAAAAAAA="); - } -}