core_bind.cpp 43 KB

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