Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_remapped_files() method to Directory #59334

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions core/io/dir_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/project_settings.h"
#include "core/io/file_access.h"
#include "core/io/resource_loader.h"
#include "core/os/memory.h"
#include "core/os/os.h"
#include "core/templates/local_vector.h"
Expand Down Expand Up @@ -496,6 +497,40 @@ PackedStringArray DirAccess::get_files_at(const String &p_path) {
return da->get_files();
}

PackedStringArray DirAccess::get_remapped_files() {
PackedStringArray files = get_files();

HashSet<String> unique_files;
for (const String &file : files) {
if (file.get_extension() == "import") {
unique_files.insert(file.trim_suffix(".import"));
} else if (file.get_extension() == "remap") {
unique_files.insert(file.trim_suffix(".remap"));
} else if (file.get_extension() == "gdc") {
unique_files.insert(file.trim_suffix("c")); // .gdc -> .gd
} else {
unique_files.insert(file);
}
}

PackedStringArray ret;
ret.resize(unique_files.size());
String *retw = ret.ptrw();

int i = 0;
for (const String &file : unique_files) {
retw[i] = file;
i++;
}
return ret;
}

PackedStringArray DirAccess::get_remapped_files_at(const String &p_path) {
Ref<DirAccess> da = DirAccess::open(p_path);
ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path));
return da->get_remapped_files();
}

PackedStringArray DirAccess::get_directories() {
return _get_contents(true);
}
Expand Down Expand Up @@ -556,6 +591,8 @@ void DirAccess::_bind_methods() {
ClassDB::bind_method(D_METHOD("list_dir_end"), &DirAccess::list_dir_end);
ClassDB::bind_method(D_METHOD("get_files"), &DirAccess::get_files);
ClassDB::bind_static_method("DirAccess", D_METHOD("get_files_at", "path"), &DirAccess::get_files_at);
ClassDB::bind_method(D_METHOD("get_remapped_files"), &DirAccess::get_remapped_files);
ClassDB::bind_static_method("DirAccess", D_METHOD("get_remapped_files_at", "path"), &DirAccess::get_remapped_files_at);
ClassDB::bind_method(D_METHOD("get_directories"), &DirAccess::get_directories);
ClassDB::bind_static_method("DirAccess", D_METHOD("get_directories_at", "path"), &DirAccess::get_directories_at);
ClassDB::bind_static_method("DirAccess", D_METHOD("get_drive_count"), &DirAccess::_get_drive_count);
Expand Down
2 changes: 2 additions & 0 deletions core/io/dir_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class DirAccess : public RefCounted {

PackedStringArray get_files();
static PackedStringArray get_files_at(const String &p_path);
PackedStringArray get_remapped_files();
static PackedStringArray get_remapped_files_at(const String &p_path);
PackedStringArray get_directories();
static PackedStringArray get_directories_at(const String &p_path);
String _get_next();
Expand Down
8 changes: 8 additions & 0 deletions doc/classes/DirAccess.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<description>
Returns a [PackedStringArray] containing filenames of the directory contents, excluding directories. The array is sorted alphabetically.
Affected by [member include_hidden].
[b]Note:[/code] This method returns the factual list of files and the results may be different when used in exported project (e.g. for importable files, like images or sounds, only the [code].import[/code] files will be in the directory). To get the list of original files based on their remaps, use [method get_remapped_files].
</description>
</method>
<method name="get_files_at" qualifiers="static">
Expand All @@ -196,6 +197,13 @@
Returns the result of the last [method open] call in the current thread.
</description>
</method>
<method name="get_remapped_files">
<return type="PackedStringArray" />
<description>
Returns the list of files in a directory, with imported or remapped files listed as their original files. The list is consistent between editor and exported project (excluding files that weren't exported).
For example, for this file list: [code]icon.png.import, icon.wav.import, player.tscn.remap, player.gdc, player.gd.remap[/code], the returned list by this method will be [code]icon.png, icon.wav, player.tscn, player.gd[/code].
</description>
</method>
<method name="get_space_left">
<return type="int" />
<description>
Expand Down