core_bind.cpp 110 KB

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