Provide a reliable way to see original resources in a directory
When exporting a project, resources are often moved, converted or remapped (source import files are gone, text scenes are converted to binary, etc).
This new function is a convenience that allows to list a directory and obtain the actual original resource files.
Example
var resources = ResourceLoader.list_directory("res://images")
print(resources)
Result will be something like:
["image1.png","image2.png","image3.png"]
instead of
["image1.png.import","image2.png.import","image3.png.import"]
- Production edit: This closes https://github.com/godotengine/godot/issues/25672. See https://github.com/godotengine/godot/issues/66014.
If you'll allow me to scrutinize the function name: if the function essentially lists files and not directories, perhaps an alternative name could be:
list_directory_fileslist_files_in_dirget_files_in_directory(withget_*being perhaps more in line with other function names in ResourceLoader)get_files
It lists both files and directories, it just appends a "/" to the end of the directory, I forgot to make note of this, will do so.
Would this also mean that we wouldn't have to deal with .remap in exported games? Looks great! Looking forward to seeing this improvement!
So I compared editor results with exported results and got this:
Editor get_files_at()
["Scene.tscn", "Script.gd", "icon.svg", "icon.svg.import", "textfile.txt"]
Editor list_directory()
["../", "./", "Folder/", "Scene.tscn", "Script.gd", "icon.svg"]
Exported get_files_at()
["Scene.tscn.remap", "Script.gd.remap", "Script.gdc", "icon.svg.import"]
Exported list_directory()
["Folder/", "Scene.tscn", "Script.gd", "Script.gdc", "icon.svg"]
Notes:
- Editor lists
.and..for some reason. This should be removed. list_directory()does not include non-resource files. This should be stated in the documentation.
Other than that seems to work correctly.
Does this PR fully supersede https://github.com/godotengine/godot/pull/59334?
Well my PR lists resource files together with non-resource files and excludes folders. But that's just alternative implementation, it's fully superseded.
Applied changes suggested.
please refer to comments in the PR this supersedes why it's not actually "reliable".
for example mine there and there
tldr: you're guessing that a certain filename means that it used to have a different one, but the mere existence of ExportPlugins makes that assumption questionable at best. like KoBeWi said in the other PR, there's no way of actually doing this "reliable" without creating a list during export, and a simple s/.remap// is not enough to resolve that list. this PR is just copying a common gdscript hack/workaround to core instead. it'll work often/usually, but doesn't do what it claims.
@nonchip Yes, but that is a very corner case and generally external to the core engine, and even with export plugins remaps are still used, so it should work in almost every case. A "proper" solution is likely so complex that its not worth it.
Likewise, this PR is fine as-is, and even if a better solution is deemed worth to implement in the future, the API exposed to the user is not changed.
Thanks!