Skip to content

Commit

Permalink
+ getClassBytes, addURL, clearNegativeEntries back to LaunchClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Rongmario committed Dec 27, 2023
1 parent d9ca342 commit 13778b8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
44 changes: 0 additions & 44 deletions src/main/java/com/cleanroommc/bouncepad/BouncepadClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ public BouncepadClassLoader(ClassLoader parentClassLoader) {
this.prepareDebugFolders();
}

@Override
public void addURL(URL url) {
super.addURL(url);
this.sources.add(url);
}

@Override
public Class<?> findClass(final String name) throws ClassNotFoundException {
var clazz = this.loadedClasses.get(name);
Expand Down Expand Up @@ -119,44 +113,6 @@ public Class<?> findClass(final String name) throws ClassNotFoundException {
return clazz;
}

// Copied and modified from above
@Override
public byte[] getClassBytes(String name) throws IOException {
var path = name.replace('.', '/').concat(".class");
var resource = this.findResource(path);
if (resource == null) {
resource = this.getResource(path);
if (resource == null) {
if (DebugOption.EXPLICIT_LOGGING.isOn()) {
Bouncepad.getLogger().debug("Cannot find resource of class: [{}]", name);
}
if (this.renameTransformer == null) {
return null;
}
var transformedName = this.renameTransformer.remapClassName(name);
if (transformedName.equals(name)) {
return null;
} else {
return this.getClassBytes(transformedName);
}
}
}
var classData = new byte[4];
var conn = resource.openConnection();
try (var is = conn.getInputStream()) {
var buffer = new ByteArrayOutputStream();
int read;
while ((read = is.readNBytes(classData, 0, classData.length)) != 0) {
buffer.write(classData, 0, read);
}
classData = buffer.toByteArray();
}
if (DebugOption.EXPLICIT_LOGGING.isOn()) {
Bouncepad.getLogger().debug("Loading [{]]'s byte array from resource: [{}]", name, resource);
}
return classData;
}

protected byte[] transformClassData(String name, byte[] classData) {
if (DebugOption.SAVE_CLASS_BEFORE_ALL_TRANSFORMATIONS.isOn()) {
this.saveClassToDisk(classData, name, BEFORE_ALL_TRANSFORMATIONS_SAVE_FOLDER);
Expand Down
49 changes: 46 additions & 3 deletions src/main/java/net/minecraft/launchwrapper/LaunchClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import com.cleanroommc.bouncepad.Bouncepad;
import com.cleanroommc.bouncepad.DebugOption;
import org.apache.logging.log4j.Level;

@Deprecated(since = "0.5")
Expand Down Expand Up @@ -63,7 +64,6 @@ public LaunchClassLoader(URL[] sources) {

}

@Deprecated(forRemoval = true)
public void registerTransformer(String transformerClassName) {
try {
IClassTransformer transformer = (IClassTransformer) loadClass(transformerClassName).newInstance();
Expand All @@ -76,6 +76,12 @@ public void registerTransformer(String transformerClassName) {
}
}

@Override
public void addURL(URL url) {
super.addURL(url);
this.sources.add(url);
}

public List<URL> getSources() {
return sources;
}
Expand All @@ -93,7 +99,44 @@ public void addTransformerExclusion(String toExclude) {
}

public byte[] getClassBytes(String name) throws IOException {
throw new UnsupportedOperationException("");
var path = name.replace('.', '/').concat(".class");
var resource = this.findResource(path);
if (resource == null) {
resource = this.getResource(path);
if (resource == null) {
if (DebugOption.EXPLICIT_LOGGING.isOn()) {
Bouncepad.getLogger().debug("Cannot find resource of class: [{}]", name);
}
if (this.renameTransformer == null) {
return null;
}
var transformedName = this.renameTransformer.remapClassName(name);
if (transformedName.equals(name)) {
return null;
} else {
return this.getClassBytes(transformedName);
}
}
}
var classData = new byte[4];
var conn = resource.openConnection();
try (var is = conn.getInputStream()) {
var buffer = new ByteArrayOutputStream();
int read;
while ((read = is.readNBytes(classData, 0, classData.length)) != 0) {
buffer.write(classData, 0, read);
}
classData = buffer.toByteArray();
}
if (DebugOption.EXPLICIT_LOGGING.isOn()) {
Bouncepad.getLogger().debug("Loading [{}]'s byte array from resource: [{}]", name, resource);
}
return classData;
}

@Deprecated(since = "0.6")
public void clearNegativeEntries(Set<String> entriesToClear) {
Bouncepad.getLogger().warn("LaunchClassLoader#clearNegativeEntries is deprecated and has no effect as of Bouncepad 0.6.");
}

}

0 comments on commit 13778b8

Please sign in to comment.