diff --git a/src/main/java/pojlib/instance/MinecraftInstance.java b/src/main/java/pojlib/instance/MinecraftInstance.java index 78bdd122..9d8d1127 100644 --- a/src/main/java/pojlib/instance/MinecraftInstance.java +++ b/src/main/java/pojlib/instance/MinecraftInstance.java @@ -8,9 +8,19 @@ 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.*; +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.CoreMods; import pojlib.util.CustomMods; @@ -21,19 +31,12 @@ import pojlib.util.Logger; import pojlib.util.VLoader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - 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; @@ -59,6 +62,7 @@ public static MinecraftInstance create(Activity activity, String instanceName, S Logger.getInstance().appendToLog("Creating new instance: " + instanceName); MinecraftInstance instance = new MinecraftInstance(); + instance.instanceName = instanceName; instance.versionName = minecraftVersion.id; instance.gameDir = new File(gameDir).getAbsolutePath(); @@ -68,16 +72,19 @@ public static MinecraftInstance create(Activity activity, String instanceName, S 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; } - default: { - FabricMeta.FabricVersion fabricVersion = FabricMeta.getLatestStableVersion(); - assert fabricVersion != null; - modLoaderVersionInfo = FabricMeta.getVersionInfo(fabricVersion, minecraftVersion); + case Forge: + case NeoForge: + { + System.out.println("Error!: You cannot use Forge or NeoForge with QuestCraft!"); + break; } } @@ -106,9 +113,36 @@ public static MinecraftInstance create(Activity activity, String instanceName, S 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"; diff --git a/src/main/java/pojlib/util/DownloadUtils.java b/src/main/java/pojlib/util/DownloadUtils.java index c5ced7d8..dd1f28ae 100644 --- a/src/main/java/pojlib/util/DownloadUtils.java +++ b/src/main/java/pojlib/util/DownloadUtils.java @@ -1,6 +1,7 @@ package pojlib.util; import android.content.res.AssetManager; + import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.apache.commons.codec.binary.Hex; @@ -25,46 +26,39 @@ public class DownloadUtils { private static void download(URL url, OutputStream os) throws IOException { - InputStream is = null; - try { - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setConnectTimeout(10000); - conn.setDoInput(true); - conn.connect(); - if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { - is = conn.getInputStream(); - } - - String[] segments = url.getPath().split("/"); - API_V1.currentDownload = segments[segments.length - 1]; - - is = new StreamDL(conn.getInputStream()); - - ((StreamDL)is).addListener((b, count) -> { + final int MAX_RETRIES = 3; + int attempts = 0; - if (b == -1) { - API_V1.downloadStatus = 0; - API_V1.currentDownload = null; - } else { - API_V1.downloadStatus = count * 0.000001; + while (true) { + try { + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setConnectTimeout(10000); + conn.setDoInput(true); + conn.connect(); + if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { + try (InputStream is = new StreamDL(conn.getInputStream())) { + String[] segments = url.getPath().split("/"); + API_V1.currentDownload = segments[segments.length - 1]; + + ((StreamDL)is).addListener((b, count) -> { + + if (b == -1) { + API_V1.downloadStatus = 0; + API_V1.currentDownload = null; + } else { + API_V1.downloadStatus = count * 0.000001; + } + + }); + + IOUtils.copy(is, os); + } + return; } - }); - - IOUtils.copy(is, os); - - } catch (IOException e) { - if (e instanceof SSLException) { - download(url, os); - return; - } - throw new IOException("Unable to download from " + url, e); - } finally { - if (is != null) { - try { - is.close(); - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + if (++attempts >= MAX_RETRIES || e instanceof SSLException) { + throw new IOException("Unable to download from " + url, e); } } }