Launcher/LaunchServer/src/main/java/pro/gravit/launchserver/binary/EXEL4JLauncherBinary.java

124 lines
4.2 KiB
Java
Raw Normal View History

package pro.gravit.launchserver.binary;
2018-09-17 10:07:32 +03:00
2019-06-03 10:58:10 +03:00
import java.io.IOException;
import java.nio.file.Path;
2018-09-17 10:07:32 +03:00
import net.sf.launch4j.Builder;
import net.sf.launch4j.Log;
2019-06-03 10:58:10 +03:00
import net.sf.launch4j.config.Config;
import net.sf.launch4j.config.ConfigPersister;
import net.sf.launch4j.config.Jre;
import net.sf.launch4j.config.LanguageID;
import net.sf.launch4j.config.VersionInfo;
import pro.gravit.launchserver.LaunchServer;
import pro.gravit.utils.Version;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
2018-09-17 10:07:32 +03:00
public final class EXEL4JLauncherBinary extends LauncherBinary {
2018-09-22 17:33:00 +03:00
private final static class Launch4JLog extends Log {
private static final Launch4JLog INSTANCE = new Launch4JLog();
@Override
public void append(String s) {
LogHelper.subInfo(s);
}
@Override
public void clear() {
// Do nothing
}
}
// URL constants
private static final String DOWNLOAD_URL = "http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html"; // Oracle
// JRE 8
2018-09-22 17:33:00 +03:00
// File constants
private final Path faviconFile;
2018-10-13 11:01:10 +03:00
2018-09-22 17:33:00 +03:00
public EXEL4JLauncherBinary(LaunchServer server) {
super(server, LauncherBinary.resolve(server, ".exe"));
2018-09-22 17:33:00 +03:00
faviconFile = server.dir.resolve("favicon.ico");
}
@Override
public void build() throws IOException {
LogHelper.info("Building launcher EXE binary file (Using Launch4J)");
setConfig();
2018-09-22 17:33:00 +03:00
// Set favicon path
Config config = ConfigPersister.getInstance().getConfig();
if (IOHelper.isFile(faviconFile))
config.setIcon(faviconFile.toFile());
else {
config.setIcon(null);
LogHelper.warning("Missing favicon.ico file");
}
// Start building
Builder builder = new Builder(Launch4JLog.INSTANCE);
try {
builder.build();
} catch (Throwable e) {
throw new IOException(e);
}
}
private void setConfig() {
Config config = new Config();
// Set file options
2018-09-22 17:33:00 +03:00
config.setChdir(".");
config.setErrTitle("JVM Error");
config.setDownloadUrl(DOWNLOAD_URL);
// Set boolean options
config.setPriorityIndex(0);
config.setHeaderType(Config.GUI_HEADER);
config.setStayAlive(false);
config.setRestartOnCrash(false);
// Prepare JRE
Jre jre = new Jre();
jre.setMinVersion("1.8.0");
2019-05-31 01:40:19 +03:00
if (server.config.launch4j.setMaxVersion)
jre.setMaxVersion(server.config.launch4j.maxVersion);
2018-09-22 17:33:00 +03:00
jre.setRuntimeBits(Jre.RUNTIME_BITS_64_AND_32);
jre.setJdkPreference(Jre.JDK_PREFERENCE_PREFER_JRE);
config.setJre(jre);
// Prepare version info (product)
VersionInfo info = new VersionInfo();
info.setProductName(server.config.launch4j.productName);
info.setProductVersion(formatVars(server.config.launch4j.productVer));
info.setFileDescription(server.config.launch4j.fileDesc);
info.setFileVersion(formatVars(server.config.launch4j.fileVer));
info.setCopyright(server.config.launch4j.copyright);
info.setTrademarks(server.config.launch4j.trademarks);
info.setInternalName(formatVars(server.config.launch4j.internalName));
// Prepare version info (file)
info.setTxtFileVersion(formatVars(server.config.launch4j.txtFileVersion));
info.setTxtProductVersion(formatVars(server.config.launch4j.txtProductVersion));
// Prepare version info (misc)
info.setOriginalFilename(syncBinaryFile.getFileName().toString());
2018-09-22 17:33:00 +03:00
info.setLanguage(LanguageID.RUSSIAN);
config.setVersionInfo(info);
// Set JAR wrapping options
config.setDontWrapJar(false);
config.setJar(server.launcherBinary.syncBinaryFile.toFile());
config.setOutfile(syncBinaryFile.toFile());
2018-09-22 17:33:00 +03:00
// Return prepared config
ConfigPersister.getInstance().setAntConfig(config, null);
}
2019-10-19 19:43:25 +03:00
private static final String VERSION = Version.getVersion().getVersionString();
private static final int BUILD = Version.getVersion().build;
2018-09-22 17:33:00 +03:00
public static String formatVars(String mask) {
return String.format(mask, VERSION, BUILD);
}
2018-09-17 10:07:32 +03:00
}