2022-07-31 20:41:56 +02:00
|
|
|
/*
|
2023-07-16 12:36:11 +02:00
|
|
|
* Copyright (C) 2022-2023 FalsePattern
|
2022-07-30 15:21:11 +02:00
|
|
|
* All Rights Reserved
|
|
|
|
|
*
|
2023-07-16 12:36:11 +02:00
|
|
|
* The above copyright notice and this permission notice
|
2022-07-30 15:21:11 +02:00
|
|
|
* shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* 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 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-07-25 19:33:39 +02:00
|
|
|
package com.falsepattern.lib.mapping;
|
2022-07-25 18:02:01 +02:00
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
import com.falsepattern.lib.StableAPI;
|
2024-02-08 08:38:58 +01:00
|
|
|
import com.falsepattern.lib.internal.FalsePatternLib;
|
|
|
|
|
import com.falsepattern.lib.internal.Share;
|
2022-08-20 20:08:26 +02:00
|
|
|
import com.falsepattern.lib.internal.asm.CoreLoadingPlugin;
|
2022-07-25 19:33:39 +02:00
|
|
|
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;
|
2022-07-25 18:02:01 +02:00
|
|
|
import com.falsepattern.lib.util.ResourceUtil;
|
|
|
|
|
import lombok.SneakyThrows;
|
|
|
|
|
import lombok.val;
|
2022-08-17 20:08:46 +02:00
|
|
|
import lombok.var;
|
2022-07-25 19:17:47 +02:00
|
|
|
import org.objectweb.asm.tree.FieldInsnNode;
|
|
|
|
|
import org.objectweb.asm.tree.MethodInsnNode;
|
2022-07-25 18:02:01 +02:00
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2022-08-06 13:12:55 +02:00
|
|
|
import java.util.Map;
|
2024-02-08 08:38:58 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2022-07-25 18:02:01 +02:00
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI(since = "0.10.0")
|
2022-07-25 18:02:01 +02:00
|
|
|
public class MappingManager {
|
2024-02-08 08:38:58 +01:00
|
|
|
private static Lookup<UniversalClass> internalLookup;
|
|
|
|
|
private static Lookup<UniversalClass> regularLookup;
|
2022-07-31 20:41:56 +02:00
|
|
|
private static boolean initialized = false;
|
2022-12-27 18:53:39 +01:00
|
|
|
private static final Object MUTEX = new Object();
|
2024-02-08 08:38:58 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
Share.LOG.info("Terminating MappingManager");
|
|
|
|
|
internalLookup = null;
|
|
|
|
|
regularLookup = null;
|
|
|
|
|
initialized = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-16 13:09:05 +02:00
|
|
|
|
2022-07-25 18:02:01 +02:00
|
|
|
@SneakyThrows
|
2022-12-27 18:53:39 +01:00
|
|
|
public static void initialize() {
|
|
|
|
|
synchronized (MUTEX) {
|
|
|
|
|
if (initialized) {
|
2024-02-08 08:38:58 +01:00
|
|
|
lastInitializedAt.set(System.currentTimeMillis());
|
2022-12-27 18:53:39 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
initialized = true;
|
2024-02-08 08:38:58 +01:00
|
|
|
val cleanupThread = new CleanupThread();
|
|
|
|
|
Share.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();
|
2022-07-25 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
2022-07-31 20:41:56 +02:00
|
|
|
public static UniversalClass classForName(NameType nameType, MappingType mappingType, String className)
|
|
|
|
|
throws ClassNotFoundException {
|
2022-07-25 18:02:01 +02:00
|
|
|
initialize();
|
2022-07-25 19:17:47 +02:00
|
|
|
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) {
|
2023-07-16 13:09:05 +02:00
|
|
|
throw new ClassNotFoundException("Could not find class \""
|
|
|
|
|
+ className
|
|
|
|
|
+ "\" with "
|
|
|
|
|
+ nameType.name()
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
+ " name in the "
|
|
|
|
|
+ mappingType.name()
|
|
|
|
|
+ " mapping.");
|
2022-07-25 19:17:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
2022-07-25 20:21:43 +02:00
|
|
|
public static boolean containsClass(NameType nameType, MappingType mappingType, String className) {
|
2022-12-27 18:53:39 +01:00
|
|
|
initialize();
|
2022-07-25 20:21:43 +02:00
|
|
|
switch (nameType) {
|
|
|
|
|
case Internal:
|
|
|
|
|
return internalLookup.containsKey(mappingType, className);
|
|
|
|
|
case Regular:
|
|
|
|
|
return regularLookup.containsKey(mappingType, className);
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Invalid enum value " + nameType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
2022-07-31 20:41:56 +02:00
|
|
|
public static UniversalField getField(FieldInsnNode instruction)
|
|
|
|
|
throws ClassNotFoundException, NoSuchFieldException {
|
2022-12-27 18:53:39 +01:00
|
|
|
initialize();
|
2022-07-25 19:17:47 +02:00
|
|
|
if (!CoreLoadingPlugin.isObfuscated()) {
|
|
|
|
|
try {
|
2022-07-31 20:41:56 +02:00
|
|
|
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getField(MappingType.MCP,
|
|
|
|
|
instruction.name);
|
2022-07-25 19:17:47 +02:00
|
|
|
} catch (ClassNotFoundException e) {
|
2023-07-16 13:09:05 +02:00
|
|
|
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).");
|
2022-07-25 19:17:47 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
2022-07-31 20:41:56 +02:00
|
|
|
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getField(MappingType.SRG,
|
|
|
|
|
instruction.name);
|
2022-07-25 19:17:47 +02:00
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
try {
|
2023-07-16 13:09:05 +02:00
|
|
|
return classForName(NameType.Internal,
|
|
|
|
|
MappingType.Notch,
|
|
|
|
|
instruction.owner).getField(MappingType.Notch, instruction.name);
|
2022-07-25 19:17:47 +02:00
|
|
|
} catch (ClassNotFoundException ex) {
|
2023-07-16 13:09:05 +02:00
|
|
|
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)");
|
2022-07-25 19:17:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 13:12:55 +02:00
|
|
|
@StableAPI.Expose
|
2022-07-25 19:17:47 +02:00
|
|
|
public static UniversalMethod getMethod(MethodInsnNode instruction)
|
|
|
|
|
throws ClassNotFoundException, NoSuchMethodException {
|
2022-12-27 18:53:39 +01:00
|
|
|
initialize();
|
2022-07-25 19:17:47 +02:00
|
|
|
if (!CoreLoadingPlugin.isObfuscated()) {
|
|
|
|
|
try {
|
2022-07-31 20:41:56 +02:00
|
|
|
return classForName(NameType.Internal, MappingType.MCP, instruction.owner).getMethod(MappingType.MCP,
|
|
|
|
|
instruction.name,
|
|
|
|
|
instruction.desc);
|
2022-07-25 19:17:47 +02:00
|
|
|
} catch (ClassNotFoundException e) {
|
2023-07-16 13:09:05 +02:00
|
|
|
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).");
|
2022-07-25 19:17:47 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
2022-07-31 20:41:56 +02:00
|
|
|
return classForName(NameType.Internal, MappingType.SRG, instruction.owner).getMethod(MappingType.SRG,
|
|
|
|
|
instruction.name,
|
|
|
|
|
instruction.desc);
|
2022-07-25 19:17:47 +02:00
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
try {
|
2023-07-16 13:09:05 +02:00
|
|
|
return classForName(NameType.Internal,
|
|
|
|
|
MappingType.Notch,
|
|
|
|
|
instruction.owner).getMethod(MappingType.Notch,
|
|
|
|
|
instruction.name,
|
|
|
|
|
instruction.desc);
|
2022-07-25 19:17:47 +02:00
|
|
|
} catch (ClassNotFoundException ex) {
|
2023-07-16 13:09:05 +02:00
|
|
|
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)");
|
2022-07-25 19:17:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-25 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|