core_bind.cpp 47 KB

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