-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMappingReader.java
341 lines (296 loc) · 10.2 KB
/
MappingReader.java
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
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 2021 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.mappingio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.mappingio.format.MappingFormat;
import net.fabricmc.mappingio.format.enigma.EnigmaDirReader;
import net.fabricmc.mappingio.format.enigma.EnigmaFileReader;
import net.fabricmc.mappingio.format.intellij.MigrationMapFileReader;
import net.fabricmc.mappingio.format.jobf.JobfFileReader;
import net.fabricmc.mappingio.format.proguard.ProGuardFileReader;
import net.fabricmc.mappingio.format.simple.RecafSimpleFileReader;
import net.fabricmc.mappingio.format.srg.JamFileReader;
import net.fabricmc.mappingio.format.srg.SrgFileReader;
import net.fabricmc.mappingio.format.srg.TsrgFileReader;
import net.fabricmc.mappingio.format.tiny.Tiny1FileReader;
import net.fabricmc.mappingio.format.tiny.Tiny2FileReader;
public final class MappingReader {
private MappingReader() {
}
@Nullable
public static MappingFormat detectFormat(Path path) throws IOException {
if (Files.isDirectory(path)) {
String enigmaExt = "." + MappingFormat.ENIGMA_FILE.fileExt;
AtomicReference<MappingFormat> ret = new AtomicReference<>();
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith(enigmaExt)) {
ret.set(MappingFormat.ENIGMA_DIR);
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
});
return ret.get();
}
try (Reader reader = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8)) {
String fileName = path.getFileName().toString();
int dotIdx = fileName.lastIndexOf('.');
String fileExt = dotIdx >= 0 ? fileName.substring(dotIdx + 1) : null;
return detectFormat(reader, fileExt);
}
}
@Nullable
public static MappingFormat detectFormat(Reader reader) throws IOException {
return detectFormat(reader, null);
}
private static MappingFormat detectFormat(Reader reader, @Nullable String fileExt) throws IOException {
char[] buffer = new char[DETECT_HEADER_LEN];
int pos = 0;
int len;
// Be careful not to close the reader, that's up to the caller.
BufferedReader br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
br.mark(DETECT_HEADER_LEN);
while (pos < buffer.length
&& (len = br.read(buffer, pos, buffer.length - pos)) >= 0) {
pos += len;
}
br.reset();
if (pos < 3) return null;
switch (String.valueOf(buffer, 0, 3)) {
case "v1\t":
return MappingFormat.TINY_FILE;
case "tin":
return MappingFormat.TINY_2_FILE;
case "tsr": // tsrg2 <nsA> <nsB> ..<nsN>
return MappingFormat.TSRG_2_FILE;
case "CLA":
return MappingFormat.ENIGMA_FILE;
case "PK:":
case "CL:":
case "FD:":
case "MD:":
return detectSrgOrXsrg(br, fileExt);
case "CL ":
case "FD ":
case "MD ":
case "MP ":
return MappingFormat.JAM_FILE;
}
String headerStr = String.valueOf(buffer, 0, pos);
if (headerStr.contains("<migrationMap>")) {
return MappingFormat.INTELLIJ_MIGRATION_MAP_FILE;
} else if ((headerStr.startsWith("p ")
|| headerStr.startsWith("c ")
|| headerStr.startsWith("f ")
|| headerStr.startsWith("m "))
&& headerStr.contains(" = ")) {
return MappingFormat.JOBF_FILE;
} else if (headerStr.contains(" -> ")) {
return MappingFormat.PROGUARD_FILE;
} else if (headerStr.contains("\n\t")) {
return MappingFormat.TSRG_FILE;
}
if (fileExt != null) {
if (fileExt.equals(MappingFormat.CSRG_FILE.fileExt)) return MappingFormat.CSRG_FILE;
}
// TODO: Recaf Simple
return null; // format unknown, not easily detectable or corrupted
}
private static MappingFormat detectSrgOrXsrg(BufferedReader reader, @Nullable String fileExt) throws IOException {
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith("FD:")) {
String[] parts = line.split(" ");
if (parts.length < 5
|| isEmptyOrStartsWithHash(parts[3])
|| isEmptyOrStartsWithHash(parts[4])) {
return MappingFormat.SRG_FILE;
}
return MappingFormat.XSRG_FILE;
}
}
return MappingFormat.XSRG_FILE.fileExt.equals(fileExt) ? MappingFormat.XSRG_FILE : MappingFormat.SRG_FILE;
}
private static boolean isEmptyOrStartsWithHash(String string) {
return string.isEmpty() || string.startsWith("#");
}
public static List<String> getNamespaces(Path file) throws IOException {
return getNamespaces(file, null);
}
public static List<String> getNamespaces(Path file, MappingFormat format) throws IOException {
if (format == null) {
format = detectFormat(file);
if (format == null) throw new IOException("invalid/unsupported mapping format");
}
if (format.features().hasNamespaces()) {
try (Reader reader = Files.newBufferedReader(file)) {
return getNamespaces(reader, format);
}
} else {
return Arrays.asList(MappingUtil.NS_SOURCE_FALLBACK, MappingUtil.NS_TARGET_FALLBACK);
}
}
public static List<String> getNamespaces(Reader reader) throws IOException {
return getNamespaces(reader, null);
}
public static List<String> getNamespaces(Reader reader, MappingFormat format) throws IOException {
if (format == null) {
if (!reader.markSupported()) reader = new BufferedReader(reader);
reader.mark(DETECT_HEADER_LEN);
format = detectFormat(reader);
reader.reset();
if (format == null) throw new IOException("invalid/unsupported mapping format");
}
if (format.features().hasNamespaces()) {
checkReaderCompatible(format);
switch (format) {
case TINY_FILE:
return Tiny1FileReader.getNamespaces(reader);
case TINY_2_FILE:
return Tiny2FileReader.getNamespaces(reader);
case TSRG_2_FILE:
return TsrgFileReader.getNamespaces(reader);
default:
throw new IllegalStateException();
}
} else {
return Arrays.asList(MappingUtil.NS_SOURCE_FALLBACK, MappingUtil.NS_TARGET_FALLBACK);
}
}
/**
* Tries to detect the format of the given path and read it.
*
* @param path The path to read from. Can be a file or a directory.
* @param visitor The receiving visitor.
* @throws IOException If the format can't be detected or reading fails.
*/
public static void read(Path path, MappingVisitor visitor) throws IOException {
read(path, null, visitor);
}
/**
* Tries to read the given path using the passed format's reader.
*
* @param path The path to read from. Can be a file or a directory.
* @param format The format to use. Has to match the path's format.
* @param visitor The receiving visitor.
* @throws IOException If reading fails.
*/
public static void read(Path path, MappingFormat format, MappingVisitor visitor) throws IOException {
if (format == null) {
format = detectFormat(path);
if (format == null) throw new IOException("invalid/unsupported mapping format");
}
if (format.hasSingleFile()) {
try (Reader reader = Files.newBufferedReader(path)) {
read(reader, format, visitor);
}
} else {
switch (format) {
case ENIGMA_DIR:
EnigmaDirReader.read(path, visitor);
break;
default:
throw new IllegalStateException();
}
}
}
/**
* Tries to detect the reader's content's format and read it.
*
* @param reader The reader to read from.
* @param visitor The receiving visitor.
* @throws IOException If the format can't be detected or reading fails.
*/
public static void read(Reader reader, MappingVisitor visitor) throws IOException {
read(reader, null, visitor);
}
/**
* Tries to read the reader's content using the passed format's mapping reader.
*
* @param reader The reader to read from.
* @param format The format to use. Has to match the reader's content's format.
* @param visitor The receiving visitor.
* @throws IOException If reading fails.
*/
public static void read(Reader reader, MappingFormat format, MappingVisitor visitor) throws IOException {
if (format == null) {
if (!reader.markSupported()) reader = new BufferedReader(reader);
reader.mark(DETECT_HEADER_LEN);
format = detectFormat(reader);
reader.reset();
if (format == null) throw new IOException("invalid/unsupported mapping format");
}
checkReaderCompatible(format);
switch (format) {
case TINY_FILE:
Tiny1FileReader.read(reader, visitor);
break;
case TINY_2_FILE:
Tiny2FileReader.read(reader, visitor);
break;
case ENIGMA_FILE:
EnigmaFileReader.read(reader, visitor);
break;
case SRG_FILE:
case XSRG_FILE:
SrgFileReader.read(reader, visitor);
break;
case JAM_FILE:
JamFileReader.read(reader, visitor);
break;
case CSRG_FILE:
case TSRG_FILE:
case TSRG_2_FILE:
TsrgFileReader.read(reader, visitor);
break;
case PROGUARD_FILE:
ProGuardFileReader.read(reader, visitor);
break;
case INTELLIJ_MIGRATION_MAP_FILE:
MigrationMapFileReader.read(reader, visitor);
break;
case RECAF_SIMPLE_FILE:
RecafSimpleFileReader.read(reader, visitor);
break;
case JOBF_FILE:
JobfFileReader.read(reader, visitor);
break;
default:
throw new IllegalStateException();
}
}
private static void checkReaderCompatible(MappingFormat format) throws IOException {
if (!format.hasSingleFile()) {
throw new IOException("can't read mapping format "+format.name+" using a Reader, use the Path based API");
}
}
private static final int DETECT_HEADER_LEN = 4096;
}