1
0
mirror of https://github.com/SKCraft/Launcher.git synced 2025-02-21 02:33:24 +01:00

[Build Tools] Stream-line startup and add more error handling.

This commit is contained in:
sk89q 2015-07-20 15:56:47 -07:00
parent 53cac3f5e3
commit 0c1358124b
2 changed files with 34 additions and 6 deletions

View File

@ -27,6 +27,7 @@ import com.skcraft.launcher.persistence.Persistence;
import com.skcraft.launcher.swing.SwingHelper; import com.skcraft.launcher.swing.SwingHelper;
import lombok.extern.java.Log; import lombok.extern.java.Log;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -50,7 +51,7 @@ public class BuildTools {
private final Launcher launcher; private final Launcher launcher;
private String configFilename = "modpack.json"; private String configFilename = "modpack.json";
private final int port; private int port;
private final File inputDir; private final File inputDir;
private final File wwwDir; private final File wwwDir;
private final File distDir; private final File distDir;
@ -68,6 +69,11 @@ public class BuildTools {
wwwDir.mkdirs(); wwwDir.mkdirs();
launcher = new Launcher(launcherDir); launcher = new Launcher(launcherDir);
setPort(port);
}
private void setPort(int port) {
this.port = port;
launcher.getProperties().setProperty("newsUrl", "http://localhost:" + port + "/news.html"); launcher.getProperties().setProperty("newsUrl", "http://localhost:" + port + "/news.html");
launcher.getProperties().setProperty("packageListUrl", "http://localhost:" + port + "/packages.json"); launcher.getProperties().setProperty("packageListUrl", "http://localhost:" + port + "/packages.json");
launcher.getProperties().setProperty("selfUpdateUrl", "http://localhost:" + port + "/latest.json"); launcher.getProperties().setProperty("selfUpdateUrl", "http://localhost:" + port + "/latest.json");
@ -115,13 +121,15 @@ public class BuildTools {
return null; return null;
} }
public void startHttpServer() throws Exception { public Server startHttpServer() throws Exception {
LocalHttpServerBuilder builder = new LocalHttpServerBuilder(); LocalHttpServerBuilder builder = new LocalHttpServerBuilder();
builder.setBaseDir(wwwDir); builder.setBaseDir(wwwDir);
builder.setPort(port); builder.setPort(port);
Server server = builder.build(); Server server = builder.build();
server.start(); server.start();
setPort(((ServerConnector) server.getConnectors()[0]).getLocalPort());
return server;
} }
private void showMainWindow() { private void showMainWindow() {
@ -256,21 +264,41 @@ public class BuildTools {
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Launcher.setupLogger();
ToolArguments options = new ToolArguments(); ToolArguments options = new ToolArguments();
new JCommander(options, args); new JCommander(options, args);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ignored) {
}
}
});
final BuildTools main = new BuildTools(options.getDir(), options.getPort()); final BuildTools main = new BuildTools(options.getDir(), options.getPort());
main.startHttpServer();
try {
main.startHttpServer();
} catch (Throwable t) {
log.log(Level.WARNING, "Web server start failure", t);
SwingHelper.showErrorDialog(null, "Couldn't start the local web server on a free TCP port! " +
"The web server is required to temporarily host the modpack files for the launcher.", "Build Tools Error", t);
System.exit(1);
return;
}
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
main.showMainWindow(); main.showMainWindow();
} catch (Throwable t) { } catch (Throwable t) {
log.log(Level.WARNING, "Load failure", t); log.log(Level.WARNING, "Load failure", t);
SwingHelper.showErrorDialog(null, "Failed to launch build tools!", "Build tools error", t); SwingHelper.showErrorDialog(null, "Failed to launch build tools!", "Build Tools Error", t);
} }
} }
}); });

View File

@ -18,6 +18,6 @@ public class ToolArguments {
private File dir = new File("."); private File dir = new File(".");
@Parameter(names = "--port") @Parameter(names = "--port")
private int port = 28888; private int port = 0;
} }