|
| 1 | +/* |
| 2 | + * This file is part of Sponge, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) SpongePowered <https://www.spongepowered.org> |
| 5 | + * Copyright (c) contributors |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
| 25 | +package org.spongepowered.gradle.impl; |
| 26 | + |
| 27 | +import dev.architectury.at.AccessChange; |
| 28 | +import dev.architectury.at.AccessTransform; |
| 29 | +import dev.architectury.at.AccessTransformSet; |
| 30 | +import dev.architectury.at.ModifierChange; |
| 31 | +import dev.architectury.at.io.AccessTransformFormats; |
| 32 | +import net.fabricmc.accesswidener.AccessWidenerReader; |
| 33 | +import net.fabricmc.accesswidener.AccessWidenerVisitor; |
| 34 | +import org.cadixdev.bombe.type.signature.MethodSignature; |
| 35 | +import org.gradle.api.DefaultTask; |
| 36 | +import org.gradle.api.GradleException; |
| 37 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 38 | +import org.gradle.api.file.RegularFileProperty; |
| 39 | +import org.gradle.api.tasks.InputFiles; |
| 40 | +import org.gradle.api.tasks.OutputFile; |
| 41 | +import org.gradle.api.tasks.TaskAction; |
| 42 | + |
| 43 | +import java.io.BufferedReader; |
| 44 | +import java.io.BufferedWriter; |
| 45 | +import java.io.File; |
| 46 | +import java.io.IOException; |
| 47 | +import java.nio.file.Files; |
| 48 | +import java.util.Set; |
| 49 | + |
| 50 | +public abstract class ConvertAWToAT extends DefaultTask { |
| 51 | + |
| 52 | + @InputFiles |
| 53 | + public abstract ConfigurableFileCollection getAccessWideners(); |
| 54 | + |
| 55 | + public void accessWideners(Object... paths) { |
| 56 | + this.getAccessWideners().from(paths); |
| 57 | + } |
| 58 | + |
| 59 | + @OutputFile |
| 60 | + public abstract RegularFileProperty getAccessTransformer(); |
| 61 | + |
| 62 | + @TaskAction |
| 63 | + public void convert() { |
| 64 | + final Set<File> awFiles = this.getAccessWideners().getFiles(); |
| 65 | + final File atFile = this.getAccessTransformer().get().getAsFile(); |
| 66 | + |
| 67 | + final AccessTransformSet at = AccessTransformSet.create(); |
| 68 | + |
| 69 | + for (final File awFile : awFiles) { |
| 70 | + try (final BufferedReader reader = Files.newBufferedReader(awFile.toPath())) { |
| 71 | + ConvertAWToAT.convert(reader, at); |
| 72 | + } catch (final IOException e) { |
| 73 | + throw new GradleException("Failed to read access widener: " + awFile, e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + try (final BufferedWriter writer = Files.newBufferedWriter(atFile.toPath())) { |
| 78 | + AccessTransformFormats.FML.write(writer, at); |
| 79 | + } catch (IOException e) { |
| 80 | + throw new GradleException("Failed to write access transformer: " + atFile, e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static void convert(final BufferedReader reader, final AccessTransformSet at) throws IOException { |
| 85 | + new AccessWidenerReader(new AccessWidenerVisitor() { |
| 86 | + @Override |
| 87 | + public void visitClass(final String name, final AccessWidenerReader.AccessType access, final boolean transitive) { |
| 88 | + at.getOrCreateClass(name).merge(ConvertAWToAT.convertEntry(access)); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void visitMethod(final String owner, final String name, final String descriptor, final AccessWidenerReader.AccessType access, final boolean transitive) { |
| 93 | + at.getOrCreateClass(owner).mergeMethod(MethodSignature.of(name, descriptor), ConvertAWToAT.convertEntry(access)); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void visitField(final String owner, final String name, final String descriptor, final AccessWidenerReader.AccessType access, final boolean transitive) { |
| 98 | + at.getOrCreateClass(owner).mergeField(name, ConvertAWToAT.convertEntry(access)); |
| 99 | + } |
| 100 | + }).read(reader); |
| 101 | + } |
| 102 | + |
| 103 | + public static AccessTransform convertEntry(final AccessWidenerReader.AccessType access) { |
| 104 | + return switch (access) { |
| 105 | + case ACCESSIBLE -> AccessTransform.of(AccessChange.PUBLIC); |
| 106 | + case EXTENDABLE, MUTABLE -> AccessTransform.of(AccessChange.PUBLIC, ModifierChange.REMOVE); |
| 107 | + }; |
| 108 | + } |
| 109 | +} |
0 commit comments