Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/instance-refactor' into instance…
Browse files Browse the repository at this point in the history
…-refactor
  • Loading branch information
thejudge156 committed Apr 18, 2024
2 parents d81200e + e6f3436 commit cdbf105
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
java-version: '17'
distribution: 'temurin'
- name: Make Gradle wrapper executable
run: chmod +x ./gradlew
run: git lfs pull | chmod +x ./gradlew
- name: Build with Gradle
uses: gradle/gradle-build-action@v2
with:
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import pojlib.util.Constants;
import pojlib.util.GsonUtils;
import pojlib.util.Logger;

public class MinecraftAccount {

Expand All @@ -37,7 +38,9 @@ public static MinecraftAccount login(String gameDir, String[] response) throws I

public static boolean logout(Activity activity) {
File accountFile = new File(activity.getFilesDir() + "/accounts/account.json");
return accountFile.delete();
File accountCache = new File(Constants.USER_HOME + "/cache_data");

return accountFile.delete() && accountCache.delete();
}

//Try this before using login - the account will have been saved to disk if previously logged in
Expand All @@ -54,16 +57,16 @@ public static MinecraftAccount load(String path, @Nullable String newToken, @Nul
GsonUtils.objectToJsonFile(path + "/account.json", acc);
return acc;
} catch (IOException | JSONException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
return null;
}
}

public static String getSkinFaceUrl(MinecraftAccount account) {
//TODO: Log this to latestlog.txt for Support staff
try {
return Constants.MINOTAR_URL + "/helm/" + account.uuid;
} catch (NullPointerException e) {
System.out.println("Username not set! Please set your username at Minecraft.net and try again.");
Logger.getInstance().appendToLog("Username likely not set! Please set your username at Minecraft.net and try again. | " + e);
return null;
}
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/pojlib/api/API_V1.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class API_V1 {
* Add a mod to an instance
*
* @param instances Acquired from {@link pojlib.api.API_V1#loadAll(String)}
* @param instance Acquired from {@link pojlib.api.API_V1#createNewInstance(Activity, String, String, boolean, String, String)}
* @param instance Acquired from {@link pojlib.api.API_V1#createNewInstance(Activity, MinecraftInstances, String, String, boolean, String, String, String)}
* or {@link pojlib.api.API_V1#load(MinecraftInstances, String)}
* @param gameDir .minecraft directory
* @param name Mod name
Expand All @@ -62,7 +62,7 @@ public static void addMod(MinecraftInstances instances, MinecraftInstances.Insta
/**
* Check if an instance has a mod
*
* @param instance Acquired from {@link pojlib.api.API_V1#createNewInstance(Activity, String, String, boolean, String, String)}
* @param instance Acquired from {@link pojlib.api.API_V1#createNewInstance(Activity, MinecraftInstances, String, String, boolean, String, String, String)}
* or {@link pojlib.api.API_V1#load(MinecraftInstances, String)}
* @param name Mod name
* @return True if the mod is already in the instance
Expand All @@ -75,7 +75,7 @@ public static boolean hasMod(MinecraftInstances.Instance instance, String name)
* Remove a mod from an instance
*
* @param instances Acquired from {@link pojlib.api.API_V1#loadAll(String)}
* @param instance Acquired from {@link pojlib.api.API_V1#createNewInstance(Activity, String, String, boolean, String, String)}
* @param instance Acquired from {@link pojlib.api.API_V1#createNewInstance(Activity, MinecraftInstances, String, String, boolean, String, String, String)}
* or {@link pojlib.api.API_V1#load(MinecraftInstances, String)}
* @param gameDir .minecraft directory
* @param name Mod name
Expand Down Expand Up @@ -132,17 +132,18 @@ public static boolean deleteInstance(MinecraftInstances instances, MinecraftInst
* @param home The base directory where minecraft should be setup
* @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 home, boolean useDefaultMods, String minecraftVersion, String modsFolderName) throws IOException {
public static MinecraftInstances.Instance createNewInstance(Activity activity, MinecraftInstances instances, String instanceName, String home, boolean useDefaultMods, String minecraftVersion, String modsFolderName, String imageURL) throws IOException {

if(ignoreInstanceName) {
return InstanceHandler.create(activity, instances, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName);
return InstanceHandler.create(activity, instances, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName, 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, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName);
return InstanceHandler.create(activity, instances, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName, imageURL);
}
}

Expand All @@ -160,7 +161,7 @@ public static void updateMods(MinecraftInstances instances, MinecraftInstances.I
*
* @param activity Android activity object
* @param account Account object
* @param instance Instance object from {@link pojlib.api.API_V1#createNewInstance(Activity, String, String, boolean, String, String)}
* @param instance Instance object from {@link pojlib.api.API_V1#createNewInstance(Activity, MinecraftInstances, String, String, boolean, String, String, String)}
* or {@link pojlib.api.API_V1#load(MinecraftInstances, String)}
*/
public static void launchInstance(Activity activity, MinecraftAccount account, MinecraftInstances.Instance instance) {
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/pojlib/instance/InstanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import android.app.Activity;

import com.google.common.collect.Lists;
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;
Expand All @@ -20,8 +17,6 @@
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;
Expand All @@ -45,7 +40,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) {
public static MinecraftInstances.Instance create(Activity activity, MinecraftInstances instances, String instanceName, String gameDir, boolean useDefaultMods, String minecraftVersion, ModLoader modLoader, String modsFolderName, String imageURL) {
File instancesFile = new File(gameDir + "/instances.json");
if (instancesFile.exists()) {
for (MinecraftInstances.Instance instance : instances.instances) {
Expand All @@ -60,6 +55,7 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns

MinecraftInstances.Instance instance = new MinecraftInstances.Instance();
instance.instanceName = instanceName;
instance.instanceImageURL = imageURL;
instance.versionName = minecraftVersion;
instance.gameDir = new File(gameDir).getAbsolutePath();
instance.defaultMods = useDefaultMods;
Expand All @@ -83,12 +79,6 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns
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);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/pojlib/instance/MinecraftInstances.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public Instance[] toArray() {

public static class Instance {
public String instanceName;
public String instanceImageURL;
public String versionName;
public String modsDirName;
public String versionType;
Expand Down Expand Up @@ -101,10 +102,12 @@ public void updateMods(String gameDir, MinecraftInstances instances) {
DownloadUtils.downloadFile(info.download_link, mod);
modInfos.add(info);
}
for(ModInfo info : version.defaultMods) {
File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.slug + ".jar");
DownloadUtils.downloadFile(info.download_link, mod);
modInfos.add(info);
if(defaultMods) {
for (ModInfo info : version.defaultMods) {
File mod = new File(gameDir + "/mods/" + modsDirName + "/" + info.slug + ".jar");
DownloadUtils.downloadFile(info.download_link, mod);
modInfos.add(info);
}
}
mods = modInfos.toArray(modInfos.toArray(new ModInfo[0]));
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
Expand Down
28 changes: 1 addition & 27 deletions src/main/java/pojlib/util/DownloadUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
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;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
Expand All @@ -14,17 +10,10 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Objects;

import static pojlib.util.FileUtil.read;
import static pojlib.util.GsonUtils.GLOBAL_GSON;

public class DownloadUtils {

public static AssetManager assetManager;


private static void download(URL url, OutputStream os) throws IOException {
final int MAX_RETRIES = 3;
int attempts = 0;
Expand Down Expand Up @@ -96,23 +85,8 @@ public static boolean compareSHA1(File f, String sourceSHA) {
else return true; // fake match

}catch (IOException e) {
System.out.println("Fake-matching a hash due to a read error: " + e);
Logger.getInstance().appendToLog("Fake-matching a hash due to a read error: " + e);
return true;
}
}

public static ArrayList<String> getCompatibleVersions(String tag) {
ArrayList<String> versions = new ArrayList<>();
try {
InputStream stream = assetManager.open("jsons/modmanager.json");
JsonObject versionsJson = GLOBAL_GSON.fromJson(read(stream), JsonObject.class);

for (JsonElement version : versionsJson.get("compatible_versions").getAsJsonObject().getAsJsonArray(tag)) {
versions.add(version.getAsString());
}
} catch (IOException e) {
e.printStackTrace();
}
return versions;
}
}

0 comments on commit cdbf105

Please sign in to comment.