Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Improve reliability of recent scenes

kobewi3935fbca7b6f
diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp
index 5360dbee73c..d7a71703307 100644
--- a/core/io/resource_uid.cpp
+++ b/core/io/resource_uid.cpp
@@ -256,6 +256,17 @@ String ResourceUID::ensure_path(const String &p_uid_or_path) {
return p_uid_or_path;
}
+String ResourceUID::ensure_path_nocheck(const String &p_uid_or_path) {
+ if (p_uid_or_path.begins_with("uid://")) {
+ ResourceUID::ID id = singleton->text_to_id(p_uid_or_path);
+ if (id == INVALID_ID || !singleton->has_id(id)) {
+ return String();
+ }
+ return singleton->get_id_path(id);
+ }
+ return p_uid_or_path;
+}
+
Vector<uint8_t> ResourceUID::encode_binary_cache(const Vector<Pair<ID, String>> &p_entries) {
Ref<StreamPeerBuffer> buffer;
buffer.instantiate();
diff --git a/core/io/resource_uid.h b/core/io/resource_uid.h
index fda20a0a027..638660b49d5 100644
--- a/core/io/resource_uid.h
+++ b/core/io/resource_uid.h
@@ -86,6 +86,8 @@ public:
static String uid_to_path(const String &p_uid);
static String path_to_uid(const String &p_path);
static String ensure_path(const String &p_uid_or_path);
+ // Doesn't print errors, returns empty path on invalid UID.
+ static String ensure_path_nocheck(const String &p_uid_or_path);
Error load_from_cache(bool p_reset);
Error save_to_cache();
diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp
index 697014c684c..6aff5968582 100644
--- a/editor/editor_node.cpp
+++ b/editor/editor_node.cpp
@@ -3501,7 +3501,13 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
} break;
case SCENE_OPEN_PREV: {
if (!prev_closed_scenes.is_empty()) {
- open_scene(prev_closed_scenes.back()->get());
+ String path = prev_closed_scenes[prev_closed_scenes.size() - 1];
+ path = ResourceUID::ensure_path_nocheck(path);
+ if (!path.is_empty() && ResourceLoader::exists(path)) {
+ open_scene(path);
+ } else {
+ prev_closed_scenes.resize(prev_closed_scenes.size() - 1);
+ }
}
} break;
case EditorSceneTabs::SCENE_CLOSE_OTHERS: {
@@ -5523,10 +5529,11 @@ void EditorNode::_show_messages() {
void EditorNode::_update_prev_closed_scenes(const String &p_scene_path, bool p_add_scene) {
if (!p_scene_path.is_empty()) {
+ const String scene_uid = ResourceUID::path_to_uid(p_scene_path);
if (p_add_scene) {
- prev_closed_scenes.push_back(p_scene_path);
+ prev_closed_scenes.push_back(scene_uid);
} else {
- prev_closed_scenes.erase(p_scene_path);
+ prev_closed_scenes.erase(scene_uid);
}
file_menu->set_item_disabled(file_menu->get_item_index(SCENE_OPEN_PREV), prev_closed_scenes.is_empty());
}
@@ -5534,22 +5541,27 @@ void EditorNode::_update_prev_closed_scenes(const String &p_scene_path, bool p_a
void EditorNode::_add_to_recent_scenes(const String &p_scene) {
Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
+ const String scene_uid = ResourceUID::path_to_uid(p_scene);
+
+#ifndef DISABLE_DEPRECATED
if (rc.has(p_scene)) {
rc.erase(p_scene);
}
- rc.push_front(p_scene);
+#endif
+ if (rc.has(scene_uid)) {
+ rc.erase(scene_uid);
+ }
+ rc.push_front(scene_uid);
if (rc.size() > 10) {
rc.resize(10);
}
EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", rc);
- _update_recent_scenes();
}
void EditorNode::_open_recent_scene(int p_idx) {
if (p_idx == recent_scenes->get_item_count() - 1) {
EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", Array());
- callable_mp(this, &EditorNode::_update_recent_scenes).call_deferred();
} else {
Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
ERR_FAIL_INDEX(p_idx, rc.size());
@@ -5557,7 +5569,6 @@ void EditorNode::_open_recent_scene(int p_idx) {
if (open_scene(rc[p_idx]) != OK) {
rc.remove_at(p_idx);
EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", rc);
- _update_recent_scenes();
}
}
}
@@ -5566,18 +5577,33 @@ void EditorNode::_update_recent_scenes() {
Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
recent_scenes->clear();
+ LocalVector<int> missing_scenes;
if (rc.size() == 0) {
recent_scenes->add_item(TTRC("No Recent Scenes"), -1);
recent_scenes->set_item_disabled(-1, true);
} else {
- String path;
for (int i = 0; i < rc.size(); i++) {
- path = rc[i];
- recent_scenes->add_item(path.replace("res://", ""), i);
+ const String path = ResourceUID::ensure_path_nocheck(rc[i]);
+ if (!path.is_empty() && ResourceLoader::exists(path)) {
+ recent_scenes->add_item(path.trim_prefix("res://"), i);
+ } else {
+ missing_scenes.push_back(i);
+ }
}
recent_scenes->add_separator();
recent_scenes->add_shortcut(ED_SHORTCUT("editor/clear_recent", TTRC("Clear Recent Scenes")));
+
+ if (!missing_scenes.is_empty()) {
+ // Some scenes are missing, so update the stored list.
+ Array new_scenes;
+ for (int i = 0; i < rc.size(); i++) {
+ if (!missing_scenes.has(i)) {
+ new_scenes.push_back(rc[i]);
+ }
+ }
+ EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", new_scenes);
+ }
}
recent_scenes->set_item_auto_translate_mode(-1, AUTO_TRANSLATE_MODE_ALWAYS);
recent_scenes->reset_size();
@@ -8094,6 +8120,7 @@ void EditorNode::_build_file_menu(bool p_dark_mode) {
if (!recent_scenes) {
recent_scenes = memnew(PopupMenu);
recent_scenes->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
+ recent_scenes->connect("about_to_popup", callable_mp(this, &EditorNode::_update_recent_scenes));
recent_scenes->connect(SceneStringName(id_pressed), callable_mp(this, &EditorNode::_open_recent_scene));
}
file_menu->add_submenu_node_item(TTRC("Open Recent"), recent_scenes, SCENE_OPEN_RECENT);
diff --git a/editor/editor_node.h b/editor/editor_node.h
index 9a47d9b8cac..f1e765dc459 100644
--- a/editor/editor_node.h
+++ b/editor/editor_node.h
@@ -375,7 +375,7 @@ private:
PopupMenu *recent_scenes = nullptr;
String _recent_scene;
- List<String> prev_closed_scenes;
+ LocalVector<String> prev_closed_scenes;
String defer_load_scene;
Node *_last_instantiated_scene = nullptr;