From cdf67aeac09698160e7a44946a44b303e11e7682 Mon Sep 17 00:00:00 2001 From: e3ndr <33337309+e3ndr@users.noreply.github.com> Date: Sat, 21 Dec 2024 19:34:08 -0600 Subject: [PATCH] Reenable the self-updater. --- .../caffeinated/updater/Updater.java | 2 + .../updater/target/WindowsTarget.java | 89 +++++++++++-------- 2 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/main/java/co/casterlabs/caffeinated/updater/Updater.java b/src/main/java/co/casterlabs/caffeinated/updater/Updater.java index 2476338..e5e9848 100644 --- a/src/main/java/co/casterlabs/caffeinated/updater/Updater.java +++ b/src/main/java/co/casterlabs/caffeinated/updater/Updater.java @@ -46,6 +46,8 @@ public class Updater { public static final Target target = Target.get(); public static boolean isLauncherOutOfDate() throws IOException, InterruptedException { + if (System.getProperty("caffeinated.reinstall", "false").equalsIgnoreCase("true")) return true; + int remoteLauncherVersion = Integer.parseInt(WebUtil.sendHttpRequest(HttpRequest.newBuilder().uri(URI.create(UPDATER_VERSION_URL))).trim()); return VERSION < remoteLauncherVersion; } diff --git a/src/main/java/co/casterlabs/caffeinated/updater/target/WindowsTarget.java b/src/main/java/co/casterlabs/caffeinated/updater/target/WindowsTarget.java index 5c5ba33..ec4c39a 100644 --- a/src/main/java/co/casterlabs/caffeinated/updater/target/WindowsTarget.java +++ b/src/main/java/co/casterlabs/caffeinated/updater/target/WindowsTarget.java @@ -1,11 +1,20 @@ package co.casterlabs.caffeinated.updater.target; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; import java.util.Arrays; import java.util.List; +import java.util.concurrent.TimeUnit; import co.casterlabs.caffeinated.updater.Updater; +import co.casterlabs.caffeinated.updater.util.WebUtil; import co.casterlabs.caffeinated.updater.window.UpdaterDialog; import co.casterlabs.commons.platform.OSDistribution; @@ -43,45 +52,47 @@ public String getLaunchCommand() { @Override public void updateUpdater(UpdaterDialog dialog) throws IOException, InterruptedException { -// HttpResponse response = WebUtil.sendRawHttpRequest(HttpRequest.newBuilder().uri(URI.create(Updater.DIST_URL_BASE + "/Caffeinated-Installer.exe")), BodyHandlers.ofInputStream()); -// -// final File tempInstaller = new File(System.getProperty("java.io.tmpdir"), "Caffeinated-Installer.exe"); -// -// tempInstaller.delete(); -// tempInstaller.createNewFile(); -// -// dialog.setStatus("Downloading installer..."); -// -// try (InputStream source = response.body(); -// OutputStream dest = new FileOutputStream(tempInstaller)) { -// -// double totalSize = Long.parseLong(response.headers().firstValue("Content-Length").orElse("0")); -// int totalRead = 0; -// -// byte[] buffer = new byte[2048]; -// int read = 0; -// -// while ((read = source.read(buffer)) != -1) { -// dest.write(buffer, 0, read); -// totalRead += read; -// -// double progress = totalRead / totalSize; -// -// dialog.setStatus(String.format("Downloading installer... (%.0f%%)", progress * 100)); -// dialog.setProgress(progress); -// } -// -// dest.flush(); -// dialog.setProgress(-1); -// -// Runtime.getRuntime().exec(new String[] { -// "powershell", -// "-Command", -// "\"Start-Process '" + tempInstaller.getCanonicalPath() + "' -Verb RunAs\"" -// }); -// TimeUnit.SECONDS.sleep(2); -// System.exit(0); -// } + HttpResponse response = WebUtil.sendRawHttpRequest(HttpRequest.newBuilder().uri(URI.create(Updater.DIST_URL_BASE + "/Caffeinated-Installer.exe")), BodyHandlers.ofInputStream()); + + final File tempInstaller = new File(System.getProperty("java.io.tmpdir"), "Caffeinated-Installer.exe"); + + tempInstaller.delete(); + tempInstaller.createNewFile(); + + dialog.setStatus("Downloading installer..."); + + try (InputStream source = response.body(); + OutputStream dest = new FileOutputStream(tempInstaller)) { + + double totalSize = Long.parseLong(response.headers().firstValue("Content-Length").orElse("0")); + int totalRead = 0; + + byte[] buffer = new byte[2048]; + int read = 0; + + while ((read = source.read(buffer)) != -1) { + dest.write(buffer, 0, read); + totalRead += read; + + double progress = totalRead / totalSize; + + dialog.setStatus(String.format("Downloading installer... (%.0f%%)", progress * 100)); + dialog.setProgress(progress); + } + + dest.flush(); + dialog.setProgress(-1); + + Runtime.getRuntime().exec(new String[] { +// "cmd.exe", +// "/c", +// "runas", +// "/user:Administrator", + tempInstaller.getCanonicalPath() + }); + TimeUnit.SECONDS.sleep(2); + System.exit(0); + } throw new UnsupportedOperationException(); }