core_bind.cpp 78 KB

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