2022-06-09 20:14:11 +02:00
|
|
|
package com.falsepattern.lib.updates;
|
|
|
|
|
|
2022-06-10 00:09:13 +02:00
|
|
|
import com.falsepattern.json.node.JsonNode;
|
2022-06-10 00:15:08 +02:00
|
|
|
import com.falsepattern.lib.StableAPI;
|
2022-06-09 20:14:11 +02:00
|
|
|
import com.falsepattern.lib.dependencies.DependencyLoader;
|
|
|
|
|
import com.falsepattern.lib.dependencies.SemanticVersion;
|
|
|
|
|
import com.falsepattern.lib.internal.FalsePatternLib;
|
2022-06-10 00:09:13 +02:00
|
|
|
import com.falsepattern.lib.internal.Internet;
|
2022-06-09 20:14:11 +02:00
|
|
|
import com.falsepattern.lib.internal.LibraryConfig;
|
|
|
|
|
import com.falsepattern.lib.internal.Tags;
|
2022-06-10 02:55:46 +02:00
|
|
|
import com.falsepattern.lib.util.Async;
|
2022-06-10 00:09:13 +02:00
|
|
|
import cpw.mods.fml.common.Loader;
|
2022-06-09 20:14:11 +02:00
|
|
|
import lombok.val;
|
|
|
|
|
|
2022-06-10 00:09:13 +02:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2022-06-09 20:14:11 +02:00
|
|
|
import java.io.IOException;
|
2022-06-10 00:09:13 +02:00
|
|
|
import java.net.MalformedURLException;
|
2022-06-09 20:14:11 +02:00
|
|
|
import java.net.URL;
|
2022-06-10 00:09:13 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
2022-06-09 20:14:11 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.Future;
|
2022-06-10 00:09:13 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
2022-06-09 20:14:11 +02:00
|
|
|
|
2022-06-10 00:15:08 +02:00
|
|
|
@StableAPI(since = "0.8.0")
|
2022-06-09 20:14:11 +02:00
|
|
|
public class UpdateChecker {
|
2022-06-10 00:15:08 +02:00
|
|
|
private static final AtomicInteger jsonLibraryLoaded = new AtomicInteger(0);
|
2022-06-09 20:14:11 +02:00
|
|
|
/**
|
|
|
|
|
* Same this as {@link #fetchUpdates(String)}, but defers the check to a different thread. Useful for asynchronous
|
|
|
|
|
* update checks, if you don't want to block loading.
|
|
|
|
|
* @param url The URL to check
|
|
|
|
|
* @return A future that will contain the update info about mods that were both available on the URL and installed
|
|
|
|
|
*/
|
|
|
|
|
public static Future<List<ModUpdateInfo>> fetchUpdatesAsync(String url) {
|
2022-06-10 02:55:46 +02:00
|
|
|
return Async.asyncWorker.submit(() -> fetchUpdates(url));
|
2022-06-09 20:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks for updates. The URL should be a JSON file that contains a list of mods, each with a mod ID, one or more
|
|
|
|
|
* versions, and a URL for the user to check for updates in case the current and latest versions are different.
|
|
|
|
|
* The JSON file must have the following format:
|
|
|
|
|
* <pre>{@code
|
|
|
|
|
* [
|
|
|
|
|
* {
|
|
|
|
|
* "modID": "modid",
|
|
|
|
|
* "latestVersion": ["1.0.0", "1.0.0-foo"],
|
|
|
|
|
* "updateURL": "https://example.com/mods/mymod"
|
|
|
|
|
* },
|
|
|
|
|
* {
|
|
|
|
|
* "modID": "modid2",
|
|
|
|
|
* "latestVersion": ["0.2.0", "0.3.0-alpha"],
|
|
|
|
|
* "updateURL": "https://example.com/mods/mymod2"
|
|
|
|
|
* },
|
|
|
|
|
* ...etc, one json object per mod.
|
|
|
|
|
* ]
|
|
|
|
|
* }</pre>
|
|
|
|
|
* @param url The URL to check
|
|
|
|
|
* @return A list of mods that were both available on the URL and installed
|
|
|
|
|
*/
|
|
|
|
|
public static List<ModUpdateInfo> fetchUpdates(String url) {
|
|
|
|
|
if (!LibraryConfig.ENABLE_UPDATE_CHECKER) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-06-10 00:09:13 +02:00
|
|
|
URL URL;
|
|
|
|
|
try {
|
|
|
|
|
URL = new URL(url);
|
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
|
FalsePatternLib.getLog().error("Invalid URL: {}", url, e);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
switch (jsonLibraryLoaded.get()) {
|
|
|
|
|
case 0:
|
|
|
|
|
DependencyLoader.addMavenRepo("https://maven.falsepattern.com/");
|
|
|
|
|
try {
|
|
|
|
|
DependencyLoader.builder()
|
|
|
|
|
.loadingModId(Tags.MODID)
|
|
|
|
|
.groupId("com.falsepattern")
|
|
|
|
|
.artifactId("json")
|
|
|
|
|
.minVersion(new SemanticVersion(0, 4, 0))
|
|
|
|
|
.maxVersion(new SemanticVersion(0, Integer.MAX_VALUE, Integer.MAX_VALUE))
|
|
|
|
|
.preferredVersion(new SemanticVersion(0, 4, 1))
|
|
|
|
|
.build();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
FalsePatternLib.getLog().error("Failed to load json library for update checker!", e);
|
|
|
|
|
jsonLibraryLoaded.set(-1);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
jsonLibraryLoaded.set(1);
|
|
|
|
|
break;
|
|
|
|
|
case -1:
|
2022-06-09 20:14:11 +02:00
|
|
|
return null;
|
2022-06-10 00:09:13 +02:00
|
|
|
}
|
|
|
|
|
AtomicReference<String> loadedData = new AtomicReference<>(null);
|
2022-06-10 00:15:08 +02:00
|
|
|
Internet.connect(URL, (ex) -> FalsePatternLib.getLog().warn("Failed to check for updates from URL {}", url, ex), (input) -> {
|
2022-06-10 00:09:13 +02:00
|
|
|
val data = new ByteArrayOutputStream();
|
|
|
|
|
try {
|
|
|
|
|
Internet.transferAndClose(input, data);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
loadedData.set(data.toString());
|
|
|
|
|
});
|
|
|
|
|
if (loadedData.get() == null) return null;
|
|
|
|
|
val result = new ArrayList<ModUpdateInfo>();
|
|
|
|
|
val parsed = JsonNode.parse(loadedData.get());
|
|
|
|
|
List<JsonNode> modList;
|
|
|
|
|
if (parsed.isList()) {
|
|
|
|
|
modList = parsed.getJavaList();
|
|
|
|
|
} else {
|
|
|
|
|
modList = Collections.singletonList(parsed);
|
|
|
|
|
}
|
|
|
|
|
val installedMods = Loader.instance().getIndexedModList();
|
|
|
|
|
for (val node: modList) {
|
|
|
|
|
if (!node.isObject()) continue;
|
|
|
|
|
if (!node.containsKey("modid")) continue;
|
|
|
|
|
if (!node.containsKey("latestVersion")) continue;
|
|
|
|
|
val modid = node.getString("modid");
|
|
|
|
|
if (!installedMods.containsKey(modid)) continue;
|
|
|
|
|
val mod = installedMods.get(modid);
|
|
|
|
|
val latestVersionsNode = node.get("latestVersion");
|
|
|
|
|
List<String> latestVersions;
|
|
|
|
|
if (latestVersionsNode.isString()) {
|
|
|
|
|
latestVersions = Collections.singletonList(latestVersionsNode.stringValue());
|
|
|
|
|
} else if (latestVersionsNode.isList()) {
|
|
|
|
|
latestVersions = new ArrayList<>();
|
|
|
|
|
for (val version: latestVersionsNode.getJavaList()) {
|
|
|
|
|
if (!version.isString()) continue;
|
|
|
|
|
latestVersions.add(version.stringValue());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
2022-06-09 20:14:11 +02:00
|
|
|
}
|
2022-06-10 00:09:13 +02:00
|
|
|
val currentVersion = mod.getVersion();
|
|
|
|
|
if (latestVersions.contains(currentVersion)) continue;
|
|
|
|
|
val updateURL = node.containsKey("updateURL") && node.get("updateURL").isString() ? node.getString("updateURL") : "";
|
|
|
|
|
result.add(new ModUpdateInfo(modid, currentVersion, latestVersions.get(0), updateURL));
|
2022-06-09 20:14:11 +02:00
|
|
|
}
|
2022-06-10 00:09:13 +02:00
|
|
|
return result;
|
2022-06-09 20:14:11 +02:00
|
|
|
}
|
|
|
|
|
}
|