Skip to content

Commit

Permalink
Refactor[modpacks]: handle non valued algo
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias-Boulay committed Nov 29, 2023
1 parent b695336 commit 3635f53
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.JsonArray;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 3635f53

Please sign in to comment.