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.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; 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; import io.gitlab.jfronny.inceptum.windows.AlertWindow; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; 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 //TODO update checker public class Inceptum { public static final Set COMMANDS = Set.of(new HelpCommand(), new GuiCommand(), new LaunchCommand(), new JvmStateCommand()); public static boolean IS_GUI; 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()) .excludeFieldsWithModifiers(Modifier.TRANSIENT) .excludeFieldsWithModifiers(Modifier.PRIVATE) .addSerializationExclusionStrategy(new GsonIgnoreExclusionStrategy()) .setPrettyPrinting() .create(); //TODO allow moving to .config or elsewhere 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"); public static final Path NATIVES_DIR = Path.of("run/natives"); private static final Path CONFIG_PATH = Path.of("run/glaunch2.json"); public static final Path ACCOUNTS_PATH = Path.of("run/accounts.json"); public static InceptumVersion VERSION; public static Config CONFIG; public static void main(String[] args) throws IOException { try (InputStream is = Inceptum.class.getClassLoader().getResourceAsStream("version.json"); InputStreamReader isr = new InputStreamReader(is)) { VERSION = GSON.fromJson(isr, InceptumVersion.class); } 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()) { LOGGER.info("Launching Inceptum v" + VERSION.version); LOGGER.info("Setting up cache dir"); } 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); Utils.clearDirectory(CACHE_DIR); 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(args); } 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) { LOGGER.info(message); if (IS_GUI) InceptumGui.WINDOWS.add(new AlertWindow(title, message, ok, cancel)); else ok.run(); } }