core_bind.cpp 102 KB

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