106 lines
4.6 KiB
Java
106 lines
4.6 KiB
Java
package io.gitlab.jfronny.inceptum.launcher;
|
|
|
|
import io.gitlab.jfronny.commons.io.JFiles;
|
|
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder;
|
|
import io.gitlab.jfronny.inceptum.common.*;
|
|
import io.gitlab.jfronny.inceptum.launcher.api.McApi;
|
|
import io.gitlab.jfronny.inceptum.launcher.api.account.MicrosoftAccount;
|
|
import io.gitlab.jfronny.inceptum.launcher.gson.*;
|
|
import io.gitlab.jfronny.inceptum.launcher.model.microsoft.OauthTokenResponse;
|
|
import io.gitlab.jfronny.inceptum.launcher.model.mojang.MinecraftArgument;
|
|
import io.gitlab.jfronny.inceptum.launcher.model.mojang.Rules;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.mds.ModsDirScanner;
|
|
import io.gitlab.jfronny.inceptum.launcher.system.source.ModSource;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.util.function.Consumer;
|
|
|
|
public class LauncherEnv {
|
|
private static EnvBackend backend;
|
|
|
|
public static void checkClassLoaderState() {
|
|
if (Utils.class.getClassLoader() != LauncherEnv.class.getClassLoader()) {
|
|
throw new IllegalStateException("Mismatching classloader between two classes. Something is wrong here");
|
|
}
|
|
}
|
|
|
|
public static void initialize(EnvBackend backend) throws IOException {
|
|
LauncherEnv.backend = backend;
|
|
|
|
GsonHolder.registerTypeAdapter(MinecraftArgument.class, new MinecraftArgumentDeserializer());
|
|
GsonHolder.registerTypeAdapter(Rules.class, new RulesDeserializer());
|
|
GsonHolder.registerTypeAdapter(OauthTokenResponse.class, new OauthTokenResponseDeserializer());
|
|
GsonHolder.registerTypeAdapter(ModSource.class, new ModSourceTypeAdapter());
|
|
GsonHolder.registerTypeAdapter(ModSourceMapDeserializer.modSourceMapType, new ModSourceMapDeserializer());
|
|
|
|
InceptumEnvironmentInitializer.initialize();
|
|
|
|
if (!Files.exists(MetaHolder.CACHE_DIR)) Files.createDirectories(MetaHolder.CACHE_DIR);
|
|
try {
|
|
McApi.getVersions();
|
|
JFiles.clearDirectory(MetaHolder.CACHE_DIR);
|
|
} catch (IOException e) {
|
|
Utils.LOGGER.error("Could not connect to the internet");
|
|
}
|
|
if (!Files.exists(MetaHolder.INSTANCE_DIR)) Files.createDirectories(MetaHolder.INSTANCE_DIR);
|
|
if (!Files.exists(MetaHolder.ASSETS_DIR)) Files.createDirectories(MetaHolder.ASSETS_DIR);
|
|
if (!Files.exists(MetaHolder.LIBRARIES_DIR)) Files.createDirectories(MetaHolder.LIBRARIES_DIR);
|
|
if (Files.exists(MetaHolder.FORCE_LOAD_PATH)) {
|
|
Utils.LOGGER.info("Force-Loading libraries:");
|
|
JFiles.listTo(MetaHolder.FORCE_LOAD_PATH, path -> {
|
|
Utils.LOGGER.info("Loading " + path);
|
|
try {
|
|
System.load(path.toAbsolutePath().toString());
|
|
} catch (UnsatisfiedLinkError le) {
|
|
Utils.LOGGER.error("Could not load library", le);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public static void terminate() {
|
|
ModsDirScanner.closeAll();
|
|
}
|
|
|
|
public static void showError(String message, String title) {
|
|
backend.showError(message, title);
|
|
}
|
|
|
|
public static void showError(String message, Throwable t) {
|
|
backend.showError(message, t);
|
|
}
|
|
|
|
public static void showInfo(String message, String title) {
|
|
backend.showInfo(message, title);
|
|
}
|
|
|
|
public static void showOkCancel(String message, String title, Runnable ok, Runnable cancel) {
|
|
backend.showOkCancel(message, title, ok, cancel);
|
|
}
|
|
|
|
public static void showOkCancel(String message, String title, Runnable ok, Runnable cancel, boolean defaultCancel) {
|
|
backend.showOkCancel(message, title, ok, cancel, defaultCancel);
|
|
}
|
|
|
|
public static void getInput(String prompt, String details, String defaultValue, Consumer<String> ok, Runnable cancel) throws IOException {
|
|
backend.getInput(prompt, details, defaultValue, ok, cancel);
|
|
}
|
|
|
|
public static void showLoginRefreshPrompt(MicrosoftAccount account) {
|
|
backend.showLoginRefreshPrompt(account);
|
|
}
|
|
|
|
public interface EnvBackend {
|
|
void showError(String message, String title);
|
|
void showError(String message, Throwable t);
|
|
void showInfo(String message, String title);
|
|
default void showOkCancel(String message, String title, Runnable ok, Runnable cancel) {
|
|
showOkCancel(message, title, ok, cancel, false);
|
|
}
|
|
void showOkCancel(String message, String title, Runnable ok, Runnable cancel, boolean defaultCancel);
|
|
void getInput(String prompt, String details, String defaultValue, Consumer<String> ok, Runnable cancel) throws IOException;
|
|
void showLoginRefreshPrompt(MicrosoftAccount account);
|
|
}
|
|
}
|