core_bind.cpp 44 KB

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