core_bind.cpp 73 KB

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