Skip to content

Commit

Permalink
Rely on the ML-Specification version present in the ModLauncher envir…
Browse files Browse the repository at this point in the history
…onment 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.
  • Loading branch information
shartte committed Jun 2, 2024
1 parent 2f35deb commit 99f9b4e
Showing 1 changed file with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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";
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -311,4 +314,20 @@ public Collection<IClassProcessor> getProcessors() {
);
}

private static VersionNumber getModLauncherApiVersion() {
Optional<String> version = Optional.empty();
try {
TypesafeMap.Key<String> 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);
}

}

0 comments on commit 99f9b4e

Please sign in to comment.