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; package org.prismlauncher.launcher;
import org.prismlauncher.launcher.impl.StandardLauncher; import org.prismlauncher.launcher.impl.StandardLauncher;
import org.prismlauncher.launcher.impl.legacy.LegacyLauncher; import org.prismlauncher.launcher.impl.legacy.LegacyLauncher;
import org.prismlauncher.utils.Parameters; import org.prismlauncher.utils.Parameters;
import java.util.HashMap;
import java.util.Map;
public final class LauncherFactory { 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() { private LauncherFactory() {
} }
public static Launcher createLauncher(Parameters parameters) { public static Launcher createLauncher(String launcherType, Parameters parameters) {
String name = parameters.getString("launcher"); return createLauncher(LauncherType.valueOf(launcherType.toUpperCase()), parameters);
}
LauncherProvider launcherProvider = launcherRegistry.get(name); public static Launcher createLauncher(LauncherType launcherType, Parameters parameters) {
LauncherProvider launcherProvider = launcherType.getLauncherProvider();
if (launcherProvider == null)
throw new IllegalArgumentException("Invalid launcher type: " + name);
return launcherProvider.provide(parameters); 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,14 +54,22 @@
package org.prismlauncher.launcher.impl; package org.prismlauncher.launcher.impl;
import org.prismlauncher.launcher.Launcher;
import org.prismlauncher.launcher.LauncherProvider;
import org.prismlauncher.utils.Parameters; import org.prismlauncher.utils.Parameters;
public final class StandardLauncher extends AbstractLauncher { public final class StandardLauncher extends AbstractLauncher {
public StandardLauncher(Parameters params) { public StandardLauncher(Parameters params) {
super(params); super(params);
} }
public static LauncherProvider getProvider() {
return new StandardLauncherProvider();
}
@Override @Override
public void launch() throws Throwable { public void launch() throws Throwable {
// window size, title and state // window size, title and state
@ -87,4 +95,11 @@ public final class StandardLauncher extends AbstractLauncher {
loadAndInvokeMain(); 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; 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.launcher.impl.AbstractLauncher;
import org.prismlauncher.utils.Parameters; import org.prismlauncher.utils.Parameters;
@ -70,6 +73,7 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* Used to launch old versions that support applets. * 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 static final Logger LOGGER = Logger.getLogger("LegacyLauncher");
private final String user, session; private final String user, session;
private final String title; private final String title;
private final String appletClass; private final String appletClass;
private final boolean noApplet; private final boolean noApplet;
private final String cwd; private final String cwd;
public LegacyLauncher(Parameters params) { public LegacyLauncher(Parameters params) {
@ -98,6 +105,35 @@ public final class LegacyLauncher extends AbstractLauncher {
cwd = System.getProperty("user.dir"); 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 @Override
public void launch() throws Throwable { public void launch() throws Throwable {
Class<?> main = loadMain(); Class<?> main = loadMain();
@ -143,27 +179,11 @@ public final class LegacyLauncher extends AbstractLauncher {
invokeMain(main); 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. private static class LegacyLauncherProvider implements LauncherProvider {
if (!Modifier.isStatic(field.getModifiers()) || !Modifier.isPrivate(field.getModifiers())) @Override
continue; public Launcher provide(Parameters parameters) {
return new LegacyLauncher(parameters);
return field;
} }
return null;
} }
} }