core_bind.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  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. _ResourceLoader *_ResourceLoader::singleton=NULL;
  9. Ref<ResourceInteractiveLoader> _ResourceLoader::load_interactive(const String& p_path,const String& p_type_hint) {
  10. return ResourceLoader::load_interactive(p_path,p_type_hint);
  11. }
  12. RES _ResourceLoader::load(const String &p_path,const String& p_type_hint, bool p_no_cache) {
  13. RES ret = ResourceLoader::load(p_path,p_type_hint, p_no_cache);
  14. return ret;
  15. }
  16. DVector<String> _ResourceLoader::get_recognized_extensions_for_type(const String& p_type) {
  17. List<String> exts;
  18. ResourceLoader::get_recognized_extensions_for_type(p_type,&exts);
  19. DVector<String> ret;
  20. for(List<String>::Element *E=exts.front();E;E=E->next()) {
  21. ret.push_back(E->get());
  22. }
  23. return ret;
  24. }
  25. void _ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  26. ResourceLoader::set_abort_on_missing_resources(p_abort);
  27. }
  28. StringArray _ResourceLoader::get_dependencies(const String& p_path) {
  29. List<String> deps;
  30. ResourceLoader::get_dependencies(p_path, &deps);
  31. StringArray ret;
  32. for(List<String>::Element *E=deps.front();E;E=E->next()) {
  33. ret.push_back(E->get());
  34. }
  35. return ret;
  36. };
  37. bool _ResourceLoader::has(const String &p_path) {
  38. String local_path = Globals::get_singleton()->localize_path(p_path);
  39. return ResourceCache::has(local_path);
  40. };
  41. void _ResourceLoader::_bind_methods() {
  42. ObjectTypeDB::bind_method(_MD("load_interactive:ResourceInteractiveLoader","path","type_hint"),&_ResourceLoader::load_interactive,DEFVAL(""));
  43. ObjectTypeDB::bind_method(_MD("load:Resource","path","type_hint", "p_no_cache"),&_ResourceLoader::load,DEFVAL(""), DEFVAL(false));
  44. ObjectTypeDB::bind_method(_MD("get_recognized_extensions_for_type","type"),&_ResourceLoader::get_recognized_extensions_for_type);
  45. ObjectTypeDB::bind_method(_MD("set_abort_on_missing_resources","abort"),&_ResourceLoader::set_abort_on_missing_resources);
  46. ObjectTypeDB::bind_method(_MD("get_dependencies"),&_ResourceLoader::get_dependencies);
  47. ObjectTypeDB::bind_method(_MD("has"),&_ResourceLoader::has);
  48. }
  49. _ResourceLoader::_ResourceLoader() {
  50. singleton=this;
  51. }
  52. Error _ResourceSaver::save(const String &p_path,const RES& p_resource, uint32_t p_flags) {
  53. ERR_FAIL_COND_V(p_resource.is_null(),ERR_INVALID_PARAMETER);
  54. return ResourceSaver::save(p_path,p_resource, p_flags);
  55. }
  56. DVector<String> _ResourceSaver::get_recognized_extensions(const RES& p_resource) {
  57. ERR_FAIL_COND_V(p_resource.is_null(),DVector<String>());
  58. List<String> exts;
  59. ResourceSaver::get_recognized_extensions(p_resource,&exts);
  60. DVector<String> ret;
  61. for(List<String>::Element *E=exts.front();E;E=E->next()) {
  62. ret.push_back(E->get());
  63. }
  64. return ret;
  65. }
  66. _ResourceSaver *_ResourceSaver::singleton=NULL;
  67. void _ResourceSaver::_bind_methods() {
  68. ObjectTypeDB::bind_method(_MD("save","path","resource:Resource"),&_ResourceSaver::save, DEFVAL(0));
  69. ObjectTypeDB::bind_method(_MD("get_recognized_extensions","type"),&_ResourceSaver::get_recognized_extensions);
  70. BIND_CONSTANT(FLAG_RELATIVE_PATHS);
  71. BIND_CONSTANT(FLAG_BUNDLE_RESOURCES);
  72. BIND_CONSTANT(FLAG_CHANGE_PATH);
  73. BIND_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
  74. BIND_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
  75. BIND_CONSTANT(FLAG_COMPRESS);
  76. }
  77. _ResourceSaver::_ResourceSaver() {
  78. singleton=this;
  79. }
  80. /////////////////OS
  81. Point2 _OS::get_mouse_pos() const {
  82. return OS::get_singleton()->get_mouse_pos();
  83. }
  84. void _OS::set_window_title(const String& p_title) {
  85. OS::get_singleton()->set_window_title(p_title);
  86. }
  87. int _OS::get_mouse_button_state() const {
  88. return OS::get_singleton()->get_mouse_button_state();
  89. }
  90. String _OS::get_unique_ID() const {
  91. return OS::get_singleton()->get_unique_ID();
  92. }
  93. bool _OS::has_touchscreen_ui_hint() const {
  94. return OS::get_singleton()->has_touchscreen_ui_hint();
  95. }
  96. void _OS::set_clipboard(const String& p_text) {
  97. OS::get_singleton()->set_clipboard(p_text);
  98. }
  99. String _OS::get_clipboard() const {
  100. return OS::get_singleton()->get_clipboard();
  101. }
  102. void _OS::set_video_mode(const Size2& p_size, bool p_fullscreen,bool p_resizeable,int p_screen) {
  103. OS::VideoMode vm;
  104. vm.width=p_size.width;
  105. vm.height=p_size.height;
  106. vm.fullscreen=p_fullscreen;
  107. vm.resizable=p_resizeable;
  108. OS::get_singleton()->set_video_mode( vm,p_screen);
  109. }
  110. Size2 _OS::get_video_mode(int p_screen) const {
  111. OS::VideoMode vm;
  112. vm = OS::get_singleton()->get_video_mode(p_screen);
  113. return Size2(vm.width,vm.height);
  114. }
  115. bool _OS::is_video_mode_fullscreen(int p_screen) const {
  116. OS::VideoMode vm;
  117. vm = OS::get_singleton()->get_video_mode(p_screen);
  118. return vm.fullscreen;
  119. }
  120. int _OS::get_screen_count() const {
  121. return OS::get_singleton()->get_screen_count();
  122. }
  123. int _OS::get_current_screen() const {
  124. return OS::get_singleton()->get_current_screen();
  125. }
  126. void _OS::set_current_screen(int p_screen) {
  127. OS::get_singleton()->set_current_screen(p_screen);
  128. }
  129. Point2 _OS::get_screen_position(int p_screen) const {
  130. return OS::get_singleton()->get_screen_position(p_screen);
  131. }
  132. Size2 _OS::get_screen_size(int p_screen) const {
  133. return OS::get_singleton()->get_screen_size(p_screen);
  134. }
  135. Point2 _OS::get_window_position() const {
  136. return OS::get_singleton()->get_window_position();
  137. }
  138. void _OS::set_window_position(const Point2& p_position) {
  139. OS::get_singleton()->set_window_position(p_position);
  140. }
  141. Size2 _OS::get_window_size() const {
  142. return OS::get_singleton()->get_window_size();
  143. }
  144. void _OS::set_window_size(const Size2& p_size) {
  145. OS::get_singleton()->set_window_size(p_size);
  146. }
  147. void _OS::set_window_fullscreen(bool p_enabled) {
  148. OS::get_singleton()->set_window_fullscreen(p_enabled);
  149. }
  150. bool _OS::is_window_fullscreen() const {
  151. return OS::get_singleton()->is_window_fullscreen();
  152. }
  153. void _OS::set_window_resizable(bool p_enabled) {
  154. OS::get_singleton()->set_window_resizable(p_enabled);
  155. }
  156. bool _OS::is_window_resizable() const {
  157. return OS::get_singleton()->is_window_resizable();
  158. }
  159. void _OS::set_window_minimized(bool p_enabled) {
  160. OS::get_singleton()->set_window_minimized(p_enabled);
  161. }
  162. bool _OS::is_window_minimized() const {
  163. return OS::get_singleton()->is_window_minimized();
  164. }
  165. void _OS::set_window_maximized(bool p_enabled) {
  166. OS::get_singleton()->set_window_maximized(p_enabled);
  167. }
  168. bool _OS::is_window_maximized() const {
  169. return OS::get_singleton()->is_window_maximized();
  170. }
  171. void _OS::set_use_file_access_save_and_swap(bool p_enable) {
  172. FileAccess::set_backup_save(p_enable);
  173. }
  174. bool _OS::is_video_mode_resizable(int p_screen) const {
  175. OS::VideoMode vm;
  176. vm = OS::get_singleton()->get_video_mode(p_screen);
  177. return vm.resizable;
  178. }
  179. Array _OS::get_fullscreen_mode_list(int p_screen) const {
  180. List<OS::VideoMode> vmlist;
  181. OS::get_singleton()->get_fullscreen_mode_list(&vmlist,p_screen);
  182. Array vmarr;
  183. for(List<OS::VideoMode>::Element *E=vmlist.front();E;E=E->next() ){
  184. vmarr.push_back(Size2(E->get().width,E->get().height));
  185. }
  186. return vmarr;
  187. }
  188. void _OS::set_iterations_per_second(int p_ips) {
  189. OS::get_singleton()->set_iterations_per_second(p_ips);
  190. }
  191. int _OS::get_iterations_per_second() const {
  192. return OS::get_singleton()->get_iterations_per_second();
  193. }
  194. void _OS::set_target_fps(int p_fps) {
  195. OS::get_singleton()->set_target_fps(p_fps);
  196. }
  197. float _OS::get_target_fps() const {
  198. return OS::get_singleton()->get_target_fps();
  199. }
  200. void _OS::set_low_processor_usage_mode(bool p_enabled) {
  201. OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  202. }
  203. bool _OS::is_in_low_processor_usage_mode() const {
  204. return OS::get_singleton()->is_in_low_processor_usage_mode();
  205. }
  206. String _OS::get_executable_path() const {
  207. return OS::get_singleton()->get_executable_path();
  208. }
  209. Error _OS::shell_open(String p_uri) {
  210. return OS::get_singleton()->shell_open(p_uri);
  211. };
  212. int _OS::execute(const String& p_path, const Vector<String> & p_arguments, bool p_blocking, Array p_output) {
  213. OS::ProcessID pid;
  214. List<String> args;
  215. for(int i=0;i<p_arguments.size();i++)
  216. args.push_back(p_arguments[i]);
  217. String pipe;
  218. Error err = OS::get_singleton()->execute(p_path,args,p_blocking,&pid, &pipe);
  219. p_output.clear();
  220. p_output.push_back(pipe);
  221. if (err != OK)
  222. return -1;
  223. else
  224. return pid;
  225. }
  226. Error _OS::kill(int p_pid) {
  227. return OS::get_singleton()->kill(p_pid);
  228. }
  229. int _OS::get_process_ID() const {
  230. return OS::get_singleton()->get_process_ID();
  231. };
  232. bool _OS::has_environment(const String& p_var) const {
  233. return OS::get_singleton()->has_environment(p_var);
  234. }
  235. String _OS::get_environment(const String& p_var) const {
  236. return OS::get_singleton()->get_environment(p_var);
  237. }
  238. String _OS::get_name() const {
  239. return OS::get_singleton()->get_name();
  240. }
  241. Vector<String> _OS::get_cmdline_args() {
  242. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  243. Vector<String> cmdlinev;
  244. for(List<String>::Element *E=cmdline.front();E;E=E->next()) {
  245. cmdlinev.push_back(E->get());
  246. }
  247. return cmdlinev;
  248. }
  249. String _OS::get_locale() const {
  250. return OS::get_singleton()->get_locale();
  251. }
  252. String _OS::get_model_name() const {
  253. return OS::get_singleton()->get_model_name();
  254. }
  255. MainLoop *_OS::get_main_loop() const {
  256. return OS::get_singleton()->get_main_loop();
  257. }
  258. void _OS::set_time_scale(float p_scale) {
  259. OS::get_singleton()->set_time_scale(p_scale);
  260. }
  261. float _OS::get_time_scale() {
  262. return OS::get_singleton()->get_time_scale();
  263. }
  264. bool _OS::is_ok_left_and_cancel_right() const {
  265. return OS::get_singleton()->get_swap_ok_cancel();
  266. }
  267. /*
  268. enum Weekday {
  269. DAY_SUNDAY,
  270. DAY_MONDAY,
  271. DAY_TUESDAY,
  272. DAY_WEDNESDAY,
  273. DAY_THURSDAY,
  274. DAY_FRIDAY,
  275. DAY_SATURDAY
  276. };
  277. enum Month {
  278. MONTH_JANUARY,
  279. MONTH_FEBRUARY,
  280. MONTH_MARCH,
  281. MONTH_APRIL,
  282. MONTH_MAY,
  283. MONTH_JUNE,
  284. MONTH_JULY,
  285. MONTH_AUGUST,
  286. MONTH_SEPTEMBER,
  287. MONTH_OCTOBER,
  288. MONTH_NOVEMBER,
  289. MONTH_DECEMBER
  290. };
  291. */
  292. /*
  293. struct Date {
  294. int year;
  295. Month month;
  296. int day;
  297. Weekday weekday;
  298. bool dst;
  299. };
  300. struct Time {
  301. int hour;
  302. int min;
  303. int sec;
  304. };
  305. */
  306. int _OS::get_static_memory_usage() const {
  307. return OS::get_singleton()->get_static_memory_usage();
  308. }
  309. int _OS::get_static_memory_peak_usage() const {
  310. return OS::get_singleton()->get_static_memory_peak_usage();
  311. }
  312. int _OS::get_dynamic_memory_usage() const{
  313. return OS::get_singleton()->get_dynamic_memory_usage();
  314. }
  315. void _OS::set_icon(const Image& p_icon) {
  316. OS::get_singleton()->set_icon(p_icon);
  317. }
  318. Dictionary _OS::get_date() const {
  319. OS::Date date = OS::get_singleton()->get_date();
  320. Dictionary dated;
  321. dated["year"]=date.year;
  322. dated["month"]=date.month;
  323. dated["day"]=date.day;
  324. dated["weekday"]=date.weekday;
  325. dated["dst"]=date.dst;
  326. return dated;
  327. }
  328. Dictionary _OS::get_time() const {
  329. OS::Time time = OS::get_singleton()->get_time();
  330. Dictionary timed;
  331. timed["hour"]=time.hour;
  332. timed["minute"]=time.min;
  333. timed["second"]=time.sec;
  334. return timed;
  335. }
  336. uint64_t _OS::get_unix_time() const {
  337. return OS::get_singleton()->get_unix_time();
  338. };
  339. void _OS::delay_usec(uint32_t p_usec) const {
  340. OS::get_singleton()->delay_usec(p_usec);
  341. }
  342. void _OS::delay_msec(uint32_t p_msec) const {
  343. OS::get_singleton()->delay_usec(int64_t(p_msec)*1000);
  344. }
  345. uint32_t _OS::get_ticks_msec() const {
  346. return OS::get_singleton()->get_ticks_msec();
  347. }
  348. bool _OS::can_use_threads() const {
  349. return OS::get_singleton()->can_use_threads();
  350. }
  351. bool _OS::can_draw() const {
  352. return OS::get_singleton()->can_draw();
  353. }
  354. int _OS::get_frames_drawn() {
  355. return OS::get_singleton()->get_frames_drawn();
  356. }
  357. int _OS::get_processor_count() const {
  358. return OS::get_singleton()->get_processor_count();
  359. }
  360. bool _OS::is_stdout_verbose() const {
  361. return OS::get_singleton()->is_stdout_verbose();
  362. }
  363. void _OS::dump_memory_to_file(const String& p_file) {
  364. OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
  365. }
  366. struct _OSCoreBindImg {
  367. String path;
  368. Size2 size;
  369. int fmt;
  370. ObjectID id;
  371. int vram;
  372. bool operator<(const _OSCoreBindImg& p_img) const { return vram==p_img.vram ? id<p_img.id : vram > p_img.vram; }
  373. };
  374. void _OS::print_all_textures_by_size() {
  375. List<_OSCoreBindImg> imgs;
  376. int total=0;
  377. {
  378. List<Ref<Resource> > rsrc;
  379. ResourceCache::get_cached_resources(&rsrc);
  380. for (List<Ref<Resource> >::Element *E=rsrc.front();E;E=E->next()) {
  381. if (!E->get()->is_type("ImageTexture"))
  382. continue;
  383. Size2 size = E->get()->call("get_size");
  384. int fmt = E->get()->call("get_format");
  385. _OSCoreBindImg img;
  386. img.size=size;
  387. img.fmt=fmt;
  388. img.path=E->get()->get_path();
  389. img.vram=Image::get_image_data_size(img.size.width,img.size.height,Image::Format(img.fmt));
  390. img.id=E->get()->get_instance_ID();
  391. total+=img.vram;
  392. imgs.push_back(img);
  393. }
  394. }
  395. imgs.sort();
  396. for(List<_OSCoreBindImg>::Element *E=imgs.front();E;E=E->next()) {
  397. print_line(E->get().path+" - "+String::humanize_size(E->get().vram)+" ("+E->get().size+") - total:"+String::humanize_size(total) );
  398. total-=E->get().vram;
  399. }
  400. }
  401. void _OS::print_resources_by_type(const Vector<String>& p_types) {
  402. Map<String,int> type_count;
  403. List<Ref<Resource> > resources;
  404. ResourceCache::get_cached_resources(&resources);
  405. List<Ref<Resource> > rsrc;
  406. ResourceCache::get_cached_resources(&rsrc);
  407. for (List<Ref<Resource> >::Element *E=rsrc.front();E;E=E->next()) {
  408. Ref<Resource> r = E->get();
  409. bool found = false;
  410. for (int i=0; i<p_types.size(); i++) {
  411. if (r->is_type(p_types[i]))
  412. found = true;
  413. }
  414. if (!found)
  415. continue;
  416. if (!type_count.has(r->get_type())) {
  417. type_count[r->get_type()]=0;
  418. }
  419. type_count[r->get_type()]++;
  420. print_line(r->get_type()+": "+r->get_path());
  421. List<String> metas;
  422. r->get_meta_list(&metas);
  423. for (List<String>::Element* me = metas.front(); me; me = me->next()) {
  424. print_line(" "+String(me->get()) + ": " + r->get_meta(me->get()));
  425. };
  426. }
  427. for(Map<String,int>::Element *E=type_count.front();E;E=E->next()) {
  428. print_line(E->key()+" count: "+itos(E->get()));
  429. }
  430. };
  431. void _OS::print_all_resources(const String& p_to_file ) {
  432. OS::get_singleton()->print_all_resources(p_to_file);
  433. }
  434. void _OS::print_resources_in_use(bool p_short) {
  435. OS::get_singleton()->print_resources_in_use(p_short);
  436. }
  437. void _OS::dump_resources_to_file(const String& p_file) {
  438. OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  439. }
  440. String _OS::get_data_dir() const {
  441. return OS::get_singleton()->get_data_dir();
  442. };
  443. float _OS::get_frames_per_second() const {
  444. return OS::get_singleton()->get_frames_per_second();
  445. }
  446. Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  447. return OS::get_singleton()->native_video_play(p_path, p_volume, p_audio_track, p_subtitle_track);
  448. };
  449. bool _OS::native_video_is_playing() {
  450. return OS::get_singleton()->native_video_is_playing();
  451. };
  452. void _OS::native_video_pause() {
  453. OS::get_singleton()->native_video_pause();
  454. };
  455. void _OS::native_video_stop() {
  456. OS::get_singleton()->native_video_stop();
  457. };
  458. bool _OS::is_debug_build() const {
  459. #ifdef DEBUG_ENABLED
  460. return true;
  461. #else
  462. return false;
  463. #endif
  464. }
  465. String _OS::get_system_dir(SystemDir p_dir) const {
  466. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
  467. }
  468. String _OS::get_custom_level() const {
  469. return OS::get_singleton()->get_custom_level();
  470. }
  471. _OS *_OS::singleton=NULL;
  472. void _OS::_bind_methods() {
  473. //ObjectTypeDB::bind_method(_MD("get_mouse_pos"),&_OS::get_mouse_pos);
  474. //ObjectTypeDB::bind_method(_MD("is_mouse_grab_enabled"),&_OS::is_mouse_grab_enabled);
  475. ObjectTypeDB::bind_method(_MD("set_clipboard","clipboard"),&_OS::set_clipboard);
  476. ObjectTypeDB::bind_method(_MD("get_clipboard"),&_OS::get_clipboard);
  477. ObjectTypeDB::bind_method(_MD("set_video_mode","size","fullscreen","resizable","screen"),&_OS::set_video_mode,DEFVAL(0));
  478. ObjectTypeDB::bind_method(_MD("get_video_mode_size","screen"),&_OS::get_video_mode,DEFVAL(0));
  479. ObjectTypeDB::bind_method(_MD("is_video_mode_fullscreen","screen"),&_OS::is_video_mode_fullscreen,DEFVAL(0));
  480. ObjectTypeDB::bind_method(_MD("is_video_mode_resizable","screen"),&_OS::is_video_mode_resizable,DEFVAL(0));
  481. ObjectTypeDB::bind_method(_MD("get_fullscreen_mode_list","screen"),&_OS::get_fullscreen_mode_list,DEFVAL(0));
  482. ObjectTypeDB::bind_method(_MD("get_screen_count"),&_OS::get_screen_count);
  483. ObjectTypeDB::bind_method(_MD("get_current_screen"),&_OS::get_current_screen);
  484. ObjectTypeDB::bind_method(_MD("set_current_screen"),&_OS::set_current_screen);
  485. ObjectTypeDB::bind_method(_MD("get_screen_position"),&_OS::get_screen_position,DEFVAL(0));
  486. ObjectTypeDB::bind_method(_MD("get_screen_size"),&_OS::get_screen_size,DEFVAL(0));
  487. ObjectTypeDB::bind_method(_MD("get_window_position"),&_OS::get_window_position);
  488. ObjectTypeDB::bind_method(_MD("set_window_position"),&_OS::set_window_position);
  489. ObjectTypeDB::bind_method(_MD("get_window_size"),&_OS::get_window_size);
  490. ObjectTypeDB::bind_method(_MD("set_window_size"),&_OS::set_window_size);
  491. ObjectTypeDB::bind_method(_MD("set_window_fullscreen","enabled"),&_OS::set_window_fullscreen);
  492. ObjectTypeDB::bind_method(_MD("is_window_fullscreen"),&_OS::is_window_fullscreen);
  493. ObjectTypeDB::bind_method(_MD("set_window_resizable","enabled"),&_OS::set_window_resizable);
  494. ObjectTypeDB::bind_method(_MD("is_window_resizable"),&_OS::is_window_resizable);
  495. ObjectTypeDB::bind_method(_MD("set_window_minimized", "enabled"),&_OS::set_window_minimized);
  496. ObjectTypeDB::bind_method(_MD("is_window_minimized"),&_OS::is_window_minimized);
  497. ObjectTypeDB::bind_method(_MD("set_window_maximized", "enabled"),&_OS::set_window_maximized);
  498. ObjectTypeDB::bind_method(_MD("is_window_maximized"),&_OS::is_window_maximized);
  499. ObjectTypeDB::bind_method(_MD("set_iterations_per_second","iterations_per_second"),&_OS::set_iterations_per_second);
  500. ObjectTypeDB::bind_method(_MD("get_iterations_per_second"),&_OS::get_iterations_per_second);
  501. ObjectTypeDB::bind_method(_MD("set_target_fps","target_fps"),&_OS::set_target_fps);
  502. ObjectTypeDB::bind_method(_MD("get_target_fps"),&_OS::get_target_fps);
  503. ObjectTypeDB::bind_method(_MD("set_time_scale","time_scale"),&_OS::set_time_scale);
  504. ObjectTypeDB::bind_method(_MD("get_time_scale"),&_OS::get_time_scale);
  505. ObjectTypeDB::bind_method(_MD("has_touchscreen_ui_hint"),&_OS::has_touchscreen_ui_hint);
  506. ObjectTypeDB::bind_method(_MD("set_window_title","title"),&_OS::set_window_title);
  507. ObjectTypeDB::bind_method(_MD("set_low_processor_usage_mode","enable"),&_OS::set_low_processor_usage_mode);
  508. ObjectTypeDB::bind_method(_MD("is_in_low_processor_usage_mode"),&_OS::is_in_low_processor_usage_mode);
  509. ObjectTypeDB::bind_method(_MD("get_processor_count"),&_OS::get_processor_count);
  510. ObjectTypeDB::bind_method(_MD("get_executable_path"),&_OS::get_executable_path);
  511. ObjectTypeDB::bind_method(_MD("execute","path","arguments","blocking","output"),&_OS::execute,DEFVAL(Array()));
  512. ObjectTypeDB::bind_method(_MD("kill","pid"),&_OS::kill);
  513. ObjectTypeDB::bind_method(_MD("shell_open","uri"),&_OS::shell_open);
  514. ObjectTypeDB::bind_method(_MD("get_process_ID"),&_OS::get_process_ID);
  515. ObjectTypeDB::bind_method(_MD("get_environment","environment"),&_OS::get_environment);
  516. ObjectTypeDB::bind_method(_MD("has_environment","environment"),&_OS::has_environment);
  517. ObjectTypeDB::bind_method(_MD("get_name"),&_OS::get_name);
  518. ObjectTypeDB::bind_method(_MD("get_cmdline_args"),&_OS::get_cmdline_args);
  519. ObjectTypeDB::bind_method(_MD("get_main_loop"),&_OS::get_main_loop);
  520. ObjectTypeDB::bind_method(_MD("get_date"),&_OS::get_date);
  521. ObjectTypeDB::bind_method(_MD("get_time"),&_OS::get_time);
  522. ObjectTypeDB::bind_method(_MD("get_unix_time"),&_OS::get_unix_time);
  523. ObjectTypeDB::bind_method(_MD("set_icon"),&_OS::set_icon);
  524. ObjectTypeDB::bind_method(_MD("delay_usec","usec"),&_OS::delay_usec);
  525. ObjectTypeDB::bind_method(_MD("delay_msec","msec"),&_OS::delay_msec);
  526. ObjectTypeDB::bind_method(_MD("get_ticks_msec"),&_OS::get_ticks_msec);
  527. ObjectTypeDB::bind_method(_MD("get_locale"),&_OS::get_locale);
  528. ObjectTypeDB::bind_method(_MD("get_model_name"),&_OS::get_model_name);
  529. ObjectTypeDB::bind_method(_MD("get_custom_level"),&_OS::get_custom_level);
  530. ObjectTypeDB::bind_method(_MD("can_draw"),&_OS::can_draw);
  531. ObjectTypeDB::bind_method(_MD("get_frames_drawn"),&_OS::get_frames_drawn);
  532. ObjectTypeDB::bind_method(_MD("is_stdout_verbose"),&_OS::is_stdout_verbose);
  533. ObjectTypeDB::bind_method(_MD("can_use_threads"),&_OS::can_use_threads);
  534. ObjectTypeDB::bind_method(_MD("is_debug_build"),&_OS::is_debug_build);
  535. //ObjectTypeDB::bind_method(_MD("get_mouse_button_state"),&_OS::get_mouse_button_state);
  536. ObjectTypeDB::bind_method(_MD("dump_memory_to_file","file"),&_OS::dump_memory_to_file);
  537. ObjectTypeDB::bind_method(_MD("dump_resources_to_file","file"),&_OS::dump_resources_to_file);
  538. ObjectTypeDB::bind_method(_MD("print_resources_in_use","short"),&_OS::print_resources_in_use,DEFVAL(false));
  539. ObjectTypeDB::bind_method(_MD("print_all_resources","tofile"),&_OS::print_all_resources,DEFVAL(""));
  540. ObjectTypeDB::bind_method(_MD("get_static_memory_usage"),&_OS::get_static_memory_usage);
  541. ObjectTypeDB::bind_method(_MD("get_static_memory_peak_usage"),&_OS::get_static_memory_peak_usage);
  542. ObjectTypeDB::bind_method(_MD("get_dynamic_memory_usage"),&_OS::get_dynamic_memory_usage);
  543. ObjectTypeDB::bind_method(_MD("get_data_dir"),&_OS::get_data_dir);
  544. ObjectTypeDB::bind_method(_MD("get_system_dir","dir"),&_OS::get_system_dir);
  545. ObjectTypeDB::bind_method(_MD("get_unique_ID"),&_OS::get_unique_ID);
  546. ObjectTypeDB::bind_method(_MD("is_ok_left_and_cancel_right"),&_OS::is_ok_left_and_cancel_right);
  547. ObjectTypeDB::bind_method(_MD("get_frames_per_second"),&_OS::get_frames_per_second);
  548. ObjectTypeDB::bind_method(_MD("print_all_textures_by_size"),&_OS::print_all_textures_by_size);
  549. ObjectTypeDB::bind_method(_MD("print_resources_by_type"),&_OS::print_resources_by_type);
  550. ObjectTypeDB::bind_method(_MD("native_video_play"),&_OS::native_video_play);
  551. ObjectTypeDB::bind_method(_MD("native_video_is_playing"),&_OS::native_video_is_playing);
  552. ObjectTypeDB::bind_method(_MD("native_video_stop"),&_OS::native_video_stop);
  553. ObjectTypeDB::bind_method(_MD("native_video_pause"),&_OS::native_video_pause);
  554. ObjectTypeDB::bind_method(_MD("set_use_file_access_save_and_swap","enabled"),&_OS::set_use_file_access_save_and_swap);
  555. BIND_CONSTANT( DAY_SUNDAY );
  556. BIND_CONSTANT( DAY_MONDAY );
  557. BIND_CONSTANT( DAY_TUESDAY );
  558. BIND_CONSTANT( DAY_WEDNESDAY );
  559. BIND_CONSTANT( DAY_THURSDAY );
  560. BIND_CONSTANT( DAY_FRIDAY );
  561. BIND_CONSTANT( DAY_SATURDAY );
  562. BIND_CONSTANT( MONTH_JANUARY );
  563. BIND_CONSTANT( MONTH_FEBRUARY );
  564. BIND_CONSTANT( MONTH_MARCH );
  565. BIND_CONSTANT( MONTH_APRIL );
  566. BIND_CONSTANT( MONTH_MAY );
  567. BIND_CONSTANT( MONTH_JUNE );
  568. BIND_CONSTANT( MONTH_JULY );
  569. BIND_CONSTANT( MONTH_AUGUST );
  570. BIND_CONSTANT( MONTH_SEPTEMBER );
  571. BIND_CONSTANT( MONTH_OCTOBER );
  572. BIND_CONSTANT( MONTH_NOVEMBER );
  573. BIND_CONSTANT( MONTH_DECEMBER );
  574. BIND_CONSTANT( SYSTEM_DIR_DESKTOP);
  575. BIND_CONSTANT( SYSTEM_DIR_DCIM );
  576. BIND_CONSTANT( SYSTEM_DIR_DOCUMENTS );
  577. BIND_CONSTANT( SYSTEM_DIR_DOWNLOADS );
  578. BIND_CONSTANT( SYSTEM_DIR_MOVIES );
  579. BIND_CONSTANT( SYSTEM_DIR_MUSIC );
  580. BIND_CONSTANT( SYSTEM_DIR_PICTURES );
  581. BIND_CONSTANT( SYSTEM_DIR_RINGTONES );
  582. }
  583. _OS::_OS() {
  584. singleton=this;
  585. }
  586. ///////////////////// GEOMETRY
  587. _Geometry *_Geometry::singleton=NULL;
  588. _Geometry *_Geometry::get_singleton() {
  589. return singleton;
  590. }
  591. DVector<Plane> _Geometry::build_box_planes(const Vector3& p_extents) {
  592. return Geometry::build_box_planes(p_extents);
  593. }
  594. DVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  595. return Geometry::build_cylinder_planes(p_radius,p_height,p_sides,p_axis);
  596. }
  597. DVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  598. return Geometry::build_capsule_planes(p_radius,p_height,p_sides,p_lats,p_axis);
  599. }
  600. real_t _Geometry::segment_intersects_circle(const Vector2& p_from, const Vector2& p_to, const Vector2& p_circle_pos, real_t p_circle_radius) {
  601. return Geometry::segment_intersects_circle(p_from,p_to,p_circle_pos,p_circle_radius);
  602. }
  603. 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) {
  604. Vector2 result;
  605. if (Geometry::segment_intersects_segment_2d(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  606. return result;
  607. } else {
  608. return Variant();
  609. };
  610. };
  611. DVector<Vector2> _Geometry::get_closest_points_between_segments_2d( const Vector2& p1,const Vector2& q1, const Vector2& p2,const Vector2& q2) {
  612. Vector2 r1, r2;
  613. Geometry::get_closest_points_between_segments(p1,q1,p2,q2,r1,r2);
  614. DVector<Vector2> r;
  615. r.resize(2);
  616. r.set(0,r1);
  617. r.set(1,r2);
  618. return r;
  619. }
  620. DVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3& p1,const Vector3& p2,const Vector3& q1,const Vector3& q2) {
  621. Vector3 r1, r2;
  622. Geometry::get_closest_points_between_segments(p1,p2,q1,q2,r1,r2);
  623. DVector<Vector3> r;
  624. r.resize(2);
  625. r.set(0,r1);
  626. r.set(1,r2);
  627. return r;
  628. }
  629. Vector3 _Geometry::get_closest_point_to_segment(const Vector3& p_point, const Vector3& p_a,const Vector3& p_b) {
  630. Vector3 s[2]={p_a,p_b};
  631. return Geometry::get_closest_point_to_segment(p_point,s);
  632. }
  633. 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) {
  634. Vector3 res;
  635. if (Geometry::ray_intersects_triangle(p_from,p_dir,p_v0,p_v1,p_v2,&res))
  636. return res;
  637. else
  638. return Variant();
  639. }
  640. 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) {
  641. Vector3 res;
  642. if (Geometry::segment_intersects_triangle(p_from,p_to,p_v0,p_v1,p_v2,&res))
  643. return res;
  644. else
  645. return Variant();
  646. }
  647. bool _Geometry::point_is_inside_triangle(const Vector2& s, const Vector2& a, const Vector2& b, const Vector2& c) const {
  648. return Geometry::is_point_in_triangle(s,a,b,c);
  649. }
  650. DVector<Vector3> _Geometry::segment_intersects_sphere( const Vector3& p_from, const Vector3& p_to, const Vector3& p_sphere_pos,real_t p_sphere_radius) {
  651. DVector<Vector3> r;
  652. Vector3 res,norm;
  653. if (!Geometry::segment_intersects_sphere(p_from,p_to,p_sphere_pos,p_sphere_radius,&res,&norm))
  654. return r;
  655. r.resize(2);
  656. r.set(0,res);
  657. r.set(1,norm);
  658. return r;
  659. }
  660. DVector<Vector3> _Geometry::segment_intersects_cylinder( const Vector3& p_from, const Vector3& p_to, float p_height,float p_radius) {
  661. DVector<Vector3> r;
  662. Vector3 res,norm;
  663. if (!Geometry::segment_intersects_cylinder(p_from,p_to,p_height,p_radius,&res,&norm))
  664. return r;
  665. r.resize(2);
  666. r.set(0,res);
  667. r.set(1,norm);
  668. return r;
  669. }
  670. DVector<Vector3> _Geometry::segment_intersects_convex(const Vector3& p_from, const Vector3& p_to,const Vector<Plane>& p_planes) {
  671. DVector<Vector3> r;
  672. Vector3 res,norm;
  673. if (!Geometry::segment_intersects_convex(p_from,p_to,p_planes.ptr(),p_planes.size(),&res,&norm))
  674. return r;
  675. r.resize(2);
  676. r.set(0,res);
  677. r.set(1,norm);
  678. return r;
  679. }
  680. Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2>& p_polygon) {
  681. return Geometry::triangulate_polygon(p_polygon);
  682. }
  683. Dictionary _Geometry::make_atlas(const Vector<Size2>& p_rects) {
  684. Dictionary ret;
  685. Vector<Size2i> rects;
  686. for (int i=0; i<p_rects.size(); i++) {
  687. rects.push_back(p_rects[i]);
  688. };
  689. Vector<Point2i> result;
  690. Size2i size;
  691. Geometry::make_atlas(rects, result, size);
  692. Size2 r_size = size;
  693. Vector<Point2> r_result;
  694. for (int i=0; i<result.size(); i++) {
  695. r_result.push_back(result[i]);
  696. };
  697. ret["points"] = r_result;
  698. ret["size"] = r_size;
  699. return ret;
  700. };
  701. int _Geometry::get_uv84_normal_bit(const Vector3& p_vector) {
  702. return Geometry::get_uv84_normal_bit(p_vector);
  703. }
  704. void _Geometry::_bind_methods() {
  705. ObjectTypeDB::bind_method(_MD("build_box_planes","extents"),&_Geometry::build_box_planes);
  706. ObjectTypeDB::bind_method(_MD("build_cylinder_planes","radius","height","sides","axis"),&_Geometry::build_cylinder_planes,DEFVAL(Vector3::AXIS_Z));
  707. ObjectTypeDB::bind_method(_MD("build_capsule_planes","radius","height","sides","lats","axis"),&_Geometry::build_capsule_planes,DEFVAL(Vector3::AXIS_Z));
  708. ObjectTypeDB::bind_method(_MD("segment_intersects_circle","segment_from","segment_to","circle_pos","circle_radius"),&_Geometry::segment_intersects_circle);
  709. ObjectTypeDB::bind_method(_MD("segment_intersects_segment_2d","from_a","to_a","from_b","to_b"),&_Geometry::segment_intersects_segment_2d);
  710. ObjectTypeDB::bind_method(_MD("get_closest_points_between_segments_2d","p1","q1","p2","q2"),&_Geometry::get_closest_points_between_segments_2d);
  711. ObjectTypeDB::bind_method(_MD("get_closest_points_between_segments","p1","p2","q1","q2"),&_Geometry::get_closest_points_between_segments);
  712. ObjectTypeDB::bind_method(_MD("get_closest_point_to_segment","point","s1","s2"),&_Geometry::get_closest_point_to_segment);
  713. ObjectTypeDB::bind_method(_MD("get_uv84_normal_bit","normal"),&_Geometry::get_uv84_normal_bit);
  714. ObjectTypeDB::bind_method(_MD("ray_intersects_triangle","from","dir","a","b","c"),&_Geometry::ray_intersects_triangle);
  715. ObjectTypeDB::bind_method(_MD("segment_intersects_triangle","from","to","a","b","c"),&_Geometry::segment_intersects_triangle);
  716. ObjectTypeDB::bind_method(_MD("segment_intersects_sphere","from","to","spos","sradius"),&_Geometry::segment_intersects_sphere);
  717. ObjectTypeDB::bind_method(_MD("segment_intersects_cylinder","from","to","height","radius"),&_Geometry::segment_intersects_cylinder);
  718. ObjectTypeDB::bind_method(_MD("segment_intersects_convex","from","to","planes"),&_Geometry::segment_intersects_convex);
  719. ObjectTypeDB::bind_method(_MD("point_is_inside_triangle","point","a","b","c"),&_Geometry::point_is_inside_triangle);
  720. ObjectTypeDB::bind_method(_MD("triangulate_polygon","polygon"),&_Geometry::triangulate_polygon);
  721. ObjectTypeDB::bind_method(_MD("make_atlas","sizes"),&_Geometry::make_atlas);
  722. }
  723. _Geometry::_Geometry() {
  724. singleton=this;
  725. }
  726. ///////////////////////// FILE
  727. Error _File::open_encrypted(const String& p_path, int p_mode_flags,const Vector<uint8_t>& p_key) {
  728. Error err = open(p_path,p_mode_flags);
  729. if (err)
  730. return err;
  731. FileAccessEncrypted *fae = memnew( FileAccessEncrypted );
  732. err = fae->open_and_parse(f,p_key,(p_mode_flags==WRITE)?FileAccessEncrypted::MODE_WRITE_AES256:FileAccessEncrypted::MODE_READ);
  733. if (err) {
  734. memdelete(fae);
  735. close();
  736. return err;
  737. }
  738. f=fae;
  739. return OK;
  740. }
  741. Error _File::open_encrypted_pass(const String& p_path, int p_mode_flags,const String& p_pass) {
  742. Error err = open(p_path,p_mode_flags);
  743. if (err)
  744. return err;
  745. FileAccessEncrypted *fae = memnew( FileAccessEncrypted );
  746. err = fae->open_and_parse_password(f,p_pass,(p_mode_flags==WRITE)?FileAccessEncrypted::MODE_WRITE_AES256:FileAccessEncrypted::MODE_READ);
  747. if (err) {
  748. memdelete(fae);
  749. close();
  750. return err;
  751. }
  752. f=fae;
  753. return OK;
  754. }
  755. Error _File::open(const String& p_path, int p_mode_flags) {
  756. close();
  757. Error err;
  758. f = FileAccess::open(p_path,p_mode_flags,&err);
  759. if (f)
  760. f->set_endian_swap(eswap);
  761. return err;
  762. }
  763. void _File::close(){
  764. if (f)
  765. memdelete(f);
  766. f=NULL;
  767. }
  768. bool _File::is_open() const{
  769. return f!=NULL;
  770. }
  771. void _File::seek(int64_t p_position){
  772. ERR_FAIL_COND(!f);
  773. f->seek(p_position);
  774. }
  775. void _File::seek_end(int64_t p_position){
  776. ERR_FAIL_COND(!f);
  777. f->seek_end(p_position);
  778. }
  779. int64_t _File::get_pos() const{
  780. ERR_FAIL_COND_V(!f,0);
  781. return f->get_pos();
  782. }
  783. int64_t _File::get_len() const{
  784. ERR_FAIL_COND_V(!f,0);
  785. return f->get_len();
  786. }
  787. bool _File::eof_reached() const{
  788. ERR_FAIL_COND_V(!f,false);
  789. return f->eof_reached();
  790. }
  791. uint8_t _File::get_8() const{
  792. ERR_FAIL_COND_V(!f,0);
  793. return f->get_8();
  794. }
  795. uint16_t _File::get_16() const{
  796. ERR_FAIL_COND_V(!f,0);
  797. return f->get_16();
  798. }
  799. uint32_t _File::get_32() const{
  800. ERR_FAIL_COND_V(!f,0);
  801. return f->get_32();
  802. }
  803. uint64_t _File::get_64() const{
  804. ERR_FAIL_COND_V(!f,0);
  805. return f->get_64();
  806. }
  807. float _File::get_float() const{
  808. ERR_FAIL_COND_V(!f,0);
  809. return f->get_float();
  810. }
  811. double _File::get_double() const{
  812. ERR_FAIL_COND_V(!f,0);
  813. return f->get_double();
  814. }
  815. real_t _File::get_real() const{
  816. ERR_FAIL_COND_V(!f,0);
  817. return f->get_real();
  818. }
  819. DVector<uint8_t> _File::get_buffer(int p_length) const{
  820. DVector<uint8_t> data;
  821. ERR_FAIL_COND_V(!f,data);
  822. ERR_FAIL_COND_V(p_length<0,data);
  823. if (p_length==0)
  824. return data;
  825. Error err = data.resize(p_length);
  826. ERR_FAIL_COND_V(err!=OK,data);
  827. DVector<uint8_t>::Write w = data.write();
  828. int len = f->get_buffer(&w[0],p_length);
  829. ERR_FAIL_COND_V( len < 0 , DVector<uint8_t>());
  830. w = DVector<uint8_t>::Write();
  831. if (len < p_length)
  832. data.resize(p_length);
  833. return data;
  834. }
  835. String _File::get_as_text() const {
  836. String text;
  837. String l = get_line();
  838. while(!eof_reached()) {
  839. text+=l+"\n";
  840. l = get_line();
  841. }
  842. text+=l;
  843. return text;
  844. }
  845. String _File::get_line() const{
  846. ERR_FAIL_COND_V(!f,String());
  847. return f->get_line();
  848. }
  849. Vector<String> _File::get_csv_line() const {
  850. ERR_FAIL_COND_V(!f,Vector<String>());
  851. return f->get_csv_line();
  852. }
  853. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  854. * It's not about the current CPU type but file formats.
  855. * this flags get reset to false (little endian) on each open
  856. */
  857. void _File::set_endian_swap(bool p_swap){
  858. eswap=p_swap;
  859. if (f)
  860. f->set_endian_swap(p_swap);
  861. }
  862. bool _File::get_endian_swap(){
  863. return eswap;
  864. }
  865. Error _File::get_error() const{
  866. if (!f)
  867. return ERR_UNCONFIGURED;
  868. return f->get_error();
  869. }
  870. void _File::store_8(uint8_t p_dest){
  871. ERR_FAIL_COND(!f);
  872. f->store_8(p_dest);
  873. }
  874. void _File::store_16(uint16_t p_dest){
  875. ERR_FAIL_COND(!f);
  876. f->store_16(p_dest);
  877. }
  878. void _File::store_32(uint32_t p_dest){
  879. ERR_FAIL_COND(!f);
  880. f->store_32(p_dest);
  881. }
  882. void _File::store_64(uint64_t p_dest){
  883. ERR_FAIL_COND(!f);
  884. f->store_64(p_dest);
  885. }
  886. void _File::store_float(float p_dest){
  887. ERR_FAIL_COND(!f);
  888. f->store_float(p_dest);
  889. }
  890. void _File::store_double(double p_dest){
  891. ERR_FAIL_COND(!f);
  892. f->store_double(p_dest);
  893. }
  894. void _File::store_real(real_t p_real){
  895. ERR_FAIL_COND(!f);
  896. f->store_real(p_real);
  897. }
  898. void _File::store_string(const String& p_string){
  899. ERR_FAIL_COND(!f);
  900. f->store_string(p_string);
  901. }
  902. void _File::store_pascal_string(const String& p_string) {
  903. ERR_FAIL_COND(!f);
  904. f->store_pascal_string(p_string);
  905. };
  906. String _File::get_pascal_string() {
  907. ERR_FAIL_COND_V(!f, "");
  908. return f->get_pascal_string();
  909. };
  910. void _File::store_line(const String& p_string){
  911. ERR_FAIL_COND(!f);
  912. f->store_line(p_string);
  913. }
  914. void _File::store_buffer(const DVector<uint8_t>& p_buffer){
  915. ERR_FAIL_COND(!f);
  916. int len = p_buffer.size();
  917. if (len==0)
  918. return;
  919. DVector<uint8_t>::Read r = p_buffer.read();
  920. f->store_buffer(&r[0],len);
  921. }
  922. bool _File::file_exists(const String& p_name) const{
  923. return FileAccess::exists(p_name);
  924. }
  925. void _File::store_var(const Variant& p_var) {
  926. ERR_FAIL_COND(!f);
  927. int len;
  928. Error err = encode_variant(p_var,NULL,len);
  929. ERR_FAIL_COND( err != OK );
  930. DVector<uint8_t> buff;
  931. buff.resize(len);
  932. DVector<uint8_t>::Write w = buff.write();
  933. err = encode_variant(p_var,&w[0],len);
  934. ERR_FAIL_COND( err != OK );
  935. w=DVector<uint8_t>::Write();
  936. store_32(len);
  937. store_buffer(buff);
  938. }
  939. Variant _File::get_var() const {
  940. ERR_FAIL_COND_V(!f,Variant());
  941. uint32_t len = get_32();
  942. DVector<uint8_t> buff = get_buffer(len);
  943. ERR_FAIL_COND_V(buff.size() != len, Variant());
  944. DVector<uint8_t>::Read r = buff.read();
  945. Variant v;
  946. Error err = decode_variant(v,&r[0],len);
  947. ERR_FAIL_COND_V( err!=OK, Variant() );
  948. return v;
  949. }
  950. void _File::_bind_methods() {
  951. ObjectTypeDB::bind_method(_MD("open_encrypted","path","mode_flags","key"),&_File::open_encrypted);
  952. ObjectTypeDB::bind_method(_MD("open_encrypted_with_pass","path","mode_flags","pass"),&_File::open_encrypted_pass);
  953. ObjectTypeDB::bind_method(_MD("open","path","flags"),&_File::open);
  954. ObjectTypeDB::bind_method(_MD("close"),&_File::close);
  955. ObjectTypeDB::bind_method(_MD("is_open"),&_File::is_open);
  956. ObjectTypeDB::bind_method(_MD("seek","pos"),&_File::seek);
  957. ObjectTypeDB::bind_method(_MD("seek_end","pos"),&_File::seek_end,DEFVAL(0));
  958. ObjectTypeDB::bind_method(_MD("get_pos"),&_File::get_pos);
  959. ObjectTypeDB::bind_method(_MD("get_len"),&_File::get_len);
  960. ObjectTypeDB::bind_method(_MD("eof_reached"),&_File::eof_reached);
  961. ObjectTypeDB::bind_method(_MD("get_8"),&_File::get_8);
  962. ObjectTypeDB::bind_method(_MD("get_16"),&_File::get_16);
  963. ObjectTypeDB::bind_method(_MD("get_32"),&_File::get_32);
  964. ObjectTypeDB::bind_method(_MD("get_64"),&_File::get_64);
  965. ObjectTypeDB::bind_method(_MD("get_float"),&_File::get_float);
  966. ObjectTypeDB::bind_method(_MD("get_double"),&_File::get_double);
  967. ObjectTypeDB::bind_method(_MD("get_real"),&_File::get_real);
  968. ObjectTypeDB::bind_method(_MD("get_buffer","len"),&_File::get_buffer);
  969. ObjectTypeDB::bind_method(_MD("get_line"),&_File::get_line);
  970. ObjectTypeDB::bind_method(_MD("get_as_text"),&_File::get_as_text);
  971. ObjectTypeDB::bind_method(_MD("get_endian_swap"),&_File::get_endian_swap);
  972. ObjectTypeDB::bind_method(_MD("set_endian_swap","enable"),&_File::set_endian_swap);
  973. ObjectTypeDB::bind_method(_MD("get_error:Error"),&_File::get_error);
  974. ObjectTypeDB::bind_method(_MD("get_var"),&_File::get_var);
  975. ObjectTypeDB::bind_method(_MD("get_csv_line"),&_File::get_csv_line);
  976. ObjectTypeDB::bind_method(_MD("store_8","value"),&_File::store_8);
  977. ObjectTypeDB::bind_method(_MD("store_16","value"),&_File::store_16);
  978. ObjectTypeDB::bind_method(_MD("store_32","value"),&_File::store_32);
  979. ObjectTypeDB::bind_method(_MD("store_64","value"),&_File::store_64);
  980. ObjectTypeDB::bind_method(_MD("store_float","value"),&_File::store_float);
  981. ObjectTypeDB::bind_method(_MD("store_double","value"),&_File::store_double);
  982. ObjectTypeDB::bind_method(_MD("store_real","value"),&_File::store_real);
  983. ObjectTypeDB::bind_method(_MD("store_buffer","buffer"),&_File::store_buffer);
  984. ObjectTypeDB::bind_method(_MD("store_line","line"),&_File::store_line);
  985. ObjectTypeDB::bind_method(_MD("store_string","string"),&_File::store_string);
  986. ObjectTypeDB::bind_method(_MD("store_var","value"),&_File::store_var);
  987. ObjectTypeDB::bind_method(_MD("store_pascal_string","string"),&_File::store_pascal_string);
  988. ObjectTypeDB::bind_method(_MD("get_pascal_string"),&_File::get_pascal_string);
  989. ObjectTypeDB::bind_method(_MD("file_exists","path"),&_File::file_exists);
  990. BIND_CONSTANT( READ );
  991. BIND_CONSTANT( WRITE );
  992. BIND_CONSTANT( READ_WRITE );
  993. }
  994. _File::_File(){
  995. f = NULL;
  996. eswap=false;
  997. }
  998. _File::~_File(){
  999. if (f)
  1000. memdelete(f);
  1001. }
  1002. ///////////////////////////////////////////////////////
  1003. Error _Directory::open(const String& p_path) {
  1004. Error err;
  1005. DirAccess *alt=DirAccess::open(p_path,&err);
  1006. if (!alt)
  1007. return err;
  1008. if (d)
  1009. memdelete(d);
  1010. d=alt;
  1011. return OK;
  1012. }
  1013. bool _Directory::list_dir_begin() {
  1014. ERR_FAIL_COND_V(!d,false);
  1015. return d->list_dir_begin();
  1016. }
  1017. String _Directory::get_next(){
  1018. ERR_FAIL_COND_V(!d,"");
  1019. return d->get_next();
  1020. }
  1021. bool _Directory::current_is_dir() const{
  1022. ERR_FAIL_COND_V(!d,false);
  1023. return d->current_is_dir();
  1024. }
  1025. void _Directory::list_dir_end(){
  1026. ERR_FAIL_COND(!d);
  1027. return d->list_dir_end();
  1028. }
  1029. int _Directory::get_drive_count(){
  1030. ERR_FAIL_COND_V(!d,0);
  1031. return d->get_drive_count();
  1032. }
  1033. String _Directory::get_drive(int p_drive){
  1034. ERR_FAIL_COND_V(!d,"");
  1035. return d->get_drive(p_drive);
  1036. }
  1037. Error _Directory::change_dir(String p_dir){
  1038. ERR_FAIL_COND_V(!d,ERR_UNCONFIGURED);
  1039. return d->change_dir(p_dir);
  1040. }
  1041. String _Directory::get_current_dir() {
  1042. ERR_FAIL_COND_V(!d,"");
  1043. return d->get_current_dir();
  1044. }
  1045. Error _Directory::make_dir(String p_dir){
  1046. ERR_FAIL_COND_V(!d,ERR_UNCONFIGURED);
  1047. return d->make_dir(p_dir);
  1048. }
  1049. Error _Directory::make_dir_recursive(String p_dir){
  1050. ERR_FAIL_COND_V(!d,ERR_UNCONFIGURED);
  1051. return d->make_dir_recursive(p_dir);
  1052. }
  1053. bool _Directory::file_exists(String p_file){
  1054. ERR_FAIL_COND_V(!d,false);
  1055. return d->file_exists(p_file);
  1056. }
  1057. bool _Directory::dir_exists(String p_dir) {
  1058. ERR_FAIL_COND_V(!d,false);
  1059. return d->dir_exists(p_dir);
  1060. }
  1061. int _Directory::get_space_left(){
  1062. ERR_FAIL_COND_V(!d,0);
  1063. return d->get_space_left();
  1064. }
  1065. Error _Directory::copy(String p_from,String p_to){
  1066. ERR_FAIL_COND_V(!d,ERR_UNCONFIGURED);
  1067. return d->copy(p_from,p_to);
  1068. }
  1069. Error _Directory::rename(String p_from, String p_to){
  1070. ERR_FAIL_COND_V(!d,ERR_UNCONFIGURED);
  1071. return d->rename(p_from,p_to);
  1072. }
  1073. Error _Directory::remove(String p_name){
  1074. ERR_FAIL_COND_V(!d,ERR_UNCONFIGURED);
  1075. return d->remove(p_name);
  1076. }
  1077. void _Directory::_bind_methods() {
  1078. ObjectTypeDB::bind_method(_MD("open:Error","path"),&_Directory::open);
  1079. ObjectTypeDB::bind_method(_MD("list_dir_begin"),&_Directory::list_dir_begin);
  1080. ObjectTypeDB::bind_method(_MD("get_next"),&_Directory::get_next);
  1081. ObjectTypeDB::bind_method(_MD("current_is_dir"),&_Directory::current_is_dir);
  1082. ObjectTypeDB::bind_method(_MD("list_dir_end"),&_Directory::list_dir_end);
  1083. ObjectTypeDB::bind_method(_MD("get_drive_count"),&_Directory::get_drive_count);
  1084. ObjectTypeDB::bind_method(_MD("get_drive","idx"),&_Directory::get_drive);
  1085. ObjectTypeDB::bind_method(_MD("change_dir:Error","todir"),&_Directory::change_dir);
  1086. ObjectTypeDB::bind_method(_MD("get_current_dir"),&_Directory::get_current_dir);
  1087. ObjectTypeDB::bind_method(_MD("make_dir:Error","name"),&_Directory::make_dir);
  1088. ObjectTypeDB::bind_method(_MD("make_dir_recursive:Error","name"),&_Directory::make_dir_recursive);
  1089. ObjectTypeDB::bind_method(_MD("file_exists","name"),&_Directory::file_exists);
  1090. ObjectTypeDB::bind_method(_MD("dir_exists","name"),&_Directory::dir_exists);
  1091. // ObjectTypeDB::bind_method(_MD("get_modified_time","file"),&_Directory::get_modified_time);
  1092. ObjectTypeDB::bind_method(_MD("get_space_left"),&_Directory::get_space_left);
  1093. ObjectTypeDB::bind_method(_MD("copy:Error","from","to"),&_Directory::copy);
  1094. ObjectTypeDB::bind_method(_MD("rename:Error","from","to"),&_Directory::rename);
  1095. ObjectTypeDB::bind_method(_MD("remove:Error","file"),&_Directory::remove);
  1096. }
  1097. _Directory::_Directory() {
  1098. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1099. }
  1100. _Directory::~_Directory() {
  1101. if (d)
  1102. memdelete(d);
  1103. }
  1104. String _Marshalls::variant_to_base64(const Variant& p_var) {
  1105. int len;
  1106. Error err = encode_variant(p_var,NULL,len);
  1107. ERR_FAIL_COND_V( err != OK, "" );
  1108. DVector<uint8_t> buff;
  1109. buff.resize(len);
  1110. DVector<uint8_t>::Write w = buff.write();
  1111. err = encode_variant(p_var,&w[0],len);
  1112. ERR_FAIL_COND_V( err != OK, "" );
  1113. int b64len = len / 3 * 4 + 4 + 1;
  1114. DVector<uint8_t> b64buff;
  1115. b64buff.resize(b64len);
  1116. DVector<uint8_t>::Write w64 = b64buff.write();
  1117. int strlen = base64_encode((char*)(&w64[0]), (char*)(&w[0]), len);
  1118. //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen);
  1119. w64[strlen] = 0;
  1120. String ret = (char*)&w64[0];
  1121. return ret;
  1122. };
  1123. Variant _Marshalls::base64_to_variant(const String& p_str) {
  1124. int strlen = p_str.length();
  1125. CharString cstr = p_str.ascii();
  1126. DVector<uint8_t> buf;
  1127. buf.resize(strlen / 4 * 3 + 1);
  1128. DVector<uint8_t>::Write w = buf.write();
  1129. int len = base64_decode((char*)(&w[0]), (char*)cstr.get_data(), strlen);
  1130. Variant v;
  1131. Error err = decode_variant(v, &w[0], len);
  1132. ERR_FAIL_COND_V( err!=OK, Variant() );
  1133. return v;
  1134. };
  1135. void _Marshalls::_bind_methods() {
  1136. ObjectTypeDB::bind_method(_MD("variant_to_base64:String","variant"),&_Marshalls::variant_to_base64);
  1137. ObjectTypeDB::bind_method(_MD("base64_to_variant:Variant","base64_str"),&_Marshalls::base64_to_variant);
  1138. };
  1139. ////////////////
  1140. Error _Semaphore::wait() {
  1141. return semaphore->wait();
  1142. }
  1143. Error _Semaphore::post() {
  1144. return semaphore->post();
  1145. }
  1146. void _Semaphore::_bind_methods() {
  1147. ObjectTypeDB::bind_method(_MD("wait:Error"),&_Semaphore::wait);
  1148. ObjectTypeDB::bind_method(_MD("post:Error"),&_Semaphore::post);
  1149. }
  1150. _Semaphore::_Semaphore() {
  1151. semaphore =Semaphore::create();
  1152. }
  1153. _Semaphore::~_Semaphore(){
  1154. memdelete(semaphore);
  1155. }
  1156. ///////////////
  1157. void _Mutex::lock() {
  1158. mutex->lock();
  1159. }
  1160. Error _Mutex::try_lock(){
  1161. return mutex->try_lock();
  1162. }
  1163. void _Mutex::unlock(){
  1164. mutex->unlock();
  1165. }
  1166. void _Mutex::_bind_methods() {
  1167. ObjectTypeDB::bind_method(_MD("lock"),&_Mutex::lock);
  1168. ObjectTypeDB::bind_method(_MD("try_lock:Error"),&_Mutex::try_lock);
  1169. ObjectTypeDB::bind_method(_MD("unlock"),&_Mutex::unlock);
  1170. }
  1171. _Mutex::_Mutex() {
  1172. mutex =Mutex::create();
  1173. }
  1174. _Mutex::~_Mutex(){
  1175. memdelete(mutex);
  1176. }
  1177. ///////////////
  1178. void _Thread::_start_func(void *ud) {
  1179. _Thread *t=(_Thread*)ud;
  1180. Variant::CallError ce;
  1181. const Variant* arg[1]={&t->userdata};
  1182. t->ret=t->target_instance->call(t->target_method,arg,1,ce);
  1183. if (ce.error!=Variant::CallError::CALL_OK) {
  1184. String reason;
  1185. switch(ce.error) {
  1186. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1187. reason="Invalid Argument #"+itos(ce.argument);
  1188. } break;
  1189. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1190. reason="Too Many Arguments";
  1191. } break;
  1192. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1193. reason="Too Many Arguments";
  1194. } break;
  1195. case Variant::CallError::CALL_ERROR_INVALID_METHOD: {
  1196. reason="Method Not Found";
  1197. } break;
  1198. default: {}
  1199. }
  1200. ERR_EXPLAIN("Could not call function '"+t->target_method.operator String()+"'' starting thread ID: "+t->get_id()+" Reason: "+reason);
  1201. ERR_FAIL();
  1202. }
  1203. }
  1204. Error _Thread::start(Object *p_instance,const StringName& p_method,const Variant& p_userdata,int p_priority) {
  1205. ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE);
  1206. ERR_FAIL_COND_V(!p_instance,ERR_INVALID_PARAMETER);
  1207. ERR_FAIL_COND_V(p_method==StringName(),ERR_INVALID_PARAMETER);
  1208. ERR_FAIL_INDEX_V(p_priority,3,ERR_INVALID_PARAMETER);
  1209. ret=Variant();
  1210. target_method=p_method;
  1211. target_instance=p_instance;
  1212. userdata=p_userdata;
  1213. active=true;
  1214. Thread::Settings s;
  1215. s.priority=(Thread::Priority)p_priority;
  1216. thread = Thread::create(_start_func,this,s);
  1217. if (!thread) {
  1218. active=false;
  1219. target_method=StringName();
  1220. target_instance=NULL;
  1221. userdata=Variant();
  1222. return ERR_CANT_CREATE;
  1223. }
  1224. return OK;
  1225. }
  1226. String _Thread::get_id() const {
  1227. if (!thread)
  1228. return String();
  1229. return itos(thread->get_ID());
  1230. }
  1231. bool _Thread::is_active() const {
  1232. return active;
  1233. }
  1234. Variant _Thread::wait_to_finish() {
  1235. ERR_FAIL_COND_V(!thread,Variant());
  1236. ERR_FAIL_COND_V(!active,Variant());
  1237. Thread::wait_to_finish(thread);
  1238. Variant r = ret;
  1239. active=false;
  1240. target_method=StringName();
  1241. target_instance=NULL;
  1242. userdata=Variant();
  1243. thread=NULL;
  1244. return r;
  1245. }
  1246. void _Thread::_bind_methods() {
  1247. ObjectTypeDB::bind_method(_MD("start:Error","instance","method","userdata","priority"),&_Thread::start,DEFVAL(Variant()),DEFVAL(PRIORITY_NORMAL));
  1248. ObjectTypeDB::bind_method(_MD("get_id"),&_Thread::get_id);
  1249. ObjectTypeDB::bind_method(_MD("is_active"),&_Thread::is_active);
  1250. ObjectTypeDB::bind_method(_MD("wait_to_finish:var"),&_Thread::wait_to_finish);
  1251. BIND_CONSTANT( PRIORITY_LOW );
  1252. BIND_CONSTANT( PRIORITY_NORMAL );
  1253. BIND_CONSTANT( PRIORITY_HIGH );
  1254. }
  1255. _Thread::_Thread() {
  1256. active=false;
  1257. thread=NULL;
  1258. target_instance=NULL;
  1259. }
  1260. _Thread::~_Thread() {
  1261. ERR_FAIL_COND(active==true);
  1262. }