|
| 1 | +package com.falsepattern.lib.internal.impl.updates; |
| 2 | + |
| 3 | +import com.falsepattern.json.node.JsonNode; |
| 4 | +import com.falsepattern.lib.dependencies.DependencyLoader; |
| 5 | +import com.falsepattern.lib.dependencies.Library; |
| 6 | +import com.falsepattern.lib.dependencies.SemanticVersion; |
| 7 | +import com.falsepattern.lib.internal.Internet; |
| 8 | +import com.falsepattern.lib.internal.Tags; |
| 9 | +import com.falsepattern.lib.internal.config.LibraryConfig; |
| 10 | +import com.falsepattern.lib.text.FormattedText; |
| 11 | +import com.falsepattern.lib.updates.ModUpdateInfo; |
| 12 | +import com.falsepattern.lib.updates.UpdateCheckException; |
| 13 | +import lombok.val; |
| 14 | + |
| 15 | +import net.minecraft.client.resources.I18n; |
| 16 | +import net.minecraft.event.ClickEvent; |
| 17 | +import net.minecraft.util.IChatComponent; |
| 18 | + |
| 19 | +import cpw.mods.fml.common.Loader; |
| 20 | + |
| 21 | +import java.net.MalformedURLException; |
| 22 | +import java.net.URL; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.CompletionException; |
| 28 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 29 | + |
| 30 | +public final class UpdateCheckerImpl { |
| 31 | + private static final AtomicBoolean jsonLibraryLoaded = new AtomicBoolean(false); |
| 32 | + |
| 33 | + public static List<IChatComponent> updateListToChatMessages(String initiator, List<ModUpdateInfo> updates) { |
| 34 | + if (updates == null || updates.size() == 0) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + val updateText = new ArrayList<IChatComponent>( |
| 38 | + FormattedText.parse(I18n.format("falsepatternlib.chat.updatesavailable", initiator)).toChatText()); |
| 39 | + val mods = Loader.instance().getIndexedModList(); |
| 40 | + for (val update : updates) { |
| 41 | + val mod = mods.get(update.modID); |
| 42 | + updateText.addAll( |
| 43 | + FormattedText.parse(I18n.format("falsepatternlib.chat.modname", mod.getName())).toChatText()); |
| 44 | + updateText.addAll( |
| 45 | + FormattedText.parse(I18n.format("falsepatternlib.chat.currentversion", update.currentVersion)) |
| 46 | + .toChatText()); |
| 47 | + updateText.addAll( |
| 48 | + FormattedText.parse(I18n.format("falsepatternlib.chat.latestversion", update.latestVersion)) |
| 49 | + .toChatText()); |
| 50 | + if (!update.updateURL.isEmpty()) { |
| 51 | + val pre = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpre")).toChatText(); |
| 52 | + val link = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurl")).toChatText(); |
| 53 | + val post = FormattedText.parse(I18n.format("falsepatternlib.chat.updateurlpost")).toChatText(); |
| 54 | + pre.get(pre.size() - 1).appendSibling(link.get(0)); |
| 55 | + link.get(link.size() - 1).appendSibling(post.get(0)); |
| 56 | + for (val l : link) { |
| 57 | + l.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, update.updateURL)); |
| 58 | + } |
| 59 | + link.remove(0); |
| 60 | + post.remove(0); |
| 61 | + updateText.addAll(pre); |
| 62 | + updateText.addAll(link); |
| 63 | + updateText.addAll(post); |
| 64 | + } |
| 65 | + } |
| 66 | + return updateText; |
| 67 | + } |
| 68 | + |
| 69 | + public static CompletableFuture<List<ModUpdateInfo>> fetchUpdatesAsync(String url) { |
| 70 | + return CompletableFuture.supplyAsync(() -> { |
| 71 | + if (!LibraryConfig.ENABLE_UPDATE_CHECKER) { |
| 72 | + throw new CompletionException(new UpdateCheckException("Update checker is disabled in config!")); |
| 73 | + } |
| 74 | + URL URL; |
| 75 | + try { |
| 76 | + URL = new URL(url); |
| 77 | + } catch (MalformedURLException e) { |
| 78 | + throw new CompletionException(new UpdateCheckException("Invalid URL: " + url, e)); |
| 79 | + } |
| 80 | + if (!jsonLibraryLoaded.get()) { |
| 81 | + try { |
| 82 | + DependencyLoader.addMavenRepo("https://maven.falsepattern.com/"); |
| 83 | + DependencyLoader.loadLibraries(Library.builder() |
| 84 | + .loadingModId(Tags.MODID) |
| 85 | + .groupId("com.falsepattern") |
| 86 | + .artifactId("json") |
| 87 | + .minVersion(new SemanticVersion(0, 4, 0)) |
| 88 | + .maxVersion(new SemanticVersion(0, Integer.MAX_VALUE, Integer.MAX_VALUE)) |
| 89 | + .preferredVersion(new SemanticVersion(0, 4, 1)) |
| 90 | + .build()); |
| 91 | + } catch (Exception e) { |
| 92 | + throw new CompletionException( |
| 93 | + new UpdateCheckException("Failed to load json library for update checker!", e)); |
| 94 | + } |
| 95 | + jsonLibraryLoaded.set(true); |
| 96 | + } |
| 97 | + val result = new ArrayList<ModUpdateInfo>(); |
| 98 | + JsonNode parsed; |
| 99 | + try { |
| 100 | + parsed = JsonNode.parse(Internet.download(URL).thenApply(String::new).join()); |
| 101 | + } catch (CompletionException e) { |
| 102 | + throw new CompletionException(new UpdateCheckException("Failed to download update checker JSON file!", |
| 103 | + e.getCause() == null ? e : e.getCause())); |
| 104 | + } |
| 105 | + List<JsonNode> modList; |
| 106 | + if (parsed.isList()) { |
| 107 | + modList = parsed.getJavaList(); |
| 108 | + } else { |
| 109 | + modList = Collections.singletonList(parsed); |
| 110 | + } |
| 111 | + val installedMods = Loader.instance().getIndexedModList(); |
| 112 | + for (val node : modList) { |
| 113 | + if (!node.isObject()) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + if (!node.containsKey("modid")) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + if (!node.containsKey("latestVersion")) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + val modid = node.getString("modid"); |
| 123 | + if (!installedMods.containsKey(modid)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + val mod = installedMods.get(modid); |
| 127 | + val latestVersionsNode = node.get("latestVersion"); |
| 128 | + List<String> latestVersions; |
| 129 | + if (latestVersionsNode.isString()) { |
| 130 | + latestVersions = Collections.singletonList(latestVersionsNode.stringValue()); |
| 131 | + } else if (latestVersionsNode.isList()) { |
| 132 | + latestVersions = new ArrayList<>(); |
| 133 | + for (val version : latestVersionsNode.getJavaList()) { |
| 134 | + if (!version.isString()) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + latestVersions.add(version.stringValue()); |
| 138 | + } |
| 139 | + } else { |
| 140 | + continue; |
| 141 | + } |
| 142 | + val currentVersion = mod.getVersion(); |
| 143 | + if (latestVersions.contains(currentVersion)) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + val updateURL = |
| 147 | + node.containsKey("updateURL") && node.get("updateURL").isString() ? node.getString("updateURL") |
| 148 | + : ""; |
| 149 | + result.add(new ModUpdateInfo(modid, currentVersion, latestVersions.get(0), updateURL)); |
| 150 | + } |
| 151 | + return result; |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + public static List<ModUpdateInfo> fetchUpdates(String url) throws UpdateCheckException { |
| 156 | + try { |
| 157 | + return fetchUpdatesAsync(url).join(); |
| 158 | + } catch (CompletionException e) { |
| 159 | + try { |
| 160 | + throw e.getCause(); |
| 161 | + } catch (UpdateCheckException e1) { |
| 162 | + throw e1; |
| 163 | + } catch (Throwable e1) { |
| 164 | + throw new UpdateCheckException("Failed to check for updates!", e1); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments