|
| 1 | +/* |
| 2 | + * This file is part of CustomLauncherRewrite. |
| 3 | + * |
| 4 | + * CustomLauncherRewrite is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * CustomLauncherRewrite is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with CustomLauncherRewrite. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +package lol.hyper.customlauncher.updater; |
| 19 | + |
| 20 | +import lol.hyper.customlauncher.invasiontracker.InvasionTracker; |
| 21 | +import org.apache.logging.log4j.LogManager; |
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | + |
| 24 | +import java.io.BufferedReader; |
| 25 | +import java.io.IOException; |
| 26 | +import java.io.InputStream; |
| 27 | +import java.io.InputStreamReader; |
| 28 | +import java.net.URL; |
| 29 | +import java.net.URLConnection; |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +public class UpdateChecker { |
| 34 | + |
| 35 | + /** |
| 36 | + * Check for updates to the program. |
| 37 | + * @return True if there is an update. |
| 38 | + */ |
| 39 | + public static boolean checkForUpdates(String currentVersion) throws IOException { |
| 40 | + Logger logger = LogManager.getLogger(UpdateChecker.class); |
| 41 | + String remoteVersion = null; |
| 42 | + URL url = new URL("https://raw.githubusercontent.com/hyperdefined/CustomLauncherRewrite/master/version"); |
| 43 | + URLConnection conn = url.openConnection(); |
| 44 | + conn.setRequestProperty( |
| 45 | + "User-Agent", "CustomLauncherRewrite https://github.com/hyperdefined/CustomLauncherRewrite"); |
| 46 | + conn.connect(); |
| 47 | + BufferedReader serverResponse = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 48 | + serverResponse.close(); |
| 49 | + |
| 50 | + try (InputStream in = url.openStream()) { |
| 51 | + BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)); |
| 52 | + remoteVersion = reader.lines().collect(Collectors.joining(System.lineSeparator())); |
| 53 | + reader.close(); |
| 54 | + } catch (IOException e) { |
| 55 | + logger.error(e); |
| 56 | + } |
| 57 | + |
| 58 | + logger.info("Current version: " + currentVersion); |
| 59 | + logger.info("Latest version: " + remoteVersion); |
| 60 | + |
| 61 | + if (remoteVersion == null || remoteVersion.equalsIgnoreCase("")) { |
| 62 | + logger.warn("Unable to find latest version from GitHub. The string either returned null or empty."); |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + return !remoteVersion.equals(currentVersion); |
| 67 | + } |
| 68 | +} |
0 commit comments