Skip to content

Commit

Permalink
Support mixins without refmaps in mod dependencies (#976)
Browse files Browse the repository at this point in the history
* Support mixins without refmaps in mod dependencies

* Fix review concerns

* Add test for MixinDetector

* Change warning to a RuntimeException

* FabricAPITest: Test building without mixin AP

* Deal with Eclipse being stuck in the 2010s and not supporting basic Groovy syntax

* Auto-fix Groovy code format

* Fix FabricAPITest not running

* Fix code style
  • Loading branch information
Juuxel authored Nov 6, 2023
1 parent fd34697 commit 731f072
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jackson = "2.15.2"
guava = "32.1.2-jre"

stitch = "0.6.2"
tiny-remapper = "0.8.9"
tiny-remapper = "0.8.11"
access-widener = "2.1.0"
mapping-io = "0.4.2"
lorenz-tiny = "4.0.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2023 FabricMC
*
* 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 net.fabricmc.loom.configuration.mods;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

import net.fabricmc.loom.LoomGradlePlugin;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.loom.util.fmj.FabricModJsonFactory;

public final class MixinDetector {
public static boolean hasMixinsWithoutRefmap(Path modJar) throws IOException {
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(modJar)) {
final List<String> mixinConfigs = getMixinConfigs(modJar);

if (!mixinConfigs.isEmpty()) {
for (String mixinConfig : mixinConfigs) {
final Path configPath = fs.getPath(mixinConfig);
if (Files.notExists(configPath)) continue;

try (BufferedReader reader = Files.newBufferedReader(configPath)) {
final JsonObject json = LoomGradlePlugin.GSON.fromJson(reader, JsonObject.class);

if (!json.has("refmap")) {
// We found a mixin config with no refmap, exit the loop.
return true;
}
} catch (JsonParseException e) {
throw new RuntimeException("Could not parse mixin config %s from jar %s".formatted(mixinConfig, modJar.toAbsolutePath()), e);
}
}
}

return false;
}
}

private static List<String> getMixinConfigs(Path modJar) {
// Nullable because we don't care here if we can't read it.
// We can just assume there are no mixins.
final FabricModJson fabricModJson = FabricModJsonFactory.createFromZipNullable(modJar);
return fabricModJson != null ? fabricModJson.getMixinConfigurations() : List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.jar.Manifest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -61,6 +63,7 @@
import net.fabricmc.tinyremapper.NonClassCopyMode;
import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.extension.mixin.MixinExtension;

public class ModProcessor {
private static final String fromM = MappingsNamespace.INTERMEDIARY.toString();
Expand Down Expand Up @@ -146,6 +149,10 @@ private void remapJars(List<ModDependency> remapList) throws IOException {
builder.extension(kotlinRemapperClassloader.getTinyRemapperExtension());
}

final Set<InputTag> hasMixinsWithoutRefmaps = new HashSet<>();
// Configure the mixin extension to remap mixins from mod jars detected not to contain refmaps.
builder.extension(new MixinExtension(hasMixinsWithoutRefmaps::contains));

final TinyRemapper remapper = builder.build();

for (Path minecraftJar : extension.getMinecraftJars(MappingsNamespace.INTERMEDIARY)) {
Expand Down Expand Up @@ -173,6 +180,12 @@ private void remapJars(List<ModDependency> remapList) throws IOException {

project.getLogger().debug("Adding " + info.getInputFile() + " as a remap input");

// Note: this is done at a jar level, not at the level of an individual mixin config.
// If a mod has multiple mixin configs, it's assumed that either all or none of them have refmaps.
if (MixinDetector.hasMixinsWithoutRefmap(info.getInputFile())) {
hasMixinsWithoutRefmaps.add(tag);
}

remapper.readInputsAsync(tag, info.getInputFile());
tagMap.put(info, tag);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 FabricMC
* Copyright (c) 2021-2023 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -41,7 +41,7 @@ class FabricAPITest extends Specification implements GradleProjectTestTrait {
private static final String API_VERSION = "0.0.0+loom"

@Unroll
def "build and run (gradle #version)"() {
def "build and run (gradle #version, mixin ap disabled: #disableMixinAp)"() {
setup:
def gradle = gradleProject(
repo: "https://github.com/FabricMC/fabric.git",
Expand All @@ -52,8 +52,20 @@ class FabricAPITest extends Specification implements GradleProjectTestTrait {

gradle.enableMultiProjectOptimisation()

// Disable the mixin ap if needed. Fabric API is a large enough test project to see if something breaks.
def mixinApPatch = ""

if (disableMixinAp) {
mixinApPatch = """
allprojects {
loom.mixin.useLegacyMixinAp = false
}
""".stripIndent()
}

// Set the version to something constant
gradle.buildGradle.text = gradle.buildGradle.text.replace('project.version + "+" + (ENV.GITHUB_RUN_NUMBER ? "" : "local-") + getBranch()', "\"$API_VERSION\"")
gradle.buildGradle.text = gradle.buildGradle.text.replace('project.version + "+" + (ENV.GITHUB_RUN_NUMBER ? "" : "local-") + getBranch()', "\"$API_VERSION\"") + mixinApPatch

def server = ServerRunner.create(gradle.projectDir, "23w33a")
.withMod(gradle.getOutputFile("fabric-api-${API_VERSION}.jar"))
Expand Down Expand Up @@ -83,7 +95,9 @@ class FabricAPITest extends Specification implements GradleProjectTestTrait {
serverResult.successful()
serverResult.output.contains("- fabric-api $API_VERSION")
where:
//version << STANDARD_TEST_VERSIONS
version << [DEFAULT_GRADLE]
[version, disableMixinAp] << [
[DEFAULT_GRADLE],
[false, true]
].combinations()
}
}
129 changes: 129 additions & 0 deletions src/test/groovy/net/fabricmc/loom/test/unit/MixinDetectorTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2023 FabricMC
*
* 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 net.fabricmc.loom.test.unit

import java.nio.file.Path

import groovy.json.JsonOutput
import spock.lang.Specification
import spock.lang.TempDir

import net.fabricmc.loom.configuration.mods.MixinDetector
import net.fabricmc.loom.util.FileSystemUtil

class MixinDetectorTest extends Specification {
@TempDir
Path tempDir

private Path makeJar(Map<String, String> mixinConfigs) {
def path = tempDir.resolve("test.jar")
def fs = FileSystemUtil.getJarFileSystem(path, true)

try {
// Create fabric.mod.json
def fabricModJson = JsonOutput.toJson([
schemaVersion: 1,
id: 'test',
version: '1',
mixins: mixinConfigs.keySet()
])
fs.getPath('fabric.mod.json').text = fabricModJson

// Write all mixin configs
mixinConfigs.forEach { name, content ->
fs.getPath(name).text = content
}
} finally {
fs.close()
}

return path
}

def "jar without mixins has no mixins without refmaps"() {
setup:
def jarPath = makeJar([:])

when:
def hasMixinsWithoutRefmaps = MixinDetector.hasMixinsWithoutRefmap(jarPath)

then:
!hasMixinsWithoutRefmaps // no mixins
}

def "jar with one mixin config with refmap has no mixins without refmaps"() {
setup:
def jarPath = makeJar([
'test.mixins.json': JsonOutput.toJson([
'package': 'com.example.test',
'mixins': ['TestMixin'],
'refmap': 'test-refmap.json'
])
])

when:
def hasMixinsWithoutRefmaps = MixinDetector.hasMixinsWithoutRefmap(jarPath)

then:
!hasMixinsWithoutRefmaps // no mixins with refmaps
}

def "jar with one mixin config without refmap has mixins without refmaps"() {
setup:
def jarPath = makeJar([
'test.mixins.json': JsonOutput.toJson([
'package': 'com.example.test',
'mixins': ['TestMixin']
])
])

when:
def hasMixinsWithoutRefmaps = MixinDetector.hasMixinsWithoutRefmap(jarPath)

then:
hasMixinsWithoutRefmaps // mixins with refmaps
}

def "jar with mixed mixin configs has mixins without refmaps"() {
setup:
def jarPath = makeJar([
'test.mixins.json': JsonOutput.toJson([
'package': 'com.example.test',
'mixins': ['TestMixin']
]),
'test2.mixins.json': JsonOutput.toJson([
'package': 'com.example.test2',
'mixins': ['TestMixin2'],
'refmap': 'test2-refmap.json'
])
])

when:
def hasMixinsWithoutRefmaps = MixinDetector.hasMixinsWithoutRefmap(jarPath)

then:
hasMixinsWithoutRefmaps // mixins with refmaps
}
}

0 comments on commit 731f072

Please sign in to comment.