package io.gitlab.jfronny.inceptum; import com.google.gson.Gson; import com.google.gson.GsonBuilder; 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; 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.AlertWindow; 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.Set; //TODO mods browser //TODO allow instance sync through metadata 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("glaunch2.json"); public static final Path ACCOUNTS_PATH = MetaHolder.BASE_PATH.resolve("accounts.json"); public static final Set COMMANDS = Set.of(new HelpCommand(), new GuiCommand(), new LaunchCommand(), new UpdateCheckCommand(), new JvmStateCommand()); 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()) .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 IOException { CommandArguments arg = new CommandArguments(_args); Command cmd = null; for (Command command : COMMANDS) { if (command.isAlias(arg.command)) { cmd = command; break; } } if (cmd == null) { System.out.println("Command not found"); return; } if (cmd.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)) 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); cmd.invoke(arg); } 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(); } }