core_bind.cpp 116 KB

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