core_bind.cpp 43 KB

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