Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/QuestCraft-Instance-Dirs' into V…
Browse files Browse the repository at this point in the history
…C-OpenXR
  • Loading branch information
The Judge committed Apr 26, 2024
2 parents 9707851 + fd5c8b7 commit 30eba0a
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.util;
package pojlib;

import com.google.gson.Gson;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.instance;
package pojlib;

import android.app.Activity;

Expand All @@ -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<ModInfo> 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),
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.util;
package pojlib.account;

import android.app.Activity;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.util;
package pojlib.account;

import com.microsoft.aad.msal4j.ITokenCacheAccessAspect;
import com.microsoft.aad.msal4j.ITokenCacheAccessContext;
Expand Down
32 changes: 27 additions & 5 deletions src/main/java/pojlib/api/API_V1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/install/FabricMeta.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/pojlib/install/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/install/MinecraftMeta.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/install/QuiltMeta.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/pojlib/modmanager/ModData.java

This file was deleted.

79 changes: 0 additions & 79 deletions src/main/java/pojlib/modmanager/State.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/pojlib/util/JREUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.instance;
package pojlib.util.json;

import java.io.File;
import java.util.ArrayList;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.util;
package pojlib.util.json;

public class ModInfo {
public String slug;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/pojlib/util/json/ModrinthIndexJson.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pojlib.util;
package pojlib.util.json;

public class ModsJson {
public Version[] versions;
Expand Down

0 comments on commit 30eba0a

Please sign in to comment.