core_bind.cpp 114 KB

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