Show a warning dialog when saving a large text-based scene

Text-based scenes that contain large amounts of binary data
are slower to save and load. Their binary resources should be moved
to separate files, or the binary `.scn` format should be used instead.
This commit is contained in:
Hugo Locurcio 2021-10-11 17:54:44 +02:00
parent f4059d0e3c
commit eb31f878f7
No known key found for this signature in database
GPG key ID: 39E8F8BE30B0A49C
3 changed files with 19 additions and 0 deletions

View file

@ -873,6 +873,10 @@
If [code]true[/code], text resources are converted to a binary format on export. This decreases file sizes and speeds up loading slightly.
[b]Note:[/b] If [member editor/export/convert_text_resources_to_binary] is [code]true[/code], [method @GDScript.load] will not be able to return the converted files in an exported project. Some file paths within the exported PCK will also change, such as [code]project.godot[/code] becoming [code]project.binary[/code]. If you rely on run-time loading of files present within the PCK, set [member editor/export/convert_text_resources_to_binary] to [code]false[/code].
</member>
<member name="editor/general/warn_on_saving_large_text_resources" type="bool" setter="" getter="" default="true">
If [code]true[/code], displays a warning toast message when saving a text-based scene or resource that is larger than 500 KB on disk. This is typically caused by binary subresources being embedded as text, which results in slow and inefficient conversion to text. This in turn impacts scene saving and loading times.
This should usually be resolved by moving the embedded binary subresource to its own binary resource file ([code].res[/code] extension instead of [code].tres[/code]). This is the preferred approach. Alternatively, the entire scene can be saved with the binary [code].scn[/code] format as opposed to [code].tscn[/code], but this will make it less friendly to version control systems.
</member>
<member name="editor/import/reimport_missing_imported_files" type="bool" setter="" getter="" default="true">
</member>
<member name="editor/import/use_multiple_threads" type="bool" setter="" getter="" default="true">

View file

@ -1839,6 +1839,19 @@ void EditorNode::_save_scene(String p_file, int idx) {
editor_data.set_scene_as_saved(idx);
editor_data.set_scene_modified_time(idx, FileAccess::get_modified_time(p_file));
if (GLOBAL_GET("editor/general/warn_on_saving_large_text_resources")) {
if (p_file.ends_with(".tscn") || p_file.ends_with(".tres")) {
Ref<FileAccess> fa = FileAccess::open(p_file, FileAccess::READ);
const uint64_t file_size = fa->get_length();
if (file_size >= 524'288) {
// File is larger than 500 KB, likely because it contains binary data serialized as Base64.
// This is slow to save and load, so warn the user.
EditorToaster::get_singleton()->popup_str(
vformat(TTR("The text-based scene or resource at path \"%s\" is large on disk (%d KB), likely because it has embedded binary data.\nThis slows down scene saving and loading.\nConsider saving its binary subresource(s) to a binary `.res` file or saving the scene as a binary `.scn` file.\nThis warning can be disabled in the Project Settings (Editor > General > Warn On Saving Large Text Resources)."), p_file, file_size / 1024), EditorToaster::SEVERITY_WARNING);
}
}
}
editor_folding.save_scene_folding(scene, p_file);
_update_title();

View file

@ -261,6 +261,8 @@ void register_editor_types() {
EditorPlugins::add_by_type<TileMapEditorPlugin>();
// For correct doc generation.
GLOBAL_DEF("editor/general/warn_on_saving_large_text_resources", true);
GLOBAL_DEF("editor/run/main_run_args", "");
GLOBAL_DEF(PropertyInfo(Variant::STRING, "editor/script/templates_search_path", PROPERTY_HINT_DIR), "res://script_templates");