-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathMappingManager.java
More file actions
217 lines (207 loc) · 10.4 KB
/
MappingManager.java
File metadata and controls
217 lines (207 loc) · 10.4 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
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib 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, only version 3 of the License.
*
* FalsePatternLib 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 FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.mapping;
import com.falsepattern.lib.internal.FPLog;
import com.falsepattern.lib.internal.asm.CoreLoadingPlugin;
import com.falsepattern.lib.mapping.storage.Lookup;
import com.falsepattern.lib.mapping.types.MappingType;
import com.falsepattern.lib.mapping.types.NameType;
import com.falsepattern.lib.mapping.types.UniversalClass;
import com.falsepattern.lib.mapping.types.UniversalField;
import com.falsepattern.lib.mapping.types.UniversalMethod;
import com.falsepattern.lib.util.ResourceUtil;
import lombok.SneakyThrows;
import lombok.val;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicLong;
public class MappingManager {
private static Lookup<UniversalClass> internalLookup;
private static Lookup<UniversalClass> regularLookup;
private static boolean initialized = false;
private static final Object MUTEX = new Object();
private static final AtomicLong lastInitializedAt = new AtomicLong();
private static class CleanupThread extends Thread {
public CleanupThread() {
setName("MappingManager Cleanup Watchdog");
setDaemon(true);
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
synchronized (MUTEX) {
long time = System.currentTimeMillis();
long lastInit = lastInitializedAt.get();
if (time - lastInit < 10_000) {
continue;
}
FPLog.LOG.info("Terminating MappingManager");
internalLookup = null;
regularLookup = null;
initialized = false;
return;
}
}
}
}
@SneakyThrows
public static void initialize() {
synchronized (MUTEX) {
if (initialized) {
lastInitializedAt.set(System.currentTimeMillis());
return;
}
initialized = true;
val cleanupThread = new CleanupThread();
FPLog.LOG.info("Initializing MappingManager");
internalLookup = new Lookup<>();
regularLookup = new Lookup<>();
val stringPool = new HashMap<String, String>();
val classMappings =
new String(ResourceUtil.getResourceBytesFromJar("/classes.csv", CoreLoadingPlugin.class)).split("\n");
for (int i = 1; i < classMappings.length; i++) {
val line = classMappings[i].split(",");
val clazz = new UniversalClass(line, stringPool);
internalLookup.unwrap(clazz.internalName, clazz);
regularLookup.unwrap(clazz.regularName, clazz);
}
var fieldMappings =
new String(ResourceUtil.getResourceBytesFromJar("/fields.csv", CoreLoadingPlugin.class)).split("\n");
for (int i = 1; i < fieldMappings.length; i++) {
val line = fieldMappings[i].split(",");
val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/')));
UniversalField.createAndAddToParent(clazz, line, stringPool);
}
val methodMappings =
new String(ResourceUtil.getResourceBytesFromJar("/methods.csv", CoreLoadingPlugin.class)).split("\n");
for (int i = 1; i < methodMappings.length; i++) {
val line = methodMappings[i].split(",");
val clazz = internalLookup.get(MappingType.Notch, line[0].substring(0, line[0].lastIndexOf('/')));
UniversalMethod.createAndAddToParent(clazz, line, stringPool);
}
lastInitializedAt.set(System.currentTimeMillis());
cleanupThread.start();
}
}
public static UniversalClass classForName(NameType nameType, MappingType mappingType, String className)
throws ClassNotFoundException {
initialize();
try {
switch (nameType) {
case Internal:
return internalLookup.get(mappingType, className);
case Regular:
return regularLookup.get(mappingType, className);
default:
throw new IllegalArgumentException("Invalid enum value " + nameType);
}
} catch (Lookup.LookupException e) {
throw new ClassNotFoundException("Could not find class \""
+ className
+ "\" with "
+ nameType.name()
.toLowerCase()
+ " name in the "
+ mappingType.name()
+ " mapping.");
}
}
public static boolean containsClass(NameType nameType, MappingType mappingType, String className) {
initialize();
switch (nameType) {
case Internal:
return internalLookup.containsKey(mappingType, className);
case Regular:
return regularLookup.containsKey(mappingType, className);
default:
throw new IllegalArgumentException("Invalid enum value " + nameType);
}
}
public static UniversalField getField(FieldInsnNode instruction)
throws ClassNotFoundException, NoSuchFieldException {
initialize();
if (!CoreLoadingPlugin.isObfuscated()) {
try {
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getField(MappingType.MCP,
instruction.name);
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("Could not find the class "
+ instruction.owner
+ " in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
}
} else {
try {
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getField(MappingType.SRG,
instruction.name);
} catch (ClassNotFoundException e) {
try {
return classForName(NameType.Internal,
MappingType.Notch,
instruction.owner).getField(MappingType.Notch, instruction.name);
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("Could not find the class "
+ instruction.owner
+ " neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
}
}
}
}
public static UniversalMethod getMethod(MethodInsnNode instruction)
throws ClassNotFoundException, NoSuchMethodException {
initialize();
if (!CoreLoadingPlugin.isObfuscated()) {
try {
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getMethod(MappingType.MCP,
instruction.name,
instruction.desc);
} catch (ClassNotFoundException e) {
throw new ClassNotFoundException("Could not find the class "
+ instruction.owner
+ " in the MCP mappings. Are you sure it's a Minecraft class? (we're in dev, cannot use SRG or Notch here).");
}
} else {
try {
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getMethod(MappingType.SRG,
instruction.name,
instruction.desc);
} catch (ClassNotFoundException e) {
try {
return classForName(NameType.Internal,
MappingType.Notch,
instruction.owner).getMethod(MappingType.Notch,
instruction.name,
instruction.desc);
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("Could not find the class "
+ instruction.owner
+ " neither in the SRG nor in the Notch mappings. Are you sure it's a Minecraft class? (we're in obf, cannot use MCP here)");
}
}
}
}
}