core_bind.cpp 58 KB

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