core_bind.cpp 34 KB

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