Skip to content

Commit

Permalink
Changed to set java.awt.headless if running in headless mode, should …
Browse files Browse the repository at this point in the history
…make awt smarter

Added generic displayMessage method in main class
  • Loading branch information
thefishlive committed Sep 28, 2013
1 parent c6530a4 commit c61fdaf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 68 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repositories {

group = 'uk.thecodingbadgers'
archivesBaseName = 'adminpack-installer'
version = '1.4'
version = '1.5'
targetCompatibility = '1.7'
sourceCompatibility = '1.7'

Expand Down
33 changes: 26 additions & 7 deletions src/main/java/cpw/mods/fml/installer/SimpleInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public static void main(String[] args) throws IOException {
}

System.out.println(profileFileLocation);
if (VersionInfo.isHeadless()) {
if (System.getenv("headless") != null || VersionInfo.isHeadless()) {
System.setProperty("java.awt.headless", "true");
headless = true;
}

Expand All @@ -62,6 +63,19 @@ public static void main(String[] args) throws IOException {

launchGui();
}

public static void displayMessage(String message, String title) {
if (SimpleInstaller.headless) {
try {
JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
return;
} catch (Exception ex) {
}
}

System.out.println(title);
System.out.println(message);
}

public static boolean runInstaller() {
InstallerAction action = InstallerAction.CLIENT;
Expand All @@ -86,17 +100,22 @@ private static boolean handleOptions(OptionParser parser, OptionSet optionSet) t
return false;
}

if (optionSet.has(profileFileOption)) {
String file = optionSet.valueOf(profileFileOption);
URL url = new URL(file);
profileFileLocation = url;
}

if (optionSet.has(headlessOption)) {
System.out.println("Running in headless mode");
headless = true;
}

if (optionSet.has(profileFileOption)) {
String file = optionSet.valueOf(profileFileOption);

try {
URL url = new URL(file);
profileFileLocation = url;
} catch (Exception ex) {
System.out.println("Unkown url provided, exiting");
}
}

if (optionSet.has(installdirOption)) {
installdir = new File(optionSet.valueOf(installdirOption));
}
Expand Down
81 changes: 24 additions & 57 deletions src/main/java/cpw/mods/fml/installer/VersionInfo.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package cpw.mods.fml.installer;

import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

Expand All @@ -21,12 +20,8 @@
import argo.saj.InvalidSyntaxException;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.OutputSupplier;

import cpw.mods.fml.installer.mods.ModInfo;
import cpw.mods.fml.installer.mods.ResourcePackInfo;
Expand All @@ -36,23 +31,29 @@ public class VersionInfo {
public static final VersionInfo INSTANCE = new VersionInfo();
private static String forgeVersion;
private static String minecraftVersion;
public final JsonRootNode versionData;
private static String versionTarget;

private JsonRootNode versionData;

public VersionInfo() {
try {
versionData = parseStream(SimpleInstaller.profileFileLocation.openStream());

int remoteVersion = Integer.parseInt(versionData.getNumberValue("version"));

if (remoteVersion < VERSION) {
JOptionPane.showMessageDialog(null, "The profile file you are using is out of date, please specify a more uptodate file", "Out of Data", JOptionPane.ERROR_MESSAGE);
SimpleInstaller.displayMessage("The profile file you are using is out of date\nplease specify a more uptodate file", "Out of Date");
System.exit(1);
} else if (remoteVersion > VERSION) {
JOptionPane.showMessageDialog(null, "The installer you are using is out of date, please update your installer", "Out of Data", JOptionPane.ERROR_MESSAGE);
SimpleInstaller.displayMessage("The installer you are using is out of date\nplease update your installer.", "Out of Date");
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("http://mcbadgercraft.com/adminpack/installer"));
}
System.exit(2);
}

} catch (Exception e) {
throw Throwables.propagate(e);
SimpleInstaller.displayMessage("Error loading version data", "Error loading data");
System.exit(3);
}
}

Expand All @@ -61,8 +62,9 @@ private JsonRootNode parseStream(InputStream openStream) throws IOException, Inv

List<String> lines = IOUtils.readLines(openStream);
JsonRootNode root = parser.parse(StringUtils.join(lines, ' '));
forgeVersion = root.getStringValue("install", "forgeVersion");
minecraftVersion = root.getStringValue("install", "minecraftVersion");
minecraftVersion = replaceMacros(root.getStringValue("install", "minecraftVersion"));
forgeVersion = replaceMacros(root.getStringValue("install", "forgeVersion"));
versionTarget = replaceMacros(root.getStringValue("install", "target"));

List<String> parsedLines = new ArrayList<String>();
for (String line : lines) {
Expand All @@ -73,8 +75,9 @@ private JsonRootNode parseStream(InputStream openStream) throws IOException, Inv
}

private static String replaceMacros(String input) {
input = input.replace("${minecraft_version}", getMinecraftVersion());
input = input.replace("${forge_version}", getForgeVersion());
if (getMinecraftVersion() != null) input = input.replace("${minecraft_version}", getMinecraftVersion());
if (getForgeVersion() != null) input = input.replace("${forge_version}", getForgeVersion());
if (getVersionTarget() != null) input = input.replace("${target}", getVersionTarget());
return input;
}

Expand All @@ -86,10 +89,6 @@ public static String getProfileName() {
return INSTANCE.versionData.getStringValue("install", "profileName");
}

public static String getVersionTarget() {
return INSTANCE.versionData.getStringValue("install", "target");
}

public static String getTitle() {
return INSTANCE.versionData.getStringValue("install", "title");
}
Expand All @@ -106,14 +105,6 @@ public static JsonNode getVersionInfo() {
return INSTANCE.versionData.getNode("versionInfo");
}

public static String getContainedFile() {
return INSTANCE.versionData.getStringValue("install", "filePath");
}

public static void extractFile(File path) throws IOException {
INSTANCE.doFileExtract(path);
}

private static String getForgeVersion() {
return forgeVersion;
}
Expand All @@ -122,36 +113,12 @@ public static String getMinecraftVersion() {
return minecraftVersion;
}

public static File getMinecraftFile(File path) {
return new File(new File(path, getMinecraftVersion()), getMinecraftVersion() + ".jar");
}

public static File getLibraryPath(File root) {
String path = INSTANCE.versionData.getStringValue("install", "path");
String[] split = Iterables.toArray(Splitter.on(':').omitEmptyStrings().split(path), String.class);
File dest = root;
Iterable<String> subSplit = Splitter.on('.').omitEmptyStrings().split(split[0]);
for (String part : subSplit) {
dest = new File(dest, part);
}
dest = new File(new File(dest, split[1]), split[2]);
String fileName = split[1] + "-" + split[2] + ".jar";
return new File(dest, fileName);
}

public static URL getForgeDownloadUrl() {
try {
return new URL(replaceMacros(INSTANCE.versionData.getStringValue("install", "forgeUrl")));
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
public static String getVersionTarget() {
return versionTarget;
}

private void doFileExtract(File path) throws IOException {
InputStream inputStream = getClass().getResourceAsStream("/" + getContainedFile());
OutputSupplier<FileOutputStream> outputSupplier = Files.newOutputStreamSupplier(path);
ByteStreams.copy(inputStream, outputSupplier);
public static File getMinecraftFile(File path) {
return new File(new File(path, getMinecraftVersion()), getMinecraftVersion() + ".jar");
}

public static List<ProfileInfo> getAccounts(File target) {
Expand Down
3 changes: 0 additions & 3 deletions src/test/resources/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@
"welcome": "Test installer",
"title": "Test installer",
"headless": true,
"filePath": "minecraftforge-universal-${minecraft_version}-${forge_version}.jar",
"path": "net.minecraftforge:minecraftforge:${forge_version}",
"forgeUrl": "http://files.minecraftforge.net/minecraftforge/minecraftforge-universal-${forge_version}.jar",
"forgeVersion": "1.6.2-9.10.0.804",
"minecraftVersion": "1.6.2",
"logo": "/big_logo.png",
Expand Down

0 comments on commit c61fdaf

Please sign in to comment.