core_bind.cpp 54 KB

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