diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..51a8bba --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..329c16d --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + UnturnedNPCCreatorAutoupdater + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/bin/autoupdater/Updater.class b/bin/autoupdater/Updater.class new file mode 100644 index 0000000..9059abd Binary files /dev/null and b/bin/autoupdater/Updater.class differ diff --git a/src/autoupdater/Updater.java b/src/autoupdater/Updater.java new file mode 100644 index 0000000..bd93fa6 --- /dev/null +++ b/src/autoupdater/Updater.java @@ -0,0 +1,82 @@ +package autoupdater; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Updater { + + static final String FILE_URL = "https://api.github.com/repos/volcanocookies/unturnednpccreator/releases/latest"; + static final String FILE_NAME = "Unturned NPC Creator.exe"; + + public static void main(String[] args) { + //Delete old program + File oldVersion = new File(args[0]); + oldVersion.delete(); + + //Get latest version + try { + Matcher matcher = Pattern.compile("\"browser_download_url\":\"(https://github.com/VolcanoCookies/UnturnedNPCCreator/releases/download/[^\"]*)\"").matcher(getText(FILE_URL)); + if(matcher.find()) { + System.out.println(matcher.group(1)); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + try (BufferedInputStream in = new BufferedInputStream(new URL(FILE_URL).openStream()); + FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME)) { + byte dataBuffer[] = new byte[1024]; + int bytesRead; + while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { + fileOutputStream.write(dataBuffer, 0, bytesRead); + } + } catch (IOException e) { + // handle exception + e.printStackTrace(); + } + + //Start the new version + try { + new ProcessBuilder(oldVersion.getAbsolutePath().substring(0, oldVersion.getAbsolutePath().lastIndexOf("\\")) + "\\" + FILE_NAME).start(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + static String getText(String url) throws IOException { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + //add headers to the connection, or check the status if desired.. + + // handle error response code it occurs + int responseCode = connection.getResponseCode(); + InputStream inputStream; + if (200 <= responseCode && responseCode <= 299) { + inputStream = connection.getInputStream(); + } else { + inputStream = connection.getErrorStream(); + } + + BufferedReader in = new BufferedReader( + new InputStreamReader( + inputStream)); + + StringBuilder response = new StringBuilder(); + String currentLine; + + while ((currentLine = in.readLine()) != null) + response.append(currentLine); + + in.close(); + + return response.toString(); + } +}