Add FileSystem & FIleSystemProtocol to allow custom path prefixes (custom://) #98544
+2,692
−1,314
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
closes godotengine/godot-proposals#11032
closes godotengine/godot-proposals#6307
This PR is a refactor of the Godot FileAccess system. It adds FileSystem and FileSystemProtocol to manage file path prefixes.
FileAccess access type and create_funcs are removed now. Different FileAccess types only deals with their targeted file source now, which means different platform's OS FileAccess now only handles paths that are in the os filesystem. They don't need to handle res:// user:// anymore, in fact, they don't even know about other protocols now. Platforms now implement their own OS protocol and OS FileAccess and register the os protocol to the FileSystem.
Current-file-unrelated methods like
file_exists(path)andget_modified_time(path)are also moved to FileSystemProtocol. The original implementation instantiates a FileAccess, calls the method and immediately throws it away. This change can theoretically improve the performance with simple file operations.By pre-defining some FileSystemProtocols that remaps paths, users can easily register a protocol prefix that remaps file paths.
Next, I'll move current-directory-unrelated methods to FileSystemProtocol. Making calls to those absolute directory methods will become cheaper. DirAccess can also just become a simple wrapper of FileSystemProtocols.
Issues
(Fixed) gdscript://
Solution
As far as I can tell,
gdscript://object_id.gdrepresents a gdscript instance in memory.To prevent stepping in old code, gdscript protocol is reserved. It fails all accesses silently.
Descripton
Introduced by #71197
This is the reason that GDScript test fails. It expects hardcoded console output. During the test, GDScript system requires a resource load for the file path prefixed
gdscript://, which would silently fail and return false in the old FileAccess implementation. But in FileSystem, an error message ofUnknown filesystem protocol gdscriptgenerates, thus failing the test.I don't think muting the error message is a good approach, if anyone adds a gdscript protocol, GDScript system would break because of this.
non-OS DirAccess change_dir to aboslute OS paths
This is caused by how change_dir handles protocol prefixes. It converts current path and target path to OS absolute paths, does the CWD changing to verify the path, and finally strip the specific protocol's root path from the aboslute path and add the protocol prefix.
It doesn't seem to be a by-design behavior for a DirAccess which handles prefixed paths to successfully change into an aboslute OS path. This seems to only be a weird edge case caused by the original implementation.
This won't work anymore after this PR, because every protocol handler are only given the path under that prefix now. You can only change_dir to a relative path or an absolute path with that prefix now.
Todo
FileSystem&FileSystemProtocolFileAccesstoFileSystemProtocolDirAccesstoFileSystemProtocolMove hard-coded protocols to FileSystemProtocol
os://andpipe://are implemented and registered by different OS platforms.res://user://uid://mem://if Cleanup and exposeFileAccessMemory. #98287 is mergedPlatform supports
Create
FileSystemProtocolOSandFileSystemProtocolPipe.Cleanup
FileAccessPlatformandFileAccessPlatformPipe.Add
OS_Platform::initialize_filesystem().Remove previous
FileAccess::make_defaultcalls fromOS_Platform::initialize()Future Tasks
FileAccessProtocolRemap