Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/extension/gdextension_library_loader.cpp
20936 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
// Apple has a complex lookup system which goes beyond looking up the filename, so we try that first.
199
err = OS::get_singleton()->open_dynamic_library(abs_path, library, &data);
200
if (err != OK) {
201
err = OS::get_singleton()->open_dynamic_library(String(), library, &data);
202
if (err != OK) {
203
return err;
204
}
205
}
206
207
return OK;
208
}
209
210
Error GDExtensionLibraryLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {
211
#ifdef TOOLS_ENABLED
212
p_extension->set_reloadable(is_reloadable && Engine::get_singleton()->is_extension_reloading_enabled());
213
#endif
214
215
for (const KeyValue<String, String> &icon : class_icon_paths) {
216
p_extension->class_icon_paths[icon.key] = icon.value;
217
}
218
219
void *entry_funcptr = nullptr;
220
221
Error err = OS::get_singleton()->get_dynamic_library_symbol_handle(library, entry_symbol, entry_funcptr, false);
222
223
if (err != OK) {
224
ERR_PRINT(vformat("GDExtension entry point '%s' not found in library %s.", entry_symbol, library_path));
225
return err;
226
}
227
228
GDExtensionInitializationFunction initialization_function = (GDExtensionInitializationFunction)entry_funcptr;
229
230
GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);
231
232
if (ret) {
233
return OK;
234
} else {
235
ERR_PRINT(vformat("GDExtension initialization function '%s' returned an error.", entry_symbol));
236
return FAILED;
237
}
238
}
239
240
void GDExtensionLibraryLoader::close_library() {
241
OS::get_singleton()->close_dynamic_library(library);
242
library = nullptr;
243
}
244
245
bool GDExtensionLibraryLoader::is_library_open() const {
246
return library != nullptr;
247
}
248
249
bool GDExtensionLibraryLoader::has_library_changed() const {
250
#ifdef TOOLS_ENABLED
251
// Check only that the last modified time is different (rather than checking
252
// that it's newer) since some OS's (namely Windows) will preserve the modified
253
// time by default when copying files.
254
if (FileAccess::get_modified_time(resource_path) != resource_last_modified_time) {
255
return true;
256
}
257
if (FileAccess::get_modified_time(library_path) != library_last_modified_time) {
258
return true;
259
}
260
#endif
261
return false;
262
}
263
264
bool GDExtensionLibraryLoader::library_exists() const {
265
return FileAccess::exists(resource_path);
266
}
267
268
Error GDExtensionLibraryLoader::parse_gdextension_file(const String &p_path) {
269
resource_path = p_path;
270
271
Ref<ConfigFile> config;
272
config.instantiate();
273
274
Error err = config->load(p_path);
275
276
if (err != OK) {
277
ERR_PRINT(vformat("Error loading GDExtension configuration file: '%s'.", p_path));
278
return err;
279
}
280
281
if (!config->has_section_key("configuration", "entry_symbol")) {
282
ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: '%s'.", p_path));
283
return ERR_INVALID_DATA;
284
}
285
286
entry_symbol = config->get_value("configuration", "entry_symbol");
287
288
uint32_t compatibility_minimum[3] = { 0, 0, 0 };
289
if (config->has_section_key("configuration", "compatibility_minimum")) {
290
String compat_string = config->get_value("configuration", "compatibility_minimum");
291
Vector<int> parts = compat_string.split_ints(".");
292
for (int i = 0; i < parts.size(); i++) {
293
if (i >= 3) {
294
break;
295
}
296
if (parts[i] >= 0) {
297
compatibility_minimum[i] = parts[i];
298
}
299
}
300
} else {
301
ERR_PRINT(vformat("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: '%s'.", p_path));
302
return ERR_INVALID_DATA;
303
}
304
305
if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {
306
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));
307
return ERR_INVALID_DATA;
308
}
309
310
bool compatible = true;
311
// Check version lexicographically.
312
if (GODOT_VERSION_MAJOR != compatibility_minimum[0]) {
313
compatible = GODOT_VERSION_MAJOR > compatibility_minimum[0];
314
} else if (GODOT_VERSION_MINOR != compatibility_minimum[1]) {
315
compatible = GODOT_VERSION_MINOR > compatibility_minimum[1];
316
} else {
317
compatible = GODOT_VERSION_PATCH >= compatibility_minimum[2];
318
}
319
if (!compatible) {
320
ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s, but your Godot version is %d.%d.%d",
321
compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path,
322
GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));
323
return ERR_INVALID_DATA;
324
}
325
326
// Optionally check maximum compatibility.
327
if (config->has_section_key("configuration", "compatibility_maximum")) {
328
uint32_t compatibility_maximum[3] = { 0, 0, 0 };
329
String compat_string = config->get_value("configuration", "compatibility_maximum");
330
Vector<int> parts = compat_string.split_ints(".");
331
for (int i = 0; i < 3; i++) {
332
if (i < parts.size() && parts[i] >= 0) {
333
compatibility_maximum[i] = parts[i];
334
} else {
335
// If a version part is missing, set the maximum to an arbitrary high value.
336
compatibility_maximum[i] = 9999;
337
}
338
}
339
340
compatible = true;
341
if (GODOT_VERSION_MAJOR != compatibility_maximum[0]) {
342
compatible = GODOT_VERSION_MAJOR < compatibility_maximum[0];
343
} else if (GODOT_VERSION_MINOR != compatibility_maximum[1]) {
344
compatible = GODOT_VERSION_MINOR < compatibility_maximum[1];
345
}
346
#if GODOT_VERSION_PATCH
347
// #if check to avoid -Wtype-limits warning when 0.
348
else {
349
compatible = GODOT_VERSION_PATCH <= compatibility_maximum[2];
350
}
351
#endif
352
353
if (!compatible) {
354
ERR_PRINT(vformat("GDExtension only compatible with Godot version %s or earlier: %s, but your Godot version is %d.%d.%d",
355
compat_string, p_path, GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR, GODOT_VERSION_PATCH));
356
return ERR_INVALID_DATA;
357
}
358
}
359
360
library_path = find_extension_library(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
361
362
if (library_path.is_empty()) {
363
const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
364
ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
365
return ERR_FILE_NOT_FOUND;
366
}
367
368
if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
369
library_path = p_path.get_base_dir().path_join(library_path);
370
}
371
372
#ifdef TOOLS_ENABLED
373
is_reloadable = config->get_value("configuration", "reloadable", false);
374
375
update_last_modified_time(
376
FileAccess::get_modified_time(resource_path),
377
FileAccess::get_modified_time(library_path));
378
#endif
379
380
library_dependencies = find_extension_dependencies(p_path, config, [](const String &p_feature) { return OS::get_singleton()->has_feature(p_feature); });
381
382
// Handle icons if any are specified.
383
if (config->has_section("icons")) {
384
Vector<String> keys = config->get_section_keys("icons");
385
for (const String &key : keys) {
386
String icon_path = config->get_value("icons", key);
387
if (icon_path.is_relative_path()) {
388
icon_path = p_path.get_base_dir().path_join(icon_path);
389
}
390
391
class_icon_paths[key] = icon_path;
392
}
393
}
394
395
return OK;
396
}
397
398