-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add masking of classpath resources for in-dev
- Loading branch information
Showing
18 changed files
with
604 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
loader/src/main/java/cpw/mods/modlauncher/ResourceMaskingClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package cpw.mods.modlauncher; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.util.Enumeration; | ||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import net.neoforged.fml.util.ClasspathResourceUtils; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class ResourceMaskingClassLoader extends ClassLoader { | ||
static { | ||
ClassLoader.registerAsParallelCapable(); | ||
} | ||
|
||
private final Set<Path> maskedClasspathElements; | ||
|
||
public ResourceMaskingClassLoader(ClassLoader parent, Set<Path> maskedClasspathElements) { | ||
super(Objects.requireNonNull(parent, "parent")); | ||
this.maskedClasspathElements = maskedClasspathElements; | ||
} | ||
|
||
@Override | ||
public @Nullable URL getResource(String name) { | ||
// This is a very tricky thing: if the resource points to one of the filtered paths, | ||
// We need to use getResources() and find the next resource *not* matching the filter. | ||
var resource = getParent().getResource(name); | ||
if (resource == null) { | ||
return null; | ||
} | ||
|
||
var resourceRoot = ClasspathResourceUtils.getRootFromResourceUrl(name, resource); | ||
if (maskedClasspathElements.contains(resourceRoot)) { | ||
try { | ||
var resources = getResources(name); | ||
resource = resources.hasMoreElements() ? resources.nextElement() : null; | ||
} catch (IOException e) { | ||
resource = null; | ||
} | ||
} | ||
|
||
return resource; | ||
} | ||
|
||
@Override | ||
public Enumeration<URL> getResources(String name) throws IOException { | ||
return new FilteringEnumeration(super.getResources(name), name); | ||
} | ||
|
||
/** | ||
* Filters an enumeration of URLs by using the given predicate. | ||
*/ | ||
class FilteringEnumeration implements Enumeration<URL> { | ||
private final Enumeration<URL> delegate; | ||
// Relative path that resources were requested for. | ||
private final String relativePath; | ||
|
||
@Nullable | ||
private URL nextElement; | ||
|
||
public FilteringEnumeration(Enumeration<URL> delegate, String relativePath) { | ||
this.delegate = delegate; | ||
this.relativePath = relativePath; | ||
seekNextElement(); | ||
} | ||
|
||
@Override | ||
public boolean hasMoreElements() { | ||
return nextElement != null; | ||
} | ||
|
||
@Override | ||
public URL nextElement() { | ||
var result = nextElement; | ||
if (result == null) { | ||
throw new NoSuchElementException(); | ||
} | ||
seekNextElement(); | ||
return result; | ||
} | ||
|
||
// Find the next element not within a masked classpath element | ||
private void seekNextElement() { | ||
while (delegate.hasMoreElements()) { | ||
var el = delegate.nextElement(); | ||
Path root = ClasspathResourceUtils.getRootFromResourceUrl(relativePath, el); | ||
if (!maskedClasspathElements.contains(root)) { | ||
nextElement = el; | ||
return; | ||
} | ||
} | ||
nextElement = null; // No more elements | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
loader/src/main/java/net/neoforged/fml/loading/ClasspathLocatorUtils.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.