core_bind.cpp 51 KB

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