Skip to content

Commit a9b5781

Browse files
committed
added a basic update checker
1 parent 4894d23 commit a9b5781

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/main/java/lol/hyper/customlauncher/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import lol.hyper.customlauncher.accounts.JSONManager;
2121
import lol.hyper.customlauncher.accounts.windows.MainWindow;
2222
import lol.hyper.customlauncher.generic.ErrorWindow;
23+
import lol.hyper.customlauncher.generic.InfoWindow;
2324
import lol.hyper.customlauncher.setup.FirstSetup;
2425
import lol.hyper.customlauncher.ttrupdater.TTRUpdater;
26+
import lol.hyper.customlauncher.updater.UpdateChecker;
2527
import org.apache.logging.log4j.LogManager;
2628
import org.apache.logging.log4j.Logger;
2729
import org.json.JSONArray;
@@ -36,6 +38,7 @@ public class Main {
3638

3739
public static Logger logger;
3840
public static String pathToUse;
41+
public static final String VERSION = "1.2";
3942

4043
public static void main(String[] args) throws IOException {
4144
System.setProperty("log4j.configurationFile", "log4j2config.xml");
@@ -99,6 +102,11 @@ public static void main(String[] args) throws IOException {
99102
updater.dispose();
100103
}
101104
}
105+
if (UpdateChecker.checkForUpdates(VERSION)) {
106+
JFrame infoWindow = new InfoWindow("A new version is available.\nPlease download the latest version from the GitHub.");
107+
infoWindow.dispose();
108+
}
109+
102110
JFrame mainWindow = new MainWindow("CustomLauncherRewrite");
103111
mainWindow.dispose();
104112
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)