Skip to content

Commit daf7dc0

Browse files
committed
Fix XSRG detection
1 parent 94546a1 commit daf7dc0

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

src/main/java/net/fabricmc/mappingio/MappingReader.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,47 +54,51 @@ public static MappingFormat detectFormat(Reader reader) throws IOException {
5454
int pos = 0;
5555
int len;
5656

57-
while (pos < buffer.length
58-
&& (len = reader.read(buffer, pos, buffer.length - pos)) >= 0) {
59-
pos += len;
60-
}
57+
try (BufferedReader br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader)) {
58+
br.mark(DETECT_HEADER_LEN);
6159

62-
if (pos < 3) return null;
63-
64-
switch (String.valueOf(buffer, 0, 3)) {
65-
case "v1\t":
66-
return MappingFormat.TINY_FILE;
67-
case "tin":
68-
return MappingFormat.TINY_2_FILE;
69-
case "tsr": // tsrg2 <nsA> <nsB> ..<nsN>
70-
return MappingFormat.TSRG_2_FILE;
71-
case "CLA":
72-
return MappingFormat.ENIGMA_FILE;
73-
case "PK:":
74-
case "CL:":
75-
case "MD:":
76-
case "FD:":
77-
return detectSrgOrXsrg(reader);
78-
}
60+
while (pos < buffer.length
61+
&& (len = br.read(buffer, pos, buffer.length - pos)) >= 0) {
62+
pos += len;
63+
}
64+
65+
br.reset();
66+
if (pos < 3) return null;
67+
68+
switch (String.valueOf(buffer, 0, 3)) {
69+
case "v1\t":
70+
return MappingFormat.TINY_FILE;
71+
case "tin":
72+
return MappingFormat.TINY_2_FILE;
73+
case "tsr": // tsrg2 <nsA> <nsB> ..<nsN>
74+
return MappingFormat.TSRG_2_FILE;
75+
case "CLA":
76+
return MappingFormat.ENIGMA_FILE;
77+
case "PK:":
78+
case "CL:":
79+
case "MD:":
80+
case "FD:":
81+
return detectSrgOrXsrg(br);
82+
}
7983

80-
String headerStr = String.valueOf(buffer, 0, pos);
84+
String headerStr = String.valueOf(buffer, 0, pos);
8185

82-
if (headerStr.contains(" -> ")) {
83-
return MappingFormat.PROGUARD_FILE;
84-
} else if (headerStr.contains("\n\t")) {
85-
return MappingFormat.TSRG_FILE;
86-
}
86+
if (headerStr.contains(" -> ")) {
87+
return MappingFormat.PROGUARD_FILE;
88+
} else if (headerStr.contains("\n\t")) {
89+
return MappingFormat.TSRG_FILE;
90+
}
8791

88-
// TODO: CSRG
92+
// TODO: CSRG
8993

90-
return null; // unknown format or corrupted
94+
return null; // unknown format or corrupted
95+
}
9196
}
9297

93-
private static MappingFormat detectSrgOrXsrg(Reader reader) throws IOException {
94-
BufferedReader br = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
98+
private static MappingFormat detectSrgOrXsrg(BufferedReader reader) throws IOException {
9599
String line;
96100

97-
while ((line = br.readLine()) != null) {
101+
while ((line = reader.readLine()) != null) {
98102
if (line.startsWith("FD:")) {
99103
String[] parts = line.split(" ");
100104

0 commit comments

Comments
 (0)