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 <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax 2022-10-27 19:52:09 -04:00 committed by TheKodeToad
parent 9062d28704
commit 107fa6b4f7
11 changed files with 227 additions and 171 deletions

View File

@ -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})

View File

@ -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<String, String> 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);
}
}

View File

@ -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;

View File

@ -14,10 +14,9 @@
* limitations under the License.
*/
package org.prismlauncher;
package org.prismlauncher.launcher;
public interface Launcher {
void launch() throws Throwable;
}

View File

@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 icelimetea, <fr3shtea@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
@ -33,11 +33,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -0,0 +1,44 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (C) 2022 solonovamax, <solonovamax@12oclockpoint.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/
package org.prismlauncher.launcher;
import org.prismlauncher.utils.Parameters;
public interface LauncherProvider {
Launcher provide(Parameters parameters);
}

View File

@ -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<String>());
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<String>());
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());
}

View File

@ -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<String> traits = params.allSafe("traits", Collections.<String>emptyList());
user = params.getString("userName");
session = params.getString("sessionId");
title = params.getString("windowTitle", "Minecraft");
appletClass = params.getString("appletClass", "net.minecraft.client.MinecraftApplet");
List<String> traits = params.getList("traits", Collections.<String>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.");

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
package org.prismlauncher.impl;
package org.prismlauncher.launcher.impl;
import org.prismlauncher.utils.Parameters;

View File

@ -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;
}
}

View File

@ -39,39 +39,39 @@ public final class Parameters {
params.add(value);
}
public List<String> all(String key) throws ParameterNotFoundException {
public List<String> getList(String key) throws ParameterNotFoundException {
List<String> params = map.get(key);
if (params == null)
throw new ParameterNotFoundException(key);
return params;
}
public List<String> allSafe(String key, List<String> def) {
public List<String> getList(String key, List<String> def) {
List<String> params = map.get(key);
if (params == null || params.isEmpty())
return def;
return params;
}
public String first(String key) throws ParameterNotFoundException {
List<String> list = all(key);
public String getString(String key) throws ParameterNotFoundException {
List<String> 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<String> params = map.get(key);
if (params == null || params.isEmpty())
return def;
return params.get(0);
}