Skip to content
0.2.0-dev Download ZIP
186 lines ·6.7 KB Raw
1/**************************************************************************/
2/* scene_cache.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "scene_cache.h"
32
33#include "godot_lsp.h"
34
35#include "core/io/resource_loader.h"
36#include "editor/file_system/editor_file_system.h"
37#include "scene/resources/packed_scene.h"
38
39void SceneCache::_get_owner_paths(EditorFileSystemDirectory *p_dir, const String &p_script_path, LocalVector<String> &r_owner_paths) {
40 if (!p_dir) {
41 return;
42 }
43
44 for (int i = 0; i < p_dir->get_subdir_count(); i++) {
45 _get_owner_paths(p_dir->get_subdir(i), p_script_path, r_owner_paths);
46 }
47
48 for (int i = 0; i < p_dir->get_file_count(); i++) {
49 if (p_dir->get_file_deps(i).has(p_script_path)) {
50 r_owner_paths.push_back(p_dir->get_file_path(i));
51 }
52 }
53}
54
55void SceneCache::_finalize_scene_load() {
56 ERR_FAIL_COND(current_loaded_owner.is_empty() || script_path_queue.is_empty());
57
58 Ref<PackedScene> scene_res = ResourceLoader::load_threaded_get(current_loaded_owner);
59
60 if (scene_res.is_valid()) {
61 cache[script_path_queue[0]] = scene_res->instantiate();
62 } else {
63 cache[script_path_queue[0]] = nullptr;
64 }
65
66 LOG_LSP("Scene cached for script:", script_path_queue[0]);
67 LOG_LSP("pending_script_queue length:", script_path_queue.size() - 1);
68
69 script_path_queue.remove_at(0);
70 current_loaded_owner = String();
71}
72
73void SceneCache::poll() {
74 if (current_loaded_owner.is_empty()) {
75 // No load ongoing, start the next one.
76
77 if (EditorFileSystem::get_singleton()->is_scanning() || script_path_queue.is_empty()) {
78 return;
79 }
80
81 LocalVector<String> owners;
82 _get_owner_paths(EditorFileSystem::get_singleton()->get_filesystem(), script_path_queue[0], owners);
83 for (const String &owner : owners) {
84 if (ResourceLoader::load_threaded_request(owner) == Error::OK) {
85 current_loaded_owner = owner;
86 LOG_LSP("Scene load started for:", current_loaded_owner);
87 break;
88 }
89 }
90
91 if (current_loaded_owner.is_empty()) {
92 cache[script_path_queue[0]] = nullptr;
93 LOG_LSP("No scene found for script:", script_path_queue[0]);
94 script_path_queue.remove_at(0);
95 LOG_LSP("pending_script_queue length:", script_path_queue.size());
96 }
97 } else {
98 ERR_FAIL_COND(script_path_queue.is_empty());
99
100 // There is an ongoing load. Check the status.
101
102 ResourceLoader::ThreadLoadStatus status = ResourceLoader::load_threaded_get_status(current_loaded_owner);
103
104 if (status == ResourceLoader::THREAD_LOAD_IN_PROGRESS) {
105 return;
106 }
107
108 if (status == ResourceLoader::THREAD_LOAD_LOADED) {
109 _finalize_scene_load();
110 } else {
111 LOG_LSP("Scene load failure for:", current_loaded_owner);
112 cache[script_path_queue[0]] = nullptr;
113
114 script_path_queue.remove_at(0);
115 current_loaded_owner = String();
116 }
117 }
118}
119
120Node *SceneCache::get(const String &p_script_path) {
121 if (!script_path_queue.is_empty() && script_path_queue[0] == p_script_path && !current_loaded_owner.is_empty()) {
122 _finalize_scene_load();
123 } else {
124 script_path_queue.erase(p_script_path);
125 }
126
127 if (Node **entry = cache.getptr(p_script_path)) {
128 return *entry;
129 }
130
131 // Fallback to blocking load. This could happen if the open request was only recently sent.
132 // TODO: This could also happen when multiple clients are connected.
133
134 LocalVector<String> owners;
135 _get_owner_paths(EditorFileSystem::get_singleton()->get_filesystem(), p_script_path, owners);
136 for (const String &owner : owners) {
137 Ref<PackedScene> scene = ResourceLoader::load(owner);
138 if (scene.is_valid()) {
139 Node *instance = scene->instantiate();
140 cache[p_script_path] = instance;
141 return instance;
142 }
143 }
144
145 cache[p_script_path] = nullptr;
146 return nullptr;
147}
148
149void SceneCache::request_load(const String &p_script_path) {
150 if (!cache.has(p_script_path) && !script_path_queue.has(p_script_path)) {
151 script_path_queue.push_back(p_script_path);
152 LOG_LSP("Scene load requested for:", p_script_path);
153 LOG_LSP("pending_script_queue length:", script_path_queue.size());
154 }
155}
156
157void SceneCache::unload(const String &p_script_path) {
158 if (!script_path_queue.is_empty() && script_path_queue[0] == p_script_path && !current_loaded_owner.is_empty()) {
159 _ALLOW_DISCARD_ ResourceLoader::load_threaded_get(current_loaded_owner);
160
161 script_path_queue.remove_at(0);
162 current_loaded_owner = String();
163 } else {
164 script_path_queue.erase(p_script_path);
165 }
166
167 if (!cache.has(p_script_path)) {
168 return;
169 }
170 memdelete(cache[p_script_path]);
171 cache.erase(p_script_path);
172 LOG_LSP("Cache cleared for path:", p_script_path);
173}
174
175void SceneCache::clear() {
176 if (!current_loaded_owner.is_empty()) {
177 _ALLOW_DISCARD_ ResourceLoader::load_threaded_get(current_loaded_owner);
178 current_loaded_owner = String();
179 }
180 script_path_queue.clear();
181 for (const KeyValue<String, Node *> &E : cache) {
182 memdelete(E.value);
183 }
184 cache.clear();
185 LOG_LSP("Cache cleared.");
186}