2
0

core_bind.cpp 101 KB

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