core_bind.cpp 62 KB

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