From 9b8096c6993df68ac99c5c24483e169fbec60979 Mon Sep 17 00:00:00 2001 From: solonovamax Date: Tue, 1 Nov 2022 11:27:31 -0400 Subject: [PATCH] Get rid of gross HashMap being used by `LauncherFactory` and instead use *fancy* enum Signed-off-by: solonovamax --- .../launcher/LauncherFactory.java | 49 +++++++-------- .../launcher/impl/StandardLauncher.java | 23 +++++-- .../launcher/impl/legacy/LegacyLauncher.java | 62 ++++++++++++------- 3 files changed, 84 insertions(+), 50 deletions(-) diff --git a/libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java b/libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java index 22e926323..6f9be63c4 100644 --- a/libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java +++ b/libraries/launcher/org/prismlauncher/launcher/LauncherFactory.java @@ -56,43 +56,42 @@ package org.prismlauncher.launcher; + import org.prismlauncher.launcher.impl.StandardLauncher; import org.prismlauncher.launcher.impl.legacy.LegacyLauncher; import org.prismlauncher.utils.Parameters; -import java.util.HashMap; -import java.util.Map; public final class LauncherFactory { - - private static final Map launcherRegistry = new HashMap<>(); - - static { - launcherRegistry.put("standard", new LauncherProvider() { - @Override - public Launcher provide(Parameters parameters) { - return new StandardLauncher(parameters); - } - }); - launcherRegistry.put("legacy", new LauncherProvider() { - @Override - public Launcher provide(Parameters parameters) { - return new LegacyLauncher(parameters); - } - }); - } private LauncherFactory() { } - public static Launcher createLauncher(Parameters parameters) { - String name = parameters.getString("launcher"); + public static Launcher createLauncher(String launcherType, Parameters parameters) { + return createLauncher(LauncherType.valueOf(launcherType.toUpperCase()), parameters); + } - LauncherProvider launcherProvider = launcherRegistry.get(name); - - if (launcherProvider == null) - throw new IllegalArgumentException("Invalid launcher type: " + name); + public static Launcher createLauncher(LauncherType launcherType, Parameters parameters) { + LauncherProvider launcherProvider = launcherType.getLauncherProvider(); return launcherProvider.provide(parameters); } + public static Launcher createLauncher(Parameters parameters) { + return createLauncher(parameters.getString("launcher"), parameters); + } + + public enum LauncherType { + STANDARD(StandardLauncher.getProvider()), + LEGACY(LegacyLauncher.getProvider()); + + private final LauncherProvider launcherProvider; + + LauncherType(LauncherProvider launcherProvider) { + this.launcherProvider = launcherProvider; + } + + public LauncherProvider getLauncherProvider() { + return launcherProvider; + } + } } diff --git a/libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java b/libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java index 5c884e999..24b12c951 100644 --- a/libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java +++ b/libraries/launcher/org/prismlauncher/launcher/impl/StandardLauncher.java @@ -54,15 +54,23 @@ package org.prismlauncher.launcher.impl; + +import org.prismlauncher.launcher.Launcher; +import org.prismlauncher.launcher.LauncherProvider; import org.prismlauncher.utils.Parameters; + public final class StandardLauncher extends AbstractLauncher { - public StandardLauncher(Parameters params) { - super(params); - } + public StandardLauncher(Parameters params) { + super(params); + } - @Override + public static LauncherProvider getProvider() { + return new StandardLauncherProvider(); + } + + @Override public void launch() throws Throwable { // window size, title and state @@ -87,4 +95,11 @@ public final class StandardLauncher extends AbstractLauncher { loadAndInvokeMain(); } + + private static class StandardLauncherProvider implements LauncherProvider { + @Override + public Launcher provide(Parameters parameters) { + return new StandardLauncher(parameters); + } + } } diff --git a/libraries/launcher/org/prismlauncher/launcher/impl/legacy/LegacyLauncher.java b/libraries/launcher/org/prismlauncher/launcher/impl/legacy/LegacyLauncher.java index d7a98352f..e342e788e 100644 --- a/libraries/launcher/org/prismlauncher/launcher/impl/legacy/LegacyLauncher.java +++ b/libraries/launcher/org/prismlauncher/launcher/impl/legacy/LegacyLauncher.java @@ -55,6 +55,9 @@ package org.prismlauncher.launcher.impl.legacy; + +import org.prismlauncher.launcher.Launcher; +import org.prismlauncher.launcher.LauncherProvider; import org.prismlauncher.launcher.impl.AbstractLauncher; import org.prismlauncher.utils.Parameters; @@ -70,6 +73,7 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; + /** * Used to launch old versions that support applets. */ @@ -78,10 +82,13 @@ public final class LegacyLauncher extends AbstractLauncher { private static final Logger LOGGER = Logger.getLogger("LegacyLauncher"); private final String user, session; + private final String title; + private final String appletClass; private final boolean noApplet; + private final String cwd; public LegacyLauncher(Parameters params) { @@ -98,6 +105,35 @@ public final class LegacyLauncher extends AbstractLauncher { cwd = System.getProperty("user.dir"); } + public static LauncherProvider getProvider() { + return new LegacyLauncherProvider(); + } + + /** + * Finds a field that looks like a Minecraft base folder in a supplied class + * + * @param clazz the class to scan + * + * @return The found field. + */ + private static Field getMinecraftGameDirField(Class clazz) { + // Field we're looking for is always + // private static File obfuscatedName = null; + for (Field field : clazz.getDeclaredFields()) { + // Has to be File + if (field.getType() != File.class) + continue; + + // And Private Static. + if (!Modifier.isStatic(field.getModifiers()) || !Modifier.isPrivate(field.getModifiers())) + continue; + + return field; + } + + return null; + } + @Override public void launch() throws Throwable { Class main = loadMain(); @@ -130,7 +166,7 @@ public final class LegacyLauncher extends AbstractLauncher { serverAddress, serverPort, mcParams.contains("--demo") - ); + ); return; } catch (Throwable e) { @@ -143,27 +179,11 @@ public final class LegacyLauncher extends AbstractLauncher { invokeMain(main); } - /** - * Finds a field that looks like a Minecraft base folder in a supplied class - * @param clazz the class to scan - * @return The found field. - */ - private static Field getMinecraftGameDirField(Class clazz) { - // Field we're looking for is always - // private static File obfuscatedName = null; - for (Field field : clazz.getDeclaredFields()) { - // Has to be File - if (field.getType() != File.class) - continue; - // And Private Static. - if (!Modifier.isStatic(field.getModifiers()) || !Modifier.isPrivate(field.getModifiers())) - continue; - - return field; + private static class LegacyLauncherProvider implements LauncherProvider { + @Override + public Launcher provide(Parameters parameters) { + return new LegacyLauncher(parameters); } - - return null; } - }