Get rid of gross HashMap being used by LauncherFactory and instead use *fancy* enum

Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
This commit is contained in:
solonovamax 2022-11-01 11:27:31 -04:00 committed by TheKodeToad
parent ac5b74301e
commit 9b8096c699
3 changed files with 84 additions and 50 deletions

View File

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

View File

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

View File

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