From 0c34bd6b6b0dc7b0830699b4d394af5955bd878d Mon Sep 17 00:00:00 2001 From: ACGaming <4818419+ACGaming@users.noreply.github.com> Date: Sun, 5 Mar 2023 09:30:09 +0100 Subject: [PATCH] Migrate to new CurseForge API Optional TODO: Utilise FasterXML for this endeavour As I don't know about the syntax, GSON will have to do for now --- build.gradle | 2 +- mod-director-core/build.gradle | 7 +-- .../configuration/type/CurseRemoteMod.java | 44 +++++++++++-------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/build.gradle b/build.gradle index 632be21..205ad64 100644 --- a/build.gradle +++ b/build.gradle @@ -1,2 +1,2 @@ group 'net.jan' -version '1.5.1-SNAPSHOT' +version '1.5.2-SNAPSHOT' diff --git a/mod-director-core/build.gradle b/mod-director-core/build.gradle index b2bc984..9201b42 100644 --- a/mod-director-core/build.gradle +++ b/mod-director-core/build.gradle @@ -12,7 +12,8 @@ repositories { } dependencies { - implementation group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.10.3" - implementation group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.10.3" - implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.10.3" + implementation group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.13.5" + implementation group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.13.5" + implementation group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.13.5" + implementation 'com.google.code.gson:gson:2.10.1' } diff --git a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java index 3f42400..1c67e67 100644 --- a/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java +++ b/mod-director-core/src/main/java/net/jan/moddirector/core/configuration/type/CurseRemoteMod.java @@ -1,11 +1,24 @@ package net.jan.moddirector.core.configuration.type; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Map; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; - import net.jan.moddirector.core.ModDirector; import net.jan.moddirector.core.configuration.*; import net.jan.moddirector.core.exception.ModDirectorException; @@ -14,15 +27,6 @@ import net.jan.moddirector.core.util.WebClient; import net.jan.moddirector.core.util.WebGetResponse; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.util.Map; - public class CurseRemoteMod extends ModDirectorRemoteMod { private final int addonId; private final int fileId; @@ -69,12 +73,16 @@ public void performInstall(Path targetFile, ProgressCallback progressCallback, M @Override public RemoteModInformation queryInformation() throws ModDirectorException { try { - URL apiUrl = new URL( - String.format("https://addons-ecs.forgesvc.net/api/v2/addon/%d/file/%d", addonId, fileId)); - - information = ConfigurationController.OBJECT_MAPPER.readValue(apiUrl, CurseAddonFileInformation.class); + URL apiUrl = new URL(String.format("https://api.curse.tools/v1/cf/mods/%s/files/%s", addonId, fileId)); + HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); + connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.42"); + JsonObject jsonObject; + try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { + jsonObject = new JsonParser().parse(reader).getAsJsonObject().getAsJsonObject("data"); + } + information = ConfigurationController.OBJECT_MAPPER.readValue(jsonObject.toString(), CurseAddonFileInformation.class); } catch(MalformedURLException e) { - throw new ModDirectorException("Failed to create ForgeSVC api url", e); + throw new ModDirectorException("Failed to create curse.tools api url", e); } catch(JsonParseException e) { throw new ModDirectorException("Failed to parse Json response from curse", e); } catch(JsonMappingException e) { @@ -83,13 +91,13 @@ public RemoteModInformation queryInformation() throws ModDirectorException { throw new ModDirectorException("Failed to open connection to curse", e); } - return new RemoteModInformation(information.name, information.fileName); + return new RemoteModInformation(information.displayName, information.fileName); } @JsonIgnoreProperties(ignoreUnknown = true) private static class CurseAddonFileInformation { @JsonProperty - private String name; + private String displayName; @JsonProperty private String fileName; @@ -98,6 +106,6 @@ private static class CurseAddonFileInformation { private URL downloadUrl; @JsonProperty - private String[] gameVersion; + private String[] gameVersions; } }