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

107 lines
4.7 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
2022-06-05 16:45:22 +02:00
import io.gitlab.jfronny.commons.serialize.gson.api.GsonHolder;
import io.gitlab.jfronny.inceptum.frontend.cli.CommandResolution;
import io.gitlab.jfronny.inceptum.frontend.cli.Commands;
import io.gitlab.jfronny.inceptum.frontend.gui.window.dialog.AlertWindow;
import io.gitlab.jfronny.inceptum.frontend.gui.window.dialog.TextBoxWindow;
import io.gitlab.jfronny.inceptum.gson.*;
import io.gitlab.jfronny.inceptum.util.*;
import io.gitlab.jfronny.inceptum.util.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;
2022-01-23 17:10:19 +01:00
import io.gitlab.jfronny.inceptum.util.mds.ModsDirScanner;
2021-11-02 13:43:18 +01:00
import io.gitlab.jfronny.inceptum.util.api.McApi;
2021-10-27 22:00:08 +02:00
import java.io.IOException;
import java.nio.file.Files;
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 boolean IS_GUI;
2021-10-27 22:00:08 +02:00
public static void main(String[] args) throws Exception {
if (Wrapper.class.getClassLoader() != Inceptum.class.getClassLoader()) {
throw new IllegalStateException("Mismatching classloader between two classes. Something is wrong here");
}
CommandResolution command = Commands.resolve(args, false);
if (command.command().enableLog()) {
Utils.LOGGER.info("Launching Inceptum v" + MetaHolder.VERSION.version + " (" + MetaHolder.VERSION.flavor + ")");
Utils.LOGGER.info("Loading from " + MetaHolder.BASE_PATH);
}
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);
2021-11-02 13:43:18 +01:00
try {
McApi.getVersions();
Utils.clearDirectory(MetaHolder.CACHE_DIR);
2021-11-02 13:43:18 +01:00
}
catch (IOException e) {
Utils.LOGGER.error("Could not connect to the internet");
2021-11-02 13:43:18 +01:00
}
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:");
Utils.ls(MetaHolder.FORCE_LOAD_PATH, path -> {
Utils.LOGGER.info("Loading " + path);
2021-12-08 21:50:32 +01:00
try {
System.load(path.toAbsolutePath().toString());
}
catch (UnsatisfiedLinkError le) {
Utils.LOGGER.error("Could not load library", le);
2021-12-08 21:50:32 +01:00
}
});
}
2021-10-30 22:05:24 +02:00
command.invoke();
2022-01-04 23:32:05 +01:00
ModsDirScanner.closeAll();
2021-10-28 21:31:51 +02:00
}
2021-10-30 22:05:24 +02:00
public static void showError(String message, String title) {
Utils.LOGGER.error(message);
2021-10-30 22:05:24 +02:00
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) {
Utils.LOGGER.error(message, t);
2021-10-30 22:05:24 +02:00
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) {
Utils.LOGGER.info(message);
2021-10-30 22:05:24 +02:00
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) {
Utils.LOGGER.info(message);
2021-10-30 22:05:24 +02:00
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
}