diff --git a/src/main/java/pojlib/api/API_V1.java b/src/main/java/pojlib/api/API_V1.java index 1a453c71..a63289ad 100644 --- a/src/main/java/pojlib/api/API_V1.java +++ b/src/main/java/pojlib/api/API_V1.java @@ -4,33 +4,17 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkCapabilities; -import android.net.NetworkInfo; -import android.util.Log; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import org.json.JSONException; - -import pojlib.UnityPlayerActivity; import pojlib.account.MinecraftAccount; import pojlib.install.*; -import pojlib.instance.MinecraftInstance; +import pojlib.instance.InstanceHandler; +import pojlib.instance.MinecraftInstances; import pojlib.util.APIHandler; -import pojlib.util.Constants; import pojlib.util.LoginHelper; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.Date; /** @@ -65,20 +49,23 @@ public static MinecraftMeta.MinecraftVersion[] getMinecraftVersions() { return MinecraftMeta.getVersions(); } - public static void addCustomMod(MinecraftInstance instance, String name, String version, String url) { - instance.addCustomMod(name, version, url); + public static void addMod(MinecraftInstances instances, MinecraftInstances.Instance instance, + String home, String name, String version, String url) { + InstanceHandler.addMod(instances, instance, home, name, version, url); } - public static boolean hasMod(MinecraftInstance instance, String name) throws IOException { - return instance.hasCustomMod(name); + public static boolean hasMod(MinecraftInstances.Instance instance, String name) { + return InstanceHandler.hasMod(instance, name); } /** * @return if the operation succeeds */ - public static boolean removeMod(MinecraftInstance instance, String name) { - return instance.removeMod(name); + public static boolean removeMod(MinecraftInstances instances, MinecraftInstances.Instance instance, + String home, String name) { + return InstanceHandler.removeMod(instances, instance, home, name); } + public static MinecraftMeta.MinecraftVersion[] getQCSupportedVersions() throws IOException { return APIHandler.getQCSupportedVersions(); } @@ -88,14 +75,17 @@ public static String getQCSupportedVersionName(MinecraftMeta.MinecraftVersion ve } /** - * Loads an instance from the filesystem. + * Loads all instances from the filesystem. * - * @param instanceName The instance being loaded * @param gameDir .minecraft directory. * @return A minecraft instance object */ - public static MinecraftInstance load(String instanceName, String gameDir) { - return MinecraftInstance.load(instanceName, gameDir); + public static MinecraftInstances loadAll(String gameDir) { + return InstanceHandler.load(gameDir); + } + + public static MinecraftInstances.Instance load(MinecraftInstances instances, String name) { + return instances.load(name); } /** @@ -109,19 +99,20 @@ public static MinecraftInstance load(String instanceName, String gameDir) { * @return A minecraft instance object * @throws IOException Throws if download of library or asset fails */ - public static MinecraftInstance createNewInstance(Activity activity, String instanceName, String home, MinecraftMeta.MinecraftVersion minecraftVersion, MinecraftInstance.ModLoader modLoader) throws IOException { + public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home, + MinecraftMeta.MinecraftVersion minecraftVersion, InstanceHandler.ModLoader modLoader, String modsFolderName) throws IOException { if(ignoreInstanceName) { - return MinecraftInstance.create(activity, instanceName, home, minecraftVersion, modLoader); + return InstanceHandler.create(activity, instanceName, home, minecraftVersion, modLoader, modsFolderName); } else if (instanceName.contains("/") || instanceName.contains("!")) { throw new IOException("You cannot use special characters (!, /, ., etc) when creating instances."); } else { - return MinecraftInstance.create(activity, instanceName, home, minecraftVersion, modLoader); + return InstanceHandler.create(activity, instanceName, home, minecraftVersion, modLoader, modsFolderName); } } - public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstance instance) { - instance.launchInstance(activity, account); + public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstances.Instance instance) { + InstanceHandler.launchInstance(activity, account, instance); } /** diff --git a/src/main/java/pojlib/instance/InstanceHandler.java b/src/main/java/pojlib/instance/InstanceHandler.java new file mode 100644 index 00000000..c4da6249 --- /dev/null +++ b/src/main/java/pojlib/instance/InstanceHandler.java @@ -0,0 +1,182 @@ +package pojlib.instance; + +import android.app.Activity; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + +import pojlib.account.MinecraftAccount; +import pojlib.api.API_V1; +import pojlib.install.FabricMeta; +import pojlib.install.Installer; +import pojlib.install.MinecraftMeta; +import pojlib.install.QuiltMeta; +import pojlib.install.VersionInfo; +import pojlib.util.Constants; +import pojlib.util.ModInfo; +import pojlib.util.ModsJson; +import pojlib.util.DownloadUtils; +import pojlib.util.GsonUtils; +import pojlib.util.JREUtils; +import pojlib.util.Logger; +import pojlib.util.VLoader; + +public class InstanceHandler { + public static final String MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/mods.json"; + public static final String DEV_MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/devmods.json"; + + public enum ModLoader { + Fabric(0), + Quilt(1), + Forge(2), + NeoForge(3); + + public final int index; + + ModLoader(int i) { + this.index = i; + } + } + + //creates a new instance of a minecraft version, install game + mod loader, stores non login related launch info to json + public static MinecraftInstances.Instance create(Activity activity, String instanceName, String gameDir, MinecraftMeta.MinecraftVersion minecraftVersion, ModLoader modLoader, String modsFolderName) { + Logger.getInstance().appendToLog("Creating new instance: " + instanceName); + + MinecraftInstances.Instance instance = new MinecraftInstances.Instance(); + instance.instanceName = instanceName; + instance.versionName = minecraftVersion.id; + instance.gameDir = new File(gameDir).getAbsolutePath(); + if(modsFolderName != null) { + instance.modsDirName = modsFolderName; + } else { + instance.modsDirName = instance.versionName; + } + + VersionInfo modLoaderVersionInfo = null; + switch (modLoader) { + case Fabric: { + FabricMeta.FabricVersion fabricVersion = FabricMeta.getLatestStableVersion(); + assert fabricVersion != null; + modLoaderVersionInfo = FabricMeta.getVersionInfo(fabricVersion, minecraftVersion); + break; + } + case Quilt: { + QuiltMeta.QuiltVersion quiltVersion = QuiltMeta.getLatestVersion(); + assert quiltVersion != null; + modLoaderVersionInfo = QuiltMeta.getVersionInfo(quiltVersion, minecraftVersion); + break; + } + case Forge: + case NeoForge: + { + System.out.println("Error!: You cannot use Forge or NeoForge with QuestCraft!"); + break; + } + } + + VersionInfo minecraftVersionInfo = MinecraftMeta.getVersionInfo(minecraftVersion); + instance.versionType = minecraftVersionInfo.type; + instance.mainClass = modLoaderVersionInfo.mainClass; + + // Install minecraft + VersionInfo finalModLoaderVersionInfo = modLoaderVersionInfo; + new Thread(() -> { + try { + String clientClasspath = Installer.installClient(minecraftVersionInfo, gameDir); + String minecraftClasspath = Installer.installLibraries(minecraftVersionInfo, gameDir); + String modLoaderClasspath = Installer.installLibraries(finalModLoaderVersionInfo, gameDir); + String lwjgl = Installer.installLwjgl(activity); + + instance.classpath = clientClasspath + File.pathSeparator + minecraftClasspath + File.pathSeparator + modLoaderClasspath + File.pathSeparator + lwjgl; + + instance.assetsDir = Installer.installAssets(minecraftVersionInfo, gameDir, activity); + } catch (IOException e) { + e.printStackTrace(); + } + instance.assetIndex = minecraftVersionInfo.assetIndex.id; + + MinecraftInstances instances; + try { + instances = GsonUtils.jsonFileToObject(gameDir + "/instances.json", MinecraftInstances.class); + } catch (Exception e) { + instances = new MinecraftInstances(); + } + assert instances != null; + instances.instances.add(instance); + + // Write instance to json file + GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances); + API_V1.finishedDownloading = true; + }).start(); + + return instance; + } + + // Load an instance from json + public static MinecraftInstances load(String gameDir) { + return GsonUtils.jsonFileToObject(gameDir + "/instances.json", MinecraftInstances.class); + } + + public static void addMod(MinecraftInstances instances, MinecraftInstances.Instance instance, + String gameDir, String name, String version, String url) { + ModInfo info = new ModInfo(); + info.name = name; + info.url = url; + info.version = version; + + instance.mods.add(info); + + GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances); + } + + public static boolean hasMod(MinecraftInstances.Instance instance, String name) { + for(ModInfo info : instance.mods) { + if(info.name.equals(name)) { + return true; + } + } + + return false; + } + + public static boolean removeMod(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir, String name) { + ModInfo oldInfo = null; + for(ModInfo info : instance.mods) { + if(info.name.equals(name)) { + oldInfo = info; + break; + } + } + + if(oldInfo != null) { + instance.mods.remove(oldInfo); + GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances); + } + + return oldInfo != null; + } + + // Return true if instance was deleted + public static boolean delete(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir) { + instances.instances.remove(instance); + GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances); + + return true; + } + + public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstances.Instance instance) { + try { + JREUtils.redirectAndPrintJRELog(); + VLoader.setAndroidInitInfo(activity); + while(!API_V1.finishedDownloading); + JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.versionName); + } catch (Throwable e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/pojlib/instance/MinecraftInstance.java b/src/main/java/pojlib/instance/MinecraftInstance.java deleted file mode 100644 index 6e70cc7f..00000000 --- a/src/main/java/pojlib/instance/MinecraftInstance.java +++ /dev/null @@ -1,335 +0,0 @@ -package pojlib.instance; - -import android.app.Activity; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - -import org.apache.commons.io.FileUtils; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import pojlib.account.MinecraftAccount; -import pojlib.api.API_V1; -import pojlib.install.FabricMeta; -import pojlib.install.Installer; -import pojlib.install.MinecraftMeta; -import pojlib.install.QuiltMeta; -import pojlib.install.VersionInfo; -import pojlib.util.Constants; -import pojlib.util.ModsJson; -import pojlib.util.InstanceJson; -import pojlib.util.DownloadUtils; -import pojlib.util.FileUtil; -import pojlib.util.GsonUtils; -import pojlib.util.JREUtils; -import pojlib.util.Logger; -import pojlib.util.VLoader; - -public class MinecraftInstance { - public static final String MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/mods.json"; - public static final String DEV_MODS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/devmods.json"; - public static final String CUSTOM_MODS = "custom_mods.json"; - public String versionName; - public String instanceName; - public String versionType; - public String classpath; - public String gameDir; - public String assetIndex; - public String assetsDir; - public String mainClass; - - public enum ModLoader { - Fabric(0), - Quilt(1), - Forge(2), - NeoForge(3); - - public final int index; - - ModLoader(int i) { - this.index = i; - } - } - - //creates a new instance of a minecraft version, install game + mod loader, stores non login related launch info to json - public static MinecraftInstance create(Activity activity, String instanceName, String gameDir, MinecraftMeta.MinecraftVersion minecraftVersion, ModLoader modLoader) { - Logger.getInstance().appendToLog("Creating new instance: " + instanceName); - - MinecraftInstance instance = new MinecraftInstance(); - instance.instanceName = instanceName; - instance.versionName = minecraftVersion.id; - instance.gameDir = new File(gameDir).getAbsolutePath(); - - VersionInfo modLoaderVersionInfo = null; - switch (modLoader) { - case Fabric: { - FabricMeta.FabricVersion fabricVersion = FabricMeta.getLatestStableVersion(); - assert fabricVersion != null; - modLoaderVersionInfo = FabricMeta.getVersionInfo(fabricVersion, minecraftVersion); - break; - } - case Quilt: { - QuiltMeta.QuiltVersion quiltVersion = QuiltMeta.getLatestVersion(); - assert quiltVersion != null; - modLoaderVersionInfo = QuiltMeta.getVersionInfo(quiltVersion, minecraftVersion); - break; - } - case Forge: - case NeoForge: - { - System.out.println("Error!: You cannot use Forge or NeoForge with QuestCraft!"); - break; - } - } - - VersionInfo minecraftVersionInfo = MinecraftMeta.getVersionInfo(minecraftVersion); - instance.versionType = minecraftVersionInfo.type; - instance.mainClass = modLoaderVersionInfo.mainClass; - - // Install minecraft - VersionInfo finalModLoaderVersionInfo = modLoaderVersionInfo; - new Thread(() -> { - try { - String clientClasspath = Installer.installClient(minecraftVersionInfo, gameDir); - String minecraftClasspath = Installer.installLibraries(minecraftVersionInfo, gameDir); - String modLoaderClasspath = Installer.installLibraries(finalModLoaderVersionInfo, gameDir); - String lwjgl = Installer.installLwjgl(activity); - - instance.classpath = clientClasspath + File.pathSeparator + minecraftClasspath + File.pathSeparator + modLoaderClasspath + File.pathSeparator + lwjgl; - - instance.assetsDir = Installer.installAssets(minecraftVersionInfo, gameDir, activity); - } catch (IOException e) { - e.printStackTrace(); - } - instance.assetIndex = minecraftVersionInfo.assetIndex.id; - - // Write instance to json file - GsonUtils.objectToJsonFile(gameDir + "/instances/" + instanceName + "/instance.json", instance); - API_V1.finishedDownloading = true; - }).start(); - - updateInstancesJson(gameDir, instance); - return instance; - } - - private static synchronized void updateInstancesJson(String gameDir, MinecraftInstance instance) { - String instancesFilePath = gameDir + "/instances.json"; - JsonArray instancesArray; - - try { - if (new File(instancesFilePath).exists()) { - String jsonContent = FileUtil.read(instancesFilePath); - instancesArray = GsonUtils.GLOBAL_GSON.fromJson(jsonContent, JsonArray.class); - } else { - instancesArray = new JsonArray(); - } - - JsonObject instancesJson = new JsonObject(); - instancesJson.addProperty("instanceName", instance.instanceName); - instancesJson.addProperty("instanceVersion", instance.versionName); - instancesJson.addProperty("gameDir", instance.gameDir); - - - instancesArray.add(instancesJson); - - GsonUtils.objectToJsonFile(instancesFilePath, instancesArray); - } catch (IOException e) { - e.printStackTrace(); - } - } - - // Load an instance from json - public static MinecraftInstance load(String instanceName, String gameDir) { - String path = gameDir + "/instances/" + instanceName + "/instance.json"; - return GsonUtils.jsonFileToObject(path, MinecraftInstance.class); - } - - // Return true if instance was deleted - public static boolean delete(String instanceName, String gameDir) throws IOException { - if (API_V1.ignoreInstanceName) { - return new File(gameDir + "/instances/" + instanceName).delete(); - } else if (instanceName.contains("/") || instanceName.contains("!")) { - throw new IOException("You cannot use special characters (!, /, ., etc) when deleting instances."); - } else { - return new File(gameDir + "/instances/" + instanceName).delete(); - } - - } - - public List generateLaunchArgs(MinecraftAccount account) { - String[] mcArgs = {"--username", account.username, "--version", versionName, "--gameDir", gameDir, - "--assetsDir", assetsDir, "--assetIndex", assetIndex, "--uuid", account.uuid.replaceAll("-", ""), - "--accessToken", account.accessToken, "--userType", account.userType, "--versionType", versionType}; - - List allArgs = new ArrayList<>(Arrays.asList("-cp", classpath)); - allArgs.add(mainClass); - allArgs.addAll(Arrays.asList(mcArgs)); - return allArgs; - } - - public void updateOrDownloadMods() throws Exception{ - API_V1.finishedDownloading = false; - new Thread(() -> { - File coreMods = new File(Constants.USER_HOME + "/coreMods.json"); - File instances = new File(Constants.USER_HOME + "/instances.json"); - - if (API_V1.developerMods) { - DownloadUtils.downloadFile(DEV_MODS, coreMods); - } else { - DownloadUtils.downloadFile(MODS, coreMods); - } - - CustomModsJson customModsObj = GsonUtils.jsonFileToObject(customMods.getAbsolutePath(), CustomModsJson.class); - ModsJson obj = GsonUtils.jsonFileToObject(mods.getAbsolutePath(), ModsJson.class); - - API_V1.finishedDownloading = true; - }).start(); - } - - public void addCustomMod(String name, String version, String url) { - File customMods = new File(Constants.MC_DIR, CUSTOM_MODS); - if(!customMods.exists()) { - CustomModsJson mods = new CustomModsJson(); - mods.instances = new CustomModsJson.InstanceMods[1]; - mods.instances[0] = new CustomModsJson.InstanceMods(); - mods.instances[0].version = this.versionName; - mods.instances[0].mods = new CustomModsJson.ModInfo[1]; - mods.instances[0].mods[0] = new CustomModsJson.ModInfo(); - mods.instances[0].mods[0].name = name; - mods.instances[0].mods[0].version = version; - mods.instances[0].mods[0].url = url; - - GsonUtils.objectToJsonFile(customMods.getPath(), mods); - return; - } - - CustomModsJson mods = GsonUtils.jsonFileToObject(customMods.getPath(), CustomModsJson.class); - for(CustomModsJson.InstanceMods instance : mods.instances) { - if(instance.version.equals(this.versionName)) { - ArrayList modInfoArray = new ArrayList<>(Arrays.asList(instance.mods)); - CustomModsJson.ModInfo info = new CustomModsJson.ModInfo(); - info.name = name; - info.version = version; - info.url = url; - modInfoArray.add(info); - - CustomModsJson.ModInfo[] infos = new CustomModsJson.ModInfo[modInfoArray.size()]; - infos = modInfoArray.toArray(infos); - - instance.mods = infos; - GsonUtils.objectToJsonFile(customMods.getPath(), mods); - return; - } - } - - // If instance does not exist in file, create it - ArrayList instanceInfo = new ArrayList<>(Arrays.asList(mods.instances)); - CustomModsJson.InstanceMods instMods = new CustomModsJson.InstanceMods(); - instMods.version = this.versionName; - instMods.mods = new CustomModsJson.ModInfo[1]; - instMods.mods[0] = new CustomModsJson.ModInfo(); - instMods.mods[0].name = name; - instMods.mods[0].version = version; - instMods.mods[0].url = url; - instanceInfo.add(instanceInfo.size(), instMods); - - // Set the array - mods.instances = instanceInfo.toArray(new CustomModsJson.InstanceMods[0]); - GsonUtils.objectToJsonFile(customMods.getAbsolutePath(), mods); - } - - public boolean hasCustomMod(String name) throws IOException { - File customMods = new File(Constants.MC_DIR, CUSTOM_MODS); - if(!customMods.exists()) { - return false; - } - - CustomModsJson mods = GsonUtils.jsonFileToObject(customMods.getPath(), CustomModsJson.class); - assert mods != null; - for(CustomModsJson.InstanceMods instance : mods.instances) { - if(instance.version.equals(this.versionName)) { - for (CustomModsJson.ModInfo info : instance.mods) { - // Check if core mod is already included - File modsOld = new File(Constants.USER_HOME + "/mods.json"); - if(!modsOld.exists()) { - if (API_V1.developerMods) { - DownloadUtils.downloadFile(DEV_MODS, modsOld); - } else { DownloadUtils.downloadFile(MODS, modsOld); } - } - JsonObject objOld = GsonUtils.jsonFileToObject(modsOld.getAbsolutePath(), JsonObject.class); - assert objOld != null; - for (JsonElement verMod : objOld.getAsJsonArray(this.versionName)) { - JsonObject object = verMod.getAsJsonObject(); - String slug = object.get("slug").getAsString(); - if(name.equalsIgnoreCase(slug)) { - return true; - } - } - if(info.name.equalsIgnoreCase(name)) { - return true; - } - } - break; - } - } - return false; - } - - public boolean removeMod(String name) { - File customMods = new File(Constants.MC_DIR, CUSTOM_MODS); - if(!customMods.exists()) { - return false; - } - - // Check if core mod is already included, if so, don't delete - File modsOld = new File(Constants.USER_HOME + "/mods.json"); - if(modsOld.exists()) { - JsonObject objOld = GsonUtils.jsonFileToObject(modsOld.getAbsolutePath(), JsonObject.class); - assert objOld != null; - for (JsonElement verMod : objOld.getAsJsonArray(this.versionName)) { - JsonObject object = verMod.getAsJsonObject(); - String slug = object.get("slug").getAsString().replace("-", " "); - if(name.equals(slug)) { - return false; - } - } - } - - CustomModsJson mods = GsonUtils.jsonFileToObject(customMods.getAbsolutePath(), CustomModsJson.class); - assert mods != null; - for(CustomModsJson.InstanceMods instance : mods.instances) { - if(instance.version.equals(this.versionName)) { - for (CustomModsJson.ModInfo info : instance.mods) { - if(info.name.equals(name)) { - ArrayList modInfoArray = new ArrayList<>(Arrays.asList(instance.mods)); - File mod = new File(Constants.MC_DIR + "/mods/" + this.versionName + "/" + info.name + ".jar"); - mod.delete(); - modInfoArray.remove(info); - instance.mods = modInfoArray.toArray(new CustomModsJson.ModInfo[0]); - GsonUtils.objectToJsonFile(customMods.getAbsolutePath(), mods); - return true; - } - } - break; - } - } - return false; - } - - public void launchInstance(Activity activity, MinecraftAccount account) { - try { - JREUtils.redirectAndPrintJRELog(); - VLoader.setAndroidInitInfo(activity); - while(!API_V1.finishedDownloading); - JREUtils.launchJavaVM(activity, generateLaunchArgs(account), versionName); - } catch (Throwable e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/src/main/java/pojlib/instance/MinecraftInstances.java b/src/main/java/pojlib/instance/MinecraftInstances.java new file mode 100644 index 00000000..191de124 --- /dev/null +++ b/src/main/java/pojlib/instance/MinecraftInstances.java @@ -0,0 +1,109 @@ +package pojlib.instance; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import pojlib.account.MinecraftAccount; +import pojlib.api.API_V1; +import pojlib.util.DownloadUtils; +import pojlib.util.GsonUtils; +import pojlib.util.Logger; +import pojlib.util.ModInfo; +import pojlib.util.ModsJson; + +public class MinecraftInstances { + public ArrayList instances; + + public Instance load(String name) { + for(Instance instance : instances) { + if(instance.instanceName.equals(name)) { + return instance; + } + } + + return null; + } + + public static class Instance { + public String instanceName; + public String versionName; + public String modsDirName; + public String versionType; + public String classpath; + public String gameDir; + public String assetIndex; + public String assetsDir; + public String mainClass; + public ArrayList mods; + + public List generateLaunchArgs(MinecraftAccount account) { + String[] mcArgs = {"--username", account.username, "--version", versionName, "--gameDir", gameDir, + "--assetsDir", assetsDir, "--assetIndex", assetIndex, "--uuid", account.uuid.replaceAll("-", ""), + "--accessToken", account.accessToken, "--userType", account.userType, "--versionType", versionType}; + + List allArgs = new ArrayList<>(Arrays.asList("-cp", classpath)); + allArgs.add(mainClass); + allArgs.addAll(Arrays.asList(mcArgs)); + return allArgs; + } + + private ModsJson parseModsJson(String gameDir) throws Exception { + File mods = new File(gameDir + "mods.json"); + if(API_V1.developerMods) { + DownloadUtils.downloadFile(InstanceHandler.DEV_MODS, mods); + } else { + DownloadUtils.downloadFile(InstanceHandler.MODS, mods); + } + + return GsonUtils.jsonFileToObject(mods.getAbsolutePath(), ModsJson.class); + } + + public void updateMods(String gameDir) { + try { + ModsJson modsJson = parseModsJson(gameDir); + + ModsJson.Version version = null; + for(ModsJson.Version info : modsJson.versions) { + if(!info.name.equals(versionName)) { + continue; + } + version = info; + break; + } + + assert version != null; + for(ModInfo info : version.coreMods) { + for(ModInfo currInfo : mods) { + if(!currInfo.name.equals(info.name)) { + continue; + } + if(currInfo.version.equals(info.version)) { + continue; + } + + File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.name); + DownloadUtils.downloadFile(info.url, mod); + } + } + + for(ModInfo info : version.defaultMods) { + for(ModInfo currInfo : mods) { + if(!currInfo.name.equals(info.name)) { + continue; + } + if(currInfo.version.equals(info.version)) { + continue; + } + + File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.name); + DownloadUtils.downloadFile(info.url, mod); + } + } + } catch (Exception e) { + Logger.getInstance().appendToLog("Mods failed to download! Are you offline?\n" + e.getMessage()); + } + } + } +} diff --git a/src/main/java/pojlib/util/APIHandler.java b/src/main/java/pojlib/util/APIHandler.java index 2d4156b1..2959cf6b 100644 --- a/src/main/java/pojlib/util/APIHandler.java +++ b/src/main/java/pojlib/util/APIHandler.java @@ -1,11 +1,16 @@ package pojlib.util; -import static pojlib.instance.MinecraftInstance.DEV_MODS; -import static pojlib.instance.MinecraftInstance.MODS; +import static pojlib.instance.InstanceHandler.DEV_MODS; +import static pojlib.instance.InstanceHandler.MODS; import com.google.gson.Gson; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; diff --git a/src/main/java/pojlib/util/InstanceJson.java b/src/main/java/pojlib/util/InstanceJson.java deleted file mode 100644 index 0d5a82ad..00000000 --- a/src/main/java/pojlib/util/InstanceJson.java +++ /dev/null @@ -1,22 +0,0 @@ -package pojlib.util; - -public class InstanceJson { - public Instance[] instances; - - public static class Instance { - public String instanceName; - public String instanceVersion; - public String modsDir; - public ModInfo[] mods; - } - - public static class ModInfo { - public String name; - public String version; - public String url; - } - - public void updateInstance() { - - } -} diff --git a/src/main/java/pojlib/util/ModInfo.java b/src/main/java/pojlib/util/ModInfo.java new file mode 100644 index 00000000..d26f4ec6 --- /dev/null +++ b/src/main/java/pojlib/util/ModInfo.java @@ -0,0 +1,7 @@ +package pojlib.util; + +public class ModInfo { + public String name; + public String version; + public String url; +} \ No newline at end of file diff --git a/src/main/java/pojlib/util/ModsJson.java b/src/main/java/pojlib/util/ModsJson.java index c7ad68f2..3fe432f4 100644 --- a/src/main/java/pojlib/util/ModsJson.java +++ b/src/main/java/pojlib/util/ModsJson.java @@ -1,17 +1,13 @@ package pojlib.util; +import java.util.ArrayList; + public class ModsJson { - public Version[] versions; + public ArrayList versions; public static class Version { public String name; - public Mod[] coreMods; - public Mod[] defaultMods; - } - - public static class Mod { - public String slug; - public String version; - public String download_link; + public ArrayList coreMods; + public ArrayList defaultMods; } }