From 107fa6b4f73c4b9178e5055995500fa9ad75da27 Mon Sep 17 00:00:00 2001 From: solonovamax Date: Thu, 27 Oct 2022 19:52:09 -0400 Subject: [PATCH] Code refactors - Refactor LauncherFactory.LauncherProvider to LauncherFactory - Refactor all launcher related components to launcher package - some basic code cleanup - Rename all, allSafe -> getList and first, firstSafe -> getString - Rename Utils -> LegacyUtils Signed-off-by: solonovamax --- libraries/launcher/CMakeLists.txt | 13 +- .../launcher/net/minecraft/Launcher.java | 182 +++++++++--------- .../org/prismlauncher/EntryPoint.java | 4 +- .../{ => launcher}/Launcher.java | 5 +- .../{ => launcher}/LauncherFactory.java | 17 +- .../launcher/LauncherProvider.java | 44 +++++ .../{ => launcher}/impl/AbstractLauncher.java | 43 +++-- .../{ => launcher}/impl/LegacyLauncher.java | 29 +-- .../{ => launcher}/impl/StandardLauncher.java | 2 +- .../utils/{Utils.java => LegacyUtils.java} | 27 +-- .../org/prismlauncher/utils/Parameters.java | 32 +-- 11 files changed, 227 insertions(+), 171 deletions(-) rename libraries/launcher/org/prismlauncher/{ => launcher}/Launcher.java (94%) rename libraries/launcher/org/prismlauncher/{ => launcher}/LauncherFactory.java (89%) create mode 100644 libraries/launcher/org/prismlauncher/launcher/LauncherProvider.java rename libraries/launcher/org/prismlauncher/{ => launcher}/impl/AbstractLauncher.java (80%) rename libraries/launcher/org/prismlauncher/{ => launcher}/impl/LegacyLauncher.java (86%) rename libraries/launcher/org/prismlauncher/{ => launcher}/impl/StandardLauncher.java (97%) rename libraries/launcher/org/prismlauncher/utils/{Utils.java => LegacyUtils.java} (71%) diff --git a/libraries/launcher/CMakeLists.txt b/libraries/launcher/CMakeLists.txt index ee88d0f90..d74c36413 100644 --- a/libraries/launcher/CMakeLists.txt +++ b/libraries/launcher/CMakeLists.txt @@ -8,16 +8,17 @@ set(CMAKE_JAVA_COMPILE_FLAGS -target 7 -source 7 -Xlint:deprecation -Xlint:unche set(SRC org/prismlauncher/EntryPoint.java - org/prismlauncher/Launcher.java - org/prismlauncher/LauncherFactory.java - org/prismlauncher/impl/AbstractLauncher.java - org/prismlauncher/impl/LegacyLauncher.java - org/prismlauncher/impl/StandardLauncher.java + org/prismlauncher/launcher/Launcher.java + org/prismlauncher/launcher/LauncherFactory.java + org/prismlauncher/launcher/LauncherProvider.java + org/prismlauncher/launcher/impl/AbstractLauncher.java + org/prismlauncher/launcher/impl/LegacyLauncher.java + org/prismlauncher/launcher/impl/StandardLauncher.java org/prismlauncher/applet/LegacyFrame.java org/prismlauncher/exception/ParameterNotFoundException.java org/prismlauncher/exception/ParseException.java org/prismlauncher/utils/Parameters.java - org/prismlauncher/utils/Utils.java + org/prismlauncher/utils/LegacyUtils.java net/minecraft/Launcher.java ) add_jar(NewLaunch ${SRC}) diff --git a/libraries/launcher/net/minecraft/Launcher.java b/libraries/launcher/net/minecraft/Launcher.java index 6bf671bec..f9fd0a976 100644 --- a/libraries/launcher/net/minecraft/Launcher.java +++ b/libraries/launcher/net/minecraft/Launcher.java @@ -16,157 +16,167 @@ package net.minecraft; + import java.applet.Applet; import java.applet.AppletStub; -import java.awt.*; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Graphics; import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import java.util.TreeMap; + /* * WARNING: This class is reflectively accessed by legacy Forge versions. * Changing field and method declarations without further testing is not recommended. */ public final class Launcher extends Applet implements AppletStub { - + private final Map params = new TreeMap<>(); - + private Applet wrappedApplet; - - private URL documentBase; - + + private final URL documentBase; + private boolean active = false; - + public Launcher(Applet applet) { this(applet, null); } - + public Launcher(Applet applet, URL documentBase) { this.setLayout(new BorderLayout()); - + this.add(applet, "Center"); - + this.wrappedApplet = applet; - + try { if (documentBase != null) { this.documentBase = documentBase; } else if (applet.getClass().getPackage().getName().startsWith("com.mojang")) { // Special case only for Classic versions - + + // TODO: 2022-10-27 Can this be changed to https this.documentBase = new URL("http", "www.minecraft.net", 80, "/game/"); } else { + // TODO: 2022-10-27 Can this be changed to https? this.documentBase = new URL("http://www.minecraft.net/game/"); } } catch (MalformedURLException e) { throw new RuntimeException(e); } } - + public void replace(Applet applet) { this.wrappedApplet = applet; - + applet.setStub(this); applet.setSize(getWidth(), getHeight()); - + this.setLayout(new BorderLayout()); this.add(applet, "Center"); - + applet.init(); - + active = true; - + applet.start(); - + validate(); } - - public void setParameter(String name, String value) { - params.put(name, value); - } - - @Override - public String getParameter(String name) { - String param = params.get(name); - - if (param != null) - return param; - - try { - return super.getParameter(name); - } catch (Exception ignored) {} - - return null; - } - + @Override public boolean isActive() { return active; } - + @Override - public void appletResize(int width, int height) { - wrappedApplet.resize(width, height); + public URL getDocumentBase() { + return documentBase; } - - @Override - public void resize(int width, int height) { - wrappedApplet.resize(width, height); - } - - @Override - public void resize(Dimension d) { - wrappedApplet.resize(d); - } - - @Override - public void init() { - if (wrappedApplet != null) - wrappedApplet.init(); - } - - @Override - public void start() { - wrappedApplet.start(); - - active = true; - } - - @Override - public void stop() { - wrappedApplet.stop(); - - active = false; - } - - public void destroy() { - wrappedApplet.destroy(); - } - + @Override public URL getCodeBase() { try { + // TODO: 2022-10-27 Can this be changed to https? return new URL("http://www.minecraft.net/game/"); } catch (MalformedURLException e) { throw new RuntimeException(e); } } - + @Override - public URL getDocumentBase() { - return documentBase; + public String getParameter(String name) { + String param = params.get(name); + + if (param != null) + return param; + + try { + return super.getParameter(name); + } catch (Exception ignored) { + } + + return null; } - + + @Override + public void resize(int width, int height) { + wrappedApplet.resize(width, height); + } + + @Override + public void resize(Dimension d) { + wrappedApplet.resize(d); + } + + @Override + public void init() { + if (wrappedApplet != null) + wrappedApplet.init(); + } + + @Override + public void start() { + wrappedApplet.start(); + + active = true; + } + + @Override + public void stop() { + wrappedApplet.stop(); + + active = false; + } + + public void destroy() { + wrappedApplet.destroy(); + } + + @Override + public void appletResize(int width, int height) { + wrappedApplet.resize(width, height); + } + @Override public void setVisible(boolean b) { super.setVisible(b); - + wrappedApplet.setVisible(b); } - - public void update(Graphics paramGraphics) {} - - public void paint(Graphics paramGraphics) {} - + + public void paint(Graphics paramGraphics) { + } + + public void update(Graphics paramGraphics) { + } + + public void setParameter(String name, String value) { + params.put(name, value); + } + } diff --git a/libraries/launcher/org/prismlauncher/EntryPoint.java b/libraries/launcher/org/prismlauncher/EntryPoint.java index 37db6a5da..44e947b1a 100644 --- a/libraries/launcher/org/prismlauncher/EntryPoint.java +++ b/libraries/launcher/org/prismlauncher/EntryPoint.java @@ -53,6 +53,8 @@ package org.prismlauncher; import org.prismlauncher.exception.ParseException; +import org.prismlauncher.launcher.Launcher; +import org.prismlauncher.launcher.LauncherFactory; import org.prismlauncher.utils.Parameters; import java.io.BufferedReader; @@ -81,7 +83,7 @@ public final class EntryPoint { } private Action parseLine(String inData) throws ParseException { - if (inData.length() == 0) + if (inData.isEmpty()) throw new ParseException("Unexpected empty string!"); String first = inData; diff --git a/libraries/launcher/org/prismlauncher/Launcher.java b/libraries/launcher/org/prismlauncher/launcher/Launcher.java similarity index 94% rename from libraries/launcher/org/prismlauncher/Launcher.java rename to libraries/launcher/org/prismlauncher/launcher/Launcher.java index 50c2c9c8f..1cea255c8 100644 --- a/libraries/launcher/org/prismlauncher/Launcher.java +++ b/libraries/launcher/org/prismlauncher/launcher/Launcher.java @@ -14,10 +14,9 @@ * limitations under the License. */ -package org.prismlauncher; +package org.prismlauncher.launcher; + public interface Launcher { - void launch() throws Throwable; - } diff --git a/libraries/launcher/org/prismlauncher/LauncherFactory.java b/libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java similarity index 89% rename from libraries/launcher/org/prismlauncher/LauncherFactory.java rename to libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java index 5b18cc5f6..6c6011711 100644 --- a/libraries/launcher/org/prismlauncher/LauncherFactory.java +++ b/libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-3.0-only /* - * PolyMC - Minecraft Launcher + * Prism Launcher - Minecraft Launcher * Copyright (C) 2022 icelimetea, * * This program is free software: you can redistribute it and/or modify @@ -33,11 +33,11 @@ * along with this program. If not, see . */ -package org.prismlauncher; +package org.prismlauncher.launcher; -import org.prismlauncher.impl.LegacyLauncher; -import org.prismlauncher.impl.StandardLauncher; +import org.prismlauncher.launcher.impl.LegacyLauncher; +import org.prismlauncher.launcher.impl.StandardLauncher; import org.prismlauncher.utils.Parameters; import java.util.HashMap; @@ -65,7 +65,7 @@ public final class LauncherFactory { } public static Launcher createLauncher(Parameters parameters) { - String name = parameters.first("launcher"); + String name = parameters.getString("launcher"); LauncherProvider launcherProvider = launcherRegistry.get(name); @@ -74,11 +74,4 @@ public final class LauncherFactory { return launcherProvider.provide(parameters); } - - public interface LauncherProvider { - - Launcher provide(Parameters parameters); - - } - } diff --git a/libraries/launcher/org/prismlauncher/launcher/LauncherProvider.java b/libraries/launcher/org/prismlauncher/launcher/LauncherProvider.java new file mode 100644 index 000000000..b6d1caabc --- /dev/null +++ b/libraries/launcher/org/prismlauncher/launcher/LauncherProvider.java @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * PolyMC - Minecraft Launcher + * Copyright (C) 2022 solonovamax, + * + * 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, version 3. + * + * 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. + * + * Linking this library statically or dynamically with other modules is + * making a combined work based on this library. Thus, the terms and + * conditions of the GNU General Public License cover the whole + * combination. + * + * As a special exception, the copyright holders of this library give + * you permission to link this library with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also meet, + * for each linked independent module, the terms and conditions of the + * license of that module. An independent module is a module which is + * not derived from or based on this library. If you modify this + * library, you may extend this exception to your version of the + * library, but you are not obliged to do so. If you do not wish to do + * so, delete this exception statement from your version. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.prismlauncher.launcher; + + +import org.prismlauncher.utils.Parameters; + + +public interface LauncherProvider { + Launcher provide(Parameters parameters); +} diff --git a/libraries/launcher/org/prismlauncher/impl/AbstractLauncher.java b/libraries/launcher/org/prismlauncher/launcher/impl/AbstractLauncher.java similarity index 80% rename from libraries/launcher/org/prismlauncher/impl/AbstractLauncher.java rename to libraries/launcher/org/prismlauncher/launcher/impl/AbstractLauncher.java index 49a984f52..637c5da78 100644 --- a/libraries/launcher/org/prismlauncher/impl/AbstractLauncher.java +++ b/libraries/launcher/org/prismlauncher/launcher/impl/AbstractLauncher.java @@ -13,21 +13,22 @@ * limitations under the License. */ -package org.prismlauncher.impl; +package org.prismlauncher.launcher.impl; + + +import org.prismlauncher.exception.ParseException; +import org.prismlauncher.launcher.Launcher; +import org.prismlauncher.utils.Parameters; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import org.prismlauncher.Launcher; -import org.prismlauncher.exception.ParseException; -import org.prismlauncher.utils.Parameters; public abstract class AbstractLauncher implements Launcher { - + private static final int DEFAULT_WINDOW_WIDTH = 854; private static final int DEFAULT_WINDOW_HEIGHT = 480; @@ -43,21 +44,21 @@ public abstract class AbstractLauncher implements Launcher { protected final String serverAddress, serverPort; protected final ClassLoader classLoader; - - public AbstractLauncher(Parameters params) { + + protected AbstractLauncher(Parameters params) { classLoader = ClassLoader.getSystemClassLoader(); - - mcParams = params.allSafe("param", new ArrayList()); - mainClass = params.firstSafe("mainClass", "net.minecraft.client.Minecraft"); - - serverAddress = params.firstSafe("serverAddress", null); - serverPort = params.firstSafe("serverPort", null); - - String windowParams = params.firstSafe("windowParams", null); - + + mcParams = params.getList("param", new ArrayList()); + mainClass = params.getString("mainClass", "net.minecraft.client.Minecraft"); + + serverAddress = params.getString("serverAddress", null); + serverPort = params.getString("serverPort", null); + + String windowParams = params.getString("windowParams", null); + if ("max".equals(windowParams) || windowParams == null) { maximize = windowParams != null; - + width = DEFAULT_WINDOW_WIDTH; height = DEFAULT_WINDOW_HEIGHT; } else { @@ -70,7 +71,7 @@ public abstract class AbstractLauncher implements Launcher { width = Integer.parseInt(windowParams.substring(0, byIndex)); height = Integer.parseInt(windowParams.substring(byIndex + 1)); return; - } catch(NumberFormatException pass) { + } catch (NumberFormatException ignored) { } } @@ -81,8 +82,8 @@ public abstract class AbstractLauncher implements Launcher { protected Class loadMain() throws ClassNotFoundException { return classLoader.loadClass(mainClass); } - - protected void loadAndInvokeMain() throws Throwable, ClassNotFoundException { + + protected void loadAndInvokeMain() throws Throwable { invokeMain(loadMain()); } diff --git a/libraries/launcher/org/prismlauncher/impl/LegacyLauncher.java b/libraries/launcher/org/prismlauncher/launcher/impl/LegacyLauncher.java similarity index 86% rename from libraries/launcher/org/prismlauncher/impl/LegacyLauncher.java rename to libraries/launcher/org/prismlauncher/launcher/impl/LegacyLauncher.java index 30a4dba79..181156c6d 100644 --- a/libraries/launcher/org/prismlauncher/impl/LegacyLauncher.java +++ b/libraries/launcher/org/prismlauncher/launcher/impl/LegacyLauncher.java @@ -13,7 +13,12 @@ * limitations under the License. */ -package org.prismlauncher.impl; +package org.prismlauncher.launcher.impl; + + +import org.prismlauncher.applet.LegacyFrame; +import org.prismlauncher.utils.LegacyUtils; +import org.prismlauncher.utils.Parameters; import java.applet.Applet; import java.io.File; @@ -26,10 +31,6 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import org.prismlauncher.applet.LegacyFrame; -import org.prismlauncher.utils.Parameters; -import org.prismlauncher.utils.Utils; - @SuppressWarnings("removal") public final class LegacyLauncher extends AbstractLauncher { @@ -44,22 +45,22 @@ public final class LegacyLauncher extends AbstractLauncher { public LegacyLauncher(Parameters params) { super(params); - - user = params.first("userName"); - session = params.first("sessionId"); - title = params.firstSafe("windowTitle", "Minecraft"); - appletClass = params.firstSafe("appletClass", "net.minecraft.client.MinecraftApplet"); - - List traits = params.allSafe("traits", Collections.emptyList()); + + user = params.getString("userName"); + session = params.getString("sessionId"); + title = params.getString("windowTitle", "Minecraft"); + appletClass = params.getString("appletClass", "net.minecraft.client.MinecraftApplet"); + + List traits = params.getList("traits", Collections.emptyList()); noApplet = traits.contains("noapplet"); - + cwd = System.getProperty("user.dir"); } @Override public void launch() throws Throwable { Class main = loadMain(); - Field gameDirField = Utils.getMinecraftGameDirField(main); + Field gameDirField = LegacyUtils.getMinecraftGameDirField(main); if (gameDirField == null) { LOGGER.warning("Could not find Mineraft path field."); diff --git a/libraries/launcher/org/prismlauncher/impl/StandardLauncher.java b/libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java similarity index 97% rename from libraries/launcher/org/prismlauncher/impl/StandardLauncher.java rename to libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java index c651b060a..c1d33958a 100644 --- a/libraries/launcher/org/prismlauncher/impl/StandardLauncher.java +++ b/libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java @@ -13,7 +13,7 @@ * limitations under the License. */ -package org.prismlauncher.impl; +package org.prismlauncher.launcher.impl; import org.prismlauncher.utils.Parameters; diff --git a/libraries/launcher/org/prismlauncher/utils/Utils.java b/libraries/launcher/org/prismlauncher/utils/LegacyUtils.java similarity index 71% rename from libraries/launcher/org/prismlauncher/utils/Utils.java rename to libraries/launcher/org/prismlauncher/utils/LegacyUtils.java index 79f5367b6..7607d7314 100644 --- a/libraries/launcher/org/prismlauncher/utils/Utils.java +++ b/libraries/launcher/org/prismlauncher/utils/LegacyUtils.java @@ -16,34 +16,39 @@ package org.prismlauncher.utils; + import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Modifier; -public final class Utils { - - private Utils() {} +public final class LegacyUtils { + + private LegacyUtils() { + } + /** * Finds a field that looks like a Minecraft base folder in a supplied class * * @param clazz the class to scan */ public static Field getMinecraftGameDirField(Class clazz) { - for (Field f : clazz.getDeclaredFields()) { + // Field we're looking for is always + // private static File obfuscatedName = null; + for (Field field : clazz.getDeclaredFields()) { // Has to be File - if (f.getType() != File.class) + if (field.getType() != File.class) continue; - + // And Private Static. - if (!Modifier.isStatic(f.getModifiers()) || !Modifier.isPrivate(f.getModifiers())) + if (!Modifier.isStatic(field.getModifiers()) || !Modifier.isPrivate(field.getModifiers())) continue; - - return f; + + return field; } - + return null; } - + } diff --git a/libraries/launcher/org/prismlauncher/utils/Parameters.java b/libraries/launcher/org/prismlauncher/utils/Parameters.java index dcaba18d1..00b0701f0 100644 --- a/libraries/launcher/org/prismlauncher/utils/Parameters.java +++ b/libraries/launcher/org/prismlauncher/utils/Parameters.java @@ -39,39 +39,39 @@ public final class Parameters { params.add(value); } - public List all(String key) throws ParameterNotFoundException { + public List getList(String key) throws ParameterNotFoundException { List params = map.get(key); - + if (params == null) throw new ParameterNotFoundException(key); - + return params; } - - public List allSafe(String key, List def) { + + public List getList(String key, List def) { List params = map.get(key); - + if (params == null || params.isEmpty()) return def; - + return params; } - - public String first(String key) throws ParameterNotFoundException { - List list = all(key); - + + public String getString(String key) throws ParameterNotFoundException { + List list = getList(key); + if (list.isEmpty()) throw new ParameterNotFoundException(key); - + return list.get(0); } - - public String firstSafe(String key, String def) { + + public String getString(String key, String def) { List params = map.get(key); - + if (params == null || params.isEmpty()) return def; - + return params.get(0); }