Skip to content

Commit

Permalink
Revert back to wrapping any non-default paths in a Union File System
Browse files Browse the repository at this point in the history
Added more sanitization to the UFS Key generator

Not doing so breaks some poorly written FileSystems
We will still benefit for the majority of libraries/mods.
  • Loading branch information
LexManos committed Nov 23, 2023
1 parent 139ad45 commit 2d292d2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'eclipse'
id 'org.cadixdev.licenser' version '0.6.1'
id 'org.gradlex.extra-java-module-info' version '1.4.2'
id 'net.minecraftforge.gradleutils' version '2.+'
id 'net.minecraftforge.gradleutils' version '[2.3.0,2.4)'
}

group = 'net.minecraftforge'
Expand Down Expand Up @@ -34,7 +34,7 @@ extraJavaModuleInfo {
}

changelog {
fromTag '0.9'
from '0.9'
}

tasks.named('jar', Jar).configure {
Expand Down
2 changes: 1 addition & 1 deletion sm-jmh/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'eclipse'
id 'java-library'
id 'org.gradlex.extra-java-module-info' version '1.4.2'
id 'net.minecraftforge.gradleutils' version '2.+'
id 'net.minecraftforge.gradleutils' version '[2.3.0,2.4)'
id 'com.diffplug.eclipse.apt' version '3.43.0'
}

Expand Down
2 changes: 1 addition & 1 deletion sm-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'eclipse'
id 'java-library'
id 'org.gradlex.extra-java-module-info' version '1.4.2'
id 'net.minecraftforge.gradleutils' version '2.+'
id 'net.minecraftforge.gradleutils' version '[2.3.0,2.4)'
}

repositories {
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/cpw/mods/jarhandling/impl/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -185,15 +184,25 @@ private Path newFileSystem(BiPredicate<String, String> filter, Path[] paths) {
if (filter == null && paths.length == 1) {
if (Files.isDirectory(paths[0]))
return paths[0];
// We have to manually open the jar files up via a URI instead of a Path
// because the ZipFileSystem implementation only caches the FileSystems
// when accessed that way. But we can only open it once or else it throws
// a FileSystemAlreadyExistsException. So, exceptions as codeflow, yay!
var uri = new URI("jar:" + paths[0].toUri());
try {
fs = FileSystems.newFileSystem(uri, Map.of(), null);
} catch (FileSystemAlreadyExistsException e) {
fs = FileSystems.getFileSystem(uri);
var uri = paths[0].toUri();
if ("file".equals(uri.getScheme())) {
// We have to manually open the jar files up via a URI instead of a Path
// because the ZipFileSystem implementation only caches the FileSystems
// when accessed that way. But we can only open it once or else it throws
// a FileSystemAlreadyExistsException. So, exceptions as codeflow, yay!
uri = new URI("jar:" + uri);
try {
fs = FileSystems.newFileSystem(uri, Map.of(), null);
} catch (FileSystemAlreadyExistsException e) {
fs = FileSystems.getFileSystem(uri);
}
} else {
// JarInJar is fucking stupid and breaks horribly if it knows about files directly.
// So because I don't want to go into the rabbit hole that is digging into that
// Any non-standard file system will be wrapped in a Union File System
// This still gets the performance benefit of having 90% of everything bypass the UFS
// TODO: [SM] Remove JarInJar file system in favor of a simpler dependency management system.
fs = UFSP.newFileSystem(paths[0], Map.of("filter", (BiPredicate<String, String>)(a, b) -> true));
}
} else {
var map = new HashMap<String, Object>();
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/cpw/mods/niofs/union/UnionFileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,22 @@ private UnionFileSystem newFileSystemInternal(final String key, final BiPredicat
}

private synchronized String makeKey(Path path) {
var key= (path instanceof UnionPath p) ? p.getFileSystem().getKey() :
path.toAbsolutePath().normalize().toUri().getPath();
return key.replace('!', '_') + "#" + index++;
String key;
if (path instanceof UnionPath p)
key = p.getFileSystem().getKey();
else {
var uri = path.toAbsolutePath().normalize().toUri();
key = uri.getPath();
if (key == null) // Fuck it this doesn't actually mean anything is just nice for debugging
key = uri.toString();
}

key = key.replace('!', '_') + "#" + index++;

if (key.charAt(0) != '/')
key = '/' + key; // URI's require anything that uses schemas to use absolute paths. So make sure our key is absolute.

return key;
}

@Override
Expand Down

0 comments on commit 2d292d2

Please sign in to comment.