From 1cb9ab661dcddddb31025943d599f2cf410e40e7 Mon Sep 17 00:00:00 2001 From: Up Date: Sun, 26 May 2024 22:42:50 +0200 Subject: [PATCH] add launcher subproject and dummy fabric.mod.json This lets us show an error message if the software is launched with an outdated version of Java --- Launcher/build.gradle | 33 +++++++++++++++ .../upcraft/axiompatcher/LauncherMain.java | 41 +++++++++++++++++++ build.gradle | 16 +++++++- settings.gradle | 2 + src/main/resources/fabric.mod.json | 14 +++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 Launcher/build.gradle create mode 100644 Launcher/src/main/java/dev/upcraft/axiompatcher/LauncherMain.java create mode 100644 src/main/resources/fabric.mod.json diff --git a/Launcher/build.gradle b/Launcher/build.gradle new file mode 100644 index 0000000..893ce85 --- /dev/null +++ b/Launcher/build.gradle @@ -0,0 +1,33 @@ +plugins { + id 'java-library' +} + +repositories { + mavenCentral() +} + +dependencies { + compileOnly 'org.slf4j:slf4j-api:2.0.13' +} + +configurations { + exports { + canBeConsumed = true + canBeResolved = false + } +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(8)) + vendor.set(JvmVendorSpec.ADOPTIUM) + } +} + +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' +} + +artifacts { + exports(jar) +} diff --git a/Launcher/src/main/java/dev/upcraft/axiompatcher/LauncherMain.java b/Launcher/src/main/java/dev/upcraft/axiompatcher/LauncherMain.java new file mode 100644 index 0000000..9dcbbd7 --- /dev/null +++ b/Launcher/src/main/java/dev/upcraft/axiompatcher/LauncherMain.java @@ -0,0 +1,41 @@ +package dev.upcraft.axiompatcher; + +import org.slf4j.LoggerFactory; + +import javax.swing.*; +import java.awt.*; + +public class LauncherMain { + + public static void main(String[] args) { + String javaVersion = System.getProperty("java.version"); + int javaVersionNumber; + String[] split = javaVersion.split("\\.", 3); + if (javaVersion.startsWith("1.")) { + javaVersionNumber = Integer.parseInt(split[1]); + } else { + javaVersionNumber = Integer.parseInt(split[0]); + } + + if (javaVersionNumber < 17) { + System.out.println("ERROR: You need to have Java 17 or higher installed to run this application."); + JOptionPane.showMessageDialog(null, "You need to have Java 17 or higher installed to run this application.", "Error", JOptionPane.ERROR_MESSAGE); + System.exit(1); + } else { + try { + Class.forName("dev.upcraft.axiompatcher.Main").getDeclaredMethod("main", String[].class).invoke(null, (Object) args); + } catch (Exception e) { + throw new RuntimeException("Unable to launch Axiom Patcher UI", e); + } + } + } + + @SuppressWarnings("unused") + public static void notAMinecraftMod() { + LoggerFactory.getLogger(LauncherMain.class).error("AxiomPatcher is not a Minecraft mod, it must be run as standalone application."); + if(!GraphicsEnvironment.isHeadless()) { + JOptionPane.showMessageDialog(null, "AxiomPatcher is not a Minecraft mod, it must be run as standalone application.", "Error", JOptionPane.ERROR_MESSAGE); + } + System.exit(1); + } +} diff --git a/build.gradle b/build.gradle index 668b352..2d2444a 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ javafx { ] } -mainClassName = 'dev.upcraft.axiompatcher.Main' +mainClassName = 'dev.upcraft.axiompatcher.LauncherMain' application { mainClass.set(mainClassName) } @@ -47,6 +47,18 @@ java { } } +processResources { + inputs.property("version", version) + filesMatching('*.mod.json') { + expand(version: version) + } +} + +tasks.withType(AbstractArchiveTask).configureEach { + preserveFileTimestamps = false + reproducibleFileOrder = true +} + repositories { mavenCentral() @@ -65,6 +77,8 @@ dependencies { testImplementation platform('org.junit:junit-bom:5.10.0') testImplementation 'org.junit.jupiter:junit-jupiter' + + runtimeOnly project(path: ':Launcher', configuration: 'exports') } test { diff --git a/settings.gradle b/settings.gradle index 2d6ad5a..7d54565 100644 --- a/settings.gradle +++ b/settings.gradle @@ -3,3 +3,5 @@ plugins { } rootProject.name = 'AxiomPatcher' + +include 'Launcher' diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..a6b72f7 --- /dev/null +++ b/src/main/resources/fabric.mod.json @@ -0,0 +1,14 @@ +{ + "schemaVersion": 1, + "id": "axiom-patcher", + "name": "Axiom Patcher", + "version": "${version}", + "authors": [ + "Up" + ], + "entrypoints": { + "preLaunch": [ + "dev.upcraft.axiompatcher.LauncherMain::notAMinecraftMod" + ] + } +}