From a40c96a63f4d000fa518e65ae7f7093c06c4228d Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Sun, 7 Jan 2024 15:50:40 +0100 Subject: [PATCH 1/2] Rely on the ML-Specification version present in the ModLauncher environment rather than on the Package specification version, since that version is unavailable in two scenarios: 1) ML loaded as a JPMS module and 2) ML added to the classpath using a folder rather than JAR-file. --- .../modlauncher/MixinServiceModLauncher.java | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java index 4f95c01c2..ac8ed2ac8 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java @@ -27,7 +27,10 @@ import java.io.InputStream; import java.lang.reflect.Constructor; import java.util.Collection; +import java.util.Optional; +import cpw.mods.modlauncher.api.IEnvironment; +import cpw.mods.modlauncher.api.TypesafeMap; import org.spongepowered.asm.launch.IClassProcessor; import org.spongepowered.asm.launch.platform.container.ContainerHandleModLauncher; import org.spongepowered.asm.logging.ILogger; @@ -47,6 +50,7 @@ import cpw.mods.modlauncher.Launcher; import cpw.mods.modlauncher.api.ITransformationService; +import org.spongepowered.asm.util.VersionNumber; /** * Mixin service for ModLauncher @@ -56,7 +60,7 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { /** * Specification version to check for at startup */ - private static final String MODLAUNCHER_4_SPECIFICATION_VERSION = "4.0"; + private static final VersionNumber MODLAUNCHER_4_SPECIFICATION_VERSION = VersionNumber.parse("4.0"); /** * Specification version for ModLauncher versions >= 9.0.4, yes this is @@ -65,8 +69,8 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { * version 5.0 for example, and ML7 and ML8 both had specification version * 7.0). */ - private static final String MODLAUNCHER_9_SPECIFICATION_VERSION = "8.0"; - + private static final VersionNumber MODLAUNCHER_9_SPECIFICATION_VERSION = VersionNumber.parse("8.0"); + private static final String CONTAINER_PACKAGE = MixinServiceAbstract.LAUNCH_PACKAGE + "platform.container."; private static final String MODLAUNCHER_4_ROOT_CONTAINER_CLASS = MixinServiceModLauncher.CONTAINER_PACKAGE + "ContainerHandleModLauncher"; private static final String MODLAUNCHER_9_ROOT_CONTAINER_CLASS = MixinServiceModLauncher.CONTAINER_PACKAGE + "ContainerHandleModLauncherEx"; @@ -117,15 +121,15 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { private CompatibilityLevel minCompatibilityLevel = CompatibilityLevel.JAVA_8; public MixinServiceModLauncher() { - final Package pkg = ITransformationService.class.getPackage(); - if (pkg.isCompatibleWith(MixinServiceModLauncher.MODLAUNCHER_9_SPECIFICATION_VERSION)) { + VersionNumber apiVersion = getModLauncherApiVersion(); + if (apiVersion.compareTo(MODLAUNCHER_9_SPECIFICATION_VERSION) >= 0) { this.createRootContainer(MixinServiceModLauncher.MODLAUNCHER_9_ROOT_CONTAINER_CLASS); this.minCompatibilityLevel = CompatibilityLevel.JAVA_16; } else { this.createRootContainer(MixinServiceModLauncher.MODLAUNCHER_4_ROOT_CONTAINER_CLASS); } } - + /** * Begin init * @@ -200,9 +204,8 @@ protected ILogger createLogger(String name) { @Override public boolean isValid() { try { - Launcher.INSTANCE.hashCode(); - final Package pkg = ITransformationService.class.getPackage(); - if (!pkg.isCompatibleWith(MixinServiceModLauncher.MODLAUNCHER_4_SPECIFICATION_VERSION)) { + VersionNumber apiVersion = getModLauncherApiVersion(); + if (apiVersion.compareTo(MixinServiceModLauncher.MODLAUNCHER_4_SPECIFICATION_VERSION) < 0) { return false; } } catch (Throwable th) { @@ -311,4 +314,20 @@ public Collection getProcessors() { ); } + private static VersionNumber getModLauncherApiVersion() { + Optional version = Optional.empty(); + try { + TypesafeMap.Key versionProperty = IEnvironment.Keys.MLSPEC_VERSION.get(); + version = Launcher.INSTANCE.environment().getProperty(versionProperty); + } catch (Exception ignored) { + } + + // Fall back to the package information (this is not present when loaded as a module) + if (!version.isPresent()) { + version = Optional.ofNullable(ITransformationService.class.getPackage().getSpecificationVersion()); + } + + return version.map(VersionNumber::parse).orElse(VersionNumber.NONE); + } + } From 9e8af15ea250182a4f68f615f3ecd94ce6d77dde Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Mon, 8 Jan 2024 22:34:53 +0100 Subject: [PATCH 2/2] Style fixes --- .../modlauncher/MixinServiceModLauncher.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java index ac8ed2ac8..fa54c5b1d 100644 --- a/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java +++ b/src/modlauncher/java/org/spongepowered/asm/service/modlauncher/MixinServiceModLauncher.java @@ -29,8 +29,6 @@ import java.util.Collection; import java.util.Optional; -import cpw.mods.modlauncher.api.IEnvironment; -import cpw.mods.modlauncher.api.TypesafeMap; import org.spongepowered.asm.launch.IClassProcessor; import org.spongepowered.asm.launch.platform.container.ContainerHandleModLauncher; import org.spongepowered.asm.logging.ILogger; @@ -49,7 +47,9 @@ import com.google.common.collect.ImmutableList; import cpw.mods.modlauncher.Launcher; +import cpw.mods.modlauncher.api.IEnvironment; import cpw.mods.modlauncher.api.ITransformationService; +import cpw.mods.modlauncher.api.TypesafeMap; import org.spongepowered.asm.util.VersionNumber; /** @@ -121,7 +121,7 @@ public class MixinServiceModLauncher extends MixinServiceAbstract { private CompatibilityLevel minCompatibilityLevel = CompatibilityLevel.JAVA_8; public MixinServiceModLauncher() { - VersionNumber apiVersion = getModLauncherApiVersion(); + VersionNumber apiVersion = MixinServiceModLauncher.getModLauncherApiVersion(); if (apiVersion.compareTo(MODLAUNCHER_9_SPECIFICATION_VERSION) >= 0) { this.createRootContainer(MixinServiceModLauncher.MODLAUNCHER_9_ROOT_CONTAINER_CLASS); this.minCompatibilityLevel = CompatibilityLevel.JAVA_16; @@ -204,7 +204,7 @@ protected ILogger createLogger(String name) { @Override public boolean isValid() { try { - VersionNumber apiVersion = getModLauncherApiVersion(); + VersionNumber apiVersion = MixinServiceModLauncher.getModLauncherApiVersion(); if (apiVersion.compareTo(MixinServiceModLauncher.MODLAUNCHER_4_SPECIFICATION_VERSION) < 0) { return false; } @@ -315,12 +315,8 @@ public Collection getProcessors() { } private static VersionNumber getModLauncherApiVersion() { - Optional version = Optional.empty(); - try { - TypesafeMap.Key versionProperty = IEnvironment.Keys.MLSPEC_VERSION.get(); - version = Launcher.INSTANCE.environment().getProperty(versionProperty); - } catch (Exception ignored) { - } + TypesafeMap.Key versionProperty = IEnvironment.Keys.MLSPEC_VERSION.get(); + Optional version = Launcher.INSTANCE.environment().getProperty(versionProperty); // Fall back to the package information (this is not present when loaded as a module) if (!version.isPresent()) {