Skip to content

Commit

Permalink
Decluttered main method a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphiMC committed Dec 12, 2024
1 parent 0c68f7c commit 635d6d8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 40 deletions.
51 changes: 11 additions & 40 deletions src/main/java/net/raphimc/viaproxy/ViaProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
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;
Expand All @@ -50,6 +48,7 @@
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;
Expand Down Expand Up @@ -170,26 +169,7 @@ public static void injectedMain(final String injectionMethod, final String[] arg
}
}
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;
Expand All @@ -216,28 +196,21 @@ public static void injectedMain(final String injectionMethod, final String[] arg
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");
ProtocolTranslator.init();
progressConsumer.accept("Loading Saves");
SAVE_MANAGER = new SaveManager();
progressConsumer.accept("Loading Config");
final ConfigLoader<ViaProxyConfig> 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");
Expand Down Expand Up @@ -275,11 +248,9 @@ public static void injectedMain(final String injectionMethod, final String[] arg
}
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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;
Expand All @@ -40,6 +41,7 @@
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;
Expand Down Expand Up @@ -182,6 +184,16 @@ public class ViaProxyConfig {
})
private boolean fakeAcceptResourcePacks = false;

public static ViaProxyConfig create(final File configFile) {
final ConfigLoader<ViaProxyConfig> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}

}

0 comments on commit 635d6d8

Please sign in to comment.