Skip to content

Commit cbb8f55

Browse files
committed
Show a warning toast 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.
1 parent 68410ac commit cbb8f55

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

doc/classes/EditorSettings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,10 @@
853853
If [code]true[/code], when saving a file, the editor will rename the old file to a different name, save a new file, then only remove the old file once the new file has been saved. This makes loss of data less likely to happen if the editor or operating system exits unexpectedly while saving (e.g. due to a crash or power outage).
854854
[b]Note:[/b] On Windows, this feature can interact negatively with certain antivirus programs. In this case, you may have to set this to [code]false[/code] to prevent file locking issues.
855855
</member>
856+
<member name="filesystem/on_save/warn_on_saving_large_text_resources" type="bool" setter="" getter="">
857+
If [code]true[/code], displays a warning toast message when saving a text-based scene or resource that is larger than 500 KiB 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.
858+
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.
859+
</member>
856860
<member name="filesystem/quick_open_dialog/default_display_mode" type="int" setter="" getter="">
857861
If set to [code]Adaptive[/code], the dialog opens in list view or grid view depending on the requested type. If set to [code]Last Used[/code], the display mode will always open the way you last used it.
858862
</member>

editor/editor_node.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ static const String EDITOR_NODE_CONFIG_SECTION = "EditorNode";
206206
static const String REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE = TTRC("The Android build template is already installed in this project and it won't be overwritten.\nRemove the \"%s\" directory manually before attempting this operation again.");
207207
static const String INSTALL_ANDROID_BUILD_TEMPLATE_MESSAGE = TTRC("This will set up your project for gradle Android builds by installing the source template to \"%s\".\nNote that in order to make gradle builds instead of using pre-built APKs, the \"Use Gradle Build\" option should be enabled in the Android export preset.");
208208

209+
constexpr int LARGE_RESOURCE_WARNING_SIZE_THRESHOLD = 512'000; // 500 KB
210+
209211
bool EditorProgress::step(const String &p_state, int p_step, bool p_force_refresh) {
210212
if (!force_background && Thread::is_main_thread()) {
211213
return EditorNode::progress_task_step(task, p_state, p_step, p_force_refresh);
@@ -1657,6 +1659,18 @@ void EditorNode::save_resource_in_path(const Ref<Resource> &p_resource, const St
16571659
clear_node_reference(p_resource); // // Check if Resource is saved to disk to potentially remove it from resource_count
16581660
emit_signal(SNAME("resource_saved"), p_resource);
16591661
editor_data.notify_resource_saved(p_resource);
1662+
1663+
if (EDITOR_GET("filesystem/on_save/warn_on_saving_large_text_resources")) {
1664+
if (p_path.ends_with(".tres")) {
1665+
const int64_t file_size = FileAccess::get_size(p_path);
1666+
if (file_size >= LARGE_RESOURCE_WARNING_SIZE_THRESHOLD) {
1667+
// File is larger than 500 KiB, likely because it contains binary data serialized as Base64.
1668+
// This is slow to save and load, so warn the user.
1669+
EditorToaster::get_singleton()->popup_str(
1670+
vformat(TTR("The text-based resource at path \"%s\" is large on disk (%s), likely because it has embedded binary data.\nThis slows down resource saving and loading.\nConsider saving its binary subresource(s) to a binary `.res` file or saving the resource as a binary `.res` file.\nThis warning can be disabled in the Editor Settings (FileSystem > On Save > Warn on Saving Large Text Resources)."), p_path, String::humanize_size(file_size)), EditorToaster::SEVERITY_WARNING);
1671+
}
1672+
}
1673+
}
16601674
}
16611675

16621676
void EditorNode::save_resource(const Ref<Resource> &p_resource) {
@@ -2404,6 +2418,18 @@ void EditorNode::_save_scene(String p_file, int idx) {
24042418
editor_data.set_scene_as_saved(idx);
24052419
editor_data.set_scene_modified_time(idx, FileAccess::get_modified_time(p_file));
24062420

2421+
if (EDITOR_GET("filesystem/on_save/warn_on_saving_large_text_resources")) {
2422+
if (p_file.ends_with(".tscn") || p_file.ends_with(".tres")) {
2423+
const int64_t file_size = FileAccess::get_size(p_file);
2424+
if (file_size >= LARGE_RESOURCE_WARNING_SIZE_THRESHOLD) {
2425+
// File is larger than 500 KiB, likely because it contains binary data serialized as Base64.
2426+
// This is slow to save and load, so warn the user.
2427+
EditorToaster::get_singleton()->popup_str(
2428+
vformat(TTR("The text-based scene at path \"%s\" is large on disk (%s), 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 Editor Settings (FileSystem > On Save > Warn on Saving Large Text Resources)."), p_file, String::humanize_size(file_size)), EditorToaster::SEVERITY_WARNING);
2429+
}
2430+
}
2431+
}
2432+
24072433
editor_folding.save_scene_folding(scene, p_file);
24082434

24092435
_update_title();

editor/settings/editor_settings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
653653
// On save
654654
_initial_set("filesystem/on_save/compress_binary_resources", true);
655655
_initial_set("filesystem/on_save/safe_save_on_backup_then_rename", true);
656+
_initial_set("filesystem/on_save/warn_on_saving_large_text_resources", true);
656657

657658
// EditorFileServer
658659
_initial_set("filesystem/file_server/port", 6010);

0 commit comments

Comments
 (0)