Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
Hide existing Minecraft JARs from getResources, and change exception …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
comp500 committed Jun 22, 2020
1 parent d994f26 commit 7080b69
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '1.0.7'
version = '1.0.8'
group = 'link.infra.jumploader' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = 'jumploader'

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
modGroup=link.infra.jumploader
modVersion=1.0.7
modVersion=1.0.8
modBaseName=jumploader
10 changes: 8 additions & 2 deletions src/main/java/link/infra/jumploader/Jumploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.Set;

public class Jumploader implements ITransformationService {
public static final String VERSION = "1.0.7";
public static final String VERSION = "1.0.8";
public static final String USER_AGENT = "Jumploader/" + VERSION;

private final Logger LOGGER = LogManager.getLogger();
Expand Down Expand Up @@ -184,7 +184,7 @@ public void onLoad(@Nonnull IEnvironment env, @Nonnull Set<String> set) {
Thread.currentThread().setUncaughtExceptionHandler((t, e) -> {
// Do nothing!
});
throw new RuntimeException("Closing main thread, not an exception!"){
throw new ThreadCloseException("Jumploader is closing the main thread, not an error!"){
@Override
public void printStackTrace(PrintStream s) {
// Do nothing!
Expand All @@ -199,6 +199,12 @@ public void printStackTrace(PrintWriter s) {
System.exit(1);
}

private static class ThreadCloseException extends RuntimeException {
public ThreadCloseException(String reason) {
super(reason);
}
}

private List<URL> resolveJars(ConfigFile config, ParsedArguments argsParsed) {
List<ResolvableJar> configuredJars = config.getConfiguredJars();
List<ResolvableJar> downloadRequiredJars = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import link.infra.jumploader.specialcases.ClassBlacklist;
import link.infra.jumploader.specialcases.ClassRedefiner;
import link.infra.jumploader.specialcases.SpecialCaseHandler;
import link.infra.jumploader.specialcases.URLBlacklist;

import java.io.IOException;
import java.net.URL;
Expand All @@ -14,19 +15,23 @@
class JumploaderClassLoader extends URLClassLoader {
private final List<ClassBlacklist> blacklists;
private final List<ClassRedefiner> classRedefiners;
private final List<URLBlacklist> urlBlacklists;
private final ClassLoader parent = JumploaderClassLoader.class.getClassLoader();

public JumploaderClassLoader(URL[] urls, SpecialCaseHandler specialCaseHandler) {
super(urls, JumploaderClassLoader.class.getClassLoader());
this.blacklists = specialCaseHandler.getImplementingCases(ClassBlacklist.class);
this.classRedefiners = specialCaseHandler.getImplementingCases(ClassRedefiner.class);
this.urlBlacklists = specialCaseHandler.getImplementingCases(URLBlacklist.class);
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
// Prioritise self over parent classloader
List<URL> urls = Collections.list(findResources(name));
urls.addAll(Collections.list(parent.getResources(name)));
// Hide blacklisted URLs, as fabric API looks at the back of the list anyway
urls.removeIf(u -> urlBlacklists.stream().filter(b -> b.shouldBlacklistUrl(u)).limit(1).count() > 0);
return Collections.enumeration(urls);
}

Expand All @@ -37,6 +42,7 @@ public URL getResource(String name) {
if (url != null) {
return url;
}
// TODO: use the blacklist in getResource as well?
return parent.getResource(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
import link.infra.jumploader.resources.ParsedArguments;
import link.infra.jumploader.util.ClasspathUtil;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class ForgeJarClasspathModifier implements ClasspathModifier, URLBlacklist {
private Set<Path> blacklistedPaths;

public class ForgeJarClasspathModifier implements ClasspathModifier {
@Override
public boolean shouldApply(List<URL> loadedJars, String mainClass, ParsedArguments gameArguments) {
// Always applies - if you're having problems this you should probably load the
Expand All @@ -18,6 +27,28 @@ public boolean shouldApply(List<URL> loadedJars, String mainClass, ParsedArgumen
@Override
public void modifyClasspath() {
// If a JAR contains the Minecraft Main classes, remove it from the classpath - so Fabric doesn't see it and think the remapper needs to use it
ClasspathUtil.removeMatchingClasses(Arrays.asList("net.minecraft.client.main.Main", "net.minecraft.server.MinecraftServer"));
blacklistedPaths = ClasspathUtil.removeMatchingClasses(Arrays.asList("net.minecraft.client.main.Main", "net.minecraft.server.MinecraftServer"));
}

@Override
public boolean shouldBlacklistUrl(URL url) {
String[] splitUrl = url.getPath().split("!");
if (splitUrl.length != 2) {
return false;
}
Path resPath;
try {
resPath = Paths.get(new URI(splitUrl[0]));
} catch (URISyntaxException e) {
return false;
}
for (Path blacklistedPath : blacklistedPaths) {
try {
if (Files.isSameFile(resPath, blacklistedPath)) {
return true;
}
} catch (IOException ignored) {}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package link.infra.jumploader.specialcases;

import java.net.URL;

public interface URLBlacklist extends SpecialCase {
boolean shouldBlacklistUrl(URL url);
}
5 changes: 4 additions & 1 deletion src/main/java/link/infra/jumploader/util/ClasspathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
public class ClasspathUtil {
private ClasspathUtil() {}

public static void removeMatchingClasses(List<String> classesToTest) {
public static Set<Path> removeMatchingClasses(List<String> classesToTest) {
List<String> classPath = new ArrayList<>(Arrays.asList(System.getProperty("java.class.path").split(File.pathSeparator)));
Set<Path> blacklistedJars = new HashSet<>();

ClassLoader throwawayClassLoader = new ClassLoader() {};
Set<Path> sources = new HashSet<>();
Expand All @@ -28,6 +29,7 @@ public static void removeMatchingClasses(List<String> classesToTest) {
while (iter.hasNext()) {
String currentJar = iter.next();
for (Path matchingSourcePath : sources) {
blacklistedJars.add(matchingSourcePath);
try {
if (Files.isSameFile(Paths.get(currentJar), matchingSourcePath)) {
iter.remove();
Expand All @@ -37,5 +39,6 @@ public static void removeMatchingClasses(List<String> classesToTest) {
}
}
System.setProperty("java.class.path", String.join(File.pathSeparator, classPath));
return blacklistedJars;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"id": "jumploader",
"version": "1.0.7",
"version": "1.0.8",

"name": "Jumploader",
"description": "Allows the use of Fabric mods in Twitch modpacks, by loading Fabric as if it were a Forge mod.",
Expand Down

0 comments on commit 7080b69

Please sign in to comment.