Skip to content

Commit

Permalink
Swap to Instanced Dirs (#41)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: The Judge <[email protected]>
  • Loading branch information
CADIndie and The Judge authored May 13, 2024
1 parent 2cf1f01 commit 305fd3b
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 314 deletions.
101 changes: 0 additions & 101 deletions src/main/assets/c2me.toml

This file was deleted.

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 Expand Up @@ -105,7 +110,7 @@ public static <T> T postFullUrl(String url, HashMap<String, Object> query, T bod
public static final String SUPPORTED_VERSIONS = "https://raw.githubusercontent.com/QuestCraftPlusPlus/Pojlib/QuestCraft/supportedVersions.json";

public static String[] getQCSupportedVersions() {
File versionsJson = new File(Constants.MC_DIR + "/supportedVersions.json");
File versionsJson = new File(Constants.USER_HOME + "/supportedVersions.json");
try {
DownloadUtils.downloadFile(SUPPORTED_VERSIONS, versionsJson);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package pojlib.instance;
package pojlib;

import android.app.Activity;

import com.google.common.collect.Lists;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import pojlib.account.MinecraftAccount;
import pojlib.api.API_V1;
Expand All @@ -16,16 +24,62 @@
import pojlib.install.QuiltMeta;
import pojlib.install.VersionInfo;
import pojlib.util.Constants;
import pojlib.util.ModInfo;
import pojlib.util.DownloadUtils;
import pojlib.util.FileUtil;
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, String mrpackFilePath, String imageURL) throws IOException {
File mrpackJson = new File(Constants.USER_HOME + "/instances/" + instanceName + "/setup/modrinth.index.json");

mrpackJson.getParentFile().mkdirs();
FileUtil.UnzipArchive(activity, mrpackFilePath, instanceName + ".mrpack", Constants.USER_HOME + "/instances/" + instanceName);

ModrinthIndexJson index = GsonUtils.jsonFileToObject(mrpackJson.getAbsolutePath(), ModrinthIndexJson.class);
if(index == null) {
Logger.getInstance().appendToLog("Couldn't install the modpack with path " + mrpackJson.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 " + mrpackJson.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 All @@ -40,7 +94,7 @@ public enum ModLoader {
}

//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, MinecraftInstances instances, String instanceName, String gameDir, boolean useDefaultMods, String minecraftVersion, ModLoader modLoader, String modsFolderName, String imageURL) {
public static MinecraftInstances.Instance create(Activity activity, MinecraftInstances instances, String instanceName, String gameDir, boolean useDefaultMods, String minecraftVersion, ModLoader modLoader, String imageURL) {
API_V1.finishedDownloading = false;
File instancesFile = new File(gameDir + "/instances.json");
if (instancesFile.exists()) {
Expand All @@ -58,12 +112,12 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns
instance.instanceName = instanceName;
instance.instanceImageURL = imageURL;
instance.versionName = minecraftVersion;
instance.gameDir = new File(gameDir).getAbsolutePath();
instance.gameDir = Constants.USER_HOME + "/instances/" + instanceName.toLowerCase(Locale.ROOT).replaceAll(" ", "_");
instance.defaultMods = useDefaultMods;
if(modsFolderName != null) {
instance.modsDirName = modsFolderName;
} else {
instance.modsDirName = instance.versionName;

File gameDirFile = new File(instance.gameDir);
if(!gameDirFile.exists()) {
gameDirFile.mkdirs();
}

VersionInfo modLoaderVersionInfo = null;
Expand Down Expand Up @@ -106,15 +160,15 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns

instance.classpath = clientClasspath + File.pathSeparator + minecraftClasspath + File.pathSeparator + modLoaderClasspath + File.pathSeparator + lwjgl;

instance.assetsDir = Installer.installAssets(minecraftVersionInfo, gameDir, activity);
instance.assetsDir = Installer.installAssets(minecraftVersionInfo, gameDir, activity, instance);
} catch (IOException e) {
e.printStackTrace();
}
instance.assetIndex = minecraftVersionInfo.assetIndex.id;

// Write instance to json file
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
instance.updateMods(Constants.MC_DIR, instances);
instance.updateMods(instances);

API_V1.finishedDownloading = true;
Logger.getInstance().appendToLog("Finished Downloading!");
Expand All @@ -141,8 +195,7 @@ public static MinecraftInstances load(String gameDir) {
return instances;
}

public static void addMod(MinecraftInstances instances, MinecraftInstances.Instance instance,
String gameDir, String name, String version, String url) {
public static void addMod(MinecraftInstances instances, MinecraftInstances.Instance instance, String name, String version, String url) {
ModInfo info = new ModInfo();
info.slug = name;
info.download_link = url;
Expand All @@ -152,7 +205,7 @@ public static void addMod(MinecraftInstances instances, MinecraftInstances.Insta
mods.add(info);
instance.mods = mods.toArray(mods.toArray(new ModInfo[0]));

GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
GsonUtils.objectToJsonFile(Constants.USER_HOME + "/instances.json", instances);
}

public static boolean hasMod(MinecraftInstances.Instance instance, String name) {
Expand All @@ -165,7 +218,7 @@ public static boolean hasMod(MinecraftInstances.Instance instance, String name)
return false;
}

public static boolean removeMod(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir, String name) {
public static boolean removeMod(MinecraftInstances instances, MinecraftInstances.Instance instance, String name) {
ModInfo oldInfo = null;
for(ModInfo info : instance.mods) {
if(info.slug.equalsIgnoreCase(name)) {
Expand All @@ -176,24 +229,27 @@ public static boolean removeMod(MinecraftInstances instances, MinecraftInstances

if(oldInfo != null) {
// Delete the mod
File modFile = new File(gameDir + "/mods/" + instance.modsDirName + "/" + name + ".jar");
File modFile = new File(instance.gameDir + "/mods/" + name + ".jar");
modFile.delete();

ArrayList<ModInfo> mods = Lists.newArrayList(instance.mods);
mods.remove(oldInfo);
instance.mods = mods.toArray(mods.toArray(new ModInfo[0]));
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
GsonUtils.objectToJsonFile(Constants.USER_HOME + "/instances.json", instances);
}

return oldInfo != null;
}

// Return true if instance was deleted
public static boolean delete(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir) {
public static boolean delete(MinecraftInstances instances, MinecraftInstances.Instance instance) {
File instanceDir = new File(instance.gameDir);
instanceDir.delete();

ArrayList<MinecraftInstances.Instance> instances1 = Lists.newArrayList(instances.instances);
instances1.remove(instance);
instances.instances = instances1.toArray(new MinecraftInstances.Instance[0]);
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
GsonUtils.objectToJsonFile(Constants.USER_HOME + "/instances.json", instances);

return true;
}
Expand All @@ -202,7 +258,7 @@ public static void launchInstance(Activity activity, MinecraftAccount account, M
try {
JREUtils.redirectAndPrintJRELog();
VLoader.setAndroidInitInfo(activity);
JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.modsDirName);
JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance);
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/pojlib/UnityPlayerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ protected String updateUnityCommandLineArguments(String cmdLine)
e.printStackTrace();
}
}
if(new File(Constants.MC_DIR + "/assets").exists()) {
API_V1.finishedDownloading = true;
}
}

public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
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
Loading

0 comments on commit 305fd3b

Please sign in to comment.