diff --git a/src/main/java/pojlib/util/APIHandler.java b/src/main/java/pojlib/APIHandler.java similarity index 97% rename from src/main/java/pojlib/util/APIHandler.java rename to src/main/java/pojlib/APIHandler.java index 9b9eca0b..73a63ce1 100644 --- a/src/main/java/pojlib/util/APIHandler.java +++ b/src/main/java/pojlib/APIHandler.java @@ -1,4 +1,4 @@ -package pojlib.util; +package pojlib; import com.google.gson.Gson; @@ -14,6 +14,11 @@ import java.util.HashMap; import java.util.stream.Collectors; +import pojlib.util.Constants; +import pojlib.util.DownloadUtils; +import pojlib.util.GsonUtils; +import pojlib.util.Logger; + public class APIHandler { public final String baseUrl; diff --git a/src/main/java/pojlib/instance/InstanceHandler.java b/src/main/java/pojlib/InstanceHandler.java similarity index 78% rename from src/main/java/pojlib/instance/InstanceHandler.java rename to src/main/java/pojlib/InstanceHandler.java index 2f16f34a..fbc83178 100644 --- a/src/main/java/pojlib/instance/InstanceHandler.java +++ b/src/main/java/pojlib/InstanceHandler.java @@ -1,4 +1,4 @@ -package pojlib.instance; +package pojlib; import android.app.Activity; @@ -17,16 +17,56 @@ import pojlib.install.QuiltMeta; import pojlib.install.VersionInfo; import pojlib.util.Constants; -import pojlib.util.ModInfo; +import pojlib.util.DownloadUtils; +import pojlib.util.json.MinecraftInstances; +import pojlib.util.json.ModInfo; import pojlib.util.GsonUtils; import pojlib.util.JREUtils; import pojlib.util.Logger; import pojlib.util.VLoader; +import pojlib.util.json.ModrinthIndexJson; 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 static MinecraftInstances.Instance create(Activity activity, MinecraftInstances instances, String instanceName, String userHome, ModLoader modLoader, File mrpackFile, String imageURL) { + ModrinthIndexJson index = GsonUtils.jsonFileToObject(mrpackFile.getAbsolutePath(), ModrinthIndexJson.class); + if(index == null) { + Logger.getInstance().appendToLog("Couldn't install the modpack with path " + mrpackFile.getAbsolutePath()); + return null; + } + + MinecraftInstances.Instance instance = create(activity, instances, instanceName, userHome, false, index.dependencies.minecraft, modLoader, imageURL); + new Thread(() -> { + API_V1.finishedDownloading = false; + for (ModrinthIndexJson.ModpackFile file : index.files) { + if (file.path.contains("mods")) { + ArrayList mods = Lists.newArrayList(instance.mods); + ModInfo info = new ModInfo(); + info.slug = file.path + .replaceAll(".*\\/", "") + .replaceAll("\\..*", ""); + info.version = "1.0.0"; + info.download_link = file.downloads[0]; + mods.add(info); + instance.mods = mods.toArray(new ModInfo[0]); + } + try { + API_V1.currentDownload = file.path; + DownloadUtils.downloadFile(file.downloads[0], new File(instance.gameDir, file.path)); + } catch (IOException e) { + Logger.getInstance().appendToLog("Couldn't install the modpack with path " + mrpackFile.getAbsolutePath()); + Logger.getInstance().appendToLog(e.toString()); + } + } + API_V1.finishedDownloading = false; + GsonUtils.objectToJsonFile(userHome + "/instances.json", instances); + }).start(); + + return instance; + } + public enum ModLoader { Fabric(0), Quilt(1), @@ -59,7 +99,7 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns instance.instanceName = instanceName; instance.instanceImageURL = imageURL; instance.versionName = minecraftVersion; - instance.gameDir = Constants.USER_HOME + "/" + instanceName.toLowerCase(Locale.ROOT).replaceAll(" ", "_"); + instance.gameDir = Constants.USER_HOME + "/instances/" + instanceName.toLowerCase(Locale.ROOT).replaceAll(" ", "_"); instance.defaultMods = useDefaultMods; File gameDirFile = new File(instance.gameDir); diff --git a/src/main/java/pojlib/util/LoginHelper.java b/src/main/java/pojlib/account/LoginHelper.java similarity index 98% rename from src/main/java/pojlib/util/LoginHelper.java rename to src/main/java/pojlib/account/LoginHelper.java index b544c66a..a41ede37 100644 --- a/src/main/java/pojlib/util/LoginHelper.java +++ b/src/main/java/pojlib/account/LoginHelper.java @@ -1,4 +1,4 @@ -package pojlib.util; +package pojlib.account; import android.app.Activity; @@ -21,8 +21,9 @@ import java.util.concurrent.ExecutionException; import java.util.function.Consumer; -import pojlib.account.MinecraftAccount; import pojlib.api.API_V1; +import pojlib.util.Constants; +import pojlib.util.Logger; public class LoginHelper { public static Thread loginThread; diff --git a/src/main/java/pojlib/util/TokenPersistence.java b/src/main/java/pojlib/account/TokenPersistence.java similarity index 98% rename from src/main/java/pojlib/util/TokenPersistence.java rename to src/main/java/pojlib/account/TokenPersistence.java index bb949f22..710889e6 100644 --- a/src/main/java/pojlib/util/TokenPersistence.java +++ b/src/main/java/pojlib/account/TokenPersistence.java @@ -1,4 +1,4 @@ -package pojlib.util; +package pojlib.account; import com.microsoft.aad.msal4j.ITokenCacheAccessAspect; import com.microsoft.aad.msal4j.ITokenCacheAccessContext; diff --git a/src/main/java/pojlib/api/API_V1.java b/src/main/java/pojlib/api/API_V1.java index 60cbd4d9..0408d9c4 100644 --- a/src/main/java/pojlib/api/API_V1.java +++ b/src/main/java/pojlib/api/API_V1.java @@ -8,13 +8,13 @@ import com.google.gson.JsonObject; import pojlib.account.MinecraftAccount; -import pojlib.install.*; -import pojlib.instance.InstanceHandler; -import pojlib.instance.MinecraftInstances; -import pojlib.util.APIHandler; +import pojlib.InstanceHandler; +import pojlib.util.json.MinecraftInstances; +import pojlib.APIHandler; import pojlib.util.Constants; -import pojlib.util.LoginHelper; +import pojlib.account.LoginHelper; +import java.io.File; import java.io.IOException; import java.util.Date; @@ -140,6 +140,28 @@ public static MinecraftInstances.Instance createNewInstance(Activity activity, M } } + /** + * Creates a new game instance from a mrpack file. + * + * @param activity The active android activity + * @param instanceName The name of the instance being created - can be anything, used for identification + * @param useDefaultMods Use QC's default mods for the version (Core mods are automatically included) + * @param minecraftVersion The version of minecraft to install + * @param imageURL Modpack image url, nullable + * @return A minecraft instance object + * @throws IOException Throws if download of library or asset fails + */ + public static MinecraftInstances.Instance createNewInstance(Activity activity, MinecraftInstances instances, String instanceName, String imageURL, File mrpackFile) throws IOException { + + if(ignoreInstanceName) { + return InstanceHandler.create(activity, instances, instanceName, Constants.USER_HOME, InstanceHandler.ModLoader.Fabric, mrpackFile, imageURL); + } else if (instanceName.contains("/") || instanceName.contains("!")) { + throw new IOException("You cannot use special characters (!, /, ., etc) when creating instances."); + } else { + return InstanceHandler.create(activity, instances, instanceName, Constants.USER_HOME, InstanceHandler.ModLoader.Fabric, mrpackFile, imageURL); + } + } + /** * Update the mods for the selected instance * diff --git a/src/main/java/pojlib/install/FabricMeta.java b/src/main/java/pojlib/install/FabricMeta.java index f2cb3642..f0d40d8b 100644 --- a/src/main/java/pojlib/install/FabricMeta.java +++ b/src/main/java/pojlib/install/FabricMeta.java @@ -1,7 +1,7 @@ package pojlib.install; import com.google.gson.annotations.SerializedName; -import pojlib.util.APIHandler; +import pojlib.APIHandler; import pojlib.util.Constants; public class FabricMeta { diff --git a/src/main/java/pojlib/install/Installer.java b/src/main/java/pojlib/install/Installer.java index 2b85a228..c91c9195 100644 --- a/src/main/java/pojlib/install/Installer.java +++ b/src/main/java/pojlib/install/Installer.java @@ -8,7 +8,8 @@ import org.apache.commons.io.FileUtils; -import pojlib.instance.MinecraftInstances; +import pojlib.APIHandler; +import pojlib.util.json.MinecraftInstances; import pojlib.util.*; import java.io.File; diff --git a/src/main/java/pojlib/install/MinecraftMeta.java b/src/main/java/pojlib/install/MinecraftMeta.java index 3215c318..4d3b13dd 100644 --- a/src/main/java/pojlib/install/MinecraftMeta.java +++ b/src/main/java/pojlib/install/MinecraftMeta.java @@ -1,7 +1,7 @@ package pojlib.install; import com.google.gson.annotations.SerializedName; -import pojlib.util.APIHandler; +import pojlib.APIHandler; import pojlib.util.Constants; public class MinecraftMeta { diff --git a/src/main/java/pojlib/install/QuiltMeta.java b/src/main/java/pojlib/install/QuiltMeta.java index 6375a685..10dc5f3a 100644 --- a/src/main/java/pojlib/install/QuiltMeta.java +++ b/src/main/java/pojlib/install/QuiltMeta.java @@ -1,7 +1,7 @@ package pojlib.install; import com.google.gson.annotations.SerializedName; -import pojlib.util.APIHandler; +import pojlib.APIHandler; import pojlib.util.Constants; public class QuiltMeta { diff --git a/src/main/java/pojlib/modmanager/ModData.java b/src/main/java/pojlib/modmanager/ModData.java deleted file mode 100644 index e4db6d99..00000000 --- a/src/main/java/pojlib/modmanager/ModData.java +++ /dev/null @@ -1,29 +0,0 @@ -package pojlib.modmanager; - -import com.google.gson.annotations.SerializedName; - -public class ModData { - @SerializedName("title") - public String title; - @SerializedName("slug") - public String slug; - @SerializedName("icon_url") - public String iconUrl; - - public String platform; - public String repo; - public boolean isActive; - public FileData fileData; - - public ModData() { - isActive = false; - fileData = new FileData(); - } - - //Only set when calling a getModFileData method - public static class FileData { - public String id; - public String url; - public String filename; - } -} diff --git a/src/main/java/pojlib/modmanager/State.java b/src/main/java/pojlib/modmanager/State.java deleted file mode 100644 index 94c43118..00000000 --- a/src/main/java/pojlib/modmanager/State.java +++ /dev/null @@ -1,79 +0,0 @@ -package pojlib.modmanager; - -import com.google.gson.annotations.SerializedName; - -import java.util.ArrayList; -import java.util.List; - -public class State { - @SerializedName("fabric-loader-version") - public String fabricLoaderVersion; - @SerializedName("instances") - private final List instances = new ArrayList<>(); - - public List getInstances() { - return instances; - } - - public Instance getInstance(String name) { - for (Instance instance : instances) { - if (instance.name.equalsIgnoreCase(name)) return instance; - } - return null; - } - - public void addInstance(Instance instance) { - instances.add(instance); - } - - public static class Instance { - @SerializedName("name") - private String name; - @SerializedName("gameVersion") - private String gameVersion; - @SerializedName("LoaderVersion") - private String LoaderVersion; - @SerializedName("mods") - private final List mods = new ArrayList<>(); - - - public void setName(String name) { - this.name = name; - } - - public void setGameVersion(String gameVersion) { - this.gameVersion = gameVersion; - } - - public void setLoaderVersion(String fabricLoaderVersion) { - this.LoaderVersion = fabricLoaderVersion; - } - - public void addMod(ModData modData) { - this.mods.add(modData); - } - - public String getName() { - return name; - } - - public String getGameVersion() { - return gameVersion; - } - - public String getLoaderVersion() { - return LoaderVersion; - } - - public List getMods() { - return mods; - } - - public ModData getMod(String slug) { - for (ModData mod : mods) { - if (mod.slug.equals(slug)) return mod; - } - return null; - } - } -} diff --git a/src/main/java/pojlib/util/JREUtils.java b/src/main/java/pojlib/util/JREUtils.java index 19b457c2..d1cdbbcd 100644 --- a/src/main/java/pojlib/util/JREUtils.java +++ b/src/main/java/pojlib/util/JREUtils.java @@ -19,7 +19,7 @@ import java.util.Map; import pojlib.api.API_V1; -import pojlib.instance.MinecraftInstances; +import pojlib.util.json.MinecraftInstances; public class JREUtils { private JREUtils() {} diff --git a/src/main/java/pojlib/instance/MinecraftInstances.java b/src/main/java/pojlib/util/json/MinecraftInstances.java similarity index 98% rename from src/main/java/pojlib/instance/MinecraftInstances.java rename to src/main/java/pojlib/util/json/MinecraftInstances.java index f362577c..a56926f3 100644 --- a/src/main/java/pojlib/instance/MinecraftInstances.java +++ b/src/main/java/pojlib/util/json/MinecraftInstances.java @@ -1,4 +1,4 @@ -package pojlib.instance; +package pojlib.util.json; import java.io.File; import java.util.ArrayList; @@ -7,12 +7,11 @@ import pojlib.account.MinecraftAccount; import pojlib.api.API_V1; +import pojlib.InstanceHandler; import pojlib.util.Constants; import pojlib.util.DownloadUtils; import pojlib.util.GsonUtils; import pojlib.util.Logger; -import pojlib.util.ModInfo; -import pojlib.util.ModsJson; public class MinecraftInstances { public Instance[] instances; diff --git a/src/main/java/pojlib/util/ModInfo.java b/src/main/java/pojlib/util/json/ModInfo.java similarity index 80% rename from src/main/java/pojlib/util/ModInfo.java rename to src/main/java/pojlib/util/json/ModInfo.java index d52cee89..eaa7cab7 100644 --- a/src/main/java/pojlib/util/ModInfo.java +++ b/src/main/java/pojlib/util/json/ModInfo.java @@ -1,4 +1,4 @@ -package pojlib.util; +package pojlib.util.json; public class ModInfo { public String slug; diff --git a/src/main/java/pojlib/util/json/ModrinthIndexJson.java b/src/main/java/pojlib/util/json/ModrinthIndexJson.java new file mode 100644 index 00000000..315e9349 --- /dev/null +++ b/src/main/java/pojlib/util/json/ModrinthIndexJson.java @@ -0,0 +1,23 @@ +package pojlib.util.json; + +import com.google.gson.annotations.SerializedName; + +public class ModrinthIndexJson { + public String versionId; + public String name; + public String summary; + public ModpackFile[] files; + public Dependencies dependencies; + + public static class ModpackFile { + public String path; + public String[] downloads; + public int fileSize; + } + + public static class Dependencies { + public String minecraft; + @SerializedName("fabric-loader") + public String fabricLoader; + } +} diff --git a/src/main/java/pojlib/util/ModsJson.java b/src/main/java/pojlib/util/json/ModsJson.java similarity index 88% rename from src/main/java/pojlib/util/ModsJson.java rename to src/main/java/pojlib/util/json/ModsJson.java index 96e03334..4e83d00c 100644 --- a/src/main/java/pojlib/util/ModsJson.java +++ b/src/main/java/pojlib/util/json/ModsJson.java @@ -1,4 +1,4 @@ -package pojlib.util; +package pojlib.util.json; public class ModsJson { public Version[] versions;