-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModTrRemapper.java
More file actions
140 lines (116 loc) · 6.45 KB
/
ModTrRemapper.java
File metadata and controls
140 lines (116 loc) · 6.45 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.ModCandidate;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.context.ModRemapperContext;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingTreeHelper;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.mappings.MappingsRegistry;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.minecraft.MinecraftRemapper;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.resource.RefmapRemapper;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.remapper.visitor.MixinPostApplyVisitor;
import io.github.fabriccompatibiltylayers.modremappingapi.impl.utils.FileUtils;
import net.fabricmc.accesswidener.AccessWidenerReader;
import net.fabricmc.accesswidener.AccessWidenerRemapper;
import net.fabricmc.accesswidener.AccessWidenerWriter;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.mappingio.tree.MappingTree;
import net.fabricmc.tinyremapper.NonClassCopyMode;
import net.fabricmc.tinyremapper.OutputConsumerPath;
import net.fabricmc.tinyremapper.TinyRemapper;
import net.fabricmc.tinyremapper.extension.mixin.MixinExtension;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.commons.Remapper;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@ApiStatus.Internal
public class ModTrRemapper {
public static TinyRemapper makeRemapper(ModRemapperContext context) {
MappingsRegistry mappingsRegistry = context.getMappingsRegistry();
List<MappingTree> trees = mappingsRegistry.getRemappingMappings();
TinyRemapper.Builder builder = TinyRemapper
.newRemapper()
.renameInvalidLocals(true)
.ignoreFieldDesc(false)
.propagatePrivate(true)
.ignoreConflicts(true);
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
builder.fixPackageAccess(true);
}
for (MappingTree tree : trees) {
builder.withMappings(MappingTreeHelper.createMappingProvider(tree, mappingsRegistry.getSourceNamespace(), mappingsRegistry.getTargetNamespace()));
}
context.addToRemapperBuilder(builder);
if (context.getRemappingFlags().contains(RemappingFlags.MIXIN)) {
MixinPostApplyVisitor mixinPostApplyVisitor = new MixinPostApplyVisitor();
builder.extraPostApplyVisitor(mixinPostApplyVisitor);
builder.extension(new MixinExtension(EnumSet.of(MixinExtension.AnnotationTarget.HARD)));
}
TinyRemapper remapper = builder.build();
try {
MinecraftRemapper.addMinecraftJar(remapper, mappingsRegistry);
} catch (IOException e) {
throw new RuntimeException(e);
}
context.getLibraryHandler().addLibrariesToRemapClasspath(remapper);
return remapper;
}
public static void remapMods(TinyRemapper remapper, Map<ModCandidate, Path> paths, ModRemapperContext context) {
List<OutputConsumerPath> outputConsumerPaths = new ArrayList<>();
List<OutputConsumerPath.ResourceRemapper> resourceRemappers = new ArrayList<>(NonClassCopyMode.FIX_META_INF.remappers);
resourceRemappers.add(new RefmapRemapper());
Consumer<TinyRemapper> consumer = getRemapperConsumer(paths, context);
TrRemapperHelper.applyRemapper(
remapper,
paths.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().original, Map.Entry::getValue)),
outputConsumerPaths,
resourceRemappers,
true,
context.getMappingsRegistry().getSourceNamespace(),
context.getMappingsRegistry().getTargetNamespace(),
consumer
);
if (context.getRemappingFlags().contains(RemappingFlags.ACCESS_WIDENER)) {
for (Map.Entry<ModCandidate, Path> entry : paths.entrySet()) {
ModCandidate candidate = entry.getKey();
Path jarPath = entry.getValue();
if (candidate.accessWidenerName != null && candidate.accessWidener != null) {
try (FileSystem fs = FileUtils.getJarFileSystem(jarPath)) {
Files.delete(fs.getPath(candidate.accessWidenerName));
Files.write(fs.getPath(candidate.accessWidenerName), candidate.accessWidener);
} catch (Throwable t) {
throw new RuntimeException("Error while writing remapped access widener for '" + candidate.modName + "'", t);
}
}
}
}
}
private static @Nullable Consumer<TinyRemapper> getRemapperConsumer(Map<ModCandidate, Path> paths, ModRemapperContext context) {
Consumer<TinyRemapper> consumer = null;
if (context.getRemappingFlags().contains(RemappingFlags.ACCESS_WIDENER)) {
consumer = (currentRemapper) -> {
for (Map.Entry<ModCandidate, Path> entry : paths.entrySet()) {
ModCandidate candidate = entry.getKey();
if (candidate.accessWidenerName != null) {
try (FileSystem jarFs = FileUtils.getJarFileSystem(candidate.original)) {
candidate.accessWidener = remapAccessWidener(Files.readAllBytes(jarFs.getPath(candidate.accessWidenerName)), currentRemapper.getRemapper(), context.getMappingsRegistry().getTargetNamespace());
} catch (Throwable t) {
throw new RuntimeException("Error while remapping access widener for '" + candidate.modName + "'", t);
}
}
}
};
}
return consumer;
}
private static byte[] remapAccessWidener(byte[] data, Remapper remapper, String targetNamespace) {
AccessWidenerWriter writer = new AccessWidenerWriter();
AccessWidenerRemapper remappingDecorator = new AccessWidenerRemapper(writer, remapper, "intermediary", targetNamespace);
AccessWidenerReader accessWidenerReader = new AccessWidenerReader(remappingDecorator);
accessWidenerReader.read(data, "intermediary");
return writer.write();
}
}