diff --git a/PC_Launcher/src/main/java/launcher/Gameupdater/ClientDownloader.java b/PC_Launcher/src/main/java/launcher/Gameupdater/ClientDownloader.java index dc156b12f..15c78b2f9 100644 --- a/PC_Launcher/src/main/java/launcher/Gameupdater/ClientDownloader.java +++ b/PC_Launcher/src/main/java/launcher/Gameupdater/ClientDownloader.java @@ -38,6 +38,7 @@ public static void downloadOrUpdate(File gamePath, String fileName, String url, error.printStackTrace(); } finally { Downloader.currently_updating = false; + Downloader.update_latch.countDown(); } }).start(); } diff --git a/PC_Launcher/src/main/java/launcher/Gameupdater/Downloader.java b/PC_Launcher/src/main/java/launcher/Gameupdater/Downloader.java index b376ef98e..1843493b6 100644 --- a/PC_Launcher/src/main/java/launcher/Gameupdater/Downloader.java +++ b/PC_Launcher/src/main/java/launcher/Gameupdater/Downloader.java @@ -12,6 +12,7 @@ import java.net.UnknownHostException; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; public class Downloader implements Runnable { @@ -21,6 +22,7 @@ public class Downloader implements Runnable { public static boolean offline_start = false; public static boolean currently_updating = false; + public static CountDownLatch update_latch = new CountDownLatch(1); private List FILE_LIST = new ArrayList<>(); @@ -124,5 +126,6 @@ public void run() { } ProgressBar.setDownloadProgress(ProgressBar.doneText, ProgressBar.donePercent); currently_updating = false; + update_latch.countDown(); } } diff --git a/PC_Launcher/src/main/java/launcher/Utils/ClientLauncher.java b/PC_Launcher/src/main/java/launcher/Utils/ClientLauncher.java index b4ed16bca..cb02c8cb1 100644 --- a/PC_Launcher/src/main/java/launcher/Utils/ClientLauncher.java +++ b/PC_Launcher/src/main/java/launcher/Utils/ClientLauncher.java @@ -12,14 +12,41 @@ import java.io.IOException; import java.io.OutputStreamWriter; -import javax.swing.JOptionPane; - public class ClientLauncher { + // Launch client automatically when updates are complete. + private static String autoPlayServer; + private static final Runnable autoPlayRunnable = new Runnable() { + @Override + public void run() { + try { + Downloader.update_latch.await(); + } catch (InterruptedException e) { + return; + } + String serverName = autoPlayServer; + autoPlayServer = null; + try { + launchClientForServer(serverName); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }; + public static void launchClientForServer(String serverName) throws IOException { - if (Downloader.currently_updating) { - JOptionPane.showMessageDialog(null, "Currently updating the client, please wait!"); - return; - } + // Wait for updates to complete before launching. + if (autoPlayServer != null || Downloader.currently_updating) { + // Store server name. + boolean threadStarted = autoPlayServer != null; + autoPlayServer = serverName; + // Wait in background. + if (!threadStarted) { + Thread t = new Thread(autoPlayRunnable); + t.start(); + } + return; + } + // Launch client. switch (serverName) { case "preservation": { String ip = "game.openrsc.com";