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