godot icon indicating copy to clipboard operation
godot copied to clipboard

Provide a reliable way to see original resources in a directory

Open reduz opened this issue 1 year ago • 7 comments

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.

reduz avatar Sep 05 '24 06:09 reduz

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_files
  • list_files_in_dir
  • get_files_in_directory (with get_* being perhaps more in line with other function names in ResourceLoader)
  • get_files

AdriaandeJongh avatar Sep 05 '24 07:09 AdriaandeJongh

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.

reduz avatar Sep 05 '24 08:09 reduz

Would this also mean that we wouldn't have to deal with .remap in exported games? Looks great! Looking forward to seeing this improvement!

JHDev2006 avatar Sep 05 '24 09:09 JHDev2006

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.

KoBeWi avatar Sep 05 '24 10:09 KoBeWi

Does this PR fully supersede https://github.com/godotengine/godot/pull/59334?

Calinou avatar Sep 06 '24 00:09 Calinou

Well my PR lists resource files together with non-resource files and excludes folders. But that's just alternative implementation, it's fully superseded.

KoBeWi avatar Sep 06 '24 00:09 KoBeWi

Applied changes suggested.

reduz avatar Oct 07 '24 07:10 reduz

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 avatar Oct 08 '24 07:10 nonchip

@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.

reduz avatar Oct 08 '24 10:10 reduz

Thanks!

Repiteo avatar Nov 11 '24 20:11 Repiteo