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

123 lines
5.4 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;
2021-10-30 22:05:24 +02:00
import io.gitlab.jfronny.inceptum.cli.*;
import io.gitlab.jfronny.inceptum.gson.*;
import io.gitlab.jfronny.inceptum.model.ComparableVersion;
import io.gitlab.jfronny.inceptum.model.inceptum.CommandArguments;
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-10-30 22:05:24 +02:00
import io.gitlab.jfronny.inceptum.windows.AlertWindow;
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-11-04 14:07:34 +01:00
import java.lang.reflect.Type;
2021-10-27 22:00:08 +02:00
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;
//TODO allow instance sync through metadata
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");
private static final Path CONFIG_PATH = MetaHolder.BASE_PATH.resolve("glaunch2.json");
public static final Path ACCOUNTS_PATH = MetaHolder.BASE_PATH.resolve("accounts.json");
public static final Set<Command> COMMANDS = Set.of(new HelpCommand(), new GuiCommand(), new LaunchCommand(), new UpdateCheckCommand(), new JvmStateCommand());
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 IOException {
CommandArguments arg = new CommandArguments(_args);
2021-10-30 22:05:24 +02:00
Command cmd = null;
for (Command command : COMMANDS) {
if (command.isAlias(arg.command)) {
2021-10-30 22:05:24 +02:00
cmd = command;
break;
}
}
if (cmd == null) {
System.out.println("Command not found");
return;
}
if (cmd.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-10-27 22:07:07 +02:00
if (!Files.exists(CONFIG_PATH)) Utils.writeObject(CONFIG_PATH, new Config());
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-10-30 22:05:24 +02:00
cmd.invoke(arg);
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-10-27 22:00:08 +02:00
}