mirror of
https://github.com/LegacyModdingMC/UniMix.git
synced 2026-11-19 16:29:45 +00:00
Add support for mixin config option inheritance, closes #273
This commit is contained in:
1 parent
45e152690c
commit
ab935e061f
3 files changed
+233
-13
No files matched your search
@@ -25,6 +25,7 @@
|
||||
package org.spongepowered.asm.mixin;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -55,6 +56,11 @@ public final class Mixins {
|
||||
*/
|
||||
private static final Set<String> errorHandlers = new LinkedHashSet<String>();
|
||||
|
||||
/**
|
||||
* Names of configs which have already been registered
|
||||
*/
|
||||
private static final Set<String> registeredConfigs = new HashSet<String>();
|
||||
|
||||
private Mixins() {}
|
||||
|
||||
/**
|
||||
@@ -97,7 +103,7 @@ public final class Mixins {
|
||||
}
|
||||
|
||||
private static void registerConfiguration(Config config) {
|
||||
if (config == null) {
|
||||
if (config == null || Mixins.registeredConfigs.contains(config.getName())) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -106,6 +112,12 @@ public final class Mixins {
|
||||
env.registerConfig(config.getName());
|
||||
}
|
||||
Mixins.getConfigs().add(config);
|
||||
Mixins.registeredConfigs.add(config.getName());
|
||||
|
||||
Config parent = config.getParent();
|
||||
if (parent != null) {
|
||||
Mixins.registerConfiguration(parent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,14 +24,32 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.spongepowered.asm.launch.MixinInitialisationError;
|
||||
import org.spongepowered.asm.mixin.MixinEnvironment;
|
||||
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
/**
|
||||
* Handle for marshalling mixin configs outside of the transformer package
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
/**
|
||||
* Logger
|
||||
*/
|
||||
private static final Logger logger = LogManager.getLogger("mixin");
|
||||
|
||||
/**
|
||||
* All loaded configs, stored by name so that parents can be assigned
|
||||
*/
|
||||
private static final Map<String, Config> allConfigs = new HashMap<String, Config>();
|
||||
|
||||
/**
|
||||
* Config name, used as identity for the purposes of {@link #equals}
|
||||
*/
|
||||
@@ -50,7 +68,7 @@ public class Config {
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Access inner config
|
||||
*/
|
||||
@@ -78,6 +96,14 @@ public class Config {
|
||||
public MixinEnvironment getEnvironment() {
|
||||
return this.config.getEnvironment();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the the config's parent
|
||||
*/
|
||||
public Config getParent() {
|
||||
MixinConfig parent = this.config.getParent();
|
||||
return parent != null ? parent.getHandle() : null;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Object#toString()
|
||||
@@ -113,7 +139,43 @@ public class Config {
|
||||
*/
|
||||
@Deprecated
|
||||
public static Config create(String configFile, MixinEnvironment outer) {
|
||||
return MixinConfig.create(configFile, outer);
|
||||
Config config = Config.allConfigs.get(configFile);
|
||||
if (config != null) {
|
||||
return config;
|
||||
}
|
||||
|
||||
try {
|
||||
config = MixinConfig.create(configFile, outer);
|
||||
if (config != null) {
|
||||
Config.allConfigs.put(config.getName(), config);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new MixinInitialisationError("Error initialising mixin config " + configFile, ex);
|
||||
}
|
||||
|
||||
if (config == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String parent = config.get().getParentName();
|
||||
if (!Strings.isNullOrEmpty(parent)) {
|
||||
Config parentConfig;
|
||||
try {
|
||||
parentConfig = Config.create(parent, outer);
|
||||
if (parentConfig != null) {
|
||||
if (!config.get().assignParent(parentConfig)) {
|
||||
config = null;
|
||||
}
|
||||
}
|
||||
} catch (Throwable th) {
|
||||
throw new MixinInitialisationError("Error initialising parent mixin config " + parent + " of " + configFile, th);
|
||||
}
|
||||
if (parentConfig == null) {
|
||||
Config.logger.error("Error encountered initialising mixin config {0}: The parent {1} could not be read.", configFile, parent);
|
||||
}
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
*/
|
||||
package org.spongepowered.asm.mixin.transformer;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -60,6 +61,7 @@ import org.spongepowered.asm.service.IMixinService;
|
||||
import org.spongepowered.asm.service.MixinService;
|
||||
import org.spongepowered.asm.util.VersionNumber;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
@@ -84,6 +86,18 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
|
||||
@SerializedName("maxShiftBy")
|
||||
int maxShiftBy = InjectionPoint.DEFAULT_ALLOWED_SHIFT_BY;
|
||||
|
||||
void mergeFrom(InjectorOptions parent) {
|
||||
if (this.defaultRequireValue == 0) {
|
||||
this.defaultRequireValue = parent.defaultRequireValue;
|
||||
}
|
||||
if ("default".equals(this.defaultGroup)) {
|
||||
this.defaultGroup = parent.defaultGroup;
|
||||
}
|
||||
if (this.maxShiftBy == InjectionPoint.DEFAULT_ALLOWED_SHIFT_BY) {
|
||||
this.maxShiftBy = parent.maxShiftBy;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -98,6 +112,11 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
@SerializedName("requireAnnotations")
|
||||
boolean requireOverwriteAnnotations;
|
||||
|
||||
void mergeFrom(OverwriteOptions parent) {
|
||||
this.conformAccessModifiers |= parent.conformAccessModifiers;
|
||||
this.requireOverwriteAnnotations |= parent.requireOverwriteAnnotations;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,6 +176,18 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
*/
|
||||
private transient Config handle;
|
||||
|
||||
/**
|
||||
* Parent config
|
||||
*/
|
||||
private transient MixinConfig parent;
|
||||
|
||||
/**
|
||||
* Name of the parent configuration, used to allow inheritance of config
|
||||
* options without duplication
|
||||
*/
|
||||
@SerializedName("parent")
|
||||
private String parentName;
|
||||
|
||||
/**
|
||||
* Target selector, eg. @env(DEFAULT)
|
||||
*/
|
||||
@@ -180,15 +211,23 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
* Determines whether failures in this mixin config are considered terminal
|
||||
* errors. Use this setting to indicate that failing to apply a mixin in
|
||||
* this config is a critical error and should cause the game to shutdown.
|
||||
* Uses boxed boolean so that absent entries can be detected and assigned
|
||||
* via parent config where specified.
|
||||
*/
|
||||
@SerializedName("required")
|
||||
private boolean required;
|
||||
private Boolean requiredValue;
|
||||
|
||||
/**
|
||||
* Actual value of required, parsed from the value in the JSON, the
|
||||
* environment options, and the parent config where specified.
|
||||
*/
|
||||
private transient boolean required;
|
||||
|
||||
/**
|
||||
* Configuration priority
|
||||
*/
|
||||
@SerializedName("priority")
|
||||
private int priority = IMixinConfig.DEFAULT_PRIORITY;
|
||||
private int priority = -1;
|
||||
|
||||
/**
|
||||
* Default mixin priority. By default, mixins get a priority of
|
||||
@@ -197,7 +236,7 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
* priority for all mixins in this config to be set to an alternate value.
|
||||
*/
|
||||
@SerializedName("mixinPriority")
|
||||
private int mixinPriority = IMixinConfig.DEFAULT_PRIORITY;
|
||||
private int mixinPriority = -1;
|
||||
|
||||
/**
|
||||
* Package containing all mixins. This package will be monitored by the
|
||||
@@ -278,13 +317,13 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
* Injector options
|
||||
*/
|
||||
@SerializedName("injectors")
|
||||
private InjectorOptions injectorOptions = new InjectorOptions();
|
||||
private InjectorOptions injectorOptions;
|
||||
|
||||
/**
|
||||
* Overwrite options
|
||||
*/
|
||||
@SerializedName("overwrites")
|
||||
private OverwriteOptions overwriteOptions = new OverwriteOptions();
|
||||
private OverwriteOptions overwriteOptions;
|
||||
|
||||
/**
|
||||
* Config plugin, if supplied
|
||||
@@ -296,6 +335,11 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
*/
|
||||
private transient IReferenceMapper refMapper;
|
||||
|
||||
/**
|
||||
* Keep track of initialisation state
|
||||
*/
|
||||
private transient boolean initialised = false;
|
||||
|
||||
/**
|
||||
* Keep track of initialisation state
|
||||
*/
|
||||
@@ -324,13 +368,101 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
private boolean onLoad(IMixinService service, String name, MixinEnvironment fallbackEnvironment) {
|
||||
this.service = service;
|
||||
this.name = name;
|
||||
|
||||
// If parent is specified, don't perform postinit until parent is assigned
|
||||
if (!Strings.isNullOrEmpty(this.parentName)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If no parent, initialise config options
|
||||
this.env = this.parseSelector(this.selector, fallbackEnvironment);
|
||||
this.required &= !this.env.getOption(Option.IGNORE_REQUIRED);
|
||||
this.required = this.requiredValue != null && this.requiredValue.booleanValue() && !this.env.getOption(Option.IGNORE_REQUIRED);
|
||||
this.initPriority(IMixinConfig.DEFAULT_PRIORITY, IMixinConfig.DEFAULT_PRIORITY);
|
||||
|
||||
if (this.injectorOptions == null) {
|
||||
this.injectorOptions = new InjectorOptions();
|
||||
}
|
||||
|
||||
if (this.overwriteOptions == null) {
|
||||
this.overwriteOptions = new OverwriteOptions();
|
||||
}
|
||||
|
||||
return this.postInit();
|
||||
}
|
||||
|
||||
String getParentName() {
|
||||
return this.parentName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by outer initialising agent to assign the parent to this config.
|
||||
* Copies relevant settings from the parent into the local config object
|
||||
* taking into account local overrides.
|
||||
*
|
||||
* @param parentConfig parent config handle
|
||||
* @return true if version check succeeded
|
||||
*/
|
||||
boolean assignParent(Config parentConfig) {
|
||||
if (this.parent != null) {
|
||||
throw new MixinInitialisationError("Mixin config " + this.name + " was already initialised");
|
||||
}
|
||||
|
||||
if (parentConfig.get() == this) {
|
||||
throw new MixinInitialisationError("Mixin config " + this.name + " cannot be its own parent");
|
||||
}
|
||||
|
||||
this.parent = parentConfig.get();
|
||||
|
||||
if (!this.parent.initialised) {
|
||||
throw new MixinInitialisationError("Mixin config " + this.name + " attempted to assign uninitialised parent config."
|
||||
+ " This probably means that there is an indirect loop in the mixin configs: child -> parent -> child");
|
||||
}
|
||||
|
||||
this.env = this.parseSelector(this.selector, this.parent.env);
|
||||
this.required = this.requiredValue == null ? this.parent.required
|
||||
: this.requiredValue.booleanValue() && !this.env.getOption(Option.IGNORE_REQUIRED);
|
||||
|
||||
this.initPriority(this.parent.priority, this.parent.mixinPriority);
|
||||
|
||||
if (this.injectorOptions == null) {
|
||||
this.injectorOptions = this.parent.injectorOptions;
|
||||
} else {
|
||||
this.injectorOptions.mergeFrom(this.parent.injectorOptions);
|
||||
}
|
||||
|
||||
if (this.overwriteOptions == null) {
|
||||
this.overwriteOptions = this.parent.overwriteOptions;
|
||||
} else {
|
||||
this.overwriteOptions.mergeFrom(this.parent.overwriteOptions);
|
||||
}
|
||||
|
||||
this.setSourceFile |= this.parent.setSourceFile;
|
||||
this.verboseLogging |= this.parent.verboseLogging;
|
||||
|
||||
return this.postInit();
|
||||
}
|
||||
|
||||
private void initPriority(int defaultPriority, int defaultMixinPriority) {
|
||||
if (this.priority < 0) {
|
||||
this.priority = defaultPriority;
|
||||
}
|
||||
|
||||
if (this.mixinPriority < 0) {
|
||||
this.priority = defaultMixinPriority;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean postInit() throws MixinInitialisationError {
|
||||
if (this.initialised) {
|
||||
throw new MixinInitialisationError("Mixin config " + this.name + " was already initialised.");
|
||||
}
|
||||
|
||||
this.initialised = true;
|
||||
this.initCompatibilityLevel();
|
||||
this.initInjectionPoints();
|
||||
return this.checkVersion();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private void initCompatibilityLevel() {
|
||||
if (this.compatibility == null) {
|
||||
@@ -404,6 +536,11 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
|
||||
private boolean checkVersion() throws MixinInitialisationError {
|
||||
if (this.version == null) {
|
||||
// If the parent is non-null, then the version check has already been
|
||||
// performed/warned at that level
|
||||
if (this.parent != null && this.parent.version != null) {
|
||||
return true;
|
||||
}
|
||||
this.logger.error("Mixin config {} does not specify \"minVersion\" property", this.name);
|
||||
}
|
||||
|
||||
@@ -624,6 +761,10 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
return this.env;
|
||||
}
|
||||
|
||||
MixinConfig getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.spongepowered.asm.mixin.transformer.IMixinConfig#getName()
|
||||
*/
|
||||
@@ -878,7 +1019,7 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
}
|
||||
return (this.priority - other.priority);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory method, creates a new mixin configuration bundle from the
|
||||
* specified configFile, which must be accessible on the classpath
|
||||
@@ -890,13 +1031,18 @@ final class MixinConfig implements Comparable<MixinConfig>, IMixinConfig {
|
||||
static Config create(String configFile, MixinEnvironment outer) {
|
||||
try {
|
||||
IMixinService service = MixinService.getService();
|
||||
MixinConfig config = new Gson().fromJson(new InputStreamReader(service.getResourceAsStream(configFile)), MixinConfig.class);
|
||||
InputStream resource = service.getResourceAsStream(configFile);
|
||||
if (resource == null) {
|
||||
throw new IllegalArgumentException(String.format("The specified resource '%s' was invalid or could not be read", configFile));
|
||||
}
|
||||
MixinConfig config = new Gson().fromJson(new InputStreamReader(resource), MixinConfig.class);
|
||||
if (config.onLoad(service, configFile, outer)) {
|
||||
return config.getHandle();
|
||||
}
|
||||
return null;
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw ex;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
throw new IllegalArgumentException(String.format("The specified resource '%s' was invalid or could not be read", configFile), ex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user