Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/extension/gdextension_library_loader.cpp
9903 views
1
/**************************************************************************/
2
/* gdextension_library_loader.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_library_loader.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/io/dir_access.h"
35
#include "core/version.h"
36
#include "gdextension.h"
37
38
Vector<SharedObject> GDExtensionLibraryLoader::find_extension_dependencies(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature) {
39
Vector<SharedObject> dependencies_shared_objects;
40
if (p_config->has_section("dependencies")) {
41
Vector<String> config_dependencies = p_config->get_section_keys("dependencies");
42
43
for (const String &dependency : config_dependencies) {
44
Vector<String> dependency_tags = dependency.split(".");
45
bool all_tags_met = true;
46
for (int i = 0; i < dependency_tags.size(); i++) {
47
String tag = dependency_tags[i].strip_edges();
48
if (!p_has_feature(tag)) {
49
all_tags_met = false;
50
break;
51
}
52
}
53
54
if (all_tags_met) {
55
Dictionary dependency_value = p_config->get_value("dependencies", dependency);
56
for (const Variant *key = dependency_value.next(nullptr); key; key = dependency_value.next(key)) {
57
String dependency_path = *key;
58
String target_path = dependency_value[*key];
59
if (dependency_path.is_relative_path()) {
60
dependency_path = p_path.get_base_dir().path_join(dependency_path);
61
}
62
dependencies_shared_objects.push_back(SharedObject(dependency_path, dependency_tags, target_path));
63
}
64
break;
65
}
66
}
67
}
68
69
return dependencies_shared_objects;
70
}
71
72
String GDExtensionLibraryLoader::find_extension_library(const String &p_path, Ref<ConfigFile> p_config, std::function<bool(String)> p_has_feature, PackedStringArray *r_tags) {
73
// First, check the explicit libraries.
74
if (p_config->has_section("libraries")) {
75
Vector<String> libraries = p_config->get_section_keys("libraries");
76
77
// Iterate the libraries, finding the best matching tags.
78
String best_library_path;
79
Vector<String> best_library_tags;
80
for (const String &E : libraries) {
81
Vector<String> tags = E.split(".");
82
bool all_tags_met = true;
83
for (int i = 0; i < tags.size(); i++) {
84
String tag = tags[i].strip_edges();
85
if (!p_has_feature(tag)) {
86
all_tags_met = false;
87
break;
88
}
89
}
90
91
if (all_tags_met && tags.size() > best_library_tags.size()) {
92
best_library_path = p_config->get_value("libraries", E);
93
best_library_tags = tags;
94
}
95
}
96
97
if (!best_library_path.is_empty()) {
98
if (best_library_path.is_relative_path()) {
99
best_library_path = p_path.get_base_dir().path_join(best_library_path);
100
}
101
if (r_tags != nullptr) {
102
r_tags->append_array(best_library_tags);
103
}
104
return best_library_path;
105
}
106
}
107
108
// Second, try to autodetect.
109
String autodetect_library_prefix;
110
if (p_config->has_section_key("configuration", "autodetect_library_prefix")) {
111
autodetect_library_prefix = p_config->get_value("configuration", "autodetect_library_prefix");
112
}
113
if (!autodetect_library_prefix.is_empty()) {
114
String autodetect_path = autodetect_library_prefix;
115
if (autodetect_path.is_relative_path()) {
116
autodetect_path = p_path.get_base_dir().path_join(autodetect_path);
117
}
118
119
// Find the folder and file parts of the prefix.
120
String folder;
121
String file_prefix;
122
if (DirAccess::dir_exists_absolute(autodetect_path)) {
123
folder = autodetect_path;
124
} else if (DirAccess::dir_exists_absolute(autodetect_path.get_base_dir())) {
125
folder = autodetect_path.get_base_dir();
126
file_prefix = autodetect_path.get_file();
127
} else {
128
ERR_FAIL_V_MSG(String(), vformat("Error in extension: %s. Could not find folder for automatic detection of libraries files. autodetect_library_prefix=\"%s\"", p_path, autodetect_library_prefix));
129
}
130
131
// Open the folder.
132
Ref<DirAccess> dir = DirAccess::open(folder);
133
ERR_FAIL_COND_V_MSG(dir.is_null(), String(), vformat("Error in extension: %s. Could not open folder for automatic detection of libraries files. autodetect_library_prefix=\"%s\"", p_path, autodetect_library_prefix));
134
135
// Iterate the files and check the prefixes, finding the best matching file.
136
String best_file;
137
Vector<String> best_file_tags;
138
dir->list_dir_begin();
139
String file_name = dir->_get_next();
140
while (file_name != "") {
141
if (!dir->current_is_dir() && file_name.begins_with(file_prefix)) {
142
// Check if the files matches all requested feature tags.
143
String tags_str = file_name.trim_prefix(file_prefix);
144
tags_str = tags_str.trim_suffix(tags_str.get_extension());
145
146
Vector<String> tags = tags_str.split(".", false);
147
bool all_tags_met = true;
148
for (int i = 0; i < tags.size(); i++) {
149
String tag = tags[i].strip_edges();
150
if (!p_has_feature(tag)) {
151
all_tags_met = false;
152
break;
153
}
154
}
155
156
// If all tags are found in the feature list, and we found more tags than before, use this file.
157
if (all_tags_met && tags.size() > best_file_tags.size()) {
158
best_file_tags = tags;
159
best_file = file_name;
160
}
161
}
162
file_name = dir->_get_next();
163
}
164
165
if (!best_file.is_empty()) {
166
String library_path = folder.path_join(best_file);
167
if (r_tags != nullptr) {
168
r_tags->append_array(best_file_tags);
169
}
170
return library_path;
171
}
172
}
173
return String();
174
}
175
176
Error GDExtensionLibraryLoader::open_library(const String &p_path) {
177
Error err = parse_gdextension_file(p_path);
178
if (err != OK) {
179
return err;
180
}
181
182
String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);
183
184
Vector<String> abs_dependencies_paths;
185
if (!library_dependencies.is_empty()) {
186
for (const SharedObject &dependency : library_dependencies) {
187
abs_dependencies_paths.push_back(ProjectSettings::get_singleton()->globalize_path(dependency.path));
188
}
189
}
190
191
OS::GDExtensionData data = {
192
true, // also_set_library_path
193
&library_path, // r_resolved_path
194
Engine::get_singleton()->is_editor_hint(), // generate_temp_files
195
&abs_dependencies_paths, // library_dependencies
196
};
197
198
err = OS::get_singleton()->open_dynamic_library(is_static_library ? String() : abs_path, library, &data);
199
if (err != OK) {
200
return err;
201
}
202
203
return OK;
204
}
205
206
Error GDExtensionLibraryLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {
207
#ifdef TOOLS_ENABLED
208
p_extension->set_reloadable(is_reloadable && Engine::get_singleton()->is_extension_reloading_enabled());
209
#endif
210
211
for (const KeyValue<String, String> &icon : class_icon_paths) {
212
p_extension->class_icon_paths[icon.key] = icon.value;
213
}
214
215
void *entry_funcptr = nullptr;
216
217
Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, entry_symbol, entry_funcptr, false);
218
219
if (err != OK) {
220
ERR_PRINT(vformat("GDExtension entry point '%s' not found in library %s.", entry_symbol, library_path));
221
return err;
222
}
223
224
GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;
225
226
GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);
227
228
if (ret) {
229
return OK;
230
} else {
231
ERR_PRINT(vformat("GDExtension initialization function '%s' returned an error.", entry_symbol));
232
return FAILED;
233
}
234
}
235
236
void GDExtensionLibraryLoader::close_library() {
237
OS::get_singleton()->close_dynamic_library(library);
238
library = nullptr;
239
}
240
241
bool GDExtensionLibraryLoader::is_library_open() const {
242
return library != nullptr;
243
}
244
245
bool GDExtensionLibraryLoader::has_library_changed() const {
246
#ifdef TOOLS_ENABLED
247
// Check only that the last modified time is different (rather than checking
248
// that it's newer) since some OS's (namely Windows) will preserve the modified
249
// time by default when copying files.
250
if (FileAccess::get_modified_time(resource_path) != resource_last_modified_time) {
251
return true;
252
}
253
if (FileAccess::get_modified_time(library_path) != library_last_modified_time) {
254
return true;
255
}
256
#endif
257
return false;
258
}
259
260
bool GDExtensionLibraryLoader::library_exists() const {
261
return FileAccess::exists(resource_path);
262
}
263
264
Error GDExtensionLibraryLoader::parse_gdextension_file(const String &p_path) {
265
resource_path = p_path;
266
267
Ref<ConfigFile> config;
268
config.instantiate();
269
270
Error err = config->load(p_path);
271
272
if (err != OK) {
273
ERR_PRINT(vformat("Error loading GDExtension configuration file: '%s'.", p_path));
274
return err;
275
}
276
277
if (!config->has_section_key("configuration", "entry_symbol")) {
278
ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: '%s'.", p_path));
279
return ERR_INVALID_DATA;
280
}
281
282
entry_symbol = config->get_value("configuration", "entry_symbol");
283
284
uint32_t compatibility_minimum[3] = { 0, 0, 0 };
285
if (config->has_section_key("configuration", "compatibility_minimum")) {
286
String compat_string = config->get_value("configuration", "compatibility_minimum");
287
Vector<int> parts = compat_string.split_ints(".");
288
for (int i = 0; i < parts.size(); i++) {
289
if (i >= 3) {
290
break;
291
}
292
if (parts[i] >= 0) {
293
compatibility_minimum[i] = parts[i];
294
}
295
}
296
} else {
297
ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: '%s'.", p_path));
298
return ERR_INVALID_DATA;
299
}
300
301
if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {
302
ERR_PRINT(vformat("GDExtension's compatibility_minimum (%d.%d.%d) must be at least 4.1.0: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
303
return ERR_INVALID_DATA;
304
}
305
306
bool compatible = true;
307
// Check version lexicographically.
308
if (GODOT_VERSION_MAJOR != compatibility_minimum[0]) {
309
compatible = GODOT_VERSION_MAJOR > compatibility_minimum[0];
310
} else if (GODOT_VERSION_MINOR != compatibility_minimum[1]) {
311
compatible = GODOT_VERSION_MINOR > compatibility_minimum[1];
312
} else {
313
compatible = GODOT_VERSION_PATCH >= compatibility_minimum[2];
314
}
315
if (!compatible) {
316
ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s, but your Godot version is %d.%d.%d",
317
compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path,
318
GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));
319
return ERR_INVALID_DATA;
320
}
321
322
// Optionally check maximum compatibility.
323
if (config->has_section_key("configuration", "compatibility_maximum")) {
324
uint32_t compatibility_maximum[3] = { 0, 0, 0 };
325
String compat_string = config->get_value("configuration", "compatibility_maximum");
326
Vector<int> parts = compat_string.split_ints(".");
327
for (int i = 0; i < 3; i++) {
328
if (i < parts.size() && parts[i] >= 0) {
329
compatibility_maximum[i] = parts[i];
330
} else {
331
// If a version part is missing, set the maximum to an arbitrary high value.
332
compatibility_maximum[i] = 9999;
333
}
334
}
335
336
compatible = true;
337
if (GODOT_VERSION_MAJOR != compatibility_maximum[0]) {
338
compatible = GODOT_VERSION_MAJOR < compatibility_maximum[0];
339
} else if (GODOT_VERSION_MINOR != compatibility_maximum[1]) {
340
compatible = GODOT_VERSION_MINOR < compatibility_maximum[1];
341
}
342
#if GODOT_VERSION_PATCH
343
// #if check to avoid -Wtype-limits warning when 0.
344
else {
345
compatible = GODOT_VERSION_PATCH <= compatibility_maximum[2];
346
}
347
#endif
348
349
if (!compatible) {
350
ERR_PRINT(vformat("GDExtension only compatible with Godot version %s or earlier: %s, but your Godot version is %d.%d.%d",
351
compat_string, p_path, GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));
352
return ERR_INVALID_DATA;
353
}
354
}
355
356
library_path = find_extension_library(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
357
358
if (library_path.is_empty()) {
359
const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
360
ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
361
return ERR_FILE_NOT_FOUND;
362
}
363
364
is_static_library = library_path.ends_with(".a") || library_path.ends_with(".xcframework");
365
366
if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
367
library_path = p_path.get_base_dir().path_join(library_path);
368
}
369
370
#ifdef TOOLS_ENABLED
371
is_reloadable = config->get_value("configuration", "reloadable", false);
372
373
update_last_modified_time(
374
FileAccess::get_modified_time(resource_path),
375
FileAccess::get_modified_time(library_path));
376
#endif
377
378
library_dependencies = find_extension_dependencies(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
379
380
// Handle icons if any are specified.
381
if (config->has_section("icons")) {
382
Vector<String> keys = config->get_section_keys("icons");
383
for (const String &key : keys) {
384
String icon_path = config->get_value("icons", key);
385
if (icon_path.is_relative_path()) {
386
icon_path = p_path.get_base_dir().path_join(icon_path);
387
}
388
389
class_icon_paths[key] = icon_path;
390
}
391
}
392
393
return OK;
394
}
395
396