core_bind.cpp 51 KB

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