core_bind.cpp 75 KB

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