From 1eab9929bb885fdf9ce5c0d303065b56fe0aabc2 Mon Sep 17 00:00:00 2001 From: Mumfrey Date: Fri, 27 Feb 2015 17:58:19 +0000 Subject: [PATCH] Add concept of version to mixin config, for forward compatibility checks --- checkstyle.xml | 2 +- .../asm/launch/MixinBootstrap.java | 8 +- .../asm/launch/MixinInitialisationError.java | 47 ++++ .../asm/mixin/MixinEnvironment.java | 4 + .../asm/mixin/transformer/MixinConfig.java | 33 ++- .../mixin/transformer/MixinTransformer.java | 7 +- .../spongepowered/asm/util/VersionNumber.java | 213 ++++++++++++++++++ 7 files changed, 306 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/spongepowered/asm/launch/MixinInitialisationError.java create mode 100644 src/main/java/org/spongepowered/asm/util/VersionNumber.java diff --git a/checkstyle.xml b/checkstyle.xml index 95d53e93..25b8ebfd 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -51,7 +51,7 @@ - + diff --git a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java index e7fe725f..b5760fdb 100644 --- a/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java +++ b/src/main/java/org/spongepowered/asm/launch/MixinBootstrap.java @@ -31,7 +31,7 @@ import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; public abstract class MixinBootstrap { - public static final String VERSION = "0.1"; + public static final String VERSION = "0.2"; public static final String INIT_KEY = "mixin.initialised"; public static final String MIXIN_PACKAGE = "org.spongepowered.asm.mixin."; @@ -52,6 +52,12 @@ public abstract class MixinBootstrap { private MixinBootstrap() {} public static void init() { + Object registeredVersion = Launch.blackboard.get(MixinBootstrap.INIT_KEY); + if (registeredVersion != null && !registeredVersion.equals(MixinBootstrap.VERSION)) { + throw new MixinInitialisationError("Mixin subsystem version " + registeredVersion + + " was already initialised. Cannot bootstrap version " + MixinBootstrap.VERSION); + } + Launch.blackboard.put(MixinBootstrap.INIT_KEY, MixinBootstrap.VERSION); MixinEnvironment.getCurrentEnvironment(); } diff --git a/src/main/java/org/spongepowered/asm/launch/MixinInitialisationError.java b/src/main/java/org/spongepowered/asm/launch/MixinInitialisationError.java new file mode 100644 index 00000000..44effb89 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/launch/MixinInitialisationError.java @@ -0,0 +1,47 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.launch; + + +public class MixinInitialisationError extends Error { + + private static final long serialVersionUID = 1L; + + public MixinInitialisationError() { + } + + public MixinInitialisationError(String message) { + super(message); + } + + public MixinInitialisationError(Throwable cause) { + super(cause); + } + + public MixinInitialisationError(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java index 43d42a61..7d348369 100644 --- a/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java +++ b/src/main/java/org/spongepowered/asm/mixin/MixinEnvironment.java @@ -180,6 +180,10 @@ public class MixinEnvironment { return this.side != null ? this.side : Side.UNKNOWN; } + + public String getVersion() { + return (String)Launch.blackboard.get(MixinBootstrap.INIT_KEY); + } public static MixinEnvironment getCurrentEnvironment() { if (MixinEnvironment.env == null) { diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java index 8459c4a1..218e46d7 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinConfig.java @@ -42,6 +42,7 @@ import org.apache.logging.log4j.core.helpers.Booleans; import org.spongepowered.asm.mixin.MixinEnvironment; import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; import org.spongepowered.asm.mixin.injection.struct.ReferenceMapper; +import org.spongepowered.asm.util.VersionNumber; import com.google.gson.Gson; import com.google.gson.annotations.SerializedName; @@ -77,6 +78,13 @@ class MixinConfig implements Comparable { */ private final transient List mixins = new ArrayList(); + /** + * Minimum version of the mixin subsystem required to correctly apply mixins + * in this configuration. + */ + @SerializedName("minVersion") + private String version; + /** * Configuration priority */ @@ -165,11 +173,24 @@ class MixinConfig implements Comparable { private MixinConfig() {} /** - * Called immediately after deserialisation + * Called immediately after deserialisation + * + * @param name Mixin config name + * @return true if the config was successfully initialised and should be + * returned, or false if initialisation failed and the config should + * be discarded */ - private void onLoad(String name) { + private boolean onLoad(String name) { this.name = name; + VersionNumber minVersion = VersionNumber.parse(this.version); + VersionNumber curVersion = VersionNumber.parse(MixinEnvironment.getCurrentEnvironment().getVersion()); + if (minVersion.compareTo(curVersion) > 0) { + this.logger.warn("Mixin config {} requires mixin subsystem version {} but {} was found. The mixin config will not be applied.", + this.name, minVersion, curVersion); + return false; + } + if (this.pluginClassName != null) { try { Class pluginClass = Class.forName(this.pluginClassName, true, Launch.classLoader); @@ -200,6 +221,8 @@ class MixinConfig implements Comparable { this.refMapper = ReferenceMapper.read(this.refMapperConfig); this.verboseLogging |= Booleans.parseBoolean(System.getProperty("mixin.debug.verbose"), false) | MixinTransformer.DEBUG_ALL; + + return true; } /** @@ -403,8 +426,10 @@ class MixinConfig implements Comparable { static MixinConfig create(String configFile) { try { MixinConfig config = new Gson().fromJson(new InputStreamReader(Launch.classLoader.getResourceAsStream(configFile)), MixinConfig.class); - config.onLoad(configFile); - return config; + if (config.onLoad(configFile)) { + return config; + } + return null; } catch (Exception ex) { ex.printStackTrace(); throw new IllegalArgumentException(String.format("The specified configuration file '%s' was invalid or could not be read", configFile)); diff --git a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java index 0bbeed0b..672470ed 100644 --- a/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java +++ b/src/main/java/org/spongepowered/asm/mixin/transformer/MixinTransformer.java @@ -214,7 +214,10 @@ public class MixinTransformer extends TreeTransformer { if (configs != null) { for (String configFile : configs) { try { - this.configs.add(MixinConfig.create(configFile)); + MixinConfig config = MixinConfig.create(configFile); + if (config != null) { + this.configs.add(config); + } } catch (Exception ex) { this.logger.warn(String.format("Failed to load mixin config: %s", configFile), ex); } @@ -760,7 +763,7 @@ public class MixinTransformer extends TreeTransformer { if (opcode == ivalidOp) { // At the moment I don't handle any transient locals because I haven't seen any in the wild, but let's avoid writing // code which will likely break things and fix it if a real test case ever appears - throw new InvalidMixinException("Cannot handle " + ASMHelper.getOpcodeName(opcode) +" opcode (0x" + throw new InvalidMixinException("Cannot handle " + ASMHelper.getOpcodeName(opcode) + " opcode (0x" + Integer.toHexString(opcode).toUpperCase() + ") in class initialiser"); } } diff --git a/src/main/java/org/spongepowered/asm/util/VersionNumber.java b/src/main/java/org/spongepowered/asm/util/VersionNumber.java new file mode 100644 index 00000000..bc9d66a2 --- /dev/null +++ b/src/main/java/org/spongepowered/asm/util/VersionNumber.java @@ -0,0 +1,213 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered.org + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.asm.util; + +import java.io.Serializable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + + +/** + * Represents a software version number in major.minor.revision.build + * format as a sequence of four shorts packed into a long. This is to + * facilitate meaningful comparison between version numbers. + */ +public class VersionNumber implements Comparable, Serializable { + + private static final long serialVersionUID = 1L; + + /** + * Represents no version number or a version number which could not be + * parsed + */ + public static final VersionNumber NONE = new VersionNumber(); + + /** + * Regex for matching a version number specified as a string + */ + private static final Pattern versionNumberPattern = + Pattern.compile("^(\\d{1,5})(?:\\.(\\d{1,5})(?:\\.(\\d{1,5})(?:\\.(\\d{1,5}))?)?)?(-[a-zA-Z0-9_\\-]+)?$"); + + /** + * The version number, packed as four shorts into a long + */ + private final long value; + + /** + * suffix such as -BETA or -SNAPSHOT, must be prefixed with "-" to be + * matched + */ + private final String suffix; + + private VersionNumber() { + this.value = 0; + this.suffix = ""; + } + + private VersionNumber(short[] parts) { + this(parts, null); + } + + private VersionNumber(short[] parts, String suffix) { + this.value = VersionNumber.pack(parts); + this.suffix = suffix != null ? suffix : ""; + } + + private VersionNumber(short major, short minor, short revision, short build) { + this(major, minor, revision, build, null); + } + + private VersionNumber(short major, short minor, short revision, short build, String suffix) { + this.value = VersionNumber.pack(major, minor, revision, build); + this.suffix = suffix != null ? suffix : ""; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + short[] parts = VersionNumber.unpack(this.value); + + return String.format("%d.%d%3$s%4$s%5$s", + parts[0], + parts[1], + (this.value & Integer.MAX_VALUE) > 0 ? String.format(".%d", parts[2]) : "", + (this.value & Short.MAX_VALUE) > 0 ? String.format(".%d", parts[3]) : "", + this.suffix); + } + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(VersionNumber other) { + if (other == null) { + return 1; + } + long delta = this.value - other.value; + return delta > 0 ? 1 : delta < 0 ? -1 : 0; + } + + /* (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object other) { + if (!(other instanceof VersionNumber)) { + return false; + } + + return ((VersionNumber)other).value == this.value; + } + + /* (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return (int)(this.value >> 32) ^ (int)(this.value & 0xFFFFFFFF); + } + + /** + * Packs the supplied array of shorts into a long + * + * @param shorts short values to pack + * @return supplied shorts packed into a long + */ + private static long pack(short... shorts) { + return (long)shorts[0] << 48 | (long)shorts[1] << 32 | shorts[2] << 16 | shorts[3]; + } + + /** + * Unpacks a long into an array of four shorts + * + * @param along Long to unpack + * @return Unpacked array of four shorts + */ + private static short[] unpack(long along) { + return new short[] { + (short)(along >> 48), + (short)(along >> 32 & Short.MAX_VALUE), + (short)(along >> 16 & Short.MAX_VALUE), + (short)(along & Short.MAX_VALUE) + }; + } + + /** + * Parse a version number specified as a string + * + * @param version Version number to parse + * @return Version number + */ + public static final VersionNumber parse(String version) { + return VersionNumber.parse(version, VersionNumber.NONE); + } + + /** + * Parse a version number specified as a string and return default if + * parsing fails + * + * @param version Version number to parse + * @param defaultVersion Version number to return if parse fails + * @return Version number + */ + public static VersionNumber parse(String version, String defaultVersion) { + return VersionNumber.parse(version, VersionNumber.parse(defaultVersion)); + } + + /** + * Parse a version number specified as a string and return default if + * parsing fails + * + * @param version Version number to parse + * @param defaultVersion Version number to return if parse fails + * @return Version number + */ + private static VersionNumber parse(String version, VersionNumber defaultVersion) { + if (version == null) { + return defaultVersion; + } + + Matcher versionNumberPatternMatcher = VersionNumber.versionNumberPattern.matcher(version); + if (!versionNumberPatternMatcher.matches()) { + return defaultVersion; + } + + short[] parts = new short[4]; + for (int pos = 0; pos < 4; pos++) { + String part = versionNumberPatternMatcher.group(pos + 1); + if (part != null) { + int value = Integer.parseInt(part); + if (value > Short.MAX_VALUE) { + throw new IllegalArgumentException("Version parts cannot exceed " + Short.MAX_VALUE + ", found " + value); + } + parts[pos] = (short)value; + } + } + + return new VersionNumber(parts, versionNumberPatternMatcher.group(5)); + } +}