|
| 1 | +/* |
| 2 | + * Copyright (c) 2019-2024 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/Floodgate |
| 24 | + */ |
| 25 | + |
| 26 | +package com.minekube.connect.util; |
| 27 | + |
| 28 | +import com.google.gson.JsonObject; |
| 29 | +import com.google.inject.Inject; |
| 30 | +import com.minekube.connect.api.logger.ConnectLogger; |
| 31 | + |
| 32 | +public class UpdateChecker { |
| 33 | + private static final String GITHUB_API_URL = |
| 34 | + "https://api.github.com/repos/minekube/connect-java/releases/latest"; |
| 35 | + |
| 36 | + private final ConnectLogger logger; |
| 37 | + |
| 38 | + @Inject |
| 39 | + public UpdateChecker(ConnectLogger logger) { |
| 40 | + this.logger = logger; |
| 41 | + } |
| 42 | + |
| 43 | + public void checkForUpdates() { |
| 44 | + HttpUtils.asyncGet(GITHUB_API_URL).thenAccept(response -> { |
| 45 | + if (!response.isCodeOk()) { |
| 46 | + logger.debug("Failed to check for updates: HTTP " + response.getHttpCode()); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + JsonObject json = response.getResponse(); |
| 51 | + if (json == null || !json.has("tag_name")) { |
| 52 | + logger.debug("Failed to parse update response"); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + String latestVersion = json.get("tag_name").getAsString(); |
| 57 | + String currentVersion = Constants.VERSION; |
| 58 | + |
| 59 | + // Remove 'v' prefix if present |
| 60 | + if (latestVersion.startsWith("v")) { |
| 61 | + latestVersion = latestVersion.substring(1); |
| 62 | + } |
| 63 | + |
| 64 | + // Skip check for development versions |
| 65 | + if (currentVersion.contains("SNAPSHOT") || currentVersion.contains("${")) { |
| 66 | + logger.debug("Running development version, skipping update check"); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (isNewerVersion(latestVersion, currentVersion)) { |
| 71 | + logger.info("A new version of Connect is available: " + latestVersion |
| 72 | + + " (current: " + currentVersion + ")"); |
| 73 | + logger.info("Download: https://github.com/minekube/connect-java/releases/latest"); |
| 74 | + } else { |
| 75 | + logger.debug("Connect is up to date (version " + currentVersion + ")"); |
| 76 | + } |
| 77 | + }).exceptionally(throwable -> { |
| 78 | + logger.debug("Failed to check for updates: " + throwable.getMessage()); |
| 79 | + return null; |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Compares two semantic version strings. |
| 85 | + * @return true if latestVersion is newer than currentVersion |
| 86 | + */ |
| 87 | + private boolean isNewerVersion(String latestVersion, String currentVersion) { |
| 88 | + try { |
| 89 | + int[] latest = parseVersion(latestVersion); |
| 90 | + int[] current = parseVersion(currentVersion); |
| 91 | + |
| 92 | + for (int i = 0; i < Math.max(latest.length, current.length); i++) { |
| 93 | + int l = i < latest.length ? latest[i] : 0; |
| 94 | + int c = i < current.length ? current[i] : 0; |
| 95 | + if (l > c) return true; |
| 96 | + if (l < c) return false; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } catch (Exception e) { |
| 100 | + // If parsing fails, do string comparison |
| 101 | + return !latestVersion.equals(currentVersion); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private int[] parseVersion(String version) { |
| 106 | + // Remove any suffix like -SNAPSHOT, -beta, etc. |
| 107 | + String cleanVersion = version.split("-")[0]; |
| 108 | + String[] parts = cleanVersion.split("\\."); |
| 109 | + int[] result = new int[parts.length]; |
| 110 | + for (int i = 0; i < parts.length; i++) { |
| 111 | + result[i] = Integer.parseInt(parts[i]); |
| 112 | + } |
| 113 | + return result; |
| 114 | + } |
| 115 | +} |
0 commit comments