aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/fabricmc/loom/util/aw2at/Aw2At.java
blob: 06fd7e86d6c0d680dfc656e0a8da9432ba80f88d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * This file is part of fabric-loom, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2021 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.util.aw2at;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import org.cadixdev.at.AccessChange;
import org.cadixdev.at.AccessTransform;
import org.cadixdev.at.AccessTransformSet;
import org.cadixdev.at.ModifierChange;
import org.cadixdev.bombe.type.signature.MethodSignature;
import org.gradle.api.Project;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;

import net.fabricmc.accesswidener.AccessWidenerReader;
import net.fabricmc.accesswidener.AccessWidenerVisitor;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.task.RemapJarTask;

/**
 * Converts AW files to AT files.
 *
 * @author Juuz
 */
public final class Aw2At {
	public static void setup(Project project, RemapJarTask remapJar) {
		LoomGradleExtension extension = LoomGradleExtension.get(project);

		if (extension.getAccessWidenerPath().isPresent()) {
			// Find the relative AW file name
			String awName = null;
			Path awPath = extension.getAccessWidenerPath().get().getAsFile().toPath();
			SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
			SourceSet main = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
			boolean found = false;

			for (File srcDir : main.getResources().getSrcDirs()) {
				Path srcDirPath = srcDir.toPath().toAbsolutePath();

				if (awPath.startsWith(srcDirPath)) {
					awName = srcDirPath.relativize(awPath).toString().replace(File.separator, "/");
					found = true;
					break;
				}
			}

			if (!found) {
				awName = awPath.getFileName().toString();
			}

			remapJar.getAtAccessWideners().add(awName);
		}

		remapJar.getAtAccessWideners().addAll(extension.getForge().getExtraAccessWideners());
	}

	public static AccessTransformSet toAccessTransformSet(BufferedReader reader) throws IOException {
		AccessTransformSet atSet = AccessTransformSet.create();

		new AccessWidenerReader(new AccessWidenerVisitor() {
			@Override
				public void visitClass(String name, AccessWidenerReader.AccessType access, boolean transitive) {
				atSet.getOrCreateClass(name).merge(toAt(access));
			}

			@Override
				public void visitMethod(String owner, String name, String descriptor, AccessWidenerReader.AccessType access, boolean transitive) {
				atSet.getOrCreateClass(owner).mergeMethod(MethodSignature.of(name, descriptor), toAt(access));
			}

			@Override
				public void visitField(String owner, String name, String descriptor, AccessWidenerReader.AccessType access, boolean transitive) {
				atSet.getOrCreateClass(owner).mergeField(name, toAt(access));
			}
		}).read(reader);

		return atSet;
	}

	private static AccessTransform toAt(AccessWidenerReader.AccessType access) {
		return switch (access) {
		// FIXME: This behaviour doesn't match what the actual AW does for methods.
		//   - accessible makes the method final if it was private
		//   - extendable makes the method protected if it was (package-)private
		//   Neither of these can be achieved with Forge ATs without using bytecode analysis.
		//   However, this might be good enough for us. (The effects only apply in prod.)
		case ACCESSIBLE -> AccessTransform.of(AccessChange.PUBLIC);
		case EXTENDABLE -> AccessTransform.of(AccessChange.PUBLIC, ModifierChange.REMOVE);
		case MUTABLE -> AccessTransform.of(ModifierChange.REMOVE);
		};
	}
}