core_bind.cpp 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203
  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("Texture")) {
  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. if (imgs.size() == 0) {
  810. print_line("No textures seem used in this project.");
  811. } else {
  812. print_line("Textures currently in use, sorted by VRAM usage:\n"
  813. "Path - VRAM usage (Dimensions)");
  814. }
  815. for (List<_OSCoreBindImg>::Element *E = imgs.front(); E; E = E->next()) {
  816. print_line(vformat("%s - %s %s",
  817. E->get().path,
  818. String::humanize_size(E->get().vram),
  819. E->get().size));
  820. }
  821. print_line(vformat("Total VRAM usage: %s.", String::humanize_size(total)));
  822. }
  823. void _OS::print_resources_by_type(const Vector<String> &p_types) {
  824. ERR_FAIL_COND_MSG(p_types.size() == 0,
  825. "At least one type should be provided to print resources by type.");
  826. print_line(vformat("Resources currently in use for the following types: %s", p_types));
  827. Map<String, int> type_count;
  828. List<Ref<Resource>> resources;
  829. ResourceCache::get_cached_resources(&resources);
  830. for (List<Ref<Resource>>::Element *E = resources.front(); E; E = E->next()) {
  831. Ref<Resource> r = E->get();
  832. bool found = false;
  833. for (int i = 0; i < p_types.size(); i++) {
  834. if (r->is_class(p_types[i])) {
  835. found = true;
  836. }
  837. }
  838. if (!found) {
  839. continue;
  840. }
  841. if (!type_count.has(r->get_class())) {
  842. type_count[r->get_class()] = 0;
  843. }
  844. type_count[r->get_class()]++;
  845. print_line(vformat("%s: %s", r->get_class(), r->get_path()));
  846. List<String> metas;
  847. r->get_meta_list(&metas);
  848. for (List<String>::Element *F = metas.front(); F; F = F->next()) {
  849. print_line(vformat(" %s: %s", F->get(), r->get_meta(F->get())));
  850. }
  851. }
  852. for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
  853. print_line(vformat("%s count: %d", E->key(), E->get()));
  854. }
  855. }
  856. bool _OS::has_virtual_keyboard() const {
  857. return OS::get_singleton()->has_virtual_keyboard();
  858. }
  859. void _OS::show_virtual_keyboard(const String &p_existing_text, bool p_multiline) {
  860. OS::get_singleton()->show_virtual_keyboard(p_existing_text, Rect2(), p_multiline);
  861. }
  862. void _OS::hide_virtual_keyboard() {
  863. OS::get_singleton()->hide_virtual_keyboard();
  864. }
  865. int _OS::get_virtual_keyboard_height() {
  866. return OS::get_singleton()->get_virtual_keyboard_height();
  867. }
  868. void _OS::print_all_resources(const String &p_to_file) {
  869. OS::get_singleton()->print_all_resources(p_to_file);
  870. }
  871. void _OS::print_resources_in_use(bool p_short) {
  872. OS::get_singleton()->print_resources_in_use(p_short);
  873. }
  874. void _OS::dump_resources_to_file(const String &p_file) {
  875. OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  876. }
  877. String _OS::get_user_data_dir() const {
  878. return OS::get_singleton()->get_user_data_dir();
  879. };
  880. Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  881. return OS::get_singleton()->native_video_play(p_path, p_volume, p_audio_track, p_subtitle_track);
  882. };
  883. bool _OS::native_video_is_playing() {
  884. return OS::get_singleton()->native_video_is_playing();
  885. };
  886. void _OS::native_video_pause() {
  887. OS::get_singleton()->native_video_pause();
  888. };
  889. void _OS::native_video_unpause() {
  890. OS::get_singleton()->native_video_unpause();
  891. };
  892. void _OS::native_video_stop() {
  893. OS::get_singleton()->native_video_stop();
  894. };
  895. void _OS::request_attention() {
  896. OS::get_singleton()->request_attention();
  897. }
  898. void _OS::center_window() {
  899. OS::get_singleton()->center_window();
  900. }
  901. void _OS::move_window_to_foreground() {
  902. OS::get_singleton()->move_window_to_foreground();
  903. }
  904. int64_t _OS::get_native_handle(HandleType p_handle_type) {
  905. return (int64_t)OS::get_singleton()->get_native_handle(p_handle_type);
  906. }
  907. String _OS::get_config_dir() const {
  908. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  909. return OS::get_singleton()->get_config_path();
  910. }
  911. String _OS::get_data_dir() const {
  912. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  913. return OS::get_singleton()->get_data_path();
  914. }
  915. String _OS::get_cache_dir() const {
  916. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  917. return OS::get_singleton()->get_cache_path();
  918. }
  919. bool _OS::is_debug_build() const {
  920. #ifdef DEBUG_ENABLED
  921. return true;
  922. #else
  923. return false;
  924. #endif
  925. }
  926. void _OS::set_screen_orientation(ScreenOrientation p_orientation) {
  927. OS::get_singleton()->set_screen_orientation(OS::ScreenOrientation(p_orientation));
  928. }
  929. _OS::ScreenOrientation _OS::get_screen_orientation() const {
  930. return ScreenOrientation(OS::get_singleton()->get_screen_orientation());
  931. }
  932. void _OS::set_keep_screen_on(bool p_enabled) {
  933. OS::get_singleton()->set_keep_screen_on(p_enabled);
  934. }
  935. bool _OS::is_keep_screen_on() const {
  936. return OS::get_singleton()->is_keep_screen_on();
  937. }
  938. String _OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  939. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir), p_shared_storage);
  940. }
  941. String _OS::get_scancode_string(uint32_t p_code) const {
  942. return keycode_get_string(p_code);
  943. }
  944. bool _OS::is_scancode_unicode(uint32_t p_unicode) const {
  945. return keycode_has_unicode(p_unicode);
  946. }
  947. int _OS::find_scancode_from_string(const String &p_code) const {
  948. return find_keycode(p_code);
  949. }
  950. void _OS::alert(const String &p_alert, const String &p_title) {
  951. OS::get_singleton()->alert(p_alert, p_title);
  952. }
  953. bool _OS::request_permission(const String &p_name) {
  954. return OS::get_singleton()->request_permission(p_name);
  955. }
  956. bool _OS::request_permissions() {
  957. return OS::get_singleton()->request_permissions();
  958. }
  959. Vector<String> _OS::get_granted_permissions() const {
  960. return OS::get_singleton()->get_granted_permissions();
  961. }
  962. int _OS::get_tablet_driver_count() const {
  963. return OS::get_singleton()->get_tablet_driver_count();
  964. }
  965. String _OS::get_tablet_driver_name(int p_driver) const {
  966. return OS::get_singleton()->get_tablet_driver_name(p_driver);
  967. }
  968. String _OS::get_current_tablet_driver() const {
  969. return OS::get_singleton()->get_current_tablet_driver();
  970. }
  971. void _OS::set_current_tablet_driver(const String &p_driver) {
  972. OS::get_singleton()->set_current_tablet_driver(p_driver);
  973. }
  974. _OS *_OS::singleton = nullptr;
  975. void _OS::_bind_methods() {
  976. //ClassDB::bind_method(D_METHOD("get_mouse_position"),&_OS::get_mouse_position);
  977. //ClassDB::bind_method(D_METHOD("is_mouse_grab_enabled"),&_OS::is_mouse_grab_enabled);
  978. ClassDB::bind_method(D_METHOD("set_clipboard", "clipboard"), &_OS::set_clipboard);
  979. ClassDB::bind_method(D_METHOD("get_clipboard"), &_OS::get_clipboard);
  980. //will not delete for now, just unexpose
  981. //ClassDB::bind_method(D_METHOD("set_video_mode","size","fullscreen","resizable","screen"),&_OS::set_video_mode,DEFVAL(0));
  982. //ClassDB::bind_method(D_METHOD("get_video_mode_size","screen"),&_OS::get_video_mode,DEFVAL(0));
  983. //ClassDB::bind_method(D_METHOD("is_video_mode_fullscreen","screen"),&_OS::is_video_mode_fullscreen,DEFVAL(0));
  984. //ClassDB::bind_method(D_METHOD("is_video_mode_resizable","screen"),&_OS::is_video_mode_resizable,DEFVAL(0));
  985. //ClassDB::bind_method(D_METHOD("get_fullscreen_mode_list","screen"),&_OS::get_fullscreen_mode_list,DEFVAL(0));
  986. ClassDB::bind_method(D_METHOD("global_menu_add_item", "menu", "label", "id", "meta"), &_OS::global_menu_add_item);
  987. ClassDB::bind_method(D_METHOD("global_menu_add_separator", "menu"), &_OS::global_menu_add_separator);
  988. ClassDB::bind_method(D_METHOD("global_menu_remove_item", "menu", "idx"), &_OS::global_menu_remove_item);
  989. ClassDB::bind_method(D_METHOD("global_menu_clear", "menu"), &_OS::global_menu_clear);
  990. ClassDB::bind_method(D_METHOD("get_video_driver_count"), &_OS::get_video_driver_count);
  991. ClassDB::bind_method(D_METHOD("get_video_driver_name", "driver"), &_OS::get_video_driver_name);
  992. ClassDB::bind_method(D_METHOD("get_current_video_driver"), &_OS::get_current_video_driver);
  993. ClassDB::bind_method(D_METHOD("get_audio_driver_count"), &_OS::get_audio_driver_count);
  994. ClassDB::bind_method(D_METHOD("get_audio_driver_name", "driver"), &_OS::get_audio_driver_name);
  995. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &_OS::get_connected_midi_inputs);
  996. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &_OS::open_midi_inputs);
  997. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &_OS::close_midi_inputs);
  998. ClassDB::bind_method(D_METHOD("get_screen_count"), &_OS::get_screen_count);
  999. ClassDB::bind_method(D_METHOD("get_current_screen"), &_OS::get_current_screen);
  1000. ClassDB::bind_method(D_METHOD("set_current_screen", "screen"), &_OS::set_current_screen);
  1001. ClassDB::bind_method(D_METHOD("get_screen_position", "screen"), &_OS::get_screen_position, DEFVAL(-1));
  1002. ClassDB::bind_method(D_METHOD("get_screen_size", "screen"), &_OS::get_screen_size, DEFVAL(-1));
  1003. ClassDB::bind_method(D_METHOD("get_screen_dpi", "screen"), &_OS::get_screen_dpi, DEFVAL(-1));
  1004. ClassDB::bind_method(D_METHOD("get_screen_scale", "screen"), &_OS::get_screen_scale, DEFVAL(-1));
  1005. ClassDB::bind_method(D_METHOD("get_screen_max_scale"), &_OS::get_screen_max_scale);
  1006. ClassDB::bind_method(D_METHOD("get_window_position"), &_OS::get_window_position);
  1007. ClassDB::bind_method(D_METHOD("set_window_position", "position"), &_OS::set_window_position);
  1008. ClassDB::bind_method(D_METHOD("get_window_size"), &_OS::get_window_size);
  1009. ClassDB::bind_method(D_METHOD("get_max_window_size"), &_OS::get_max_window_size);
  1010. ClassDB::bind_method(D_METHOD("get_min_window_size"), &_OS::get_min_window_size);
  1011. ClassDB::bind_method(D_METHOD("set_max_window_size", "size"), &_OS::set_max_window_size);
  1012. ClassDB::bind_method(D_METHOD("set_min_window_size", "size"), &_OS::set_min_window_size);
  1013. ClassDB::bind_method(D_METHOD("set_window_size", "size"), &_OS::set_window_size);
  1014. ClassDB::bind_method(D_METHOD("get_window_safe_area"), &_OS::get_window_safe_area);
  1015. ClassDB::bind_method(D_METHOD("set_window_fullscreen", "enabled"), &_OS::set_window_fullscreen);
  1016. ClassDB::bind_method(D_METHOD("is_window_fullscreen"), &_OS::is_window_fullscreen);
  1017. ClassDB::bind_method(D_METHOD("set_window_resizable", "enabled"), &_OS::set_window_resizable);
  1018. ClassDB::bind_method(D_METHOD("is_window_resizable"), &_OS::is_window_resizable);
  1019. ClassDB::bind_method(D_METHOD("set_window_minimized", "enabled"), &_OS::set_window_minimized);
  1020. ClassDB::bind_method(D_METHOD("is_window_minimized"), &_OS::is_window_minimized);
  1021. ClassDB::bind_method(D_METHOD("set_window_maximized", "enabled"), &_OS::set_window_maximized);
  1022. ClassDB::bind_method(D_METHOD("is_window_maximized"), &_OS::is_window_maximized);
  1023. ClassDB::bind_method(D_METHOD("set_window_always_on_top", "enabled"), &_OS::set_window_always_on_top);
  1024. ClassDB::bind_method(D_METHOD("is_window_always_on_top"), &_OS::is_window_always_on_top);
  1025. ClassDB::bind_method(D_METHOD("is_window_focused"), &_OS::is_window_focused);
  1026. ClassDB::bind_method(D_METHOD("request_attention"), &_OS::request_attention);
  1027. ClassDB::bind_method(D_METHOD("get_real_window_size"), &_OS::get_real_window_size);
  1028. ClassDB::bind_method(D_METHOD("center_window"), &_OS::center_window);
  1029. ClassDB::bind_method(D_METHOD("move_window_to_foreground"), &_OS::move_window_to_foreground);
  1030. ClassDB::bind_method(D_METHOD("get_native_handle", "handle_type"), &_OS::get_native_handle);
  1031. ClassDB::bind_method(D_METHOD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
  1032. ClassDB::bind_method(D_METHOD("get_borderless_window"), &_OS::get_borderless_window);
  1033. ClassDB::bind_method(D_METHOD("get_window_per_pixel_transparency_enabled"), &_OS::get_window_per_pixel_transparency_enabled);
  1034. ClassDB::bind_method(D_METHOD("set_window_per_pixel_transparency_enabled", "enabled"), &_OS::set_window_per_pixel_transparency_enabled);
  1035. ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &_OS::set_ime_active);
  1036. ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &_OS::set_ime_position);
  1037. ClassDB::bind_method(D_METHOD("get_ime_selection"), &_OS::get_ime_selection);
  1038. ClassDB::bind_method(D_METHOD("get_ime_text"), &_OS::get_ime_text);
  1039. ClassDB::bind_method(D_METHOD("set_screen_orientation", "orientation"), &_OS::set_screen_orientation);
  1040. ClassDB::bind_method(D_METHOD("get_screen_orientation"), &_OS::get_screen_orientation);
  1041. ClassDB::bind_method(D_METHOD("set_keep_screen_on", "enabled"), &_OS::set_keep_screen_on);
  1042. ClassDB::bind_method(D_METHOD("is_keep_screen_on"), &_OS::is_keep_screen_on);
  1043. ClassDB::bind_method(D_METHOD("has_touchscreen_ui_hint"), &_OS::has_touchscreen_ui_hint);
  1044. ClassDB::bind_method(D_METHOD("set_window_title", "title"), &_OS::set_window_title);
  1045. ClassDB::bind_method(D_METHOD("set_window_mouse_passthrough", "region"), &_OS::set_window_mouse_passthrough);
  1046. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
  1047. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
  1048. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &_OS::set_low_processor_usage_mode_sleep_usec);
  1049. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &_OS::get_low_processor_usage_mode_sleep_usec);
  1050. ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count);
  1051. ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
  1052. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "blocking", "output", "read_stderr"), &_OS::execute, DEFVAL(true), DEFVAL(Array()), DEFVAL(false));
  1053. ClassDB::bind_method(D_METHOD("kill", "pid"), &_OS::kill);
  1054. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &_OS::shell_open);
  1055. ClassDB::bind_method(D_METHOD("get_process_id"), &_OS::get_process_id);
  1056. ClassDB::bind_method(D_METHOD("get_environment", "variable"), &_OS::get_environment);
  1057. ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &_OS::set_environment);
  1058. ClassDB::bind_method(D_METHOD("has_environment", "variable"), &_OS::has_environment);
  1059. ClassDB::bind_method(D_METHOD("get_name"), &_OS::get_name);
  1060. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &_OS::get_cmdline_args);
  1061. ClassDB::bind_method(D_METHOD("get_datetime", "utc"), &_OS::get_datetime, DEFVAL(false));
  1062. ClassDB::bind_method(D_METHOD("get_date", "utc"), &_OS::get_date, DEFVAL(false));
  1063. ClassDB::bind_method(D_METHOD("get_time", "utc"), &_OS::get_time, DEFVAL(false));
  1064. ClassDB::bind_method(D_METHOD("get_time_zone_info"), &_OS::get_time_zone_info);
  1065. ClassDB::bind_method(D_METHOD("get_unix_time"), &_OS::get_unix_time);
  1066. ClassDB::bind_method(D_METHOD("get_datetime_from_unix_time", "unix_time_val"), &_OS::get_datetime_from_unix_time);
  1067. ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime", "datetime"), &_OS::get_unix_time_from_datetime);
  1068. ClassDB::bind_method(D_METHOD("get_system_time_secs"), &_OS::get_system_time_secs);
  1069. ClassDB::bind_method(D_METHOD("get_system_time_msecs"), &_OS::get_system_time_msecs);
  1070. ClassDB::bind_method(D_METHOD("set_native_icon", "filename"), &_OS::set_native_icon);
  1071. ClassDB::bind_method(D_METHOD("set_icon", "icon"), &_OS::set_icon);
  1072. ClassDB::bind_method(D_METHOD("get_exit_code"), &_OS::get_exit_code);
  1073. ClassDB::bind_method(D_METHOD("set_exit_code", "code"), &_OS::set_exit_code);
  1074. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &_OS::delay_usec);
  1075. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &_OS::delay_msec);
  1076. ClassDB::bind_method(D_METHOD("get_ticks_msec"), &_OS::get_ticks_msec);
  1077. ClassDB::bind_method(D_METHOD("get_ticks_usec"), &_OS::get_ticks_usec);
  1078. ClassDB::bind_method(D_METHOD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
  1079. ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
  1080. ClassDB::bind_method(D_METHOD("get_latin_keyboard_variant"), &_OS::get_latin_keyboard_variant);
  1081. ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
  1082. ClassDB::bind_method(D_METHOD("keyboard_get_layout_count"), &_OS::keyboard_get_layout_count);
  1083. ClassDB::bind_method(D_METHOD("keyboard_get_current_layout"), &_OS::keyboard_get_current_layout);
  1084. ClassDB::bind_method(D_METHOD("keyboard_set_current_layout", "index"), &_OS::keyboard_set_current_layout);
  1085. ClassDB::bind_method(D_METHOD("keyboard_get_layout_language", "index"), &_OS::keyboard_get_layout_language);
  1086. ClassDB::bind_method(D_METHOD("keyboard_get_layout_name", "index"), &_OS::keyboard_get_layout_name);
  1087. ClassDB::bind_method(D_METHOD("can_draw"), &_OS::can_draw);
  1088. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &_OS::is_userfs_persistent);
  1089. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &_OS::is_stdout_verbose);
  1090. ClassDB::bind_method(D_METHOD("can_use_threads"), &_OS::can_use_threads);
  1091. ClassDB::bind_method(D_METHOD("is_debug_build"), &_OS::is_debug_build);
  1092. //ClassDB::bind_method(D_METHOD("get_mouse_button_state"),&_OS::get_mouse_button_state);
  1093. ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
  1094. ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
  1095. ClassDB::bind_method(D_METHOD("has_virtual_keyboard"), &_OS::has_virtual_keyboard);
  1096. ClassDB::bind_method(D_METHOD("show_virtual_keyboard", "existing_text", "multiline"), &_OS::show_virtual_keyboard, DEFVAL(""), DEFVAL(false));
  1097. ClassDB::bind_method(D_METHOD("hide_virtual_keyboard"), &_OS::hide_virtual_keyboard);
  1098. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_height"), &_OS::get_virtual_keyboard_height);
  1099. ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
  1100. ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
  1101. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &_OS::get_static_memory_usage);
  1102. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
  1103. ClassDB::bind_method(D_METHOD("get_dynamic_memory_usage"), &_OS::get_dynamic_memory_usage);
  1104. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir);
  1105. ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &_OS::get_system_dir, DEFVAL(true));
  1106. ClassDB::bind_method(D_METHOD("get_config_dir"), &_OS::get_config_dir);
  1107. ClassDB::bind_method(D_METHOD("get_data_dir"), &_OS::get_data_dir);
  1108. ClassDB::bind_method(D_METHOD("get_cache_dir"), &_OS::get_cache_dir);
  1109. ClassDB::bind_method(D_METHOD("get_unique_id"), &_OS::get_unique_id);
  1110. ClassDB::bind_method(D_METHOD("is_ok_left_and_cancel_right"), &_OS::is_ok_left_and_cancel_right);
  1111. ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
  1112. ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
  1113. ClassDB::bind_method(D_METHOD("native_video_play", "path", "volume", "audio_track", "subtitle_track"), &_OS::native_video_play);
  1114. ClassDB::bind_method(D_METHOD("native_video_is_playing"), &_OS::native_video_is_playing);
  1115. ClassDB::bind_method(D_METHOD("native_video_stop"), &_OS::native_video_stop);
  1116. ClassDB::bind_method(D_METHOD("native_video_pause"), &_OS::native_video_pause);
  1117. ClassDB::bind_method(D_METHOD("native_video_unpause"), &_OS::native_video_unpause);
  1118. ClassDB::bind_method(D_METHOD("get_scancode_string", "code"), &_OS::get_scancode_string);
  1119. ClassDB::bind_method(D_METHOD("is_scancode_unicode", "code"), &_OS::is_scancode_unicode);
  1120. ClassDB::bind_method(D_METHOD("find_scancode_from_string", "string"), &_OS::find_scancode_from_string);
  1121. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
  1122. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &_OS::alert, DEFVAL("Alert!"));
  1123. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &_OS::set_thread_name);
  1124. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &_OS::get_thread_caller_id);
  1125. ClassDB::bind_method(D_METHOD("set_use_vsync", "enable"), &_OS::set_use_vsync);
  1126. ClassDB::bind_method(D_METHOD("is_vsync_enabled"), &_OS::is_vsync_enabled);
  1127. ClassDB::bind_method(D_METHOD("set_vsync_via_compositor", "enable"), &_OS::set_vsync_via_compositor);
  1128. ClassDB::bind_method(D_METHOD("is_vsync_via_compositor_enabled"), &_OS::is_vsync_via_compositor_enabled);
  1129. ClassDB::bind_method(D_METHOD("set_delta_smoothing", "delta_smoothing_enabled"), &_OS::set_delta_smoothing);
  1130. ClassDB::bind_method(D_METHOD("is_delta_smoothing_enabled"), &_OS::is_delta_smoothing_enabled);
  1131. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &_OS::has_feature);
  1132. ClassDB::bind_method(D_METHOD("get_power_state"), &_OS::get_power_state);
  1133. ClassDB::bind_method(D_METHOD("get_power_seconds_left"), &_OS::get_power_seconds_left);
  1134. ClassDB::bind_method(D_METHOD("get_power_percent_left"), &_OS::get_power_percent_left);
  1135. ClassDB::bind_method(D_METHOD("request_permission", "name"), &_OS::request_permission);
  1136. ClassDB::bind_method(D_METHOD("request_permissions"), &_OS::request_permissions);
  1137. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &_OS::get_granted_permissions);
  1138. ClassDB::bind_method(D_METHOD("get_tablet_driver_count"), &_OS::get_tablet_driver_count);
  1139. ClassDB::bind_method(D_METHOD("get_tablet_driver_name", "idx"), &_OS::get_tablet_driver_name);
  1140. ClassDB::bind_method(D_METHOD("get_current_tablet_driver"), &_OS::get_current_tablet_driver);
  1141. ClassDB::bind_method(D_METHOD("set_current_tablet_driver", "name"), &_OS::set_current_tablet_driver);
  1142. ADD_PROPERTY(PropertyInfo(Variant::STRING, "tablet_driver"), "set_current_tablet_driver", "get_current_tablet_driver");
  1143. ADD_PROPERTY(PropertyInfo(Variant::STRING, "clipboard"), "set_clipboard", "get_clipboard");
  1144. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen"), "set_current_screen", "get_current_screen");
  1145. ADD_PROPERTY(PropertyInfo(Variant::INT, "exit_code"), "set_exit_code", "get_exit_code");
  1146. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vsync_enabled"), "set_use_vsync", "is_vsync_enabled");
  1147. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vsync_via_compositor"), "set_vsync_via_compositor", "is_vsync_via_compositor_enabled");
  1148. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "delta_smoothing"), "set_delta_smoothing", "is_delta_smoothing_enabled");
  1149. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  1150. 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");
  1151. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_screen_on"), "set_keep_screen_on", "is_keep_screen_on");
  1152. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "min_window_size"), "set_min_window_size", "get_min_window_size");
  1153. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "max_window_size"), "set_max_window_size", "get_max_window_size");
  1154. 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");
  1155. ADD_GROUP("Window", "window_");
  1156. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_borderless"), "set_borderless_window", "get_borderless_window");
  1157. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_per_pixel_transparency_enabled"), "set_window_per_pixel_transparency_enabled", "get_window_per_pixel_transparency_enabled");
  1158. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_fullscreen"), "set_window_fullscreen", "is_window_fullscreen");
  1159. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_maximized"), "set_window_maximized", "is_window_maximized");
  1160. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_minimized"), "set_window_minimized", "is_window_minimized");
  1161. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_resizable"), "set_window_resizable", "is_window_resizable");
  1162. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "window_position"), "set_window_position", "get_window_position");
  1163. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "window_size"), "set_window_size", "get_window_size");
  1164. // Those default values need to be specified for the docs generator,
  1165. // to avoid using values from the documentation writer's own OS instance.
  1166. ADD_PROPERTY_DEFAULT("clipboard", "");
  1167. ADD_PROPERTY_DEFAULT("current_screen", 0);
  1168. ADD_PROPERTY_DEFAULT("exit_code", 0);
  1169. ADD_PROPERTY_DEFAULT("vsync_enabled", true);
  1170. ADD_PROPERTY_DEFAULT("vsync_via_compositor", false);
  1171. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  1172. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  1173. ADD_PROPERTY_DEFAULT("keep_screen_on", true);
  1174. ADD_PROPERTY_DEFAULT("min_window_size", Vector2());
  1175. ADD_PROPERTY_DEFAULT("max_window_size", Vector2());
  1176. ADD_PROPERTY_DEFAULT("screen_orientation", 0);
  1177. ADD_PROPERTY_DEFAULT("window_borderless", false);
  1178. ADD_PROPERTY_DEFAULT("window_per_pixel_transparency_enabled", false);
  1179. ADD_PROPERTY_DEFAULT("window_fullscreen", false);
  1180. ADD_PROPERTY_DEFAULT("window_maximized", false);
  1181. ADD_PROPERTY_DEFAULT("window_minimized", false);
  1182. ADD_PROPERTY_DEFAULT("window_resizable", true);
  1183. ADD_PROPERTY_DEFAULT("window_position", Vector2());
  1184. ADD_PROPERTY_DEFAULT("window_size", Vector2());
  1185. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES2);
  1186. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES3);
  1187. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  1188. BIND_ENUM_CONSTANT(DAY_MONDAY);
  1189. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  1190. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  1191. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  1192. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  1193. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  1194. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  1195. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  1196. BIND_ENUM_CONSTANT(MONTH_MARCH);
  1197. BIND_ENUM_CONSTANT(MONTH_APRIL);
  1198. BIND_ENUM_CONSTANT(MONTH_MAY);
  1199. BIND_ENUM_CONSTANT(MONTH_JUNE);
  1200. BIND_ENUM_CONSTANT(MONTH_JULY);
  1201. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  1202. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  1203. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  1204. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  1205. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  1206. BIND_ENUM_CONSTANT(APPLICATION_HANDLE);
  1207. BIND_ENUM_CONSTANT(DISPLAY_HANDLE);
  1208. BIND_ENUM_CONSTANT(WINDOW_HANDLE);
  1209. BIND_ENUM_CONSTANT(WINDOW_VIEW);
  1210. BIND_ENUM_CONSTANT(OPENGL_CONTEXT);
  1211. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_LANDSCAPE);
  1212. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_PORTRAIT);
  1213. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
  1214. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
  1215. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  1216. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  1217. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR);
  1218. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  1219. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  1220. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  1221. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  1222. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  1223. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  1224. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  1225. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  1226. BIND_ENUM_CONSTANT(POWERSTATE_UNKNOWN);
  1227. BIND_ENUM_CONSTANT(POWERSTATE_ON_BATTERY);
  1228. BIND_ENUM_CONSTANT(POWERSTATE_NO_BATTERY);
  1229. BIND_ENUM_CONSTANT(POWERSTATE_CHARGING);
  1230. BIND_ENUM_CONSTANT(POWERSTATE_CHARGED);
  1231. }
  1232. _OS::_OS() {
  1233. singleton = this;
  1234. }
  1235. ///////////////////// GEOMETRY
  1236. _Geometry *_Geometry::singleton = nullptr;
  1237. _Geometry *_Geometry::get_singleton() {
  1238. return singleton;
  1239. }
  1240. PoolVector<Plane> _Geometry::build_box_planes(const Vector3 &p_extents) {
  1241. return Geometry::build_box_planes(p_extents);
  1242. }
  1243. PoolVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  1244. return Geometry::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  1245. }
  1246. PoolVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  1247. return Geometry::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  1248. }
  1249. bool _Geometry::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  1250. return Geometry::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  1251. }
  1252. real_t _Geometry::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  1253. return Geometry::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  1254. }
  1255. 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) {
  1256. Vector2 result;
  1257. if (Geometry::segment_intersects_segment_2d(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  1258. return result;
  1259. } else {
  1260. return Variant();
  1261. };
  1262. };
  1263. 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) {
  1264. Vector2 result;
  1265. if (Geometry::line_intersects_line_2d(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  1266. return result;
  1267. } else {
  1268. return Variant();
  1269. }
  1270. }
  1271. PoolVector<Vector2> _Geometry::get_closest_points_between_segments_2d(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  1272. Vector2 r1, r2;
  1273. Geometry::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  1274. PoolVector<Vector2> r;
  1275. r.resize(2);
  1276. r.set(0, r1);
  1277. r.set(1, r2);
  1278. return r;
  1279. }
  1280. PoolVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  1281. Vector3 r1, r2;
  1282. Geometry::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  1283. PoolVector<Vector3> r;
  1284. r.resize(2);
  1285. r.set(0, r1);
  1286. r.set(1, r2);
  1287. return r;
  1288. }
  1289. Vector2 _Geometry::get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  1290. Vector2 s[2] = { p_a, p_b };
  1291. return Geometry::get_closest_point_to_segment_2d(p_point, s);
  1292. }
  1293. Vector3 _Geometry::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  1294. Vector3 s[2] = { p_a, p_b };
  1295. return Geometry::get_closest_point_to_segment(p_point, s);
  1296. }
  1297. Vector2 _Geometry::get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  1298. Vector2 s[2] = { p_a, p_b };
  1299. return Geometry::get_closest_point_to_segment_uncapped_2d(p_point, s);
  1300. }
  1301. Vector3 _Geometry::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  1302. Vector3 s[2] = { p_a, p_b };
  1303. return Geometry::get_closest_point_to_segment_uncapped(p_point, s);
  1304. }
  1305. 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) {
  1306. Vector3 res;
  1307. if (Geometry::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  1308. return res;
  1309. } else {
  1310. return Variant();
  1311. }
  1312. }
  1313. 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) {
  1314. Vector3 res;
  1315. if (Geometry::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  1316. return res;
  1317. } else {
  1318. return Variant();
  1319. }
  1320. }
  1321. bool _Geometry::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  1322. return Geometry::is_point_in_triangle(s, a, b, c);
  1323. }
  1324. PoolVector<Vector3> _Geometry::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  1325. PoolVector<Vector3> r;
  1326. Vector3 res, norm;
  1327. if (!Geometry::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  1328. return r;
  1329. }
  1330. r.resize(2);
  1331. r.set(0, res);
  1332. r.set(1, norm);
  1333. return r;
  1334. }
  1335. PoolVector<Vector3> _Geometry::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  1336. PoolVector<Vector3> r;
  1337. Vector3 res, norm;
  1338. if (!Geometry::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  1339. return r;
  1340. }
  1341. r.resize(2);
  1342. r.set(0, res);
  1343. r.set(1, norm);
  1344. return r;
  1345. }
  1346. PoolVector<Vector3> _Geometry::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  1347. PoolVector<Vector3> r;
  1348. Vector3 res, norm;
  1349. if (!Geometry::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm)) {
  1350. return r;
  1351. }
  1352. r.resize(2);
  1353. r.set(0, res);
  1354. r.set(1, norm);
  1355. return r;
  1356. }
  1357. bool _Geometry::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  1358. return Geometry::is_polygon_clockwise(p_polygon);
  1359. }
  1360. bool _Geometry::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  1361. return Geometry::is_point_in_polygon(p_point, p_polygon);
  1362. }
  1363. Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  1364. return Geometry::triangulate_polygon(p_polygon);
  1365. }
  1366. Vector<int> _Geometry::triangulate_delaunay_2d(const Vector<Vector2> &p_points) {
  1367. return Geometry::triangulate_delaunay_2d(p_points);
  1368. }
  1369. Vector<Point2> _Geometry::convex_hull_2d(const Vector<Point2> &p_points) {
  1370. return Geometry::convex_hull_2d(p_points);
  1371. }
  1372. Vector<Vector3> _Geometry::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  1373. return Geometry::clip_polygon(p_points, p_plane);
  1374. }
  1375. Array _Geometry::merge_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1376. Vector<Vector<Point2>> polys = Geometry::merge_polygons_2d(p_polygon_a, p_polygon_b);
  1377. Array ret;
  1378. for (int i = 0; i < polys.size(); ++i) {
  1379. ret.push_back(polys[i]);
  1380. }
  1381. return ret;
  1382. }
  1383. Array _Geometry::clip_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1384. Vector<Vector<Point2>> polys = Geometry::clip_polygons_2d(p_polygon_a, p_polygon_b);
  1385. Array ret;
  1386. for (int i = 0; i < polys.size(); ++i) {
  1387. ret.push_back(polys[i]);
  1388. }
  1389. return ret;
  1390. }
  1391. Array _Geometry::intersect_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1392. Vector<Vector<Point2>> polys = Geometry::intersect_polygons_2d(p_polygon_a, p_polygon_b);
  1393. Array ret;
  1394. for (int i = 0; i < polys.size(); ++i) {
  1395. ret.push_back(polys[i]);
  1396. }
  1397. return ret;
  1398. }
  1399. Array _Geometry::exclude_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1400. Vector<Vector<Point2>> polys = Geometry::exclude_polygons_2d(p_polygon_a, p_polygon_b);
  1401. Array ret;
  1402. for (int i = 0; i < polys.size(); ++i) {
  1403. ret.push_back(polys[i]);
  1404. }
  1405. return ret;
  1406. }
  1407. Array _Geometry::clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  1408. Vector<Vector<Point2>> polys = Geometry::clip_polyline_with_polygon_2d(p_polyline, p_polygon);
  1409. Array ret;
  1410. for (int i = 0; i < polys.size(); ++i) {
  1411. ret.push_back(polys[i]);
  1412. }
  1413. return ret;
  1414. }
  1415. Array _Geometry::intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  1416. Vector<Vector<Point2>> polys = Geometry::intersect_polyline_with_polygon_2d(p_polyline, p_polygon);
  1417. Array ret;
  1418. for (int i = 0; i < polys.size(); ++i) {
  1419. ret.push_back(polys[i]);
  1420. }
  1421. return ret;
  1422. }
  1423. Array _Geometry::offset_polygon_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  1424. Vector<Vector<Point2>> polys = Geometry::offset_polygon_2d(p_polygon, p_delta, Geometry::PolyJoinType(p_join_type));
  1425. Array ret;
  1426. for (int i = 0; i < polys.size(); ++i) {
  1427. ret.push_back(polys[i]);
  1428. }
  1429. return ret;
  1430. }
  1431. Array _Geometry::offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  1432. Vector<Vector<Point2>> polys = Geometry::offset_polyline_2d(p_polygon, p_delta, Geometry::PolyJoinType(p_join_type), Geometry::PolyEndType(p_end_type));
  1433. Array ret;
  1434. for (int i = 0; i < polys.size(); ++i) {
  1435. ret.push_back(polys[i]);
  1436. }
  1437. return ret;
  1438. }
  1439. Dictionary _Geometry::make_atlas(const Vector<Size2> &p_rects) {
  1440. Dictionary ret;
  1441. Vector<Size2i> rects;
  1442. for (int i = 0; i < p_rects.size(); i++) {
  1443. rects.push_back(p_rects[i]);
  1444. };
  1445. Vector<Point2i> result;
  1446. Size2i size;
  1447. Geometry::make_atlas(rects, result, size);
  1448. Size2 r_size = size;
  1449. Vector<Point2> r_result;
  1450. for (int i = 0; i < result.size(); i++) {
  1451. r_result.push_back(result[i]);
  1452. };
  1453. ret["points"] = r_result;
  1454. ret["size"] = r_size;
  1455. return ret;
  1456. };
  1457. int _Geometry::get_uv84_normal_bit(const Vector3 &p_vector) {
  1458. return Geometry::get_uv84_normal_bit(p_vector);
  1459. }
  1460. void _Geometry::_bind_methods() {
  1461. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry::build_box_planes);
  1462. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  1463. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  1464. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &_Geometry::is_point_in_circle);
  1465. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &_Geometry::segment_intersects_circle);
  1466. ClassDB::bind_method(D_METHOD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d);
  1467. ClassDB::bind_method(D_METHOD("line_intersects_line_2d", "from_a", "dir_a", "from_b", "dir_b"), &_Geometry::line_intersects_line_2d);
  1468. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments_2d", "p1", "q1", "p2", "q2"), &_Geometry::get_closest_points_between_segments_2d);
  1469. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry::get_closest_points_between_segments);
  1470. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_2d);
  1471. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment);
  1472. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped_2d);
  1473. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped);
  1474. ClassDB::bind_method(D_METHOD("get_uv84_normal_bit", "normal"), &_Geometry::get_uv84_normal_bit);
  1475. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry::ray_intersects_triangle);
  1476. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry::segment_intersects_triangle);
  1477. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &_Geometry::segment_intersects_sphere);
  1478. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry::segment_intersects_cylinder);
  1479. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &_Geometry::segment_intersects_convex);
  1480. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle);
  1481. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &_Geometry::is_polygon_clockwise);
  1482. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &_Geometry::is_point_in_polygon);
  1483. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &_Geometry::triangulate_polygon);
  1484. ClassDB::bind_method(D_METHOD("triangulate_delaunay_2d", "points"), &_Geometry::triangulate_delaunay_2d);
  1485. ClassDB::bind_method(D_METHOD("convex_hull_2d", "points"), &_Geometry::convex_hull_2d);
  1486. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &_Geometry::clip_polygon);
  1487. ClassDB::bind_method(D_METHOD("merge_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::merge_polygons_2d);
  1488. ClassDB::bind_method(D_METHOD("clip_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::clip_polygons_2d);
  1489. ClassDB::bind_method(D_METHOD("intersect_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::intersect_polygons_2d);
  1490. ClassDB::bind_method(D_METHOD("exclude_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::exclude_polygons_2d);
  1491. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon_2d", "polyline", "polygon"), &_Geometry::clip_polyline_with_polygon_2d);
  1492. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon_2d", "polyline", "polygon"), &_Geometry::intersect_polyline_with_polygon_2d);
  1493. ClassDB::bind_method(D_METHOD("offset_polygon_2d", "polygon", "delta", "join_type"), &_Geometry::offset_polygon_2d, DEFVAL(JOIN_SQUARE));
  1494. ClassDB::bind_method(D_METHOD("offset_polyline_2d", "polyline", "delta", "join_type", "end_type"), &_Geometry::offset_polyline_2d, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  1495. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &_Geometry::make_atlas);
  1496. BIND_ENUM_CONSTANT(OPERATION_UNION);
  1497. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  1498. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  1499. BIND_ENUM_CONSTANT(OPERATION_XOR);
  1500. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  1501. BIND_ENUM_CONSTANT(JOIN_ROUND);
  1502. BIND_ENUM_CONSTANT(JOIN_MITER);
  1503. BIND_ENUM_CONSTANT(END_POLYGON);
  1504. BIND_ENUM_CONSTANT(END_JOINED);
  1505. BIND_ENUM_CONSTANT(END_BUTT);
  1506. BIND_ENUM_CONSTANT(END_SQUARE);
  1507. BIND_ENUM_CONSTANT(END_ROUND);
  1508. }
  1509. _Geometry::_Geometry() {
  1510. singleton = this;
  1511. }
  1512. ///////////////////////// FILE
  1513. Error _File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  1514. Error err = open(p_path, p_mode_flags);
  1515. if (err) {
  1516. return err;
  1517. }
  1518. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  1519. err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  1520. if (err) {
  1521. memdelete(fae);
  1522. close();
  1523. return err;
  1524. }
  1525. f = fae;
  1526. return OK;
  1527. }
  1528. Error _File::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  1529. Error err = open(p_path, p_mode_flags);
  1530. if (err) {
  1531. return err;
  1532. }
  1533. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  1534. err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  1535. if (err) {
  1536. memdelete(fae);
  1537. close();
  1538. return err;
  1539. }
  1540. f = fae;
  1541. return OK;
  1542. }
  1543. Error _File::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  1544. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1545. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  1546. Error err = fac->_open(p_path, p_mode_flags);
  1547. if (err) {
  1548. memdelete(fac);
  1549. return err;
  1550. }
  1551. f = fac;
  1552. return OK;
  1553. }
  1554. Error _File::open(const String &p_path, ModeFlags p_mode_flags) {
  1555. close();
  1556. Error err;
  1557. f = FileAccess::open(p_path, p_mode_flags, &err);
  1558. if (f) {
  1559. f->set_endian_swap(eswap);
  1560. }
  1561. return err;
  1562. }
  1563. void _File::flush() {
  1564. ERR_FAIL_COND_MSG(!f, "File must be opened before flushing.");
  1565. f->flush();
  1566. }
  1567. void _File::close() {
  1568. if (f) {
  1569. memdelete(f);
  1570. }
  1571. f = nullptr;
  1572. }
  1573. bool _File::is_open() const {
  1574. return f != nullptr;
  1575. }
  1576. String _File::get_path() const {
  1577. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  1578. return f->get_path();
  1579. }
  1580. String _File::get_path_absolute() const {
  1581. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  1582. return f->get_path_absolute();
  1583. }
  1584. void _File::seek(int64_t p_position) {
  1585. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1586. ERR_FAIL_COND_MSG(p_position < 0, "Seek position must be a positive integer.");
  1587. f->seek(p_position);
  1588. }
  1589. void _File::seek_end(int64_t p_position) {
  1590. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1591. f->seek_end(p_position);
  1592. }
  1593. uint64_t _File::get_position() const {
  1594. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1595. return f->get_position();
  1596. }
  1597. uint64_t _File::get_len() const {
  1598. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1599. return f->get_len();
  1600. }
  1601. bool _File::eof_reached() const {
  1602. ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use.");
  1603. return f->eof_reached();
  1604. }
  1605. uint8_t _File::get_8() const {
  1606. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1607. return f->get_8();
  1608. }
  1609. uint16_t _File::get_16() const {
  1610. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1611. return f->get_16();
  1612. }
  1613. uint32_t _File::get_32() const {
  1614. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1615. return f->get_32();
  1616. }
  1617. uint64_t _File::get_64() const {
  1618. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1619. return f->get_64();
  1620. }
  1621. float _File::get_float() const {
  1622. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1623. return f->get_float();
  1624. }
  1625. double _File::get_double() const {
  1626. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1627. return f->get_double();
  1628. }
  1629. real_t _File::get_real() const {
  1630. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1631. return f->get_real();
  1632. }
  1633. PoolVector<uint8_t> _File::get_buffer(int64_t p_length) const {
  1634. PoolVector<uint8_t> data;
  1635. ERR_FAIL_COND_V_MSG(!f, data, "File must be opened before use.");
  1636. ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0.");
  1637. if (p_length == 0) {
  1638. return data;
  1639. }
  1640. Error err = data.resize(p_length);
  1641. ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");
  1642. PoolVector<uint8_t>::Write w = data.write();
  1643. int64_t len = f->get_buffer(&w[0], p_length);
  1644. ERR_FAIL_COND_V(len < 0, PoolVector<uint8_t>());
  1645. w.release();
  1646. if (len < p_length) {
  1647. data.resize(p_length);
  1648. }
  1649. return data;
  1650. }
  1651. String _File::get_as_text() const {
  1652. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  1653. String text;
  1654. uint64_t original_pos = f->get_position();
  1655. f->seek(0);
  1656. String l = get_line();
  1657. while (!eof_reached()) {
  1658. text += l + "\n";
  1659. l = get_line();
  1660. }
  1661. text += l;
  1662. f->seek(original_pos);
  1663. return text;
  1664. }
  1665. String _File::get_md5(const String &p_path) const {
  1666. return FileAccess::get_md5(p_path);
  1667. }
  1668. String _File::get_sha256(const String &p_path) const {
  1669. return FileAccess::get_sha256(p_path);
  1670. }
  1671. String _File::get_line() const {
  1672. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  1673. return f->get_line();
  1674. }
  1675. Vector<String> _File::get_csv_line(const String &p_delim) const {
  1676. ERR_FAIL_COND_V_MSG(!f, Vector<String>(), "File must be opened before use.");
  1677. return f->get_csv_line(p_delim);
  1678. }
  1679. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  1680. * It's not about the current CPU type but file formats.
  1681. * this flags get reset to false (little endian) on each open
  1682. */
  1683. void _File::set_endian_swap(bool p_swap) {
  1684. eswap = p_swap;
  1685. if (f) {
  1686. f->set_endian_swap(p_swap);
  1687. }
  1688. }
  1689. bool _File::get_endian_swap() {
  1690. return eswap;
  1691. }
  1692. Error _File::get_error() const {
  1693. if (!f) {
  1694. return ERR_UNCONFIGURED;
  1695. }
  1696. return f->get_error();
  1697. }
  1698. void _File::store_8(uint8_t p_dest) {
  1699. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1700. f->store_8(p_dest);
  1701. }
  1702. void _File::store_16(uint16_t p_dest) {
  1703. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1704. f->store_16(p_dest);
  1705. }
  1706. void _File::store_32(uint32_t p_dest) {
  1707. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1708. f->store_32(p_dest);
  1709. }
  1710. void _File::store_64(uint64_t p_dest) {
  1711. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1712. f->store_64(p_dest);
  1713. }
  1714. void _File::store_float(float p_dest) {
  1715. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1716. f->store_float(p_dest);
  1717. }
  1718. void _File::store_double(double p_dest) {
  1719. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1720. f->store_double(p_dest);
  1721. }
  1722. void _File::store_real(real_t p_real) {
  1723. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1724. f->store_real(p_real);
  1725. }
  1726. void _File::store_string(const String &p_string) {
  1727. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1728. f->store_string(p_string);
  1729. }
  1730. void _File::store_pascal_string(const String &p_string) {
  1731. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1732. f->store_pascal_string(p_string);
  1733. };
  1734. String _File::get_pascal_string() {
  1735. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  1736. return f->get_pascal_string();
  1737. };
  1738. void _File::store_line(const String &p_string) {
  1739. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1740. f->store_line(p_string);
  1741. }
  1742. void _File::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  1743. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1744. f->store_csv_line(p_values, p_delim);
  1745. }
  1746. void _File::store_buffer(const PoolVector<uint8_t> &p_buffer) {
  1747. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1748. uint64_t len = p_buffer.size();
  1749. if (len == 0) {
  1750. return;
  1751. }
  1752. PoolVector<uint8_t>::Read r = p_buffer.read();
  1753. f->store_buffer(&r[0], len);
  1754. }
  1755. bool _File::file_exists(const String &p_name) const {
  1756. return FileAccess::exists(p_name);
  1757. }
  1758. void _File::store_var(const Variant &p_var, bool p_full_objects) {
  1759. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1760. int len;
  1761. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1762. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1763. PoolVector<uint8_t> buff;
  1764. buff.resize(len);
  1765. PoolVector<uint8_t>::Write w = buff.write();
  1766. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1767. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1768. w.release();
  1769. store_32(len);
  1770. store_buffer(buff);
  1771. }
  1772. Variant _File::get_var(bool p_allow_objects) const {
  1773. ERR_FAIL_COND_V_MSG(!f, Variant(), "File must be opened before use.");
  1774. uint32_t len = get_32();
  1775. PoolVector<uint8_t> buff = get_buffer(len);
  1776. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  1777. PoolVector<uint8_t>::Read r = buff.read();
  1778. Variant v;
  1779. Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects);
  1780. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant.");
  1781. return v;
  1782. }
  1783. uint64_t _File::get_modified_time(const String &p_file) const {
  1784. return FileAccess::get_modified_time(p_file);
  1785. }
  1786. void _File::_bind_methods() {
  1787. ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
  1788. ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
  1789. ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &_File::open_compressed, DEFVAL(0));
  1790. ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open);
  1791. ClassDB::bind_method(D_METHOD("flush"), &_File::flush);
  1792. ClassDB::bind_method(D_METHOD("close"), &_File::close);
  1793. ClassDB::bind_method(D_METHOD("get_path"), &_File::get_path);
  1794. ClassDB::bind_method(D_METHOD("get_path_absolute"), &_File::get_path_absolute);
  1795. ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open);
  1796. ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek);
  1797. ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0));
  1798. ClassDB::bind_method(D_METHOD("get_position"), &_File::get_position);
  1799. ClassDB::bind_method(D_METHOD("get_len"), &_File::get_len);
  1800. ClassDB::bind_method(D_METHOD("eof_reached"), &_File::eof_reached);
  1801. ClassDB::bind_method(D_METHOD("get_8"), &_File::get_8);
  1802. ClassDB::bind_method(D_METHOD("get_16"), &_File::get_16);
  1803. ClassDB::bind_method(D_METHOD("get_32"), &_File::get_32);
  1804. ClassDB::bind_method(D_METHOD("get_64"), &_File::get_64);
  1805. ClassDB::bind_method(D_METHOD("get_float"), &_File::get_float);
  1806. ClassDB::bind_method(D_METHOD("get_double"), &_File::get_double);
  1807. ClassDB::bind_method(D_METHOD("get_real"), &_File::get_real);
  1808. ClassDB::bind_method(D_METHOD("get_buffer", "len"), &_File::get_buffer);
  1809. ClassDB::bind_method(D_METHOD("get_line"), &_File::get_line);
  1810. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
  1811. ClassDB::bind_method(D_METHOD("get_as_text"), &_File::get_as_text);
  1812. ClassDB::bind_method(D_METHOD("get_md5", "path"), &_File::get_md5);
  1813. ClassDB::bind_method(D_METHOD("get_sha256", "path"), &_File::get_sha256);
  1814. ClassDB::bind_method(D_METHOD("get_endian_swap"), &_File::get_endian_swap);
  1815. ClassDB::bind_method(D_METHOD("set_endian_swap", "enable"), &_File::set_endian_swap);
  1816. ClassDB::bind_method(D_METHOD("get_error"), &_File::get_error);
  1817. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &_File::get_var, DEFVAL(false));
  1818. ClassDB::bind_method(D_METHOD("store_8", "value"), &_File::store_8);
  1819. ClassDB::bind_method(D_METHOD("store_16", "value"), &_File::store_16);
  1820. ClassDB::bind_method(D_METHOD("store_32", "value"), &_File::store_32);
  1821. ClassDB::bind_method(D_METHOD("store_64", "value"), &_File::store_64);
  1822. ClassDB::bind_method(D_METHOD("store_float", "value"), &_File::store_float);
  1823. ClassDB::bind_method(D_METHOD("store_double", "value"), &_File::store_double);
  1824. ClassDB::bind_method(D_METHOD("store_real", "value"), &_File::store_real);
  1825. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &_File::store_buffer);
  1826. ClassDB::bind_method(D_METHOD("store_line", "line"), &_File::store_line);
  1827. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &_File::store_csv_line, DEFVAL(","));
  1828. ClassDB::bind_method(D_METHOD("store_string", "string"), &_File::store_string);
  1829. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &_File::store_var, DEFVAL(false));
  1830. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &_File::store_pascal_string);
  1831. ClassDB::bind_method(D_METHOD("get_pascal_string"), &_File::get_pascal_string);
  1832. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists);
  1833. ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time);
  1834. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "endian_swap"), "set_endian_swap", "get_endian_swap");
  1835. BIND_ENUM_CONSTANT(READ);
  1836. BIND_ENUM_CONSTANT(WRITE);
  1837. BIND_ENUM_CONSTANT(READ_WRITE);
  1838. BIND_ENUM_CONSTANT(WRITE_READ);
  1839. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  1840. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  1841. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  1842. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  1843. }
  1844. _File::_File() {
  1845. f = nullptr;
  1846. eswap = false;
  1847. }
  1848. _File::~_File() {
  1849. if (f) {
  1850. memdelete(f);
  1851. }
  1852. }
  1853. ///////////////////////////////////////////////////////
  1854. Error _Directory::open(const String &p_path) {
  1855. Error err;
  1856. DirAccess *alt = DirAccess::open(p_path, &err);
  1857. if (!alt) {
  1858. return err;
  1859. }
  1860. if (d) {
  1861. memdelete(d);
  1862. }
  1863. d = alt;
  1864. return OK;
  1865. }
  1866. Error _Directory::list_dir_begin(bool p_skip_navigational, bool p_skip_hidden) {
  1867. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1868. _list_skip_navigational = p_skip_navigational;
  1869. _list_skip_hidden = p_skip_hidden;
  1870. return d->list_dir_begin();
  1871. }
  1872. String _Directory::get_next() {
  1873. ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use.");
  1874. String next = d->get_next();
  1875. while (next != "" && ((_list_skip_navigational && (next == "." || next == "..")) || (_list_skip_hidden && d->current_is_hidden()))) {
  1876. next = d->get_next();
  1877. }
  1878. return next;
  1879. }
  1880. bool _Directory::current_is_dir() const {
  1881. ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use.");
  1882. return d->current_is_dir();
  1883. }
  1884. void _Directory::list_dir_end() {
  1885. ERR_FAIL_COND_MSG(!d, "Directory must be opened before use.");
  1886. d->list_dir_end();
  1887. }
  1888. int _Directory::get_drive_count() {
  1889. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1890. return d->get_drive_count();
  1891. }
  1892. String _Directory::get_drive(int p_drive) {
  1893. ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use.");
  1894. return d->get_drive(p_drive);
  1895. }
  1896. int _Directory::get_current_drive() {
  1897. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1898. return d->get_current_drive();
  1899. }
  1900. Error _Directory::change_dir(String p_dir) {
  1901. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1902. return d->change_dir(p_dir);
  1903. }
  1904. String _Directory::get_current_dir() {
  1905. ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use.");
  1906. return d->get_current_dir();
  1907. }
  1908. Error _Directory::make_dir(String p_dir) {
  1909. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1910. if (!p_dir.is_rel_path()) {
  1911. DirAccess *d = DirAccess::create_for_path(p_dir);
  1912. Error err = d->make_dir(p_dir);
  1913. memdelete(d);
  1914. return err;
  1915. }
  1916. return d->make_dir(p_dir);
  1917. }
  1918. Error _Directory::make_dir_recursive(String p_dir) {
  1919. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1920. if (!p_dir.is_rel_path()) {
  1921. DirAccess *d = DirAccess::create_for_path(p_dir);
  1922. Error err = d->make_dir_recursive(p_dir);
  1923. memdelete(d);
  1924. return err;
  1925. }
  1926. return d->make_dir_recursive(p_dir);
  1927. }
  1928. bool _Directory::file_exists(String p_file) {
  1929. ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use.");
  1930. if (!p_file.is_rel_path()) {
  1931. return FileAccess::exists(p_file);
  1932. }
  1933. return d->file_exists(p_file);
  1934. }
  1935. bool _Directory::dir_exists(String p_dir) {
  1936. ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use.");
  1937. if (!p_dir.is_rel_path()) {
  1938. DirAccess *d = DirAccess::create_for_path(p_dir);
  1939. bool exists = d->dir_exists(p_dir);
  1940. memdelete(d);
  1941. return exists;
  1942. } else {
  1943. return d->dir_exists(p_dir);
  1944. }
  1945. }
  1946. uint64_t _Directory::get_space_left() {
  1947. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1948. return d->get_space_left() / 1024 * 1024; // Truncate to closest MiB.
  1949. }
  1950. Error _Directory::copy(String p_from, String p_to) {
  1951. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1952. return d->copy(p_from, p_to);
  1953. }
  1954. Error _Directory::rename(String p_from, String p_to) {
  1955. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1956. ERR_FAIL_COND_V_MSG(p_from.empty() || p_from == "." || p_from == "..", ERR_INVALID_PARAMETER, "Invalid path to rename.");
  1957. if (!p_from.is_rel_path()) {
  1958. DirAccess *d = DirAccess::create_for_path(p_from);
  1959. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
  1960. Error err = d->rename(p_from, p_to);
  1961. memdelete(d);
  1962. return err;
  1963. }
  1964. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
  1965. return d->rename(p_from, p_to);
  1966. }
  1967. Error _Directory::remove(String p_name) {
  1968. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1969. if (!p_name.is_rel_path()) {
  1970. DirAccess *d = DirAccess::create_for_path(p_name);
  1971. Error err = d->remove(p_name);
  1972. memdelete(d);
  1973. return err;
  1974. }
  1975. return d->remove(p_name);
  1976. }
  1977. void _Directory::_bind_methods() {
  1978. ClassDB::bind_method(D_METHOD("open", "path"), &_Directory::open);
  1979. ClassDB::bind_method(D_METHOD("list_dir_begin", "skip_navigational", "skip_hidden"), &_Directory::list_dir_begin, DEFVAL(false), DEFVAL(false));
  1980. ClassDB::bind_method(D_METHOD("get_next"), &_Directory::get_next);
  1981. ClassDB::bind_method(D_METHOD("current_is_dir"), &_Directory::current_is_dir);
  1982. ClassDB::bind_method(D_METHOD("list_dir_end"), &_Directory::list_dir_end);
  1983. ClassDB::bind_method(D_METHOD("get_drive_count"), &_Directory::get_drive_count);
  1984. ClassDB::bind_method(D_METHOD("get_drive", "idx"), &_Directory::get_drive);
  1985. ClassDB::bind_method(D_METHOD("get_current_drive"), &_Directory::get_current_drive);
  1986. ClassDB::bind_method(D_METHOD("change_dir", "todir"), &_Directory::change_dir);
  1987. ClassDB::bind_method(D_METHOD("get_current_dir"), &_Directory::get_current_dir);
  1988. ClassDB::bind_method(D_METHOD("make_dir", "path"), &_Directory::make_dir);
  1989. ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &_Directory::make_dir_recursive);
  1990. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_Directory::file_exists);
  1991. ClassDB::bind_method(D_METHOD("dir_exists", "path"), &_Directory::dir_exists);
  1992. //ClassDB::bind_method(D_METHOD("get_modified_time","file"),&_Directory::get_modified_time);
  1993. ClassDB::bind_method(D_METHOD("get_space_left"), &_Directory::get_space_left);
  1994. ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy);
  1995. ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename);
  1996. ClassDB::bind_method(D_METHOD("remove", "path"), &_Directory::remove);
  1997. }
  1998. _Directory::_Directory() {
  1999. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  2000. }
  2001. _Directory::~_Directory() {
  2002. if (d) {
  2003. memdelete(d);
  2004. }
  2005. }
  2006. _Marshalls *_Marshalls::singleton = nullptr;
  2007. _Marshalls *_Marshalls::get_singleton() {
  2008. return singleton;
  2009. }
  2010. String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  2011. int len;
  2012. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  2013. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  2014. PoolVector<uint8_t> buff;
  2015. buff.resize(len);
  2016. PoolVector<uint8_t>::Write w = buff.write();
  2017. err = encode_variant(p_var, &w[0], len, p_full_objects);
  2018. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  2019. String ret = CryptoCore::b64_encode_str(&w[0], len);
  2020. ERR_FAIL_COND_V(ret == "", ret);
  2021. return ret;
  2022. };
  2023. Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  2024. int strlen = p_str.length();
  2025. CharString cstr = p_str.ascii();
  2026. PoolVector<uint8_t> buf;
  2027. buf.resize(strlen / 4 * 3 + 1);
  2028. PoolVector<uint8_t>::Write w = buf.write();
  2029. size_t len = 0;
  2030. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  2031. Variant v;
  2032. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  2033. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  2034. return v;
  2035. };
  2036. String _Marshalls::raw_to_base64(const PoolVector<uint8_t> &p_arr) {
  2037. String ret = CryptoCore::b64_encode_str(p_arr.read().ptr(), p_arr.size());
  2038. ERR_FAIL_COND_V(ret == "", ret);
  2039. return ret;
  2040. };
  2041. PoolVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
  2042. int strlen = p_str.length();
  2043. CharString cstr = p_str.ascii();
  2044. size_t arr_len = 0;
  2045. PoolVector<uint8_t> buf;
  2046. {
  2047. buf.resize(strlen / 4 * 3 + 1);
  2048. PoolVector<uint8_t>::Write w = buf.write();
  2049. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, PoolVector<uint8_t>());
  2050. }
  2051. buf.resize(arr_len);
  2052. return buf;
  2053. };
  2054. String _Marshalls::utf8_to_base64(const String &p_str) {
  2055. CharString cstr = p_str.utf8();
  2056. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  2057. ERR_FAIL_COND_V(ret == "", ret);
  2058. return ret;
  2059. };
  2060. String _Marshalls::base64_to_utf8(const String &p_str) {
  2061. int strlen = p_str.length();
  2062. CharString cstr = p_str.ascii();
  2063. PoolVector<uint8_t> buf;
  2064. buf.resize(strlen / 4 * 3 + 1 + 1);
  2065. PoolVector<uint8_t>::Write w = buf.write();
  2066. size_t len = 0;
  2067. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  2068. w[len] = 0;
  2069. String ret = String::utf8((char *)&w[0]);
  2070. return ret;
  2071. };
  2072. void _Marshalls::_bind_methods() {
  2073. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &_Marshalls::variant_to_base64, DEFVAL(false));
  2074. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &_Marshalls::base64_to_variant, DEFVAL(false));
  2075. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &_Marshalls::raw_to_base64);
  2076. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &_Marshalls::base64_to_raw);
  2077. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &_Marshalls::utf8_to_base64);
  2078. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &_Marshalls::base64_to_utf8);
  2079. };
  2080. ////////////////
  2081. Error _Semaphore::wait() {
  2082. semaphore.wait();
  2083. return OK; // Can't fail anymore; keep compat
  2084. }
  2085. Error _Semaphore::post() {
  2086. semaphore.post();
  2087. return OK; // Can't fail anymore; keep compat
  2088. }
  2089. void _Semaphore::_bind_methods() {
  2090. ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait);
  2091. ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
  2092. }
  2093. ///////////////
  2094. void _Mutex::lock() {
  2095. mutex.lock();
  2096. }
  2097. Error _Mutex::try_lock() {
  2098. return mutex.try_lock();
  2099. }
  2100. void _Mutex::unlock() {
  2101. mutex.unlock();
  2102. }
  2103. void _Mutex::_bind_methods() {
  2104. ClassDB::bind_method(D_METHOD("lock"), &_Mutex::lock);
  2105. ClassDB::bind_method(D_METHOD("try_lock"), &_Mutex::try_lock);
  2106. ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock);
  2107. }
  2108. ///////////////
  2109. void _Thread::_start_func(void *ud) {
  2110. Ref<_Thread> *tud = (Ref<_Thread> *)ud;
  2111. Ref<_Thread> t = *tud;
  2112. memdelete(tud);
  2113. Variant::CallError ce;
  2114. const Variant *arg[1] = { &t->userdata };
  2115. int argc = 0;
  2116. if (arg[0]->get_type() != Variant::NIL) {
  2117. // Just pass to the target function whatever came as user data
  2118. argc = 1;
  2119. } else {
  2120. // There are two cases of null user data:
  2121. // a) The target function has zero parameters and the caller is just honoring that.
  2122. // b) The target function has at least one parameter with no default and the caller is
  2123. // leveraging the fact that user data defaults to null in Thread.start().
  2124. // We care about the case of more than one parameter because, even if a thread
  2125. // function can have one at most, out mindset here is to do our best with the
  2126. // only/first one and let the call handle any other error conditions, like too
  2127. // much arguments.
  2128. // We must check if we are in case b).
  2129. int target_param_count = 0;
  2130. int target_default_arg_count = 0;
  2131. Ref<Script> script = t->target_instance->get_script();
  2132. if (script.is_valid()) {
  2133. MethodInfo mi = script->get_method_info(t->target_method);
  2134. target_param_count = mi.arguments.size();
  2135. target_default_arg_count = mi.default_arguments.size();
  2136. } else {
  2137. MethodBind *method = ClassDB::get_method(t->target_instance->get_class_name(), t->target_method);
  2138. target_param_count = method->get_argument_count();
  2139. target_default_arg_count = method->get_default_argument_count();
  2140. }
  2141. if (target_param_count >= 1 && target_default_arg_count < target_param_count) {
  2142. argc = 1;
  2143. }
  2144. }
  2145. Thread::set_name(t->target_method);
  2146. t->ret = t->target_instance->call(t->target_method, arg, argc, ce);
  2147. if (ce.error != Variant::CallError::CALL_OK) {
  2148. String reason;
  2149. switch (ce.error) {
  2150. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2151. reason = "Invalid Argument #" + itos(ce.argument);
  2152. } break;
  2153. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  2154. reason = "Too Many Arguments";
  2155. } break;
  2156. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  2157. reason = "Too Few Arguments";
  2158. } break;
  2159. case Variant::CallError::CALL_ERROR_INVALID_METHOD: {
  2160. reason = "Method Not Found";
  2161. } break;
  2162. default: {
  2163. }
  2164. }
  2165. ERR_FAIL_MSG("Could not call function '" + t->target_method.operator String() + "' to start thread " + t->get_id() + ": " + reason + ".");
  2166. }
  2167. }
  2168. Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {
  2169. ERR_FAIL_COND_V_MSG(active.is_set(), ERR_ALREADY_IN_USE, "Thread already started.");
  2170. ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
  2171. ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
  2172. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  2173. ret = Variant();
  2174. target_method = p_method;
  2175. target_instance = p_instance;
  2176. userdata = p_userdata;
  2177. active.set();
  2178. Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
  2179. Thread::Settings s;
  2180. s.priority = (Thread::Priority)p_priority;
  2181. thread.start(_start_func, ud, s);
  2182. return OK;
  2183. }
  2184. String _Thread::get_id() const {
  2185. return itos(thread.get_id());
  2186. }
  2187. bool _Thread::is_active() const {
  2188. return active.is_set();
  2189. }
  2190. Variant _Thread::wait_to_finish() {
  2191. ERR_FAIL_COND_V_MSG(!active.is_set(), Variant(), "Thread must be active to wait for its completion.");
  2192. thread.wait_to_finish();
  2193. Variant r = ret;
  2194. target_method = StringName();
  2195. target_instance = nullptr;
  2196. userdata = Variant();
  2197. active.clear();
  2198. return r;
  2199. }
  2200. void _Thread::_bind_methods() {
  2201. ClassDB::bind_method(D_METHOD("start", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
  2202. ClassDB::bind_method(D_METHOD("get_id"), &_Thread::get_id);
  2203. ClassDB::bind_method(D_METHOD("is_active"), &_Thread::is_active);
  2204. ClassDB::bind_method(D_METHOD("wait_to_finish"), &_Thread::wait_to_finish);
  2205. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  2206. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  2207. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  2208. }
  2209. _Thread::_Thread() {
  2210. target_instance = nullptr;
  2211. }
  2212. _Thread::~_Thread() {
  2213. ERR_FAIL_COND_MSG(active.is_set(), "Reference to a Thread object was lost while the thread is still running...");
  2214. }
  2215. /////////////////////////////////////
  2216. PoolStringArray _ClassDB::get_class_list() const {
  2217. List<StringName> classes;
  2218. ClassDB::get_class_list(&classes);
  2219. PoolStringArray ret;
  2220. ret.resize(classes.size());
  2221. int idx = 0;
  2222. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  2223. ret.set(idx++, E->get());
  2224. }
  2225. return ret;
  2226. }
  2227. PoolStringArray _ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  2228. List<StringName> classes;
  2229. ClassDB::get_inheriters_from_class(p_class, &classes);
  2230. PoolStringArray ret;
  2231. ret.resize(classes.size());
  2232. int idx = 0;
  2233. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  2234. ret.set(idx++, E->get());
  2235. }
  2236. return ret;
  2237. }
  2238. StringName _ClassDB::get_parent_class(const StringName &p_class) const {
  2239. return ClassDB::get_parent_class(p_class);
  2240. }
  2241. bool _ClassDB::class_exists(const StringName &p_class) const {
  2242. return ClassDB::class_exists(p_class);
  2243. }
  2244. bool _ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  2245. return ClassDB::is_parent_class(p_class, p_inherits);
  2246. }
  2247. bool _ClassDB::can_instance(const StringName &p_class) const {
  2248. return ClassDB::can_instance(p_class);
  2249. }
  2250. Variant _ClassDB::instance(const StringName &p_class) const {
  2251. Object *obj = ClassDB::instance(p_class);
  2252. if (!obj) {
  2253. return Variant();
  2254. }
  2255. Reference *r = Object::cast_to<Reference>(obj);
  2256. if (r) {
  2257. return REF(r);
  2258. } else {
  2259. return obj;
  2260. }
  2261. }
  2262. bool _ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  2263. return ClassDB::has_signal(p_class, p_signal);
  2264. }
  2265. Dictionary _ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  2266. MethodInfo signal;
  2267. if (ClassDB::get_signal(p_class, p_signal, &signal)) {
  2268. return signal.operator Dictionary();
  2269. } else {
  2270. return Dictionary();
  2271. }
  2272. }
  2273. Array _ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  2274. List<MethodInfo> signals;
  2275. ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  2276. Array ret;
  2277. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2278. ret.push_back(E->get().operator Dictionary());
  2279. }
  2280. return ret;
  2281. }
  2282. Array _ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  2283. List<PropertyInfo> plist;
  2284. ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  2285. Array ret;
  2286. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  2287. ret.push_back(E->get().operator Dictionary());
  2288. }
  2289. return ret;
  2290. }
  2291. Variant _ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  2292. Variant ret;
  2293. ClassDB::get_property(p_object, p_property, ret);
  2294. return ret;
  2295. }
  2296. Error _ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  2297. Variant ret;
  2298. bool valid;
  2299. if (!ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  2300. return ERR_UNAVAILABLE;
  2301. } else if (!valid) {
  2302. return ERR_INVALID_DATA;
  2303. }
  2304. return OK;
  2305. }
  2306. bool _ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  2307. return ClassDB::has_method(p_class, p_method, p_no_inheritance);
  2308. }
  2309. Array _ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  2310. List<MethodInfo> methods;
  2311. ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  2312. Array ret;
  2313. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2314. #ifdef DEBUG_METHODS_ENABLED
  2315. ret.push_back(E->get().operator Dictionary());
  2316. #else
  2317. Dictionary dict;
  2318. dict["name"] = E->get().name;
  2319. ret.push_back(dict);
  2320. #endif
  2321. }
  2322. return ret;
  2323. }
  2324. PoolStringArray _ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  2325. List<String> constants;
  2326. ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  2327. PoolStringArray ret;
  2328. ret.resize(constants.size());
  2329. int idx = 0;
  2330. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2331. ret.set(idx++, E->get());
  2332. }
  2333. return ret;
  2334. }
  2335. bool _ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  2336. bool success;
  2337. ClassDB::get_integer_constant(p_class, p_name, &success);
  2338. return success;
  2339. }
  2340. int _ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  2341. bool found;
  2342. int c = ClassDB::get_integer_constant(p_class, p_name, &found);
  2343. ERR_FAIL_COND_V(!found, 0);
  2344. return c;
  2345. }
  2346. StringName _ClassDB::get_category(const StringName &p_node) const {
  2347. return ClassDB::get_category(p_node);
  2348. }
  2349. bool _ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  2350. return ClassDB::has_enum(p_class, p_name, p_no_inheritance);
  2351. }
  2352. PoolStringArray _ClassDB::get_enum_list(const StringName &p_class, bool p_no_inheritance) const {
  2353. List<StringName> enums;
  2354. ClassDB::get_enum_list(p_class, &enums, p_no_inheritance);
  2355. PoolStringArray ret;
  2356. ret.resize(enums.size());
  2357. int idx = 0;
  2358. for (List<StringName>::Element *E = enums.front(); E; E = E->next()) {
  2359. ret.set(idx++, E->get());
  2360. }
  2361. return ret;
  2362. }
  2363. PoolStringArray _ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
  2364. List<StringName> constants;
  2365. ClassDB::get_enum_constants(p_class, p_enum, &constants, p_no_inheritance);
  2366. PoolStringArray ret;
  2367. ret.resize(constants.size());
  2368. int idx = 0;
  2369. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  2370. ret.set(idx++, E->get());
  2371. }
  2372. return ret;
  2373. }
  2374. StringName _ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  2375. return ClassDB::get_integer_constant_enum(p_class, p_name, p_no_inheritance);
  2376. }
  2377. bool _ClassDB::is_class_enabled(StringName p_class) const {
  2378. return ClassDB::is_class_enabled(p_class);
  2379. }
  2380. void _ClassDB::_bind_methods() {
  2381. ClassDB::bind_method(D_METHOD("get_class_list"), &_ClassDB::get_class_list);
  2382. ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &_ClassDB::get_inheriters_from_class);
  2383. ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &_ClassDB::get_parent_class);
  2384. ClassDB::bind_method(D_METHOD("class_exists", "class"), &_ClassDB::class_exists);
  2385. ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &_ClassDB::is_parent_class);
  2386. ClassDB::bind_method(D_METHOD("can_instance", "class"), &_ClassDB::can_instance);
  2387. ClassDB::bind_method(D_METHOD("instance", "class"), &_ClassDB::instance);
  2388. ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &_ClassDB::has_signal);
  2389. ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &_ClassDB::get_signal);
  2390. ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &_ClassDB::get_signal_list, DEFVAL(false));
  2391. ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &_ClassDB::get_property_list, DEFVAL(false));
  2392. ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &_ClassDB::get_property);
  2393. ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &_ClassDB::set_property);
  2394. ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &_ClassDB::has_method, DEFVAL(false));
  2395. ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &_ClassDB::get_method_list, DEFVAL(false));
  2396. ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &_ClassDB::get_integer_constant_list, DEFVAL(false));
  2397. ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &_ClassDB::has_integer_constant);
  2398. ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &_ClassDB::get_integer_constant);
  2399. ClassDB::bind_method(D_METHOD("class_has_enum", "class", "name", "no_inheritance"), &_ClassDB::has_enum, DEFVAL(false));
  2400. ClassDB::bind_method(D_METHOD("class_get_enum_list", "class", "no_inheritance"), &_ClassDB::get_enum_list, DEFVAL(false));
  2401. ClassDB::bind_method(D_METHOD("class_get_enum_constants", "class", "enum", "no_inheritance"), &_ClassDB::get_enum_constants, DEFVAL(false));
  2402. ClassDB::bind_method(D_METHOD("class_get_integer_constant_enum", "class", "name", "no_inheritance"), &_ClassDB::get_integer_constant_enum, DEFVAL(false));
  2403. ClassDB::bind_method(D_METHOD("class_get_category", "class"), &_ClassDB::get_category);
  2404. ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &_ClassDB::is_class_enabled);
  2405. }
  2406. _ClassDB::_ClassDB() {
  2407. }
  2408. _ClassDB::~_ClassDB() {
  2409. }
  2410. ///////////////////////////////
  2411. void _Engine::set_iterations_per_second(int p_ips) {
  2412. Engine::get_singleton()->set_iterations_per_second(p_ips);
  2413. }
  2414. int _Engine::get_iterations_per_second() const {
  2415. return Engine::get_singleton()->get_iterations_per_second();
  2416. }
  2417. void _Engine::set_physics_jitter_fix(float p_threshold) {
  2418. Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  2419. }
  2420. float _Engine::get_physics_jitter_fix() const {
  2421. return Engine::get_singleton()->get_physics_jitter_fix();
  2422. }
  2423. float _Engine::get_physics_interpolation_fraction() const {
  2424. return Engine::get_singleton()->get_physics_interpolation_fraction();
  2425. }
  2426. void _Engine::set_target_fps(int p_fps) {
  2427. Engine::get_singleton()->set_target_fps(p_fps);
  2428. }
  2429. int _Engine::get_target_fps() const {
  2430. return Engine::get_singleton()->get_target_fps();
  2431. }
  2432. float _Engine::get_frames_per_second() const {
  2433. return Engine::get_singleton()->get_frames_per_second();
  2434. }
  2435. uint64_t _Engine::get_physics_frames() const {
  2436. return Engine::get_singleton()->get_physics_frames();
  2437. }
  2438. uint64_t _Engine::get_idle_frames() const {
  2439. return Engine::get_singleton()->get_idle_frames();
  2440. }
  2441. void _Engine::set_time_scale(float p_scale) {
  2442. Engine::get_singleton()->set_time_scale(p_scale);
  2443. }
  2444. float _Engine::get_time_scale() {
  2445. return Engine::get_singleton()->get_time_scale();
  2446. }
  2447. int _Engine::get_frames_drawn() {
  2448. return Engine::get_singleton()->get_frames_drawn();
  2449. }
  2450. MainLoop *_Engine::get_main_loop() const {
  2451. //needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  2452. return OS::get_singleton()->get_main_loop();
  2453. }
  2454. Dictionary _Engine::get_version_info() const {
  2455. return Engine::get_singleton()->get_version_info();
  2456. }
  2457. Dictionary _Engine::get_author_info() const {
  2458. return Engine::get_singleton()->get_author_info();
  2459. }
  2460. Array _Engine::get_copyright_info() const {
  2461. return Engine::get_singleton()->get_copyright_info();
  2462. }
  2463. Dictionary _Engine::get_donor_info() const {
  2464. return Engine::get_singleton()->get_donor_info();
  2465. }
  2466. Dictionary _Engine::get_license_info() const {
  2467. return Engine::get_singleton()->get_license_info();
  2468. }
  2469. String _Engine::get_license_text() const {
  2470. return Engine::get_singleton()->get_license_text();
  2471. }
  2472. bool _Engine::is_in_physics_frame() const {
  2473. return Engine::get_singleton()->is_in_physics_frame();
  2474. }
  2475. bool _Engine::has_singleton(const String &p_name) const {
  2476. return Engine::get_singleton()->has_singleton(p_name);
  2477. }
  2478. Object *_Engine::get_singleton_object(const String &p_name) const {
  2479. return Engine::get_singleton()->get_singleton_object(p_name);
  2480. }
  2481. void _Engine::set_editor_hint(bool p_enabled) {
  2482. Engine::get_singleton()->set_editor_hint(p_enabled);
  2483. }
  2484. bool _Engine::is_editor_hint() const {
  2485. return Engine::get_singleton()->is_editor_hint();
  2486. }
  2487. void _Engine::set_print_error_messages(bool p_enabled) {
  2488. Engine::get_singleton()->set_print_error_messages(p_enabled);
  2489. }
  2490. bool _Engine::is_printing_error_messages() const {
  2491. return Engine::get_singleton()->is_printing_error_messages();
  2492. }
  2493. void _Engine::_bind_methods() {
  2494. ClassDB::bind_method(D_METHOD("set_iterations_per_second", "iterations_per_second"), &_Engine::set_iterations_per_second);
  2495. ClassDB::bind_method(D_METHOD("get_iterations_per_second"), &_Engine::get_iterations_per_second);
  2496. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &_Engine::set_physics_jitter_fix);
  2497. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &_Engine::get_physics_jitter_fix);
  2498. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &_Engine::get_physics_interpolation_fraction);
  2499. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &_Engine::set_target_fps);
  2500. ClassDB::bind_method(D_METHOD("get_target_fps"), &_Engine::get_target_fps);
  2501. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &_Engine::set_time_scale);
  2502. ClassDB::bind_method(D_METHOD("get_time_scale"), &_Engine::get_time_scale);
  2503. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &_Engine::get_frames_drawn);
  2504. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &_Engine::get_frames_per_second);
  2505. ClassDB::bind_method(D_METHOD("get_physics_frames"), &_Engine::get_physics_frames);
  2506. ClassDB::bind_method(D_METHOD("get_idle_frames"), &_Engine::get_idle_frames);
  2507. ClassDB::bind_method(D_METHOD("get_main_loop"), &_Engine::get_main_loop);
  2508. ClassDB::bind_method(D_METHOD("get_version_info"), &_Engine::get_version_info);
  2509. ClassDB::bind_method(D_METHOD("get_author_info"), &_Engine::get_author_info);
  2510. ClassDB::bind_method(D_METHOD("get_copyright_info"), &_Engine::get_copyright_info);
  2511. ClassDB::bind_method(D_METHOD("get_donor_info"), &_Engine::get_donor_info);
  2512. ClassDB::bind_method(D_METHOD("get_license_info"), &_Engine::get_license_info);
  2513. ClassDB::bind_method(D_METHOD("get_license_text"), &_Engine::get_license_text);
  2514. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &_Engine::is_in_physics_frame);
  2515. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &_Engine::has_singleton);
  2516. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &_Engine::get_singleton_object);
  2517. ClassDB::bind_method(D_METHOD("set_editor_hint", "enabled"), &_Engine::set_editor_hint);
  2518. ClassDB::bind_method(D_METHOD("is_editor_hint"), &_Engine::is_editor_hint);
  2519. ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &_Engine::set_print_error_messages);
  2520. ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &_Engine::is_printing_error_messages);
  2521. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_hint"), "set_editor_hint", "is_editor_hint");
  2522. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
  2523. ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations_per_second"), "set_iterations_per_second", "get_iterations_per_second");
  2524. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  2525. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_scale"), "set_time_scale", "get_time_scale");
  2526. ADD_PROPERTY(PropertyInfo(Variant::REAL, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  2527. }
  2528. _Engine *_Engine::singleton = nullptr;
  2529. _Engine::_Engine() {
  2530. singleton = this;
  2531. }
  2532. void JSONParseResult::_bind_methods() {
  2533. ClassDB::bind_method(D_METHOD("get_error"), &JSONParseResult::get_error);
  2534. ClassDB::bind_method(D_METHOD("get_error_string"), &JSONParseResult::get_error_string);
  2535. ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParseResult::get_error_line);
  2536. ClassDB::bind_method(D_METHOD("get_result"), &JSONParseResult::get_result);
  2537. ClassDB::bind_method(D_METHOD("set_error", "error"), &JSONParseResult::set_error);
  2538. ClassDB::bind_method(D_METHOD("set_error_string", "error_string"), &JSONParseResult::set_error_string);
  2539. ClassDB::bind_method(D_METHOD("set_error_line", "error_line"), &JSONParseResult::set_error_line);
  2540. ClassDB::bind_method(D_METHOD("set_result", "result"), &JSONParseResult::set_result);
  2541. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "error", PROPERTY_HINT_NONE, "Error", PROPERTY_USAGE_CLASS_IS_ENUM), "set_error", "get_error");
  2542. ADD_PROPERTY(PropertyInfo(Variant::STRING, "error_string"), "set_error_string", "get_error_string");
  2543. ADD_PROPERTY(PropertyInfo(Variant::INT, "error_line"), "set_error_line", "get_error_line");
  2544. ADD_PROPERTY(PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "set_result", "get_result");
  2545. }
  2546. void JSONParseResult::set_error(Error p_error) {
  2547. error = p_error;
  2548. }
  2549. Error JSONParseResult::get_error() const {
  2550. return error;
  2551. }
  2552. void JSONParseResult::set_error_string(const String &p_error_string) {
  2553. error_string = p_error_string;
  2554. }
  2555. String JSONParseResult::get_error_string() const {
  2556. return error_string;
  2557. }
  2558. void JSONParseResult::set_error_line(int p_error_line) {
  2559. error_line = p_error_line;
  2560. }
  2561. int JSONParseResult::get_error_line() const {
  2562. return error_line;
  2563. }
  2564. void JSONParseResult::set_result(const Variant &p_result) {
  2565. result = p_result;
  2566. }
  2567. Variant JSONParseResult::get_result() const {
  2568. return result;
  2569. }
  2570. void _JSON::_bind_methods() {
  2571. ClassDB::bind_method(D_METHOD("print", "value", "indent", "sort_keys"), &_JSON::print, DEFVAL(String()), DEFVAL(false));
  2572. ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse);
  2573. }
  2574. String _JSON::print(const Variant &p_value, const String &p_indent, bool p_sort_keys) {
  2575. return JSON::print(p_value, p_indent, p_sort_keys);
  2576. }
  2577. Ref<JSONParseResult> _JSON::parse(const String &p_json) {
  2578. Ref<JSONParseResult> result;
  2579. result.instance();
  2580. result->error = JSON::parse(p_json, result->result, result->error_string, result->error_line);
  2581. if (result->error != OK) {
  2582. ERR_PRINT(vformat("Error parsing JSON at line %s: %s", result->error_line, result->error_string));
  2583. }
  2584. return result;
  2585. }
  2586. _JSON *_JSON::singleton = nullptr;
  2587. _JSON::_JSON() {
  2588. singleton = this;
  2589. }