core_bind.cpp 115 KB

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