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

[4.4 backport] Assorted editor improvements #73

Merged
merged 10 commits into from
Oct 19, 2024
8 changes: 3 additions & 5 deletions core/io/file_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ bool FileAccess::exists(const String &p_name) {
return true;
}

Ref<FileAccess> f = open(p_name, READ);
if (f.is_null()) {
return false;
}
return true;
// Using file_exists because it's faster than trying to open the file.
Ref<FileAccess> ret = create_for_path(p_name);
return ret->file_exists(p_name);
}

void FileAccess::_set_access_type(AccessType p_access) {
Expand Down
42 changes: 41 additions & 1 deletion core/io/resource_importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "core/os/os.h"
#include "core/variant/variant_parser.h"

ResourceFormatImporterLoadOnStartup ResourceImporter::load_on_startup = nullptr;

bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const {
return p_a->get_importer_name() < p_b->get_importer_name();
}
Expand Down Expand Up @@ -137,6 +139,20 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy
}

Ref<Resource> ResourceFormatImporter::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
#ifdef TOOLS_ENABLED
// When loading a resource on startup, we use the load_on_startup callback,
// which executes the loading in the EditorFileSystem. It can reimport
// the resource and retry the load, allowing the resource to be loaded
// even if it is not yet imported.
if (ResourceImporter::load_on_startup != nullptr) {
return ResourceImporter::load_on_startup(this, p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode);
}
#endif

return load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, false);
}

Ref<Resource> ResourceFormatImporter::load_internal(const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode, bool p_silence_errors) {
PathAndType pat;
Error err = _get_path_and_type(p_path, pat);

Expand All @@ -148,6 +164,13 @@ Ref<Resource> ResourceFormatImporter::load(const String &p_path, const String &p
return Ref<Resource>();
}

if (p_silence_errors) {
// Note: Some importers do not create files in the .godot folder, so we need to check if the path is empty.
if (!pat.path.is_empty() && !FileAccess::exists(pat.path)) {
return Ref<Resource>();
}
}

Ref<Resource> res = ResourceLoader::_load(pat.path, p_path, pat.type, p_cache_mode, r_error, p_use_sub_threads, r_progress);

#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -364,6 +387,23 @@ ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) c
return pat.uid;
}

Error ResourceFormatImporter::get_resource_import_info(const String &p_path, StringName &r_type, ResourceUID::ID &r_uid, String &r_import_group_file) const {
PathAndType pat;
Error err = _get_path_and_type(p_path, pat);

if (err == OK) {
r_type = pat.type;
r_uid = pat.uid;
r_import_group_file = pat.group_file;
} else {
r_type = "";
r_uid = ResourceUID::INVALID_ID;
r_import_group_file = "";
}

return err;
}

Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) const {
PathAndType pat;
Error err = _get_path_and_type(p_path, pat);
Expand Down Expand Up @@ -467,7 +507,7 @@ bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) con

for (int i = 0; i < importers.size(); i++) {
if (importers[i]->get_importer_name() == pat.importer) {
if (!importers[i]->are_import_settings_valid(p_path)) { //importer thinks this is not valid
if (!importers[i]->are_import_settings_valid(p_path, pat.metadata)) { //importer thinks this is not valid
return false;
}
}
Expand Down
10 changes: 9 additions & 1 deletion core/io/resource_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#include "core/io/resource_saver.h"

class ResourceImporter;
class ResourceFormatImporter;

typedef Ref<Resource> (*ResourceFormatImporterLoadOnStartup)(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode);

class ResourceFormatImporter : public ResourceFormatLoader {
struct PathAndType {
Expand All @@ -60,6 +63,7 @@ class ResourceFormatImporter : public ResourceFormatLoader {
public:
static ResourceFormatImporter *get_singleton() { return singleton; }
virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
Ref<Resource> load_internal(const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode, bool p_silence_errors);
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override;
virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const override;
Expand Down Expand Up @@ -93,6 +97,8 @@ class ResourceFormatImporter : public ResourceFormatLoader {
String get_import_settings_hash() const;

String get_import_base_path(const String &p_for_file) const;
Error get_resource_import_info(const String &p_path, StringName &r_type, ResourceUID::ID &r_uid, String &r_import_group_file) const;

ResourceFormatImporter();
};

Expand All @@ -103,6 +109,8 @@ class ResourceImporter : public RefCounted {
static void _bind_methods();

public:
static ResourceFormatImporterLoadOnStartup load_on_startup;

virtual String get_importer_name() const = 0;
virtual String get_visible_name() const = 0;
virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
Expand Down Expand Up @@ -145,7 +153,7 @@ class ResourceImporter : public RefCounted {
virtual void import_threaded_end() {}

virtual Error import_group_file(const String &p_group_file, const HashMap<String, HashMap<StringName, Variant>> &p_source_file_options, const HashMap<String, String> &p_base_paths) { return ERR_UNAVAILABLE; }
virtual bool are_import_settings_valid(const String &p_path) const { return true; }
virtual bool are_import_settings_valid(const String &p_path, const Dictionary &p_meta) const { return true; }
virtual String get_import_settings_string() const { return String(); }
};

Expand Down
61 changes: 32 additions & 29 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,36 +1060,39 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem
new_path = path_remaps[new_path];
} else {
// Try file remap.
Error err;
Ref<FileAccess> f = FileAccess::open(new_path + ".remap", FileAccess::READ, &err);
if (f.is_valid()) {
VariantParser::StreamFile stream;
stream.f = f;

String assign;
Variant value;
VariantParser::Tag next_tag;

int lines = 0;
String error_text;
while (true) {
assign = Variant();
next_tag.fields.clear();
next_tag.name = String();

err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
if (err == ERR_FILE_EOF) {
break;
} else if (err != OK) {
ERR_PRINT("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text + ".");
break;
}
// Usually, there's no remap file and FileAccess::exists() is faster than FileAccess::open().
if (FileAccess::exists(new_path + ".remap")) {
Error err;
Ref<FileAccess> f = FileAccess::open(new_path + ".remap", FileAccess::READ, &err);
if (f.is_valid()) {
VariantParser::StreamFile stream;
stream.f = f;

String assign;
Variant value;
VariantParser::Tag next_tag;

int lines = 0;
String error_text;
while (true) {
assign = Variant();
next_tag.fields.clear();
next_tag.name = String();

err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
if (err == ERR_FILE_EOF) {
break;
} else if (err != OK) {
ERR_PRINT("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text + ".");
break;
}

if (assign == "path") {
new_path = value;
break;
} else if (next_tag.name != "remap") {
break;
if (assign == "path") {
new_path = value;
break;
} else if (next_tag.name != "remap") {
break;
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion doc/classes/EditorSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@
If [code]true[/code], displays folders in the FileSystem dock's bottom pane when split mode is enabled. If [code]false[/code], only files will be displayed in the bottom pane. Split mode can be toggled by pressing the icon next to the [code]res://[/code] folder path.
[b]Note:[/b] This setting has no effect when split mode is disabled (which is the default).
</member>
<member name="docks/filesystem/other_file_extensions" type="String" setter="" getter="">
A comma separated list of unsupported file extensions to show in the FileSystem dock, e.g. [code]"ico,icns"[/code].
</member>
<member name="docks/filesystem/textfile_extensions" type="String" setter="" getter="">
List of file extensions to consider as editable text files in the FileSystem dock (by double-clicking on the files).
A comma separated list of file extensions to consider as editable text files in the FileSystem dock (by double-clicking on the files), e.g. [code]"txt,md,cfg,ini,log,json,yml,yaml,toml,xml"[/code].
</member>
<member name="docks/filesystem/thumbnail_size" type="int" setter="" getter="">
The thumbnail size to use in the FileSystem dock (in pixels). See also [member filesystem/file_dialog/thumbnail_size].
Expand Down
1 change: 0 additions & 1 deletion drivers/unix/file_access_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
if (!err) {
return status.st_mtime;
} else {
WARN_PRINT("Failed to get modified time for: " + p_file);
return 0;
}
}
Expand Down
1 change: 0 additions & 1 deletion drivers/windows/file_access_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
}
}

print_verbose("Failed to get modified time for: " + p_file);
return 0;
}

Expand Down
Loading
Loading