From 0f903820fab5621d258ed3e7a0280ebccb7ce12c Mon Sep 17 00:00:00 2001 From: Natty <31473117+493msi@users.noreply.github.com> Date: Wed, 6 Apr 2022 21:21:27 +0200 Subject: [PATCH] Implemented FileChannel and symlink support --- UPDATE_NOTES.md | 3 + .../main/kotlin/org/plutoengine/Versions.kt | 2 +- .../ResourceFileSystemProvider.java | 66 +++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/UPDATE_NOTES.md b/UPDATE_NOTES.md index 80bc206..8154af8 100755 --- a/UPDATE_NOTES.md +++ b/UPDATE_NOTES.md @@ -1,3 +1,6 @@ +## 22.0.0.0-alpha.4 +* `[PlutoRuntime]` Implemented optional `ResourceFileSystem` features + ## 22.0.0.0-alpha.3 * `[SDK]` **Extensions are now published via GitHub actions** diff --git a/buildSrc/src/main/kotlin/org/plutoengine/Versions.kt b/buildSrc/src/main/kotlin/org/plutoengine/Versions.kt index c0efd3c..8f02e60 100755 --- a/buildSrc/src/main/kotlin/org/plutoengine/Versions.kt +++ b/buildSrc/src/main/kotlin/org/plutoengine/Versions.kt @@ -22,7 +22,7 @@ object Versions { const val isPrerelease = true const val prereleaseName = "alpha" - const val prerealeaseUpdate = 3 + const val prerealeaseUpdate = 4 val versionFull = if (isPrerelease) diff --git a/engine-core/plutoruntime/src/main/java/org/plutoengine/resource/filesystem/ResourceFileSystemProvider.java b/engine-core/plutoruntime/src/main/java/org/plutoengine/resource/filesystem/ResourceFileSystemProvider.java index 60e4d05..ebbd1e1 100755 --- a/engine-core/plutoruntime/src/main/java/org/plutoengine/resource/filesystem/ResourceFileSystemProvider.java +++ b/engine-core/plutoruntime/src/main/java/org/plutoengine/resource/filesystem/ResourceFileSystemProvider.java @@ -8,6 +8,8 @@ import org.plutoengine.mod.ModLoader; import java.io.IOException; import java.net.URI; +import java.nio.channels.AsynchronousFileChannel; +import java.nio.channels.FileChannel; import java.nio.channels.SeekableByteChannel; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; @@ -17,6 +19,7 @@ import java.nio.file.spi.FileSystemProvider; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.concurrent.ExecutorService; public class ResourceFileSystemProvider extends FileSystemProvider { @@ -137,6 +140,69 @@ public class ResourceFileSystemProvider extends FileSystemProvider return backingProvider.newByteChannel(backingPath, options, attrs); } + @Override + public FileChannel newFileChannel(Path path, Set options, FileAttribute... attrs) throws IOException + { + if (!(path instanceof ResourcePath rp)) + throw new IllegalArgumentException("Expected a path of type %s!".formatted(ResourcePath.class)); + + var backingPath = rp.getBackingPath(); + var backingFileSystem = backingPath.getFileSystem(); + var backingProvider = backingFileSystem.provider(); + + return backingProvider.newFileChannel(backingPath, options, attrs); + } + + @Override + public AsynchronousFileChannel newAsynchronousFileChannel(Path path, Set options, ExecutorService executor, FileAttribute... attrs) throws IOException + { + return super.newAsynchronousFileChannel(path, options, executor, attrs); + } + + @Override + public Path readSymbolicLink(Path link) throws IOException + { + if (!(link instanceof ResourcePath rp)) + throw new IllegalArgumentException("Expected a path of type %s!".formatted(ResourcePath.class)); + + var backingPath = rp.getBackingPath(); + var backingFileSystem = backingPath.getFileSystem(); + var backingProvider = backingFileSystem.provider(); + + return backingProvider.readSymbolicLink(backingPath); + } + + @Override + public void createSymbolicLink(Path link, Path target, FileAttribute... attrs) throws IOException + { + if (!(link instanceof ResourcePath rpLink)) + throw new IllegalArgumentException("Expected a path of type %s!".formatted(ResourcePath.class)); + + if (!(target instanceof ResourcePath rpTarget)) + throw new IllegalArgumentException("Expected a path of type %s!".formatted(ResourcePath.class)); + + var linkBackingPath = rpLink.getBackingPath(); + var linkBackingFileSystem = linkBackingPath.getFileSystem(); + var linkBackingProvider = linkBackingFileSystem.provider(); + + var targetBackingPath = rpTarget.getBackingPath(); + + linkBackingProvider.createSymbolicLink(linkBackingPath, targetBackingPath, attrs); + } + + @Override + public FileSystem newFileSystem(Path path, Map env) throws IOException + { + if (!(path instanceof ResourcePath rp)) + throw new IllegalArgumentException("Expected a path of type %s!".formatted(ResourcePath.class)); + + var backingPath = rp.getBackingPath(); + var backingFileSystem = backingPath.getFileSystem(); + var backingProvider = backingFileSystem.provider(); + + return backingProvider.newFileSystem(path, env); + } + @Override public synchronized DirectoryStream newDirectoryStream(Path dir, DirectoryStream.Filter filter) throws IOException {