|
| 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 | +} |
0 commit comments