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

117 lines
4.9 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.*;
2021-10-29 22:50:42 +02:00
import io.gitlab.jfronny.inceptum.gson.GsonIgnoreExclusionStrategy;
import io.gitlab.jfronny.inceptum.gson.MinecraftArgumentDeserializer;
import io.gitlab.jfronny.inceptum.gson.OauthTokenResponseDeserializer;
import io.gitlab.jfronny.inceptum.gson.RulesDeserializer;
import io.gitlab.jfronny.inceptum.model.inceptum.InceptumVersion;
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;
import io.gitlab.jfronny.inceptum.util.Utils;
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 20:19:09 +02:00
import java.io.InputStream;
2021-10-29 22:50:42 +02:00
import java.io.InputStreamReader;
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;
import java.util.Set;
//TODO mods browser
//TODO allow instance sync through metadata
2021-10-27 22:07:07 +02:00
//TODO update checker
2021-10-29 22:50:42 +02:00
public class Inceptum {
2021-10-30 22:05:24 +02:00
public static final Set<Command> COMMANDS = Set.of(new HelpCommand(), new GuiCommand(), new LaunchCommand(), new JvmStateCommand());
public static boolean IS_GUI;
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())
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-10-28 20:19:09 +02:00
//TODO allow moving to .config or elsewhere
2021-10-27 22:00:08 +02:00
public static final Path CACHE_DIR = Path.of("run/cache");
public static final Path INSTANCE_DIR = Path.of("run/instances");
public static final Path ASSETS_DIR = Path.of("run/assets");
public static final Path LIBRARIES_DIR = Path.of("run/libraries");
2021-10-30 16:12:39 +02:00
public static final Path NATIVES_DIR = Path.of("run/natives");
2021-10-28 20:19:09 +02:00
private static final Path CONFIG_PATH = Path.of("run/glaunch2.json");
2021-10-29 22:50:42 +02:00
public static final Path ACCOUNTS_PATH = Path.of("run/accounts.json");
public static InceptumVersion VERSION;
2021-10-27 22:07:07 +02:00
public static Config CONFIG;
2021-10-27 22:00:08 +02:00
public static void main(String[] args) throws IOException {
2021-10-29 22:50:42 +02:00
try (InputStream is = Inceptum.class.getClassLoader().getResourceAsStream("version.json");
InputStreamReader isr = new InputStreamReader(is)) {
VERSION = GSON.fromJson(isr, InceptumVersion.class);
}
2021-10-30 22:05:24 +02:00
if (args.length == 0) args = new String[]{"gui"};
Command cmd = null;
for (Command command : COMMANDS) {
if (command.isAlias(args[0])) {
cmd = command;
break;
}
}
if (cmd == null) {
System.out.println("Command not found");
return;
}
if (cmd.enableLog()) {
2021-10-31 13:37:07 +01:00
LOGGER.info("Launching Inceptum v" + VERSION.version + " (" + VERSION.flavor + ")");
2021-10-30 22:05:24 +02:00
LOGGER.info("Setting up cache dir");
}
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-10-30 22:05:24 +02:00
Utils.clearDirectory(CACHE_DIR);
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(args);
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) {
LOGGER.info(message);
if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message, ok, cancel));
else ok.run();
2021-10-28 21:31:51 +02:00
}
2021-10-27 22:00:08 +02:00
}