This repository has been archived by the owner on Feb 18, 2021. It is now read-only.
forked from FabricMC/tiny-remapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyUtils.java
182 lines (155 loc) · 6.18 KB
/
TinyUtils.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
/*
* Copyright (C) 2016, 2018 Player, asie
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.fabricmc.tinyremapper;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.zip.GZIPInputStream;
import org.objectweb.asm.commons.Remapper;
public final class TinyUtils {
public static class Mapping {
public final String owner, name, desc;
public Mapping(String owner, String name, String desc) {
this.owner = owner;
this.name = name;
this.desc = desc;
}
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof Mapping)) {
return false;
} else {
Mapping otherM = (Mapping) other;
return owner.equals(otherM.owner) && name.equals(otherM.name) && desc.equals(otherM.desc);
}
}
@Override
public int hashCode() {
return Objects.hash(owner, name, desc);
}
}
private static class SimpleClassMapper extends Remapper {
final Map<String, String> classMap;
public SimpleClassMapper(Map<String, String> map) {
this.classMap = map;
}
@Override
public String map(String typeName) {
return classMap.getOrDefault(typeName, typeName);
}
}
private TinyUtils() {
}
public static IMappingProvider createTinyMappingProvider(final Path mappings, String fromM, String toM) {
return (classMap, fieldMap, methodMap) -> {
try (BufferedReader reader = getMappingReader(mappings.toFile())) {
readInternal(reader, fromM, toM, classMap, fieldMap, methodMap);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.printf("%s: %d classes, %d methods, %d fields%n", mappings.getFileName().toString(), classMap.size(), methodMap.size(), fieldMap.size());
};
}
private static BufferedReader getMappingReader(File file) throws IOException {
InputStream is = new FileInputStream(file);
if (file.getName().endsWith(".gz")) {
is = new GZIPInputStream(is);
}
return new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
}
public static IMappingProvider createTinyMappingProvider(final BufferedReader reader, String fromM, String toM) {
return (classMap, fieldMap, methodMap) -> {
try {
readInternal(reader, fromM, toM, classMap, fieldMap, methodMap);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.printf("%d classes, %d methods, %d fields%n", classMap.size(), methodMap.size(), fieldMap.size());
};
}
private static void readInternal(BufferedReader reader, String fromM, String toM, Map<String, String> classMap, Map<String, String> fieldMap, Map<String, String> methodMap) throws IOException {
TinyUtils.read(reader, fromM, toM, (classFrom, classTo) -> {
classMap.put(classFrom, classTo);
}, (fieldFrom, fieldTo) -> {
fieldMap.put(fieldFrom.owner + "/" + fieldFrom.name + ";;" + fieldFrom.desc, fieldTo.owner + "/" + fieldTo.name);
}, (methodFrom, methodTo) -> {
methodMap.put(methodFrom.owner + "/" + methodFrom.name + methodFrom.desc, methodTo.owner + "/" + methodTo.name);
});
}
public static void read(BufferedReader reader, String from, String to,
BiConsumer<String, String> classMappingConsumer,
BiConsumer<Mapping, Mapping> fieldMappingConsumer,
BiConsumer<Mapping, Mapping> methodMappingConsumer)
throws IOException {
String[] header = reader.readLine().split("\t");
if (header.length <= 1
|| !header[0].equals("v1")) {
throw new IOException("Invalid mapping version!");
}
List<String> headerList = Arrays.asList(header);
int fromIndex = headerList.indexOf(from) - 1;
int toIndex = headerList.indexOf(to) - 1;
if (fromIndex < 0) throw new IOException("Could not find mapping '" + from + "'!");
if (toIndex < 0) throw new IOException("Could not find mapping '" + to + "'!");
Map<String, String> obfFrom = new HashMap<>();
Map<String, String> obfTo = new HashMap<>();
List<String[]> linesStageTwo = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
String[] splitLine = line.split("\t");
if (splitLine.length >= 2) {
if ("CLASS".equals(splitLine[0])) {
classMappingConsumer.accept(splitLine[1 + fromIndex], splitLine[1 + toIndex]);
obfFrom.put(splitLine[1], splitLine[1 + fromIndex]);
obfTo.put(splitLine[1], splitLine[1 + toIndex]);
} else {
linesStageTwo.add(splitLine);
}
}
}
SimpleClassMapper descObfFrom = new SimpleClassMapper(obfFrom);
SimpleClassMapper descObfTo = new SimpleClassMapper(obfTo);
for (String[] splitLine : linesStageTwo) {
if ("FIELD".equals(splitLine[0])) {
String owner = obfFrom.getOrDefault(splitLine[1], splitLine[1]);
String desc = descObfFrom.mapDesc(splitLine[2]);
String tOwner = obfTo.getOrDefault(splitLine[1], splitLine[1]);
String tDesc = descObfTo.mapDesc(splitLine[2]);
fieldMappingConsumer.accept(
new Mapping(owner, splitLine[3 + fromIndex], desc),
new Mapping(tOwner, splitLine[3 + toIndex], tDesc)
);
} else if ("METHOD".equals(splitLine[0])) {
String owner = obfFrom.getOrDefault(splitLine[1], splitLine[1]);
String desc = descObfFrom.mapMethodDesc(splitLine[2]);
String tOwner = obfTo.getOrDefault(splitLine[1], splitLine[1]);
String tDesc = descObfTo.mapMethodDesc(splitLine[2]);
methodMappingConsumer.accept(
new Mapping(owner, splitLine[3 + fromIndex], desc),
new Mapping(tOwner, splitLine[3 + toIndex], tDesc)
);
}
}
}
}