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

142 lines
6.2 KiB
Java
Raw Normal View History

2021-10-29 22:50:42 +02:00
package io.gitlab.jfronny.inceptum;
2021-10-27 22:00:08 +02:00
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;
2021-11-02 16:44:26 +01:00
import io.gitlab.jfronny.inceptum.model.inceptum.source.ModSource;
2021-10-29 22:50:42 +02:00
import io.gitlab.jfronny.inceptum.model.microsoft.OauthTokenResponse;
import io.gitlab.jfronny.inceptum.model.mojang.MinecraftArgument;
import io.gitlab.jfronny.inceptum.model.mojang.Rules;
2021-11-02 13:43:18 +01:00
import io.gitlab.jfronny.inceptum.util.MetaHolder;
2021-10-29 22:50:42 +02:00
import io.gitlab.jfronny.inceptum.util.Utils;
2021-11-02 13:43:18 +01:00
import io.gitlab.jfronny.inceptum.util.api.McApi;
2021-11-24 19:53:06 +01:00
import io.gitlab.jfronny.inceptum.windows.dialog.AlertWindow;
import io.gitlab.jfronny.inceptum.windows.dialog.TextBoxWindow;
2021-10-30 19:26:59 +02:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2021-10-27 22:00:08 +02:00
import java.io.IOException;
2021-10-28 23:27:47 +02:00
import java.lang.reflect.Modifier;
2021-10-27 22:00:08 +02:00
import java.nio.file.Files;
import java.nio.file.Path;
2021-11-24 19:53:06 +01:00
import java.util.Scanner;
import java.util.function.Consumer;
2021-10-27 22:00:08 +02:00
2021-10-29 22:50:42 +02:00
public class Inceptum {
2021-11-02 13:43:18 +01:00
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");
2021-11-24 19:53:06 +01:00
private static final Path CONFIG_PATH = MetaHolder.BASE_PATH.resolve("inceptum.json");
2021-11-02 13:43:18 +01:00
public static final Path ACCOUNTS_PATH = MetaHolder.BASE_PATH.resolve("accounts.json");
2021-11-11 20:11:20 +01:00
public static final Path FORCE_LOAD_PATH = NATIVES_DIR.resolve("forceload");
2021-10-30 19:26:59 +02:00
public static final Logger LOGGER = LoggerFactory.getLogger("Inceptum");
2021-10-27 22:00:08 +02:00
public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(MinecraftArgument.class, new MinecraftArgumentDeserializer())
2021-10-27 22:07:07 +02:00
.registerTypeAdapter(Rules.class, new RulesDeserializer())
2021-10-29 22:50:42 +02:00
.registerTypeAdapter(OauthTokenResponse.class, new OauthTokenResponseDeserializer())
.registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter())
2021-11-02 16:44:26 +01:00
.registerTypeAdapter(ModSource.class, new ModSourceTypeAdapter())
2021-11-04 14:07:34 +01:00
.registerTypeAdapter(ModSourceMapDeserializer.modSourceMapType, new ModSourceMapDeserializer())
2021-10-28 23:27:47 +02:00
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.addSerializationExclusionStrategy(new GsonIgnoreExclusionStrategy())
.setPrettyPrinting()
2021-10-27 22:00:08 +02:00
.create();
2021-11-02 13:43:18 +01:00
2021-10-27 22:07:07 +02:00
public static Config CONFIG;
2021-11-02 13:43:18 +01:00
public static boolean IS_GUI;
2021-10-27 22:00:08 +02:00
public static void main(String[] args) throws Exception {
CommandResolution command = Commands.resolve(args, false);
if (command.command().enableLog()) {
2021-11-02 13:43:18 +01:00
LOGGER.info("Launching Inceptum v" + MetaHolder.VERSION.version + " (" + MetaHolder.VERSION.flavor + ")");
LOGGER.info("Loading from " + MetaHolder.BASE_PATH);
2021-10-30 22:05:24 +02:00
}
if (!Files.exists(CONFIG_PATH.getParent())) Files.createDirectories(CONFIG_PATH.getParent());
2021-11-24 19:53:06 +01:00
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());
}
}
2021-10-27 22:07:07 +02:00
CONFIG = Utils.loadObject(CONFIG_PATH, Config.class);
2021-10-27 22:00:08 +02:00
if (!Files.exists(CACHE_DIR)) Files.createDirectories(CACHE_DIR);
2021-11-02 13:43:18 +01:00
try {
McApi.getVersions();
Utils.clearDirectory(CACHE_DIR);
}
catch (IOException e) {
Inceptum.LOGGER.error("Could not connect to the internet");
}
2021-10-27 22:00:08 +02:00
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);
2021-11-11 20:11:20 +01:00
if (Files.exists(FORCE_LOAD_PATH)) {
Inceptum.LOGGER.info("Force-Loading libraries:");
2021-11-11 20:11:20 +01:00
Utils.ls(FORCE_LOAD_PATH, path -> {
Inceptum.LOGGER.info("Loading " + path);
2021-12-08 21:50:32 +01:00
try {
System.load(path.toAbsolutePath().toString());
}
catch (UnsatisfiedLinkError le) {
LOGGER.error("Could not load library", le);
}
});
}
2021-10-30 22:05:24 +02:00
command.invoke();
2021-10-28 21:31:51 +02:00
}
public static void saveConfig() {
try {
Utils.writeObject(CONFIG_PATH, CONFIG);
} catch (IOException e) {
LOGGER.error("Could not save config", e);
}
2021-10-27 22:00:08 +02:00
}
2021-10-30 22:05:24 +02:00
public static void showError(String message, String title) {
LOGGER.error(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message));
2021-10-27 22:00:08 +02:00
}
2021-10-30 22:05:24 +02:00
public static void showError(String message, Throwable t) {
LOGGER.error(message, t);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(message, t.toString()));
2021-10-27 22:00:08 +02:00
}
2021-10-30 22:05:24 +02:00
public static void showInfo(String message, String title) {
LOGGER.info(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message));
2021-10-27 22:00:08 +02:00
}
2021-10-30 22:05:24 +02:00
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) {
2021-10-30 22:05:24 +02:00
LOGGER.info(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message, ok, cancel));
else if (!defaultCancel) ok.run();
2021-10-28 21:31:51 +02:00
}
2021-11-24 19:53:06 +01:00
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());
}
}
2021-10-27 22:00:08 +02:00
}