core_bind.cpp 50 KB

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