core_bind.cpp 81 KB

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