|
| 1 | +package dev.xpple.seedmapper.util; |
| 2 | + |
| 3 | +import com.google.gson.JsonArray; |
| 4 | +import com.google.gson.JsonElement; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import com.google.gson.JsonParser; |
| 7 | +import com.mojang.logging.LogUtils; |
| 8 | +import net.minecraft.ChatFormatting; |
| 9 | +import net.minecraft.client.Minecraft; |
| 10 | +import net.minecraft.network.chat.Component; |
| 11 | +import net.minecraft.network.chat.MutableComponent; |
| 12 | +import org.slf4j.Logger; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.net.URI; |
| 16 | +import java.net.http.HttpClient; |
| 17 | +import java.net.http.HttpRequest; |
| 18 | +import java.net.http.HttpResponse; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | + |
| 24 | +import static dev.xpple.seedmapper.util.ChatBuilder.accent; |
| 25 | + |
| 26 | +public final class ModrinthUpdateChecker { |
| 27 | + private static final Logger LOGGER = LogUtils.getLogger(); |
| 28 | + private static final String MODRINTH_PROJECT_ID = "2qXosh15"; |
| 29 | + private static final HttpClient HTTP = HttpClient.newBuilder() |
| 30 | + .connectTimeout(Duration.ofSeconds(5)) |
| 31 | + .build(); |
| 32 | + |
| 33 | + private static volatile boolean hasCheckedThisSession = false; |
| 34 | + |
| 35 | + private ModrinthUpdateChecker() { |
| 36 | + } |
| 37 | + |
| 38 | + public static void checkAndNotify() { |
| 39 | + if (hasCheckedThisSession) { |
| 40 | + return; |
| 41 | + } |
| 42 | + hasCheckedThisSession = true; |
| 43 | + |
| 44 | + String localVersion = normalizeVersion(BuildInfo.FORK_RELEASE_VERSION); |
| 45 | + if (localVersion.isEmpty()) { |
| 46 | + LOGGER.warn("Skipping update check because fork release version is missing in build_info.json"); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + CompletableFuture |
| 51 | + .supplyAsync(() -> fetchLatestForkVersion().orElse(null)) |
| 52 | + .thenAccept(remoteVersion -> { |
| 53 | + Minecraft minecraft = Minecraft.getInstance(); |
| 54 | + minecraft.execute(() -> { |
| 55 | + if (minecraft.player == null || remoteVersion == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + int cmp = compareVersions(localVersion, remoteVersion); |
| 59 | + if (cmp < 0) { |
| 60 | + minecraft.player.sendSystemMessage(Component.empty() |
| 61 | + .append(Component.literal("[SeedMapper] ").withStyle(ChatFormatting.GOLD)) |
| 62 | + .append(Component.literal("Update available: ").withStyle(ChatFormatting.YELLOW)) |
| 63 | + .append(Component.literal("you are on ")) |
| 64 | + .append(accent(localVersion)) |
| 65 | + .append(Component.literal(", latest is ")) |
| 66 | + .append(accent(remoteVersion)) |
| 67 | + .append(Component.literal("."))); |
| 68 | + return; |
| 69 | + } |
| 70 | + minecraft.player.sendSystemMessage(Component.empty() |
| 71 | + .append(Component.literal("[SeedMapper] ").withStyle(ChatFormatting.DARK_GREEN)) |
| 72 | + .append(Component.literal("You are up to date (")) |
| 73 | + .append(accent(localVersion)) |
| 74 | + .append(Component.literal(")."))); |
| 75 | + }); |
| 76 | + }) |
| 77 | + .exceptionally(ex -> { |
| 78 | + LOGGER.debug("Update check failed", ex); |
| 79 | + return null; |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private static java.util.Optional<String> fetchLatestForkVersion() { |
| 84 | + try { |
| 85 | + URI uri = URI.create("https://api.modrinth.com/v2/project/" + MODRINTH_PROJECT_ID + "/version?featured=true&include_changelog=false"); |
| 86 | + HttpRequest request = HttpRequest.newBuilder(uri) |
| 87 | + .GET() |
| 88 | + .timeout(Duration.ofSeconds(8)) |
| 89 | + .header("User-Agent", "SeedMapper/" + BuildInfo.VERSION + " (update-check)") |
| 90 | + .build(); |
| 91 | + HttpResponse<String> response = HTTP.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); |
| 92 | + if (response.statusCode() != 200) { |
| 93 | + LOGGER.debug("Update check returned HTTP {}", response.statusCode()); |
| 94 | + return java.util.Optional.empty(); |
| 95 | + } |
| 96 | + return parseLatestVersion(response.body()); |
| 97 | + } catch (IOException | InterruptedException e) { |
| 98 | + if (e instanceof InterruptedException) { |
| 99 | + Thread.currentThread().interrupt(); |
| 100 | + } |
| 101 | + LOGGER.debug("Update check request failed", e); |
| 102 | + return java.util.Optional.empty(); |
| 103 | + } catch (RuntimeException e) { |
| 104 | + LOGGER.debug("Update check parse failed", e); |
| 105 | + return java.util.Optional.empty(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static java.util.Optional<String> parseLatestVersion(String body) { |
| 110 | + JsonElement parsed = JsonParser.parseString(body); |
| 111 | + if (!parsed.isJsonArray()) { |
| 112 | + return java.util.Optional.empty(); |
| 113 | + } |
| 114 | + JsonArray versions = parsed.getAsJsonArray(); |
| 115 | + String latestVersion = null; |
| 116 | + String latestDate = ""; |
| 117 | + for (JsonElement element : versions) { |
| 118 | + if (!element.isJsonObject()) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + JsonObject obj = element.getAsJsonObject(); |
| 122 | + if (!obj.has("version_number") || !obj.has("date_published")) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + String version = normalizeVersion(obj.get("version_number").getAsString()); |
| 126 | + String date = obj.get("date_published").getAsString(); |
| 127 | + if (version.isEmpty()) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + if (latestVersion == null || date.compareTo(latestDate) > 0) { |
| 131 | + latestVersion = version; |
| 132 | + latestDate = date; |
| 133 | + } |
| 134 | + } |
| 135 | + return java.util.Optional.ofNullable(latestVersion); |
| 136 | + } |
| 137 | + |
| 138 | + private static String normalizeVersion(String version) { |
| 139 | + if (version == null) { |
| 140 | + return ""; |
| 141 | + } |
| 142 | + String normalized = version.trim(); |
| 143 | + if (normalized.startsWith("v") || normalized.startsWith("V")) { |
| 144 | + normalized = normalized.substring(1); |
| 145 | + } |
| 146 | + return normalized; |
| 147 | + } |
| 148 | + |
| 149 | + private static int compareVersions(String local, String remote) { |
| 150 | + String[] localParts = local.split("[^A-Za-z0-9]+"); |
| 151 | + String[] remoteParts = remote.split("[^A-Za-z0-9]+"); |
| 152 | + int len = Math.max(localParts.length, remoteParts.length); |
| 153 | + for (int i = 0; i < len; i++) { |
| 154 | + String a = i < localParts.length ? localParts[i] : "0"; |
| 155 | + String b = i < remoteParts.length ? remoteParts[i] : "0"; |
| 156 | + int cmp; |
| 157 | + if (isDigits(a) && isDigits(b)) { |
| 158 | + cmp = Integer.compare(Integer.parseInt(a), Integer.parseInt(b)); |
| 159 | + } else { |
| 160 | + cmp = a.toLowerCase(Locale.ROOT).compareTo(b.toLowerCase(Locale.ROOT)); |
| 161 | + } |
| 162 | + if (cmp != 0) { |
| 163 | + return cmp; |
| 164 | + } |
| 165 | + } |
| 166 | + return 0; |
| 167 | + } |
| 168 | + |
| 169 | + private static boolean isDigits(String value) { |
| 170 | + if (value == null || value.isEmpty()) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + for (int i = 0; i < value.length(); i++) { |
| 174 | + if (!Character.isDigit(value.charAt(i))) { |
| 175 | + return false; |
| 176 | + } |
| 177 | + } |
| 178 | + return true; |
| 179 | + } |
| 180 | +} |
0 commit comments