core_bind.cpp 120 KB

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