Skip to content

Commit

Permalink
Finish Instance Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
The Judge committed Apr 12, 2024
1 parent 02b360a commit c2dd986
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 402 deletions.
57 changes: 24 additions & 33 deletions src/main/java/pojlib/api/API_V1.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,17 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.util.Log;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.json.JSONException;

import pojlib.UnityPlayerActivity;
import pojlib.account.MinecraftAccount;
import pojlib.install.*;
import pojlib.instance.MinecraftInstance;
import pojlib.instance.InstanceHandler;
import pojlib.instance.MinecraftInstances;
import pojlib.util.APIHandler;
import pojlib.util.Constants;
import pojlib.util.LoginHelper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
Expand Down Expand Up @@ -65,20 +49,23 @@ public static MinecraftMeta.MinecraftVersion[] getMinecraftVersions() {
return MinecraftMeta.getVersions();
}

public static void addCustomMod(MinecraftInstance instance, String name, String version, String url) {
instance.addCustomMod(name, version, url);
public static void addMod(MinecraftInstances instances, MinecraftInstances.Instance instance,
String home, String name, String version, String url) {
InstanceHandler.addMod(instances, instance, home, name, version, url);
}

public static boolean hasMod(MinecraftInstance instance, String name) throws IOException {
return instance.hasCustomMod(name);
public static boolean hasMod(MinecraftInstances.Instance instance, String name) {
return InstanceHandler.hasMod(instance, name);
}

/**
* @return if the operation succeeds
*/
public static boolean removeMod(MinecraftInstance instance, String name) {
return instance.removeMod(name);
public static boolean removeMod(MinecraftInstances instances, MinecraftInstances.Instance instance,
String home, String name) {
return InstanceHandler.removeMod(instances, instance, home, name);
}

public static MinecraftMeta.MinecraftVersion[] getQCSupportedVersions() throws IOException {
return APIHandler.getQCSupportedVersions();
}
Expand All @@ -88,14 +75,17 @@ public static String getQCSupportedVersionName(MinecraftMeta.MinecraftVersion ve
}

/**
* Loads an instance from the filesystem.
* Loads all instances from the filesystem.
*
* @param instanceName The instance being loaded
* @param gameDir .minecraft directory.
* @return A minecraft instance object
*/
public static MinecraftInstance load(String instanceName, String gameDir) {
return MinecraftInstance.load(instanceName, gameDir);
public static MinecraftInstances loadAll(String gameDir) {
return InstanceHandler.load(gameDir);
}

public static MinecraftInstances.Instance load(MinecraftInstances instances, String name) {
return instances.load(name);
}

/**
Expand All @@ -109,19 +99,20 @@ public static MinecraftInstance load(String instanceName, String gameDir) {
* @return A minecraft instance object
* @throws IOException Throws if download of library or asset fails
*/
public static MinecraftInstance createNewInstance(Activity activity, String instanceName, String home, MinecraftMeta.MinecraftVersion minecraftVersion, MinecraftInstance.ModLoader modLoader) throws IOException {
public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home,
MinecraftMeta.MinecraftVersion minecraftVersion, InstanceHandler.ModLoader modLoader, String modsFolderName) throws IOException {

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

public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstance instance) {
instance.launchInstance(activity, account);
public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstances.Instance instance) {
InstanceHandler.launchInstance(activity, account, instance);
}

/**
Expand Down
182 changes: 182 additions & 0 deletions src/main/java/pojlib/instance/InstanceHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package pojlib.instance;

import android.app.Activity;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import pojlib.account.MinecraftAccount;
import pojlib.api.API_V1;
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.ModInfo;
import pojlib.util.ModsJson;
import pojlib.util.DownloadUtils;
import pojlib.util.GsonUtils;
import pojlib.util.JREUtils;
import pojlib.util.Logger;
import pojlib.util.VLoader;

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 enum ModLoader {
Fabric(0),
Quilt(1),
Forge(2),
NeoForge(3);

public final int index;

ModLoader(int i) {
this.index = i;
}
}

//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) {
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();
if(modsFolderName != null) {
instance.modsDirName = modsFolderName;
} else {
instance.modsDirName = instance.versionName;
}

VersionInfo modLoaderVersionInfo = null;
switch (modLoader) {
case Fabric: {
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;
}
case Forge:
case NeoForge:
{
System.out.println("Error!: You cannot use Forge or NeoForge with QuestCraft!");
break;
}
}

VersionInfo minecraftVersionInfo = MinecraftMeta.getVersionInfo(minecraftVersion);
instance.versionType = minecraftVersionInfo.type;
instance.mainClass = modLoaderVersionInfo.mainClass;

// Install minecraft
VersionInfo finalModLoaderVersionInfo = modLoaderVersionInfo;
new Thread(() -> {
try {
String clientClasspath = Installer.installClient(minecraftVersionInfo, gameDir);
String minecraftClasspath = Installer.installLibraries(minecraftVersionInfo, gameDir);
String modLoaderClasspath = Installer.installLibraries(finalModLoaderVersionInfo, gameDir);
String lwjgl = Installer.installLwjgl(activity);

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

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

MinecraftInstances instances;
try {
instances = GsonUtils.jsonFileToObject(gameDir + "/instances.json", MinecraftInstances.class);
} catch (Exception e) {
instances = new MinecraftInstances();
}
assert instances != null;
instances.instances.add(instance);

// Write instance to json file
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
API_V1.finishedDownloading = true;
}).start();

return instance;
}

// Load an instance from json
public static MinecraftInstances load(String gameDir) {
return GsonUtils.jsonFileToObject(gameDir + "/instances.json", MinecraftInstances.class);
}

public static void addMod(MinecraftInstances instances, MinecraftInstances.Instance instance,
String gameDir, String name, String version, String url) {
ModInfo info = new ModInfo();
info.name = name;
info.url = url;
info.version = version;

instance.mods.add(info);

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

public static boolean hasMod(MinecraftInstances.Instance instance, String name) {
for(ModInfo info : instance.mods) {
if(info.name.equals(name)) {
return true;
}
}

return false;
}

public static boolean removeMod(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir, String name) {
ModInfo oldInfo = null;
for(ModInfo info : instance.mods) {
if(info.name.equals(name)) {
oldInfo = info;
break;
}
}

if(oldInfo != null) {
instance.mods.remove(oldInfo);
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
}

return oldInfo != null;
}

// Return true if instance was deleted
public static boolean delete(MinecraftInstances instances, MinecraftInstances.Instance instance, String gameDir) {
instances.instances.remove(instance);
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);

return true;
}

public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstances.Instance instance) {
try {
JREUtils.redirectAndPrintJRELog();
VLoader.setAndroidInitInfo(activity);
while(!API_V1.finishedDownloading);
JREUtils.launchJavaVM(activity, instance.generateLaunchArgs(account), instance.versionName);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Loading

0 comments on commit c2dd986

Please sign in to comment.