Inceptum/src/main/java/io/gitlab/jfronny/inceptum/Inceptum.java

142 lines
6.2 KiB
Java

package io.gitlab.jfronny.inceptum;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.gitlab.jfronny.inceptum.cli.CommandResolution;
import io.gitlab.jfronny.inceptum.cli.Commands;
import io.gitlab.jfronny.inceptum.gson.*;
import io.gitlab.jfronny.inceptum.model.ComparableVersion;
import io.gitlab.jfronny.inceptum.model.inceptum.Config;
import io.gitlab.jfronny.inceptum.model.inceptum.source.ModSource;
import io.gitlab.jfronny.inceptum.model.microsoft.OauthTokenResponse;
import io.gitlab.jfronny.inceptum.model.mojang.MinecraftArgument;
import io.gitlab.jfronny.inceptum.model.mojang.Rules;
import io.gitlab.jfronny.inceptum.util.MetaHolder;
import io.gitlab.jfronny.inceptum.util.Utils;
import io.gitlab.jfronny.inceptum.util.api.McApi;
import io.gitlab.jfronny.inceptum.windows.dialog.AlertWindow;
import io.gitlab.jfronny.inceptum.windows.dialog.TextBoxWindow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Scanner;
import java.util.function.Consumer;
public class Inceptum {
public static final Path CACHE_DIR = MetaHolder.BASE_PATH.resolve("cache");
public static final Path INSTANCE_DIR = MetaHolder.BASE_PATH.resolve("instances");
public static final Path ASSETS_DIR = MetaHolder.BASE_PATH.resolve("assets");
public static final Path LIBRARIES_DIR = MetaHolder.BASE_PATH.resolve("libraries");
public static final Path NATIVES_DIR = MetaHolder.BASE_PATH.resolve("natives");
private static final Path CONFIG_PATH = MetaHolder.BASE_PATH.resolve("inceptum.json");
public static final Path ACCOUNTS_PATH = MetaHolder.BASE_PATH.resolve("accounts.json");
public static final Path FORCE_LOAD_PATH = NATIVES_DIR.resolve("forceload");
public static final Logger LOGGER = LoggerFactory.getLogger("Inceptum");
public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(MinecraftArgument.class, new MinecraftArgumentDeserializer())
.registerTypeAdapter(Rules.class, new RulesDeserializer())
.registerTypeAdapter(OauthTokenResponse.class, new OauthTokenResponseDeserializer())
.registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter())
.registerTypeAdapter(ModSource.class, new ModSourceTypeAdapter())
.registerTypeAdapter(ModSourceMapDeserializer.modSourceMapType, new ModSourceMapDeserializer())
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.addSerializationExclusionStrategy(new GsonIgnoreExclusionStrategy())
.setPrettyPrinting()
.create();
public static Config CONFIG;
public static boolean IS_GUI;
public static void main(String[] args) throws Exception {
CommandResolution command = Commands.resolve(args, false);
if (command.command().enableLog()) {
LOGGER.info("Launching Inceptum v" + MetaHolder.VERSION.version + " (" + MetaHolder.VERSION.flavor + ")");
LOGGER.info("Loading from " + MetaHolder.BASE_PATH);
}
if (!Files.exists(CONFIG_PATH.getParent())) Files.createDirectories(CONFIG_PATH.getParent());
if (!Files.exists(CONFIG_PATH)) {
Path gLaunch2 = MetaHolder.BASE_PATH.resolve("glaunch2.json");
if (Files.exists(gLaunch2)) {
Files.move(gLaunch2, CONFIG_PATH);
}
else {
Utils.writeObject(CONFIG_PATH, new Config());
}
}
CONFIG = Utils.loadObject(CONFIG_PATH, Config.class);
if (!Files.exists(CACHE_DIR)) Files.createDirectories(CACHE_DIR);
try {
McApi.getVersions();
Utils.clearDirectory(CACHE_DIR);
}
catch (IOException e) {
Inceptum.LOGGER.error("Could not connect to the internet");
}
if (!Files.exists(INSTANCE_DIR)) Files.createDirectories(INSTANCE_DIR);
if (!Files.exists(ASSETS_DIR)) Files.createDirectories(ASSETS_DIR);
if (!Files.exists(LIBRARIES_DIR)) Files.createDirectories(LIBRARIES_DIR);
if (Files.exists(FORCE_LOAD_PATH)) {
Inceptum.LOGGER.info("Force-Loading libraries:");
Utils.ls(FORCE_LOAD_PATH, path -> {
Inceptum.LOGGER.info("Loading " + path);
try {
System.load(path.toAbsolutePath().toString());
}
catch (UnsatisfiedLinkError le) {
LOGGER.error("Could not load library", le);
}
});
}
command.invoke();
}
public static void saveConfig() {
try {
Utils.writeObject(CONFIG_PATH, CONFIG);
} catch (IOException e) {
LOGGER.error("Could not save config", e);
}
}
public static void showError(String message, String title) {
LOGGER.error(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message));
}
public static void showError(String message, Throwable t) {
LOGGER.error(message, t);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(message, t.toString()));
}
public static void showInfo(String message, String title) {
LOGGER.info(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message));
}
public static void showOkCancel(String message, String title, Runnable ok, Runnable cancel) {
showOkCancel(message, title, ok, cancel, false);
}
public static void showOkCancel(String message, String title, Runnable ok, Runnable cancel, boolean defaultCancel) {
LOGGER.info(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message, ok, cancel));
else if (!defaultCancel) ok.run();
}
public static void getInput(String prompt, String defaultValue, Consumer<String> ok, Runnable cancel) throws IOException {
if (IS_GUI) InceptumGui.WINDOWS.add(new TextBoxWindow(prompt, prompt, defaultValue, ok, cancel));
else {
Scanner scanner = new Scanner(System.in);
System.out.println(prompt);
System.out.print("> ");
ok.accept(scanner.nextLine());
}
}
}