-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mixins without refmaps in mod dependencies (#976)
* 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
Showing
5 changed files
with
236 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/net/fabricmc/loom/configuration/mods/MixinDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/test/groovy/net/fabricmc/loom/test/unit/MixinDetectorTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |