Skip to content

Commit

Permalink
Add deletion, fix mod dir and default mods
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Apr 12, 2024
1 parent c2dd986 commit 7b5ae4d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/main/java/pojlib/api/API_V1.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public static MinecraftInstances.Instance load(MinecraftInstances instances, Str
return instances.load(name);
}

public static boolean deleteInstance(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir) {
return InstanceHandler.delete(instances, instance, gameDir);
}

/**
* Creates a new game instance with a selected mod loader. The latest version of the mod loader will be installed
*
Expand All @@ -99,15 +103,15 @@ public static MinecraftInstances.Instance load(MinecraftInstances instances, Str
* @return A minecraft instance object
* @throws IOException Throws if download of library or asset fails
*/
public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home,
public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home, boolean useDefaultMods,
MinecraftMeta.MinecraftVersion minecraftVersion, InstanceHandler.ModLoader modLoader, String modsFolderName) throws IOException {

if(ignoreInstanceName) {
return InstanceHandler.create(activity, instanceName, home, minecraftVersion, modLoader, modsFolderName);
return InstanceHandler.create(activity, instanceName, home, useDefaultMods, minecraftVersion, modLoader, modsFolderName);
} else if (instanceName.contains("/") || instanceName.contains("!")) {
throw new IOException("You cannot use special characters (!, /, ., etc) when creating instances.");
} else {
return InstanceHandler.create(activity, instanceName, home, minecraftVersion, modLoader, modsFolderName);
return InstanceHandler.create(activity, instanceName, home, useDefaultMods, minecraftVersion, modLoader, modsFolderName);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/pojlib/instance/InstanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ 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, String instanceName, String gameDir, MinecraftMeta.MinecraftVersion minecraftVersion, ModLoader modLoader, String modsFolderName) {
public static MinecraftInstances.Instance create(Activity activity, String instanceName, String gameDir, boolean useDefaultMods,
MinecraftMeta.MinecraftVersion minecraftVersion, ModLoader modLoader, String modsFolderName) {
Logger.getInstance().appendToLog("Creating new instance: " + instanceName);

MinecraftInstances.Instance instance = new MinecraftInstances.Instance();
instance.instanceName = instanceName;
instance.versionName = minecraftVersion.id;
instance.gameDir = new File(gameDir).getAbsolutePath();
instance.defaultMods = useDefaultMods;
if(modsFolderName != null) {
instance.modsDirName = modsFolderName;
} else {
Expand Down Expand Up @@ -173,8 +175,9 @@ public static void launchInstance(Activity activity, MinecraftAccount account, M
try {
JREUtils.redirectAndPrintJRELog();
VLoader.setAndroidInitInfo(activity);
instance.updateMods(Constants.MC_DIR);
while(!API_V1.finishedDownloading);
JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.versionName);
JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.modsDirName);
} catch (Throwable e) {
e.printStackTrace();
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/pojlib/instance/MinecraftInstances.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public static class Instance {
public String assetsDir;
public String mainClass;
public ArrayList<ModInfo> mods;
public boolean defaultMods;

public List<String> generateLaunchArgs(MinecraftAccount account) {
String[] mcArgs = {"--username", account.username, "--version", versionName, "--gameDir", gameDir,
Expand All @@ -61,6 +62,7 @@ private ModsJson parseModsJson(String gameDir) throws Exception {
}

public void updateMods(String gameDir) {
API_V1.finishedDownloading = false;
try {
ModsJson modsJson = parseModsJson(gameDir);

Expand Down Expand Up @@ -88,21 +90,25 @@ public void updateMods(String gameDir) {
}
}

for(ModInfo info : version.defaultMods) {
for(ModInfo currInfo : mods) {
if(!currInfo.name.equals(info.name)) {
continue;
if(defaultMods) {
for (ModInfo info : version.defaultMods) {
for (ModInfo currInfo : mods) {
if (!currInfo.name.equals(info.name)) {
continue;
}
if (currInfo.version.equals(info.version)) {
continue;
}

File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.name);
DownloadUtils.downloadFile(info.url, mod);
}
if(currInfo.version.equals(info.version)) {
continue;
}

File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.name);
DownloadUtils.downloadFile(info.url, mod);
}
}
API_V1.finishedDownloading = true;
} catch (Exception e) {
Logger.getInstance().appendToLog("Mods failed to download! Are you offline?\n" + e.getMessage());
API_V1.finishedDownloading = true;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/pojlib/util/JREUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public static void setJavaEnvironment(Activity activity) throws Throwable {
setLdLibraryPath(jvmLibraryPath+":"+LD_LIBRARY_PATH);
}

public static int launchJavaVM(final Activity activity, final List<String> JVMArgs, String versionName) throws Throwable {
public static int launchJavaVM(final Activity activity, final List<String> JVMArgs, String modsDirName) throws Throwable {
JREUtils.relocateLibPath(activity);
setJavaEnvironment(activity);

Expand All @@ -196,7 +196,7 @@ public static int launchJavaVM(final Activity activity, final List<String> JVMAr
userArgs.add("-Dorg.lwjgl.opengl.libname=" + graphicsLib);
userArgs.add("-Dorg.lwjgl.opengles.libname=" + "/system/lib64/libGLESv3.so");
userArgs.add("-Dorg.lwjgl.egl.libname=" + "/system/lib64/libEGL_dri.so");
userArgs.add("-Dfabric.addMods=" + Constants.MC_DIR + "/mods/" + versionName);
userArgs.add("-Dfabric.addMods=" + Constants.MC_DIR + "/mods/" + modsDirName);

userArgs.addAll(JVMArgs);
System.out.println(JVMArgs);
Expand Down

0 comments on commit 7b5ae4d

Please sign in to comment.