1 /*************************************************************************/
2 /* core_bind.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
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 #include "core_bind.h"
31
32 #include "core/globals.h"
33 #include "geometry.h"
34 #include "io/file_access_encrypted.h"
35 #include "io/marshalls.h"
36 #include "os/keyboard.h"
37 #include "os/os.h"
38
39 #include "thirdparty/misc/base64.h"
40
41 /**
42 * Time constants borrowed from loc_time.h
43 */
44 #define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
45 #define SECS_DAY (24L * 60L * 60L)
46 #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
47 #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
48 #define SECOND_KEY "second"
49 #define MINUTE_KEY "minute"
50 #define HOUR_KEY "hour"
51 #define DAY_KEY "day"
52 #define MONTH_KEY "month"
53 #define YEAR_KEY "year"
54 #define WEEKDAY_KEY "weekday"
55 #define DST_KEY "dst"
56
57 /// Table of number of days in each month (for regular year and leap year)
58 static const unsigned int MONTH_DAYS_TABLE[2][12] = {
59 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
60 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
61 };
62
63 _ResourceLoader *_ResourceLoader::singleton = NULL;
64
load_interactive(const String & p_path,const String & p_type_hint)65 Ref<ResourceInteractiveLoader> _ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint) {
66 return ResourceLoader::load_interactive(p_path, p_type_hint);
67 }
68
load(const String & p_path,const String & p_type_hint,bool p_no_cache)69 RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache) {
70
71 Error err = OK;
72 RES ret = ResourceLoader::load(p_path, p_type_hint, p_no_cache, &err);
73
74 if (err != OK) {
75 ERR_EXPLAIN("Error loading resource: '" + p_path + "'");
76 ERR_FAIL_COND_V(err != OK, ret);
77 }
78 return ret;
79 }
80
get_recognized_extensions_for_type(const String & p_type)81 DVector<String> _ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
82
83 List<String> exts;
84 ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
85 DVector<String> ret;
86 for (List<String>::Element *E = exts.front(); E; E = E->next()) {
87
88 ret.push_back(E->get());
89 }
90
91 return ret;
92 }
93
set_abort_on_missing_resources(bool p_abort)94 void _ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
95
96 ResourceLoader::set_abort_on_missing_resources(p_abort);
97 }
98
get_dependencies(const String & p_path)99 StringArray _ResourceLoader::get_dependencies(const String &p_path) {
100
101 List<String> deps;
102 ResourceLoader::get_dependencies(p_path, &deps);
103
104 StringArray ret;
105 for (List<String>::Element *E = deps.front(); E; E = E->next()) {
106 ret.push_back(E->get());
107 }
108
109 return ret;
110 };
111
has(const String & p_path)112 bool _ResourceLoader::has(const String &p_path) {
113
114 String local_path = Globals::get_singleton()->localize_path(p_path);
115 return ResourceCache::has(local_path);
116 };
117
load_import_metadata(const String & p_path)118 Ref<ResourceImportMetadata> _ResourceLoader::load_import_metadata(const String &p_path) {
119
120 return ResourceLoader::load_import_metadata(p_path);
121 }
122
_bind_methods()123 void _ResourceLoader::_bind_methods() {
124
125 ObjectTypeDB::bind_method(_MD("load_interactive:ResourceInteractiveLoader", "path", "type_hint"), &_ResourceLoader::load_interactive, DEFVAL(""));
126 ObjectTypeDB::bind_method(_MD("load:Resource", "path", "type_hint", "p_no_cache"), &_ResourceLoader::load, DEFVAL(""), DEFVAL(false));
127 ObjectTypeDB::bind_method(_MD("load_import_metadata:ResourceImportMetadata", "path"), &_ResourceLoader::load_import_metadata);
128 ObjectTypeDB::bind_method(_MD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type);
129 ObjectTypeDB::bind_method(_MD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources);
130 ObjectTypeDB::bind_method(_MD("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
131 ObjectTypeDB::bind_method(_MD("has", "path"), &_ResourceLoader::has);
132 }
133
_ResourceLoader()134 _ResourceLoader::_ResourceLoader() {
135
136 singleton = this;
137 }
138
save(const String & p_path,const RES & p_resource,uint32_t p_flags)139 Error _ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
140
141 ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
142 return ResourceSaver::save(p_path, p_resource, p_flags);
143 }
144
get_recognized_extensions(const RES & p_resource)145 DVector<String> _ResourceSaver::get_recognized_extensions(const RES &p_resource) {
146
147 ERR_FAIL_COND_V(p_resource.is_null(), DVector<String>());
148 List<String> exts;
149 ResourceSaver::get_recognized_extensions(p_resource, &exts);
150 DVector<String> ret;
151 for (List<String>::Element *E = exts.front(); E; E = E->next()) {
152
153 ret.push_back(E->get());
154 }
155 return ret;
156 }
157
158 _ResourceSaver *_ResourceSaver::singleton = NULL;
159
_bind_methods()160 void _ResourceSaver::_bind_methods() {
161
162 ObjectTypeDB::bind_method(_MD("save", "path", "resource:Resource", "flags"), &_ResourceSaver::save, DEFVAL(0));
163 ObjectTypeDB::bind_method(_MD("get_recognized_extensions", "type"), &_ResourceSaver::get_recognized_extensions);
164
165 BIND_CONSTANT(FLAG_RELATIVE_PATHS);
166 BIND_CONSTANT(FLAG_BUNDLE_RESOURCES);
167 BIND_CONSTANT(FLAG_CHANGE_PATH);
168 BIND_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
169 BIND_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
170 BIND_CONSTANT(FLAG_COMPRESS);
171 }
172
_ResourceSaver()173 _ResourceSaver::_ResourceSaver() {
174
175 singleton = this;
176 }
177
178 /////////////////OS
179
get_mouse_pos() const180 Point2 _OS::get_mouse_pos() const {
181
182 return OS::get_singleton()->get_mouse_pos();
183 }
set_window_title(const String & p_title)184 void _OS::set_window_title(const String &p_title) {
185
186 OS::get_singleton()->set_window_title(p_title);
187 }
188
get_mouse_button_state() const189 int _OS::get_mouse_button_state() const {
190
191 return OS::get_singleton()->get_mouse_button_state();
192 }
193
get_unique_ID() const194 String _OS::get_unique_ID() const {
195 return OS::get_singleton()->get_unique_ID();
196 }
has_touchscreen_ui_hint() const197 bool _OS::has_touchscreen_ui_hint() const {
198
199 return OS::get_singleton()->has_touchscreen_ui_hint();
200 }
201
set_clipboard(const String & p_text)202 void _OS::set_clipboard(const String &p_text) {
203
204 OS::get_singleton()->set_clipboard(p_text);
205 }
get_clipboard() const206 String _OS::get_clipboard() const {
207
208 return OS::get_singleton()->get_clipboard();
209 }
210
set_video_mode(const Size2 & p_size,bool p_fullscreen,bool p_resizeable,int p_screen)211 void _OS::set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen) {
212
213 OS::VideoMode vm;
214 vm.width = p_size.width;
215 vm.height = p_size.height;
216 vm.fullscreen = p_fullscreen;
217 vm.resizable = p_resizeable;
218 OS::get_singleton()->set_video_mode(vm, p_screen);
219 }
get_video_mode(int p_screen) const220 Size2 _OS::get_video_mode(int p_screen) const {
221
222 OS::VideoMode vm;
223 vm = OS::get_singleton()->get_video_mode(p_screen);
224 return Size2(vm.width, vm.height);
225 }
is_video_mode_fullscreen(int p_screen) const226 bool _OS::is_video_mode_fullscreen(int p_screen) const {
227
228 OS::VideoMode vm;
229 vm = OS::get_singleton()->get_video_mode(p_screen);
230 return vm.fullscreen;
231 }
232
get_screen_count() const233 int _OS::get_screen_count() const {
234 return OS::get_singleton()->get_screen_count();
235 }
236
get_current_screen() const237 int _OS::get_current_screen() const {
238 return OS::get_singleton()->get_current_screen();
239 }
240
set_current_screen(int p_screen)241 void _OS::set_current_screen(int p_screen) {
242 OS::get_singleton()->set_current_screen(p_screen);
243 }
244
get_screen_position(int p_screen) const245 Point2 _OS::get_screen_position(int p_screen) const {
246 return OS::get_singleton()->get_screen_position(p_screen);
247 }
248
get_screen_size(int p_screen) const249 Size2 _OS::get_screen_size(int p_screen) const {
250 return OS::get_singleton()->get_screen_size(p_screen);
251 }
252
get_screen_dpi(int p_screen) const253 int _OS::get_screen_dpi(int p_screen) const {
254
255 return OS::get_singleton()->get_screen_dpi(p_screen);
256 }
257
get_window_position() const258 Point2 _OS::get_window_position() const {
259 return OS::get_singleton()->get_window_position();
260 }
261
set_window_position(const Point2 & p_position)262 void _OS::set_window_position(const Point2 &p_position) {
263 OS::get_singleton()->set_window_position(p_position);
264 }
265
get_window_size() const266 Size2 _OS::get_window_size() const {
267 return OS::get_singleton()->get_window_size();
268 }
269
get_real_window_size() const270 Size2 _OS::get_real_window_size() const {
271 return OS::get_singleton()->get_real_window_size();
272 }
273
set_window_size(const Size2 & p_size)274 void _OS::set_window_size(const Size2 &p_size) {
275 OS::get_singleton()->set_window_size(p_size);
276 }
277
set_window_fullscreen(bool p_enabled)278 void _OS::set_window_fullscreen(bool p_enabled) {
279 OS::get_singleton()->set_window_fullscreen(p_enabled);
280 }
281
is_window_fullscreen() const282 bool _OS::is_window_fullscreen() const {
283 return OS::get_singleton()->is_window_fullscreen();
284 }
285
set_window_resizable(bool p_enabled)286 void _OS::set_window_resizable(bool p_enabled) {
287 OS::get_singleton()->set_window_resizable(p_enabled);
288 }
289
is_window_resizable() const290 bool _OS::is_window_resizable() const {
291 return OS::get_singleton()->is_window_resizable();
292 }
293
set_window_minimized(bool p_enabled)294 void _OS::set_window_minimized(bool p_enabled) {
295 OS::get_singleton()->set_window_minimized(p_enabled);
296 }
297
is_window_minimized() const298 bool _OS::is_window_minimized() const {
299 return OS::get_singleton()->is_window_minimized();
300 }
301
set_window_maximized(bool p_enabled)302 void _OS::set_window_maximized(bool p_enabled) {
303 OS::get_singleton()->set_window_maximized(p_enabled);
304 }
305
is_window_maximized() const306 bool _OS::is_window_maximized() const {
307 return OS::get_singleton()->is_window_maximized();
308 }
309
set_window_always_on_top(bool p_enabled)310 void _OS::set_window_always_on_top(bool p_enabled) {
311 OS::get_singleton()->set_window_always_on_top(p_enabled);
312 }
313
is_window_always_on_top() const314 bool _OS::is_window_always_on_top() const {
315 return OS::get_singleton()->is_window_always_on_top();
316 }
317
set_borderless_window(bool p_borderless)318 void _OS::set_borderless_window(bool p_borderless) {
319 OS::get_singleton()->set_borderless_window(p_borderless);
320 }
321
get_borderless_window() const322 bool _OS::get_borderless_window() const {
323 return OS::get_singleton()->get_borderless_window();
324 }
325
set_use_file_access_save_and_swap(bool p_enable)326 void _OS::set_use_file_access_save_and_swap(bool p_enable) {
327
328 FileAccess::set_backup_save(p_enable);
329 }
330
is_video_mode_resizable(int p_screen) const331 bool _OS::is_video_mode_resizable(int p_screen) const {
332
333 OS::VideoMode vm;
334 vm = OS::get_singleton()->get_video_mode(p_screen);
335 return vm.resizable;
336 }
337
get_fullscreen_mode_list(int p_screen) const338 Array _OS::get_fullscreen_mode_list(int p_screen) const {
339
340 List<OS::VideoMode> vmlist;
341 OS::get_singleton()->get_fullscreen_mode_list(&vmlist, p_screen);
342 Array vmarr;
343 for (List<OS::VideoMode>::Element *E = vmlist.front(); E; E = E->next()) {
344
345 vmarr.push_back(Size2(E->get().width, E->get().height));
346 }
347
348 return vmarr;
349 }
350
set_iterations_per_second(int p_ips)351 void _OS::set_iterations_per_second(int p_ips) {
352
353 OS::get_singleton()->set_iterations_per_second(p_ips);
354 }
get_iterations_per_second() const355 int _OS::get_iterations_per_second() const {
356
357 return OS::get_singleton()->get_iterations_per_second();
358 }
359
set_target_fps(int p_fps)360 void _OS::set_target_fps(int p_fps) {
361 OS::get_singleton()->set_target_fps(p_fps);
362 }
363
get_target_fps() const364 float _OS::get_target_fps() const {
365 return OS::get_singleton()->get_target_fps();
366 }
367
set_low_processor_usage_mode(bool p_enabled)368 void _OS::set_low_processor_usage_mode(bool p_enabled) {
369
370 OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
371 }
is_in_low_processor_usage_mode() const372 bool _OS::is_in_low_processor_usage_mode() const {
373
374 return OS::get_singleton()->is_in_low_processor_usage_mode();
375 }
376
get_executable_path() const377 String _OS::get_executable_path() const {
378
379 return OS::get_singleton()->get_executable_path();
380 }
381
shell_open(String p_uri)382 Error _OS::shell_open(String p_uri) {
383
384 return OS::get_singleton()->shell_open(p_uri);
385 };
386
execute(const String & p_path,const Vector<String> & p_arguments,bool p_blocking,Array p_output)387 int _OS::execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output) {
388
389 OS::ProcessID pid;
390 List<String> args;
391 for (int i = 0; i < p_arguments.size(); i++)
392 args.push_back(p_arguments[i]);
393 String pipe;
394 Error err = OS::get_singleton()->execute(p_path, args, p_blocking, &pid, &pipe);
395 p_output.clear();
396 p_output.push_back(pipe);
397 if (err != OK)
398 return -1;
399 else
400 return pid;
401 }
kill(int p_pid)402 Error _OS::kill(int p_pid) {
403
404 return OS::get_singleton()->kill(p_pid);
405 }
406
get_process_ID() const407 int _OS::get_process_ID() const {
408
409 return OS::get_singleton()->get_process_ID();
410 };
411
has_environment(const String & p_var) const412 bool _OS::has_environment(const String &p_var) const {
413
414 return OS::get_singleton()->has_environment(p_var);
415 }
get_environment(const String & p_var) const416 String _OS::get_environment(const String &p_var) const {
417
418 return OS::get_singleton()->get_environment(p_var);
419 }
420
get_name() const421 String _OS::get_name() const {
422
423 return OS::get_singleton()->get_name();
424 }
get_cmdline_args()425 Vector<String> _OS::get_cmdline_args() {
426
427 List<String> cmdline = OS::get_singleton()->get_cmdline_args();
428 Vector<String> cmdlinev;
429 for (List<String>::Element *E = cmdline.front(); E; E = E->next()) {
430
431 cmdlinev.push_back(E->get());
432 }
433
434 return cmdlinev;
435 }
436
get_locale() const437 String _OS::get_locale() const {
438
439 return OS::get_singleton()->get_locale();
440 }
441
get_latin_keyboard_variant() const442 String _OS::get_latin_keyboard_variant() const {
443 switch (OS::get_singleton()->get_latin_keyboard_variant()) {
444 case OS::LATIN_KEYBOARD_QWERTY: return "QWERTY";
445 case OS::LATIN_KEYBOARD_QWERTZ: return "QWERTZ";
446 case OS::LATIN_KEYBOARD_AZERTY: return "AZERTY";
447 case OS::LATIN_KEYBOARD_QZERTY: return "QZERTY";
448 case OS::LATIN_KEYBOARD_DVORAK: return "DVORAK";
449 case OS::LATIN_KEYBOARD_NEO: return "NEO";
450 case OS::LATIN_KEYBOARD_COLEMAK: return "COLEMAK";
451 default: return "ERROR";
452 }
453 }
454
get_model_name() const455 String _OS::get_model_name() const {
456
457 return OS::get_singleton()->get_model_name();
458 }
459
get_main_loop() const460 MainLoop *_OS::get_main_loop() const {
461
462 return OS::get_singleton()->get_main_loop();
463 }
464
set_time_scale(float p_scale)465 void _OS::set_time_scale(float p_scale) {
466 OS::get_singleton()->set_time_scale(p_scale);
467 }
468
get_time_scale()469 float _OS::get_time_scale() {
470
471 return OS::get_singleton()->get_time_scale();
472 }
473
is_ok_left_and_cancel_right() const474 bool _OS::is_ok_left_and_cancel_right() const {
475
476 return OS::get_singleton()->get_swap_ok_cancel();
477 }
478
set_thread_name(const String & p_name)479 Error _OS::set_thread_name(const String &p_name) {
480
481 return Thread::set_name(p_name);
482 };
483
set_use_vsync(bool p_enable)484 void _OS::set_use_vsync(bool p_enable) {
485 OS::get_singleton()->set_use_vsync(p_enable);
486 }
487
is_vsync_enabled() const488 bool _OS::is_vsync_enabled() const {
489
490 return OS::get_singleton()->is_vsync_enabled();
491 }
492
493 /*
494 enum Weekday {
495 DAY_SUNDAY,
496 DAY_MONDAY,
497 DAY_TUESDAY,
498 DAY_WEDNESDAY,
499 DAY_THURSDAY,
500 DAY_FRIDAY,
501 DAY_SATURDAY
502 };
503
504 enum Month {
505 MONTH_JANUARY,
506 MONTH_FEBRUARY,
507 MONTH_MARCH,
508 MONTH_APRIL,
509 MONTH_MAY,
510 MONTH_JUNE,
511 MONTH_JULY,
512 MONTH_AUGUST,
513 MONTH_SEPTEMBER,
514 MONTH_OCTOBER,
515 MONTH_NOVEMBER,
516 MONTH_DECEMBER
517 };
518 */
519 /*
520 struct Date {
521
522 int year;
523 Month month;
524 int day;
525 Weekday weekday;
526 bool dst;
527 };
528
529 struct Time {
530
531 int hour;
532 int min;
533 int sec;
534 };
535 */
536
get_static_memory_usage() const537 int _OS::get_static_memory_usage() const {
538
539 return OS::get_singleton()->get_static_memory_usage();
540 }
541
get_static_memory_peak_usage() const542 int _OS::get_static_memory_peak_usage() const {
543
544 return OS::get_singleton()->get_static_memory_peak_usage();
545 }
546
get_dynamic_memory_usage() const547 int _OS::get_dynamic_memory_usage() const {
548
549 return OS::get_singleton()->get_dynamic_memory_usage();
550 }
551
set_icon(const Image & p_icon)552 void _OS::set_icon(const Image &p_icon) {
553
554 OS::get_singleton()->set_icon(p_icon);
555 }
556
get_exit_code() const557 int _OS::get_exit_code() const {
558
559 return OS::get_singleton()->get_exit_code();
560 }
561
set_exit_code(int p_code)562 void _OS::set_exit_code(int p_code) {
563
564 OS::get_singleton()->set_exit_code(p_code);
565 }
566
567 /**
568 * Get current datetime with consideration for utc and
569 * dst
570 */
get_datetime(bool utc) const571 Dictionary _OS::get_datetime(bool utc) const {
572
573 Dictionary dated = get_date(utc);
574 Dictionary timed = get_time(utc);
575
576 List<Variant> keys;
577 timed.get_key_list(&keys);
578
579 for (int i = 0; i < keys.size(); i++) {
580 dated[keys[i]] = timed[keys[i]];
581 }
582
583 return dated;
584 }
585
get_date(bool utc) const586 Dictionary _OS::get_date(bool utc) const {
587
588 OS::Date date = OS::get_singleton()->get_date(utc);
589 Dictionary dated;
590 dated[YEAR_KEY] = date.year;
591 dated[MONTH_KEY] = date.month;
592 dated[DAY_KEY] = date.day;
593 dated[WEEKDAY_KEY] = date.weekday;
594 dated[DST_KEY] = date.dst;
595 return dated;
596 }
597
get_time(bool utc) const598 Dictionary _OS::get_time(bool utc) const {
599
600 OS::Time time = OS::get_singleton()->get_time(utc);
601 Dictionary timed;
602 timed[HOUR_KEY] = time.hour;
603 timed[MINUTE_KEY] = time.min;
604 timed[SECOND_KEY] = time.sec;
605 return timed;
606 }
607
608 /**
609 * Get a epoch time value from a dictionary of time values
610 * @p datetime must be populated with the following keys:
611 * day, hour, minute, month, second, year. (dst is ignored).
612 *
613 * You can pass the output from
614 * get_datetime_from_unix_time directly into this function
615 *
616 * @param datetime dictionary of date and time values to convert
617 *
618 * @return epoch calculated
619 */
get_unix_time_from_datetime(Dictionary datetime) const620 uint64_t _OS::get_unix_time_from_datetime(Dictionary datetime) const {
621
622 // Bunch of conversion constants
623 static const unsigned int SECONDS_PER_MINUTE = 60;
624 static const unsigned int MINUTES_PER_HOUR = 60;
625 static const unsigned int HOURS_PER_DAY = 24;
626 static const unsigned int SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
627 static const unsigned int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
628
629 // Get all time values from the dictionary, set to zero if it doesn't exist.
630 // Risk incorrect calculation over throwing errors
631 unsigned int second = ((datetime.has(SECOND_KEY)) ? static_cast<unsigned int>(datetime[SECOND_KEY]) : 0);
632 unsigned int minute = ((datetime.has(MINUTE_KEY)) ? static_cast<unsigned int>(datetime[MINUTE_KEY]) : 0);
633 unsigned int hour = ((datetime.has(HOUR_KEY)) ? static_cast<unsigned int>(datetime[HOUR_KEY]) : 0);
634 unsigned int day = ((datetime.has(DAY_KEY)) ? static_cast<unsigned int>(datetime[DAY_KEY]) : 0);
635 unsigned int month = ((datetime.has(MONTH_KEY)) ? static_cast<unsigned int>(datetime[MONTH_KEY]) - 1 : 0);
636 unsigned int year = ((datetime.has(YEAR_KEY)) ? static_cast<unsigned int>(datetime[YEAR_KEY]) : 0);
637
638 /// How many days come before each month (0-12)
639 static const unsigned short int DAYS_PAST_THIS_YEAR_TABLE[2][13] = {
640 /* Normal years. */
641 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
642 /* Leap years. */
643 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
644 };
645
646 ERR_EXPLAIN("Invalid second value of: " + itos(second));
647 ERR_FAIL_COND_V(second > 59, 0);
648
649 ERR_EXPLAIN("Invalid minute value of: " + itos(minute));
650 ERR_FAIL_COND_V(minute > 59, 0);
651
652 ERR_EXPLAIN("Invalid hour value of: " + itos(hour));
653 ERR_FAIL_COND_V(hour > 23, 0);
654
655 ERR_EXPLAIN("Invalid month value of: " + itos(month + 1));
656 ERR_FAIL_COND_V(month + 1 > 12, 0);
657
658 // Do this check after month is tested as valid
659 ERR_EXPLAIN("Invalid day value of: " + itos(day) + " which is larger than " + itos(MONTH_DAYS_TABLE[LEAPYEAR(year)][month]));
660 ERR_FAIL_COND_V(day > MONTH_DAYS_TABLE[LEAPYEAR(year)][month], 0);
661
662 // Calculate all the seconds from months past in this year
663 uint64_t SECONDS_FROM_MONTHS_PAST_THIS_YEAR = DAYS_PAST_THIS_YEAR_TABLE[LEAPYEAR(year)][month] * SECONDS_PER_DAY;
664
665 uint64_t SECONDS_FROM_YEARS_PAST = 0;
666 for (unsigned int iyear = EPOCH_YR; iyear < year; iyear++) {
667
668 SECONDS_FROM_YEARS_PAST += YEARSIZE(iyear) * SECONDS_PER_DAY;
669 }
670
671 uint64_t epoch =
672 second +
673 minute * SECONDS_PER_MINUTE +
674 hour * SECONDS_PER_HOUR +
675 // Subtract 1 from day, since the current day isn't over yet
676 // and we cannot count all 24 hours.
677 (day - 1) * SECONDS_PER_DAY +
678 SECONDS_FROM_MONTHS_PAST_THIS_YEAR +
679 SECONDS_FROM_YEARS_PAST;
680 return epoch;
681 }
682
683 /**
684 * Get a dictionary of time values when given epoch time
685 *
686 * Dictionary Time values will be a union if values from #get_time
687 * and #get_date dictionaries (with the exception of dst =
688 * day light standard time, as it cannot be determined from epoch)
689 *
690 * @param unix_time_val epoch time to convert
691 *
692 * @return dictionary of date and time values
693 */
get_datetime_from_unix_time(uint64_t unix_time_val) const694 Dictionary _OS::get_datetime_from_unix_time(uint64_t unix_time_val) const {
695
696 // Just fail if unix time is negative (when interpreted as an int).
697 // This means the user passed in a negative value by accident
698 ERR_EXPLAIN("unix_time_val was really huge!" + itos(unix_time_val) + " You probably passed in a negative value!");
699 ERR_FAIL_COND_V((int64_t)unix_time_val < 0, Dictionary());
700
701 OS::Date date;
702 OS::Time time;
703
704 unsigned long dayclock, dayno;
705 int year = EPOCH_YR;
706
707 dayclock = (unsigned long)unix_time_val % SECS_DAY;
708 dayno = (unsigned long)unix_time_val / SECS_DAY;
709
710 time.sec = dayclock % 60;
711 time.min = (dayclock % 3600) / 60;
712 time.hour = dayclock / 3600;
713
714 /* day 0 was a thursday */
715 date.weekday = static_cast<OS::Weekday>((dayno + 4) % 7);
716
717 while (dayno >= YEARSIZE(year)) {
718 dayno -= YEARSIZE(year);
719 year++;
720 }
721
722 date.year = year;
723
724 size_t imonth = 0;
725
726 while (dayno >= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth]) {
727 dayno -= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth];
728 imonth++;
729 }
730
731 /// Add 1 to month to make sure months are indexed starting at 1
732 date.month = static_cast<OS::Month>(imonth + 1);
733
734 date.day = dayno + 1;
735
736 Dictionary timed;
737 timed[HOUR_KEY] = time.hour;
738 timed[MINUTE_KEY] = time.min;
739 timed[SECOND_KEY] = time.sec;
740 timed[YEAR_KEY] = date.year;
741 timed[MONTH_KEY] = date.month;
742 timed[DAY_KEY] = date.day;
743 timed[WEEKDAY_KEY] = date.weekday;
744
745 return timed;
746 }
747
get_time_zone_info() const748 Dictionary _OS::get_time_zone_info() const {
749 OS::TimeZoneInfo info = OS::get_singleton()->get_time_zone_info();
750 Dictionary infod;
751 infod["bias"] = info.bias;
752 infod["name"] = info.name;
753 return infod;
754 }
755
get_unix_time() const756 uint64_t _OS::get_unix_time() const {
757
758 return OS::get_singleton()->get_unix_time();
759 }
760
get_system_time_secs() const761 uint64_t _OS::get_system_time_secs() const {
762 return OS::get_singleton()->get_system_time_secs();
763 }
764
delay_usec(uint32_t p_usec) const765 void _OS::delay_usec(uint32_t p_usec) const {
766
767 OS::get_singleton()->delay_usec(p_usec);
768 }
769
delay_msec(uint32_t p_msec) const770 void _OS::delay_msec(uint32_t p_msec) const {
771
772 OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
773 }
774
get_ticks_msec() const775 uint32_t _OS::get_ticks_msec() const {
776
777 return OS::get_singleton()->get_ticks_msec();
778 }
779
get_splash_tick_msec() const780 uint32_t _OS::get_splash_tick_msec() const {
781
782 return OS::get_singleton()->get_splash_tick_msec();
783 }
784
can_use_threads() const785 bool _OS::can_use_threads() const {
786
787 return OS::get_singleton()->can_use_threads();
788 }
789
can_draw() const790 bool _OS::can_draw() const {
791
792 return OS::get_singleton()->can_draw();
793 }
794
get_frames_drawn()795 int _OS::get_frames_drawn() {
796
797 return OS::get_singleton()->get_frames_drawn();
798 }
799
get_processor_count() const800 int _OS::get_processor_count() const {
801
802 return OS::get_singleton()->get_processor_count();
803 }
804
is_stdout_verbose() const805 bool _OS::is_stdout_verbose() const {
806
807 return OS::get_singleton()->is_stdout_verbose();
808 }
809
dump_memory_to_file(const String & p_file)810 void _OS::dump_memory_to_file(const String &p_file) {
811
812 OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
813 }
814
815 struct _OSCoreBindImg {
816
817 String path;
818 Size2 size;
819 int fmt;
820 ObjectID id;
821 int vram;
operator <_OSCoreBindImg822 bool operator<(const _OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
823 };
824
print_all_textures_by_size()825 void _OS::print_all_textures_by_size() {
826
827 List<_OSCoreBindImg> imgs;
828 int total = 0;
829 {
830 List<Ref<Resource> > rsrc;
831 ResourceCache::get_cached_resources(&rsrc);
832
833 for (List<Ref<Resource> >::Element *E = rsrc.front(); E; E = E->next()) {
834
835 if (!E->get()->is_type("ImageTexture"))
836 continue;
837
838 Size2 size = E->get()->call("get_size");
839 int fmt = E->get()->call("get_format");
840
841 _OSCoreBindImg img;
842 img.size = size;
843 img.fmt = fmt;
844 img.path = E->get()->get_path();
845 img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt));
846 img.id = E->get()->get_instance_ID();
847 total += img.vram;
848 imgs.push_back(img);
849 }
850 }
851
852 imgs.sort();
853
854 for (List<_OSCoreBindImg>::Element *E = imgs.front(); E; E = E->next()) {
855
856 total -= E->get().vram;
857 }
858 }
859
print_resources_by_type(const Vector<String> & p_types)860 void _OS::print_resources_by_type(const Vector<String> &p_types) {
861
862 Map<String, int> type_count;
863
864 List<Ref<Resource> > resources;
865 ResourceCache::get_cached_resources(&resources);
866
867 List<Ref<Resource> > rsrc;
868 ResourceCache::get_cached_resources(&rsrc);
869
870 for (List<Ref<Resource> >::Element *E = rsrc.front(); E; E = E->next()) {
871
872 Ref<Resource> r = E->get();
873
874 bool found = false;
875
876 for (int i = 0; i < p_types.size(); i++) {
877 if (r->is_type(p_types[i]))
878 found = true;
879 }
880 if (!found)
881 continue;
882
883 if (!type_count.has(r->get_type())) {
884 type_count[r->get_type()] = 0;
885 }
886
887 type_count[r->get_type()]++;
888 }
889 };
890
has_virtual_keyboard() const891 bool _OS::has_virtual_keyboard() const {
892 return OS::get_singleton()->has_virtual_keyboard();
893 }
894
show_virtual_keyboard(const String & p_existing_text)895 void _OS::show_virtual_keyboard(const String &p_existing_text) {
896 OS::get_singleton()->show_virtual_keyboard(p_existing_text, Rect2());
897 }
898
hide_virtual_keyboard()899 void _OS::hide_virtual_keyboard() {
900 OS::get_singleton()->hide_virtual_keyboard();
901 }
902
print_all_resources(const String & p_to_file)903 void _OS::print_all_resources(const String &p_to_file) {
904
905 OS::get_singleton()->print_all_resources(p_to_file);
906 }
907
print_resources_in_use(bool p_short)908 void _OS::print_resources_in_use(bool p_short) {
909
910 OS::get_singleton()->print_resources_in_use(p_short);
911 }
912
dump_resources_to_file(const String & p_file)913 void _OS::dump_resources_to_file(const String &p_file) {
914
915 OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
916 }
917
get_data_dir() const918 String _OS::get_data_dir() const {
919
920 return OS::get_singleton()->get_data_dir();
921 };
922
get_frames_per_second() const923 float _OS::get_frames_per_second() const {
924
925 return OS::get_singleton()->get_frames_per_second();
926 }
927
native_video_play(String p_path,float p_volume,String p_audio_track,String p_subtitle_track)928 Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
929
930 return OS::get_singleton()->native_video_play(p_path, p_volume, p_audio_track, p_subtitle_track);
931 };
932
native_video_is_playing()933 bool _OS::native_video_is_playing() {
934
935 return OS::get_singleton()->native_video_is_playing();
936 };
937
native_video_pause()938 void _OS::native_video_pause() {
939
940 OS::get_singleton()->native_video_pause();
941 };
942
native_video_unpause()943 void _OS::native_video_unpause() {
944 OS::get_singleton()->native_video_unpause();
945 };
946
native_video_stop()947 void _OS::native_video_stop() {
948
949 OS::get_singleton()->native_video_stop();
950 };
951
request_attention()952 void _OS::request_attention() {
953
954 OS::get_singleton()->request_attention();
955 }
956
center_window()957 void _OS::center_window() {
958
959 OS::get_singleton()->center_window();
960 }
961
is_debug_build() const962 bool _OS::is_debug_build() const {
963
964 #ifdef DEBUG_ENABLED
965 return true;
966 #else
967 return false;
968 #endif
969 }
970
set_screen_orientation(ScreenOrientation p_orientation)971 void _OS::set_screen_orientation(ScreenOrientation p_orientation) {
972
973 OS::get_singleton()->set_screen_orientation(OS::ScreenOrientation(p_orientation));
974 }
975
get_screen_orientation() const976 _OS::ScreenOrientation _OS::get_screen_orientation() const {
977
978 return ScreenOrientation(OS::get_singleton()->get_screen_orientation());
979 }
980
set_keep_screen_on(bool p_enabled)981 void _OS::set_keep_screen_on(bool p_enabled) {
982
983 OS::get_singleton()->set_keep_screen_on(p_enabled);
984 }
985
is_keep_screen_on() const986 bool _OS::is_keep_screen_on() const {
987
988 return OS::get_singleton()->is_keep_screen_on();
989 }
990
get_system_dir(SystemDir p_dir) const991 String _OS::get_system_dir(SystemDir p_dir) const {
992
993 return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
994 }
995
get_custom_level() const996 String _OS::get_custom_level() const {
997
998 return OS::get_singleton()->get_custom_level();
999 }
1000
get_scancode_string(uint32_t p_code) const1001 String _OS::get_scancode_string(uint32_t p_code) const {
1002
1003 return keycode_get_string(p_code);
1004 }
is_scancode_unicode(uint32_t p_unicode) const1005 bool _OS::is_scancode_unicode(uint32_t p_unicode) const {
1006
1007 return keycode_has_unicode(p_unicode);
1008 }
find_scancode_from_string(const String & p_code) const1009 int _OS::find_scancode_from_string(const String &p_code) const {
1010
1011 return find_keycode(p_code);
1012 }
1013
alert(const String & p_alert,const String & p_title)1014 void _OS::alert(const String &p_alert, const String &p_title) {
1015
1016 OS::get_singleton()->alert(p_alert, p_title);
1017 }
1018
get_engine_version() const1019 Dictionary _OS::get_engine_version() const {
1020
1021 return OS::get_singleton()->get_engine_version();
1022 }
1023
1024 _OS *_OS::singleton = NULL;
1025
_bind_methods()1026 void _OS::_bind_methods() {
1027
1028 //ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&_OS::get_mouse_pos);
1029 //ObjectTypeDB::bind_method(_MD("is_mouse_grab_enabled"),&_OS::is_mouse_grab_enabled);
1030
1031 ObjectTypeDB::bind_method(_MD("set_clipboard", "clipboard"), &_OS::set_clipboard);
1032 ObjectTypeDB::bind_method(_MD("get_clipboard"), &_OS::get_clipboard);
1033
1034 ObjectTypeDB::bind_method(_MD("set_video_mode", "size", "fullscreen", "resizable", "screen"), &_OS::set_video_mode, DEFVAL(0));
1035 ObjectTypeDB::bind_method(_MD("get_video_mode_size", "screen"), &_OS::get_video_mode, DEFVAL(0));
1036 ObjectTypeDB::bind_method(_MD("is_video_mode_fullscreen", "screen"), &_OS::is_video_mode_fullscreen, DEFVAL(0));
1037 ObjectTypeDB::bind_method(_MD("is_video_mode_resizable", "screen"), &_OS::is_video_mode_resizable, DEFVAL(0));
1038 ObjectTypeDB::bind_method(_MD("get_fullscreen_mode_list", "screen"), &_OS::get_fullscreen_mode_list, DEFVAL(0));
1039
1040 ObjectTypeDB::bind_method(_MD("get_screen_count"), &_OS::get_screen_count);
1041 ObjectTypeDB::bind_method(_MD("get_current_screen"), &_OS::get_current_screen);
1042 ObjectTypeDB::bind_method(_MD("set_current_screen", "screen"), &_OS::set_current_screen);
1043 ObjectTypeDB::bind_method(_MD("get_screen_position", "screen"), &_OS::get_screen_position, DEFVAL(0));
1044 ObjectTypeDB::bind_method(_MD("get_screen_size", "screen"), &_OS::get_screen_size, DEFVAL(0));
1045 ObjectTypeDB::bind_method(_MD("get_screen_dpi", "screen"), &_OS::get_screen_dpi, DEFVAL(0));
1046 ObjectTypeDB::bind_method(_MD("get_window_position"), &_OS::get_window_position);
1047 ObjectTypeDB::bind_method(_MD("set_window_position", "position"), &_OS::set_window_position);
1048 ObjectTypeDB::bind_method(_MD("get_window_size"), &_OS::get_window_size);
1049 ObjectTypeDB::bind_method(_MD("set_window_size", "size"), &_OS::set_window_size);
1050 ObjectTypeDB::bind_method(_MD("set_window_fullscreen", "enabled"), &_OS::set_window_fullscreen);
1051 ObjectTypeDB::bind_method(_MD("is_window_fullscreen"), &_OS::is_window_fullscreen);
1052 ObjectTypeDB::bind_method(_MD("set_window_resizable", "enabled"), &_OS::set_window_resizable);
1053 ObjectTypeDB::bind_method(_MD("is_window_resizable"), &_OS::is_window_resizable);
1054 ObjectTypeDB::bind_method(_MD("set_window_minimized", "enabled"), &_OS::set_window_minimized);
1055 ObjectTypeDB::bind_method(_MD("is_window_minimized"), &_OS::is_window_minimized);
1056 ObjectTypeDB::bind_method(_MD("set_window_maximized", "enabled"), &_OS::set_window_maximized);
1057 ObjectTypeDB::bind_method(_MD("is_window_maximized"), &_OS::is_window_maximized);
1058 ObjectTypeDB::bind_method(_MD("set_window_always_on_top", "enabled"), &_OS::set_window_always_on_top);
1059 ObjectTypeDB::bind_method(_MD("is_window_always_on_top"), &_OS::is_window_always_on_top);
1060 ObjectTypeDB::bind_method(_MD("request_attention"), &_OS::request_attention);
1061 ObjectTypeDB::bind_method(_MD("get_real_window_size"), &_OS::get_real_window_size);
1062 ObjectTypeDB::bind_method(_MD("center_window"), &_OS::center_window);
1063
1064 ObjectTypeDB::bind_method(_MD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
1065 ObjectTypeDB::bind_method(_MD("get_borderless_window"), &_OS::get_borderless_window);
1066
1067 ObjectTypeDB::bind_method(_MD("set_screen_orientation", "orientation"), &_OS::set_screen_orientation);
1068 ObjectTypeDB::bind_method(_MD("get_screen_orientation"), &_OS::get_screen_orientation);
1069
1070 ObjectTypeDB::bind_method(_MD("set_keep_screen_on", "enabled"), &_OS::set_keep_screen_on);
1071 ObjectTypeDB::bind_method(_MD("is_keep_screen_on"), &_OS::is_keep_screen_on);
1072
1073 ObjectTypeDB::bind_method(_MD("set_iterations_per_second", "iterations_per_second"), &_OS::set_iterations_per_second);
1074 ObjectTypeDB::bind_method(_MD("get_iterations_per_second"), &_OS::get_iterations_per_second);
1075 ObjectTypeDB::bind_method(_MD("set_target_fps", "target_fps"), &_OS::set_target_fps);
1076 ObjectTypeDB::bind_method(_MD("get_target_fps"), &_OS::get_target_fps);
1077
1078 ObjectTypeDB::bind_method(_MD("set_time_scale", "time_scale"), &_OS::set_time_scale);
1079 ObjectTypeDB::bind_method(_MD("get_time_scale"), &_OS::get_time_scale);
1080
1081 ObjectTypeDB::bind_method(_MD("has_touchscreen_ui_hint"), &_OS::has_touchscreen_ui_hint);
1082
1083 ObjectTypeDB::bind_method(_MD("set_window_title", "title"), &_OS::set_window_title);
1084
1085 ObjectTypeDB::bind_method(_MD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
1086 ObjectTypeDB::bind_method(_MD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
1087
1088 ObjectTypeDB::bind_method(_MD("get_processor_count"), &_OS::get_processor_count);
1089
1090 ObjectTypeDB::bind_method(_MD("get_executable_path"), &_OS::get_executable_path);
1091 ObjectTypeDB::bind_method(_MD("execute", "path", "arguments", "blocking", "output"), &_OS::execute, DEFVAL(Array()));
1092 ObjectTypeDB::bind_method(_MD("kill", "pid"), &_OS::kill);
1093 ObjectTypeDB::bind_method(_MD("shell_open", "uri"), &_OS::shell_open);
1094 ObjectTypeDB::bind_method(_MD("get_process_ID"), &_OS::get_process_ID);
1095
1096 ObjectTypeDB::bind_method(_MD("get_environment", "environment"), &_OS::get_environment);
1097 ObjectTypeDB::bind_method(_MD("has_environment", "environment"), &_OS::has_environment);
1098
1099 ObjectTypeDB::bind_method(_MD("get_name"), &_OS::get_name);
1100 ObjectTypeDB::bind_method(_MD("get_cmdline_args"), &_OS::get_cmdline_args);
1101 ObjectTypeDB::bind_method(_MD("get_main_loop"), &_OS::get_main_loop);
1102
1103 ObjectTypeDB::bind_method(_MD("get_datetime", "utc"), &_OS::get_datetime, DEFVAL(false));
1104 ObjectTypeDB::bind_method(_MD("get_date", "utc"), &_OS::get_date, DEFVAL(false));
1105 ObjectTypeDB::bind_method(_MD("get_time", "utc"), &_OS::get_time, DEFVAL(false));
1106 ObjectTypeDB::bind_method(_MD("get_time_zone_info"), &_OS::get_time_zone_info);
1107 ObjectTypeDB::bind_method(_MD("get_unix_time"), &_OS::get_unix_time);
1108 ObjectTypeDB::bind_method(_MD("get_datetime_from_unix_time", "unix_time_val"),
1109 &_OS::get_datetime_from_unix_time);
1110 ObjectTypeDB::bind_method(_MD("get_unix_time_from_datetime", "datetime"),
1111 &_OS::get_unix_time_from_datetime);
1112 ObjectTypeDB::bind_method(_MD("get_system_time_secs"), &_OS::get_system_time_secs);
1113
1114 ObjectTypeDB::bind_method(_MD("set_icon", "icon"), &_OS::set_icon);
1115
1116 ObjectTypeDB::bind_method(_MD("get_exit_code"), &_OS::get_exit_code);
1117 ObjectTypeDB::bind_method(_MD("set_exit_code", "code"), &_OS::set_exit_code);
1118
1119 ObjectTypeDB::bind_method(_MD("delay_usec", "usec"), &_OS::delay_usec);
1120 ObjectTypeDB::bind_method(_MD("delay_msec", "msec"), &_OS::delay_msec);
1121 ObjectTypeDB::bind_method(_MD("get_ticks_msec"), &_OS::get_ticks_msec);
1122 ObjectTypeDB::bind_method(_MD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
1123 ObjectTypeDB::bind_method(_MD("get_locale"), &_OS::get_locale);
1124 ObjectTypeDB::bind_method(_MD("get_latin_keyboard_variant"), &_OS::get_latin_keyboard_variant);
1125 ObjectTypeDB::bind_method(_MD("get_model_name"), &_OS::get_model_name);
1126
1127 ObjectTypeDB::bind_method(_MD("get_custom_level"), &_OS::get_custom_level);
1128
1129 ObjectTypeDB::bind_method(_MD("can_draw"), &_OS::can_draw);
1130 ObjectTypeDB::bind_method(_MD("get_frames_drawn"), &_OS::get_frames_drawn);
1131 ObjectTypeDB::bind_method(_MD("is_stdout_verbose"), &_OS::is_stdout_verbose);
1132
1133 ObjectTypeDB::bind_method(_MD("can_use_threads"), &_OS::can_use_threads);
1134
1135 ObjectTypeDB::bind_method(_MD("is_debug_build"), &_OS::is_debug_build);
1136
1137 //ObjectTypeDB::bind_method(_MD("get_mouse_button_state"),&_OS::get_mouse_button_state);
1138
1139 ObjectTypeDB::bind_method(_MD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
1140 ObjectTypeDB::bind_method(_MD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
1141 ObjectTypeDB::bind_method(_MD("has_virtual_keyboard"), &_OS::has_virtual_keyboard);
1142 ObjectTypeDB::bind_method(_MD("show_virtual_keyboard", "existing_text"), &_OS::show_virtual_keyboard, DEFVAL(""));
1143 ObjectTypeDB::bind_method(_MD("hide_virtual_keyboard"), &_OS::hide_virtual_keyboard);
1144 ObjectTypeDB::bind_method(_MD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
1145 ObjectTypeDB::bind_method(_MD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
1146
1147 ObjectTypeDB::bind_method(_MD("get_static_memory_usage"), &_OS::get_static_memory_usage);
1148 ObjectTypeDB::bind_method(_MD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
1149 ObjectTypeDB::bind_method(_MD("get_dynamic_memory_usage"), &_OS::get_dynamic_memory_usage);
1150
1151 ObjectTypeDB::bind_method(_MD("get_data_dir"), &_OS::get_data_dir);
1152 ObjectTypeDB::bind_method(_MD("get_system_dir", "dir"), &_OS::get_system_dir);
1153 ObjectTypeDB::bind_method(_MD("get_unique_ID"), &_OS::get_unique_ID);
1154
1155 ObjectTypeDB::bind_method(_MD("is_ok_left_and_cancel_right"), &_OS::is_ok_left_and_cancel_right);
1156
1157 ObjectTypeDB::bind_method(_MD("get_frames_per_second"), &_OS::get_frames_per_second);
1158
1159 ObjectTypeDB::bind_method(_MD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
1160 ObjectTypeDB::bind_method(_MD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
1161
1162 ObjectTypeDB::bind_method(_MD("native_video_play", "path", "volume", "audio_track", "subtitle_track"), &_OS::native_video_play);
1163 ObjectTypeDB::bind_method(_MD("native_video_is_playing"), &_OS::native_video_is_playing);
1164 ObjectTypeDB::bind_method(_MD("native_video_stop"), &_OS::native_video_stop);
1165 ObjectTypeDB::bind_method(_MD("native_video_pause"), &_OS::native_video_pause);
1166 ObjectTypeDB::bind_method(_MD("native_video_unpause"), &_OS::native_video_unpause);
1167
1168 ObjectTypeDB::bind_method(_MD("get_scancode_string", "code"), &_OS::get_scancode_string);
1169 ObjectTypeDB::bind_method(_MD("is_scancode_unicode", "code"), &_OS::is_scancode_unicode);
1170 ObjectTypeDB::bind_method(_MD("find_scancode_from_string", "string"), &_OS::find_scancode_from_string);
1171
1172 ObjectTypeDB::bind_method(_MD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
1173
1174 ObjectTypeDB::bind_method(_MD("alert", "text", "title"), &_OS::alert, DEFVAL("Alert!"));
1175
1176 ObjectTypeDB::bind_method(_MD("set_thread_name", "name"), &_OS::set_thread_name);
1177
1178 ObjectTypeDB::bind_method(_MD("set_use_vsync", "enable"), &_OS::set_use_vsync);
1179 ObjectTypeDB::bind_method(_MD("is_vsync_enabled"), &_OS::is_vsync_enabled);
1180
1181 ObjectTypeDB::bind_method(_MD("get_engine_version"), &_OS::get_engine_version);
1182
1183 BIND_CONSTANT(DAY_SUNDAY);
1184 BIND_CONSTANT(DAY_MONDAY);
1185 BIND_CONSTANT(DAY_TUESDAY);
1186 BIND_CONSTANT(DAY_WEDNESDAY);
1187 BIND_CONSTANT(DAY_THURSDAY);
1188 BIND_CONSTANT(DAY_FRIDAY);
1189 BIND_CONSTANT(DAY_SATURDAY);
1190
1191 BIND_CONSTANT(MONTH_JANUARY);
1192 BIND_CONSTANT(MONTH_FEBRUARY);
1193 BIND_CONSTANT(MONTH_MARCH);
1194 BIND_CONSTANT(MONTH_APRIL);
1195 BIND_CONSTANT(MONTH_MAY);
1196 BIND_CONSTANT(MONTH_JUNE);
1197 BIND_CONSTANT(MONTH_JULY);
1198 BIND_CONSTANT(MONTH_AUGUST);
1199 BIND_CONSTANT(MONTH_SEPTEMBER);
1200 BIND_CONSTANT(MONTH_OCTOBER);
1201 BIND_CONSTANT(MONTH_NOVEMBER);
1202 BIND_CONSTANT(MONTH_DECEMBER);
1203
1204 BIND_CONSTANT(SCREEN_ORIENTATION_LANDSCAPE);
1205 BIND_CONSTANT(SCREEN_ORIENTATION_PORTRAIT);
1206 BIND_CONSTANT(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
1207 BIND_CONSTANT(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
1208 BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
1209 BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR_PORTRAIT);
1210 BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR);
1211
1212 BIND_CONSTANT(SYSTEM_DIR_DESKTOP);
1213 BIND_CONSTANT(SYSTEM_DIR_DCIM);
1214 BIND_CONSTANT(SYSTEM_DIR_DOCUMENTS);
1215 BIND_CONSTANT(SYSTEM_DIR_DOWNLOADS);
1216 BIND_CONSTANT(SYSTEM_DIR_MOVIES);
1217 BIND_CONSTANT(SYSTEM_DIR_MUSIC);
1218 BIND_CONSTANT(SYSTEM_DIR_PICTURES);
1219 BIND_CONSTANT(SYSTEM_DIR_RINGTONES);
1220 }
1221
_OS()1222 _OS::_OS() {
1223
1224 singleton = this;
1225 }
1226
1227 ///////////////////// GEOMETRY
1228
1229 _Geometry *_Geometry::singleton = NULL;
1230
get_singleton()1231 _Geometry *_Geometry::get_singleton() {
1232
1233 return singleton;
1234 }
1235
build_box_planes(const Vector3 & p_extents)1236 DVector<Plane> _Geometry::build_box_planes(const Vector3 &p_extents) {
1237
1238 return Geometry::build_box_planes(p_extents);
1239 }
1240
build_cylinder_planes(float p_radius,float p_height,int p_sides,Vector3::Axis p_axis)1241 DVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
1242
1243 return Geometry::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
1244 }
build_capsule_planes(float p_radius,float p_height,int p_sides,int p_lats,Vector3::Axis p_axis)1245 DVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
1246
1247 return Geometry::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
1248 }
1249
segment_intersects_circle(const Vector2 & p_from,const Vector2 & p_to,const Vector2 & p_circle_pos,real_t p_circle_radius)1250 real_t _Geometry::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
1251
1252 return Geometry::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
1253 }
1254
segment_intersects_segment_2d(const Vector2 & p_from_a,const Vector2 & p_to_a,const Vector2 & p_from_b,const Vector2 & p_to_b)1255 Variant _Geometry::segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b) {
1256
1257 Vector2 result;
1258 if (Geometry::segment_intersects_segment_2d(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
1259
1260 return result;
1261 } else {
1262 return Variant();
1263 };
1264 };
1265
get_closest_points_between_segments_2d(const Vector2 & p1,const Vector2 & q1,const Vector2 & p2,const Vector2 & q2)1266 DVector<Vector2> _Geometry::get_closest_points_between_segments_2d(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
1267
1268 Vector2 r1, r2;
1269 Geometry::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
1270 DVector<Vector2> r;
1271 r.resize(2);
1272 r.set(0, r1);
1273 r.set(1, r2);
1274 return r;
1275 }
1276
get_closest_points_between_segments(const Vector3 & p1,const Vector3 & p2,const Vector3 & q1,const Vector3 & q2)1277 DVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
1278
1279 Vector3 r1, r2;
1280 Geometry::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
1281 DVector<Vector3> r;
1282 r.resize(2);
1283 r.set(0, r1);
1284 r.set(1, r2);
1285 return r;
1286 }
get_closest_point_to_segment_2d(const Vector2 & p_point,const Vector2 & p_a,const Vector2 & p_b)1287 Vector2 _Geometry::get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
1288
1289 Vector2 s[2] = { p_a, p_b };
1290 return Geometry::get_closest_point_to_segment_2d(p_point, s);
1291 }
get_closest_point_to_segment(const Vector3 & p_point,const Vector3 & p_a,const Vector3 & p_b)1292 Vector3 _Geometry::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
1293
1294 Vector3 s[2] = { p_a, p_b };
1295 return Geometry::get_closest_point_to_segment(p_point, s);
1296 }
get_closest_point_to_segment_uncapped_2d(const Vector2 & p_point,const Vector2 & p_a,const Vector2 & p_b)1297 Vector2 _Geometry::get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
1298
1299 Vector2 s[2] = { p_a, p_b };
1300 return Geometry::get_closest_point_to_segment_uncapped_2d(p_point, s);
1301 }
get_closest_point_to_segment_uncapped(const Vector3 & p_point,const Vector3 & p_a,const Vector3 & p_b)1302 Vector3 _Geometry::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
1303
1304 Vector3 s[2] = { p_a, p_b };
1305 return Geometry::get_closest_point_to_segment_uncapped(p_point, s);
1306 }
ray_intersects_triangle(const Vector3 & p_from,const Vector3 & p_dir,const Vector3 & p_v0,const Vector3 & p_v1,const Vector3 & p_v2)1307 Variant _Geometry::ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
1308
1309 Vector3 res;
1310 if (Geometry::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res))
1311 return res;
1312 else
1313 return Variant();
1314 }
segment_intersects_triangle(const Vector3 & p_from,const Vector3 & p_to,const Vector3 & p_v0,const Vector3 & p_v1,const Vector3 & p_v2)1315 Variant _Geometry::segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
1316
1317 Vector3 res;
1318 if (Geometry::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res))
1319 return res;
1320 else
1321 return Variant();
1322 }
1323
point_is_inside_triangle(const Vector2 & s,const Vector2 & a,const Vector2 & b,const Vector2 & c) const1324 bool _Geometry::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
1325
1326 return Geometry::is_point_in_triangle(s, a, b, c);
1327 }
1328
segment_intersects_sphere(const Vector3 & p_from,const Vector3 & p_to,const Vector3 & p_sphere_pos,real_t p_sphere_radius)1329 DVector<Vector3> _Geometry::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
1330
1331 DVector<Vector3> r;
1332 Vector3 res, norm;
1333 if (!Geometry::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm))
1334 return r;
1335
1336 r.resize(2);
1337 r.set(0, res);
1338 r.set(1, norm);
1339 return r;
1340 }
segment_intersects_cylinder(const Vector3 & p_from,const Vector3 & p_to,float p_height,float p_radius)1341 DVector<Vector3> _Geometry::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
1342
1343 DVector<Vector3> r;
1344 Vector3 res, norm;
1345 if (!Geometry::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm))
1346 return r;
1347
1348 r.resize(2);
1349 r.set(0, res);
1350 r.set(1, norm);
1351 return r;
1352 }
segment_intersects_convex(const Vector3 & p_from,const Vector3 & p_to,const Vector<Plane> & p_planes)1353 DVector<Vector3> _Geometry::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
1354
1355 DVector<Vector3> r;
1356 Vector3 res, norm;
1357 if (!Geometry::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm))
1358 return r;
1359
1360 r.resize(2);
1361 r.set(0, res);
1362 r.set(1, norm);
1363 return r;
1364 }
1365
triangulate_polygon(const Vector<Vector2> & p_polygon)1366 Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2> &p_polygon) {
1367
1368 return Geometry::triangulate_polygon(p_polygon);
1369 }
1370
make_atlas(const Vector<Size2> & p_rects)1371 Dictionary _Geometry::make_atlas(const Vector<Size2> &p_rects) {
1372
1373 Dictionary ret;
1374
1375 Vector<Size2i> rects;
1376 for (int i = 0; i < p_rects.size(); i++) {
1377
1378 rects.push_back(p_rects[i]);
1379 };
1380
1381 Vector<Point2i> result;
1382 Size2i size;
1383
1384 Geometry::make_atlas(rects, result, size);
1385
1386 Size2 r_size = size;
1387 Vector<Point2> r_result;
1388 for (int i = 0; i < result.size(); i++) {
1389
1390 r_result.push_back(result[i]);
1391 };
1392
1393 ret["points"] = r_result;
1394 ret["size"] = r_size;
1395
1396 return ret;
1397 };
1398
get_uv84_normal_bit(const Vector3 & p_vector)1399 int _Geometry::get_uv84_normal_bit(const Vector3 &p_vector) {
1400
1401 return Geometry::get_uv84_normal_bit(p_vector);
1402 }
1403
_bind_methods()1404 void _Geometry::_bind_methods() {
1405
1406 ObjectTypeDB::bind_method(_MD("build_box_planes", "extents"), &_Geometry::build_box_planes);
1407 ObjectTypeDB::bind_method(_MD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
1408 ObjectTypeDB::bind_method(_MD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
1409 ObjectTypeDB::bind_method(_MD("segment_intersects_circle", "segment_from", "segment_to", "circle_pos", "circle_radius"), &_Geometry::segment_intersects_circle);
1410 ObjectTypeDB::bind_method(_MD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d);
1411
1412 ObjectTypeDB::bind_method(_MD("get_closest_points_between_segments_2d", "p1", "q1", "p2", "q2"), &_Geometry::get_closest_points_between_segments_2d);
1413 ObjectTypeDB::bind_method(_MD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry::get_closest_points_between_segments);
1414
1415 ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_2d);
1416 ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment);
1417
1418 ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment_uncapped_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped_2d);
1419 ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped);
1420
1421 ObjectTypeDB::bind_method(_MD("get_uv84_normal_bit", "normal"), &_Geometry::get_uv84_normal_bit);
1422
1423 ObjectTypeDB::bind_method(_MD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry::ray_intersects_triangle);
1424 ObjectTypeDB::bind_method(_MD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry::segment_intersects_triangle);
1425 ObjectTypeDB::bind_method(_MD("segment_intersects_sphere", "from", "to", "spos", "sradius"), &_Geometry::segment_intersects_sphere);
1426 ObjectTypeDB::bind_method(_MD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry::segment_intersects_cylinder);
1427 ObjectTypeDB::bind_method(_MD("segment_intersects_convex", "from", "to", "planes"), &_Geometry::segment_intersects_convex);
1428 ObjectTypeDB::bind_method(_MD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle);
1429
1430 ObjectTypeDB::bind_method(_MD("triangulate_polygon", "polygon"), &_Geometry::triangulate_polygon);
1431
1432 ObjectTypeDB::bind_method(_MD("make_atlas", "sizes"), &_Geometry::make_atlas);
1433 }
1434
_Geometry()1435 _Geometry::_Geometry() {
1436 singleton = this;
1437 }
1438
1439 ///////////////////////// FILE
1440
open_encrypted(const String & p_path,int p_mode_flags,const Vector<uint8_t> & p_key)1441 Error _File::open_encrypted(const String &p_path, int p_mode_flags, const Vector<uint8_t> &p_key) {
1442
1443 Error err = open(p_path, p_mode_flags);
1444 if (err)
1445 return err;
1446
1447 FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
1448 err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
1449 if (err) {
1450 memdelete(fae);
1451 close();
1452 return err;
1453 }
1454 f = fae;
1455 return OK;
1456 }
1457
open_encrypted_pass(const String & p_path,int p_mode_flags,const String & p_pass)1458 Error _File::open_encrypted_pass(const String &p_path, int p_mode_flags, const String &p_pass) {
1459
1460 Error err = open(p_path, p_mode_flags);
1461 if (err)
1462 return err;
1463
1464 FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
1465 err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
1466 if (err) {
1467 memdelete(fae);
1468 close();
1469 return err;
1470 }
1471
1472 f = fae;
1473 return OK;
1474 }
1475
open(const String & p_path,int p_mode_flags)1476 Error _File::open(const String &p_path, int p_mode_flags) {
1477
1478 close();
1479 Error err;
1480 f = FileAccess::open(p_path, p_mode_flags, &err);
1481 if (f)
1482 f->set_endian_swap(eswap);
1483 return err;
1484 }
1485
close()1486 void _File::close() {
1487
1488 if (f)
1489 memdelete(f);
1490 f = NULL;
1491 }
is_open() const1492 bool _File::is_open() const {
1493
1494 return f != NULL;
1495 }
1496
seek(int64_t p_position)1497 void _File::seek(int64_t p_position) {
1498
1499 ERR_FAIL_COND(!f);
1500 f->seek(p_position);
1501 }
seek_end(int64_t p_position)1502 void _File::seek_end(int64_t p_position) {
1503
1504 ERR_FAIL_COND(!f);
1505 f->seek_end(p_position);
1506 }
get_pos() const1507 int64_t _File::get_pos() const {
1508
1509 ERR_FAIL_COND_V(!f, 0);
1510 return f->get_pos();
1511 }
1512
get_len() const1513 int64_t _File::get_len() const {
1514
1515 ERR_FAIL_COND_V(!f, 0);
1516 return f->get_len();
1517 }
1518
eof_reached() const1519 bool _File::eof_reached() const {
1520
1521 ERR_FAIL_COND_V(!f, false);
1522 return f->eof_reached();
1523 }
1524
get_8() const1525 uint8_t _File::get_8() const {
1526
1527 ERR_FAIL_COND_V(!f, 0);
1528 return f->get_8();
1529 }
get_16() const1530 uint16_t _File::get_16() const {
1531
1532 ERR_FAIL_COND_V(!f, 0);
1533 return f->get_16();
1534 }
get_32() const1535 uint32_t _File::get_32() const {
1536
1537 ERR_FAIL_COND_V(!f, 0);
1538 return f->get_32();
1539 }
get_64() const1540 uint64_t _File::get_64() const {
1541
1542 ERR_FAIL_COND_V(!f, 0);
1543 return f->get_64();
1544 }
1545
get_float() const1546 float _File::get_float() const {
1547
1548 ERR_FAIL_COND_V(!f, 0);
1549 return f->get_float();
1550 }
get_double() const1551 double _File::get_double() const {
1552
1553 ERR_FAIL_COND_V(!f, 0);
1554 return f->get_double();
1555 }
get_real() const1556 real_t _File::get_real() const {
1557
1558 ERR_FAIL_COND_V(!f, 0);
1559 return f->get_real();
1560 }
1561
get_buffer(int p_length) const1562 DVector<uint8_t> _File::get_buffer(int p_length) const {
1563
1564 DVector<uint8_t> data;
1565 ERR_FAIL_COND_V(!f, data);
1566
1567 ERR_FAIL_COND_V(p_length < 0, data);
1568 if (p_length == 0)
1569 return data;
1570 Error err = data.resize(p_length);
1571 ERR_FAIL_COND_V(err != OK, data);
1572 DVector<uint8_t>::Write w = data.write();
1573 int len = f->get_buffer(&w[0], p_length);
1574 ERR_FAIL_COND_V(len < 0, DVector<uint8_t>());
1575
1576 w = DVector<uint8_t>::Write();
1577
1578 if (len < p_length)
1579 data.resize(p_length);
1580
1581 return data;
1582 }
1583
get_as_text() const1584 String _File::get_as_text() const {
1585
1586 ERR_FAIL_COND_V(!f, String());
1587
1588 String text;
1589 size_t original_pos = f->get_pos();
1590 f->seek(0);
1591
1592 String l = get_line();
1593 while (!eof_reached()) {
1594 text += l + "\n";
1595 l = get_line();
1596 }
1597 text += l;
1598
1599 f->seek(original_pos);
1600
1601 return text;
1602 }
1603
get_md5(const String & p_path) const1604 String _File::get_md5(const String &p_path) const {
1605
1606 return FileAccess::get_md5(p_path);
1607 }
1608
get_sha256(const String & p_path) const1609 String _File::get_sha256(const String &p_path) const {
1610
1611 return FileAccess::get_sha256(p_path);
1612 }
1613
get_line() const1614 String _File::get_line() const {
1615
1616 ERR_FAIL_COND_V(!f, String());
1617 return f->get_line();
1618 }
1619
get_csv_line(String delim) const1620 Vector<String> _File::get_csv_line(String delim) const {
1621 ERR_FAIL_COND_V(!f, Vector<String>());
1622 return f->get_csv_line(delim);
1623 }
1624
1625 /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
1626 * It's not about the current CPU type but file formats.
1627 * this flags get reset to false (little endian) on each open
1628 */
1629
set_endian_swap(bool p_swap)1630 void _File::set_endian_swap(bool p_swap) {
1631
1632 eswap = p_swap;
1633 if (f)
1634 f->set_endian_swap(p_swap);
1635 }
get_endian_swap()1636 bool _File::get_endian_swap() {
1637
1638 return eswap;
1639 }
1640
get_error() const1641 Error _File::get_error() const {
1642
1643 if (!f)
1644 return ERR_UNCONFIGURED;
1645 return f->get_error();
1646 }
1647
store_8(uint8_t p_dest)1648 void _File::store_8(uint8_t p_dest) {
1649
1650 ERR_FAIL_COND(!f);
1651
1652 f->store_8(p_dest);
1653 }
store_16(uint16_t p_dest)1654 void _File::store_16(uint16_t p_dest) {
1655
1656 ERR_FAIL_COND(!f);
1657
1658 f->store_16(p_dest);
1659 }
store_32(uint32_t p_dest)1660 void _File::store_32(uint32_t p_dest) {
1661
1662 ERR_FAIL_COND(!f);
1663
1664 f->store_32(p_dest);
1665 }
store_64(uint64_t p_dest)1666 void _File::store_64(uint64_t p_dest) {
1667
1668 ERR_FAIL_COND(!f);
1669
1670 f->store_64(p_dest);
1671 }
1672
store_float(float p_dest)1673 void _File::store_float(float p_dest) {
1674
1675 ERR_FAIL_COND(!f);
1676
1677 f->store_float(p_dest);
1678 }
store_double(double p_dest)1679 void _File::store_double(double p_dest) {
1680
1681 ERR_FAIL_COND(!f);
1682
1683 f->store_double(p_dest);
1684 }
store_real(real_t p_real)1685 void _File::store_real(real_t p_real) {
1686
1687 ERR_FAIL_COND(!f);
1688
1689 f->store_real(p_real);
1690 }
1691
store_string(const String & p_string)1692 void _File::store_string(const String &p_string) {
1693
1694 ERR_FAIL_COND(!f);
1695
1696 f->store_string(p_string);
1697 }
1698
store_pascal_string(const String & p_string)1699 void _File::store_pascal_string(const String &p_string) {
1700
1701 ERR_FAIL_COND(!f);
1702
1703 f->store_pascal_string(p_string);
1704 };
1705
get_pascal_string()1706 String _File::get_pascal_string() {
1707
1708 ERR_FAIL_COND_V(!f, "");
1709
1710 return f->get_pascal_string();
1711 };
1712
store_line(const String & p_string)1713 void _File::store_line(const String &p_string) {
1714
1715 ERR_FAIL_COND(!f);
1716 f->store_line(p_string);
1717 }
1718
store_buffer(const DVector<uint8_t> & p_buffer)1719 void _File::store_buffer(const DVector<uint8_t> &p_buffer) {
1720
1721 ERR_FAIL_COND(!f);
1722
1723 int len = p_buffer.size();
1724 if (len == 0)
1725 return;
1726
1727 DVector<uint8_t>::Read r = p_buffer.read();
1728
1729 f->store_buffer(&r[0], len);
1730 }
1731
file_exists(const String & p_name) const1732 bool _File::file_exists(const String &p_name) const {
1733
1734 return FileAccess::exists(p_name);
1735 }
1736
store_var(const Variant & p_var)1737 void _File::store_var(const Variant &p_var) {
1738
1739 ERR_FAIL_COND(!f);
1740 int len;
1741 Error err = encode_variant(p_var, NULL, len);
1742 ERR_FAIL_COND(err != OK);
1743
1744 DVector<uint8_t> buff;
1745 buff.resize(len);
1746 DVector<uint8_t>::Write w = buff.write();
1747
1748 err = encode_variant(p_var, &w[0], len);
1749 ERR_FAIL_COND(err != OK);
1750 w = DVector<uint8_t>::Write();
1751
1752 store_32(len);
1753 store_buffer(buff);
1754 }
1755
get_var() const1756 Variant _File::get_var() const {
1757
1758 ERR_FAIL_COND_V(!f, Variant());
1759 uint32_t len = get_32();
1760 DVector<uint8_t> buff = get_buffer(len);
1761 ERR_FAIL_COND_V(buff.size() != len, Variant());
1762
1763 DVector<uint8_t>::Read r = buff.read();
1764
1765 Variant v;
1766 Error err = decode_variant(v, &r[0], len);
1767 ERR_FAIL_COND_V(err != OK, Variant());
1768
1769 return v;
1770 }
1771
get_modified_time(const String & p_file) const1772 uint64_t _File::get_modified_time(const String &p_file) const {
1773
1774 return FileAccess::get_modified_time(p_file);
1775 }
1776
_bind_methods()1777 void _File::_bind_methods() {
1778
1779 ObjectTypeDB::bind_method(_MD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
1780 ObjectTypeDB::bind_method(_MD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
1781
1782 ObjectTypeDB::bind_method(_MD("open", "path", "flags"), &_File::open);
1783 ObjectTypeDB::bind_method(_MD("close"), &_File::close);
1784 ObjectTypeDB::bind_method(_MD("is_open"), &_File::is_open);
1785 ObjectTypeDB::bind_method(_MD("seek", "pos"), &_File::seek);
1786 ObjectTypeDB::bind_method(_MD("seek_end", "pos"), &_File::seek_end, DEFVAL(0));
1787 ObjectTypeDB::bind_method(_MD("get_pos"), &_File::get_pos);
1788 ObjectTypeDB::bind_method(_MD("get_len"), &_File::get_len);
1789 ObjectTypeDB::bind_method(_MD("eof_reached"), &_File::eof_reached);
1790 ObjectTypeDB::bind_method(_MD("get_8"), &_File::get_8);
1791 ObjectTypeDB::bind_method(_MD("get_16"), &_File::get_16);
1792 ObjectTypeDB::bind_method(_MD("get_32"), &_File::get_32);
1793 ObjectTypeDB::bind_method(_MD("get_64"), &_File::get_64);
1794 ObjectTypeDB::bind_method(_MD("get_float"), &_File::get_float);
1795 ObjectTypeDB::bind_method(_MD("get_double"), &_File::get_double);
1796 ObjectTypeDB::bind_method(_MD("get_real"), &_File::get_real);
1797 ObjectTypeDB::bind_method(_MD("get_buffer", "len"), &_File::get_buffer);
1798 ObjectTypeDB::bind_method(_MD("get_line"), &_File::get_line);
1799 ObjectTypeDB::bind_method(_MD("get_as_text"), &_File::get_as_text);
1800 ObjectTypeDB::bind_method(_MD("get_md5", "path"), &_File::get_md5);
1801 ObjectTypeDB::bind_method(_MD("get_sha256", "path"), &_File::get_sha256);
1802 ObjectTypeDB::bind_method(_MD("get_endian_swap"), &_File::get_endian_swap);
1803 ObjectTypeDB::bind_method(_MD("set_endian_swap", "enable"), &_File::set_endian_swap);
1804 ObjectTypeDB::bind_method(_MD("get_error:Error"), &_File::get_error);
1805 ObjectTypeDB::bind_method(_MD("get_var"), &_File::get_var);
1806 ObjectTypeDB::bind_method(_MD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
1807
1808 ObjectTypeDB::bind_method(_MD("store_8", "value"), &_File::store_8);
1809 ObjectTypeDB::bind_method(_MD("store_16", "value"), &_File::store_16);
1810 ObjectTypeDB::bind_method(_MD("store_32", "value"), &_File::store_32);
1811 ObjectTypeDB::bind_method(_MD("store_64", "value"), &_File::store_64);
1812 ObjectTypeDB::bind_method(_MD("store_float", "value"), &_File::store_float);
1813 ObjectTypeDB::bind_method(_MD("store_double", "value"), &_File::store_double);
1814 ObjectTypeDB::bind_method(_MD("store_real", "value"), &_File::store_real);
1815 ObjectTypeDB::bind_method(_MD("store_buffer", "buffer"), &_File::store_buffer);
1816 ObjectTypeDB::bind_method(_MD("store_line", "line"), &_File::store_line);
1817 ObjectTypeDB::bind_method(_MD("store_string", "string"), &_File::store_string);
1818 ObjectTypeDB::bind_method(_MD("store_var", "value"), &_File::store_var);
1819
1820 ObjectTypeDB::bind_method(_MD("store_pascal_string", "string"), &_File::store_pascal_string);
1821 ObjectTypeDB::bind_method(_MD("get_pascal_string"), &_File::get_pascal_string);
1822
1823 ObjectTypeDB::bind_method(_MD("file_exists", "path"), &_File::file_exists);
1824 ObjectTypeDB::bind_method(_MD("get_modified_time", "file"), &_File::get_modified_time);
1825
1826 BIND_CONSTANT(READ);
1827 BIND_CONSTANT(WRITE);
1828 BIND_CONSTANT(READ_WRITE);
1829 BIND_CONSTANT(WRITE_READ);
1830 }
1831
_File()1832 _File::_File() {
1833
1834 f = NULL;
1835 eswap = false;
1836 }
1837
~_File()1838 _File::~_File() {
1839
1840 if (f)
1841 memdelete(f);
1842 }
1843
1844 ///////////////////////////////////////////////////////
1845
open(const String & p_path)1846 Error _Directory::open(const String &p_path) {
1847 Error err;
1848 DirAccess *alt = DirAccess::open(p_path, &err);
1849
1850 if (!alt)
1851 return err;
1852 if (d)
1853 memdelete(d);
1854 d = alt;
1855
1856 return OK;
1857 }
1858
list_dir_begin()1859 bool _Directory::list_dir_begin() {
1860
1861 ERR_FAIL_COND_V(!d, false);
1862 return d->list_dir_begin();
1863 }
1864
get_next()1865 String _Directory::get_next() {
1866
1867 ERR_FAIL_COND_V(!d, "");
1868 return d->get_next();
1869 }
current_is_dir() const1870 bool _Directory::current_is_dir() const {
1871
1872 ERR_FAIL_COND_V(!d, false);
1873 return d->current_is_dir();
1874 }
1875
list_dir_end()1876 void _Directory::list_dir_end() {
1877
1878 ERR_FAIL_COND(!d);
1879 return d->list_dir_end();
1880 }
1881
get_drive_count()1882 int _Directory::get_drive_count() {
1883
1884 ERR_FAIL_COND_V(!d, 0);
1885 return d->get_drive_count();
1886 }
get_drive(int p_drive)1887 String _Directory::get_drive(int p_drive) {
1888
1889 ERR_FAIL_COND_V(!d, "");
1890 return d->get_drive(p_drive);
1891 }
1892
change_dir(String p_dir)1893 Error _Directory::change_dir(String p_dir) {
1894
1895 ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
1896 return d->change_dir(p_dir);
1897 }
get_current_dir()1898 String _Directory::get_current_dir() {
1899
1900 ERR_FAIL_COND_V(!d, "");
1901 return d->get_current_dir();
1902 }
make_dir(String p_dir)1903 Error _Directory::make_dir(String p_dir) {
1904
1905 ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
1906 if (!p_dir.is_rel_path()) {
1907 DirAccess *d = DirAccess::create_for_path(p_dir);
1908 Error err = d->make_dir(p_dir);
1909 memdelete(d);
1910 return err;
1911 }
1912 return d->make_dir(p_dir);
1913 }
make_dir_recursive(String p_dir)1914 Error _Directory::make_dir_recursive(String p_dir) {
1915
1916 ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
1917 if (!p_dir.is_rel_path()) {
1918 DirAccess *d = DirAccess::create_for_path(p_dir);
1919 Error err = d->make_dir_recursive(p_dir);
1920 memdelete(d);
1921 return err;
1922 }
1923 return d->make_dir_recursive(p_dir);
1924 }
1925
file_exists(String p_file)1926 bool _Directory::file_exists(String p_file) {
1927
1928 ERR_FAIL_COND_V(!d, false);
1929
1930 if (!p_file.is_rel_path()) {
1931 return FileAccess::exists(p_file);
1932 }
1933
1934 return d->file_exists(p_file);
1935 }
1936
dir_exists(String p_dir)1937 bool _Directory::dir_exists(String p_dir) {
1938 ERR_FAIL_COND_V(!d, false);
1939 if (!p_dir.is_rel_path()) {
1940
1941 DirAccess *d = DirAccess::create_for_path(p_dir);
1942 bool exists = d->dir_exists(p_dir);
1943 memdelete(d);
1944 return exists;
1945
1946 } else {
1947 return d->dir_exists(p_dir);
1948 }
1949 }
1950
get_space_left()1951 int _Directory::get_space_left() {
1952
1953 ERR_FAIL_COND_V(!d, 0);
1954 return d->get_space_left() / 1024 * 1024; //return value in megabytes, given binding is int
1955 }
1956
copy(String p_from,String p_to)1957 Error _Directory::copy(String p_from, String p_to) {
1958
1959 ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
1960 return d->copy(p_from, p_to);
1961 }
rename(String p_from,String p_to)1962 Error _Directory::rename(String p_from, String p_to) {
1963
1964 ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
1965 if (!p_from.is_rel_path()) {
1966 DirAccess *d = DirAccess::create_for_path(p_from);
1967 Error err = d->rename(p_from, p_to);
1968 memdelete(d);
1969 return err;
1970 }
1971
1972 return d->rename(p_from, p_to);
1973 }
remove(String p_name)1974 Error _Directory::remove(String p_name) {
1975
1976 ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
1977 if (!p_name.is_rel_path()) {
1978 DirAccess *d = DirAccess::create_for_path(p_name);
1979 Error err = d->remove(p_name);
1980 memdelete(d);
1981 return err;
1982 }
1983
1984 return d->remove(p_name);
1985 }
1986
_bind_methods()1987 void _Directory::_bind_methods() {
1988
1989 ObjectTypeDB::bind_method(_MD("open:Error", "path"), &_Directory::open);
1990 ObjectTypeDB::bind_method(_MD("list_dir_begin"), &_Directory::list_dir_begin);
1991 ObjectTypeDB::bind_method(_MD("get_next"), &_Directory::get_next);
1992 ObjectTypeDB::bind_method(_MD("current_is_dir"), &_Directory::current_is_dir);
1993 ObjectTypeDB::bind_method(_MD("list_dir_end"), &_Directory::list_dir_end);
1994 ObjectTypeDB::bind_method(_MD("get_drive_count"), &_Directory::get_drive_count);
1995 ObjectTypeDB::bind_method(_MD("get_drive", "idx"), &_Directory::get_drive);
1996 ObjectTypeDB::bind_method(_MD("change_dir:Error", "todir"), &_Directory::change_dir);
1997 ObjectTypeDB::bind_method(_MD("get_current_dir"), &_Directory::get_current_dir);
1998 ObjectTypeDB::bind_method(_MD("make_dir:Error", "path"), &_Directory::make_dir);
1999 ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error", "path"), &_Directory::make_dir_recursive);
2000 ObjectTypeDB::bind_method(_MD("file_exists", "path"), &_Directory::file_exists);
2001 ObjectTypeDB::bind_method(_MD("dir_exists", "path"), &_Directory::dir_exists);
2002 // ObjectTypeDB::bind_method(_MD("get_modified_time","file"),&_Directory::get_modified_time);
2003 ObjectTypeDB::bind_method(_MD("get_space_left"), &_Directory::get_space_left);
2004 ObjectTypeDB::bind_method(_MD("copy:Error", "from", "to"), &_Directory::copy);
2005 ObjectTypeDB::bind_method(_MD("rename:Error", "from", "to"), &_Directory::rename);
2006 ObjectTypeDB::bind_method(_MD("remove:Error", "path"), &_Directory::remove);
2007 }
2008
_Directory()2009 _Directory::_Directory() {
2010
2011 d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
2012 }
2013
~_Directory()2014 _Directory::~_Directory() {
2015
2016 if (d)
2017 memdelete(d);
2018 }
2019
variant_to_base64(const Variant & p_var)2020 String _Marshalls::variant_to_base64(const Variant &p_var) {
2021
2022 int len;
2023 Error err = encode_variant(p_var, NULL, len);
2024 ERR_FAIL_COND_V(err != OK, "");
2025
2026 DVector<uint8_t> buff;
2027 buff.resize(len);
2028 DVector<uint8_t>::Write w = buff.write();
2029
2030 err = encode_variant(p_var, &w[0], len);
2031 ERR_FAIL_COND_V(err != OK, "");
2032
2033 int b64len = len / 3 * 4 + 4 + 1;
2034 DVector<uint8_t> b64buff;
2035 b64buff.resize(b64len);
2036 DVector<uint8_t>::Write w64 = b64buff.write();
2037
2038 int strlen = base64_encode((char *)(&w64[0]), (char *)(&w[0]), len);
2039 //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen);
2040 w64[strlen] = 0;
2041 String ret = (char *)&w64[0];
2042
2043 return ret;
2044 };
2045
base64_to_variant(const String & p_str)2046 Variant _Marshalls::base64_to_variant(const String &p_str) {
2047
2048 int strlen = p_str.length();
2049 CharString cstr = p_str.ascii();
2050
2051 DVector<uint8_t> buf;
2052 buf.resize(strlen / 4 * 3 + 1);
2053 DVector<uint8_t>::Write w = buf.write();
2054
2055 int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
2056
2057 Variant v;
2058 Error err = decode_variant(v, &w[0], len);
2059 ERR_FAIL_COND_V(err != OK, Variant());
2060
2061 return v;
2062 };
2063
raw_to_base64(const DVector<uint8_t> & p_arr)2064 String _Marshalls::raw_to_base64(const DVector<uint8_t> &p_arr) {
2065
2066 int len = p_arr.size();
2067 DVector<uint8_t>::Read r = p_arr.read();
2068
2069 int b64len = len / 3 * 4 + 4 + 1;
2070 DVector<uint8_t> b64buff;
2071 b64buff.resize(b64len);
2072 DVector<uint8_t>::Write w64 = b64buff.write();
2073
2074 int strlen = base64_encode((char *)(&w64[0]), (char *)(&r[0]), len);
2075 w64[strlen] = 0;
2076 String ret = (char *)&w64[0];
2077
2078 return ret;
2079 };
2080
base64_to_raw(const String & p_str)2081 DVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
2082
2083 int strlen = p_str.length();
2084 CharString cstr = p_str.ascii();
2085
2086 int arr_len;
2087 DVector<uint8_t> buf;
2088 {
2089 buf.resize(strlen / 4 * 3 + 1);
2090 DVector<uint8_t>::Write w = buf.write();
2091
2092 arr_len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
2093 };
2094 buf.resize(arr_len);
2095
2096 // conversion from DVector<uint8_t> to raw array?
2097 return buf;
2098 };
2099
utf8_to_base64(const String & p_str)2100 String _Marshalls::utf8_to_base64(const String &p_str) {
2101
2102 CharString cstr = p_str.utf8();
2103 int len = cstr.length();
2104
2105 int b64len = len / 3 * 4 + 4 + 1;
2106 DVector<uint8_t> b64buff;
2107 b64buff.resize(b64len);
2108 DVector<uint8_t>::Write w64 = b64buff.write();
2109
2110 int strlen = base64_encode((char *)(&w64[0]), (char *)cstr.get_data(), len);
2111
2112 w64[strlen] = 0;
2113 String ret = (char *)&w64[0];
2114
2115 return ret;
2116 };
2117
base64_to_utf8(const String & p_str)2118 String _Marshalls::base64_to_utf8(const String &p_str) {
2119
2120 int strlen = p_str.length();
2121 CharString cstr = p_str.ascii();
2122
2123 DVector<uint8_t> buf;
2124 buf.resize(strlen / 4 * 3 + 1 + 1);
2125 DVector<uint8_t>::Write w = buf.write();
2126
2127 int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
2128
2129 w[len] = 0;
2130 String ret = String::utf8((char *)&w[0]);
2131
2132 return ret;
2133 };
2134
_bind_methods()2135 void _Marshalls::_bind_methods() {
2136
2137 ObjectTypeDB::bind_method(_MD("variant_to_base64:String", "variant"), &_Marshalls::variant_to_base64);
2138 ObjectTypeDB::bind_method(_MD("base64_to_variant:Variant", "base64_str"), &_Marshalls::base64_to_variant);
2139
2140 ObjectTypeDB::bind_method(_MD("raw_to_base64:String", "array"), &_Marshalls::raw_to_base64);
2141 ObjectTypeDB::bind_method(_MD("base64_to_raw:RawArray", "base64_str"), &_Marshalls::base64_to_raw);
2142
2143 ObjectTypeDB::bind_method(_MD("utf8_to_base64:String", "utf8_str"), &_Marshalls::utf8_to_base64);
2144 ObjectTypeDB::bind_method(_MD("base64_to_utf8:String", "base64_str"), &_Marshalls::base64_to_utf8);
2145 };
2146
2147 ////////////////
2148
wait()2149 Error _Semaphore::wait() {
2150
2151 return semaphore->wait();
2152 }
2153
post()2154 Error _Semaphore::post() {
2155
2156 return semaphore->post();
2157 }
2158
_bind_methods()2159 void _Semaphore::_bind_methods() {
2160
2161 ObjectTypeDB::bind_method(_MD("wait:Error"), &_Semaphore::wait);
2162 ObjectTypeDB::bind_method(_MD("post:Error"), &_Semaphore::post);
2163 }
2164
_Semaphore()2165 _Semaphore::_Semaphore() {
2166
2167 semaphore = Semaphore::create();
2168 }
2169
~_Semaphore()2170 _Semaphore::~_Semaphore() {
2171
2172 memdelete(semaphore);
2173 }
2174
2175 ///////////////
2176
lock()2177 void _Mutex::lock() {
2178
2179 mutex->lock();
2180 }
2181
try_lock()2182 Error _Mutex::try_lock() {
2183
2184 return mutex->try_lock();
2185 }
2186
unlock()2187 void _Mutex::unlock() {
2188
2189 mutex->unlock();
2190 }
2191
_bind_methods()2192 void _Mutex::_bind_methods() {
2193
2194 ObjectTypeDB::bind_method(_MD("lock"), &_Mutex::lock);
2195 ObjectTypeDB::bind_method(_MD("try_lock:Error"), &_Mutex::try_lock);
2196 ObjectTypeDB::bind_method(_MD("unlock"), &_Mutex::unlock);
2197 }
2198
_Mutex()2199 _Mutex::_Mutex() {
2200
2201 mutex = Mutex::create();
2202 }
2203
~_Mutex()2204 _Mutex::~_Mutex() {
2205
2206 memdelete(mutex);
2207 }
2208
2209 ///////////////
2210
_start_func(void * ud)2211 void _Thread::_start_func(void *ud) {
2212
2213 Ref<_Thread> *tud = (Ref<_Thread> *)ud;
2214 Ref<_Thread> t = *tud;
2215 memdelete(tud);
2216 Variant::CallError ce;
2217 const Variant *arg[1] = { &t->userdata };
2218
2219 Thread::set_name(t->target_method);
2220
2221 t->ret = t->target_instance->call(t->target_method, arg, 1, ce);
2222 if (ce.error != Variant::CallError::CALL_OK) {
2223
2224 String reason;
2225 switch (ce.error) {
2226 case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
2227
2228 reason = "Invalid Argument #" + itos(ce.argument);
2229 } break;
2230 case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
2231
2232 reason = "Too Many Arguments";
2233 } break;
2234 case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
2235
2236 reason = "Too Many Arguments";
2237 } break;
2238 case Variant::CallError::CALL_ERROR_INVALID_METHOD: {
2239
2240 reason = "Method Not Found";
2241 } break;
2242 default: {}
2243 }
2244
2245 ERR_EXPLAIN("Could not call function '" + t->target_method.operator String() + "'' starting thread ID: " + t->get_id() + " Reason: " + reason);
2246 ERR_FAIL();
2247 }
2248 }
2249
start(Object * p_instance,const StringName & p_method,const Variant & p_userdata,int p_priority)2250 Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, int p_priority) {
2251
2252 ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
2253 ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
2254 ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
2255 ERR_FAIL_INDEX_V(p_priority, 3, ERR_INVALID_PARAMETER);
2256
2257 ret = Variant();
2258 target_method = p_method;
2259 target_instance = p_instance;
2260 userdata = p_userdata;
2261 active = true;
2262
2263 Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
2264
2265 Thread::Settings s;
2266 s.priority = (Thread::Priority)p_priority;
2267 thread = Thread::create(_start_func, ud, s);
2268 if (!thread) {
2269 active = false;
2270 target_method = StringName();
2271 target_instance = NULL;
2272 userdata = Variant();
2273 return ERR_CANT_CREATE;
2274 }
2275
2276 return OK;
2277 }
2278
get_id() const2279 String _Thread::get_id() const {
2280
2281 if (!thread)
2282 return String();
2283
2284 return itos(thread->get_ID());
2285 }
2286
is_active() const2287 bool _Thread::is_active() const {
2288
2289 return active;
2290 }
wait_to_finish()2291 Variant _Thread::wait_to_finish() {
2292
2293 ERR_FAIL_COND_V(!thread, Variant());
2294 ERR_FAIL_COND_V(!active, Variant());
2295 Thread::wait_to_finish(thread);
2296 Variant r = ret;
2297 active = false;
2298 target_method = StringName();
2299 target_instance = NULL;
2300 userdata = Variant();
2301 thread = NULL;
2302
2303 return r;
2304 }
2305
_bind_methods()2306 void _Thread::_bind_methods() {
2307
2308 ObjectTypeDB::bind_method(_MD("start:Error", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
2309 ObjectTypeDB::bind_method(_MD("get_id"), &_Thread::get_id);
2310 ObjectTypeDB::bind_method(_MD("is_active"), &_Thread::is_active);
2311 ObjectTypeDB::bind_method(_MD("wait_to_finish:Variant"), &_Thread::wait_to_finish);
2312
2313 BIND_CONSTANT(PRIORITY_LOW);
2314 BIND_CONSTANT(PRIORITY_NORMAL);
2315 BIND_CONSTANT(PRIORITY_HIGH);
2316 }
_Thread()2317 _Thread::_Thread() {
2318
2319 active = false;
2320 thread = NULL;
2321 target_instance = NULL;
2322 }
2323
~_Thread()2324 _Thread::~_Thread() {
2325
2326 if (active) {
2327 ERR_EXPLAIN("Reference to a Thread object object was lost while the thread is still running..");
2328 }
2329 ERR_FAIL_COND(active == true);
2330 }
2331