Skip to content

Commit 0ec173f

Browse files
authored
Add enableMixinRefmaps, as a helper for mapping related Mixin Magic. See PR for more details. (#37)
1 parent 5c19130 commit 0ec173f

File tree

6 files changed

+556
-0
lines changed

6 files changed

+556
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.renamer.gradle;
6+
7+
import java.util.Locale;
8+
9+
import org.gradle.api.Action;
10+
import org.gradle.api.provider.ListProperty;
11+
import org.gradle.api.provider.Provider;
12+
import org.gradle.api.tasks.SourceSet;
13+
import org.gradle.api.tasks.TaskProvider;
14+
import org.gradle.api.tasks.bundling.Jar;
15+
16+
public interface MixinConfig extends MixinSourceSetConfig {
17+
ListProperty<String> getConfigs();
18+
19+
/**
20+
* {@inheritDoc}
21+
* Add a Mixin config
22+
* @param name path to config file, searches from {@code resources/}
23+
*/
24+
default void config(String name) {
25+
getConfigs().add(name);
26+
}
27+
28+
/**
29+
* {@inheritDoc}
30+
* Add a Mixin config
31+
* @param name path to config file, searches from {@code resources/}
32+
*/
33+
default void config(Provider<String> name) {
34+
getConfigs().add(name);
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
* Add a source set to Mixin, automatically infers refmap name
40+
* @param source source set to add
41+
*/
42+
default MixinSourceSetConfig source(SourceSet source) {
43+
return source(source, source.getName().toLowerCase(Locale.ENGLISH));
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
* Add and source set to Mixin and configure it, automatically infers refmap name
49+
* @param source source set to add
50+
* @param action configuration for the source set
51+
*/
52+
default MixinSourceSetConfig source(SourceSet source, Action<? super MixinSourceSetConfig> action) {
53+
return source(source, source.getName().toLowerCase(Locale.ENGLISH), action);
54+
}
55+
56+
/**
57+
* {@inheritDoc}
58+
* Add a source set to Mixin
59+
* @param source source set to add
60+
* @param name prefix to the output refmap.json file
61+
*/
62+
default MixinSourceSetConfig source(SourceSet source, String name) {
63+
return source(source, name, cfg -> {});
64+
}
65+
66+
/**
67+
* {@inheritDoc}
68+
* Add and source set to Mixin and configure it
69+
* @param source source set to add
70+
* @param name prefix to the output refmap.json file
71+
* @param action configuration for the source set
72+
*/
73+
MixinSourceSetConfig source(SourceSet source, String name, Action<? super MixinSourceSetConfig> action);
74+
75+
void run(Object runConfig);
76+
void jar(TaskProvider<Jar> provider);
77+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.renamer.gradle;
6+
7+
import java.io.File;
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import javax.inject.Inject;
13+
14+
import org.apache.log4j.LogManager;
15+
import org.apache.log4j.Logger;
16+
import org.codehaus.groovy.runtime.InvokerHelper;
17+
import org.gradle.api.Action;
18+
import org.gradle.api.provider.Provider;
19+
import org.gradle.api.tasks.SourceSet;
20+
import org.gradle.api.tasks.TaskProvider;
21+
import org.gradle.api.tasks.bundling.Jar;
22+
23+
abstract class MixinConfigImpl implements MixinConfig {
24+
private static final Logger LOGGER = LogManager.getLogger(MixinConfig.class);
25+
private final RenamerExtensionImpl ext;
26+
private final Map<String, MixinSourceSetConfigImpl> sourceSets = new HashMap<>();
27+
28+
@Inject
29+
public MixinConfigImpl(RenamerExtensionImpl ext) {
30+
this.ext = ext;
31+
this.getMappingTypes().convention(List.of("tsrg"));
32+
33+
/*
34+
* Mixin Magic:
35+
* Run Configs:
36+
* Args:
37+
* --mixin.config <filename> - the Mixin config file, I think users should manually specify this
38+
*
39+
* System Property:
40+
* mixin.env.disableRefMap = true - Disable ref maps. Would be the preferred option once Renamer supports renaming mixins
41+
* mixin.env.remapRefMap = true/false - True if we need to remap refmaps,
42+
* mixin.env.refMapRemappingFile = <map file> - The map needs to be Dependency runtime -> Dev Runtime So typically SRG->Named
43+
*
44+
* Compile:
45+
* -AreobfTsrgFile=
46+
* -AoutTsrgFile=
47+
* -AoutRefMapfile=
48+
* -AdisableTargetValidator=true
49+
* -AdisableTargetExport=true
50+
* -AdisableOverwriteChecker=true
51+
* -Aquiet=true
52+
* -DshowMessageTypes=true
53+
* -AoverwriteErrorLevel=level
54+
* -AdefaultObfuscationEnv=searge
55+
* -AmappingTypes=tsrg
56+
* -Atokens=key=value;
57+
* -AreobfTsrgFiles=ExtraMapping.tsrg;
58+
* -AMSG_{NAME}={value}
59+
* Not Supported:
60+
* -ApluginVersion=MixinGradlePlugin.VERSION
61+
* -AdependencyTargetsFile
62+
*/
63+
}
64+
65+
@Override
66+
public MixinSourceSetConfig source(SourceSet source, String name, Action<? super MixinSourceSetConfig> action) {
67+
if (this.sourceSets.containsKey(source.getName()))
68+
throw new IllegalStateException("Can not register the same sourceset multiple times");
69+
LOGGER.info("[Renamer][Mixin] Configuring SourceSet: " + source.getName());
70+
var ret = ext.getProject().getObjects().newInstance(MixinSourceSetConfigImpl.class, this.ext, this, name, source);
71+
this.sourceSets.put(source.getName(), ret);
72+
action.execute(ret);
73+
return ret;
74+
}
75+
76+
@Override
77+
public void run(Object runConfig) {
78+
LOGGER.info("[Renamer][Mixin] Configuring Run: " + InvokerHelper.getProperty(runConfig, "name"));
79+
InvokerHelper.invokeMethod(runConfig, "args", this.getConfigs().map(list -> {
80+
var ret = new ArrayList<String>(list.size() * 2);
81+
for (var cfg : list) {
82+
ret.add("--mixin.config");
83+
ret.add(cfg);
84+
}
85+
return ret;
86+
}));
87+
//InvokerHelper.invokeMethod(runConfig, "systemProperty", new String[]{"mixin.env.disableRefMap", "true"});
88+
InvokerHelper.invokeMethod(runConfig, "systemProperties", this.ext.mixinMappings.flatMap(task -> task.getOutput()).map(
89+
file -> Map.of(
90+
"mixin.env.remapRefMap", "true",
91+
"mixin.env.refMapRemappingFile", file.getAsFile().getAbsolutePath()
92+
)
93+
));
94+
}
95+
96+
@Override
97+
public void jar(TaskProvider<Jar> provider) {
98+
provider.configure(task -> {
99+
// Add MixinConfigs Manifest entry
100+
if (!task.getManifest().getAttributes().containsKey("MixinConfigs"))
101+
task.getManifest().attributes(Map.of("MixinConfigs", String.join(",", this.getConfigs().get())));
102+
103+
// Add the refmap files
104+
for (var inst : this.sourceSets.values()) {
105+
var ext = inst.sourceSet.getExtensions().getExtraProperties();
106+
@SuppressWarnings("unchecked")
107+
var file = (Provider<File>)ext.get("refMapFile");
108+
LOGGER.info("[Renamer][Mixin] Adding " + inst.getRefMap().get() + " to " + task.getName() + " from " + inst.sourceSet.getCompileJavaTaskName());
109+
task.from(file, cfg -> cfg.rename(name -> inst.getRefMap().get()));
110+
}
111+
});
112+
}
113+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.renamer.gradle;
6+
7+
import org.gradle.api.file.ConfigurableFileCollection;
8+
import org.gradle.api.provider.ListProperty;
9+
import org.gradle.api.provider.MapProperty;
10+
import org.gradle.api.provider.Property;
11+
12+
public interface MixinSourceSetConfig {
13+
/**
14+
* {@inheritDoc}
15+
* The reference mapping for this source set.
16+
* Will inform Mixin of which symbols are mapped to which names at runtime
17+
*/
18+
Property<String> getRefMap();
19+
20+
/**
21+
* {@inheritDoc}
22+
* Stops the Mixin annotation processor from throwing an error when it finds an invalid Mixin target
23+
*/
24+
Property<Boolean> getDisableTargetValidator();
25+
26+
/**
27+
* {@inheritDoc}
28+
* Prevents the annotation processor from creating a small {@code .csv} file with data pertaining to annotation processing.
29+
* Keeping this on will make the AP run slightly faster, but may be worth turning off if you are getting AP file access errors
30+
*/
31+
Property<Boolean> getDisableTargetExport();
32+
33+
/**
34+
* {@inheritDoc}
35+
* By default, when a Mixin target is annotated with {@code @Overwrite}, it is enforced that a Javadoc block above it
36+
* contains at least an {@code @author} and {@code @reason} tag. Turning this off will prevent Mixin from complaining
37+
* (ignore, warn or error, see {@link #getOverwriteErrorLevel()}) in such a case where these tags are missing
38+
*/
39+
Property<Boolean> getDisableOverwriteChecker();
40+
41+
/**
42+
* {@inheritDoc}
43+
* The level of complaining the Mixin annotation processor will do when it finds an {@code @Overwrite} annotated target
44+
* lacking the {@code @author} and {@code @reason} tags. Can be any of the following:
45+
* <ul>
46+
* <li>{@code ignore}: Do nothing</li>
47+
* <li>{@code warning}: Write a warning message to the console</li>
48+
* <li>{@code error}: Throw an error</li>
49+
* </ul>
50+
*/
51+
Property<String> getOverwriteErrorLevel();
52+
53+
/**
54+
* {@inheritDoc}
55+
* The current workspace mapping type, used so that Mixin can translate your obfuscated names for creating the refmap
56+
*/
57+
Property<String> getDefaultObfuscationEnv();
58+
59+
/**
60+
* {@inheritDoc}
61+
* The other half of {@link #getDefaultObfuscationEnv()}, the output mapping type to translate to for creating entries in the refmap
62+
*/
63+
ListProperty<String> getMappingTypes();
64+
65+
/**
66+
* {@inheritDoc}
67+
* Extra data passed to the compiler as key-value pairs. These extra data can then be used in annotation parameters.
68+
* <p>
69+
* Example :{@code @Inject(method = "foo" at = @At("HEAD"), constraints="myToken(myValue)"}
70+
*/
71+
MapProperty<String, String> getTokens();
72+
73+
/**
74+
* {@inheritDoc}
75+
* Extra mapping files to use for creating refmaps, usually for obfuscated external dependencies
76+
*/
77+
ConfigurableFileCollection getExtraMappings();
78+
79+
/**
80+
* {@inheritDoc}
81+
* "Quietens" the console by suppressing trivial and often unneeded messages
82+
*/
83+
Property<Boolean> getQuiet();
84+
85+
/**
86+
* {@inheritDoc}
87+
* Prefix mixin logs with the type of message, such as {@code [MIXIN_0100]} which refers to a missing {@code @author} tag
88+
*/
89+
Property<Boolean> getShowMessageTypes();
90+
91+
/**
92+
* {@inheritDoc}
93+
* Which message types to hide, warn about or simply display. These can be seen by enabling {@link #getShowMessageTypes()}
94+
* <p>
95+
* Example: {@code "MIXIN_0100": "ignore"}
96+
*/
97+
MapProperty<String, String> getMessages();
98+
}

0 commit comments

Comments
 (0)