Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/extension/gdextension_resource_format.cpp
45997 views
1
/**************************************************************************/
2
/* gdextension_resource_format.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 "gdextension_resource_format.h"
32
33
#include "core/extension/gdextension_manager.h"
34
#include "core/object/class_db.h"
35
36
Ref<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) {
37
// We can't have two GDExtension resource object representing the same library, because
38
// loading (or unloading) a GDExtension affects global data. So, we need reuse the same
39
// object if one has already been loaded (even if caching is disabled at the resource
40
// loader level).
41
GDExtensionManager *manager = GDExtensionManager::get_singleton();
42
if (manager->is_extension_loaded(p_path)) {
43
return manager->get_extension(p_path);
44
}
45
46
Ref<GDExtension> lib;
47
Error err = load_gdextension_resource(p_path, lib);
48
if (err != OK && r_error) {
49
// Errors already logged in load_gdextension_resource().
50
*r_error = err;
51
}
52
return lib;
53
}
54
55
void GDExtensionResourceLoader::get_recognized_extensions(List<String> *p_extensions) const {
56
p_extensions->push_back("gdextension");
57
}
58
59
bool GDExtensionResourceLoader::handles_type(const String &p_type) const {
60
return p_type == "GDExtension";
61
}
62
63
String GDExtensionResourceLoader::get_resource_type(const String &p_path) const {
64
if (p_path.has_extension("gdextension")) {
65
return "GDExtension";
66
}
67
return "";
68
}
69
70
Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension) {
71
ERR_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.");
72
73
GDExtensionManager *extension_manager = GDExtensionManager::get_singleton();
74
75
GDExtensionManager::LoadStatus status = extension_manager->load_extension(p_path);
76
if (status != GDExtensionManager::LOAD_STATUS_OK && status != GDExtensionManager::LOAD_STATUS_ALREADY_LOADED) {
77
// Errors already logged in load_extension().
78
return FAILED;
79
}
80
81
p_extension = extension_manager->get_extension(p_path);
82
return OK;
83
}
84
85
#ifdef TOOLS_ENABLED
86
void GDExtensionResourceLoader::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
87
Ref<GDExtension> gdext = ResourceLoader::load(p_path);
88
if (gdext.is_null()) {
89
return;
90
}
91
92
for (const StringName class_name : gdext->get_classes_used()) {
93
if (ClassDB::class_exists(class_name)) {
94
r_classes->insert(class_name);
95
}
96
}
97
}
98
#endif // TOOLS_ENABLED
99
100