Path: blob/master/core/extension/gdextension_resource_format.cpp
45997 views
/**************************************************************************/1/* gdextension_resource_format.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 "gdextension_resource_format.h"3132#include "core/extension/gdextension_manager.h"33#include "core/object/class_db.h"3435Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {36// We can't have two GDExtension resource object representing the same library, because37// loading (or unloading) a GDExtension affects global data. So, we need reuse the same38// object if one has already been loaded (even if caching is disabled at the resource39// loader level).40GDExtensionManager *manager = GDExtensionManager::get_singleton();41if (manager->is_extension_loaded(p_path)) {42return manager->get_extension(p_path);43}4445Ref<GDExtension> lib;46Error err = load_gdextension_resource(p_path, lib);47if (err != OK && r_error) {48// Errors already logged in load_gdextension_resource().49*r_error = err;50}51return lib;52}5354void GDExtensionResourceLoader::get_recognized_extensions(List<String> *p_extensions) const {55p_extensions->push_back("gdextension");56}5758bool GDExtensionResourceLoader::handles_type(const String &p_type) const {59return p_type == "GDExtension";60}6162String GDExtensionResourceLoader::get_resource_type(const String &p_path) const {63if (p_path.has_extension("gdextension")) {64return "GDExtension";65}66return "";67}6869Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension) {70ERR_FAIL_COND_V_MSG(p_extension.is_valid() && p_extension->is_library_open(), ERR_ALREADY_IN_USE, "Cannot load GDExtension resource into already opened library.");7172GDExtensionManager *extension_manager = GDExtensionManager::get_singleton();7374GDExtensionManager::LoadStatus status = extension_manager->load_extension(p_path);75if (status != GDExtensionManager::LOAD_STATUS_OK && status != GDExtensionManager::LOAD_STATUS_ALREADY_LOADED) {76// Errors already logged in load_extension().77return FAILED;78}7980p_extension = extension_manager->get_extension(p_path);81return OK;82}8384#ifdef TOOLS_ENABLED85void GDExtensionResourceLoader::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {86Ref<GDExtension> gdext = ResourceLoader::load(p_path);87if (gdext.is_null()) {88return;89}9091for (const StringName class_name : gdext->get_classes_used()) {92if (ClassDB::class_exists(class_name)) {93r_classes->insert(class_name);94}95}96}97#endif // TOOLS_ENABLED9899100