core_bind.cpp 59 KB

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