-
-
Notifications
You must be signed in to change notification settings - Fork 0
GH-48 Introduce simple update checker, using Modrinth api. #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
02e8b58
Introduce simple update checker, using Modrinth api.
vLuckyyy fa95908
Use old eternalcombat version for testing purposes.
vLuckyyy 12df81d
Use `org.json` for json parsing.
vLuckyyy 7abfca4
Fix.
vLuckyyy 0e7c815
Fix.
vLuckyyy a828e23
Cleanup and use gson
Rollczi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ plugins { | |
} | ||
|
||
repositories { | ||
mavenCentral() | ||
gradlePluginPortal() | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
plugins { | ||
`commons-java-17` | ||
`commons-repositories` | ||
} | ||
|
||
dependencies { | ||
implementation(project(":eternalcode-commons-updater")) | ||
} |
20 changes: 20 additions & 0 deletions
20
...updater-example/src/main/java/com/eternalcode/commons/updater/example/ExampleChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.eternalcode.commons.updater.example; | ||
|
||
import com.eternalcode.commons.updater.UpdateResult; | ||
|
||
public class ExampleChecker { | ||
|
||
private static final String OLD_ETERNALCOMBAT_VERSION = "1.3.3"; | ||
|
||
public static void main(String[] args) { | ||
ExampleUpdateService updateService = new ExampleUpdateService(); | ||
|
||
UpdateResult modrinthResult = updateService.checkModrinth("EternalCombat", OLD_ETERNALCOMBAT_VERSION); | ||
System.out.println("Modrinth update available: " + modrinthResult.isUpdateAvailable()); | ||
if (modrinthResult.isUpdateAvailable()) { | ||
System.out.println("Latest: " + modrinthResult.latestVersion()); | ||
System.out.println("Download: " + modrinthResult.downloadUrl()); | ||
System.out.println("Release page: " + modrinthResult.releaseUrl()); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...r-example/src/main/java/com/eternalcode/commons/updater/example/ExampleUpdateService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.eternalcode.commons.updater.example; | ||
|
||
import com.eternalcode.commons.updater.UpdateResult; | ||
import com.eternalcode.commons.updater.Version; | ||
import com.eternalcode.commons.updater.impl.ModrinthUpdateChecker; | ||
|
||
public final class ExampleUpdateService { | ||
private final ModrinthUpdateChecker modrinthChecker = new ModrinthUpdateChecker(); | ||
|
||
public UpdateResult checkModrinth(String projectId, String currentVersion) { | ||
return modrinthChecker.check(projectId, new Version(currentVersion)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
plugins { | ||
`commons-java-17` | ||
`commons-publish` | ||
`commons-repositories` | ||
`commons-java-unit-test` | ||
} | ||
|
||
tasks.test { | ||
useJUnitPlatform() | ||
} | ||
|
||
|
||
dependencies { | ||
api("com.google.code.gson:gson:2.13.1") | ||
api(project(":eternalcode-commons-shared")) | ||
|
||
api("org.jetbrains:annotations:24.1.0") | ||
} |
6 changes: 6 additions & 0 deletions
6
eternalcode-commons-updater/src/main/java/com/eternalcode/commons/updater/UpdateChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.eternalcode.commons.updater; | ||
|
||
public interface UpdateChecker { | ||
|
||
UpdateResult check(String projectId, Version currentVersion); | ||
} |
13 changes: 13 additions & 0 deletions
13
eternalcode-commons-updater/src/main/java/com/eternalcode/commons/updater/UpdateResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.eternalcode.commons.updater; | ||
|
||
public record UpdateResult(Version currentVersion, Version latestVersion, String downloadUrl, String releaseUrl) { | ||
|
||
public static UpdateResult empty(Version currentVersion) { | ||
return new UpdateResult(currentVersion, currentVersion, null, null); | ||
} | ||
|
||
public boolean isUpdateAvailable() { | ||
return this.latestVersion.isNewerThan(this.currentVersion); | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
eternalcode-commons-updater/src/main/java/com/eternalcode/commons/updater/Version.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.eternalcode.commons.updater; | ||
|
||
import java.util.Arrays; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class Version implements Comparable<Version> { | ||
|
||
private static final int DEFAULT_VERSION_COMPONENT_VALUE = 0; | ||
|
||
private final String value; | ||
private final int[] versionComponents; | ||
|
||
public Version(String version) { | ||
if (version == null || version.trim().isEmpty()) { | ||
throw new IllegalArgumentException("Version cannot be null or empty"); | ||
} | ||
|
||
this.value = version.trim(); | ||
this.versionComponents = parseVersion(this.value); | ||
} | ||
|
||
private int[] parseVersion(String version) { | ||
String cleaned = cleanVersion(version); | ||
String[] rawVersionComponents = cleaned.split("\\."); | ||
int[] versionComponents = new int[rawVersionComponents.length]; | ||
|
||
for (int i = 0; i < rawVersionComponents.length; i++) { | ||
try { | ||
versionComponents[i] = Integer.parseInt(rawVersionComponents[i]); | ||
} | ||
catch (NumberFormatException exception) { | ||
throw new IllegalArgumentException("Invalid version format: " + version); | ||
} | ||
} | ||
|
||
return versionComponents; | ||
} | ||
|
||
private static String cleanVersion(String version) { | ||
String cleaned = version.startsWith("v") ? version.substring(1) : version; | ||
int dashIndex = cleaned.indexOf('-'); | ||
if (dashIndex > 0) { | ||
return cleaned.substring(0, dashIndex); | ||
} | ||
|
||
return cleaned; | ||
} | ||
|
||
@Override | ||
public int compareTo(@NotNull Version other) { | ||
int maxLength = Math.max(this.versionComponents.length, other.versionComponents.length); | ||
|
||
for (int i = 0; i < maxLength; i++) { | ||
int thisComponent = getComponentAtIndex(i, this); | ||
int otherComponent = getComponentAtIndex(i, other); | ||
|
||
int result = Integer.compare(thisComponent, otherComponent); | ||
if (result != 0) { | ||
return result; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
private int getComponentAtIndex(int index, Version version) { | ||
return index < version.versionComponents.length | ||
? version.versionComponents[index] | ||
: DEFAULT_VERSION_COMPONENT_VALUE; | ||
} | ||
|
||
public boolean isNewerThan(Version other) { | ||
return this.compareTo(other) > 0; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (obj == null || getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
|
||
Version version = (Version) obj; | ||
return this.compareTo(version) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Arrays.hashCode(versionComponents); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...ons-updater/src/main/java/com/eternalcode/commons/updater/impl/ModrinthUpdateChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.eternalcode.commons.updater.impl; | ||
|
||
import com.eternalcode.commons.updater.UpdateChecker; | ||
import com.eternalcode.commons.updater.UpdateResult; | ||
import com.eternalcode.commons.updater.Version; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonParseException; | ||
import com.google.gson.annotations.SerializedName; | ||
import com.google.gson.reflect.TypeToken; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public final class ModrinthUpdateChecker implements UpdateChecker { | ||
|
||
private static final String API_BASE_URL = "https://api.modrinth.com/v2"; | ||
private static final String MODRINTH_BASE_URL = "https://modrinth.com/plugin"; | ||
private static final String USER_AGENT = "UpdateChecker/1.0"; | ||
|
||
private static final Gson GSON = new Gson(); | ||
|
||
private final HttpClient client = HttpClient.newBuilder() | ||
.connectTimeout(Duration.ofSeconds(60)) | ||
.build(); | ||
|
||
@Override | ||
public UpdateResult check(String projectId, Version currentVersion) { | ||
if (projectId == null || projectId.isBlank()) { | ||
throw new IllegalArgumentException("Project ID cannot be null or empty"); | ||
} | ||
|
||
try { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.uri(URI.create(API_BASE_URL + "/project/" + projectId + "/version")) | ||
.header("User-Agent", USER_AGENT) | ||
.timeout(Duration.ofSeconds(30)) | ||
.build(); | ||
|
||
HttpResponse<String> response = this.client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
if (response.statusCode() != 200) { | ||
return UpdateResult.empty(currentVersion); | ||
} | ||
|
||
return this.parseVersionResponse(response.body(), currentVersion, projectId); | ||
} | ||
catch (Exception exception) { | ||
throw new RuntimeException("Failed to check Modrinth updates for project: " + projectId, exception); | ||
} | ||
} | ||
|
||
private UpdateResult parseVersionResponse(String json, Version currentVersion, String projectId) { | ||
try { | ||
List<ModrinthVersion> versions = GSON.fromJson(json, new TypeToken<>(){}); | ||
if (versions == null || versions.isEmpty()) { | ||
return UpdateResult.empty(currentVersion); | ||
} | ||
|
||
ModrinthVersion latestVersionData = versions.get(0); | ||
String versionNumber = latestVersionData.versionNumber(); | ||
if (versionNumber == null || versionNumber.trim().isEmpty()) { | ||
return UpdateResult.empty(currentVersion); | ||
} | ||
|
||
String releaseUrl = MODRINTH_BASE_URL + "/" + projectId + "/version/" + versionNumber; | ||
String downloadUrl = latestVersionData.files().stream() | ||
.map(modrinthFile -> modrinthFile.url()) | ||
.filter(obj -> Objects.nonNull(obj)) | ||
.findFirst() | ||
.orElse(releaseUrl); | ||
|
||
Version latestVersion = new Version(versionNumber); | ||
return new UpdateResult(currentVersion, latestVersion, downloadUrl, releaseUrl); | ||
} | ||
catch (JsonParseException exception) { | ||
return UpdateResult.empty(currentVersion); | ||
} | ||
} | ||
|
||
private record ModrinthVersion(@SerializedName("version_number") String versionNumber, List<ModrinthFile> files) { | ||
} | ||
|
||
private record ModrinthFile(String url) { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.