forked from MinecraftForge/MinecraftMavenizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMappings.java
More file actions
328 lines (271 loc) · 11.3 KB
/
Mappings.java
File metadata and controls
328 lines (271 loc) · 11.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.mcmaven.impl.mappings;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import net.minecraftforge.mcmaven.impl.Mavenizer;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPConfigRepo;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MCPSide;
import net.minecraftforge.mcmaven.impl.repo.mcpconfig.MinecraftTasks;
import net.minecraftforge.mcmaven.impl.util.Artifact;
import net.minecraftforge.mcmaven.impl.util.Constants;
import net.minecraftforge.mcmaven.impl.util.ProcessUtils;
import net.minecraftforge.mcmaven.impl.util.Task;
import net.minecraftforge.mcmaven.impl.util.Util;
import net.minecraftforge.srgutils.IMappingFile;
import net.minecraftforge.srgutils.IRenamer;
import net.minecraftforge.srgutils.IMappingFile.IField;
import net.minecraftforge.srgutils.IMappingFile.IMethod;
import net.minecraftforge.srgutils.IMappingFile.IParameter;
import org.jetbrains.annotations.Nullable;
import de.siegmar.fastcsv.reader.CsvReader;
public class Mappings {
public static final String CHANNEL_ATTR = "net.minecraftforge.mappings.channel";
public static final String VERSION_ATTR = "net.minecraftforge.mappings.version";
protected enum Tasks {
CSVs("srg2names"),
MappedToSrg("mapped2srg"),
MappedToObf("mapped2obf");
private final String name;
private Tasks(String name) {
this.name = name;
}
};
protected record Key(Tasks type, MCPSide side) {}
protected final Map<Key, Task> tasks = new HashMap<>();
private final String channel;
private final @Nullable String version;
public static Mappings of(String mappingsNotation) {
var split = mappingsNotation.split(":", 2);
var channel = split[0];
var version = split.length > 1 ? split[1] : null;
switch (channel) {
case "parchment":
return new ParchmentMappings(version);
case "official":
return new Mappings(channel, version);
case "snapshot":
case "snapshot_nodoc":
case "stable":
case "stable_nodoc":
return new MCPMappings(channel, version);
}
throw new IllegalArgumentException("Unsupported Mappings: " + mappingsNotation);
}
public record Data(Map<String, String> names, Map<String, String> docs) { }
public static Data load(File data) throws IOException {
var names = new HashMap<String, String>();
var docs = new HashMap<String, String>();
try (var zip = new ZipFile(data)) {
var entries = zip.stream().filter(e -> e.getName().endsWith(".csv")).toList();
for (var entry : entries) {
try (var reader = CsvReader.builder().ofNamedCsvRecord(new InputStreamReader(zip.getInputStream(entry)))) {
for (var row : reader) {
var header = row.getHeader();
var obf = header.contains("searge") ? "searge" : "param";
var searge = row.getField(obf);
names.put(searge, row.getField("name"));
if (header.contains("desc")) {
String desc = row.getField("desc");
if (!desc.isBlank())
docs.put(searge, desc);
}
}
}
}
}
return new Data(names, docs);
}
public Mappings(String channel, @Nullable String version) {
this.channel = channel;
this.version = version;
}
public String channel() {
return this.channel;
}
public @Nullable String version() {
return this.version;
}
@Override
public String toString() {
return channel() + (version() == null ? "" : '-' + version());
}
public File getFolder(File root) {
return new File(root, channel() + '/' + version());
}
public boolean isPrimary() {
// This is the 'primary' mapping and thus what we publish as the root artifacts.
// Not as gradle module metadata only variants.
// Basically the thing that looks like a normal maven artifact
return true;
}
public Mappings withMCVersion(String version) {
if (Objects.equals(this.version, version))
return this;
return new Mappings(channel(), version);
}
public Artifact getArtifact(MCPSide side) {
//net.minecraft:mappings_{CHANNEL}:{MCP_VERSION}[-{VERSION}]@zip
var mcpVersion = side.getMCP().getName().getVersion();
var mcVersion = side.getMCP().getConfig().version;
var artifactVersion = mcpVersion;
if (this.version() != null && !mcVersion.equals(this.version()))
artifactVersion += '-' + this.version();
return Artifact.from(Constants.MC_GROUP, "mappings_" + this.channel, artifactVersion)
.withExtension("zip");
}
public Task getCsvZip(MCPSide side) {
var key = new Key(Tasks.CSVs, side);
var ret = tasks.get(key);
if (ret != null)
return ret;
var mc = side.getMCP().getMinecraftTasks();
var srg = side.getTasks().getMappings();
if (MCPConfigRepo.isObfuscated(mc.getVersion())) {
var client = mc.versionFile(MinecraftTasks.MCFile.CLIENT_MAPPINGS);
var server = mc.versionFile(MinecraftTasks.MCFile.SERVER_MAPPINGS);
ret = Task.named("srg2names[" + this + ']',
Task.deps(srg, client, server),
() -> getMappings(side, srg, client, server)
);
} else {
// Create an empty srg->mapped zip file when requested. This is needed by old MCPConfig setups until we bump it to a version that doesn't require the concept of mappings at all
ret = Task.named("srg2names[" + this + "][Empty]", () -> makeEmptyCsv(side));
}
tasks.put(key, ret);
return ret;
}
public Task getMapped2Srg(MCPSide side) {
return getTsrg(side, Tasks.MappedToSrg);
}
public Task getMapped2Obf(MCPSide side) {
return getTsrg(side, Tasks.MappedToObf);
}
private Task getTsrg(MCPSide side, Tasks type) {
var key = new Key(type, side);
var ret = tasks.get(key);
if (ret != null)
return ret;
var srg = side.getTasks().getMappings();
var csv = getCsvZip(side);
ret = Task.named(type.name + '[' + this + ']',
Task.deps(srg, csv),
() -> makeTsrg(side, srg, csv, type == Tasks.MappedToObf)
);
tasks.put(key, ret);
return ret;
}
private File makeEmptyCsv(MCPSide side) {
var root = getFolder(new File(side.getMCP().getBuildFolder(), "data/mapings"));
var output = new File(root, "official.zip");
var cache = Util.cache(output);
if (Mavenizer.checkCache(output, cache))
return output;
if (!output.getParentFile().exists())
output.getParentFile().mkdirs();
byte[] header = String.join(",", "searge", "name", "side", "desc").getBytes(StandardCharsets.UTF_8);
try (var fos = new FileOutputStream(output);
var out = new ZipOutputStream(fos)) {
out.putNextEntry(new ZipEntry("fields.csv"));
out.write(header);
out.closeEntry();
out.putNextEntry(new ZipEntry("methods.csv"));
out.write(header);
out.closeEntry();
} catch (IOException e) {
Util.sneak(e);
}
cache.save();
return output;
}
private File getMappings(MCPSide side, Task srgMappings, Task clientTask, Task serverTask) {
var tool = side.getMCP().getCache().maven().download(Constants.INSTALLER_TOOLS);
var root = getFolder(new File(side.getMCP().getBuildFolder(), "data/mapings"));
var output = new File(root, "official.zip");
var log = new File(root, "official.log");
var mappings = srgMappings.execute();
var client = clientTask.execute();
var server = serverTask.execute();
var cache = Util.cache(output);
cache.add("tool", tool);
cache.add("mappings", mappings);
cache.add("client", client);
cache.add("server", server);
if (Mavenizer.checkCache(output, cache))
return output;
var args = List.of(
"--task",
"MAPPINGS_CSV",
"--srg",
mappings.getAbsolutePath(),
"--client",
client.getAbsolutePath(),
"--server",
server.getAbsolutePath(),
"--output",
output.getAbsolutePath()
);
File jdk;
try {
jdk = side.getMCP().getCache().jdks().get(Constants.INSTALLER_TOOLS_JAVA_VERSION);
} catch (Exception e) {
throw new IllegalStateException("Failed to find JDK for version " + Constants.INSTALLER_TOOLS_JAVA_VERSION, e);
}
var ret = ProcessUtils.runJar(jdk, log.getParentFile(), log, tool, Collections.emptyList(), args);
if (ret.exitCode != 0)
throw new IllegalStateException("Failed to run MCP Step (exit code " + ret.exitCode + "), See log: " + log.getAbsolutePath());
cache.save();
return output;
}
private File makeTsrg(MCPSide side, Task srgTask, Task csvTask, boolean toObf) {
var root = getFolder(new File(side.getMCP().getBuildFolder(), "data/mapings"));
var output = new File(root, channel() + '-' + version + '-' + (toObf ? "srg" : "obf") + ".tsrg.gz");
var srg = srgTask.execute();
var csv = csvTask.execute();
var cache = Util.cache(output)
.add("srg", srg)
.add("csv", csv);
if (Mavenizer.checkCache(output, cache))
return output;
try {
var names = Mappings.load(csv).names();
var map = IMappingFile.load(srg); // obf2srg
if (!toObf)
map = map.reverse().chain(map); // srg2obf + obf2srg = srg2srg
// Now we rename target2mapped
map = map.rename(new IRenamer() {
@Override
public String rename(IField value) {
return names.getOrDefault(value.getMapped(), value.getMapped());
}
@Override
public String rename(IMethod value) {
return names.getOrDefault(value.getMapped(), value.getMapped());
}
@Override
public String rename(IParameter value) {
return names.getOrDefault(value.getMapped(), value.getMapped());
}
});
// Write in reversed == mapped2target
map.write(output.getAbsoluteFile().toPath(), IMappingFile.Format.TSRG2, true);
} catch (IOException e) {
Util.sneak(e);
}
cache.save();
return output;
}
}