improve speed of findClass
All checks were successful
/ Build BaseBand Loader (push) Successful in 1m23s

This commit is contained in:
Daniella / Tove 2024-06-11 22:18:59 +02:00
parent 30add3041a
commit 88ba90e2b3
Signed by: TudbuT
GPG key ID: B3CF345217F202D3

View file

@ -22,14 +22,16 @@ import java.util.stream.Collectors;
import static com.baseband.launcher.Loader.exit;
public class CustomClassLoader extends ClassLoader implements Util {
private final HashSet<String> names = new HashSet<>();
private final DataKeeper<Map<String, byte[]>> binaryKeeper;
private final DataKeeper<PermissionManager> mixinPermissionManager;
public CustomClassLoader(Map<String, byte[]> data) {
AccessKiller.killReflectionFor(CustomClassLoader.class, CustomMixinServer.class);
names.addAll(data.keySet());
this.binaryKeeper = new DataKeeper<>(
new PermissionOR(new ClassLoaderRestriction(this), new CallClassRestriction(CustomClassLoader.class, CustomClassLoader.CustomMixinServer.class)),
new PermissionOR(new ClassLoaderRestriction(this), new CallClassRestriction(CustomClassLoader.class, CustomMixinServer.class)),
StrictnessBuilder.create()
.property("Restriction.ClassLoader.RestrictLambda", true)
.property("Restriction.ClassLoader.MaxDistance", 5)
@ -41,7 +43,7 @@ public class CustomClassLoader extends ClassLoader implements Util {
PermissionManager mixinRestriction = new CallClassRestriction(MixinTransformer.class);
mixinRestriction.killReflection();
this.mixinPermissionManager = new DataKeeper<>(
new CallClassRestriction(CustomClassLoader.CustomMixinServer.class),
new CallClassRestriction(CustomMixinServer.class),
StrictnessBuilder.create()
.property("Restriction.CallClass.MaxDistance", 4)
.build(),
@ -86,7 +88,7 @@ public class CustomClassLoader extends ClassLoader implements Util {
@Override
protected URL findResource(String name) {
byte[] ownBytes = getBytes(name);
byte[] ownBytes = getResourceBytes(name);
if(ownBytes != null) {
try {
return URLWrapper.wrap(name, ownBytes);
@ -100,12 +102,15 @@ public class CustomClassLoader extends ClassLoader implements Util {
return r;
}
private byte[] getBytes(String name) {
private byte[] getResourceBytes(String name) {
if (!name.endsWith(".class")) {
final byte[][] data = {null};
while(name.startsWith("/"))
name = name.substring(1);
String finalName = name;
if (!names.contains(name)) {
return null;
}
binaryKeeper.access(m -> data[0] = m.getValue().get(finalName));
return data[0];
}
@ -114,6 +119,19 @@ public class CustomClassLoader extends ClassLoader implements Util {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
if (names.contains(name.replace('.', '/') + ".class")) {
Class<?> clazz = loadBBClass(name);
if(clazz != null)
return clazz;
}
try {
return Launch.classLoader.findClass(name);
} catch (ClassNotFoundException e) {
return super.findClass(name);
}
}
private Class<?> loadBBClass(String name) {
Set<ClassLoader> uniqueClassLoaders = Thread.getAllStackTraces().keySet().stream()
.map(Thread::getContextClassLoader)
.filter(Objects::nonNull)
@ -144,11 +162,7 @@ public class CustomClassLoader extends ClassLoader implements Util {
if (bytes[0] != null) {
return defineClass(name, bytes[0], 0, bytes[0].length);
}
try {
return Launch.classLoader.findClass(name);
} catch (ClassNotFoundException e) {
return super.findClass(name);
}
return null;
}
public static class CustomMixinServer extends MixinServiceLaunchWrapper {