Path: blob/master/modules/gdscript/language_server/scene_cache.cpp
21022 views
/**************************************************************************/1/* scene_cache.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "scene_cache.h"3132#include "godot_lsp.h"3334#include "core/io/resource_loader.h"35#include "editor/file_system/editor_file_system.h"36#include "scene/resources/packed_scene.h"3738void SceneCache::_get_owner_paths(EditorFileSystemDirectory *p_dir, const String &p_script_path, LocalVector<String> &r_owner_paths) {39if (!p_dir) {40return;41}4243for (int i = 0; i < p_dir->get_subdir_count(); i++) {44_get_owner_paths(p_dir->get_subdir(i), p_script_path, r_owner_paths);45}4647for (int i = 0; i < p_dir->get_file_count(); i++) {48if (p_dir->get_file_deps(i).has(p_script_path)) {49r_owner_paths.push_back(p_dir->get_file_path(i));50}51}52}5354void SceneCache::_finalize_scene_load() {55ERR_FAIL_COND(current_loaded_owner.is_empty() || script_path_queue.is_empty());5657Ref<PackedScene> scene_res = ResourceLoader::load_threaded_get(current_loaded_owner);5859if (scene_res.is_valid()) {60cache[script_path_queue[0]] = scene_res->instantiate();61} else {62cache[script_path_queue[0]] = nullptr;63}6465LOG_LSP("Scene cached for script:", script_path_queue[0]);66LOG_LSP("pending_script_queue length:", script_path_queue.size() - 1);6768script_path_queue.remove_at(0);69current_loaded_owner = String();70}7172void SceneCache::poll() {73if (current_loaded_owner.is_empty()) {74// No load ongoing, start the next one.7576if (EditorFileSystem::get_singleton()->is_scanning() || script_path_queue.is_empty()) {77return;78}7980LocalVector<String> owners;81_get_owner_paths(EditorFileSystem::get_singleton()->get_filesystem(), script_path_queue[0], owners);82for (const String &owner : owners) {83if (ResourceLoader::load_threaded_request(owner) == Error::OK) {84current_loaded_owner = owner;85LOG_LSP("Scene load started for:", current_loaded_owner);86break;87}88}8990if (current_loaded_owner.is_empty()) {91cache[script_path_queue[0]] = nullptr;92LOG_LSP("No scene found for script:", script_path_queue[0]);93script_path_queue.remove_at(0);94LOG_LSP("pending_script_queue length:", script_path_queue.size());95}96} else {97ERR_FAIL_COND(script_path_queue.is_empty());9899// There is an ongoing load. Check the status.100101ResourceLoader::ThreadLoadStatus status = ResourceLoader::load_threaded_get_status(current_loaded_owner);102103if (status == ResourceLoader::THREAD_LOAD_IN_PROGRESS) {104return;105}106107if (status == ResourceLoader::THREAD_LOAD_LOADED) {108_finalize_scene_load();109} else {110LOG_LSP("Scene load failure for:", current_loaded_owner);111cache[script_path_queue[0]] = nullptr;112113script_path_queue.remove_at(0);114current_loaded_owner = String();115}116}117}118119Node *SceneCache::get(const String &p_script_path) {120if (!script_path_queue.is_empty() && script_path_queue[0] == p_script_path && !current_loaded_owner.is_empty()) {121_finalize_scene_load();122} else {123script_path_queue.erase(p_script_path);124}125126if (Node **entry = cache.getptr(p_script_path)) {127return *entry;128}129130// Fallback to blocking load. This could happen if the open request was only recently sent.131// TODO: This could also happen when multiple clients are connected.132133LocalVector<String> owners;134_get_owner_paths(EditorFileSystem::get_singleton()->get_filesystem(), p_script_path, owners);135for (const String &owner : owners) {136Ref<PackedScene> scene = ResourceLoader::load(owner);137if (scene.is_valid()) {138Node *instance = scene->instantiate();139cache[p_script_path] = instance;140return instance;141}142}143144cache[p_script_path] = nullptr;145return nullptr;146}147148void SceneCache::request_load(const String &p_script_path) {149if (!cache.has(p_script_path) && !script_path_queue.has(p_script_path)) {150script_path_queue.push_back(p_script_path);151LOG_LSP("Scene load requested for:", p_script_path);152LOG_LSP("pending_script_queue length:", script_path_queue.size());153}154}155156void SceneCache::unload(const String &p_script_path) {157if (!script_path_queue.is_empty() && script_path_queue[0] == p_script_path && !current_loaded_owner.is_empty()) {158_ALLOW_DISCARD_ ResourceLoader::load_threaded_get(current_loaded_owner);159160script_path_queue.remove_at(0);161current_loaded_owner = String();162} else {163script_path_queue.erase(p_script_path);164}165166if (!cache.has(p_script_path)) {167return;168}169memdelete_notnull(cache[p_script_path]);170cache.erase(p_script_path);171LOG_LSP("Cache cleared for path:", p_script_path);172}173174void SceneCache::clear() {175if (!current_loaded_owner.is_empty()) {176_ALLOW_DISCARD_ ResourceLoader::load_threaded_get(current_loaded_owner);177current_loaded_owner = String();178}179script_path_queue.clear();180for (const KeyValue<String, Node *> &E : cache) {181memdelete_notnull(E.value);182}183cache.clear();184LOG_LSP("Cache cleared.");185}186187188