Skip to content

Commit

Permalink
Migrate to new CurseForge API
Browse files Browse the repository at this point in the history
Optional TODO: Utilise FasterXML for this endeavour
As I don't know about the syntax, GSON will have to do for now
  • Loading branch information
ACGaming committed Mar 5, 2023
1 parent 84b903b commit 0c34bd6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group 'net.jan'
version '1.5.1-SNAPSHOT'
version '1.5.2-SNAPSHOT'
7 changes: 4 additions & 3 deletions mod-director-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -98,6 +106,6 @@ private static class CurseAddonFileInformation {
private URL downloadUrl;

@JsonProperty
private String[] gameVersion;
private String[] gameVersions;
}
}

1 comment on commit 0c34bd6

@ACGaming
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closes Janrupf#27

Please sign in to comment.