core_bind.cpp 101 KB

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