core_bind.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  1. /*************************************************************************/
  2. /* core_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "core/globals.h"
  32. #include "geometry.h"
  33. #include "io/file_access_encrypted.h"
  34. #include "io/marshalls.h"
  35. #include "os/keyboard.h"
  36. #include "os/os.h"
  37. #include "thirdparty/misc/base64.h"
  38. /**
  39. * Time constants borrowed from loc_time.h
  40. */
  41. #define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
  42. #define SECS_DAY (24L * 60L * 60L)
  43. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
  44. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  45. #define SECOND_KEY "second"
  46. #define MINUTE_KEY "minute"
  47. #define HOUR_KEY "hour"
  48. #define DAY_KEY "day"
  49. #define MONTH_KEY "month"
  50. #define YEAR_KEY "year"
  51. #define WEEKDAY_KEY "weekday"
  52. #define DST_KEY "dst"
  53. /// Table of number of days in each month (for regular year and leap year)
  54. static const unsigned int MONTH_DAYS_TABLE[2][12] = {
  55. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  56. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  57. };
  58. _ResourceLoader *_ResourceLoader::singleton = NULL;
  59. Ref<ResourceInteractiveLoader> _ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint) {
  60. return ResourceLoader::load_interactive(p_path, p_type_hint);
  61. }
  62. RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache) {
  63. Error err = OK;
  64. RES ret = ResourceLoader::load(p_path, p_type_hint, p_no_cache, &err);
  65. if (err != OK) {
  66. ERR_EXPLAIN("Error loading resource: '" + p_path + "'");
  67. ERR_FAIL_COND_V(err != OK, ret);
  68. }
  69. return ret;
  70. }
  71. DVector<String> _ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
  72. List<String> exts;
  73. ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
  74. DVector<String> ret;
  75. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  76. ret.push_back(E->get());
  77. }
  78. return ret;
  79. }
  80. void _ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  81. ResourceLoader::set_abort_on_missing_resources(p_abort);
  82. }
  83. StringArray _ResourceLoader::get_dependencies(const String &p_path) {
  84. List<String> deps;
  85. ResourceLoader::get_dependencies(p_path, &deps);
  86. StringArray ret;
  87. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  88. ret.push_back(E->get());
  89. }
  90. return ret;
  91. };
  92. bool _ResourceLoader::has(const String &p_path) {
  93. String local_path = Globals::get_singleton()->localize_path(p_path);
  94. return ResourceCache::has(local_path);
  95. };
  96. Ref<ResourceImportMetadata> _ResourceLoader::load_import_metadata(const String &p_path) {
  97. return ResourceLoader::load_import_metadata(p_path);
  98. }
  99. void _ResourceLoader::_bind_methods() {
  100. ObjectTypeDB::bind_method(_MD("load_interactive:ResourceInteractiveLoader", "path", "type_hint"), &_ResourceLoader::load_interactive, DEFVAL(""));
  101. ObjectTypeDB::bind_method(_MD("load:Resource", "path", "type_hint", "p_no_cache"), &_ResourceLoader::load, DEFVAL(""), DEFVAL(false));
  102. ObjectTypeDB::bind_method(_MD("load_import_metadata:ResourceImportMetadata", "path"), &_ResourceLoader::load_import_metadata);
  103. ObjectTypeDB::bind_method(_MD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type);
  104. ObjectTypeDB::bind_method(_MD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources);
  105. ObjectTypeDB::bind_method(_MD("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
  106. ObjectTypeDB::bind_method(_MD("has", "path"), &_ResourceLoader::has);
  107. }
  108. _ResourceLoader::_ResourceLoader() {
  109. singleton = this;
  110. }
  111. Error _ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  112. ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
  113. return ResourceSaver::save(p_path, p_resource, p_flags);
  114. }
  115. DVector<String> _ResourceSaver::get_recognized_extensions(const RES &p_resource) {
  116. ERR_FAIL_COND_V(p_resource.is_null(), DVector<String>());
  117. List<String> exts;
  118. ResourceSaver::get_recognized_extensions(p_resource, &exts);
  119. DVector<String> ret;
  120. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  121. ret.push_back(E->get());
  122. }
  123. return ret;
  124. }
  125. _ResourceSaver *_ResourceSaver::singleton = NULL;
  126. void _ResourceSaver::_bind_methods() {
  127. ObjectTypeDB::bind_method(_MD("save", "path", "resource:Resource", "flags"), &_ResourceSaver::save, DEFVAL(0));
  128. ObjectTypeDB::bind_method(_MD("get_recognized_extensions", "type"), &_ResourceSaver::get_recognized_extensions);
  129. BIND_CONSTANT(FLAG_RELATIVE_PATHS);
  130. BIND_CONSTANT(FLAG_BUNDLE_RESOURCES);
  131. BIND_CONSTANT(FLAG_CHANGE_PATH);
  132. BIND_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
  133. BIND_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
  134. BIND_CONSTANT(FLAG_COMPRESS);
  135. }
  136. _ResourceSaver::_ResourceSaver() {
  137. singleton = this;
  138. }
  139. /////////////////OS
  140. Point2 _OS::get_mouse_pos() const {
  141. return OS::get_singleton()->get_mouse_pos();
  142. }
  143. void _OS::set_window_title(const String &p_title) {
  144. OS::get_singleton()->set_window_title(p_title);
  145. }
  146. int _OS::get_mouse_button_state() const {
  147. return OS::get_singleton()->get_mouse_button_state();
  148. }
  149. String _OS::get_unique_ID() const {
  150. return OS::get_singleton()->get_unique_ID();
  151. }
  152. bool _OS::has_touchscreen_ui_hint() const {
  153. return OS::get_singleton()->has_touchscreen_ui_hint();
  154. }
  155. void _OS::set_clipboard(const String &p_text) {
  156. OS::get_singleton()->set_clipboard(p_text);
  157. }
  158. String _OS::get_clipboard() const {
  159. return OS::get_singleton()->get_clipboard();
  160. }
  161. void _OS::set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen) {
  162. OS::VideoMode vm;
  163. vm.width = p_size.width;
  164. vm.height = p_size.height;
  165. vm.fullscreen = p_fullscreen;
  166. vm.resizable = p_resizeable;
  167. OS::get_singleton()->set_video_mode(vm, p_screen);
  168. }
  169. Size2 _OS::get_video_mode(int p_screen) const {
  170. OS::VideoMode vm;
  171. vm = OS::get_singleton()->get_video_mode(p_screen);
  172. return Size2(vm.width, vm.height);
  173. }
  174. bool _OS::is_video_mode_fullscreen(int p_screen) const {
  175. OS::VideoMode vm;
  176. vm = OS::get_singleton()->get_video_mode(p_screen);
  177. return vm.fullscreen;
  178. }
  179. int _OS::get_screen_count() const {
  180. return OS::get_singleton()->get_screen_count();
  181. }
  182. int _OS::get_current_screen() const {
  183. return OS::get_singleton()->get_current_screen();
  184. }
  185. void _OS::set_current_screen(int p_screen) {
  186. OS::get_singleton()->set_current_screen(p_screen);
  187. }
  188. Point2 _OS::get_screen_position(int p_screen) const {
  189. return OS::get_singleton()->get_screen_position(p_screen);
  190. }
  191. Size2 _OS::get_screen_size(int p_screen) const {
  192. return OS::get_singleton()->get_screen_size(p_screen);
  193. }
  194. int _OS::get_screen_dpi(int p_screen) const {
  195. return OS::get_singleton()->get_screen_dpi(p_screen);
  196. }
  197. Point2 _OS::get_window_position() const {
  198. return OS::get_singleton()->get_window_position();
  199. }
  200. void _OS::set_window_position(const Point2 &p_position) {
  201. OS::get_singleton()->set_window_position(p_position);
  202. }
  203. Size2 _OS::get_window_size() const {
  204. return OS::get_singleton()->get_window_size();
  205. }
  206. Size2 _OS::get_real_window_size() const {
  207. return OS::get_singleton()->get_real_window_size();
  208. }
  209. void _OS::set_window_size(const Size2 &p_size) {
  210. OS::get_singleton()->set_window_size(p_size);
  211. }
  212. void _OS::set_window_fullscreen(bool p_enabled) {
  213. OS::get_singleton()->set_window_fullscreen(p_enabled);
  214. }
  215. bool _OS::is_window_fullscreen() const {
  216. return OS::get_singleton()->is_window_fullscreen();
  217. }
  218. void _OS::set_window_resizable(bool p_enabled) {
  219. OS::get_singleton()->set_window_resizable(p_enabled);
  220. }
  221. bool _OS::is_window_resizable() const {
  222. return OS::get_singleton()->is_window_resizable();
  223. }
  224. void _OS::set_window_minimized(bool p_enabled) {
  225. OS::get_singleton()->set_window_minimized(p_enabled);
  226. }
  227. bool _OS::is_window_minimized() const {
  228. return OS::get_singleton()->is_window_minimized();
  229. }
  230. void _OS::set_window_maximized(bool p_enabled) {
  231. OS::get_singleton()->set_window_maximized(p_enabled);
  232. }
  233. bool _OS::is_window_maximized() const {
  234. return OS::get_singleton()->is_window_maximized();
  235. }
  236. void _OS::set_window_always_on_top(bool p_enabled) {
  237. OS::get_singleton()->set_window_always_on_top(p_enabled);
  238. }
  239. bool _OS::is_window_always_on_top() const {
  240. return OS::get_singleton()->is_window_always_on_top();
  241. }
  242. void _OS::set_borderless_window(bool p_borderless) {
  243. OS::get_singleton()->set_borderless_window(p_borderless);
  244. }
  245. bool _OS::get_borderless_window() const {
  246. return OS::get_singleton()->get_borderless_window();
  247. }
  248. void _OS::set_use_file_access_save_and_swap(bool p_enable) {
  249. FileAccess::set_backup_save(p_enable);
  250. }
  251. bool _OS::is_video_mode_resizable(int p_screen) const {
  252. OS::VideoMode vm;
  253. vm = OS::get_singleton()->get_video_mode(p_screen);
  254. return vm.resizable;
  255. }
  256. Array _OS::get_fullscreen_mode_list(int p_screen) const {
  257. List<OS::VideoMode> vmlist;
  258. OS::get_singleton()->get_fullscreen_mode_list(&vmlist, p_screen);
  259. Array vmarr;
  260. for (List<OS::VideoMode>::Element *E = vmlist.front(); E; E = E->next()) {
  261. vmarr.push_back(Size2(E->get().width, E->get().height));
  262. }
  263. return vmarr;
  264. }
  265. void _OS::set_iterations_per_second(int p_ips) {
  266. OS::get_singleton()->set_iterations_per_second(p_ips);
  267. }
  268. int _OS::get_iterations_per_second() const {
  269. return OS::get_singleton()->get_iterations_per_second();
  270. }
  271. void _OS::set_target_fps(int p_fps) {
  272. OS::get_singleton()->set_target_fps(p_fps);
  273. }
  274. float _OS::get_target_fps() const {
  275. return OS::get_singleton()->get_target_fps();
  276. }
  277. void _OS::set_low_processor_usage_mode(bool p_enabled) {
  278. OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  279. }
  280. bool _OS::is_in_low_processor_usage_mode() const {
  281. return OS::get_singleton()->is_in_low_processor_usage_mode();
  282. }
  283. String _OS::get_executable_path() const {
  284. return OS::get_singleton()->get_executable_path();
  285. }
  286. Error _OS::shell_open(String p_uri) {
  287. return OS::get_singleton()->shell_open(p_uri);
  288. };
  289. int _OS::execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output) {
  290. OS::ProcessID pid;
  291. List<String> args;
  292. for (int i = 0; i < p_arguments.size(); i++)
  293. args.push_back(p_arguments[i]);
  294. String pipe;
  295. Error err = OS::get_singleton()->execute(p_path, args, p_blocking, &pid, &pipe);
  296. p_output.clear();
  297. p_output.push_back(pipe);
  298. if (err != OK)
  299. return -1;
  300. else
  301. return pid;
  302. }
  303. Error _OS::kill(int p_pid) {
  304. return OS::get_singleton()->kill(p_pid);
  305. }
  306. int _OS::get_process_ID() const {
  307. return OS::get_singleton()->get_process_ID();
  308. };
  309. bool _OS::has_environment(const String &p_var) const {
  310. return OS::get_singleton()->has_environment(p_var);
  311. }
  312. String _OS::get_environment(const String &p_var) const {
  313. return OS::get_singleton()->get_environment(p_var);
  314. }
  315. String _OS::get_name() const {
  316. return OS::get_singleton()->get_name();
  317. }
  318. Vector<String> _OS::get_cmdline_args() {
  319. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  320. Vector<String> cmdlinev;
  321. for (List<String>::Element *E = cmdline.front(); E; E = E->next()) {
  322. cmdlinev.push_back(E->get());
  323. }
  324. return cmdlinev;
  325. }
  326. String _OS::get_locale() const {
  327. return OS::get_singleton()->get_locale();
  328. }
  329. String _OS::get_latin_keyboard_variant() const {
  330. switch (OS::get_singleton()->get_latin_keyboard_variant()) {
  331. case OS::LATIN_KEYBOARD_QWERTY: return "QWERTY";
  332. case OS::LATIN_KEYBOARD_QWERTZ: return "QWERTZ";
  333. case OS::LATIN_KEYBOARD_AZERTY: return "AZERTY";
  334. case OS::LATIN_KEYBOARD_QZERTY: return "QZERTY";
  335. case OS::LATIN_KEYBOARD_DVORAK: return "DVORAK";
  336. case OS::LATIN_KEYBOARD_NEO: return "NEO";
  337. case OS::LATIN_KEYBOARD_COLEMAK: return "COLEMAK";
  338. default: return "ERROR";
  339. }
  340. }
  341. String _OS::get_model_name() const {
  342. return OS::get_singleton()->get_model_name();
  343. }
  344. MainLoop *_OS::get_main_loop() const {
  345. return OS::get_singleton()->get_main_loop();
  346. }
  347. void _OS::set_time_scale(float p_scale) {
  348. OS::get_singleton()->set_time_scale(p_scale);
  349. }
  350. float _OS::get_time_scale() {
  351. return OS::get_singleton()->get_time_scale();
  352. }
  353. bool _OS::is_ok_left_and_cancel_right() const {
  354. return OS::get_singleton()->get_swap_ok_cancel();
  355. }
  356. Error _OS::set_thread_name(const String &p_name) {
  357. return Thread::set_name(p_name);
  358. };
  359. void _OS::set_use_vsync(bool p_enable) {
  360. OS::get_singleton()->set_use_vsync(p_enable);
  361. }
  362. bool _OS::is_vsync_enabled() const {
  363. return OS::get_singleton()->is_vsync_enabled();
  364. }
  365. /*
  366. enum Weekday {
  367. DAY_SUNDAY,
  368. DAY_MONDAY,
  369. DAY_TUESDAY,
  370. DAY_WEDNESDAY,
  371. DAY_THURSDAY,
  372. DAY_FRIDAY,
  373. DAY_SATURDAY
  374. };
  375. enum Month {
  376. MONTH_JANUARY,
  377. MONTH_FEBRUARY,
  378. MONTH_MARCH,
  379. MONTH_APRIL,
  380. MONTH_MAY,
  381. MONTH_JUNE,
  382. MONTH_JULY,
  383. MONTH_AUGUST,
  384. MONTH_SEPTEMBER,
  385. MONTH_OCTOBER,
  386. MONTH_NOVEMBER,
  387. MONTH_DECEMBER
  388. };
  389. */
  390. /*
  391. struct Date {
  392. int year;
  393. Month month;
  394. int day;
  395. Weekday weekday;
  396. bool dst;
  397. };
  398. struct Time {
  399. int hour;
  400. int min;
  401. int sec;
  402. };
  403. */
  404. int _OS::get_static_memory_usage() const {
  405. return OS::get_singleton()->get_static_memory_usage();
  406. }
  407. int _OS::get_static_memory_peak_usage() const {
  408. return OS::get_singleton()->get_static_memory_peak_usage();
  409. }
  410. int _OS::get_dynamic_memory_usage() const {
  411. return OS::get_singleton()->get_dynamic_memory_usage();
  412. }
  413. void _OS::set_icon(const Image &p_icon) {
  414. OS::get_singleton()->set_icon(p_icon);
  415. }
  416. int _OS::get_exit_code() const {
  417. return OS::get_singleton()->get_exit_code();
  418. }
  419. void _OS::set_exit_code(int p_code) {
  420. OS::get_singleton()->set_exit_code(p_code);
  421. }
  422. /**
  423. * Get current datetime with consideration for utc and
  424. * dst
  425. */
  426. Dictionary _OS::get_datetime(bool utc) const {
  427. Dictionary dated = get_date(utc);
  428. Dictionary timed = get_time(utc);
  429. List<Variant> keys;
  430. timed.get_key_list(&keys);
  431. for (int i = 0; i < keys.size(); i++) {
  432. dated[keys[i]] = timed[keys[i]];
  433. }
  434. return dated;
  435. }
  436. Dictionary _OS::get_date(bool utc) const {
  437. OS::Date date = OS::get_singleton()->get_date(utc);
  438. Dictionary dated;
  439. dated[YEAR_KEY] = date.year;
  440. dated[MONTH_KEY] = date.month;
  441. dated[DAY_KEY] = date.day;
  442. dated[WEEKDAY_KEY] = date.weekday;
  443. dated[DST_KEY] = date.dst;
  444. return dated;
  445. }
  446. Dictionary _OS::get_time(bool utc) const {
  447. OS::Time time = OS::get_singleton()->get_time(utc);
  448. Dictionary timed;
  449. timed[HOUR_KEY] = time.hour;
  450. timed[MINUTE_KEY] = time.min;
  451. timed[SECOND_KEY] = time.sec;
  452. return timed;
  453. }
  454. /**
  455. * Get a epoch time value from a dictionary of time values
  456. * @p datetime must be populated with the following keys:
  457. * day, hour, minute, month, second, year. (dst is ignored).
  458. *
  459. * You can pass the output from
  460. * get_datetime_from_unix_time directly into this function
  461. *
  462. * @param datetime dictionary of date and time values to convert
  463. *
  464. * @return epoch calculated
  465. */
  466. uint64_t _OS::get_unix_time_from_datetime(Dictionary datetime) const {
  467. // Bunch of conversion constants
  468. static const unsigned int SECONDS_PER_MINUTE = 60;
  469. static const unsigned int MINUTES_PER_HOUR = 60;
  470. static const unsigned int HOURS_PER_DAY = 24;
  471. static const unsigned int SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
  472. static const unsigned int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
  473. // Get all time values from the dictionary, set to zero if it doesn't exist.
  474. // Risk incorrect calculation over throwing errors
  475. unsigned int second = ((datetime.has(SECOND_KEY)) ? static_cast<unsigned int>(datetime[SECOND_KEY]) : 0);
  476. unsigned int minute = ((datetime.has(MINUTE_KEY)) ? static_cast<unsigned int>(datetime[MINUTE_KEY]) : 0);
  477. unsigned int hour = ((datetime.has(HOUR_KEY)) ? static_cast<unsigned int>(datetime[HOUR_KEY]) : 0);
  478. unsigned int day = ((datetime.has(DAY_KEY)) ? static_cast<unsigned int>(datetime[DAY_KEY]) : 0);
  479. unsigned int month = ((datetime.has(MONTH_KEY)) ? static_cast<unsigned int>(datetime[MONTH_KEY]) - 1 : 0);
  480. unsigned int year = ((datetime.has(YEAR_KEY)) ? static_cast<unsigned int>(datetime[YEAR_KEY]) : 0);
  481. /// How many days come before each month (0-12)
  482. static const unsigned short int DAYS_PAST_THIS_YEAR_TABLE[2][13] = {
  483. /* Normal years. */
  484. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  485. /* Leap years. */
  486. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  487. };
  488. ERR_EXPLAIN("Invalid second value of: " + itos(second));
  489. ERR_FAIL_COND_V(second > 59, 0);
  490. ERR_EXPLAIN("Invalid minute value of: " + itos(minute));
  491. ERR_FAIL_COND_V(minute > 59, 0);
  492. ERR_EXPLAIN("Invalid hour value of: " + itos(hour));
  493. ERR_FAIL_COND_V(hour > 23, 0);
  494. ERR_EXPLAIN("Invalid month value of: " + itos(month + 1));
  495. ERR_FAIL_COND_V(month + 1 > 12, 0);
  496. // Do this check after month is tested as valid
  497. ERR_EXPLAIN("Invalid day value of: " + itos(day) + " which is larger than " + itos(MONTH_DAYS_TABLE[LEAPYEAR(year)][month]));
  498. ERR_FAIL_COND_V(day > MONTH_DAYS_TABLE[LEAPYEAR(year)][month], 0);
  499. // Calculate all the seconds from months past in this year
  500. uint64_t SECONDS_FROM_MONTHS_PAST_THIS_YEAR = DAYS_PAST_THIS_YEAR_TABLE[LEAPYEAR(year)][month] * SECONDS_PER_DAY;
  501. uint64_t SECONDS_FROM_YEARS_PAST = 0;
  502. for (unsigned int iyear = EPOCH_YR; iyear < year; iyear++) {
  503. SECONDS_FROM_YEARS_PAST += YEARSIZE(iyear) * SECONDS_PER_DAY;
  504. }
  505. uint64_t epoch =
  506. second +
  507. minute * SECONDS_PER_MINUTE +
  508. hour * SECONDS_PER_HOUR +
  509. // Subtract 1 from day, since the current day isn't over yet
  510. // and we cannot count all 24 hours.
  511. (day - 1) * SECONDS_PER_DAY +
  512. SECONDS_FROM_MONTHS_PAST_THIS_YEAR +
  513. SECONDS_FROM_YEARS_PAST;
  514. return epoch;
  515. }
  516. /**
  517. * Get a dictionary of time values when given epoch time
  518. *
  519. * Dictionary Time values will be a union if values from #get_time
  520. * and #get_date dictionaries (with the exception of dst =
  521. * day light standard time, as it cannot be determined from epoch)
  522. *
  523. * @param unix_time_val epoch time to convert
  524. *
  525. * @return dictionary of date and time values
  526. */
  527. Dictionary _OS::get_datetime_from_unix_time(uint64_t unix_time_val) const {
  528. // Just fail if unix time is negative (when interpreted as an int).
  529. // This means the user passed in a negative value by accident
  530. ERR_EXPLAIN("unix_time_val was really huge!" + itos(unix_time_val) + " You probably passed in a negative value!");
  531. ERR_FAIL_COND_V((int64_t)unix_time_val < 0, Dictionary());
  532. OS::Date date;
  533. OS::Time time;
  534. unsigned long dayclock, dayno;
  535. int year = EPOCH_YR;
  536. dayclock = (unsigned long)unix_time_val % SECS_DAY;
  537. dayno = (unsigned long)unix_time_val / SECS_DAY;
  538. time.sec = dayclock % 60;
  539. time.min = (dayclock % 3600) / 60;
  540. time.hour = dayclock / 3600;
  541. /* day 0 was a thursday */
  542. date.weekday = static_cast<OS::Weekday>((dayno + 4) % 7);
  543. while (dayno >= YEARSIZE(year)) {
  544. dayno -= YEARSIZE(year);
  545. year++;
  546. }
  547. date.year = year;
  548. size_t imonth = 0;
  549. while (dayno >= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth]) {
  550. dayno -= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth];
  551. imonth++;
  552. }
  553. /// Add 1 to month to make sure months are indexed starting at 1
  554. date.month = static_cast<OS::Month>(imonth + 1);
  555. date.day = dayno + 1;
  556. Dictionary timed;
  557. timed[HOUR_KEY] = time.hour;
  558. timed[MINUTE_KEY] = time.min;
  559. timed[SECOND_KEY] = time.sec;
  560. timed[YEAR_KEY] = date.year;
  561. timed[MONTH_KEY] = date.month;
  562. timed[DAY_KEY] = date.day;
  563. timed[WEEKDAY_KEY] = date.weekday;
  564. return timed;
  565. }
  566. Dictionary _OS::get_time_zone_info() const {
  567. OS::TimeZoneInfo info = OS::get_singleton()->get_time_zone_info();
  568. Dictionary infod;
  569. infod["bias"] = info.bias;
  570. infod["name"] = info.name;
  571. return infod;
  572. }
  573. uint64_t _OS::get_unix_time() const {
  574. return OS::get_singleton()->get_unix_time();
  575. }
  576. uint64_t _OS::get_system_time_secs() const {
  577. return OS::get_singleton()->get_system_time_secs();
  578. }
  579. void _OS::delay_usec(uint32_t p_usec) const {
  580. OS::get_singleton()->delay_usec(p_usec);
  581. }
  582. void _OS::delay_msec(uint32_t p_msec) const {
  583. OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  584. }
  585. uint32_t _OS::get_ticks_msec() const {
  586. return OS::get_singleton()->get_ticks_msec();
  587. }
  588. uint32_t _OS::get_splash_tick_msec() const {
  589. return OS::get_singleton()->get_splash_tick_msec();
  590. }
  591. bool _OS::can_use_threads() const {
  592. return OS::get_singleton()->can_use_threads();
  593. }
  594. bool _OS::can_draw() const {
  595. return OS::get_singleton()->can_draw();
  596. }
  597. int _OS::get_frames_drawn() {
  598. return OS::get_singleton()->get_frames_drawn();
  599. }
  600. int _OS::get_processor_count() const {
  601. return OS::get_singleton()->get_processor_count();
  602. }
  603. bool _OS::is_stdout_verbose() const {
  604. return OS::get_singleton()->is_stdout_verbose();
  605. }
  606. void _OS::dump_memory_to_file(const String &p_file) {
  607. OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
  608. }
  609. struct _OSCoreBindImg {
  610. String path;
  611. Size2 size;
  612. int fmt;
  613. ObjectID id;
  614. int vram;
  615. bool operator<(const _OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
  616. };
  617. void _OS::print_all_textures_by_size() {
  618. List<_OSCoreBindImg> imgs;
  619. int total = 0;
  620. {
  621. List<Ref<Resource> > rsrc;
  622. ResourceCache::get_cached_resources(&rsrc);
  623. for (List<Ref<Resource> >::Element *E = rsrc.front(); E; E = E->next()) {
  624. if (!E->get()->is_type("ImageTexture"))
  625. continue;
  626. Size2 size = E->get()->call("get_size");
  627. int fmt = E->get()->call("get_format");
  628. _OSCoreBindImg img;
  629. img.size = size;
  630. img.fmt = fmt;
  631. img.path = E->get()->get_path();
  632. img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt));
  633. img.id = E->get()->get_instance_ID();
  634. total += img.vram;
  635. imgs.push_back(img);
  636. }
  637. }
  638. imgs.sort();
  639. for (List<_OSCoreBindImg>::Element *E = imgs.front(); E; E = E->next()) {
  640. total -= E->get().vram;
  641. }
  642. }
  643. void _OS::print_resources_by_type(const Vector<String> &p_types) {
  644. Map<String, int> type_count;
  645. List<Ref<Resource> > resources;
  646. ResourceCache::get_cached_resources(&resources);
  647. List<Ref<Resource> > rsrc;
  648. ResourceCache::get_cached_resources(&rsrc);
  649. for (List<Ref<Resource> >::Element *E = rsrc.front(); E; E = E->next()) {
  650. Ref<Resource> r = E->get();
  651. bool found = false;
  652. for (int i = 0; i < p_types.size(); i++) {
  653. if (r->is_type(p_types[i]))
  654. found = true;
  655. }
  656. if (!found)
  657. continue;
  658. if (!type_count.has(r->get_type())) {
  659. type_count[r->get_type()] = 0;
  660. }
  661. type_count[r->get_type()]++;
  662. }
  663. };
  664. bool _OS::has_virtual_keyboard() const {
  665. return OS::get_singleton()->has_virtual_keyboard();
  666. }
  667. void _OS::show_virtual_keyboard(const String &p_existing_text) {
  668. OS::get_singleton()->show_virtual_keyboard(p_existing_text, Rect2());
  669. }
  670. void _OS::hide_virtual_keyboard() {
  671. OS::get_singleton()->hide_virtual_keyboard();
  672. }
  673. void _OS::print_all_resources(const String &p_to_file) {
  674. OS::get_singleton()->print_all_resources(p_to_file);
  675. }
  676. void _OS::print_resources_in_use(bool p_short) {
  677. OS::get_singleton()->print_resources_in_use(p_short);
  678. }
  679. void _OS::dump_resources_to_file(const String &p_file) {
  680. OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  681. }
  682. String _OS::get_data_dir() const {
  683. return OS::get_singleton()->get_data_dir();
  684. };
  685. float _OS::get_frames_per_second() const {
  686. return OS::get_singleton()->get_frames_per_second();
  687. }
  688. Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  689. return OS::get_singleton()->native_video_play(p_path, p_volume, p_audio_track, p_subtitle_track);
  690. };
  691. bool _OS::native_video_is_playing() {
  692. return OS::get_singleton()->native_video_is_playing();
  693. };
  694. void _OS::native_video_pause() {
  695. OS::get_singleton()->native_video_pause();
  696. };
  697. void _OS::native_video_unpause() {
  698. OS::get_singleton()->native_video_unpause();
  699. };
  700. void _OS::native_video_stop() {
  701. OS::get_singleton()->native_video_stop();
  702. };
  703. void _OS::request_attention() {
  704. OS::get_singleton()->request_attention();
  705. }
  706. void _OS::center_window() {
  707. OS::get_singleton()->center_window();
  708. }
  709. bool _OS::is_debug_build() const {
  710. #ifdef DEBUG_ENABLED
  711. return true;
  712. #else
  713. return false;
  714. #endif
  715. }
  716. void _OS::set_screen_orientation(ScreenOrientation p_orientation) {
  717. OS::get_singleton()->set_screen_orientation(OS::ScreenOrientation(p_orientation));
  718. }
  719. _OS::ScreenOrientation _OS::get_screen_orientation() const {
  720. return ScreenOrientation(OS::get_singleton()->get_screen_orientation());
  721. }
  722. void _OS::set_keep_screen_on(bool p_enabled) {
  723. OS::get_singleton()->set_keep_screen_on(p_enabled);
  724. }
  725. bool _OS::is_keep_screen_on() const {
  726. return OS::get_singleton()->is_keep_screen_on();
  727. }
  728. String _OS::get_system_dir(SystemDir p_dir) const {
  729. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
  730. }
  731. String _OS::get_custom_level() const {
  732. return OS::get_singleton()->get_custom_level();
  733. }
  734. String _OS::get_scancode_string(uint32_t p_code) const {
  735. return keycode_get_string(p_code);
  736. }
  737. bool _OS::is_scancode_unicode(uint32_t p_unicode) const {
  738. return keycode_has_unicode(p_unicode);
  739. }
  740. int _OS::find_scancode_from_string(const String &p_code) const {
  741. return find_keycode(p_code);
  742. }
  743. void _OS::alert(const String &p_alert, const String &p_title) {
  744. OS::get_singleton()->alert(p_alert, p_title);
  745. }
  746. Dictionary _OS::get_engine_version() const {
  747. return OS::get_singleton()->get_engine_version();
  748. }
  749. _OS *_OS::singleton = NULL;
  750. void _OS::_bind_methods() {
  751. //ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&_OS::get_mouse_pos);
  752. //ObjectTypeDB::bind_method(_MD("is_mouse_grab_enabled"),&_OS::is_mouse_grab_enabled);
  753. ObjectTypeDB::bind_method(_MD("set_clipboard", "clipboard"), &_OS::set_clipboard);
  754. ObjectTypeDB::bind_method(_MD("get_clipboard"), &_OS::get_clipboard);
  755. ObjectTypeDB::bind_method(_MD("set_video_mode", "size", "fullscreen", "resizable", "screen"), &_OS::set_video_mode, DEFVAL(0));
  756. ObjectTypeDB::bind_method(_MD("get_video_mode_size", "screen"), &_OS::get_video_mode, DEFVAL(0));
  757. ObjectTypeDB::bind_method(_MD("is_video_mode_fullscreen", "screen"), &_OS::is_video_mode_fullscreen, DEFVAL(0));
  758. ObjectTypeDB::bind_method(_MD("is_video_mode_resizable", "screen"), &_OS::is_video_mode_resizable, DEFVAL(0));
  759. ObjectTypeDB::bind_method(_MD("get_fullscreen_mode_list", "screen"), &_OS::get_fullscreen_mode_list, DEFVAL(0));
  760. ObjectTypeDB::bind_method(_MD("get_screen_count"), &_OS::get_screen_count);
  761. ObjectTypeDB::bind_method(_MD("get_current_screen"), &_OS::get_current_screen);
  762. ObjectTypeDB::bind_method(_MD("set_current_screen", "screen"), &_OS::set_current_screen);
  763. ObjectTypeDB::bind_method(_MD("get_screen_position", "screen"), &_OS::get_screen_position, DEFVAL(0));
  764. ObjectTypeDB::bind_method(_MD("get_screen_size", "screen"), &_OS::get_screen_size, DEFVAL(0));
  765. ObjectTypeDB::bind_method(_MD("get_screen_dpi", "screen"), &_OS::get_screen_dpi, DEFVAL(0));
  766. ObjectTypeDB::bind_method(_MD("get_window_position"), &_OS::get_window_position);
  767. ObjectTypeDB::bind_method(_MD("set_window_position", "position"), &_OS::set_window_position);
  768. ObjectTypeDB::bind_method(_MD("get_window_size"), &_OS::get_window_size);
  769. ObjectTypeDB::bind_method(_MD("set_window_size", "size"), &_OS::set_window_size);
  770. ObjectTypeDB::bind_method(_MD("set_window_fullscreen", "enabled"), &_OS::set_window_fullscreen);
  771. ObjectTypeDB::bind_method(_MD("is_window_fullscreen"), &_OS::is_window_fullscreen);
  772. ObjectTypeDB::bind_method(_MD("set_window_resizable", "enabled"), &_OS::set_window_resizable);
  773. ObjectTypeDB::bind_method(_MD("is_window_resizable"), &_OS::is_window_resizable);
  774. ObjectTypeDB::bind_method(_MD("set_window_minimized", "enabled"), &_OS::set_window_minimized);
  775. ObjectTypeDB::bind_method(_MD("is_window_minimized"), &_OS::is_window_minimized);
  776. ObjectTypeDB::bind_method(_MD("set_window_maximized", "enabled"), &_OS::set_window_maximized);
  777. ObjectTypeDB::bind_method(_MD("is_window_maximized"), &_OS::is_window_maximized);
  778. ObjectTypeDB::bind_method(_MD("set_window_always_on_top", "enabled"), &_OS::set_window_always_on_top);
  779. ObjectTypeDB::bind_method(_MD("is_window_always_on_top"), &_OS::is_window_always_on_top);
  780. ObjectTypeDB::bind_method(_MD("request_attention"), &_OS::request_attention);
  781. ObjectTypeDB::bind_method(_MD("get_real_window_size"), &_OS::get_real_window_size);
  782. ObjectTypeDB::bind_method(_MD("center_window"), &_OS::center_window);
  783. ObjectTypeDB::bind_method(_MD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
  784. ObjectTypeDB::bind_method(_MD("get_borderless_window"), &_OS::get_borderless_window);
  785. ObjectTypeDB::bind_method(_MD("set_screen_orientation", "orientation"), &_OS::set_screen_orientation);
  786. ObjectTypeDB::bind_method(_MD("get_screen_orientation"), &_OS::get_screen_orientation);
  787. ObjectTypeDB::bind_method(_MD("set_keep_screen_on", "enabled"), &_OS::set_keep_screen_on);
  788. ObjectTypeDB::bind_method(_MD("is_keep_screen_on"), &_OS::is_keep_screen_on);
  789. ObjectTypeDB::bind_method(_MD("set_iterations_per_second", "iterations_per_second"), &_OS::set_iterations_per_second);
  790. ObjectTypeDB::bind_method(_MD("get_iterations_per_second"), &_OS::get_iterations_per_second);
  791. ObjectTypeDB::bind_method(_MD("set_target_fps", "target_fps"), &_OS::set_target_fps);
  792. ObjectTypeDB::bind_method(_MD("get_target_fps"), &_OS::get_target_fps);
  793. ObjectTypeDB::bind_method(_MD("set_time_scale", "time_scale"), &_OS::set_time_scale);
  794. ObjectTypeDB::bind_method(_MD("get_time_scale"), &_OS::get_time_scale);
  795. ObjectTypeDB::bind_method(_MD("has_touchscreen_ui_hint"), &_OS::has_touchscreen_ui_hint);
  796. ObjectTypeDB::bind_method(_MD("set_window_title", "title"), &_OS::set_window_title);
  797. ObjectTypeDB::bind_method(_MD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
  798. ObjectTypeDB::bind_method(_MD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
  799. ObjectTypeDB::bind_method(_MD("get_processor_count"), &_OS::get_processor_count);
  800. ObjectTypeDB::bind_method(_MD("get_executable_path"), &_OS::get_executable_path);
  801. ObjectTypeDB::bind_method(_MD("execute", "path", "arguments", "blocking", "output"), &_OS::execute, DEFVAL(Array()));
  802. ObjectTypeDB::bind_method(_MD("kill", "pid"), &_OS::kill);
  803. ObjectTypeDB::bind_method(_MD("shell_open", "uri"), &_OS::shell_open);
  804. ObjectTypeDB::bind_method(_MD("get_process_ID"), &_OS::get_process_ID);
  805. ObjectTypeDB::bind_method(_MD("get_environment", "environment"), &_OS::get_environment);
  806. ObjectTypeDB::bind_method(_MD("has_environment", "environment"), &_OS::has_environment);
  807. ObjectTypeDB::bind_method(_MD("get_name"), &_OS::get_name);
  808. ObjectTypeDB::bind_method(_MD("get_cmdline_args"), &_OS::get_cmdline_args);
  809. ObjectTypeDB::bind_method(_MD("get_main_loop"), &_OS::get_main_loop);
  810. ObjectTypeDB::bind_method(_MD("get_datetime", "utc"), &_OS::get_datetime, DEFVAL(false));
  811. ObjectTypeDB::bind_method(_MD("get_date", "utc"), &_OS::get_date, DEFVAL(false));
  812. ObjectTypeDB::bind_method(_MD("get_time", "utc"), &_OS::get_time, DEFVAL(false));
  813. ObjectTypeDB::bind_method(_MD("get_time_zone_info"), &_OS::get_time_zone_info);
  814. ObjectTypeDB::bind_method(_MD("get_unix_time"), &_OS::get_unix_time);
  815. ObjectTypeDB::bind_method(_MD("get_datetime_from_unix_time", "unix_time_val"),
  816. &_OS::get_datetime_from_unix_time);
  817. ObjectTypeDB::bind_method(_MD("get_unix_time_from_datetime", "datetime"),
  818. &_OS::get_unix_time_from_datetime);
  819. ObjectTypeDB::bind_method(_MD("get_system_time_secs"), &_OS::get_system_time_secs);
  820. ObjectTypeDB::bind_method(_MD("set_icon", "icon"), &_OS::set_icon);
  821. ObjectTypeDB::bind_method(_MD("get_exit_code"), &_OS::get_exit_code);
  822. ObjectTypeDB::bind_method(_MD("set_exit_code", "code"), &_OS::set_exit_code);
  823. ObjectTypeDB::bind_method(_MD("delay_usec", "usec"), &_OS::delay_usec);
  824. ObjectTypeDB::bind_method(_MD("delay_msec", "msec"), &_OS::delay_msec);
  825. ObjectTypeDB::bind_method(_MD("get_ticks_msec"), &_OS::get_ticks_msec);
  826. ObjectTypeDB::bind_method(_MD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
  827. ObjectTypeDB::bind_method(_MD("get_locale"), &_OS::get_locale);
  828. ObjectTypeDB::bind_method(_MD("get_latin_keyboard_variant"), &_OS::get_latin_keyboard_variant);
  829. ObjectTypeDB::bind_method(_MD("get_model_name"), &_OS::get_model_name);
  830. ObjectTypeDB::bind_method(_MD("get_custom_level"), &_OS::get_custom_level);
  831. ObjectTypeDB::bind_method(_MD("can_draw"), &_OS::can_draw);
  832. ObjectTypeDB::bind_method(_MD("get_frames_drawn"), &_OS::get_frames_drawn);
  833. ObjectTypeDB::bind_method(_MD("is_stdout_verbose"), &_OS::is_stdout_verbose);
  834. ObjectTypeDB::bind_method(_MD("can_use_threads"), &_OS::can_use_threads);
  835. ObjectTypeDB::bind_method(_MD("is_debug_build"), &_OS::is_debug_build);
  836. //ObjectTypeDB::bind_method(_MD("get_mouse_button_state"),&_OS::get_mouse_button_state);
  837. ObjectTypeDB::bind_method(_MD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
  838. ObjectTypeDB::bind_method(_MD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
  839. ObjectTypeDB::bind_method(_MD("has_virtual_keyboard"), &_OS::has_virtual_keyboard);
  840. ObjectTypeDB::bind_method(_MD("show_virtual_keyboard", "existing_text"), &_OS::show_virtual_keyboard, DEFVAL(""));
  841. ObjectTypeDB::bind_method(_MD("hide_virtual_keyboard"), &_OS::hide_virtual_keyboard);
  842. ObjectTypeDB::bind_method(_MD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
  843. ObjectTypeDB::bind_method(_MD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
  844. ObjectTypeDB::bind_method(_MD("get_static_memory_usage"), &_OS::get_static_memory_usage);
  845. ObjectTypeDB::bind_method(_MD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
  846. ObjectTypeDB::bind_method(_MD("get_dynamic_memory_usage"), &_OS::get_dynamic_memory_usage);
  847. ObjectTypeDB::bind_method(_MD("get_data_dir"), &_OS::get_data_dir);
  848. ObjectTypeDB::bind_method(_MD("get_system_dir", "dir"), &_OS::get_system_dir);
  849. ObjectTypeDB::bind_method(_MD("get_unique_ID"), &_OS::get_unique_ID);
  850. ObjectTypeDB::bind_method(_MD("is_ok_left_and_cancel_right"), &_OS::is_ok_left_and_cancel_right);
  851. ObjectTypeDB::bind_method(_MD("get_frames_per_second"), &_OS::get_frames_per_second);
  852. ObjectTypeDB::bind_method(_MD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
  853. ObjectTypeDB::bind_method(_MD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
  854. ObjectTypeDB::bind_method(_MD("native_video_play", "path", "volume", "audio_track", "subtitle_track"), &_OS::native_video_play);
  855. ObjectTypeDB::bind_method(_MD("native_video_is_playing"), &_OS::native_video_is_playing);
  856. ObjectTypeDB::bind_method(_MD("native_video_stop"), &_OS::native_video_stop);
  857. ObjectTypeDB::bind_method(_MD("native_video_pause"), &_OS::native_video_pause);
  858. ObjectTypeDB::bind_method(_MD("native_video_unpause"), &_OS::native_video_unpause);
  859. ObjectTypeDB::bind_method(_MD("get_scancode_string", "code"), &_OS::get_scancode_string);
  860. ObjectTypeDB::bind_method(_MD("is_scancode_unicode", "code"), &_OS::is_scancode_unicode);
  861. ObjectTypeDB::bind_method(_MD("find_scancode_from_string", "string"), &_OS::find_scancode_from_string);
  862. ObjectTypeDB::bind_method(_MD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
  863. ObjectTypeDB::bind_method(_MD("alert", "text", "title"), &_OS::alert, DEFVAL("Alert!"));
  864. ObjectTypeDB::bind_method(_MD("set_thread_name", "name"), &_OS::set_thread_name);
  865. ObjectTypeDB::bind_method(_MD("set_use_vsync", "enable"), &_OS::set_use_vsync);
  866. ObjectTypeDB::bind_method(_MD("is_vsync_enabled"), &_OS::is_vsync_enabled);
  867. ObjectTypeDB::bind_method(_MD("get_engine_version"), &_OS::get_engine_version);
  868. BIND_CONSTANT(DAY_SUNDAY);
  869. BIND_CONSTANT(DAY_MONDAY);
  870. BIND_CONSTANT(DAY_TUESDAY);
  871. BIND_CONSTANT(DAY_WEDNESDAY);
  872. BIND_CONSTANT(DAY_THURSDAY);
  873. BIND_CONSTANT(DAY_FRIDAY);
  874. BIND_CONSTANT(DAY_SATURDAY);
  875. BIND_CONSTANT(MONTH_JANUARY);
  876. BIND_CONSTANT(MONTH_FEBRUARY);
  877. BIND_CONSTANT(MONTH_MARCH);
  878. BIND_CONSTANT(MONTH_APRIL);
  879. BIND_CONSTANT(MONTH_MAY);
  880. BIND_CONSTANT(MONTH_JUNE);
  881. BIND_CONSTANT(MONTH_JULY);
  882. BIND_CONSTANT(MONTH_AUGUST);
  883. BIND_CONSTANT(MONTH_SEPTEMBER);
  884. BIND_CONSTANT(MONTH_OCTOBER);
  885. BIND_CONSTANT(MONTH_NOVEMBER);
  886. BIND_CONSTANT(MONTH_DECEMBER);
  887. BIND_CONSTANT(SCREEN_ORIENTATION_LANDSCAPE);
  888. BIND_CONSTANT(SCREEN_ORIENTATION_PORTRAIT);
  889. BIND_CONSTANT(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
  890. BIND_CONSTANT(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
  891. BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  892. BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  893. BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR);
  894. BIND_CONSTANT(SYSTEM_DIR_DESKTOP);
  895. BIND_CONSTANT(SYSTEM_DIR_DCIM);
  896. BIND_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  897. BIND_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  898. BIND_CONSTANT(SYSTEM_DIR_MOVIES);
  899. BIND_CONSTANT(SYSTEM_DIR_MUSIC);
  900. BIND_CONSTANT(SYSTEM_DIR_PICTURES);
  901. BIND_CONSTANT(SYSTEM_DIR_RINGTONES);
  902. }
  903. _OS::_OS() {
  904. singleton = this;
  905. }
  906. ///////////////////// GEOMETRY
  907. _Geometry *_Geometry::singleton = NULL;
  908. _Geometry *_Geometry::get_singleton() {
  909. return singleton;
  910. }
  911. DVector<Plane> _Geometry::build_box_planes(const Vector3 &p_extents) {
  912. return Geometry::build_box_planes(p_extents);
  913. }
  914. DVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  915. return Geometry::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  916. }
  917. DVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  918. return Geometry::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  919. }
  920. real_t _Geometry::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  921. return Geometry::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  922. }
  923. 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) {
  924. Vector2 result;
  925. if (Geometry::segment_intersects_segment_2d(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  926. return result;
  927. } else {
  928. return Variant();
  929. };
  930. };
  931. DVector<Vector2> _Geometry::get_closest_points_between_segments_2d(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  932. Vector2 r1, r2;
  933. Geometry::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  934. DVector<Vector2> r;
  935. r.resize(2);
  936. r.set(0, r1);
  937. r.set(1, r2);
  938. return r;
  939. }
  940. DVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  941. Vector3 r1, r2;
  942. Geometry::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  943. DVector<Vector3> r;
  944. r.resize(2);
  945. r.set(0, r1);
  946. r.set(1, r2);
  947. return r;
  948. }
  949. Vector2 _Geometry::get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  950. Vector2 s[2] = { p_a, p_b };
  951. return Geometry::get_closest_point_to_segment_2d(p_point, s);
  952. }
  953. Vector3 _Geometry::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  954. Vector3 s[2] = { p_a, p_b };
  955. return Geometry::get_closest_point_to_segment(p_point, s);
  956. }
  957. Vector2 _Geometry::get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  958. Vector2 s[2] = { p_a, p_b };
  959. return Geometry::get_closest_point_to_segment_uncapped_2d(p_point, s);
  960. }
  961. Vector3 _Geometry::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  962. Vector3 s[2] = { p_a, p_b };
  963. return Geometry::get_closest_point_to_segment_uncapped(p_point, s);
  964. }
  965. 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) {
  966. Vector3 res;
  967. if (Geometry::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res))
  968. return res;
  969. else
  970. return Variant();
  971. }
  972. 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) {
  973. Vector3 res;
  974. if (Geometry::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res))
  975. return res;
  976. else
  977. return Variant();
  978. }
  979. bool _Geometry::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  980. return Geometry::is_point_in_triangle(s, a, b, c);
  981. }
  982. DVector<Vector3> _Geometry::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  983. DVector<Vector3> r;
  984. Vector3 res, norm;
  985. if (!Geometry::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm))
  986. return r;
  987. r.resize(2);
  988. r.set(0, res);
  989. r.set(1, norm);
  990. return r;
  991. }
  992. DVector<Vector3> _Geometry::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  993. DVector<Vector3> r;
  994. Vector3 res, norm;
  995. if (!Geometry::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm))
  996. return r;
  997. r.resize(2);
  998. r.set(0, res);
  999. r.set(1, norm);
  1000. return r;
  1001. }
  1002. DVector<Vector3> _Geometry::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  1003. DVector<Vector3> r;
  1004. Vector3 res, norm;
  1005. if (!Geometry::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm))
  1006. return r;
  1007. r.resize(2);
  1008. r.set(0, res);
  1009. r.set(1, norm);
  1010. return r;
  1011. }
  1012. Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  1013. return Geometry::triangulate_polygon(p_polygon);
  1014. }
  1015. Dictionary _Geometry::make_atlas(const Vector<Size2> &p_rects) {
  1016. Dictionary ret;
  1017. Vector<Size2i> rects;
  1018. for (int i = 0; i < p_rects.size(); i++) {
  1019. rects.push_back(p_rects[i]);
  1020. };
  1021. Vector<Point2i> result;
  1022. Size2i size;
  1023. Geometry::make_atlas(rects, result, size);
  1024. Size2 r_size = size;
  1025. Vector<Point2> r_result;
  1026. for (int i = 0; i < result.size(); i++) {
  1027. r_result.push_back(result[i]);
  1028. };
  1029. ret["points"] = r_result;
  1030. ret["size"] = r_size;
  1031. return ret;
  1032. };
  1033. int _Geometry::get_uv84_normal_bit(const Vector3 &p_vector) {
  1034. return Geometry::get_uv84_normal_bit(p_vector);
  1035. }
  1036. void _Geometry::_bind_methods() {
  1037. ObjectTypeDB::bind_method(_MD("build_box_planes", "extents"), &_Geometry::build_box_planes);
  1038. ObjectTypeDB::bind_method(_MD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  1039. ObjectTypeDB::bind_method(_MD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  1040. ObjectTypeDB::bind_method(_MD("segment_intersects_circle", "segment_from", "segment_to", "circle_pos", "circle_radius"), &_Geometry::segment_intersects_circle);
  1041. ObjectTypeDB::bind_method(_MD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d);
  1042. ObjectTypeDB::bind_method(_MD("get_closest_points_between_segments_2d", "p1", "q1", "p2", "q2"), &_Geometry::get_closest_points_between_segments_2d);
  1043. ObjectTypeDB::bind_method(_MD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry::get_closest_points_between_segments);
  1044. ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_2d);
  1045. ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment);
  1046. ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment_uncapped_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped_2d);
  1047. ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped);
  1048. ObjectTypeDB::bind_method(_MD("get_uv84_normal_bit", "normal"), &_Geometry::get_uv84_normal_bit);
  1049. ObjectTypeDB::bind_method(_MD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry::ray_intersects_triangle);
  1050. ObjectTypeDB::bind_method(_MD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry::segment_intersects_triangle);
  1051. ObjectTypeDB::bind_method(_MD("segment_intersects_sphere", "from", "to", "spos", "sradius"), &_Geometry::segment_intersects_sphere);
  1052. ObjectTypeDB::bind_method(_MD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry::segment_intersects_cylinder);
  1053. ObjectTypeDB::bind_method(_MD("segment_intersects_convex", "from", "to", "planes"), &_Geometry::segment_intersects_convex);
  1054. ObjectTypeDB::bind_method(_MD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle);
  1055. ObjectTypeDB::bind_method(_MD("triangulate_polygon", "polygon"), &_Geometry::triangulate_polygon);
  1056. ObjectTypeDB::bind_method(_MD("make_atlas", "sizes"), &_Geometry::make_atlas);
  1057. }
  1058. _Geometry::_Geometry() {
  1059. singleton = this;
  1060. }
  1061. ///////////////////////// FILE
  1062. Error _File::open_encrypted(const String &p_path, int p_mode_flags, const Vector<uint8_t> &p_key) {
  1063. Error err = open(p_path, p_mode_flags);
  1064. if (err)
  1065. return err;
  1066. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  1067. err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  1068. if (err) {
  1069. memdelete(fae);
  1070. close();
  1071. return err;
  1072. }
  1073. f = fae;
  1074. return OK;
  1075. }
  1076. Error _File::open_encrypted_pass(const String &p_path, int p_mode_flags, const String &p_pass) {
  1077. Error err = open(p_path, p_mode_flags);
  1078. if (err)
  1079. return err;
  1080. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  1081. err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  1082. if (err) {
  1083. memdelete(fae);
  1084. close();
  1085. return err;
  1086. }
  1087. f = fae;
  1088. return OK;
  1089. }
  1090. Error _File::open(const String &p_path, int p_mode_flags) {
  1091. close();
  1092. Error err;
  1093. f = FileAccess::open(p_path, p_mode_flags, &err);
  1094. if (f)
  1095. f->set_endian_swap(eswap);
  1096. return err;
  1097. }
  1098. void _File::close() {
  1099. if (f)
  1100. memdelete(f);
  1101. f = NULL;
  1102. }
  1103. bool _File::is_open() const {
  1104. return f != NULL;
  1105. }
  1106. void _File::seek(int64_t p_position) {
  1107. ERR_FAIL_COND(!f);
  1108. f->seek(p_position);
  1109. }
  1110. void _File::seek_end(int64_t p_position) {
  1111. ERR_FAIL_COND(!f);
  1112. f->seek_end(p_position);
  1113. }
  1114. int64_t _File::get_pos() const {
  1115. ERR_FAIL_COND_V(!f, 0);
  1116. return f->get_pos();
  1117. }
  1118. int64_t _File::get_len() const {
  1119. ERR_FAIL_COND_V(!f, 0);
  1120. return f->get_len();
  1121. }
  1122. bool _File::eof_reached() const {
  1123. ERR_FAIL_COND_V(!f, false);
  1124. return f->eof_reached();
  1125. }
  1126. uint8_t _File::get_8() const {
  1127. ERR_FAIL_COND_V(!f, 0);
  1128. return f->get_8();
  1129. }
  1130. uint16_t _File::get_16() const {
  1131. ERR_FAIL_COND_V(!f, 0);
  1132. return f->get_16();
  1133. }
  1134. uint32_t _File::get_32() const {
  1135. ERR_FAIL_COND_V(!f, 0);
  1136. return f->get_32();
  1137. }
  1138. uint64_t _File::get_64() const {
  1139. ERR_FAIL_COND_V(!f, 0);
  1140. return f->get_64();
  1141. }
  1142. float _File::get_float() const {
  1143. ERR_FAIL_COND_V(!f, 0);
  1144. return f->get_float();
  1145. }
  1146. double _File::get_double() const {
  1147. ERR_FAIL_COND_V(!f, 0);
  1148. return f->get_double();
  1149. }
  1150. real_t _File::get_real() const {
  1151. ERR_FAIL_COND_V(!f, 0);
  1152. return f->get_real();
  1153. }
  1154. DVector<uint8_t> _File::get_buffer(int p_length) const {
  1155. DVector<uint8_t> data;
  1156. ERR_FAIL_COND_V(!f, data);
  1157. ERR_FAIL_COND_V(p_length < 0, data);
  1158. if (p_length == 0)
  1159. return data;
  1160. Error err = data.resize(p_length);
  1161. ERR_FAIL_COND_V(err != OK, data);
  1162. DVector<uint8_t>::Write w = data.write();
  1163. int len = f->get_buffer(&w[0], p_length);
  1164. ERR_FAIL_COND_V(len < 0, DVector<uint8_t>());
  1165. w = DVector<uint8_t>::Write();
  1166. if (len < p_length)
  1167. data.resize(p_length);
  1168. return data;
  1169. }
  1170. String _File::get_as_text() const {
  1171. ERR_FAIL_COND_V(!f, String());
  1172. String text;
  1173. size_t original_pos = f->get_pos();
  1174. f->seek(0);
  1175. String l = get_line();
  1176. while (!eof_reached()) {
  1177. text += l + "\n";
  1178. l = get_line();
  1179. }
  1180. text += l;
  1181. f->seek(original_pos);
  1182. return text;
  1183. }
  1184. String _File::get_md5(const String &p_path) const {
  1185. return FileAccess::get_md5(p_path);
  1186. }
  1187. String _File::get_sha256(const String &p_path) const {
  1188. return FileAccess::get_sha256(p_path);
  1189. }
  1190. String _File::get_line() const {
  1191. ERR_FAIL_COND_V(!f, String());
  1192. return f->get_line();
  1193. }
  1194. Vector<String> _File::get_csv_line(String delim) const {
  1195. ERR_FAIL_COND_V(!f, Vector<String>());
  1196. return f->get_csv_line(delim);
  1197. }
  1198. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  1199. * It's not about the current CPU type but file formats.
  1200. * this flags get reset to false (little endian) on each open
  1201. */
  1202. void _File::set_endian_swap(bool p_swap) {
  1203. eswap = p_swap;
  1204. if (f)
  1205. f->set_endian_swap(p_swap);
  1206. }
  1207. bool _File::get_endian_swap() {
  1208. return eswap;
  1209. }
  1210. Error _File::get_error() const {
  1211. if (!f)
  1212. return ERR_UNCONFIGURED;
  1213. return f->get_error();
  1214. }
  1215. void _File::store_8(uint8_t p_dest) {
  1216. ERR_FAIL_COND(!f);
  1217. f->store_8(p_dest);
  1218. }
  1219. void _File::store_16(uint16_t p_dest) {
  1220. ERR_FAIL_COND(!f);
  1221. f->store_16(p_dest);
  1222. }
  1223. void _File::store_32(uint32_t p_dest) {
  1224. ERR_FAIL_COND(!f);
  1225. f->store_32(p_dest);
  1226. }
  1227. void _File::store_64(uint64_t p_dest) {
  1228. ERR_FAIL_COND(!f);
  1229. f->store_64(p_dest);
  1230. }
  1231. void _File::store_float(float p_dest) {
  1232. ERR_FAIL_COND(!f);
  1233. f->store_float(p_dest);
  1234. }
  1235. void _File::store_double(double p_dest) {
  1236. ERR_FAIL_COND(!f);
  1237. f->store_double(p_dest);
  1238. }
  1239. void _File::store_real(real_t p_real) {
  1240. ERR_FAIL_COND(!f);
  1241. f->store_real(p_real);
  1242. }
  1243. void _File::store_string(const String &p_string) {
  1244. ERR_FAIL_COND(!f);
  1245. f->store_string(p_string);
  1246. }
  1247. void _File::store_pascal_string(const String &p_string) {
  1248. ERR_FAIL_COND(!f);
  1249. f->store_pascal_string(p_string);
  1250. };
  1251. String _File::get_pascal_string() {
  1252. ERR_FAIL_COND_V(!f, "");
  1253. return f->get_pascal_string();
  1254. };
  1255. void _File::store_line(const String &p_string) {
  1256. ERR_FAIL_COND(!f);
  1257. f->store_line(p_string);
  1258. }
  1259. void _File::store_buffer(const DVector<uint8_t> &p_buffer) {
  1260. ERR_FAIL_COND(!f);
  1261. int len = p_buffer.size();
  1262. if (len == 0)
  1263. return;
  1264. DVector<uint8_t>::Read r = p_buffer.read();
  1265. f->store_buffer(&r[0], len);
  1266. }
  1267. bool _File::file_exists(const String &p_name) const {
  1268. return FileAccess::exists(p_name);
  1269. }
  1270. void _File::store_var(const Variant &p_var) {
  1271. ERR_FAIL_COND(!f);
  1272. int len;
  1273. Error err = encode_variant(p_var, NULL, len);
  1274. ERR_FAIL_COND(err != OK);
  1275. DVector<uint8_t> buff;
  1276. buff.resize(len);
  1277. DVector<uint8_t>::Write w = buff.write();
  1278. err = encode_variant(p_var, &w[0], len);
  1279. ERR_FAIL_COND(err != OK);
  1280. w = DVector<uint8_t>::Write();
  1281. store_32(len);
  1282. store_buffer(buff);
  1283. }
  1284. Variant _File::get_var() const {
  1285. ERR_FAIL_COND_V(!f, Variant());
  1286. uint32_t len = get_32();
  1287. DVector<uint8_t> buff = get_buffer(len);
  1288. ERR_FAIL_COND_V(buff.size() != len, Variant());
  1289. DVector<uint8_t>::Read r = buff.read();
  1290. Variant v;
  1291. Error err = decode_variant(v, &r[0], len);
  1292. ERR_FAIL_COND_V(err != OK, Variant());
  1293. return v;
  1294. }
  1295. uint64_t _File::get_modified_time(const String &p_file) const {
  1296. return FileAccess::get_modified_time(p_file);
  1297. }
  1298. void _File::_bind_methods() {
  1299. ObjectTypeDB::bind_method(_MD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
  1300. ObjectTypeDB::bind_method(_MD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
  1301. ObjectTypeDB::bind_method(_MD("open", "path", "flags"), &_File::open);
  1302. ObjectTypeDB::bind_method(_MD("close"), &_File::close);
  1303. ObjectTypeDB::bind_method(_MD("is_open"), &_File::is_open);
  1304. ObjectTypeDB::bind_method(_MD("seek", "pos"), &_File::seek);
  1305. ObjectTypeDB::bind_method(_MD("seek_end", "pos"), &_File::seek_end, DEFVAL(0));
  1306. ObjectTypeDB::bind_method(_MD("get_pos"), &_File::get_pos);
  1307. ObjectTypeDB::bind_method(_MD("get_len"), &_File::get_len);
  1308. ObjectTypeDB::bind_method(_MD("eof_reached"), &_File::eof_reached);
  1309. ObjectTypeDB::bind_method(_MD("get_8"), &_File::get_8);
  1310. ObjectTypeDB::bind_method(_MD("get_16"), &_File::get_16);
  1311. ObjectTypeDB::bind_method(_MD("get_32"), &_File::get_32);
  1312. ObjectTypeDB::bind_method(_MD("get_64"), &_File::get_64);
  1313. ObjectTypeDB::bind_method(_MD("get_float"), &_File::get_float);
  1314. ObjectTypeDB::bind_method(_MD("get_double"), &_File::get_double);
  1315. ObjectTypeDB::bind_method(_MD("get_real"), &_File::get_real);
  1316. ObjectTypeDB::bind_method(_MD("get_buffer", "len"), &_File::get_buffer);
  1317. ObjectTypeDB::bind_method(_MD("get_line"), &_File::get_line);
  1318. ObjectTypeDB::bind_method(_MD("get_as_text"), &_File::get_as_text);
  1319. ObjectTypeDB::bind_method(_MD("get_md5", "path"), &_File::get_md5);
  1320. ObjectTypeDB::bind_method(_MD("get_sha256", "path"), &_File::get_sha256);
  1321. ObjectTypeDB::bind_method(_MD("get_endian_swap"), &_File::get_endian_swap);
  1322. ObjectTypeDB::bind_method(_MD("set_endian_swap", "enable"), &_File::set_endian_swap);
  1323. ObjectTypeDB::bind_method(_MD("get_error:Error"), &_File::get_error);
  1324. ObjectTypeDB::bind_method(_MD("get_var"), &_File::get_var);
  1325. ObjectTypeDB::bind_method(_MD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
  1326. ObjectTypeDB::bind_method(_MD("store_8", "value"), &_File::store_8);
  1327. ObjectTypeDB::bind_method(_MD("store_16", "value"), &_File::store_16);
  1328. ObjectTypeDB::bind_method(_MD("store_32", "value"), &_File::store_32);
  1329. ObjectTypeDB::bind_method(_MD("store_64", "value"), &_File::store_64);
  1330. ObjectTypeDB::bind_method(_MD("store_float", "value"), &_File::store_float);
  1331. ObjectTypeDB::bind_method(_MD("store_double", "value"), &_File::store_double);
  1332. ObjectTypeDB::bind_method(_MD("store_real", "value"), &_File::store_real);
  1333. ObjectTypeDB::bind_method(_MD("store_buffer", "buffer"), &_File::store_buffer);
  1334. ObjectTypeDB::bind_method(_MD("store_line", "line"), &_File::store_line);
  1335. ObjectTypeDB::bind_method(_MD("store_string", "string"), &_File::store_string);
  1336. ObjectTypeDB::bind_method(_MD("store_var", "value"), &_File::store_var);
  1337. ObjectTypeDB::bind_method(_MD("store_pascal_string", "string"), &_File::store_pascal_string);
  1338. ObjectTypeDB::bind_method(_MD("get_pascal_string"), &_File::get_pascal_string);
  1339. ObjectTypeDB::bind_method(_MD("file_exists", "path"), &_File::file_exists);
  1340. ObjectTypeDB::bind_method(_MD("get_modified_time", "file"), &_File::get_modified_time);
  1341. BIND_CONSTANT(READ);
  1342. BIND_CONSTANT(WRITE);
  1343. BIND_CONSTANT(READ_WRITE);
  1344. BIND_CONSTANT(WRITE_READ);
  1345. }
  1346. _File::_File() {
  1347. f = NULL;
  1348. eswap = false;
  1349. }
  1350. _File::~_File() {
  1351. if (f)
  1352. memdelete(f);
  1353. }
  1354. ///////////////////////////////////////////////////////
  1355. Error _Directory::open(const String &p_path) {
  1356. Error err;
  1357. DirAccess *alt = DirAccess::open(p_path, &err);
  1358. if (!alt)
  1359. return err;
  1360. if (d)
  1361. memdelete(d);
  1362. d = alt;
  1363. return OK;
  1364. }
  1365. bool _Directory::list_dir_begin() {
  1366. ERR_FAIL_COND_V(!d, false);
  1367. return d->list_dir_begin();
  1368. }
  1369. String _Directory::get_next() {
  1370. ERR_FAIL_COND_V(!d, "");
  1371. return d->get_next();
  1372. }
  1373. bool _Directory::current_is_dir() const {
  1374. ERR_FAIL_COND_V(!d, false);
  1375. return d->current_is_dir();
  1376. }
  1377. void _Directory::list_dir_end() {
  1378. ERR_FAIL_COND(!d);
  1379. return d->list_dir_end();
  1380. }
  1381. int _Directory::get_drive_count() {
  1382. ERR_FAIL_COND_V(!d, 0);
  1383. return d->get_drive_count();
  1384. }
  1385. String _Directory::get_drive(int p_drive) {
  1386. ERR_FAIL_COND_V(!d, "");
  1387. return d->get_drive(p_drive);
  1388. }
  1389. Error _Directory::change_dir(String p_dir) {
  1390. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1391. return d->change_dir(p_dir);
  1392. }
  1393. String _Directory::get_current_dir() {
  1394. ERR_FAIL_COND_V(!d, "");
  1395. return d->get_current_dir();
  1396. }
  1397. Error _Directory::make_dir(String p_dir) {
  1398. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1399. if (!p_dir.is_rel_path()) {
  1400. DirAccess *d = DirAccess::create_for_path(p_dir);
  1401. Error err = d->make_dir(p_dir);
  1402. memdelete(d);
  1403. return err;
  1404. }
  1405. return d->make_dir(p_dir);
  1406. }
  1407. Error _Directory::make_dir_recursive(String p_dir) {
  1408. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1409. if (!p_dir.is_rel_path()) {
  1410. DirAccess *d = DirAccess::create_for_path(p_dir);
  1411. Error err = d->make_dir_recursive(p_dir);
  1412. memdelete(d);
  1413. return err;
  1414. }
  1415. return d->make_dir_recursive(p_dir);
  1416. }
  1417. bool _Directory::file_exists(String p_file) {
  1418. ERR_FAIL_COND_V(!d, false);
  1419. if (!p_file.is_rel_path()) {
  1420. return FileAccess::exists(p_file);
  1421. }
  1422. return d->file_exists(p_file);
  1423. }
  1424. bool _Directory::dir_exists(String p_dir) {
  1425. ERR_FAIL_COND_V(!d, false);
  1426. if (!p_dir.is_rel_path()) {
  1427. DirAccess *d = DirAccess::create_for_path(p_dir);
  1428. bool exists = d->dir_exists(p_dir);
  1429. memdelete(d);
  1430. return exists;
  1431. } else {
  1432. return d->dir_exists(p_dir);
  1433. }
  1434. }
  1435. int _Directory::get_space_left() {
  1436. ERR_FAIL_COND_V(!d, 0);
  1437. return d->get_space_left() / 1024 * 1024; //return value in megabytes, given binding is int
  1438. }
  1439. Error _Directory::copy(String p_from, String p_to) {
  1440. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1441. return d->copy(p_from, p_to);
  1442. }
  1443. Error _Directory::rename(String p_from, String p_to) {
  1444. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1445. if (!p_from.is_rel_path()) {
  1446. DirAccess *d = DirAccess::create_for_path(p_from);
  1447. Error err = d->rename(p_from, p_to);
  1448. memdelete(d);
  1449. return err;
  1450. }
  1451. return d->rename(p_from, p_to);
  1452. }
  1453. Error _Directory::remove(String p_name) {
  1454. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1455. if (!p_name.is_rel_path()) {
  1456. DirAccess *d = DirAccess::create_for_path(p_name);
  1457. Error err = d->remove(p_name);
  1458. memdelete(d);
  1459. return err;
  1460. }
  1461. return d->remove(p_name);
  1462. }
  1463. void _Directory::_bind_methods() {
  1464. ObjectTypeDB::bind_method(_MD("open:Error", "path"), &_Directory::open);
  1465. ObjectTypeDB::bind_method(_MD("list_dir_begin"), &_Directory::list_dir_begin);
  1466. ObjectTypeDB::bind_method(_MD("get_next"), &_Directory::get_next);
  1467. ObjectTypeDB::bind_method(_MD("current_is_dir"), &_Directory::current_is_dir);
  1468. ObjectTypeDB::bind_method(_MD("list_dir_end"), &_Directory::list_dir_end);
  1469. ObjectTypeDB::bind_method(_MD("get_drive_count"), &_Directory::get_drive_count);
  1470. ObjectTypeDB::bind_method(_MD("get_drive", "idx"), &_Directory::get_drive);
  1471. ObjectTypeDB::bind_method(_MD("change_dir:Error", "todir"), &_Directory::change_dir);
  1472. ObjectTypeDB::bind_method(_MD("get_current_dir"), &_Directory::get_current_dir);
  1473. ObjectTypeDB::bind_method(_MD("make_dir:Error", "path"), &_Directory::make_dir);
  1474. ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error", "path"), &_Directory::make_dir_recursive);
  1475. ObjectTypeDB::bind_method(_MD("file_exists", "path"), &_Directory::file_exists);
  1476. ObjectTypeDB::bind_method(_MD("dir_exists", "path"), &_Directory::dir_exists);
  1477. // ObjectTypeDB::bind_method(_MD("get_modified_time","file"),&_Directory::get_modified_time);
  1478. ObjectTypeDB::bind_method(_MD("get_space_left"), &_Directory::get_space_left);
  1479. ObjectTypeDB::bind_method(_MD("copy:Error", "from", "to"), &_Directory::copy);
  1480. ObjectTypeDB::bind_method(_MD("rename:Error", "from", "to"), &_Directory::rename);
  1481. ObjectTypeDB::bind_method(_MD("remove:Error", "path"), &_Directory::remove);
  1482. }
  1483. _Directory::_Directory() {
  1484. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1485. }
  1486. _Directory::~_Directory() {
  1487. if (d)
  1488. memdelete(d);
  1489. }
  1490. String _Marshalls::variant_to_base64(const Variant &p_var) {
  1491. int len;
  1492. Error err = encode_variant(p_var, NULL, len);
  1493. ERR_FAIL_COND_V(err != OK, "");
  1494. DVector<uint8_t> buff;
  1495. buff.resize(len);
  1496. DVector<uint8_t>::Write w = buff.write();
  1497. err = encode_variant(p_var, &w[0], len);
  1498. ERR_FAIL_COND_V(err != OK, "");
  1499. int b64len = len / 3 * 4 + 4 + 1;
  1500. DVector<uint8_t> b64buff;
  1501. b64buff.resize(b64len);
  1502. DVector<uint8_t>::Write w64 = b64buff.write();
  1503. int strlen = base64_encode((char *)(&w64[0]), (char *)(&w[0]), len);
  1504. //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen);
  1505. w64[strlen] = 0;
  1506. String ret = (char *)&w64[0];
  1507. return ret;
  1508. };
  1509. Variant _Marshalls::base64_to_variant(const String &p_str) {
  1510. int strlen = p_str.length();
  1511. CharString cstr = p_str.ascii();
  1512. DVector<uint8_t> buf;
  1513. buf.resize(strlen / 4 * 3 + 1);
  1514. DVector<uint8_t>::Write w = buf.write();
  1515. int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
  1516. Variant v;
  1517. Error err = decode_variant(v, &w[0], len);
  1518. ERR_FAIL_COND_V(err != OK, Variant());
  1519. return v;
  1520. };
  1521. String _Marshalls::raw_to_base64(const DVector<uint8_t> &p_arr) {
  1522. int len = p_arr.size();
  1523. DVector<uint8_t>::Read r = p_arr.read();
  1524. int b64len = len / 3 * 4 + 4 + 1;
  1525. DVector<uint8_t> b64buff;
  1526. b64buff.resize(b64len);
  1527. DVector<uint8_t>::Write w64 = b64buff.write();
  1528. int strlen = base64_encode((char *)(&w64[0]), (char *)(&r[0]), len);
  1529. w64[strlen] = 0;
  1530. String ret = (char *)&w64[0];
  1531. return ret;
  1532. };
  1533. DVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
  1534. int strlen = p_str.length();
  1535. CharString cstr = p_str.ascii();
  1536. int arr_len;
  1537. DVector<uint8_t> buf;
  1538. {
  1539. buf.resize(strlen / 4 * 3 + 1);
  1540. DVector<uint8_t>::Write w = buf.write();
  1541. arr_len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
  1542. };
  1543. buf.resize(arr_len);
  1544. // conversion from DVector<uint8_t> to raw array?
  1545. return buf;
  1546. };
  1547. String _Marshalls::utf8_to_base64(const String &p_str) {
  1548. CharString cstr = p_str.utf8();
  1549. int len = cstr.length();
  1550. int b64len = len / 3 * 4 + 4 + 1;
  1551. DVector<uint8_t> b64buff;
  1552. b64buff.resize(b64len);
  1553. DVector<uint8_t>::Write w64 = b64buff.write();
  1554. int strlen = base64_encode((char *)(&w64[0]), (char *)cstr.get_data(), len);
  1555. w64[strlen] = 0;
  1556. String ret = (char *)&w64[0];
  1557. return ret;
  1558. };
  1559. String _Marshalls::base64_to_utf8(const String &p_str) {
  1560. int strlen = p_str.length();
  1561. CharString cstr = p_str.ascii();
  1562. DVector<uint8_t> buf;
  1563. buf.resize(strlen / 4 * 3 + 1 + 1);
  1564. DVector<uint8_t>::Write w = buf.write();
  1565. int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
  1566. w[len] = 0;
  1567. String ret = String::utf8((char *)&w[0]);
  1568. return ret;
  1569. };
  1570. void _Marshalls::_bind_methods() {
  1571. ObjectTypeDB::bind_method(_MD("variant_to_base64:String", "variant"), &_Marshalls::variant_to_base64);
  1572. ObjectTypeDB::bind_method(_MD("base64_to_variant:Variant", "base64_str"), &_Marshalls::base64_to_variant);
  1573. ObjectTypeDB::bind_method(_MD("raw_to_base64:String", "array"), &_Marshalls::raw_to_base64);
  1574. ObjectTypeDB::bind_method(_MD("base64_to_raw:RawArray", "base64_str"), &_Marshalls::base64_to_raw);
  1575. ObjectTypeDB::bind_method(_MD("utf8_to_base64:String", "utf8_str"), &_Marshalls::utf8_to_base64);
  1576. ObjectTypeDB::bind_method(_MD("base64_to_utf8:String", "base64_str"), &_Marshalls::base64_to_utf8);
  1577. };
  1578. ////////////////
  1579. Error _Semaphore::wait() {
  1580. return semaphore->wait();
  1581. }
  1582. Error _Semaphore::post() {
  1583. return semaphore->post();
  1584. }
  1585. void _Semaphore::_bind_methods() {
  1586. ObjectTypeDB::bind_method(_MD("wait:Error"), &_Semaphore::wait);
  1587. ObjectTypeDB::bind_method(_MD("post:Error"), &_Semaphore::post);
  1588. }
  1589. _Semaphore::_Semaphore() {
  1590. semaphore = Semaphore::create();
  1591. }
  1592. _Semaphore::~_Semaphore() {
  1593. memdelete(semaphore);
  1594. }
  1595. ///////////////
  1596. void _Mutex::lock() {
  1597. mutex->lock();
  1598. }
  1599. Error _Mutex::try_lock() {
  1600. return mutex->try_lock();
  1601. }
  1602. void _Mutex::unlock() {
  1603. mutex->unlock();
  1604. }
  1605. void _Mutex::_bind_methods() {
  1606. ObjectTypeDB::bind_method(_MD("lock"), &_Mutex::lock);
  1607. ObjectTypeDB::bind_method(_MD("try_lock:Error"), &_Mutex::try_lock);
  1608. ObjectTypeDB::bind_method(_MD("unlock"), &_Mutex::unlock);
  1609. }
  1610. _Mutex::_Mutex() {
  1611. mutex = Mutex::create();
  1612. }
  1613. _Mutex::~_Mutex() {
  1614. memdelete(mutex);
  1615. }
  1616. ///////////////
  1617. void _Thread::_start_func(void *ud) {
  1618. Ref<_Thread> *tud = (Ref<_Thread> *)ud;
  1619. Ref<_Thread> t = *tud;
  1620. memdelete(tud);
  1621. Variant::CallError ce;
  1622. const Variant *arg[1] = { &t->userdata };
  1623. Thread::set_name(t->target_method);
  1624. t->ret = t->target_instance->call(t->target_method, arg, 1, ce);
  1625. if (ce.error != Variant::CallError::CALL_OK) {
  1626. String reason;
  1627. switch (ce.error) {
  1628. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1629. reason = "Invalid Argument #" + itos(ce.argument);
  1630. } break;
  1631. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1632. reason = "Too Many Arguments";
  1633. } break;
  1634. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1635. reason = "Too Many Arguments";
  1636. } break;
  1637. case Variant::CallError::CALL_ERROR_INVALID_METHOD: {
  1638. reason = "Method Not Found";
  1639. } break;
  1640. default: {
  1641. }
  1642. }
  1643. ERR_EXPLAIN("Could not call function '" + t->target_method.operator String() + "'' starting thread ID: " + t->get_id() + " Reason: " + reason);
  1644. ERR_FAIL();
  1645. }
  1646. }
  1647. Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, int p_priority) {
  1648. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  1649. ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
  1650. ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
  1651. ERR_FAIL_INDEX_V(p_priority, 3, ERR_INVALID_PARAMETER);
  1652. ret = Variant();
  1653. target_method = p_method;
  1654. target_instance = p_instance;
  1655. userdata = p_userdata;
  1656. active = true;
  1657. Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
  1658. Thread::Settings s;
  1659. s.priority = (Thread::Priority)p_priority;
  1660. thread = Thread::create(_start_func, ud, s);
  1661. if (!thread) {
  1662. active = false;
  1663. target_method = StringName();
  1664. target_instance = NULL;
  1665. userdata = Variant();
  1666. return ERR_CANT_CREATE;
  1667. }
  1668. return OK;
  1669. }
  1670. String _Thread::get_id() const {
  1671. if (!thread)
  1672. return String();
  1673. return itos(thread->get_ID());
  1674. }
  1675. bool _Thread::is_active() const {
  1676. return active;
  1677. }
  1678. Variant _Thread::wait_to_finish() {
  1679. ERR_FAIL_COND_V(!thread, Variant());
  1680. ERR_FAIL_COND_V(!active, Variant());
  1681. Thread::wait_to_finish(thread);
  1682. Variant r = ret;
  1683. active = false;
  1684. target_method = StringName();
  1685. target_instance = NULL;
  1686. userdata = Variant();
  1687. thread = NULL;
  1688. return r;
  1689. }
  1690. void _Thread::_bind_methods() {
  1691. ObjectTypeDB::bind_method(_MD("start:Error", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
  1692. ObjectTypeDB::bind_method(_MD("get_id"), &_Thread::get_id);
  1693. ObjectTypeDB::bind_method(_MD("is_active"), &_Thread::is_active);
  1694. ObjectTypeDB::bind_method(_MD("wait_to_finish:Variant"), &_Thread::wait_to_finish);
  1695. BIND_CONSTANT(PRIORITY_LOW);
  1696. BIND_CONSTANT(PRIORITY_NORMAL);
  1697. BIND_CONSTANT(PRIORITY_HIGH);
  1698. }
  1699. _Thread::_Thread() {
  1700. active = false;
  1701. thread = NULL;
  1702. target_instance = NULL;
  1703. }
  1704. _Thread::~_Thread() {
  1705. if (active) {
  1706. ERR_EXPLAIN("Reference to a Thread object object was lost while the thread is still running..");
  1707. }
  1708. ERR_FAIL_COND(active == true);
  1709. }