core_bind.cpp 74 KB

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