core_bind.cpp 40 KB

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