Make access transformer spec hash code consistent

Fixes #119.
This commit is contained in:
Juuz
2023-02-19 03:16:56 +02:00
parent 674572f1df
commit 7c83033d94
3 changed files with 137 additions and 24 deletions

View File

@@ -0,0 +1,65 @@
/*
* 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.accesstransformer;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import net.fabricmc.loom.util.Constants;
import net.fabricmc.loom.util.fmj.FabricModJson;
public interface AccessTransformerEntry {
Reader openReader() throws IOException;
record Standalone(Path path, String hash) implements AccessTransformerEntry {
@Override
public Reader openReader() throws IOException {
return Files.newBufferedReader(path);
}
@Override
public String toString() {
return path.toString();
}
}
record Mod(FabricModJson fmj, String hash) implements AccessTransformerEntry {
@Override
public Reader openReader() throws IOException {
final byte[] bytes = fmj.getSource().read(Constants.Forge.ACCESS_TRANSFORMER_PATH);
return new InputStreamReader(new ByteArrayInputStream(bytes), StandardCharsets.UTF_8);
}
@Override
public String toString() {
return fmj.toString();
}
}
}

View File

@@ -27,6 +27,7 @@ package net.fabricmc.loom.configuration.accesstransformer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
@@ -43,7 +44,6 @@ import dev.architectury.loom.util.TempFiles;
import org.cadixdev.at.AccessTransformSet;
import org.cadixdev.at.io.AccessTransformFormats;
import org.gradle.api.Project;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.FileCollection;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
@@ -65,11 +65,10 @@ public class AccessTransformerJarProcessor implements MinecraftJarProcessor<Acce
private static final Logger LOGGER = Logging.getLogger(AccessTransformerJarProcessor.class);
private final String name;
private final Project project;
private final ConfigurableFileCollection localAccessTransformers;
private final TempFiles tempFiles = new TempFiles();
private final Iterable<File> localAccessTransformers;
@Inject
public AccessTransformerJarProcessor(String name, Project project, ConfigurableFileCollection localAccessTransformers) {
public AccessTransformerJarProcessor(String name, Project project, Iterable<File> localAccessTransformers) {
this.name = name;
this.project = project;
this.localAccessTransformers = localAccessTransformers;
@@ -89,7 +88,7 @@ public class AccessTransformerJarProcessor implements MinecraftJarProcessor<Acce
throw new UncheckedIOException("Could not compute AT hash", e);
}
entries.add(new AccessTransformerEntry(atPath, hash));
entries.add(new AccessTransformerEntry.Standalone(atPath, hash));
}
for (FabricModJson localMod : context.localMods()) {
@@ -104,16 +103,7 @@ public class AccessTransformerJarProcessor implements MinecraftJarProcessor<Acce
}
final String hash = Hashing.sha256().hashBytes(bytes).toString();
final Path atPath;
try {
atPath = tempFiles.file("accesstransformer", ".cfg");
Files.write(atPath, bytes);
} catch (IOException e) {
throw ExceptionUtil.createDescriptiveWrapper(UncheckedIOException::new, "Could not create temporary AT file", e);
}
entries.add(new AccessTransformerEntry(atPath, hash));
entries.add(new AccessTransformerEntry.Mod(localMod, hash));
}
return !entries.isEmpty() ? new Spec(entries) : null;
@@ -121,11 +111,11 @@ public class AccessTransformerJarProcessor implements MinecraftJarProcessor<Acce
@Override
public void processJar(Path jar, Spec spec, ProcessorContext context) throws IOException {
try (tempFiles) {
try (var tempFiles = new TempFiles()) {
LOGGER.lifecycle(":applying project access transformers");
final Path tempInput = tempFiles.file("input", ".jar");
Files.copy(jar, tempInput, StandardCopyOption.REPLACE_EXISTING);
final Path atPath = mergeAndRemapAccessTransformers(context, spec.accessTransformers());
final Path atPath = mergeAndRemapAccessTransformers(context, spec.accessTransformers(), tempFiles);
executeAt(project, tempInput, jar, args -> {
args.add("--atFile");
@@ -136,14 +126,14 @@ public class AccessTransformerJarProcessor implements MinecraftJarProcessor<Acce
}
}
private Path mergeAndRemapAccessTransformers(ProcessorContext context, List<AccessTransformerEntry> accessTransformers) throws IOException {
private Path mergeAndRemapAccessTransformers(ProcessorContext context, List<AccessTransformerEntry> accessTransformers, TempFiles tempFiles) throws IOException {
AccessTransformSet accessTransformSet = AccessTransformSet.create();
for (AccessTransformerEntry entry : accessTransformers) {
try {
accessTransformSet.merge(AccessTransformFormats.FML.read(entry.path()));
try (Reader reader = entry.openReader()) {
accessTransformSet.merge(AccessTransformFormats.FML.read(reader));
} catch (IOException e) {
throw new IOException("Could not read access transformer " + entry.path(), e);
throw new IOException("Could not read access transformer " + entry, e);
}
}
@@ -193,7 +183,4 @@ public class AccessTransformerJarProcessor implements MinecraftJarProcessor<Acce
public record Spec(List<AccessTransformerEntry> accessTransformers) implements MinecraftJarProcessor.Spec {
}
private record AccessTransformerEntry(Path path, String hash) {
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.forge
import net.fabricmc.loom.api.processor.SpecContext
import net.fabricmc.loom.configuration.accesstransformer.AccessTransformerJarProcessor
import net.fabricmc.loom.util.fmj.FabricModJsonFactory
import org.gradle.api.Project
import spock.lang.Specification
import spock.lang.TempDir
import java.nio.file.Files
import java.nio.file.Path
class AccessTransformerJarProcessorTest extends Specification {
private static final String TEST_ACCESS_TRANSFORMER = 'public-f net.minecraft.world.level.block.IronBarsBlock m_54217_(Lnet/minecraft/world/level/block/state/BlockState;Z)Z'
@TempDir
Path tempDir
def "consistent spec hash"() {
given:
// Set up mods.toml and access transformer
def metaInf = tempDir.resolve('META-INF')
Files.createDirectory(metaInf)
metaInf.resolve('accesstransformer.cfg').text = TEST_ACCESS_TRANSFORMER
metaInf.resolve('mods.toml').text = '[[mods]]\nmodId="hello"'
// Create processor and context
def processor = new AccessTransformerJarProcessor('at', Mock(Project), [])
def modJson = FabricModJsonFactory.createFromDirectory(tempDir)
def context = Mock(SpecContext)
context.localMods() >> [modJson]
when:
def spec = processor.buildSpec(context)
then:
spec.hashCode() == 1575235360
}
}