-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid the github API for better accessibility.
This also allowed me to get rid of gson.
- Loading branch information
1 parent
65055a0
commit 8c20b57
Showing
9 changed files
with
290 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
package io.cubyz; | ||
|
||
import java.net.URL; | ||
|
||
import javax.swing.JOptionPane; | ||
|
||
import io.cubyz.github.GitHubConnection; | ||
import io.cubyz.github.GithubRelease; | ||
import io.cubyz.ui.MainGUI; | ||
|
||
public class Main { | ||
public static final String versionString = "0.2.1"; | ||
public static final int RELEASE_NUMBER = 1; | ||
public static void main(String[] args) { | ||
// Check if a new version is available: | ||
try { | ||
GithubRelease newest = GitHubConnection.downloadReleaseData("https://api.github.com/repos/PixelGuys/Cubyz-Launcher/releases?per_page=1")[0]; | ||
if(!newest.version.equals(versionString)) { | ||
// Prompt the user to update: | ||
JOptionPane.showInternalMessageDialog(null, "A new update of launcher is available.", "information", JOptionPane.INFORMATION_MESSAGE); | ||
} | ||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
new MainGUI(GitHubConnection.downloadReleaseData("https://api.github.com/repos/PixelGuys/Cubyz/releases?per_page=100")); | ||
String link = "https://github.com/PixelGuys/Cubyz-Launcher/releases/download/R"+(RELEASE_NUMBER + 1)+"/cubyz-launcher.jar"; | ||
URL url = new URL(link); | ||
url.openStream().close(); | ||
// If no exception was thrown, there is a new launcher version. | ||
// Prompt the user to update: | ||
JOptionPane.showInternalMessageDialog(null, "A new update of launcher is available.", "information", JOptionPane.INFORMATION_MESSAGE); | ||
} catch(Exception e) {} | ||
GitHubConnection.downloadReleaseData(); | ||
new MainGUI(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,148 @@ | ||
package io.cubyz.github; | ||
import java.io.ByteArrayOutputStream; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.lang.ProcessBuilder.Redirect; | ||
import java.net.URL; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.ReadableByteChannel; | ||
import java.util.ArrayList; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.swing.JOptionPane; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import io.cubyz.OSInfo; | ||
import io.cubyz.dependency.DependencyManager; | ||
import io.cubyz.util.DownloadAndFileManager; | ||
|
||
/** | ||
* Connect to github and get an array of all existing releases. | ||
*/ | ||
public class GitHubConnection { | ||
/** | ||
* Function to remove comments(as [[comment]]) from the description and to ensure the result isn't null. | ||
* Downloads the general information about every available release. | ||
* Creates new folders for the results. | ||
*/ | ||
private static String getDescription(JsonElement e) { | ||
if(e.isJsonNull()) return ""; | ||
return e.getAsString().replaceAll("\\[\\[(.|\\R)*\\]\\]", "").replace("\n", "<br>"); | ||
public static void downloadReleaseData() { | ||
try { | ||
for(int i = 1; ; i++) { | ||
// If the folder already exists, there is no need to try and download the data again: | ||
File file = new File(System.getProperty("user.home") + "/.cubyz/" + "R" + i + "/release_info"); | ||
if(file.exists()) continue; | ||
|
||
// Tests if the version `i` is available if so reads the file, otherwise throws an Exception. | ||
String link = "https://github.com/PixelGuys/Cubyz/releases/download/R"+i+"/release_info"; | ||
URL url = new URL(link); | ||
ReadableByteChannel rbc = Channels.newChannel(url.openStream()); | ||
new File(System.getProperty("user.home") + "/.cubyz/" + "R" + i).mkdirs(); // Create the directory. | ||
// Stores the release info: | ||
FileOutputStream fos = new FileOutputStream(file); | ||
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); | ||
fos.close(); | ||
} | ||
} catch(Exception e) {} | ||
} | ||
/** | ||
* Connect to github and get an array of all existing releases. | ||
* Reads the title image of a release to be displayed alongside the changelog. | ||
*/ | ||
public static GithubRelease[] downloadReleaseData(String link) { | ||
String json = ""; | ||
public static BufferedImage readOrDownloadImage(File folder) { | ||
File imageLocation = new File(folder, "image.png"); | ||
if(!imageLocation.exists()) { | ||
// Download the image: | ||
String tag = folder.getName(); | ||
String link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/image.png"; | ||
try { | ||
DownloadAndFileManager.downloadToFile(imageLocation, link); | ||
} catch(IOException e) { | ||
System.err.println("Couldn't load the image of release \""+tag+"\""); | ||
} | ||
} | ||
return DownloadAndFileManager.readImage(imageLocation); | ||
} | ||
/** | ||
* Downloads the executables, assets and such, if necessary and launches the jar file. | ||
*/ | ||
public static void downloadAndLaunchRelease(File folder) { | ||
File jarLocation = new File(folder, "main.jar"); | ||
String tag = folder.getName(); | ||
if(!jarLocation.exists()) { | ||
// The game files need to be downloaded. | ||
|
||
// Addons folder: | ||
String link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/addons.zip"; | ||
try { | ||
DownloadAndFileManager.downloadAndUnzip(folder, link); | ||
} catch(IOException e) { | ||
// That's normal behaviour. Not every release needs this folder. | ||
} | ||
|
||
// Assets folder: | ||
link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/assets.zip"; | ||
try { | ||
DownloadAndFileManager.downloadAndUnzip(folder, link); | ||
} catch(IOException e) { | ||
// That's normal behaviour. Not every release needs this folder. | ||
} | ||
|
||
// Dependencies: | ||
link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/pom.xml"; | ||
DependencyManager.fetchDependencies(link, System.getProperty("user.home") + "/.cubyz", new File(folder, "pom.xml")); | ||
|
||
// And finally the jar file. This is downloaded last to reduce potential issues when the user closes the application before everything is done. | ||
link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/Cubyz.jar"; | ||
try { | ||
DownloadAndFileManager.downloadToFile(jarLocation, link); | ||
} catch(IOException e) { | ||
try { | ||
// The old version 0.6.0 packaged the libraries into the jar file, so there are multiple files for each OS and arch: | ||
if(OSInfo.OS_FAMILY.equals("windows")) { | ||
if(OSInfo.OS_ARCH.contains("64")) { | ||
link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/Cubyz_win_64.jar"; | ||
} else { | ||
link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/Cubyz_win_x86.jar"; | ||
} | ||
DownloadAndFileManager.downloadToFile(jarLocation, link); | ||
} else if(OSInfo.OS_FAMILY.equals("unix")) { | ||
if(OSInfo.OS_ARCH.contains("64")) { | ||
link = "https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/Cubyz_win_64.jar"; | ||
DownloadAndFileManager.downloadToFile(jarLocation, link); | ||
} | ||
} | ||
} catch(IOException e2) {} | ||
} | ||
} | ||
if(!jarLocation.exists()) { | ||
// Something went wrong in the download. | ||
JOptionPane.showInternalMessageDialog(null, "Couldn't download file. Please check your internet connection.", "information", JOptionPane.INFORMATION_MESSAGE); | ||
return; | ||
} | ||
|
||
// Build the classpath: | ||
ArrayList<String> libs = DependencyManager.fetchDependencies("https://github.com/PixelGuys/Cubyz/releases/download/"+tag+"/pom.xml", System.getProperty("user.home") + "/.cubyz", new File(folder, "pom.xml")); | ||
// Put it all together as a classpath attribute: | ||
char classpathSeperator = OSInfo.OS_FAMILY.equals("windows") ? ';' : ':'; | ||
String classpath = ""; | ||
for(String lib : libs) { | ||
classpath += lib+classpathSeperator; | ||
} | ||
// Add the executable jar to the classpath: | ||
classpath += folder.getAbsolutePath()+"/main.jar"; | ||
|
||
// Launch it: | ||
ProcessBuilder pb = new ProcessBuilder("java", "-cp", classpath, "io.cubyz.client.GameLauncher"); | ||
pb.directory(folder); | ||
pb.redirectOutput(Redirect.INHERIT); | ||
pb.redirectError(Redirect.INHERIT); | ||
try { | ||
URL url = new URL(link); | ||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); | ||
connection.connect(); | ||
InputStream stream = connection.getInputStream(); | ||
json = new String(readAllBytes(stream)); | ||
} catch (MalformedURLException e) { | ||
e.printStackTrace(); | ||
pb.start(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
StringWriter sw = new StringWriter(); | ||
e.printStackTrace(new PrintWriter(sw)); | ||
String exceptionAsString = sw.toString(); | ||
JOptionPane.showInternalMessageDialog(null, "Couldn't launch this version. "+exceptionAsString, "information", JOptionPane.INFORMATION_MESSAGE); | ||
} | ||
JsonElement jo = JsonParser.parseString(json); | ||
JsonArray jsonArray = jo.getAsJsonArray(); | ||
ArrayList<GithubRelease> list = new ArrayList<>(); | ||
jsonArray.forEach((elem) -> { | ||
JsonObject obj = elem.getAsJsonObject(); | ||
GithubRelease release = new GithubRelease(); | ||
release.preRelease = obj.get("prerelease").getAsBoolean(); | ||
release.date = obj.get("published_at").getAsString(); | ||
release.name = obj.get("name").getAsString(); | ||
release.version = obj.get("tag_name").getAsString(); | ||
release.description = getDescription(obj.get("body")); | ||
ArrayList<GithubAsset> assets = new ArrayList<>(); | ||
obj.get("assets").getAsJsonArray().forEach((elem2) -> { | ||
JsonObject obj2 = elem2.getAsJsonObject(); | ||
GithubAsset asset = new GithubAsset(); | ||
asset.name = obj2.get("name").getAsString(); | ||
asset.url = obj2.get("browser_download_url").getAsString(); | ||
assets.add(asset); | ||
}); | ||
release.assets = assets.toArray(new GithubAsset[0]); | ||
list.add(release); | ||
}); | ||
return list.toArray(new GithubRelease[0]); | ||
} | ||
public static byte[] readAllBytes(InputStream inputStream) throws IOException { | ||
final int bufLen = 1024; | ||
byte[] buf = new byte[bufLen]; | ||
int readLen; | ||
|
||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) | ||
outputStream.write(buf, 0, readLen); | ||
|
||
return outputStream.toByteArray(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.