Skip to content

Commit 2dae27c

Browse files
RizlimDevRizlimDev
authored andcommitted
v1.5
Change how the launcher behaves making it more user friendly, we now can connect to GitHub using API Tokens and when you have no API requests left a little menu pops up.
1 parent 21f4aeb commit 2dae27c

File tree

10 files changed

+749
-40
lines changed

10 files changed

+749
-40
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package API;
2+
3+
import GUI.Data;
4+
import org.kohsuke.github.GitHub;
5+
6+
import java.io.IOException;
7+
8+
/**
9+
*
10+
*/
11+
public class GithubHandler
12+
{
13+
public static boolean canQuerry(GitHub gitHub)
14+
{
15+
try
16+
{
17+
return gitHub.getRateLimit().remaining > 2;
18+
}
19+
catch (IOException e)
20+
{
21+
e.printStackTrace();
22+
}
23+
return false;
24+
}
25+
26+
public static GitHub connect(String token)
27+
{
28+
if (token == null)
29+
{
30+
try
31+
{
32+
Data.setStatus("Connecting to GitHub anonymously");
33+
return GitHub.connectAnonymously();
34+
}
35+
catch (IOException e)
36+
{
37+
e.printStackTrace();
38+
}
39+
}
40+
if (token.equals(""))
41+
{
42+
try
43+
{
44+
Data.setStatus("Connecting to GitHub anonymously");
45+
return GitHub.connectAnonymously();
46+
}
47+
catch (IOException e)
48+
{
49+
e.printStackTrace();
50+
}
51+
}
52+
else
53+
{
54+
try
55+
{
56+
Data.setStatus("Connecting to GitHub using your token");
57+
return GitHub.connectUsingOAuth(token);
58+
}
59+
catch (IOException e)
60+
{
61+
e.printStackTrace();
62+
}
63+
}
64+
return null;
65+
}
66+
}

src/main/java/GUI/Data.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.Properties;
66

77
/**
8-
* Data Class.
8+
* PAL Class.
99
* *gasp* That's not proper coding!
1010
* Yes, but it is so much easier than doing it proper :).
1111
*/

src/main/java/GUI/LauncherUI_Controller.java

Lines changed: 184 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package GUI;
22

3+
import API.GithubHandler;
4+
import GUI.PopUp.Popup;
35
import Helper.QuickSorter;
46
import Helper.Release;
57
import Helper.UpdateCheckerClient;
8+
import PAL.*;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
610
import javafx.application.Platform;
711
import javafx.fxml.FXML;
812
import javafx.fxml.Initializable;
913
import javafx.scene.text.Text;
1014
import javafx.stage.Stage;
15+
import org.kohsuke.github.GHRepository;
16+
import org.kohsuke.github.GitHub;
1117

1218
import java.io.File;
1319
import java.io.IOException;
@@ -39,25 +45,6 @@ public void initialize(URL location, ResourceBundle resources)
3945
daemon_launch();
4046
}
4147

42-
private void runMostRecentVersion()
43-
{
44-
// Find out what the most recent version is.
45-
cleanUp();
46-
47-
Runtime runtime = Runtime.getRuntime();
48-
try
49-
{
50-
runtime.exec("java -jar " + mr_jar.getPath());
51-
} catch (IOException e)
52-
{
53-
e.printStackTrace();
54-
}
55-
56-
Stage s = LauncherUI.stage;
57-
Platform.runLater(() -> s.close());
58-
59-
}
60-
6148
private boolean isNewer(String _old, String _new)
6249
{
6350
_old = _old.replace("b", "");
@@ -145,6 +132,119 @@ private void daemon_launch()
145132
t_launch.start();
146133
}
147134

135+
private String github_token = null;
136+
137+
public boolean readCoreSettings()
138+
{
139+
File core_settings_pal = new File(Data2.getINSTANCE().getLocal() + File.separator + "PAL" + File.separator + "core_settings.pal");
140+
141+
PALsettings palsettings = new PALsettings(false, "", true, "", true, false, "", "", false, false);
142+
143+
if (core_settings_pal.exists())
144+
{
145+
ObjectMapper objectMapper = new ObjectMapper();
146+
try
147+
{
148+
palsettings = objectMapper.readValue(core_settings_pal, PALsettings.class);
149+
github_token = palsettings.getGithub_token();
150+
return true;
151+
}
152+
catch (IOException e)
153+
{
154+
e.printStackTrace();
155+
}
156+
}
157+
return false;
158+
}
159+
160+
private void noAPIrequests()
161+
{
162+
File f = new File(Data2.getINSTANCE().getCoreFolder());
163+
if (f.exists())
164+
{
165+
if (f.isDirectory())
166+
{
167+
Popup popup = new Popup();
168+
for (File f_ : f.listFiles())
169+
{
170+
if (f_.getName().matches("b(\\d){0,9999999}(.jar)"))
171+
{
172+
Popup.files.add(f_);
173+
}
174+
}
175+
176+
Platform.runLater(() ->
177+
{
178+
try
179+
{
180+
popup.start(new Stage());
181+
}
182+
catch (Exception e)
183+
{
184+
e.printStackTrace();
185+
}
186+
});
187+
final String wait = "Waiting for user to select an option...";
188+
while (true)
189+
{
190+
if (!Data.getStatus().equals(wait))
191+
Data.setStatus(wait);
192+
193+
try
194+
{
195+
Thread.sleep(300L);
196+
}
197+
catch (InterruptedException e)
198+
{
199+
e.printStackTrace();
200+
}
201+
if (Popup.jar != null)
202+
{
203+
Data.setStatus("Attempting to launch: " + Popup.jar.getName());
204+
Platform.runLater(() -> Popup.stage.close());
205+
runSpecificVersion(Popup.jar);
206+
break;
207+
}
208+
209+
if (!Popup.url.equals("NOTSET"))
210+
{
211+
Platform.runLater(() -> Popup.stage.close());
212+
Data.setStatus("Attempting to download from: " + Popup.url);
213+
// Download and run.
214+
String jar = UpdateCheckerClient.getINSTANCE().downloadUpdate(Popup.url);
215+
if (jar.equals("NO_DOWNLOAD"))
216+
{
217+
Platform.runLater(() ->
218+
{
219+
try
220+
{
221+
popup.start(new Stage());
222+
}
223+
catch (Exception e)
224+
{
225+
e.printStackTrace();
226+
}
227+
});
228+
}
229+
else
230+
{
231+
runSpecificVersion(new File(jar));
232+
break;
233+
}
234+
}
235+
}
236+
}
237+
else
238+
{
239+
System.out.println("Core folder is not a directory?");
240+
}
241+
}
242+
else
243+
{
244+
System.out.println("Core folder doesn't exist!");
245+
}
246+
}
247+
148248
/**
149249
* Core checking thread.
150250
*/
@@ -153,39 +253,70 @@ private void daemon_checker()
153253
Runnable r = () ->
154254
{
155255
Data.setProgress(0.0);
156-
//Data.getINSTANCE().createProperties();
157256

158-
// Create Releases Array
159-
Data.setStatus("Accessing github for releases");
160-
ArrayList<Release> releases = UpdateCheckerClient.getINSTANCE().getReleases();
161-
if (releases == null)
257+
// Check if core_settings.pal exists and get Github API Token.
258+
Data.setStatus("Attempting to read core_settings.pal");
259+
readCoreSettings();
260+
261+
boolean canConnect;
262+
GitHub gitHub = GithubHandler.connect(github_token);
263+
assert gitHub != null;
264+
canConnect = GithubHandler.canQuerry(gitHub);
265+
266+
if (canConnect)
162267
{
163-
File f = new File(Data2.getINSTANCE().getCoreFolder() + File.separator + "b10.jar");
164-
if (f.exists())
268+
try
165269
{
166-
Data.setStatus("Can't connect to the GitHub API using installed version.");
167-
runMostRecentVersion();
168-
return;
270+
GHRepository repository = gitHub.getRepository("POE-Addon-Launcher/Core");
271+
272+
String current_most_recent = repository.getLatestRelease().getTagName() + ".jar";
273+
274+
File f = new File(Data2.getINSTANCE().getCoreFolder());
275+
276+
for (File f_ : f.listFiles())
277+
{
278+
if (f_.getName().equals(current_most_recent))
279+
{
280+
Data.setStatus("You have " + current_most_recent + " which is the most recent build!");
281+
runSpecificVersion(f_);
282+
return;
283+
}
284+
}
285+
Data.setStatus("You are out of date! Downloading new version...");
286+
String jar = UpdateCheckerClient.getINSTANCE().downloadUpdate(repository.getLatestRelease().getAssets().get(0).getBrowserDownloadUrl());
287+
runSpecificVersion(new File(jar));
169288
}
170-
else
289+
catch (IOException e)
171290
{
172-
Release release = new Release();
173-
release.setNum(10);
174-
release.setName("b10.jar");
175-
release.setDownload_url("https://github.com/POE-Addon-Launcher/Core/releases/download/b10/b10.jar");
176-
release.setVersion("b10");
177-
UpdateCheckerClient.getINSTANCE().downloadUpdate(release);
291+
e.printStackTrace();
178292
}
179293
}
180294
else
295+
{
296+
Data.setStatus("You have no GitHub API requests left; Get yourself a GitHub API Token!");
297+
noAPIrequests();
298+
}
299+
300+
301+
//PAL.getINSTANCE().createProperties();
302+
/*
303+
// Create Releases Array
304+
Data.setStatus("Accessing github for releases");
305+
ArrayList<Release> releases = UpdateCheckerClient.getINSTANCE().getReleases();
306+
if (releases == null)
307+
{
308+
// Check dir for files.
309+
310+
}
311+
else
181312
{
182313
Data.setStatus("Got all " + releases.size() + " releases!");
183314
184315
Data.setStatus("Checking if we're up to date");
185316
// Launch Checker
186317
UpdateCheckerClient.getINSTANCE().downloadUpdate(getMostRecentRelease(releases));
187318
}
188-
319+
*/
189320
Data.setStatus("Up to date, launching program!");
190321
Data.setProgress(1.0);
191322
};
@@ -194,6 +325,22 @@ private void daemon_checker()
194325
t.start();
195326
}
196327

328+
private void runSpecificVersion(File file)
329+
{
330+
Runtime runtime = Runtime.getRuntime();
331+
try
332+
{
333+
runtime.exec("java -jar " + file.getPath());
334+
} catch (IOException e)
335+
{
336+
e.printStackTrace();
337+
}
338+
339+
Stage s = LauncherUI.stage;
340+
Platform.runLater(() -> s.close());
341+
342+
}
343+
197344
/**
198345
* Retrieves the most recent release from an ArrayList.
199346
* (*Probably shouldn't be in this class)

0 commit comments

Comments
 (0)