From 3635f53faebd245dc59ed23731815c27974b534d Mon Sep 17 00:00:00 2001 From: Mathias-Boulay Date: Thu, 30 Nov 2023 00:10:37 +0100 Subject: [PATCH] Refactor[modpacks]: handle non valued algo --- .../modpacks/api/CurseforgeApi.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/CurseforgeApi.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/CurseforgeApi.java index 733c713c65..5c6d83a1ee 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/CurseforgeApi.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/modloaders/modpacks/api/CurseforgeApi.java @@ -2,6 +2,7 @@ import android.util.Log; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.gson.JsonArray; @@ -124,15 +125,7 @@ public ModDetail getModDetails(ModItem item) { break; } - JsonArray downloadHashes = modDetail.getAsJsonArray("hashes"); - hashes[i] = null; - for (JsonElement jsonElement : downloadHashes) { - // The sha1 = 1; md5 = 2; - if(jsonElement.getAsJsonObject().get("algo").getAsInt() == ALGO_SHA_1){ - hashes[i] = jsonElement.getAsJsonObject().get("value").getAsString(); - break; - } - } + hashes[i] = getSha1FromResponse(modDetail); } return new ModDetail(item, versionNames, mcVersionNames, versionUrls, hashes); } @@ -247,15 +240,18 @@ private String getDownloadUrl(long projectID, long fileID) { JsonObject response = mApiHandler.get("mods/"+projectID+"/files/"+fileID, JsonObject.class); if (response == null || response.get("data").isJsonNull()) return null; - JsonArray hashes = response.get("data").getAsJsonObject().getAsJsonArray("hashes"); + return getSha1FromResponse(response); + } + + private String getSha1FromResponse(@NonNull JsonElement element) { + JsonArray hashes = element.getAsJsonObject().get("data").getAsJsonObject().getAsJsonArray("hashes"); for (JsonElement jsonElement : hashes) { // The sha1 = 1; md5 = 2; - if(jsonElement.getAsJsonObject().get("algo").getAsInt() == ALGO_SHA_1){ + JsonElement algo = jsonElement.getAsJsonObject().get("algo"); + if(algo != null && algo.getAsInt() == ALGO_SHA_1){ return jsonElement.getAsJsonObject().get("value").getAsString(); } } - - // No hashes available return null; }