mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-16 23:25:12 +00:00
V2 update checker API. No more manual bumps!
This commit is contained in:
5 files changed
+289
-54
No files matched your search
@@ -28,14 +28,17 @@ import com.falsepattern.lib.updates.ModUpdateInfo;
|
||||
import com.falsepattern.lib.updates.UpdateCheckException;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import lombok.val;
|
||||
import lombok.var;
|
||||
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.event.ClickEvent;
|
||||
import net.minecraft.util.IChatComponent;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
@@ -43,11 +46,9 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class UpdateCheckerImpl {
|
||||
private static final AtomicBoolean jsonLibraryLoaded = new AtomicBoolean(false);
|
||||
|
||||
public static List<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) {
|
||||
if (updates == null || updates.size() == 0) {
|
||||
return null;
|
||||
@@ -84,32 +85,37 @@ public final class UpdateCheckerImpl {
|
||||
return updateText;
|
||||
}
|
||||
|
||||
private static JsonArray fetchRootListShared(String url) {
|
||||
if (!LibraryConfig.ENABLE_UPDATE_CHECKER) {
|
||||
throw new CompletionException(new UpdateCheckException("Update checker is disabled in config!"));
|
||||
}
|
||||
URL URL;
|
||||
try {
|
||||
URL = new URL(url);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new CompletionException(new UpdateCheckException("Invalid URL: " + url, e));
|
||||
}
|
||||
JsonElement parsed;
|
||||
try {
|
||||
parsed = new JsonParser().parse(Internet.download(URL).thenApply(String::new).join());
|
||||
} catch (CompletionException e) {
|
||||
throw new CompletionException(new UpdateCheckException("Failed to download update checker JSON file!",
|
||||
e.getCause() == null ? e : e.getCause()));
|
||||
}
|
||||
JsonArray modList;
|
||||
if (parsed.isJsonArray()) {
|
||||
modList = parsed.getAsJsonArray();
|
||||
} else {
|
||||
modList = new JsonArray();
|
||||
modList.add(parsed);
|
||||
}
|
||||
return modList;
|
||||
}
|
||||
|
||||
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
if (!LibraryConfig.ENABLE_UPDATE_CHECKER) {
|
||||
throw new CompletionException(new UpdateCheckException("Update checker is disabled in config!"));
|
||||
}
|
||||
URL URL;
|
||||
try {
|
||||
URL = new URL(url);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new CompletionException(new UpdateCheckException("Invalid URL: " + url, e));
|
||||
}
|
||||
val modList = fetchRootListShared(url);
|
||||
val result = new ArrayList<ModUpdateInfo>();
|
||||
JsonElement parsed;
|
||||
try {
|
||||
parsed = new JsonParser().parse(Internet.download(URL).thenApply(String::new).join());
|
||||
} catch (CompletionException e) {
|
||||
throw new CompletionException(new UpdateCheckException("Failed to download update checker JSON file!",
|
||||
e.getCause() == null ? e : e.getCause()));
|
||||
}
|
||||
JsonArray modList;
|
||||
if (parsed.isJsonArray()) {
|
||||
modList = parsed.getAsJsonArray();
|
||||
} else {
|
||||
modList = new JsonArray();
|
||||
modList.add(parsed);
|
||||
}
|
||||
val installedMods = Loader.instance().getIndexedModList();
|
||||
for (val node : modList) {
|
||||
if (!node.isJsonObject()) {
|
||||
@@ -170,4 +176,159 @@ public final class UpdateCheckerImpl {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsyncV2(String url) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
val modList = fetchRootListShared(url);
|
||||
val result = new ArrayList<ModUpdateInfo>();
|
||||
val installedMods = Loader.instance().getIndexedModList();
|
||||
for (val node : modList) {
|
||||
if (!node.isJsonObject()) {
|
||||
continue;
|
||||
}
|
||||
val obj = node.getAsJsonObject();
|
||||
if (!obj.has("modid")) {
|
||||
continue;
|
||||
}
|
||||
if (!obj.has("versions")) {
|
||||
continue;
|
||||
}
|
||||
val modid = obj.get("modid").getAsString();
|
||||
if (!installedMods.containsKey(modid)) {
|
||||
continue;
|
||||
}
|
||||
val mod = installedMods.get(modid);
|
||||
var versions = obj.get("versions");
|
||||
if (!versions.isJsonArray()) {
|
||||
if (!versions.isJsonObject()) {
|
||||
continue;
|
||||
}
|
||||
val ver = versions.getAsJsonObject();
|
||||
versions = new JsonArray();
|
||||
versions.getAsJsonArray().add(ver);
|
||||
}
|
||||
val parsedVersions = parseVersionSpecs(versions.getAsJsonArray()).join();
|
||||
val latestVersions = new ArrayList<String>();
|
||||
for (val version : parsedVersions) {
|
||||
val versionObj = version.getAsJsonObject();
|
||||
latestVersions.add(versionObj.get("version").getAsString());
|
||||
}
|
||||
val currentVersion = mod.getVersion();
|
||||
if (latestVersions.contains(currentVersion)) {
|
||||
continue;
|
||||
}
|
||||
val latest = parsedVersions.get(0).getAsJsonObject();
|
||||
result.add(new ModUpdateInfo(modid, currentVersion, latest.get("version").getAsString(), latest.get("url").getAsString()));
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private static String getString(JsonObject object, String entry) {
|
||||
if (!object.has(entry)) {
|
||||
return null;
|
||||
}
|
||||
val element = object.get(entry);
|
||||
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isString()) {
|
||||
return element.getAsString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static CompletableFuture<JsonArray> parseVersionSpecs(JsonArray versions) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
val result = new JsonArray();
|
||||
for (val entry: versions) {
|
||||
if (!entry.isJsonObject()) {
|
||||
continue;
|
||||
}
|
||||
val obj = entry.getAsJsonObject();
|
||||
val type = getString(obj, "type");
|
||||
if (type == null) {
|
||||
continue;
|
||||
}
|
||||
switch (type) {
|
||||
case "raw": {
|
||||
val version = getString(obj, "version");
|
||||
val url = getString(obj, "updateURL");
|
||||
if (version == null || url == null) {
|
||||
continue;
|
||||
}
|
||||
val res = new JsonObject();
|
||||
res.addProperty("version", version);
|
||||
res.addProperty("url", url);
|
||||
result.add(res);
|
||||
}
|
||||
case "github": {
|
||||
val repo = getString(obj, "repo");
|
||||
if (repo == null) {
|
||||
continue;
|
||||
}
|
||||
JsonObject response;
|
||||
try {
|
||||
response = parseLatestGithubVersion(repo);
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
continue;
|
||||
}
|
||||
if (response == null) {
|
||||
continue;
|
||||
}
|
||||
result.add(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
private static JsonObject parseLatestGithubVersion(String repo) throws MalformedURLException {
|
||||
val parts = repo.split("/");
|
||||
if (parts.length != 2) {
|
||||
return null;
|
||||
}
|
||||
val owner = parts[0];
|
||||
val name = parts[1];
|
||||
val response = new AtomicReference<JsonObject>();
|
||||
val result = new JsonObject();
|
||||
Internet.connect(new URL("https://api.github.com/repos/" + owner + "/" + name + "/releases/latest"),
|
||||
Internet.constructHeaders("Accept", "application/vnd.github+json",
|
||||
"X-GitHub-Api-Version", "2022-11-28"),
|
||||
(e) -> {throw new CompletionException(e);
|
||||
},
|
||||
(stream) -> {
|
||||
val parser = new JsonParser();
|
||||
val json = parser.parse(new InputStreamReader(stream));
|
||||
if (!json.isJsonObject()) {
|
||||
return;
|
||||
}
|
||||
response.set(json.getAsJsonObject());
|
||||
});
|
||||
if (response.get() == null) {
|
||||
return null;
|
||||
}
|
||||
val payload = response.get();
|
||||
val url = getString(payload, "html_url");
|
||||
val version = getString(payload, "tag_name");
|
||||
if (version == null) {
|
||||
return null;
|
||||
}
|
||||
result.addProperty("version", version);
|
||||
result.addProperty("url", url == null ? "" : url);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static List<ModUpdateInfo> fetchUpdatesV2(String url) throws UpdateCheckException {
|
||||
try {
|
||||
return fetchUpdatesAsyncV2(url).join();
|
||||
} catch (CompletionException e) {
|
||||
try {
|
||||
throw e.getCause();
|
||||
} catch (UpdateCheckException e1) {
|
||||
throw e1;
|
||||
} catch (Throwable e1) {
|
||||
throw new UpdateCheckException("Failed to check for updates!", e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user