Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Merge #35

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions src/main/java/pojlib/instance/MinecraftInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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";
Expand Down
68 changes: 31 additions & 37 deletions src/main/java/pojlib/util/DownloadUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}
Expand Down
Loading