Skip to content

Commit

Permalink
Track source of mixin configs and use source id in conformed names
Browse files Browse the repository at this point in the history
  • Loading branch information
Mumfrey committed Jan 21, 2024
1 parent 7a3cd42 commit f695176
Show file tree
Hide file tree
Showing 17 changed files with 242 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void prepare() {
String mixinConfigs = this.handle.getAttribute(ManifestAttributes.MIXINCONFIGS);
if (mixinConfigs != null) {
for (String config : mixinConfigs.split(",")) {
this.manager.addConfig(config.trim());
this.manager.addConfig(config.trim(), this.handle);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@
import java.util.Locale;
import java.util.Map;

import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.launch.platform.container.IContainerHandle;
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.MixinEnvironment.CompatibilityLevel;
import org.spongepowered.asm.mixin.MixinEnvironment.Phase;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.service.ServiceVersionError;
import org.spongepowered.asm.mixin.Mixins;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigSource;
import org.spongepowered.asm.mixin.throwables.MixinError;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.service.ServiceVersionError;

//import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -166,7 +167,7 @@ public final void prepare(CommandLineOptions args) {
}

for (String config : args.getConfigs()) {
this.addConfig(config);
this.addConfig(config, null);
}
}

Expand Down Expand Up @@ -256,10 +257,10 @@ final void setCompatibilityLevel(String level) {
*
* @param config config resource name, does not require a leading /
*/
final void addConfig(String config) {
final void addConfig(String config, IMixinConfigSource source) {
if (config.endsWith(".json")) {
MixinPlatformManager.logger.debug("Registering mixin config: {}", config);
Mixins.addConfiguration(config);
MixinPlatformManager.logger.debug("Registering mixin config: {} source={}", config, source);
Mixins.addConfiguration(config, source);
} else if (config.contains(".json@")) {
throw new MixinError("Setting config phase via manifest is no longer supported: " + config + ". Specify target in config instead");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public ContainerHandleURI(URI uri) {
this.attributes = MainAttributes.of(uri);
}

@Override
public String getId() {
// Not enough information for this
return null;
}

@Override
public String getDescription() {
return this.uri.toString();
}

/**
* Get the URI for this handle
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public ContainerHandleVirtual(String name) {
this.name = name;
}

@Override
public String getId() {
return this.name;
}

@Override
public String getDescription() {
return this.toString();
}

/**
* Get the name of this container
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@

import java.util.Collection;

import org.spongepowered.asm.mixin.extensibility.IMixinConfigSource;

/**
* Interface for container handles. Previously resources considered by Mixin
* were indexed by URI but in order to provide more flexibility the container
* handle system now wraps URIs in a more expressive object, and provides
* support for both virtual containers and nested container resolution.
*/
public interface IContainerHandle {
public interface IContainerHandle extends IMixinConfigSource {

/**
* Retrieve the value of attribute with the specified name, or null if not
Expand Down
33 changes: 27 additions & 6 deletions src/main/java/org/spongepowered/asm/mixin/Mixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.spongepowered.asm.logging.ILogger;
import org.spongepowered.asm.launch.GlobalProperties;
import org.spongepowered.asm.launch.GlobalProperties.Keys;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigSource;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;
import org.spongepowered.asm.mixin.transformer.ClassInfo;
import org.spongepowered.asm.mixin.transformer.Config;
Expand Down Expand Up @@ -66,15 +67,25 @@ public final class Mixins {

private Mixins() {}

// /**
// * Add multiple configurations
// *
// * @param configFiles config resources to add
// */
// public static void addConfigurations(String... configFiles) {
// Mixins.addConfigurations(configFiles, null);
// }

/**
* Add multiple configurations
*
* @param configFiles config resources to add
* @param source source of the configuration resource
*/
public static void addConfigurations(String... configFiles) {
public static void addConfigurations(String[] configFiles, IMixinConfigSource source) {
MixinEnvironment fallback = MixinEnvironment.getDefaultEnvironment();
for (String configFile : configFiles) {
Mixins.createConfiguration(configFile, fallback);
Mixins.createConfiguration(configFile, fallback, source);
}
}

Expand All @@ -84,20 +95,30 @@ public static void addConfigurations(String... configFiles) {
* @param configFile path to configuration resource
*/
public static void addConfiguration(String configFile) {
Mixins.createConfiguration(configFile, MixinEnvironment.getDefaultEnvironment());
Mixins.createConfiguration(configFile, null, null);
}

/**
* Add a mixin configuration resource
*
* @param configFile path to configuration resource
* @param source source of the configuration resource
*/
public static void addConfiguration(String configFile, IMixinConfigSource source) {
Mixins.createConfiguration(configFile, MixinEnvironment.getDefaultEnvironment(), source);
}

@Deprecated
static void addConfiguration(String configFile, MixinEnvironment fallback) {
Mixins.createConfiguration(configFile, fallback);
Mixins.createConfiguration(configFile, fallback, null);
}

@SuppressWarnings("deprecation")
private static void createConfiguration(String configFile, MixinEnvironment fallback) {
private static void createConfiguration(String configFile, MixinEnvironment fallback, IMixinConfigSource source) {
Config config = null;

try {
config = Config.create(configFile, fallback);
config = Config.create(configFile, fallback, source);
} catch (Exception ex) {
Mixins.logger.error("Error encountered reading mixin config " + configFile + ": " + ex.getClass().getName() + " " + ex.getMessage(), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,22 @@ public interface IMixinConfig {
* @return the config filename (resource name)
*/
public abstract String getName();


/**
* Get the source which initially provide this configuration object, for
* example the jar which specified it.
*
* @return the config source, or null if not provided by the service
*/
public abstract IMixinConfigSource getSource();

/**
* Get the id of the source id with all non-alpha characters removed. If the
* source is null or the source's id is null then this method returns null
* also.
*/
public abstract String getCleanSourceId();

/**
* Get the package containing all mixin classes
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of Mixin, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.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.mixin.extensibility;

/**
* Interface for loaded mixin configurations
*/
public interface IMixinConfigSource {

/**
* Get the identifier for this source
*/
public abstract String getId();

/**
* Plain text description of the config source, can be as descriptive as
* necessary to help the user identify the source, for example could be the
* full path to the source item, or something more descriptive.
*/
public abstract String getDescription();

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.spongepowered.asm.mixin.injection.struct.Target;
import org.spongepowered.asm.mixin.injection.throwables.InjectionError;
import org.spongepowered.asm.mixin.injection.throwables.InvalidSliceException;
import org.spongepowered.asm.mixin.refmap.IMixinContext;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Annotations;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.spongepowered.asm.mixin.injection.struct.InjectionPointData;
import org.spongepowered.asm.mixin.injection.throwables.InvalidInjectionPointException;
import org.spongepowered.asm.service.MixinService;
import org.spongepowered.asm.util.Bytecode;
import org.spongepowered.asm.util.asm.MarkerNode;

/**
Expand Down
36 changes: 31 additions & 5 deletions src/main/java/org/spongepowered/asm/mixin/transformer/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.spongepowered.asm.launch.MixinInitialisationError;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.extensibility.IMixinConfig;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigSource;
import org.spongepowered.asm.service.MixinService;

import com.google.common.base.Strings;
Expand Down Expand Up @@ -129,23 +130,37 @@ public int hashCode() {
return this.name.hashCode();
}

// /**
// * Factory method, create a config from the specified config file and fail
// * over to the specified environment if no selector is present in the config
// *
// * @param configFile config resource
// * @param outer failover environment
// * @return new config or null if invalid config version
// */
// @Deprecated
// public static Config create(String configFile, MixinEnvironment outer) {
// return Config.create(configFile, outer, null);
// }

/**
* Factory method, create a config from the specified config file and fail
* over to the specified environment if no selector is present in the config
*
* @param configFile config resource
* @param outer failover environment
* @param source config source
* @return new config or null if invalid config version
*/
@Deprecated
public static Config create(String configFile, MixinEnvironment outer) {
public static Config create(String configFile, MixinEnvironment outer, IMixinConfigSource source) {
Config config = Config.allConfigs.get(configFile);
if (config != null) {
return config;
}

try {
config = MixinConfig.create(configFile, outer);
config = MixinConfig.create(configFile, outer, source);
if (config != null) {
Config.allConfigs.put(config.getName(), config);
}
Expand All @@ -161,7 +176,7 @@ public static Config create(String configFile, MixinEnvironment outer) {
if (!Strings.isNullOrEmpty(parent)) {
Config parentConfig;
try {
parentConfig = Config.create(parent, outer);
parentConfig = Config.create(parent, outer, source);
if (parentConfig != null) {
if (!config.get().assignParent(parentConfig)) {
config = null;
Expand All @@ -178,14 +193,25 @@ public static Config create(String configFile, MixinEnvironment outer) {
return config;
}

// /**
// * Factory method, create a config from the specified config resource
// *
// * @param configFile config resource
// * @return new config or null if invalid config version
// */
// public static Config create(String configFile) {
// return Config.create(configFile, (IMixinConfigSource)null);
// }

/**
* Factory method, create a config from the specified config resource
*
* @param configFile config resource
* @param source config source
* @return new config or null if invalid config version
*/
public static Config create(String configFile) {
return MixinConfig.create(configFile, MixinEnvironment.getDefaultEnvironment());
public static Config create(String configFile, IMixinConfigSource source) {
return MixinConfig.create(configFile, MixinEnvironment.getDefaultEnvironment(), source);
}

}
Loading

0 comments on commit f695176

Please sign in to comment.