|
| 1 | +package com.falsepattern.lib.updates; |
| 2 | + |
| 3 | +import com.falsepattern.lib.dependencies.DependencyLoader; |
| 4 | +import com.falsepattern.lib.dependencies.SemanticVersion; |
| 5 | +import com.falsepattern.lib.internal.FalsePatternLib; |
| 6 | +import com.falsepattern.lib.internal.LibraryConfig; |
| 7 | +import com.falsepattern.lib.internal.Tags; |
| 8 | +import lombok.val; |
| 9 | +import lombok.var; |
| 10 | + |
| 11 | +import java.io.BufferedOutputStream; |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.net.URL; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.ExecutorService; |
| 19 | +import java.util.concurrent.Executors; |
| 20 | +import java.util.concurrent.Future; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | + |
| 23 | +public class UpdateChecker { |
| 24 | + private static AtomicBoolean jsonLibraryLoaded = new AtomicBoolean(false); |
| 25 | + private static final ExecutorService asyncExecutor = Executors.newSingleThreadExecutor((runnable) -> { |
| 26 | + Thread thread = new Thread(runnable); |
| 27 | + thread.setDaemon(true); |
| 28 | + thread.setName(Tags.MODNAME + " Asynchronous Update Check Thread"); |
| 29 | + return thread; |
| 30 | + }); |
| 31 | + /** |
| 32 | + * Same this as {@link #fetchUpdates(String)}, but defers the check to a different thread. Useful for asynchronous |
| 33 | + * update checks, if you don't want to block loading. |
| 34 | + * @param url The URL to check |
| 35 | + * @return A future that will contain the update info about mods that were both available on the URL and installed |
| 36 | + */ |
| 37 | + public static Future<List<ModUpdateInfo>> fetchUpdatesAsync(String url) { |
| 38 | + return asyncExecutor.submit(() -> fetchUpdates(url)); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Checks for updates. The URL should be a JSON file that contains a list of mods, each with a mod ID, one or more |
| 43 | + * versions, and a URL for the user to check for updates in case the current and latest versions are different. |
| 44 | + * The JSON file must have the following format: |
| 45 | + * <pre>{@code |
| 46 | + * [ |
| 47 | + * { |
| 48 | + * "modID": "modid", |
| 49 | + * "latestVersion": ["1.0.0", "1.0.0-foo"], |
| 50 | + * "updateURL": "https://example.com/mods/mymod" |
| 51 | + * }, |
| 52 | + * { |
| 53 | + * "modID": "modid2", |
| 54 | + * "latestVersion": ["0.2.0", "0.3.0-alpha"], |
| 55 | + * "updateURL": "https://example.com/mods/mymod2" |
| 56 | + * }, |
| 57 | + * ...etc, one json object per mod. |
| 58 | + * ] |
| 59 | + * }</pre> |
| 60 | + * @param url The URL to check |
| 61 | + * @return A list of mods that were both available on the URL and installed |
| 62 | + */ |
| 63 | + public static List<ModUpdateInfo> fetchUpdates(String url) { |
| 64 | + if (!LibraryConfig.ENABLE_UPDATE_CHECKER) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + if (!jsonLibraryLoaded.get()) { |
| 68 | + DependencyLoader.addMavenRepo("https://maven.falsepattern.com/"); |
| 69 | + try { |
| 70 | + DependencyLoader.builder() |
| 71 | + .groupId("com.falsepattern") |
| 72 | + .artifactId("json") |
| 73 | + .minVersion(new SemanticVersion(0, 4, 0)) |
| 74 | + .maxVersion(new SemanticVersion(0, Integer.MAX_VALUE, Integer.MAX_VALUE)) |
| 75 | + .build(); |
| 76 | + } catch (Exception e) { |
| 77 | + FalsePatternLib.getLog().error("Failed to load json library for update checker!", e); |
| 78 | + return null; |
| 79 | + } |
| 80 | + jsonLibraryLoaded.set(true); |
| 81 | + } |
| 82 | + //TODO |
| 83 | + return null; |
| 84 | + } |
| 85 | +} |
0 commit comments