Skip to content

Commit

Permalink
Reenable the self-updater.
Browse files Browse the repository at this point in the history
  • Loading branch information
e3ndr committed Dec 22, 2024
1 parent 5b63e28 commit cdf67ae
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/main/java/co/casterlabs/caffeinated/updater/Updater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -43,45 +52,47 @@ public String getLaunchCommand() {

@Override
public void updateUpdater(UpdaterDialog dialog) throws IOException, InterruptedException {
// HttpResponse<InputStream> 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<InputStream> 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();
}

Expand Down

0 comments on commit cdf67ae

Please sign in to comment.