Skip to content

Commit 635d6d8

Browse files
committed
Decluttered main method a bit
1 parent 0c68f7c commit 635d6d8

File tree

3 files changed

+72
-40
lines changed

3 files changed

+72
-40
lines changed

src/main/java/net/raphimc/viaproxy/ViaProxy.java

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import net.lenni0451.classtransform.utils.tree.IClassProvider;
3131
import net.lenni0451.lambdaevents.LambdaManager;
3232
import net.lenni0451.lambdaevents.generator.LambdaMetaFactoryGenerator;
33-
import net.lenni0451.optconfig.ConfigLoader;
34-
import net.lenni0451.optconfig.provider.ConfigProvider;
3533
import net.lenni0451.reflect.Agents;
3634
import net.lenni0451.reflect.ClassLoaders;
3735
import net.lenni0451.reflect.JavaBypass;
@@ -50,6 +48,7 @@
5048
import net.raphimc.viaproxy.proxy.client2proxy.Client2ProxyHandler;
5149
import net.raphimc.viaproxy.proxy.session.ProxyConnection;
5250
import net.raphimc.viaproxy.saves.SaveManager;
51+
import net.raphimc.viaproxy.tasks.SystemRequirementsCheck;
5352
import net.raphimc.viaproxy.tasks.UpdateCheckTask;
5453
import net.raphimc.viaproxy.ui.SplashScreen;
5554
import net.raphimc.viaproxy.ui.ViaProxyWindow;
@@ -170,26 +169,7 @@ public static void injectedMain(final String injectionMethod, final String[] arg
170169
}
171170
}
172171
if (System.getProperty("ignoreSystemRequirements") == null) {
173-
if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
174-
Logger.LOGGER.fatal("ViaProxy is not able to run on 32bit Java.");
175-
if (useUI) {
176-
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run on 32bit Java. Please install 64bit Java", "ViaProxy", JOptionPane.ERROR_MESSAGE);
177-
}
178-
System.exit(1);
179-
}
180-
181-
if (Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
182-
Logger.LOGGER.fatal("ViaProxy is not able to run with less than 256MB of RAM.");
183-
if (useUI) {
184-
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run with less than 256MB of RAM.", "ViaProxy", JOptionPane.ERROR_MESSAGE);
185-
}
186-
System.exit(1);
187-
} else if (Runtime.getRuntime().maxMemory() < 512 * 1024 * 1024) {
188-
Logger.LOGGER.warn("ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.");
189-
if (useUI) {
190-
JOptionPane.showMessageDialog(null, "ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.", "ViaProxy", JOptionPane.WARNING_MESSAGE);
191-
}
192-
}
172+
SystemRequirementsCheck.run(useUI);
193173
}
194174

195175
final SplashScreen splashScreen;
@@ -216,28 +196,21 @@ public static void injectedMain(final String injectionMethod, final String[] arg
216196
ViaProxy.loadNetty();
217197
ClassLoaderPriorityUtil.loadOverridingJars();
218198

219-
final File viaProxyConfigFile;
220-
if (useConfig) {
221-
viaProxyConfigFile = new File(args[1]);
222-
} else {
223-
viaProxyConfigFile = new File(ViaProxy.getCwd(), "viaproxy.yml");
224-
}
225-
final boolean firstStart = !viaProxyConfigFile.exists();
226-
227199
progressConsumer.accept("Loading Plugins");
228200
PLUGIN_MANAGER = new PluginManager();
229201
progressConsumer.accept("Loading Protocol Translators");
230202
ProtocolTranslator.init();
231203
progressConsumer.accept("Loading Saves");
232204
SAVE_MANAGER = new SaveManager();
233205
progressConsumer.accept("Loading Config");
234-
final ConfigLoader<ViaProxyConfig> configLoader = new ConfigLoader<>(ViaProxyConfig.class);
235-
configLoader.getConfigOptions().setResetInvalidOptions(true).setRewriteConfig(true).setCommentSpacing(1);
236-
try {
237-
CONFIG = configLoader.load(ConfigProvider.file(viaProxyConfigFile)).getConfigInstance();
238-
} catch (Throwable e) {
239-
throw new RuntimeException("Failed to load config", e);
206+
final File viaProxyConfigFile;
207+
if (useConfig) {
208+
viaProxyConfigFile = new File(args[1]);
209+
} else {
210+
viaProxyConfigFile = new File(ViaProxy.getCwd(), "viaproxy.yml");
240211
}
212+
final boolean firstStart = !viaProxyConfigFile.exists();
213+
CONFIG = ViaProxyConfig.create(viaProxyConfigFile);
241214

242215
if (useUI) {
243216
progressConsumer.accept("Loading GUI");
@@ -275,11 +248,9 @@ public static void injectedMain(final String injectionMethod, final String[] arg
275248
}
276249
EVENT_MANAGER.call(new ViaProxyLoadedEvent());
277250
Logger.LOGGER.info("ViaProxy started successfully!");
278-
startProxy();
251+
ViaProxy.startProxy();
279252

280-
while (true) {
281-
Thread.sleep(Integer.MAX_VALUE);
282-
}
253+
Thread.sleep(Integer.MAX_VALUE);
283254
}
284255
}
285256

src/main/java/net/raphimc/viaproxy/protocoltranslator/viaproxy/ViaProxyConfig.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import net.lenni0451.optconfig.index.ConfigType;
3030
import net.lenni0451.optconfig.index.types.ConfigOption;
3131
import net.lenni0451.optconfig.index.types.SectionIndex;
32+
import net.lenni0451.optconfig.provider.ConfigProvider;
3233
import net.raphimc.viaproxy.ViaProxy;
3334
import net.raphimc.viaproxy.cli.BetterHelpFormatter;
3435
import net.raphimc.viaproxy.cli.HelpRequestedException;
@@ -40,6 +41,7 @@
4041
import net.raphimc.viaproxy.util.config.*;
4142
import net.raphimc.viaproxy.util.logging.Logger;
4243

44+
import java.io.File;
4345
import java.net.SocketAddress;
4446
import java.net.URI;
4547
import java.util.HashMap;
@@ -182,6 +184,16 @@ public class ViaProxyConfig {
182184
})
183185
private boolean fakeAcceptResourcePacks = false;
184186

187+
public static ViaProxyConfig create(final File configFile) {
188+
final ConfigLoader<ViaProxyConfig> configLoader = new ConfigLoader<>(ViaProxyConfig.class);
189+
configLoader.getConfigOptions().setResetInvalidOptions(true).setRewriteConfig(true).setCommentSpacing(1);
190+
try {
191+
return configLoader.load(ConfigProvider.file(configFile)).getConfigInstance();
192+
} catch (Throwable e) {
193+
throw new RuntimeException("Failed to load config", e);
194+
}
195+
}
196+
185197
@SuppressWarnings("UnstableApiUsage")
186198
public void loadFromArguments(final String[] args) throws Exception {
187199
final OptionParser optionParser = new OptionParser();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
3+
* Copyright (C) 2021-2024 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.viaproxy.tasks;
19+
20+
import net.raphimc.viaproxy.util.logging.Logger;
21+
22+
import javax.swing.*;
23+
24+
public class SystemRequirementsCheck {
25+
26+
public static void run(final boolean hasUI) {
27+
if ("32".equals(System.getProperty("sun.arch.data.model")) && Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
28+
Logger.LOGGER.fatal("ViaProxy is not able to run on 32-Bit Java. Please install 64-Bit Java.");
29+
if (hasUI) {
30+
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run on 32-Bit Java. Please install 64-Bit Java.", "ViaProxy", JOptionPane.ERROR_MESSAGE);
31+
}
32+
System.exit(1);
33+
}
34+
35+
if (Runtime.getRuntime().maxMemory() < 256 * 1024 * 1024) {
36+
Logger.LOGGER.fatal("ViaProxy is not able to run with less than 256MB of RAM.");
37+
if (hasUI) {
38+
JOptionPane.showMessageDialog(null, "ViaProxy is not able to run with less than 256MB of RAM.", "ViaProxy", JOptionPane.ERROR_MESSAGE);
39+
}
40+
System.exit(1);
41+
} else if (Runtime.getRuntime().maxMemory() < 512 * 1024 * 1024) {
42+
Logger.LOGGER.warn("ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.");
43+
if (hasUI) {
44+
JOptionPane.showMessageDialog(null, "ViaProxy has less than 512MB of RAM. This may cause issues with multiple clients connected.", "ViaProxy", JOptionPane.WARNING_MESSAGE);
45+
}
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)