Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/core_bind.cpp
20874 views
1
/**************************************************************************/
2
/* core_bind.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 "core_bind.h"
32
#include "core_bind.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "core/crypto/crypto_core.h"
36
#include "core/debugger/engine_debugger.h"
37
#include "core/debugger/script_debugger.h"
38
#include "core/io/file_access.h"
39
#include "core/io/marshalls.h"
40
#include "core/math/geometry_2d.h"
41
#include "core/math/geometry_3d.h"
42
#include "core/os/keyboard.h"
43
#include "core/os/main_loop.h"
44
#include "core/os/thread_safe.h"
45
#include "core/variant/typed_array.h"
46
47
namespace CoreBind {
48
49
////// ResourceLoader //////
50
51
Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, CacheMode p_cache_mode) {
52
return ::ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads, ResourceFormatLoader::CacheMode(p_cache_mode));
53
}
54
55
ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
56
// Progress being the default array indicates the user hasn't requested for it to be computed.
57
// Default array should never be modified, it causes the hash of the method to change.
58
const bool return_progress = !ClassDB::is_default_array_arg(r_progress);
59
float progress = 0;
60
::ResourceLoader::ThreadLoadStatus tls = ::ResourceLoader::load_threaded_get_status(p_path, return_progress ? &progress : nullptr);
61
if (return_progress) {
62
r_progress.resize(1);
63
r_progress[0] = progress;
64
}
65
return (ThreadLoadStatus)tls;
66
}
67
68
Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path) {
69
Error error;
70
Ref<Resource> res = ::ResourceLoader::load_threaded_get(p_path, &error);
71
return res;
72
}
73
74
Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, CacheMode p_cache_mode) {
75
Error err = OK;
76
Ref<Resource> ret = ::ResourceLoader::load(p_path, p_type_hint, ResourceFormatLoader::CacheMode(p_cache_mode), &err);
77
78
ERR_FAIL_COND_V_MSG(err != OK, ret, vformat("Error loading resource: '%s'.", p_path));
79
return ret;
80
}
81
82
Vector<String> ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
83
List<String> exts;
84
::ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
85
Vector<String> ret;
86
for (const String &E : exts) {
87
ret.push_back(E);
88
}
89
90
return ret;
91
}
92
93
void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) {
94
::ResourceLoader::add_resource_format_loader(p_format_loader, p_at_front);
95
}
96
97
void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) {
98
::ResourceLoader::remove_resource_format_loader(p_format_loader);
99
}
100
101
void ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
102
::ResourceLoader::set_abort_on_missing_resources(p_abort);
103
}
104
105
PackedStringArray ResourceLoader::get_dependencies(const String &p_path) {
106
List<String> deps;
107
::ResourceLoader::get_dependencies(p_path, &deps);
108
109
PackedStringArray ret;
110
for (const String &E : deps) {
111
ret.push_back(E);
112
}
113
114
return ret;
115
}
116
117
bool ResourceLoader::has_cached(const String &p_path) {
118
String local_path = ::ResourceLoader::_validate_local_path(p_path);
119
return ResourceCache::has(local_path);
120
}
121
122
Ref<Resource> ResourceLoader::get_cached_ref(const String &p_path) {
123
String local_path = ::ResourceLoader::_validate_local_path(p_path);
124
return ResourceCache::get_ref(local_path);
125
}
126
127
bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
128
return ::ResourceLoader::exists(p_path, p_type_hint);
129
}
130
131
ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
132
return ::ResourceLoader::get_resource_uid(p_path);
133
}
134
135
Vector<String> ResourceLoader::list_directory(const String &p_directory) {
136
return ::ResourceLoader::list_directory(p_directory);
137
}
138
139
void ResourceLoader::_bind_methods() {
140
ClassDB::bind_method(D_METHOD("load_threaded_request", "path", "type_hint", "use_sub_threads", "cache_mode"), &ResourceLoader::load_threaded_request, DEFVAL(""), DEFVAL(false), DEFVAL(CACHE_MODE_REUSE));
141
ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &ResourceLoader::load_threaded_get_status, DEFVAL_ARRAY);
142
ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &ResourceLoader::load_threaded_get);
143
144
ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
145
ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &ResourceLoader::get_recognized_extensions_for_type);
146
ClassDB::bind_method(D_METHOD("add_resource_format_loader", "format_loader", "at_front"), &ResourceLoader::add_resource_format_loader, DEFVAL(false));
147
ClassDB::bind_method(D_METHOD("remove_resource_format_loader", "format_loader"), &ResourceLoader::remove_resource_format_loader);
148
ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &ResourceLoader::set_abort_on_missing_resources);
149
ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &ResourceLoader::get_dependencies);
150
ClassDB::bind_method(D_METHOD("has_cached", "path"), &ResourceLoader::has_cached);
151
ClassDB::bind_method(D_METHOD("get_cached_ref", "path"), &ResourceLoader::get_cached_ref);
152
ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &ResourceLoader::exists, DEFVAL(""));
153
ClassDB::bind_method(D_METHOD("get_resource_uid", "path"), &ResourceLoader::get_resource_uid);
154
ClassDB::bind_method(D_METHOD("list_directory", "directory_path"), &ResourceLoader::list_directory);
155
156
BIND_ENUM_CONSTANT(THREAD_LOAD_INVALID_RESOURCE);
157
BIND_ENUM_CONSTANT(THREAD_LOAD_IN_PROGRESS);
158
BIND_ENUM_CONSTANT(THREAD_LOAD_FAILED);
159
BIND_ENUM_CONSTANT(THREAD_LOAD_LOADED);
160
161
BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE);
162
BIND_ENUM_CONSTANT(CACHE_MODE_REUSE);
163
BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);
164
BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE_DEEP);
165
BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE_DEEP);
166
}
167
168
////// ResourceSaver //////
169
170
Error ResourceSaver::save(RequiredParam<Resource> p_resource, const String &p_path, BitField<SaverFlags> p_flags) {
171
return ::ResourceSaver::save(p_resource, p_path, p_flags);
172
}
173
174
Error ResourceSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
175
return ::ResourceSaver::set_uid(p_path, p_uid);
176
}
177
178
Vector<String> ResourceSaver::get_recognized_extensions(const Ref<Resource> &p_resource) {
179
List<String> exts;
180
::ResourceSaver::get_recognized_extensions(p_resource, &exts);
181
Vector<String> ret;
182
for (const String &E : exts) {
183
ret.push_back(E);
184
}
185
return ret;
186
}
187
188
void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
189
::ResourceSaver::add_resource_format_saver(p_format_saver, p_at_front);
190
}
191
192
void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
193
::ResourceSaver::remove_resource_format_saver(p_format_saver);
194
}
195
196
ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
197
return ::ResourceSaver::get_resource_id_for_path(p_path, p_generate);
198
}
199
200
void ResourceSaver::_bind_methods() {
201
ClassDB::bind_method(D_METHOD("save", "resource", "path", "flags"), &ResourceSaver::save, DEFVAL(""), DEFVAL((uint32_t)FLAG_NONE));
202
ClassDB::bind_method(D_METHOD("set_uid", "resource", "uid"), &ResourceSaver::set_uid);
203
ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &ResourceSaver::get_recognized_extensions);
204
ClassDB::bind_method(D_METHOD("add_resource_format_saver", "format_saver", "at_front"), &ResourceSaver::add_resource_format_saver, DEFVAL(false));
205
ClassDB::bind_method(D_METHOD("remove_resource_format_saver", "format_saver"), &ResourceSaver::remove_resource_format_saver);
206
ClassDB::bind_method(D_METHOD("get_resource_id_for_path", "path", "generate"), &ResourceSaver::get_resource_id_for_path, DEFVAL(false));
207
208
BIND_BITFIELD_FLAG(FLAG_NONE);
209
BIND_BITFIELD_FLAG(FLAG_RELATIVE_PATHS);
210
BIND_BITFIELD_FLAG(FLAG_BUNDLE_RESOURCES);
211
BIND_BITFIELD_FLAG(FLAG_CHANGE_PATH);
212
BIND_BITFIELD_FLAG(FLAG_OMIT_EDITOR_PROPERTIES);
213
BIND_BITFIELD_FLAG(FLAG_SAVE_BIG_ENDIAN);
214
BIND_BITFIELD_FLAG(FLAG_COMPRESS);
215
BIND_BITFIELD_FLAG(FLAG_REPLACE_SUBRESOURCE_PATHS);
216
}
217
218
////// Logger ///////
219
220
void Logger::_bind_methods() {
221
GDVIRTUAL_BIND(_log_error, "function", "file", "line", "code", "rationale", "editor_notify", "error_type", "script_backtraces");
222
GDVIRTUAL_BIND(_log_message, "message", "error");
223
BIND_ENUM_CONSTANT(ERROR_TYPE_ERROR);
224
BIND_ENUM_CONSTANT(ERROR_TYPE_WARNING);
225
BIND_ENUM_CONSTANT(ERROR_TYPE_SCRIPT);
226
BIND_ENUM_CONSTANT(ERROR_TYPE_SHADER);
227
}
228
229
void Logger::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const TypedArray<ScriptBacktrace> &p_script_backtraces) {
230
GDVIRTUAL_CALL(_log_error, String::utf8(p_function), String::utf8(p_file), p_line, String::utf8(p_code), String::utf8(p_rationale), p_editor_notify, p_type, p_script_backtraces);
231
}
232
233
void Logger::log_message(const String &p_text, bool p_error) {
234
GDVIRTUAL_CALL(_log_message, p_text, p_error);
235
}
236
237
////// OS //////
238
239
void OS::LoggerBind::logv(const char *p_format, va_list p_list, bool p_err) {
240
if (!should_log(p_err)) {
241
return;
242
}
243
244
constexpr int static_buf_size = 1024;
245
char static_buf[static_buf_size] = { '\0' };
246
char *buf = static_buf;
247
va_list list_copy;
248
va_copy(list_copy, p_list);
249
int len = vsnprintf(buf, static_buf_size, p_format, p_list);
250
if (len >= static_buf_size) {
251
buf = (char *)Memory::alloc_static(len + 1);
252
vsnprintf(buf, len + 1, p_format, list_copy);
253
}
254
va_end(list_copy);
255
256
String str;
257
str.append_utf8(buf, len);
258
for (Ref<CoreBind::Logger> &logger : loggers) {
259
logger->log_message(str, p_err);
260
}
261
262
if (len >= static_buf_size) {
263
Memory::free_static(buf);
264
}
265
}
266
267
void OS::LoggerBind::log_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, ErrorType p_type, const Vector<Ref<ScriptBacktrace>> &p_script_backtraces) {
268
if (!should_log(true)) {
269
return;
270
}
271
272
TypedArray<ScriptBacktrace> backtraces;
273
backtraces.resize(p_script_backtraces.size());
274
for (int i = 0; i < p_script_backtraces.size(); i++) {
275
backtraces[i] = p_script_backtraces[i];
276
}
277
278
for (Ref<CoreBind::Logger> &logger : loggers) {
279
logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, CoreBind::Logger::ErrorType(p_type), backtraces);
280
}
281
}
282
283
PackedByteArray OS::get_entropy(int p_bytes) {
284
PackedByteArray pba;
285
pba.resize(p_bytes);
286
Error err = ::OS::get_singleton()->get_entropy(pba.ptrw(), p_bytes);
287
ERR_FAIL_COND_V(err != OK, PackedByteArray());
288
return pba;
289
}
290
291
String OS::get_system_ca_certificates() {
292
return ::OS::get_singleton()->get_system_ca_certificates();
293
}
294
295
PackedStringArray OS::get_connected_midi_inputs() {
296
return ::OS::get_singleton()->get_connected_midi_inputs();
297
}
298
299
void OS::open_midi_inputs() {
300
::OS::get_singleton()->open_midi_inputs();
301
}
302
303
void OS::close_midi_inputs() {
304
::OS::get_singleton()->close_midi_inputs();
305
}
306
307
void OS::set_use_file_access_save_and_swap(bool p_enable) {
308
FileAccess::set_backup_save(p_enable);
309
}
310
311
void OS::set_low_processor_usage_mode(bool p_enabled) {
312
::OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
313
}
314
315
bool OS::is_in_low_processor_usage_mode() const {
316
return ::OS::get_singleton()->is_in_low_processor_usage_mode();
317
}
318
319
void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
320
::OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(p_usec);
321
}
322
323
int OS::get_low_processor_usage_mode_sleep_usec() const {
324
return ::OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
325
}
326
327
void OS::set_delta_smoothing(bool p_enabled) {
328
::OS::get_singleton()->set_delta_smoothing(p_enabled);
329
}
330
331
bool OS::is_delta_smoothing_enabled() const {
332
return ::OS::get_singleton()->is_delta_smoothing_enabled();
333
}
334
335
void OS::alert(const String &p_alert, const String &p_title) {
336
::OS::get_singleton()->alert(p_alert, p_title);
337
}
338
339
void OS::crash(const String &p_message) {
340
CRASH_NOW_MSG(p_message);
341
}
342
343
Vector<String> OS::get_system_fonts() const {
344
return ::OS::get_singleton()->get_system_fonts();
345
}
346
347
String OS::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
348
return ::OS::get_singleton()->get_system_font_path(p_font_name, p_weight, p_stretch, p_italic);
349
}
350
351
Vector<String> OS::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const {
352
return ::OS::get_singleton()->get_system_font_path_for_text(p_font_name, p_text, p_locale, p_script, p_weight, p_stretch, p_italic);
353
}
354
355
String OS::get_executable_path() const {
356
return ::OS::get_singleton()->get_executable_path();
357
}
358
359
Error OS::shell_open(const String &p_uri) {
360
if (p_uri.begins_with("res://")) {
361
WARN_PRINT("Attempting to open an URL with the \"res://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_open()`.");
362
} else if (p_uri.begins_with("user://")) {
363
WARN_PRINT("Attempting to open an URL with the \"user://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_open()`.");
364
}
365
return ::OS::get_singleton()->shell_open(p_uri);
366
}
367
368
Error OS::shell_show_in_file_manager(const String &p_path, bool p_open_folder) {
369
if (p_path.begins_with("res://")) {
370
WARN_PRINT("Attempting to explore file path with the \"res://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_show_in_file_manager()`.");
371
} else if (p_path.begins_with("user://")) {
372
WARN_PRINT("Attempting to explore file path with the \"user://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_show_in_file_manager()`.");
373
}
374
return ::OS::get_singleton()->shell_show_in_file_manager(p_path, p_open_folder);
375
}
376
377
String OS::read_string_from_stdin(int64_t p_buffer_size) {
378
return ::OS::get_singleton()->get_stdin_string(p_buffer_size);
379
}
380
381
PackedByteArray OS::read_buffer_from_stdin(int64_t p_buffer_size) {
382
return ::OS::get_singleton()->get_stdin_buffer(p_buffer_size);
383
}
384
385
OS::StdHandleType OS::get_stdin_type() const {
386
return (OS::StdHandleType)::OS::get_singleton()->get_stdin_type();
387
}
388
389
OS::StdHandleType OS::get_stdout_type() const {
390
return (OS::StdHandleType)::OS::get_singleton()->get_stdout_type();
391
}
392
393
OS::StdHandleType OS::get_stderr_type() const {
394
return (OS::StdHandleType)::OS::get_singleton()->get_stderr_type();
395
}
396
397
int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) {
398
List<String> args;
399
for (const String &arg : p_arguments) {
400
args.push_back(arg);
401
}
402
String pipe;
403
int exitcode = 0;
404
Error err = ::OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr, nullptr, p_open_console);
405
// Default array should never be modified, it causes the hash of the method to change.
406
if (!ClassDB::is_default_array_arg(r_output)) {
407
r_output.push_back(pipe);
408
}
409
if (err != OK) {
410
return -1;
411
}
412
return exitcode;
413
}
414
415
Dictionary OS::execute_with_pipe(const String &p_path, const Vector<String> &p_arguments, bool p_blocking) {
416
List<String> args;
417
for (const String &arg : p_arguments) {
418
args.push_back(arg);
419
}
420
return ::OS::get_singleton()->execute_with_pipe(p_path, args, p_blocking);
421
}
422
423
int OS::create_instance(const Vector<String> &p_arguments) {
424
List<String> args;
425
for (const String &arg : p_arguments) {
426
args.push_back(arg);
427
}
428
::OS::ProcessID pid = 0;
429
Error err = ::OS::get_singleton()->create_instance(args, &pid);
430
if (err != OK) {
431
return -1;
432
}
433
return pid;
434
}
435
436
Error OS::open_with_program(const String &p_program_path, const Vector<String> &p_paths) {
437
List<String> paths;
438
for (const String &path : p_paths) {
439
paths.push_back(path);
440
}
441
return ::OS::get_singleton()->open_with_program(p_program_path, paths);
442
}
443
444
int OS::create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console) {
445
List<String> args;
446
for (const String &arg : p_arguments) {
447
args.push_back(arg);
448
}
449
::OS::ProcessID pid = 0;
450
Error err = ::OS::get_singleton()->create_process(p_path, args, &pid, p_open_console);
451
if (err != OK) {
452
return -1;
453
}
454
return pid;
455
}
456
457
Error OS::kill(int p_pid) {
458
return ::OS::get_singleton()->kill(p_pid);
459
}
460
461
bool OS::is_process_running(int p_pid) const {
462
return ::OS::get_singleton()->is_process_running(p_pid);
463
}
464
465
int OS::get_process_exit_code(int p_pid) const {
466
return ::OS::get_singleton()->get_process_exit_code(p_pid);
467
}
468
469
int OS::get_process_id() const {
470
return ::OS::get_singleton()->get_process_id();
471
}
472
473
bool OS::has_environment(const String &p_var) const {
474
return ::OS::get_singleton()->has_environment(p_var);
475
}
476
477
String OS::get_environment(const String &p_var) const {
478
return ::OS::get_singleton()->get_environment(p_var);
479
}
480
481
void OS::set_environment(const String &p_var, const String &p_value) const {
482
::OS::get_singleton()->set_environment(p_var, p_value);
483
}
484
485
void OS::unset_environment(const String &p_var) const {
486
::OS::get_singleton()->unset_environment(p_var);
487
}
488
489
String OS::get_name() const {
490
return ::OS::get_singleton()->get_name();
491
}
492
493
String OS::get_distribution_name() const {
494
return ::OS::get_singleton()->get_distribution_name();
495
}
496
497
String OS::get_version() const {
498
return ::OS::get_singleton()->get_version();
499
}
500
501
String OS::get_version_alias() const {
502
return ::OS::get_singleton()->get_version_alias();
503
}
504
505
Vector<String> OS::get_video_adapter_driver_info() const {
506
return ::OS::get_singleton()->get_video_adapter_driver_info();
507
}
508
509
Vector<String> OS::get_cmdline_args() {
510
List<String> cmdline = ::OS::get_singleton()->get_cmdline_args();
511
Vector<String> cmdlinev;
512
for (const String &E : cmdline) {
513
cmdlinev.push_back(E);
514
}
515
516
return cmdlinev;
517
}
518
519
Vector<String> OS::get_cmdline_user_args() {
520
List<String> cmdline = ::OS::get_singleton()->get_cmdline_user_args();
521
Vector<String> cmdlinev;
522
for (const String &E : cmdline) {
523
cmdlinev.push_back(E);
524
}
525
526
return cmdlinev;
527
}
528
529
void OS::set_restart_on_exit(bool p_restart, const Vector<String> &p_restart_arguments) {
530
List<String> args_list;
531
for (const String &restart_argument : p_restart_arguments) {
532
args_list.push_back(restart_argument);
533
}
534
535
::OS::get_singleton()->set_restart_on_exit(p_restart, args_list);
536
}
537
538
bool OS::is_restart_on_exit_set() const {
539
return ::OS::get_singleton()->is_restart_on_exit_set();
540
}
541
542
Vector<String> OS::get_restart_on_exit_arguments() const {
543
List<String> args = ::OS::get_singleton()->get_restart_on_exit_arguments();
544
Vector<String> args_vector;
545
for (const String &arg : args) {
546
args_vector.push_back(arg);
547
}
548
549
return args_vector;
550
}
551
552
String OS::get_locale() const {
553
return ::OS::get_singleton()->get_locale();
554
}
555
556
String OS::get_locale_language() const {
557
return ::OS::get_singleton()->get_locale_language();
558
}
559
560
String OS::get_model_name() const {
561
return ::OS::get_singleton()->get_model_name();
562
}
563
564
Error OS::set_thread_name(const String &p_name) {
565
return ::Thread::set_name(p_name);
566
}
567
568
::Thread::ID OS::get_thread_caller_id() const {
569
return ::Thread::get_caller_id();
570
}
571
572
::Thread::ID OS::get_main_thread_id() const {
573
return ::Thread::get_main_id();
574
}
575
576
bool OS::has_feature(const String &p_feature) const {
577
const bool *value_ptr = feature_cache.getptr(p_feature);
578
if (value_ptr) {
579
return *value_ptr;
580
} else {
581
const bool has = ::OS::get_singleton()->has_feature(p_feature);
582
feature_cache[p_feature] = has;
583
return has;
584
}
585
}
586
587
bool OS::is_sandboxed() const {
588
return ::OS::get_singleton()->is_sandboxed();
589
}
590
591
uint64_t OS::get_static_memory_usage() const {
592
return ::OS::get_singleton()->get_static_memory_usage();
593
}
594
595
uint64_t OS::get_static_memory_peak_usage() const {
596
return ::OS::get_singleton()->get_static_memory_peak_usage();
597
}
598
599
Dictionary OS::get_memory_info() const {
600
return ::OS::get_singleton()->get_memory_info();
601
}
602
603
/** This method uses a signed argument for better error reporting as it's used from the scripting API. */
604
void OS::delay_usec(int p_usec) const {
605
ERR_FAIL_COND_MSG(
606
p_usec < 0,
607
vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
608
::OS::get_singleton()->delay_usec(p_usec);
609
}
610
611
/** This method uses a signed argument for better error reporting as it's used from the scripting API. */
612
void OS::delay_msec(int p_msec) const {
613
ERR_FAIL_COND_MSG(
614
p_msec < 0,
615
vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
616
::OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
617
}
618
619
bool OS::is_userfs_persistent() const {
620
return ::OS::get_singleton()->is_userfs_persistent();
621
}
622
623
int OS::get_processor_count() const {
624
return ::OS::get_singleton()->get_processor_count();
625
}
626
627
String OS::get_processor_name() const {
628
return ::OS::get_singleton()->get_processor_name();
629
}
630
631
bool OS::is_stdout_verbose() const {
632
return ::OS::get_singleton()->is_stdout_verbose();
633
}
634
635
Error OS::move_to_trash(const String &p_path) const {
636
return ::OS::get_singleton()->move_to_trash(p_path);
637
}
638
639
String OS::get_user_data_dir() const {
640
return ::OS::get_singleton()->get_user_data_dir();
641
}
642
643
String OS::get_config_dir() const {
644
// Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
645
return ::OS::get_singleton()->get_config_path();
646
}
647
648
String OS::get_data_dir() const {
649
// Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
650
return ::OS::get_singleton()->get_data_path();
651
}
652
653
String OS::get_cache_dir() const {
654
// Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
655
return ::OS::get_singleton()->get_cache_path();
656
}
657
658
String OS::get_temp_dir() const {
659
// Exposed as `get_temp_dir()` instead of `get_temp_path()` for consistency with other exposed OS methods.
660
return ::OS::get_singleton()->get_temp_path();
661
}
662
663
bool OS::is_debug_build() const {
664
#ifdef DEBUG_ENABLED
665
return true;
666
#else
667
return false;
668
#endif // DEBUG_ENABLED
669
}
670
671
String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
672
return ::OS::get_singleton()->get_system_dir(::OS::SystemDir(p_dir), p_shared_storage);
673
}
674
675
String OS::get_keycode_string(Key p_code) const {
676
return ::keycode_get_string(p_code);
677
}
678
679
bool OS::is_keycode_unicode(char32_t p_unicode) const {
680
return ::keycode_has_unicode((Key)p_unicode);
681
}
682
683
Key OS::find_keycode_from_string(const String &p_code) const {
684
return find_keycode(p_code);
685
}
686
687
bool OS::request_permission(const String &p_name) {
688
return ::OS::get_singleton()->request_permission(p_name);
689
}
690
691
bool OS::request_permissions() {
692
return ::OS::get_singleton()->request_permissions();
693
}
694
695
Vector<String> OS::get_granted_permissions() const {
696
return ::OS::get_singleton()->get_granted_permissions();
697
}
698
699
void OS::revoke_granted_permissions() {
700
::OS::get_singleton()->revoke_granted_permissions();
701
}
702
703
String OS::get_unique_id() const {
704
return ::OS::get_singleton()->get_unique_id();
705
}
706
707
void OS::add_logger(const Ref<Logger> &p_logger) {
708
ERR_FAIL_COND(p_logger.is_null());
709
710
if (!logger_bind) {
711
logger_bind = memnew(LoggerBind);
712
::OS::get_singleton()->add_logger(logger_bind);
713
}
714
715
ERR_FAIL_COND_MSG(logger_bind->loggers.find(p_logger) != -1, "Could not add logger, as it has already been added.");
716
logger_bind->loggers.push_back(p_logger);
717
}
718
719
void OS::remove_logger(const Ref<Logger> &p_logger) {
720
ERR_FAIL_COND(p_logger.is_null());
721
ERR_FAIL_COND_MSG(!logger_bind || logger_bind->loggers.find(p_logger) == -1, "Could not remove logger, as it hasn't been added.");
722
logger_bind->loggers.erase(p_logger);
723
}
724
725
void OS::remove_script_loggers(const ScriptLanguage *p_script) {
726
if (logger_bind) {
727
LocalVector<Ref<CoreBind::Logger>> to_remove;
728
for (const Ref<CoreBind::Logger> &logger : logger_bind->loggers) {
729
if (logger.is_null()) {
730
continue;
731
}
732
ScriptInstance *si = logger->get_script_instance();
733
if (!si) {
734
continue;
735
}
736
if (si->get_language() == p_script) {
737
to_remove.push_back(logger);
738
}
739
}
740
for (const Ref<CoreBind::Logger> &logger : to_remove) {
741
logger_bind->loggers.erase(logger);
742
}
743
}
744
}
745
746
void OS::_bind_methods() {
747
ClassDB::bind_method(D_METHOD("get_entropy", "size"), &OS::get_entropy);
748
ClassDB::bind_method(D_METHOD("get_system_ca_certificates"), &OS::get_system_ca_certificates);
749
ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &OS::get_connected_midi_inputs);
750
ClassDB::bind_method(D_METHOD("open_midi_inputs"), &OS::open_midi_inputs);
751
ClassDB::bind_method(D_METHOD("close_midi_inputs"), &OS::close_midi_inputs);
752
753
ClassDB::bind_method(D_METHOD("alert", "text", "title"), &OS::alert, DEFVAL("Alert!"));
754
ClassDB::bind_method(D_METHOD("crash", "message"), &OS::crash);
755
756
ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &OS::set_low_processor_usage_mode);
757
ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &OS::is_in_low_processor_usage_mode);
758
759
ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &OS::set_low_processor_usage_mode_sleep_usec);
760
ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &OS::get_low_processor_usage_mode_sleep_usec);
761
762
ClassDB::bind_method(D_METHOD("set_delta_smoothing", "delta_smoothing_enabled"), &OS::set_delta_smoothing);
763
ClassDB::bind_method(D_METHOD("is_delta_smoothing_enabled"), &OS::is_delta_smoothing_enabled);
764
765
ClassDB::bind_method(D_METHOD("get_processor_count"), &OS::get_processor_count);
766
ClassDB::bind_method(D_METHOD("get_processor_name"), &OS::get_processor_name);
767
768
ClassDB::bind_method(D_METHOD("get_system_fonts"), &OS::get_system_fonts);
769
ClassDB::bind_method(D_METHOD("get_system_font_path", "font_name", "weight", "stretch", "italic"), &OS::get_system_font_path, DEFVAL(400), DEFVAL(100), DEFVAL(false));
770
ClassDB::bind_method(D_METHOD("get_system_font_path_for_text", "font_name", "text", "locale", "script", "weight", "stretch", "italic"), &OS::get_system_font_path_for_text, DEFVAL(String()), DEFVAL(String()), DEFVAL(400), DEFVAL(100), DEFVAL(false));
771
ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
772
773
ClassDB::bind_method(D_METHOD("read_string_from_stdin", "buffer_size"), &OS::read_string_from_stdin, DEFVAL(1024));
774
ClassDB::bind_method(D_METHOD("read_buffer_from_stdin", "buffer_size"), &OS::read_buffer_from_stdin, DEFVAL(1024));
775
ClassDB::bind_method(D_METHOD("get_stdin_type"), &OS::get_stdin_type);
776
ClassDB::bind_method(D_METHOD("get_stdout_type"), &OS::get_stdout_type);
777
ClassDB::bind_method(D_METHOD("get_stderr_type"), &OS::get_stderr_type);
778
779
ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL_ARRAY, DEFVAL(false), DEFVAL(false));
780
ClassDB::bind_method(D_METHOD("execute_with_pipe", "path", "arguments", "blocking"), &OS::execute_with_pipe, DEFVAL(true));
781
ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
782
ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
783
ClassDB::bind_method(D_METHOD("open_with_program", "program_path", "paths"), &OS::open_with_program);
784
ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
785
ClassDB::bind_method(D_METHOD("shell_open", "uri"), &OS::shell_open);
786
ClassDB::bind_method(D_METHOD("shell_show_in_file_manager", "file_or_dir_path", "open_folder"), &OS::shell_show_in_file_manager, DEFVAL(true));
787
ClassDB::bind_method(D_METHOD("is_process_running", "pid"), &OS::is_process_running);
788
ClassDB::bind_method(D_METHOD("get_process_exit_code", "pid"), &OS::get_process_exit_code);
789
ClassDB::bind_method(D_METHOD("get_process_id"), &OS::get_process_id);
790
791
ClassDB::bind_method(D_METHOD("has_environment", "variable"), &OS::has_environment);
792
ClassDB::bind_method(D_METHOD("get_environment", "variable"), &OS::get_environment);
793
ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &OS::set_environment);
794
ClassDB::bind_method(D_METHOD("unset_environment", "variable"), &OS::unset_environment);
795
796
ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name);
797
ClassDB::bind_method(D_METHOD("get_distribution_name"), &OS::get_distribution_name);
798
ClassDB::bind_method(D_METHOD("get_version"), &OS::get_version);
799
ClassDB::bind_method(D_METHOD("get_version_alias"), &OS::get_version_alias);
800
ClassDB::bind_method(D_METHOD("get_cmdline_args"), &OS::get_cmdline_args);
801
ClassDB::bind_method(D_METHOD("get_cmdline_user_args"), &OS::get_cmdline_user_args);
802
803
ClassDB::bind_method(D_METHOD("get_video_adapter_driver_info"), &OS::get_video_adapter_driver_info);
804
805
ClassDB::bind_method(D_METHOD("set_restart_on_exit", "restart", "arguments"), &OS::set_restart_on_exit, DEFVAL(Vector<String>()));
806
ClassDB::bind_method(D_METHOD("is_restart_on_exit_set"), &OS::is_restart_on_exit_set);
807
ClassDB::bind_method(D_METHOD("get_restart_on_exit_arguments"), &OS::get_restart_on_exit_arguments);
808
809
ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &OS::delay_usec);
810
ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &OS::delay_msec);
811
ClassDB::bind_method(D_METHOD("get_locale"), &OS::get_locale);
812
ClassDB::bind_method(D_METHOD("get_locale_language"), &OS::get_locale_language);
813
ClassDB::bind_method(D_METHOD("get_model_name"), &OS::get_model_name);
814
815
ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &OS::is_userfs_persistent);
816
ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &OS::is_stdout_verbose);
817
818
ClassDB::bind_method(D_METHOD("is_debug_build"), &OS::is_debug_build);
819
820
ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &OS::get_static_memory_usage);
821
ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &OS::get_static_memory_peak_usage);
822
ClassDB::bind_method(D_METHOD("get_memory_info"), &OS::get_memory_info);
823
824
ClassDB::bind_method(D_METHOD("move_to_trash", "path"), &OS::move_to_trash);
825
ClassDB::bind_method(D_METHOD("get_user_data_dir"), &OS::get_user_data_dir);
826
ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &OS::get_system_dir, DEFVAL(true));
827
ClassDB::bind_method(D_METHOD("get_config_dir"), &OS::get_config_dir);
828
ClassDB::bind_method(D_METHOD("get_data_dir"), &OS::get_data_dir);
829
ClassDB::bind_method(D_METHOD("get_cache_dir"), &OS::get_cache_dir);
830
ClassDB::bind_method(D_METHOD("get_temp_dir"), &OS::get_temp_dir);
831
ClassDB::bind_method(D_METHOD("get_unique_id"), &OS::get_unique_id);
832
833
ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &OS::get_keycode_string);
834
ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &OS::is_keycode_unicode);
835
ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &OS::find_keycode_from_string);
836
837
ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &OS::set_use_file_access_save_and_swap);
838
839
ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &OS::set_thread_name);
840
ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &OS::get_thread_caller_id);
841
ClassDB::bind_method(D_METHOD("get_main_thread_id"), &OS::get_main_thread_id);
842
843
ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &OS::has_feature);
844
ClassDB::bind_method(D_METHOD("is_sandboxed"), &OS::is_sandboxed);
845
846
ClassDB::bind_method(D_METHOD("request_permission", "name"), &OS::request_permission);
847
ClassDB::bind_method(D_METHOD("request_permissions"), &OS::request_permissions);
848
ClassDB::bind_method(D_METHOD("get_granted_permissions"), &OS::get_granted_permissions);
849
ClassDB::bind_method(D_METHOD("revoke_granted_permissions"), &OS::revoke_granted_permissions);
850
851
ClassDB::bind_method(D_METHOD("add_logger", "logger"), &OS::add_logger);
852
ClassDB::bind_method(D_METHOD("remove_logger", "logger"), &OS::remove_logger);
853
854
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
855
ADD_PROPERTY(PropertyInfo(Variant::INT, "low_processor_usage_mode_sleep_usec"), "set_low_processor_usage_mode_sleep_usec", "get_low_processor_usage_mode_sleep_usec");
856
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "delta_smoothing"), "set_delta_smoothing", "is_delta_smoothing_enabled");
857
858
// Those default values need to be specified for the docs generator,
859
// to avoid using values from the documentation writer's own OS instance.
860
ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
861
ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
862
863
BIND_ENUM_CONSTANT(RENDERING_DRIVER_VULKAN);
864
BIND_ENUM_CONSTANT(RENDERING_DRIVER_OPENGL3);
865
BIND_ENUM_CONSTANT(RENDERING_DRIVER_D3D12);
866
BIND_ENUM_CONSTANT(RENDERING_DRIVER_METAL);
867
868
BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
869
BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
870
BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
871
BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
872
BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
873
BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
874
BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
875
BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
876
877
BIND_ENUM_CONSTANT(STD_HANDLE_INVALID);
878
BIND_ENUM_CONSTANT(STD_HANDLE_CONSOLE);
879
BIND_ENUM_CONSTANT(STD_HANDLE_FILE);
880
BIND_ENUM_CONSTANT(STD_HANDLE_PIPE);
881
BIND_ENUM_CONSTANT(STD_HANDLE_UNKNOWN);
882
}
883
884
OS::OS() {
885
singleton = this;
886
}
887
888
OS::~OS() {
889
if (singleton == this) {
890
singleton = nullptr;
891
}
892
893
if (logger_bind) {
894
logger_bind->clear();
895
}
896
}
897
898
////// Geometry2D //////
899
900
Geometry2D *Geometry2D::get_singleton() {
901
return singleton;
902
}
903
904
bool Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
905
return ::Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
906
}
907
908
real_t Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
909
return ::Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
910
}
911
912
Variant Geometry2D::segment_intersects_segment(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b) {
913
Vector2 result;
914
if (::Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
915
return result;
916
} else {
917
return Variant();
918
}
919
}
920
921
Variant Geometry2D::line_intersects_line(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b) {
922
Vector2 result;
923
if (::Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
924
return result;
925
} else {
926
return Variant();
927
}
928
}
929
930
Vector<Vector2> Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
931
Vector2 r1, r2;
932
::Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
933
Vector<Vector2> r = { r1, r2 };
934
return r;
935
}
936
937
Vector2 Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
938
return ::Geometry2D::get_closest_point_to_segment(p_point, p_a, p_b);
939
}
940
941
Vector2 Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
942
return ::Geometry2D::get_closest_point_to_segment_uncapped(p_point, p_a, p_b);
943
}
944
945
bool Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
946
return ::Geometry2D::is_point_in_triangle(s, a, b, c);
947
}
948
949
bool Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
950
return ::Geometry2D::is_polygon_clockwise(p_polygon);
951
}
952
953
bool Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
954
return ::Geometry2D::is_point_in_polygon(p_point, p_polygon);
955
}
956
957
Vector<int> Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
958
return ::Geometry2D::triangulate_polygon(p_polygon);
959
}
960
961
Vector<int> Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
962
return ::Geometry2D::triangulate_delaunay(p_points);
963
}
964
965
Vector<Point2> Geometry2D::convex_hull(const Vector<Point2> &p_points) {
966
return ::Geometry2D::convex_hull(p_points);
967
}
968
969
TypedArray<PackedVector2Array> Geometry2D::decompose_polygon_in_convex(const Vector<Vector2> &p_polygon) {
970
Vector<Vector<Point2>> decomp = ::Geometry2D::decompose_polygon_in_convex(p_polygon);
971
972
TypedArray<PackedVector2Array> ret;
973
974
for (int i = 0; i < decomp.size(); ++i) {
975
ret.push_back(decomp[i]);
976
}
977
return ret;
978
}
979
980
TypedArray<PackedVector2Array> Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
981
Vector<Vector<Point2>> polys = ::Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
982
983
TypedArray<PackedVector2Array> ret;
984
985
for (int i = 0; i < polys.size(); ++i) {
986
ret.push_back(polys[i]);
987
}
988
return ret;
989
}
990
991
TypedArray<PackedVector2Array> Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
992
Vector<Vector<Point2>> polys = ::Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
993
994
TypedArray<PackedVector2Array> ret;
995
996
for (int i = 0; i < polys.size(); ++i) {
997
ret.push_back(polys[i]);
998
}
999
return ret;
1000
}
1001
1002
TypedArray<PackedVector2Array> Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
1003
Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
1004
1005
TypedArray<PackedVector2Array> ret;
1006
1007
for (int i = 0; i < polys.size(); ++i) {
1008
ret.push_back(polys[i]);
1009
}
1010
return ret;
1011
}
1012
1013
TypedArray<PackedVector2Array> Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
1014
Vector<Vector<Point2>> polys = ::Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b);
1015
1016
TypedArray<PackedVector2Array> ret;
1017
1018
for (int i = 0; i < polys.size(); ++i) {
1019
ret.push_back(polys[i]);
1020
}
1021
return ret;
1022
}
1023
1024
TypedArray<PackedVector2Array> Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
1025
Vector<Vector<Point2>> polys = ::Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
1026
1027
TypedArray<PackedVector2Array> ret;
1028
1029
for (int i = 0; i < polys.size(); ++i) {
1030
ret.push_back(polys[i]);
1031
}
1032
return ret;
1033
}
1034
1035
TypedArray<PackedVector2Array> Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
1036
Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon);
1037
1038
TypedArray<PackedVector2Array> ret;
1039
1040
for (int i = 0; i < polys.size(); ++i) {
1041
ret.push_back(polys[i]);
1042
}
1043
return ret;
1044
}
1045
1046
TypedArray<PackedVector2Array> Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
1047
Vector<Vector<Point2>> polys = ::Geometry2D::offset_polygon(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type));
1048
1049
TypedArray<PackedVector2Array> ret;
1050
1051
for (int i = 0; i < polys.size(); ++i) {
1052
ret.push_back(polys[i]);
1053
}
1054
return ret;
1055
}
1056
1057
TypedArray<PackedVector2Array> Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
1058
Vector<Vector<Point2>> polys = ::Geometry2D::offset_polyline(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type), ::Geometry2D::PolyEndType(p_end_type));
1059
1060
TypedArray<PackedVector2Array> ret;
1061
1062
for (int i = 0; i < polys.size(); ++i) {
1063
ret.push_back(polys[i]);
1064
}
1065
return ret;
1066
}
1067
1068
Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
1069
Dictionary ret;
1070
1071
Vector<Size2i> rects;
1072
for (int i = 0; i < p_rects.size(); i++) {
1073
rects.push_back(p_rects[i]);
1074
}
1075
1076
Vector<Point2i> result;
1077
Size2i size;
1078
1079
::Geometry2D::make_atlas(rects, result, size);
1080
1081
Vector<Point2> r_result;
1082
for (int i = 0; i < result.size(); i++) {
1083
r_result.push_back(result[i]);
1084
}
1085
1086
ret["points"] = r_result;
1087
ret["size"] = size;
1088
1089
return ret;
1090
}
1091
1092
TypedArray<Point2i> Geometry2D::bresenham_line(const Point2i &p_from, const Point2i &p_to) {
1093
Vector<Point2i> points = ::Geometry2D::bresenham_line(p_from, p_to);
1094
1095
TypedArray<Point2i> result;
1096
result.resize(points.size());
1097
1098
for (int i = 0; i < points.size(); i++) {
1099
result[i] = points[i];
1100
}
1101
1102
return result;
1103
}
1104
1105
void Geometry2D::_bind_methods() {
1106
ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &Geometry2D::is_point_in_circle);
1107
ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &Geometry2D::segment_intersects_circle);
1108
ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &Geometry2D::segment_intersects_segment);
1109
ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &Geometry2D::line_intersects_line);
1110
1111
ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &Geometry2D::get_closest_points_between_segments);
1112
1113
ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment);
1114
1115
ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment_uncapped);
1116
1117
ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &Geometry2D::point_is_inside_triangle);
1118
1119
ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &Geometry2D::is_polygon_clockwise);
1120
ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &Geometry2D::is_point_in_polygon);
1121
ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &Geometry2D::triangulate_polygon);
1122
ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &Geometry2D::triangulate_delaunay);
1123
ClassDB::bind_method(D_METHOD("convex_hull", "points"), &Geometry2D::convex_hull);
1124
ClassDB::bind_method(D_METHOD("decompose_polygon_in_convex", "polygon"), &Geometry2D::decompose_polygon_in_convex);
1125
1126
ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &Geometry2D::merge_polygons);
1127
ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &Geometry2D::clip_polygons);
1128
ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &Geometry2D::intersect_polygons);
1129
ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &Geometry2D::exclude_polygons);
1130
1131
ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::clip_polyline_with_polygon);
1132
ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::intersect_polyline_with_polygon);
1133
1134
ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
1135
ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
1136
1137
ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &Geometry2D::make_atlas);
1138
1139
ClassDB::bind_method(D_METHOD("bresenham_line", "from", "to"), &Geometry2D::bresenham_line);
1140
1141
BIND_ENUM_CONSTANT(OPERATION_UNION);
1142
BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
1143
BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
1144
BIND_ENUM_CONSTANT(OPERATION_XOR);
1145
1146
BIND_ENUM_CONSTANT(JOIN_SQUARE);
1147
BIND_ENUM_CONSTANT(JOIN_ROUND);
1148
BIND_ENUM_CONSTANT(JOIN_MITER);
1149
1150
BIND_ENUM_CONSTANT(END_POLYGON);
1151
BIND_ENUM_CONSTANT(END_JOINED);
1152
BIND_ENUM_CONSTANT(END_BUTT);
1153
BIND_ENUM_CONSTANT(END_SQUARE);
1154
BIND_ENUM_CONSTANT(END_ROUND);
1155
}
1156
1157
////// Geometry3D //////
1158
1159
Geometry3D *Geometry3D::get_singleton() {
1160
return singleton;
1161
}
1162
1163
Vector<Vector3> Geometry3D::compute_convex_mesh_points(const TypedArray<Plane> &p_planes) {
1164
Vector<Plane> planes_vec;
1165
int size = p_planes.size();
1166
planes_vec.resize(size);
1167
for (int i = 0; i < size; ++i) {
1168
planes_vec.set(i, p_planes[i]);
1169
}
1170
Variant ret = ::Geometry3D::compute_convex_mesh_points(planes_vec.ptr(), size);
1171
return ret;
1172
}
1173
1174
TypedArray<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
1175
Variant ret = ::Geometry3D::build_box_planes(p_extents);
1176
return ret;
1177
}
1178
1179
TypedArray<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
1180
Variant ret = ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
1181
return ret;
1182
}
1183
1184
TypedArray<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
1185
Variant ret = ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
1186
return ret;
1187
}
1188
1189
Vector<Vector3> Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
1190
Vector3 r1, r2;
1191
::Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
1192
Vector<Vector3> r = { r1, r2 };
1193
return r;
1194
}
1195
1196
Vector3 Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
1197
return ::Geometry3D::get_closest_point_to_segment(p_point, p_a, p_b);
1198
}
1199
1200
Vector3 Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
1201
return ::Geometry3D::get_closest_point_to_segment_uncapped(p_point, p_a, p_b);
1202
}
1203
1204
Vector3 Geometry3D::get_triangle_barycentric_coords(const Vector3 &p_point, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
1205
Vector3 res = ::Geometry3D::triangle_get_barycentric_coords(p_v0, p_v1, p_v2, p_point);
1206
return res;
1207
}
1208
1209
Variant Geometry3D::ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
1210
Vector3 res;
1211
if (::Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
1212
return res;
1213
} else {
1214
return Variant();
1215
}
1216
}
1217
1218
Variant Geometry3D::segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
1219
Vector3 res;
1220
if (::Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
1221
return res;
1222
} else {
1223
return Variant();
1224
}
1225
}
1226
1227
Vector<Vector3> Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
1228
Vector<Vector3> r;
1229
Vector3 res, norm;
1230
if (!::Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
1231
return r;
1232
}
1233
1234
r.resize(2);
1235
r.set(0, res);
1236
r.set(1, norm);
1237
return r;
1238
}
1239
1240
Vector<Vector3> Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
1241
Vector<Vector3> r;
1242
Vector3 res, norm;
1243
if (!::Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
1244
return r;
1245
}
1246
1247
r.resize(2);
1248
r.set(0, res);
1249
r.set(1, norm);
1250
return r;
1251
}
1252
1253
Vector<Vector3> Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const TypedArray<Plane> &p_planes) {
1254
Vector<Vector3> r;
1255
Vector3 res, norm;
1256
Vector<Plane> planes = Variant(p_planes);
1257
if (!::Geometry3D::segment_intersects_convex(p_from, p_to, planes.ptr(), planes.size(), &res, &norm)) {
1258
return r;
1259
}
1260
1261
r.resize(2);
1262
r.set(0, res);
1263
r.set(1, norm);
1264
return r;
1265
}
1266
1267
Vector<Vector3> Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
1268
return ::Geometry3D::clip_polygon(p_points, p_plane);
1269
}
1270
1271
Vector<int32_t> Geometry3D::tetrahedralize_delaunay(const Vector<Vector3> &p_points) {
1272
return ::Geometry3D::tetrahedralize_delaunay(p_points);
1273
}
1274
1275
void Geometry3D::_bind_methods() {
1276
ClassDB::bind_method(D_METHOD("compute_convex_mesh_points", "planes"), &Geometry3D::compute_convex_mesh_points);
1277
ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &Geometry3D::build_box_planes);
1278
ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
1279
ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
1280
1281
ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &Geometry3D::get_closest_points_between_segments);
1282
1283
ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment);
1284
1285
ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment_uncapped);
1286
1287
ClassDB::bind_method(D_METHOD("get_triangle_barycentric_coords", "point", "a", "b", "c"), &Geometry3D::get_triangle_barycentric_coords);
1288
1289
ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &Geometry3D::ray_intersects_triangle);
1290
ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &Geometry3D::segment_intersects_triangle);
1291
ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &Geometry3D::segment_intersects_sphere);
1292
ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &Geometry3D::segment_intersects_cylinder);
1293
ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &Geometry3D::segment_intersects_convex);
1294
1295
ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &Geometry3D::clip_polygon);
1296
ClassDB::bind_method(D_METHOD("tetrahedralize_delaunay", "points"), &Geometry3D::tetrahedralize_delaunay);
1297
}
1298
1299
////// Marshalls //////
1300
1301
Marshalls *Marshalls::get_singleton() {
1302
return singleton;
1303
}
1304
1305
String Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
1306
int len;
1307
Error err = encode_variant(p_var, nullptr, len, p_full_objects);
1308
ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
1309
1310
Vector<uint8_t> buff;
1311
buff.resize(len);
1312
uint8_t *w = buff.ptrw();
1313
1314
err = encode_variant(p_var, &w[0], len, p_full_objects);
1315
ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
1316
1317
String ret = CryptoCore::b64_encode_str(&w[0], len);
1318
ERR_FAIL_COND_V(ret.is_empty(), ret);
1319
1320
return ret;
1321
}
1322
1323
Variant Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
1324
int strlen = p_str.length();
1325
CharString cstr = p_str.ascii();
1326
1327
Vector<uint8_t> buf;
1328
buf.resize(strlen / 4 * 3 + 1);
1329
uint8_t *w = buf.ptrw();
1330
1331
size_t len = 0;
1332
ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
1333
1334
Variant v;
1335
Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
1336
ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
1337
1338
return v;
1339
}
1340
1341
String Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
1342
String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
1343
ERR_FAIL_COND_V(ret.is_empty(), ret);
1344
return ret;
1345
}
1346
1347
Vector<uint8_t> Marshalls::base64_to_raw(const String &p_str) {
1348
int strlen = p_str.length();
1349
CharString cstr = p_str.ascii();
1350
1351
size_t arr_len = 0;
1352
Vector<uint8_t> buf;
1353
{
1354
buf.resize(strlen / 4 * 3 + 1);
1355
uint8_t *w = buf.ptrw();
1356
1357
ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
1358
}
1359
buf.resize(arr_len);
1360
1361
return buf;
1362
}
1363
1364
String Marshalls::utf8_to_base64(const String &p_str) {
1365
if (p_str.is_empty()) {
1366
return String();
1367
}
1368
CharString cstr = p_str.utf8();
1369
String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
1370
ERR_FAIL_COND_V(ret.is_empty(), ret);
1371
return ret;
1372
}
1373
1374
String Marshalls::base64_to_utf8(const String &p_str) {
1375
int strlen = p_str.length();
1376
CharString cstr = p_str.ascii();
1377
1378
Vector<uint8_t> buf;
1379
buf.resize(strlen / 4 * 3 + 1 + 1);
1380
uint8_t *w = buf.ptrw();
1381
1382
size_t len = 0;
1383
ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
1384
1385
w[len] = 0;
1386
String ret = String::utf8((char *)&w[0]);
1387
1388
return ret;
1389
}
1390
1391
void Marshalls::_bind_methods() {
1392
ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &Marshalls::variant_to_base64, DEFVAL(false));
1393
ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &Marshalls::base64_to_variant, DEFVAL(false));
1394
1395
ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &Marshalls::raw_to_base64);
1396
ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &Marshalls::base64_to_raw);
1397
1398
ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &Marshalls::utf8_to_base64);
1399
ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &Marshalls::base64_to_utf8);
1400
}
1401
1402
////// Semaphore //////
1403
1404
void Semaphore::wait() {
1405
semaphore.wait();
1406
}
1407
1408
bool Semaphore::try_wait() {
1409
return semaphore.try_wait();
1410
}
1411
1412
void Semaphore::post(int p_count) {
1413
ERR_FAIL_COND(p_count <= 0);
1414
semaphore.post(p_count);
1415
}
1416
1417
void Semaphore::_bind_methods() {
1418
ClassDB::bind_method(D_METHOD("wait"), &Semaphore::wait);
1419
ClassDB::bind_method(D_METHOD("try_wait"), &Semaphore::try_wait);
1420
ClassDB::bind_method(D_METHOD("post", "count"), &Semaphore::post, DEFVAL(1));
1421
}
1422
1423
////// Mutex //////
1424
1425
void Mutex::lock() {
1426
mutex.lock();
1427
}
1428
1429
bool Mutex::try_lock() {
1430
return mutex.try_lock();
1431
}
1432
1433
void Mutex::unlock() {
1434
mutex.unlock();
1435
}
1436
1437
void Mutex::_bind_methods() {
1438
ClassDB::bind_method(D_METHOD("lock"), &Mutex::lock);
1439
ClassDB::bind_method(D_METHOD("try_lock"), &Mutex::try_lock);
1440
ClassDB::bind_method(D_METHOD("unlock"), &Mutex::unlock);
1441
}
1442
1443
////// Thread //////
1444
1445
void Thread::_start_func(void *ud) {
1446
Ref<Thread> *tud = (Ref<Thread> *)ud;
1447
Ref<Thread> t = *tud;
1448
memdelete(tud);
1449
1450
if (!t->target_callable.is_valid()) {
1451
t->running.clear();
1452
ERR_FAIL_MSG(vformat("Could not call function '%s' on previously freed instance to start thread %s.", t->target_callable.get_method(), t->get_id()));
1453
}
1454
1455
// Finding out a suitable name for the thread can involve querying a node, if the target is one.
1456
// We know this is safe (unless the user is causing life cycle race conditions, which would be a bug on their part).
1457
set_current_thread_safe_for_nodes(true);
1458
String func_name = t->target_callable.is_custom() ? t->target_callable.get_custom()->get_as_text() : String(t->target_callable.get_method());
1459
set_current_thread_safe_for_nodes(false);
1460
::Thread::set_name(func_name);
1461
1462
// To avoid a circular reference between the thread and the script which can possibly contain a reference
1463
// to the thread, we will do the call (keeping a reference up to that point) and then break chains with it.
1464
// When the call returns, we will reference the thread again if possible.
1465
ObjectID th_instance_id = t->get_instance_id();
1466
Callable target_callable = t->target_callable;
1467
String id = t->get_id();
1468
t = Ref<Thread>();
1469
1470
Callable::CallError ce;
1471
Variant ret;
1472
target_callable.callp(nullptr, 0, ret, ce);
1473
// If script properly kept a reference to the thread, we should be able to re-reference it now
1474
// (well, or if the call failed, since we had to break chains anyway because the outcome isn't known upfront).
1475
t = ObjectDB::get_ref<Thread>(th_instance_id);
1476
if (t.is_valid()) {
1477
t->ret = ret;
1478
t->running.clear();
1479
} else {
1480
// We could print a warning here, but the Thread object will be eventually destroyed
1481
// noticing wait_to_finish() hasn't been called on it, and it will print a warning itself.
1482
}
1483
1484
if (ce.error != Callable::CallError::CALL_OK) {
1485
ERR_FAIL_MSG(vformat("Could not call function '%s' to start thread %s: %s.", func_name, id, Variant::get_callable_error_text(target_callable, nullptr, 0, ce)));
1486
}
1487
}
1488
1489
Error Thread::start(const Callable &p_callable, Priority p_priority) {
1490
ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
1491
ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
1492
ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
1493
1494
ret = Variant();
1495
target_callable = p_callable;
1496
running.set();
1497
1498
Ref<Thread> *ud = memnew(Ref<Thread>(this));
1499
1500
::Thread::Settings s;
1501
s.priority = (::Thread::Priority)p_priority;
1502
thread.start(_start_func, ud, s);
1503
1504
return OK;
1505
}
1506
1507
String Thread::get_id() const {
1508
return itos(thread.get_id());
1509
}
1510
1511
bool Thread::is_started() const {
1512
return thread.is_started();
1513
}
1514
1515
bool Thread::is_alive() const {
1516
return running.is_set();
1517
}
1518
1519
Variant Thread::wait_to_finish() {
1520
ERR_FAIL_COND_V_MSG(!is_started(), Variant(), "Thread must have been started to wait for its completion.");
1521
thread.wait_to_finish();
1522
Variant r = ret;
1523
target_callable = Callable();
1524
1525
return r;
1526
}
1527
1528
void Thread::set_thread_safety_checks_enabled(bool p_enabled) {
1529
ERR_FAIL_COND_MSG(::Thread::is_main_thread(), "This call is forbidden on the main thread.");
1530
set_current_thread_safe_for_nodes(!p_enabled);
1531
}
1532
1533
bool Thread::is_main_thread() {
1534
return ::Thread::is_main_thread();
1535
}
1536
1537
void Thread::_bind_methods() {
1538
ClassDB::bind_method(D_METHOD("start", "callable", "priority"), &Thread::start, DEFVAL(PRIORITY_NORMAL));
1539
ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
1540
ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
1541
ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive);
1542
ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish);
1543
1544
ClassDB::bind_static_method("Thread", D_METHOD("set_thread_safety_checks_enabled", "enabled"), &Thread::set_thread_safety_checks_enabled);
1545
ClassDB::bind_static_method("Thread", D_METHOD("is_main_thread"), &Thread::is_main_thread);
1546
1547
BIND_ENUM_CONSTANT(PRIORITY_LOW);
1548
BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
1549
BIND_ENUM_CONSTANT(PRIORITY_HIGH);
1550
}
1551
1552
namespace Special {
1553
1554
////// ClassDB //////
1555
1556
PackedStringArray ClassDB::get_class_list() const {
1557
LocalVector<StringName> classes;
1558
::ClassDB::get_class_list(classes);
1559
1560
PackedStringArray ret;
1561
ret.resize(classes.size());
1562
String *ptrw = ret.ptrw();
1563
int idx = 0;
1564
for (const StringName &cls : classes) {
1565
ptrw[idx] = cls;
1566
idx++;
1567
}
1568
1569
return ret;
1570
}
1571
1572
PackedStringArray ClassDB::get_inheriters_from_class(const StringName &p_class) const {
1573
LocalVector<StringName> classes;
1574
::ClassDB::get_inheriters_from_class(p_class, classes);
1575
1576
PackedStringArray ret;
1577
ret.resize(classes.size());
1578
int idx = 0;
1579
for (const StringName &E : classes) {
1580
ret.set(idx++, E);
1581
}
1582
1583
return ret;
1584
}
1585
1586
StringName ClassDB::get_parent_class(const StringName &p_class) const {
1587
return ::ClassDB::get_parent_class(p_class);
1588
}
1589
1590
bool ClassDB::class_exists(const StringName &p_class) const {
1591
return ::ClassDB::class_exists(p_class);
1592
}
1593
1594
bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
1595
return ::ClassDB::is_parent_class(p_class, p_inherits);
1596
}
1597
1598
bool ClassDB::can_instantiate(const StringName &p_class) const {
1599
return ::ClassDB::can_instantiate(p_class);
1600
}
1601
1602
Variant ClassDB::instantiate(const StringName &p_class) const {
1603
Object *obj = ::ClassDB::instantiate(p_class);
1604
if (!obj) {
1605
return Variant();
1606
}
1607
1608
RefCounted *r = Object::cast_to<RefCounted>(obj);
1609
if (r) {
1610
return Ref<RefCounted>(r);
1611
} else {
1612
return obj;
1613
}
1614
}
1615
1616
ClassDB::APIType ClassDB::class_get_api_type(const StringName &p_class) const {
1617
::ClassDB::APIType api_type = ::ClassDB::get_api_type(p_class);
1618
return (APIType)api_type;
1619
}
1620
1621
bool ClassDB::class_has_signal(const StringName &p_class, const StringName &p_signal) const {
1622
return ::ClassDB::has_signal(p_class, p_signal);
1623
}
1624
1625
Dictionary ClassDB::class_get_signal(const StringName &p_class, const StringName &p_signal) const {
1626
MethodInfo signal;
1627
if (::ClassDB::get_signal(p_class, p_signal, &signal)) {
1628
return signal.operator Dictionary();
1629
} else {
1630
return Dictionary();
1631
}
1632
}
1633
1634
TypedArray<Dictionary> ClassDB::class_get_signal_list(const StringName &p_class, bool p_no_inheritance) const {
1635
List<MethodInfo> signals;
1636
::ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
1637
TypedArray<Dictionary> ret;
1638
1639
for (const MethodInfo &E : signals) {
1640
ret.push_back(E.operator Dictionary());
1641
}
1642
1643
return ret;
1644
}
1645
1646
TypedArray<Dictionary> ClassDB::class_get_property_list(const StringName &p_class, bool p_no_inheritance) const {
1647
List<PropertyInfo> plist;
1648
::ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
1649
TypedArray<Dictionary> ret;
1650
for (const PropertyInfo &E : plist) {
1651
ret.push_back(E.operator Dictionary());
1652
}
1653
1654
return ret;
1655
}
1656
1657
StringName ClassDB::class_get_property_getter(const StringName &p_class, const StringName &p_property) {
1658
return ::ClassDB::get_property_getter(p_class, p_property);
1659
}
1660
1661
StringName ClassDB::class_get_property_setter(const StringName &p_class, const StringName &p_property) {
1662
return ::ClassDB::get_property_setter(p_class, p_property);
1663
}
1664
1665
Variant ClassDB::class_get_property(Object *p_object, const StringName &p_property) const {
1666
Variant ret;
1667
::ClassDB::get_property(p_object, p_property, ret);
1668
return ret;
1669
}
1670
1671
Error ClassDB::class_set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
1672
Variant ret;
1673
bool valid;
1674
if (!::ClassDB::set_property(p_object, p_property, p_value, &valid)) {
1675
return ERR_UNAVAILABLE;
1676
} else if (!valid) {
1677
return ERR_INVALID_DATA;
1678
}
1679
return OK;
1680
}
1681
1682
Variant ClassDB::class_get_property_default_value(const StringName &p_class, const StringName &p_property) const {
1683
bool valid;
1684
Variant ret = ::ClassDB::class_get_default_property_value(p_class, p_property, &valid);
1685
if (valid) {
1686
return ret;
1687
}
1688
return Variant();
1689
}
1690
1691
bool ClassDB::class_has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) const {
1692
return ::ClassDB::has_method(p_class, p_method, p_no_inheritance);
1693
}
1694
1695
int ClassDB::class_get_method_argument_count(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) const {
1696
return ::ClassDB::get_method_argument_count(p_class, p_method, nullptr, p_no_inheritance);
1697
}
1698
1699
TypedArray<Dictionary> ClassDB::class_get_method_list(const StringName &p_class, bool p_no_inheritance) const {
1700
List<MethodInfo> methods;
1701
::ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
1702
TypedArray<Dictionary> ret;
1703
1704
for (const MethodInfo &method : methods) {
1705
ret.push_back(method.operator Dictionary());
1706
}
1707
1708
return ret;
1709
}
1710
1711
Variant ClassDB::class_call_static(const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) {
1712
if (p_argcount < 2) {
1713
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
1714
return Variant::NIL;
1715
}
1716
if (!p_arguments[0]->is_string() || !p_arguments[1]->is_string()) {
1717
r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
1718
return Variant::NIL;
1719
}
1720
StringName class_ = *p_arguments[0];
1721
StringName method = *p_arguments[1];
1722
const MethodBind *bind = ::ClassDB::get_method(class_, method);
1723
ERR_FAIL_NULL_V_MSG(bind, Variant::NIL, "Cannot find static method.");
1724
ERR_FAIL_COND_V_MSG(!bind->is_static(), Variant::NIL, "Method is not static.");
1725
return bind->call(nullptr, p_arguments + 2, p_argcount - 2, r_call_error);
1726
}
1727
1728
PackedStringArray ClassDB::class_get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
1729
List<String> constants;
1730
::ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
1731
1732
PackedStringArray ret;
1733
ret.resize(constants.size());
1734
int idx = 0;
1735
for (const String &E : constants) {
1736
ret.set(idx++, E);
1737
}
1738
1739
return ret;
1740
}
1741
1742
bool ClassDB::class_has_integer_constant(const StringName &p_class, const StringName &p_name) const {
1743
bool success;
1744
::ClassDB::get_integer_constant(p_class, p_name, &success);
1745
return success;
1746
}
1747
1748
int64_t ClassDB::class_get_integer_constant(const StringName &p_class, const StringName &p_name) const {
1749
bool found;
1750
int64_t c = ::ClassDB::get_integer_constant(p_class, p_name, &found);
1751
ERR_FAIL_COND_V(!found, 0);
1752
return c;
1753
}
1754
1755
bool ClassDB::class_has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
1756
return ::ClassDB::has_enum(p_class, p_name, p_no_inheritance);
1757
}
1758
1759
PackedStringArray ClassDB::class_get_enum_list(const StringName &p_class, bool p_no_inheritance) const {
1760
List<StringName> enums;
1761
::ClassDB::get_enum_list(p_class, &enums, p_no_inheritance);
1762
1763
PackedStringArray ret;
1764
ret.resize(enums.size());
1765
int idx = 0;
1766
for (const StringName &E : enums) {
1767
ret.set(idx++, E);
1768
}
1769
1770
return ret;
1771
}
1772
1773
PackedStringArray ClassDB::class_get_enum_constants(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
1774
List<StringName> constants;
1775
::ClassDB::get_enum_constants(p_class, p_enum, &constants, p_no_inheritance);
1776
1777
PackedStringArray ret;
1778
ret.resize(constants.size());
1779
int idx = 0;
1780
for (const StringName &E : constants) {
1781
ret.set(idx++, E);
1782
}
1783
1784
return ret;
1785
}
1786
1787
StringName ClassDB::class_get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
1788
return ::ClassDB::get_integer_constant_enum(p_class, p_name, p_no_inheritance);
1789
}
1790
1791
bool ClassDB::is_class_enum_bitfield(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
1792
return ::ClassDB::is_enum_bitfield(p_class, p_enum, p_no_inheritance);
1793
}
1794
1795
bool ClassDB::is_class_enabled(const StringName &p_class) const {
1796
return ::ClassDB::is_class_enabled(p_class);
1797
}
1798
1799
#ifdef TOOLS_ENABLED
1800
void ClassDB::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
1801
const String pf = p_function;
1802
bool first_argument_is_class = false;
1803
if (p_idx == 0) {
1804
first_argument_is_class = (pf == "get_inheriters_from_class" || pf == "get_parent_class" ||
1805
pf == "class_exists" || pf == "can_instantiate" || pf == "instantiate" ||
1806
pf == "class_has_signal" || pf == "class_get_signal" || pf == "class_get_signal_list" ||
1807
pf == "class_get_property_list" || pf == "class_get_property" || pf == "class_set_property" ||
1808
pf == "class_has_method" || pf == "class_get_method_list" ||
1809
pf == "class_get_integer_constant_list" || pf == "class_has_integer_constant" || pf == "class_get_integer_constant" ||
1810
pf == "class_has_enum" || pf == "class_get_enum_list" || pf == "class_get_enum_constants" || pf == "class_get_integer_constant_enum" ||
1811
pf == "is_class_enabled" || pf == "is_class_enum_bitfield" || pf == "class_get_api_type");
1812
}
1813
if (first_argument_is_class || pf == "is_parent_class") {
1814
LocalVector<StringName> classes;
1815
::ClassDB::get_class_list(classes);
1816
for (const StringName &E : classes) {
1817
if (::ClassDB::is_class_exposed(E)) {
1818
r_options->push_back(E.operator String().quote());
1819
}
1820
}
1821
}
1822
1823
Object::get_argument_options(p_function, p_idx, r_options);
1824
}
1825
#endif
1826
1827
void ClassDB::_bind_methods() {
1828
::ClassDB::bind_method(D_METHOD("get_class_list"), &ClassDB::get_class_list);
1829
::ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &ClassDB::get_inheriters_from_class);
1830
::ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &ClassDB::get_parent_class);
1831
::ClassDB::bind_method(D_METHOD("class_exists", "class"), &ClassDB::class_exists);
1832
::ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &ClassDB::is_parent_class);
1833
::ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &ClassDB::can_instantiate);
1834
::ClassDB::bind_method(D_METHOD("instantiate", "class"), &ClassDB::instantiate);
1835
1836
::ClassDB::bind_method(D_METHOD("class_get_api_type", "class"), &ClassDB::class_get_api_type);
1837
1838
::ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &ClassDB::class_has_signal);
1839
::ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &ClassDB::class_get_signal);
1840
::ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &ClassDB::class_get_signal_list, DEFVAL(false));
1841
1842
::ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &ClassDB::class_get_property_list, DEFVAL(false));
1843
::ClassDB::bind_method(D_METHOD("class_get_property_getter", "class", "property"), &ClassDB::class_get_property_getter);
1844
::ClassDB::bind_method(D_METHOD("class_get_property_setter", "class", "property"), &ClassDB::class_get_property_setter);
1845
::ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &ClassDB::class_get_property);
1846
::ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &ClassDB::class_set_property);
1847
1848
::ClassDB::bind_method(D_METHOD("class_get_property_default_value", "class", "property"), &ClassDB::class_get_property_default_value);
1849
1850
::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::class_has_method, DEFVAL(false));
1851
1852
::ClassDB::bind_method(D_METHOD("class_get_method_argument_count", "class", "method", "no_inheritance"), &ClassDB::class_get_method_argument_count, DEFVAL(false));
1853
1854
::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::class_get_method_list, DEFVAL(false));
1855
1856
::ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "class_call_static", &ClassDB::class_call_static, MethodInfo("class_call_static", PropertyInfo(Variant::STRING_NAME, "class"), PropertyInfo(Variant::STRING_NAME, "method")));
1857
1858
::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::class_get_integer_constant_list, DEFVAL(false));
1859
1860
::ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &ClassDB::class_has_integer_constant);
1861
::ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &ClassDB::class_get_integer_constant);
1862
1863
::ClassDB::bind_method(D_METHOD("class_has_enum", "class", "name", "no_inheritance"), &ClassDB::class_has_enum, DEFVAL(false));
1864
::ClassDB::bind_method(D_METHOD("class_get_enum_list", "class", "no_inheritance"), &ClassDB::class_get_enum_list, DEFVAL(false));
1865
::ClassDB::bind_method(D_METHOD("class_get_enum_constants", "class", "enum", "no_inheritance"), &ClassDB::class_get_enum_constants, DEFVAL(false));
1866
::ClassDB::bind_method(D_METHOD("class_get_integer_constant_enum", "class", "name", "no_inheritance"), &ClassDB::class_get_integer_constant_enum, DEFVAL(false));
1867
1868
::ClassDB::bind_method(D_METHOD("is_class_enum_bitfield", "class", "enum", "no_inheritance"), &ClassDB::is_class_enum_bitfield, DEFVAL(false));
1869
1870
::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);
1871
1872
BIND_ENUM_CONSTANT(API_CORE);
1873
BIND_ENUM_CONSTANT(API_EDITOR);
1874
BIND_ENUM_CONSTANT(API_EXTENSION);
1875
BIND_ENUM_CONSTANT(API_EDITOR_EXTENSION);
1876
BIND_ENUM_CONSTANT(API_NONE);
1877
}
1878
1879
} // namespace Special
1880
1881
////// Engine //////
1882
1883
void Engine::set_physics_ticks_per_second(int p_ips) {
1884
::Engine::get_singleton()->set_physics_ticks_per_second(p_ips);
1885
}
1886
1887
int Engine::get_physics_ticks_per_second() const {
1888
return ::Engine::get_singleton()->get_physics_ticks_per_second();
1889
}
1890
1891
void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) {
1892
::Engine::get_singleton()->set_max_physics_steps_per_frame(p_max_physics_steps);
1893
}
1894
1895
int Engine::get_max_physics_steps_per_frame() const {
1896
return ::Engine::get_singleton()->get_max_physics_steps_per_frame();
1897
}
1898
1899
void Engine::set_physics_jitter_fix(double p_threshold) {
1900
::Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
1901
}
1902
1903
double Engine::get_physics_jitter_fix() const {
1904
return ::Engine::get_singleton()->get_physics_jitter_fix();
1905
}
1906
1907
double Engine::get_physics_interpolation_fraction() const {
1908
return ::Engine::get_singleton()->get_physics_interpolation_fraction();
1909
}
1910
1911
void Engine::set_max_fps(int p_fps) {
1912
::Engine::get_singleton()->set_max_fps(p_fps);
1913
}
1914
1915
int Engine::get_max_fps() const {
1916
return ::Engine::get_singleton()->get_max_fps();
1917
}
1918
1919
double Engine::get_frames_per_second() const {
1920
return ::Engine::get_singleton()->get_frames_per_second();
1921
}
1922
1923
uint64_t Engine::get_physics_frames() const {
1924
return ::Engine::get_singleton()->get_physics_frames();
1925
}
1926
1927
uint64_t Engine::get_process_frames() const {
1928
return ::Engine::get_singleton()->get_process_frames();
1929
}
1930
1931
void Engine::set_time_scale(double p_scale) {
1932
::Engine::get_singleton()->set_time_scale(p_scale);
1933
}
1934
1935
double Engine::get_time_scale() {
1936
return ::Engine::get_singleton()->get_time_scale();
1937
}
1938
1939
int Engine::get_frames_drawn() {
1940
return ::Engine::get_singleton()->get_frames_drawn();
1941
}
1942
1943
MainLoop *Engine::get_main_loop() const {
1944
// Needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
1945
return ::OS::get_singleton()->get_main_loop();
1946
}
1947
1948
Dictionary Engine::get_version_info() const {
1949
return ::Engine::get_singleton()->get_version_info();
1950
}
1951
1952
Dictionary Engine::get_author_info() const {
1953
return ::Engine::get_singleton()->get_author_info();
1954
}
1955
1956
TypedArray<Dictionary> Engine::get_copyright_info() const {
1957
return ::Engine::get_singleton()->get_copyright_info();
1958
}
1959
1960
Dictionary Engine::get_donor_info() const {
1961
return ::Engine::get_singleton()->get_donor_info();
1962
}
1963
1964
Dictionary Engine::get_license_info() const {
1965
return ::Engine::get_singleton()->get_license_info();
1966
}
1967
1968
String Engine::get_license_text() const {
1969
return ::Engine::get_singleton()->get_license_text();
1970
}
1971
1972
String Engine::get_architecture_name() const {
1973
return ::Engine::get_singleton()->get_architecture_name();
1974
}
1975
1976
bool Engine::is_in_physics_frame() const {
1977
return ::Engine::get_singleton()->is_in_physics_frame();
1978
}
1979
1980
bool Engine::has_singleton(const StringName &p_name) const {
1981
return ::Engine::get_singleton()->has_singleton(p_name);
1982
}
1983
1984
Object *Engine::get_singleton_object(const StringName &p_name) const {
1985
return ::Engine::get_singleton()->get_singleton_object(p_name);
1986
}
1987
1988
void Engine::register_singleton(const StringName &p_name, Object *p_object) {
1989
ERR_FAIL_COND_MSG(has_singleton(p_name), vformat("Singleton already registered: '%s'.", String(p_name)));
1990
ERR_FAIL_COND_MSG(!String(p_name).is_valid_ascii_identifier(), vformat("Singleton name is not a valid identifier: '%s'.", p_name));
1991
::Engine::Singleton s;
1992
s.class_name = p_name;
1993
s.name = p_name;
1994
s.ptr = p_object;
1995
s.user_created = true;
1996
::Engine::get_singleton()->add_singleton(s);
1997
}
1998
1999
void Engine::unregister_singleton(const StringName &p_name) {
2000
ERR_FAIL_COND_MSG(!has_singleton(p_name), vformat("Attempt to remove unregistered singleton: '%s'.", String(p_name)));
2001
ERR_FAIL_COND_MSG(!::Engine::get_singleton()->is_singleton_user_created(p_name), vformat("Attempt to remove non-user created singleton: '%s'.", String(p_name)));
2002
::Engine::get_singleton()->remove_singleton(p_name);
2003
}
2004
2005
Vector<String> Engine::get_singleton_list() const {
2006
List<::Engine::Singleton> singletons;
2007
::Engine::get_singleton()->get_singletons(&singletons);
2008
Vector<String> ret;
2009
for (const ::Engine::Singleton &E : singletons) {
2010
ret.push_back(E.name);
2011
}
2012
return ret;
2013
}
2014
2015
Error Engine::register_script_language(ScriptLanguage *p_language) {
2016
return ScriptServer::register_language(p_language);
2017
}
2018
2019
Error Engine::unregister_script_language(const ScriptLanguage *p_language) {
2020
return ScriptServer::unregister_language(p_language);
2021
}
2022
2023
int Engine::get_script_language_count() {
2024
return ScriptServer::get_language_count();
2025
}
2026
2027
ScriptLanguage *Engine::get_script_language(int p_index) const {
2028
return ScriptServer::get_language(p_index);
2029
}
2030
2031
TypedArray<ScriptBacktrace> Engine::capture_script_backtraces(bool p_include_variables) const {
2032
Vector<Ref<ScriptBacktrace>> backtraces = ScriptServer::capture_script_backtraces(p_include_variables);
2033
TypedArray<ScriptBacktrace> result;
2034
result.resize(backtraces.size());
2035
for (int i = 0; i < backtraces.size(); i++) {
2036
result[i] = backtraces[i];
2037
}
2038
return result;
2039
}
2040
2041
void Engine::set_editor_hint(bool p_enabled) {
2042
::Engine::get_singleton()->set_editor_hint(p_enabled);
2043
}
2044
2045
bool Engine::is_editor_hint() const {
2046
return ::Engine::get_singleton()->is_editor_hint();
2047
}
2048
2049
bool Engine::is_embedded_in_editor() const {
2050
return ::Engine::get_singleton()->is_embedded_in_editor();
2051
}
2052
2053
String Engine::get_write_movie_path() const {
2054
return ::Engine::get_singleton()->get_write_movie_path();
2055
}
2056
2057
void Engine::set_print_to_stdout(bool p_enabled) {
2058
::Engine::get_singleton()->set_print_to_stdout(p_enabled);
2059
}
2060
2061
bool Engine::is_printing_to_stdout() const {
2062
return ::Engine::get_singleton()->is_printing_to_stdout();
2063
}
2064
2065
void Engine::set_print_error_messages(bool p_enabled) {
2066
::Engine::get_singleton()->set_print_error_messages(p_enabled);
2067
}
2068
2069
bool Engine::is_printing_error_messages() const {
2070
return ::Engine::get_singleton()->is_printing_error_messages();
2071
}
2072
2073
#ifdef TOOLS_ENABLED
2074
void Engine::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
2075
const String pf = p_function;
2076
if (p_idx == 0 && (pf == "has_singleton" || pf == "get_singleton" || pf == "unregister_singleton")) {
2077
for (const String &E : get_singleton_list()) {
2078
r_options->push_back(E.quote());
2079
}
2080
}
2081
Object::get_argument_options(p_function, p_idx, r_options);
2082
}
2083
#endif
2084
2085
void Engine::_bind_methods() {
2086
ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second);
2087
ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second);
2088
ClassDB::bind_method(D_METHOD("set_max_physics_steps_per_frame", "max_physics_steps"), &Engine::set_max_physics_steps_per_frame);
2089
ClassDB::bind_method(D_METHOD("get_max_physics_steps_per_frame"), &Engine::get_max_physics_steps_per_frame);
2090
ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix);
2091
ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix);
2092
ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction);
2093
ClassDB::bind_method(D_METHOD("set_max_fps", "max_fps"), &Engine::set_max_fps);
2094
ClassDB::bind_method(D_METHOD("get_max_fps"), &Engine::get_max_fps);
2095
2096
ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &Engine::set_time_scale);
2097
ClassDB::bind_method(D_METHOD("get_time_scale"), &Engine::get_time_scale);
2098
2099
ClassDB::bind_method(D_METHOD("get_frames_drawn"), &Engine::get_frames_drawn);
2100
ClassDB::bind_method(D_METHOD("get_frames_per_second"), &Engine::get_frames_per_second);
2101
ClassDB::bind_method(D_METHOD("get_physics_frames"), &Engine::get_physics_frames);
2102
ClassDB::bind_method(D_METHOD("get_process_frames"), &Engine::get_process_frames);
2103
2104
ClassDB::bind_method(D_METHOD("get_main_loop"), &Engine::get_main_loop);
2105
2106
ClassDB::bind_method(D_METHOD("get_version_info"), &Engine::get_version_info);
2107
ClassDB::bind_method(D_METHOD("get_author_info"), &Engine::get_author_info);
2108
ClassDB::bind_method(D_METHOD("get_copyright_info"), &Engine::get_copyright_info);
2109
ClassDB::bind_method(D_METHOD("get_donor_info"), &Engine::get_donor_info);
2110
ClassDB::bind_method(D_METHOD("get_license_info"), &Engine::get_license_info);
2111
ClassDB::bind_method(D_METHOD("get_license_text"), &Engine::get_license_text);
2112
2113
ClassDB::bind_method(D_METHOD("get_architecture_name"), &Engine::get_architecture_name);
2114
2115
ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &Engine::is_in_physics_frame);
2116
2117
ClassDB::bind_method(D_METHOD("has_singleton", "name"), &Engine::has_singleton);
2118
ClassDB::bind_method(D_METHOD("get_singleton", "name"), &Engine::get_singleton_object);
2119
2120
ClassDB::bind_method(D_METHOD("register_singleton", "name", "instance"), &Engine::register_singleton);
2121
ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton);
2122
ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list);
2123
2124
ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language);
2125
ClassDB::bind_method(D_METHOD("unregister_script_language", "language"), &Engine::unregister_script_language);
2126
ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count);
2127
ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language);
2128
ClassDB::bind_method(D_METHOD("capture_script_backtraces", "include_variables"), &Engine::capture_script_backtraces, DEFVAL(false));
2129
2130
ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint);
2131
ClassDB::bind_method(D_METHOD("is_embedded_in_editor"), &Engine::is_embedded_in_editor);
2132
2133
ClassDB::bind_method(D_METHOD("get_write_movie_path"), &Engine::get_write_movie_path);
2134
2135
ClassDB::bind_method(D_METHOD("set_print_to_stdout", "enabled"), &Engine::set_print_to_stdout);
2136
ClassDB::bind_method(D_METHOD("is_printing_to_stdout"), &Engine::is_printing_to_stdout);
2137
2138
ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages);
2139
ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &Engine::is_printing_error_messages);
2140
2141
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
2142
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_to_stdout"), "set_print_to_stdout", "is_printing_to_stdout");
2143
ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second");
2144
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_physics_steps_per_frame"), "set_max_physics_steps_per_frame", "get_max_physics_steps_per_frame");
2145
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_fps"), "set_max_fps", "get_max_fps");
2146
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
2147
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
2148
}
2149
2150
////// EngineDebugger //////
2151
2152
bool EngineDebugger::is_active() {
2153
return ::EngineDebugger::is_active();
2154
}
2155
2156
void EngineDebugger::register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler) {
2157
ERR_FAIL_COND(p_profiler.is_null());
2158
ERR_FAIL_COND_MSG(p_profiler->is_bound(), "Profiler already registered.");
2159
ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), vformat("Profiler name already in use: '%s'.", p_name));
2160
Error err = p_profiler->bind(p_name);
2161
ERR_FAIL_COND_MSG(err != OK, vformat("Profiler failed to register with error: %d.", err));
2162
profilers.insert(p_name, p_profiler);
2163
}
2164
2165
void EngineDebugger::unregister_profiler(const StringName &p_name) {
2166
ERR_FAIL_COND_MSG(!profilers.has(p_name), vformat("Profiler not registered: '%s'.", p_name));
2167
profilers[p_name]->unbind();
2168
profilers.erase(p_name);
2169
}
2170
2171
bool EngineDebugger::is_profiling(const StringName &p_name) {
2172
return ::EngineDebugger::is_profiling(p_name);
2173
}
2174
2175
bool EngineDebugger::has_profiler(const StringName &p_name) {
2176
return ::EngineDebugger::has_profiler(p_name);
2177
}
2178
2179
void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
2180
::EngineDebugger::profiler_add_frame_data(p_name, p_data);
2181
}
2182
2183
void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
2184
if (::EngineDebugger::get_singleton()) {
2185
::EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
2186
}
2187
}
2188
2189
void EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
2190
ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), vformat("Capture already registered: '%s'.", p_name));
2191
captures.insert(p_name, p_callable);
2192
Callable &c = captures[p_name];
2193
::EngineDebugger::Capture capture(&c, &EngineDebugger::call_capture);
2194
::EngineDebugger::register_message_capture(p_name, capture);
2195
}
2196
2197
void EngineDebugger::unregister_message_capture(const StringName &p_name) {
2198
ERR_FAIL_COND_MSG(!captures.has(p_name), vformat("Capture not registered: '%s'.", p_name));
2199
::EngineDebugger::unregister_message_capture(p_name);
2200
captures.erase(p_name);
2201
}
2202
2203
bool EngineDebugger::has_capture(const StringName &p_name) {
2204
return ::EngineDebugger::has_capture(p_name);
2205
}
2206
2207
void EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
2208
ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send message. No active debugger");
2209
::EngineDebugger::get_singleton()->send_message(p_msg, p_data);
2210
}
2211
2212
void EngineDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
2213
ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send debug. No active debugger");
2214
::EngineDebugger::get_singleton()->debug(p_can_continue, p_is_error_breakpoint);
2215
}
2216
2217
void EngineDebugger::script_debug(ScriptLanguage *p_lang, bool p_can_continue, bool p_is_error_breakpoint) {
2218
ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't send debug. No active debugger");
2219
::EngineDebugger::get_script_debugger()->debug(p_lang, p_can_continue, p_is_error_breakpoint);
2220
}
2221
2222
Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
2223
Callable &capture = *(Callable *)p_user;
2224
if (!capture.is_valid()) {
2225
return FAILED;
2226
}
2227
Variant cmd = p_cmd, data = p_data;
2228
const Variant *args[2] = { &cmd, &data };
2229
Variant retval;
2230
Callable::CallError err;
2231
capture.callp(args, 2, retval, err);
2232
ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, FAILED, vformat("Error calling 'capture' to callable: %s.", Variant::get_callable_error_text(capture, args, 2, err)));
2233
ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, vformat("Error calling 'capture' to callable: '%s'. Return type is not bool.", String(capture)));
2234
r_captured = retval;
2235
return OK;
2236
}
2237
2238
void EngineDebugger::line_poll() {
2239
ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't poll. No active debugger");
2240
::EngineDebugger::get_singleton()->line_poll();
2241
}
2242
2243
void EngineDebugger::set_lines_left(int p_lines) {
2244
ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't set lines left. No active debugger");
2245
::EngineDebugger::get_script_debugger()->set_lines_left(p_lines);
2246
}
2247
2248
int EngineDebugger::get_lines_left() const {
2249
ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), 0, "Can't get lines left. No active debugger");
2250
return ::EngineDebugger::get_script_debugger()->get_lines_left();
2251
}
2252
2253
void EngineDebugger::set_depth(int p_depth) {
2254
ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't set depth. No active debugger");
2255
::EngineDebugger::get_script_debugger()->set_depth(p_depth);
2256
}
2257
2258
int EngineDebugger::get_depth() const {
2259
ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), 0, "Can't get depth. No active debugger");
2260
return ::EngineDebugger::get_script_debugger()->get_depth();
2261
}
2262
2263
bool EngineDebugger::is_breakpoint(int p_line, const StringName &p_source) const {
2264
ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), false, "Can't check breakpoint. No active debugger");
2265
return ::EngineDebugger::get_script_debugger()->is_breakpoint(p_line, p_source);
2266
}
2267
2268
bool EngineDebugger::is_skipping_breakpoints() const {
2269
ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), false, "Can't check skipping breakpoint. No active debugger");
2270
return ::EngineDebugger::get_script_debugger()->is_skipping_breakpoints();
2271
}
2272
2273
void EngineDebugger::insert_breakpoint(int p_line, const StringName &p_source) {
2274
ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't insert breakpoint. No active debugger");
2275
::EngineDebugger::get_script_debugger()->insert_breakpoint(p_line, p_source);
2276
}
2277
2278
void EngineDebugger::remove_breakpoint(int p_line, const StringName &p_source) {
2279
ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't remove breakpoint. No active debugger");
2280
::EngineDebugger::get_script_debugger()->remove_breakpoint(p_line, p_source);
2281
}
2282
2283
void EngineDebugger::clear_breakpoints() {
2284
ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't clear breakpoints. No active debugger");
2285
::EngineDebugger::get_script_debugger()->clear_breakpoints();
2286
}
2287
2288
EngineDebugger::~EngineDebugger() {
2289
for (const KeyValue<StringName, Callable> &E : captures) {
2290
::EngineDebugger::unregister_message_capture(E.key);
2291
}
2292
captures.clear();
2293
}
2294
2295
void EngineDebugger::_bind_methods() {
2296
ClassDB::bind_method(D_METHOD("is_active"), &EngineDebugger::is_active);
2297
2298
ClassDB::bind_method(D_METHOD("register_profiler", "name", "profiler"), &EngineDebugger::register_profiler);
2299
ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &EngineDebugger::unregister_profiler);
2300
2301
ClassDB::bind_method(D_METHOD("is_profiling", "name"), &EngineDebugger::is_profiling);
2302
ClassDB::bind_method(D_METHOD("has_profiler", "name"), &EngineDebugger::has_profiler);
2303
2304
ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &EngineDebugger::profiler_add_frame_data);
2305
ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &EngineDebugger::profiler_enable, DEFVAL(Array()));
2306
2307
ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &EngineDebugger::register_message_capture);
2308
ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &EngineDebugger::unregister_message_capture);
2309
ClassDB::bind_method(D_METHOD("has_capture", "name"), &EngineDebugger::has_capture);
2310
2311
ClassDB::bind_method(D_METHOD("line_poll"), &EngineDebugger::line_poll);
2312
2313
ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &EngineDebugger::send_message);
2314
ClassDB::bind_method(D_METHOD("debug", "can_continue", "is_error_breakpoint"), &EngineDebugger::debug, DEFVAL(true), DEFVAL(false));
2315
ClassDB::bind_method(D_METHOD("script_debug", "language", "can_continue", "is_error_breakpoint"), &EngineDebugger::script_debug, DEFVAL(true), DEFVAL(false));
2316
2317
ClassDB::bind_method(D_METHOD("set_lines_left", "lines"), &EngineDebugger::set_lines_left);
2318
ClassDB::bind_method(D_METHOD("get_lines_left"), &EngineDebugger::get_lines_left);
2319
2320
ClassDB::bind_method(D_METHOD("set_depth", "depth"), &EngineDebugger::set_depth);
2321
ClassDB::bind_method(D_METHOD("get_depth"), &EngineDebugger::get_depth);
2322
2323
ClassDB::bind_method(D_METHOD("is_breakpoint", "line", "source"), &EngineDebugger::is_breakpoint);
2324
ClassDB::bind_method(D_METHOD("is_skipping_breakpoints"), &EngineDebugger::is_skipping_breakpoints);
2325
ClassDB::bind_method(D_METHOD("insert_breakpoint", "line", "source"), &EngineDebugger::insert_breakpoint);
2326
ClassDB::bind_method(D_METHOD("remove_breakpoint", "line", "source"), &EngineDebugger::remove_breakpoint);
2327
ClassDB::bind_method(D_METHOD("clear_breakpoints"), &EngineDebugger::clear_breakpoints);
2328
}
2329
2330
} // namespace CoreBind
2331
2332