From 635d6d80441624e58e21f23930c5fb776a300f60 Mon Sep 17 00:00:00 2001 From: RaphiMC <50594595+RaphiMC@users.noreply.github.com> Date: Thu, 12 Dec 2024 19:39:13 +0100 Subject: [PATCH] Decluttered main method a bit --- .../java/net/raphimc/viaproxy/ViaProxy.java | 51 ++++--------------- .../viaproxy/ViaProxyConfig.java | 12 +++++ .../tasks/SystemRequirementsCheck.java | 49 ++++++++++++++++++ 3 files changed, 72 insertions(+), 40 deletions(-) create mode 100644 src/main/java/net/raphimc/viaproxy/tasks/SystemRequirementsCheck.java diff --git a/src/main/java/net/raphimc/viaproxy/ViaProxy.java b/src/main/java/net/raphimc/viaproxy/ViaProxy.java index a3297d3..527ebd6 100644 --- a/src/main/java/net/raphimc/viaproxy/ViaProxy.java +++ b/src/main/java/net/raphimc/viaproxy/ViaProxy.java @@ -30,8 +30,6 @@ import net.lenni0451.classtransform.utils.loader.InjectionClassLoader; import net.lenni0451.classtransform.utils.tree.IClassProvider; import net.lenni0451.lambdaevents.LambdaManager; import net.lenni0451.lambdaevents.generator.LambdaMetaFactoryGenerator; -import net.lenni0451.optconfig.ConfigLoader; -import net.lenni0451.optconfig.provider.ConfigProvider; import net.lenni0451.reflect.Agents; import net.lenni0451.reflect.ClassLoaders; import net.lenni0451.reflect.JavaBypass; @@ -50,6 +48,7 @@ import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyChannelInitializer; import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyHandler; import net.raphimc.viaproxy.proxy.session.ProxyConnection; import net.raphimc.viaproxy.saves.SaveManager; +import net.raphimc.viaproxy.tasks.SystemRequirementsCheck; import net.raphimc.viaproxy.tasks.UpdateCheckTask; import net.raphimc.viaproxy.ui.SplashScreen; import net.raphimc.viaproxy.ui.ViaProxyWindow; @@ -170,26 +169,7 @@ public class ViaProxy { } } if (System.getProperty("ignoreSystemRequirements") == null) { - if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) { - Logger.LOGGER.fatal("ViaProxy is not able to run on 32bit Java."); - if (useUI) { - JOptionPane.showMessageDialog(null, "ViaProxy is not able to run on 32bit Java. Please install 64bit Java", "ViaProxy", JOptionPane.ERROR_MESSAGE); - } - System.exit(1); - } - - if (Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) { - Logger.LOGGER.fatal("ViaProxy is not able to run with less than 256MB of RAM."); - if (useUI) { - JOptionPane.showMessageDialog(null, "ViaProxy is not able to run with less than 256MB of RAM.", "ViaProxy", JOptionPane.ERROR_MESSAGE); - } - System.exit(1); - } else if (Runtime.getRuntime().maxMemory() < 512 * 1024 * 1024) { - Logger.LOGGER.warn("ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected."); - if (useUI) { - JOptionPane.showMessageDialog(null, "ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.", "ViaProxy", JOptionPane.WARNING_MESSAGE); - } - } + SystemRequirementsCheck.run(useUI); } final SplashScreen splashScreen; @@ -216,14 +196,6 @@ public class ViaProxy { ViaProxy.loadNetty(); ClassLoaderPriorityUtil.loadOverridingJars(); - final File viaProxyConfigFile; - if (useConfig) { - viaProxyConfigFile = new File(args[1]); - } else { - viaProxyConfigFile = new File(ViaProxy.getCwd(), "viaproxy.yml"); - } - final boolean firstStart = !viaProxyConfigFile.exists(); - progressConsumer.accept("Loading Plugins"); PLUGIN_MANAGER = new PluginManager(); progressConsumer.accept("Loading Protocol Translators"); @@ -231,13 +203,14 @@ public class ViaProxy { progressConsumer.accept("Loading Saves"); SAVE_MANAGER = new SaveManager(); progressConsumer.accept("Loading Config"); - final ConfigLoader configLoader = new ConfigLoader<>(ViaProxyConfig.class); - configLoader.getConfigOptions().setResetInvalidOptions(true).setRewriteConfig(true).setCommentSpacing(1); - try { - CONFIG = configLoader.load(ConfigProvider.file(viaProxyConfigFile)).getConfigInstance(); - } catch (Throwable e) { - throw new RuntimeException("Failed to load config", e); + final File viaProxyConfigFile; + if (useConfig) { + viaProxyConfigFile = new File(args[1]); + } else { + viaProxyConfigFile = new File(ViaProxy.getCwd(), "viaproxy.yml"); } + final boolean firstStart = !viaProxyConfigFile.exists(); + CONFIG = ViaProxyConfig.create(viaProxyConfigFile); if (useUI) { progressConsumer.accept("Loading GUI"); @@ -275,11 +248,9 @@ public class ViaProxy { } EVENT_MANAGER.call(new ViaProxyLoadedEvent()); Logger.LOGGER.info("ViaProxy started successfully!"); - startProxy(); + ViaProxy.startProxy(); - while (true) { - Thread.sleep(Integer.MAX_VALUE); - } + Thread.sleep(Integer.MAX_VALUE); } } diff --git a/src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java b/src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java index 8b1b3d9..e26e796 100644 --- a/src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java +++ b/src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java @@ -29,6 +29,7 @@ import net.lenni0451.optconfig.index.ClassIndexer; import net.lenni0451.optconfig.index.ConfigType; import net.lenni0451.optconfig.index.types.ConfigOption; import net.lenni0451.optconfig.index.types.SectionIndex; +import net.lenni0451.optconfig.provider.ConfigProvider; import net.raphimc.viaproxy.ViaProxy; import net.raphimc.viaproxy.cli.BetterHelpFormatter; import net.raphimc.viaproxy.cli.HelpRequestedException; @@ -40,6 +41,7 @@ import net.raphimc.viaproxy.util.AddressUtil; import net.raphimc.viaproxy.util.config.*; import net.raphimc.viaproxy.util.logging.Logger; +import java.io.File; import java.net.SocketAddress; import java.net.URI; import java.util.HashMap; @@ -182,6 +184,16 @@ public class ViaProxyConfig { }) private boolean fakeAcceptResourcePacks = false; + public static ViaProxyConfig create(final File configFile) { + final ConfigLoader configLoader = new ConfigLoader<>(ViaProxyConfig.class); + configLoader.getConfigOptions().setResetInvalidOptions(true).setRewriteConfig(true).setCommentSpacing(1); + try { + return configLoader.load(ConfigProvider.file(configFile)).getConfigInstance(); + } catch (Throwable e) { + throw new RuntimeException("Failed to load config", e); + } + } + @SuppressWarnings("UnstableApiUsage") public void loadFromArguments(final String[] args) throws Exception { final OptionParser optionParser = new OptionParser(); diff --git a/src/main/java/net/raphimc/viaproxy/tasks/SystemRequirementsCheck.java b/src/main/java/net/raphimc/viaproxy/tasks/SystemRequirementsCheck.java new file mode 100644 index 0000000..54987c3 --- /dev/null +++ b/src/main/java/net/raphimc/viaproxy/tasks/SystemRequirementsCheck.java @@ -0,0 +1,49 @@ +/* + * This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy + * Copyright (C) 2021-2024 RK_01/RaphiMC and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.raphimc.viaproxy.tasks; + +import net.raphimc.viaproxy.util.logging.Logger; + +import javax.swing.*; + +public class SystemRequirementsCheck { + + public static void run(final boolean hasUI) { + if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) { + Logger.LOGGER.fatal("ViaProxy is not able to run on 32-Bit Java. Please install 64-Bit Java."); + if (hasUI) { + JOptionPane.showMessageDialog(null, "ViaProxy is not able to run on 32-Bit Java. Please install 64-Bit Java.", "ViaProxy", JOptionPane.ERROR_MESSAGE); + } + System.exit(1); + } + + if (Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) { + Logger.LOGGER.fatal("ViaProxy is not able to run with less than 256MB of RAM."); + if (hasUI) { + JOptionPane.showMessageDialog(null, "ViaProxy is not able to run with less than 256MB of RAM.", "ViaProxy", JOptionPane.ERROR_MESSAGE); + } + System.exit(1); + } else if (Runtime.getRuntime().maxMemory() < 512 * 1024 * 1024) { + Logger.LOGGER.warn("ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected."); + if (hasUI) { + JOptionPane.showMessageDialog(null, "ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.", "ViaProxy", JOptionPane.WARNING_MESSAGE); + } + } + } + +}