core_bind.cpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  1. /*************************************************************************/
  2. /* core_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "core_bind.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/io/file_access_compressed.h"
  33. #include "core/io/file_access_encrypted.h"
  34. #include "core/io/json.h"
  35. #include "core/io/marshalls.h"
  36. #include "core/math/geometry.h"
  37. #include "core/os/keyboard.h"
  38. #include "core/os/os.h"
  39. #include "core/project_settings.h"
  40. /**
  41. * Time constants borrowed from loc_time.h
  42. */
  43. #define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
  44. #define SECS_DAY (24L * 60L * 60L)
  45. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
  46. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  47. #define SECOND_KEY "second"
  48. #define MINUTE_KEY "minute"
  49. #define HOUR_KEY "hour"
  50. #define DAY_KEY "day"
  51. #define MONTH_KEY "month"
  52. #define YEAR_KEY "year"
  53. #define WEEKDAY_KEY "weekday"
  54. #define DST_KEY "dst"
  55. /// Table of number of days in each month (for regular year and leap year)
  56. static const unsigned int MONTH_DAYS_TABLE[2][12] = {
  57. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  58. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  59. };
  60. _ResourceLoader *_ResourceLoader::singleton = NULL;
  61. Error _ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads) {
  62. return ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads);
  63. }
  64. _ResourceLoader::ThreadLoadStatus _ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
  65. float progress = 0;
  66. ResourceLoader::ThreadLoadStatus tls = ResourceLoader::load_threaded_get_status(p_path, &progress);
  67. r_progress.resize(1);
  68. r_progress[0] = progress;
  69. return (ThreadLoadStatus)tls;
  70. }
  71. RES _ResourceLoader::load_threaded_get(const String &p_path) {
  72. Error error;
  73. RES res = ResourceLoader::load_threaded_get(p_path, &error);
  74. return res;
  75. }
  76. RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache) {
  77. Error err = OK;
  78. RES ret = ResourceLoader::load(p_path, p_type_hint, p_no_cache, &err);
  79. ERR_FAIL_COND_V_MSG(err != OK, ret, "Error loading resource: '" + p_path + "'.");
  80. return ret;
  81. }
  82. Vector<String> _ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
  83. List<String> exts;
  84. ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
  85. Vector<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. void _ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  92. ResourceLoader::set_abort_on_missing_resources(p_abort);
  93. }
  94. PackedStringArray _ResourceLoader::get_dependencies(const String &p_path) {
  95. List<String> deps;
  96. ResourceLoader::get_dependencies(p_path, &deps);
  97. PackedStringArray ret;
  98. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  99. ret.push_back(E->get());
  100. }
  101. return ret;
  102. };
  103. bool _ResourceLoader::has_cached(const String &p_path) {
  104. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  105. return ResourceCache::has(local_path);
  106. }
  107. bool _ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  108. return ResourceLoader::exists(p_path, p_type_hint);
  109. }
  110. void _ResourceLoader::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("load_threaded_request", "path", "type_hint", "use_sub_threads"), &_ResourceLoader::load_threaded_request, DEFVAL(""), DEFVAL(false));
  112. ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &_ResourceLoader::load_threaded_get_status, DEFVAL(Array()));
  113. ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &_ResourceLoader::load_threaded_get);
  114. ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "no_cache"), &_ResourceLoader::load, DEFVAL(""), DEFVAL(false));
  115. ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type);
  116. ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources);
  117. ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
  118. ClassDB::bind_method(D_METHOD("has_cached", "path"), &_ResourceLoader::has_cached);
  119. ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &_ResourceLoader::exists, DEFVAL(""));
  120. BIND_ENUM_CONSTANT(THREAD_LOAD_INVALID_RESOURCE);
  121. BIND_ENUM_CONSTANT(THREAD_LOAD_IN_PROGRESS);
  122. BIND_ENUM_CONSTANT(THREAD_LOAD_FAILED);
  123. BIND_ENUM_CONSTANT(THREAD_LOAD_LOADED);
  124. }
  125. _ResourceLoader::_ResourceLoader() {
  126. singleton = this;
  127. }
  128. Error _ResourceSaver::save(const String &p_path, const RES &p_resource, SaverFlags p_flags) {
  129. ERR_FAIL_COND_V_MSG(p_resource.is_null(), ERR_INVALID_PARAMETER, "Can't save empty resource to path '" + String(p_path) + "'.");
  130. return ResourceSaver::save(p_path, p_resource, p_flags);
  131. }
  132. Vector<String> _ResourceSaver::get_recognized_extensions(const RES &p_resource) {
  133. ERR_FAIL_COND_V_MSG(p_resource.is_null(), Vector<String>(), "It's not a reference to a valid Resource object.");
  134. List<String> exts;
  135. ResourceSaver::get_recognized_extensions(p_resource, &exts);
  136. Vector<String> ret;
  137. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  138. ret.push_back(E->get());
  139. }
  140. return ret;
  141. }
  142. _ResourceSaver *_ResourceSaver::singleton = NULL;
  143. void _ResourceSaver::_bind_methods() {
  144. ClassDB::bind_method(D_METHOD("save", "path", "resource", "flags"), &_ResourceSaver::save, DEFVAL(0));
  145. ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &_ResourceSaver::get_recognized_extensions);
  146. BIND_ENUM_CONSTANT(FLAG_RELATIVE_PATHS);
  147. BIND_ENUM_CONSTANT(FLAG_BUNDLE_RESOURCES);
  148. BIND_ENUM_CONSTANT(FLAG_CHANGE_PATH);
  149. BIND_ENUM_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
  150. BIND_ENUM_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
  151. BIND_ENUM_CONSTANT(FLAG_COMPRESS);
  152. BIND_ENUM_CONSTANT(FLAG_REPLACE_SUBRESOURCE_PATHS);
  153. }
  154. _ResourceSaver::_ResourceSaver() {
  155. singleton = this;
  156. }
  157. PackedStringArray _OS::get_connected_midi_inputs() {
  158. return OS::get_singleton()->get_connected_midi_inputs();
  159. }
  160. void _OS::open_midi_inputs() {
  161. OS::get_singleton()->open_midi_inputs();
  162. }
  163. void _OS::close_midi_inputs() {
  164. OS::get_singleton()->close_midi_inputs();
  165. }
  166. void _OS::set_use_file_access_save_and_swap(bool p_enable) {
  167. FileAccess::set_backup_save(p_enable);
  168. }
  169. void _OS::set_low_processor_usage_mode(bool p_enabled) {
  170. OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  171. }
  172. bool _OS::is_in_low_processor_usage_mode() const {
  173. return OS::get_singleton()->is_in_low_processor_usage_mode();
  174. }
  175. void _OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  176. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(p_usec);
  177. }
  178. int _OS::get_low_processor_usage_mode_sleep_usec() const {
  179. return OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
  180. }
  181. String _OS::get_executable_path() const {
  182. return OS::get_singleton()->get_executable_path();
  183. }
  184. Error _OS::shell_open(String p_uri) {
  185. return OS::get_singleton()->shell_open(p_uri);
  186. };
  187. int _OS::execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output, bool p_read_stderr) {
  188. OS::ProcessID pid = -2;
  189. int exitcode = 0;
  190. List<String> args;
  191. for (int i = 0; i < p_arguments.size(); i++)
  192. args.push_back(p_arguments[i]);
  193. String pipe;
  194. Error err = OS::get_singleton()->execute(p_path, args, p_blocking, &pid, &pipe, &exitcode, p_read_stderr);
  195. p_output.clear();
  196. p_output.push_back(pipe);
  197. if (err != OK)
  198. return -1;
  199. else if (p_blocking)
  200. return exitcode;
  201. else
  202. return pid;
  203. }
  204. Error _OS::kill(int p_pid) {
  205. return OS::get_singleton()->kill(p_pid);
  206. }
  207. int _OS::get_process_id() const {
  208. return OS::get_singleton()->get_process_id();
  209. };
  210. bool _OS::has_environment(const String &p_var) const {
  211. return OS::get_singleton()->has_environment(p_var);
  212. }
  213. String _OS::get_environment(const String &p_var) const {
  214. return OS::get_singleton()->get_environment(p_var);
  215. }
  216. String _OS::get_name() const {
  217. return OS::get_singleton()->get_name();
  218. }
  219. Vector<String> _OS::get_cmdline_args() {
  220. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  221. Vector<String> cmdlinev;
  222. for (List<String>::Element *E = cmdline.front(); E; E = E->next()) {
  223. cmdlinev.push_back(E->get());
  224. }
  225. return cmdlinev;
  226. }
  227. String _OS::get_locale() const {
  228. return OS::get_singleton()->get_locale();
  229. }
  230. String _OS::get_model_name() const {
  231. return OS::get_singleton()->get_model_name();
  232. }
  233. Error _OS::set_thread_name(const String &p_name) {
  234. return Thread::set_name(p_name);
  235. };
  236. bool _OS::has_feature(const String &p_feature) const {
  237. return OS::get_singleton()->has_feature(p_feature);
  238. }
  239. /*
  240. enum Weekday {
  241. DAY_SUNDAY,
  242. DAY_MONDAY,
  243. DAY_TUESDAY,
  244. DAY_WEDNESDAY,
  245. DAY_THURSDAY,
  246. DAY_FRIDAY,
  247. DAY_SATURDAY
  248. };
  249. enum Month {
  250. MONTH_JANUARY,
  251. MONTH_FEBRUARY,
  252. MONTH_MARCH,
  253. MONTH_APRIL,
  254. MONTH_MAY,
  255. MONTH_JUNE,
  256. MONTH_JULY,
  257. MONTH_AUGUST,
  258. MONTH_SEPTEMBER,
  259. MONTH_OCTOBER,
  260. MONTH_NOVEMBER,
  261. MONTH_DECEMBER
  262. };
  263. */
  264. /*
  265. struct Date {
  266. int year;
  267. Month month;
  268. int day;
  269. Weekday weekday;
  270. bool dst;
  271. };
  272. struct Time {
  273. int hour;
  274. int min;
  275. int sec;
  276. };
  277. */
  278. uint64_t _OS::get_static_memory_usage() const {
  279. return OS::get_singleton()->get_static_memory_usage();
  280. }
  281. uint64_t _OS::get_static_memory_peak_usage() const {
  282. return OS::get_singleton()->get_static_memory_peak_usage();
  283. }
  284. int _OS::get_exit_code() const {
  285. return OS::get_singleton()->get_exit_code();
  286. }
  287. void _OS::set_exit_code(int p_code) {
  288. if (p_code < 0 || p_code > 125) {
  289. WARN_PRINT("For portability reasons, the exit code should be set between 0 and 125 (inclusive).");
  290. }
  291. OS::get_singleton()->set_exit_code(p_code);
  292. }
  293. /**
  294. * Get current datetime with consideration for utc and
  295. * dst
  296. */
  297. Dictionary _OS::get_datetime(bool utc) const {
  298. Dictionary dated = get_date(utc);
  299. Dictionary timed = get_time(utc);
  300. List<Variant> keys;
  301. timed.get_key_list(&keys);
  302. for (int i = 0; i < keys.size(); i++) {
  303. dated[keys[i]] = timed[keys[i]];
  304. }
  305. return dated;
  306. }
  307. Dictionary _OS::get_date(bool utc) const {
  308. OS::Date date = OS::get_singleton()->get_date(utc);
  309. Dictionary dated;
  310. dated[YEAR_KEY] = date.year;
  311. dated[MONTH_KEY] = date.month;
  312. dated[DAY_KEY] = date.day;
  313. dated[WEEKDAY_KEY] = date.weekday;
  314. dated[DST_KEY] = date.dst;
  315. return dated;
  316. }
  317. Dictionary _OS::get_time(bool utc) const {
  318. OS::Time time = OS::get_singleton()->get_time(utc);
  319. Dictionary timed;
  320. timed[HOUR_KEY] = time.hour;
  321. timed[MINUTE_KEY] = time.min;
  322. timed[SECOND_KEY] = time.sec;
  323. return timed;
  324. }
  325. /**
  326. * Get an epoch time value from a dictionary of time values
  327. * @p datetime must be populated with the following keys:
  328. * day, hour, minute, month, second, year. (dst is ignored).
  329. *
  330. * You can pass the output from
  331. * get_datetime_from_unix_time directly into this function
  332. *
  333. * @param datetime dictionary of date and time values to convert
  334. *
  335. * @return epoch calculated
  336. */
  337. int64_t _OS::get_unix_time_from_datetime(Dictionary datetime) const {
  338. // Bunch of conversion constants
  339. static const unsigned int SECONDS_PER_MINUTE = 60;
  340. static const unsigned int MINUTES_PER_HOUR = 60;
  341. static const unsigned int HOURS_PER_DAY = 24;
  342. static const unsigned int SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
  343. static const unsigned int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
  344. // Get all time values from the dictionary, set to zero if it doesn't exist.
  345. // Risk incorrect calculation over throwing errors
  346. unsigned int second = ((datetime.has(SECOND_KEY)) ? static_cast<unsigned int>(datetime[SECOND_KEY]) : 0);
  347. unsigned int minute = ((datetime.has(MINUTE_KEY)) ? static_cast<unsigned int>(datetime[MINUTE_KEY]) : 0);
  348. unsigned int hour = ((datetime.has(HOUR_KEY)) ? static_cast<unsigned int>(datetime[HOUR_KEY]) : 0);
  349. unsigned int day = ((datetime.has(DAY_KEY)) ? static_cast<unsigned int>(datetime[DAY_KEY]) : 1);
  350. unsigned int month = ((datetime.has(MONTH_KEY)) ? static_cast<unsigned int>(datetime[MONTH_KEY]) : 1);
  351. unsigned int year = ((datetime.has(YEAR_KEY)) ? static_cast<unsigned int>(datetime[YEAR_KEY]) : 0);
  352. /// How many days come before each month (0-12)
  353. static const unsigned short int DAYS_PAST_THIS_YEAR_TABLE[2][13] = {
  354. /* Normal years. */
  355. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  356. /* Leap years. */
  357. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  358. };
  359. ERR_FAIL_COND_V_MSG(second > 59, 0, "Invalid second value of: " + itos(second) + ".");
  360. ERR_FAIL_COND_V_MSG(minute > 59, 0, "Invalid minute value of: " + itos(minute) + ".");
  361. ERR_FAIL_COND_V_MSG(hour > 23, 0, "Invalid hour value of: " + itos(hour) + ".");
  362. ERR_FAIL_COND_V_MSG(month > 12 || month == 0, 0, "Invalid month value of: " + itos(month) + ".");
  363. // Do this check after month is tested as valid
  364. ERR_FAIL_COND_V_MSG(day > MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1] || day == 0, 0, "Invalid day value of '" + itos(day) + "' which is larger than '" + itos(MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1]) + "' or 0.");
  365. // Calculate all the seconds from months past in this year
  366. uint64_t SECONDS_FROM_MONTHS_PAST_THIS_YEAR = DAYS_PAST_THIS_YEAR_TABLE[LEAPYEAR(year)][month - 1] * SECONDS_PER_DAY;
  367. int64_t SECONDS_FROM_YEARS_PAST = 0;
  368. if (year >= EPOCH_YR) {
  369. for (unsigned int iyear = EPOCH_YR; iyear < year; iyear++) {
  370. SECONDS_FROM_YEARS_PAST += YEARSIZE(iyear) * SECONDS_PER_DAY;
  371. }
  372. } else {
  373. for (unsigned int iyear = EPOCH_YR - 1; iyear >= year; iyear--) {
  374. SECONDS_FROM_YEARS_PAST -= YEARSIZE(iyear) * SECONDS_PER_DAY;
  375. }
  376. }
  377. int64_t epoch =
  378. second +
  379. minute * SECONDS_PER_MINUTE +
  380. hour * SECONDS_PER_HOUR +
  381. // Subtract 1 from day, since the current day isn't over yet
  382. // and we cannot count all 24 hours.
  383. (day - 1) * SECONDS_PER_DAY +
  384. SECONDS_FROM_MONTHS_PAST_THIS_YEAR +
  385. SECONDS_FROM_YEARS_PAST;
  386. return epoch;
  387. }
  388. /**
  389. * Get a dictionary of time values when given epoch time
  390. *
  391. * Dictionary Time values will be a union if values from #get_time
  392. * and #get_date dictionaries (with the exception of dst =
  393. * day light standard time, as it cannot be determined from epoch)
  394. *
  395. * @param unix_time_val epoch time to convert
  396. *
  397. * @return dictionary of date and time values
  398. */
  399. Dictionary _OS::get_datetime_from_unix_time(int64_t unix_time_val) const {
  400. OS::Date date;
  401. OS::Time time;
  402. long dayclock, dayno;
  403. int year = EPOCH_YR;
  404. if (unix_time_val >= 0) {
  405. dayno = unix_time_val / SECS_DAY;
  406. dayclock = unix_time_val % SECS_DAY;
  407. /* day 0 was a thursday */
  408. date.weekday = static_cast<OS::Weekday>((dayno + 4) % 7);
  409. while (dayno >= YEARSIZE(year)) {
  410. dayno -= YEARSIZE(year);
  411. year++;
  412. }
  413. } else {
  414. dayno = (unix_time_val - SECS_DAY + 1) / SECS_DAY;
  415. dayclock = unix_time_val - dayno * SECS_DAY;
  416. date.weekday = static_cast<OS::Weekday>((dayno - 3) % 7 + 7);
  417. do {
  418. year--;
  419. dayno += YEARSIZE(year);
  420. } while (dayno < 0);
  421. }
  422. time.sec = dayclock % 60;
  423. time.min = (dayclock % 3600) / 60;
  424. time.hour = dayclock / 3600;
  425. date.year = year;
  426. size_t imonth = 0;
  427. while ((unsigned long)dayno >= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth]) {
  428. dayno -= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth];
  429. imonth++;
  430. }
  431. /// Add 1 to month to make sure months are indexed starting at 1
  432. date.month = static_cast<OS::Month>(imonth + 1);
  433. date.day = dayno + 1;
  434. Dictionary timed;
  435. timed[HOUR_KEY] = time.hour;
  436. timed[MINUTE_KEY] = time.min;
  437. timed[SECOND_KEY] = time.sec;
  438. timed[YEAR_KEY] = date.year;
  439. timed[MONTH_KEY] = date.month;
  440. timed[DAY_KEY] = date.day;
  441. timed[WEEKDAY_KEY] = date.weekday;
  442. return timed;
  443. }
  444. Dictionary _OS::get_time_zone_info() const {
  445. OS::TimeZoneInfo info = OS::get_singleton()->get_time_zone_info();
  446. Dictionary infod;
  447. infod["bias"] = info.bias;
  448. infod["name"] = info.name;
  449. return infod;
  450. }
  451. uint64_t _OS::get_unix_time() const {
  452. return OS::get_singleton()->get_unix_time();
  453. }
  454. uint64_t _OS::get_system_time_secs() const {
  455. return OS::get_singleton()->get_system_time_secs();
  456. }
  457. uint64_t _OS::get_system_time_msecs() const {
  458. return OS::get_singleton()->get_system_time_msecs();
  459. }
  460. void _OS::delay_usec(uint32_t p_usec) const {
  461. OS::get_singleton()->delay_usec(p_usec);
  462. }
  463. void _OS::delay_msec(uint32_t p_msec) const {
  464. OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  465. }
  466. uint32_t _OS::get_ticks_msec() const {
  467. return OS::get_singleton()->get_ticks_msec();
  468. }
  469. uint64_t _OS::get_ticks_usec() const {
  470. return OS::get_singleton()->get_ticks_usec();
  471. }
  472. uint32_t _OS::get_splash_tick_msec() const {
  473. return OS::get_singleton()->get_splash_tick_msec();
  474. }
  475. bool _OS::can_use_threads() const {
  476. return OS::get_singleton()->can_use_threads();
  477. }
  478. bool _OS::is_userfs_persistent() const {
  479. return OS::get_singleton()->is_userfs_persistent();
  480. }
  481. int _OS::get_processor_count() const {
  482. return OS::get_singleton()->get_processor_count();
  483. }
  484. bool _OS::is_stdout_verbose() const {
  485. return OS::get_singleton()->is_stdout_verbose();
  486. }
  487. void _OS::dump_memory_to_file(const String &p_file) {
  488. OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
  489. }
  490. struct _OSCoreBindImg {
  491. String path;
  492. Size2 size;
  493. int fmt;
  494. ObjectID id;
  495. int vram;
  496. bool operator<(const _OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
  497. };
  498. void _OS::print_all_textures_by_size() {
  499. List<_OSCoreBindImg> imgs;
  500. int total = 0;
  501. {
  502. List<Ref<Resource>> rsrc;
  503. ResourceCache::get_cached_resources(&rsrc);
  504. for (List<Ref<Resource>>::Element *E = rsrc.front(); E; E = E->next()) {
  505. if (!E->get()->is_class("ImageTexture"))
  506. continue;
  507. Size2 size = E->get()->call("get_size");
  508. int fmt = E->get()->call("get_format");
  509. _OSCoreBindImg img;
  510. img.size = size;
  511. img.fmt = fmt;
  512. img.path = E->get()->get_path();
  513. img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt));
  514. img.id = E->get()->get_instance_id();
  515. total += img.vram;
  516. imgs.push_back(img);
  517. }
  518. }
  519. imgs.sort();
  520. for (List<_OSCoreBindImg>::Element *E = imgs.front(); E; E = E->next()) {
  521. total -= E->get().vram;
  522. }
  523. }
  524. void _OS::print_resources_by_type(const Vector<String> &p_types) {
  525. Map<String, int> type_count;
  526. List<Ref<Resource>> resources;
  527. ResourceCache::get_cached_resources(&resources);
  528. List<Ref<Resource>> rsrc;
  529. ResourceCache::get_cached_resources(&rsrc);
  530. for (List<Ref<Resource>>::Element *E = rsrc.front(); E; E = E->next()) {
  531. Ref<Resource> r = E->get();
  532. bool found = false;
  533. for (int i = 0; i < p_types.size(); i++) {
  534. if (r->is_class(p_types[i]))
  535. found = true;
  536. }
  537. if (!found)
  538. continue;
  539. if (!type_count.has(r->get_class())) {
  540. type_count[r->get_class()] = 0;
  541. }
  542. type_count[r->get_class()]++;
  543. }
  544. };
  545. void _OS::print_all_resources(const String &p_to_file) {
  546. OS::get_singleton()->print_all_resources(p_to_file);
  547. }
  548. void _OS::print_resources_in_use(bool p_short) {
  549. OS::get_singleton()->print_resources_in_use(p_short);
  550. }
  551. void _OS::dump_resources_to_file(const String &p_file) {
  552. OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  553. }
  554. String _OS::get_user_data_dir() const {
  555. return OS::get_singleton()->get_user_data_dir();
  556. };
  557. bool _OS::is_debug_build() const {
  558. #ifdef DEBUG_ENABLED
  559. return true;
  560. #else
  561. return false;
  562. #endif
  563. }
  564. String _OS::get_system_dir(SystemDir p_dir) const {
  565. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
  566. }
  567. String _OS::get_keycode_string(uint32_t p_code) const {
  568. return keycode_get_string(p_code);
  569. }
  570. bool _OS::is_keycode_unicode(uint32_t p_unicode) const {
  571. return keycode_has_unicode(p_unicode);
  572. }
  573. int _OS::find_keycode_from_string(const String &p_code) const {
  574. return find_keycode(p_code);
  575. }
  576. bool _OS::request_permission(const String &p_name) {
  577. return OS::get_singleton()->request_permission(p_name);
  578. }
  579. bool _OS::request_permissions() {
  580. return OS::get_singleton()->request_permissions();
  581. }
  582. Vector<String> _OS::get_granted_permissions() const {
  583. return OS::get_singleton()->get_granted_permissions();
  584. }
  585. String _OS::get_unique_id() const {
  586. return OS::get_singleton()->get_unique_id();
  587. }
  588. _OS *_OS::singleton = NULL;
  589. void _OS::_bind_methods() {
  590. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &_OS::get_connected_midi_inputs);
  591. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &_OS::open_midi_inputs);
  592. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &_OS::close_midi_inputs);
  593. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
  594. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
  595. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &_OS::set_low_processor_usage_mode_sleep_usec);
  596. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &_OS::get_low_processor_usage_mode_sleep_usec);
  597. ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count);
  598. ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
  599. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "blocking", "output", "read_stderr"), &_OS::execute, DEFVAL(true), DEFVAL(Array()), DEFVAL(false));
  600. ClassDB::bind_method(D_METHOD("kill", "pid"), &_OS::kill);
  601. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &_OS::shell_open);
  602. ClassDB::bind_method(D_METHOD("get_process_id"), &_OS::get_process_id);
  603. ClassDB::bind_method(D_METHOD("get_environment", "environment"), &_OS::get_environment);
  604. ClassDB::bind_method(D_METHOD("has_environment", "environment"), &_OS::has_environment);
  605. ClassDB::bind_method(D_METHOD("get_name"), &_OS::get_name);
  606. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &_OS::get_cmdline_args);
  607. ClassDB::bind_method(D_METHOD("get_datetime", "utc"), &_OS::get_datetime, DEFVAL(false));
  608. ClassDB::bind_method(D_METHOD("get_date", "utc"), &_OS::get_date, DEFVAL(false));
  609. ClassDB::bind_method(D_METHOD("get_time", "utc"), &_OS::get_time, DEFVAL(false));
  610. ClassDB::bind_method(D_METHOD("get_time_zone_info"), &_OS::get_time_zone_info);
  611. ClassDB::bind_method(D_METHOD("get_unix_time"), &_OS::get_unix_time);
  612. ClassDB::bind_method(D_METHOD("get_datetime_from_unix_time", "unix_time_val"), &_OS::get_datetime_from_unix_time);
  613. ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime", "datetime"), &_OS::get_unix_time_from_datetime);
  614. ClassDB::bind_method(D_METHOD("get_system_time_secs"), &_OS::get_system_time_secs);
  615. ClassDB::bind_method(D_METHOD("get_system_time_msecs"), &_OS::get_system_time_msecs);
  616. ClassDB::bind_method(D_METHOD("get_exit_code"), &_OS::get_exit_code);
  617. ClassDB::bind_method(D_METHOD("set_exit_code", "code"), &_OS::set_exit_code);
  618. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &_OS::delay_usec);
  619. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &_OS::delay_msec);
  620. ClassDB::bind_method(D_METHOD("get_ticks_msec"), &_OS::get_ticks_msec);
  621. ClassDB::bind_method(D_METHOD("get_ticks_usec"), &_OS::get_ticks_usec);
  622. ClassDB::bind_method(D_METHOD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
  623. ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
  624. ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
  625. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &_OS::is_userfs_persistent);
  626. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &_OS::is_stdout_verbose);
  627. ClassDB::bind_method(D_METHOD("can_use_threads"), &_OS::can_use_threads);
  628. ClassDB::bind_method(D_METHOD("is_debug_build"), &_OS::is_debug_build);
  629. //ClassDB::bind_method(D_METHOD("get_mouse_button_state"),&_OS::get_mouse_button_state);
  630. ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
  631. ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
  632. ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
  633. ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
  634. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &_OS::get_static_memory_usage);
  635. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
  636. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir);
  637. ClassDB::bind_method(D_METHOD("get_system_dir", "dir"), &_OS::get_system_dir);
  638. ClassDB::bind_method(D_METHOD("get_unique_id"), &_OS::get_unique_id);
  639. ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
  640. ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
  641. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &_OS::get_keycode_string);
  642. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &_OS::is_keycode_unicode);
  643. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &_OS::find_keycode_from_string);
  644. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
  645. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &_OS::set_thread_name);
  646. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &_OS::has_feature);
  647. ClassDB::bind_method(D_METHOD("request_permission", "name"), &_OS::request_permission);
  648. ClassDB::bind_method(D_METHOD("request_permissions"), &_OS::request_permissions);
  649. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &_OS::get_granted_permissions);
  650. ADD_PROPERTY(PropertyInfo(Variant::INT, "exit_code"), "set_exit_code", "get_exit_code");
  651. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  652. ADD_PROPERTY(PropertyInfo(Variant::INT, "low_processor_usage_mode_sleep_usec"), "set_low_processor_usage_mode_sleep_usec", "get_low_processor_usage_mode_sleep_usec");
  653. // Those default values need to be specified for the docs generator,
  654. // to avoid using values from the documentation writer's own OS instance.
  655. ADD_PROPERTY_DEFAULT("exit_code", 0);
  656. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  657. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  658. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES2);
  659. BIND_ENUM_CONSTANT(VIDEO_DRIVER_VULKAN);
  660. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  661. BIND_ENUM_CONSTANT(DAY_MONDAY);
  662. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  663. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  664. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  665. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  666. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  667. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  668. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  669. BIND_ENUM_CONSTANT(MONTH_MARCH);
  670. BIND_ENUM_CONSTANT(MONTH_APRIL);
  671. BIND_ENUM_CONSTANT(MONTH_MAY);
  672. BIND_ENUM_CONSTANT(MONTH_JUNE);
  673. BIND_ENUM_CONSTANT(MONTH_JULY);
  674. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  675. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  676. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  677. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  678. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  679. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  680. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  681. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  682. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  683. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  684. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  685. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  686. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  687. }
  688. _OS::_OS() {
  689. singleton = this;
  690. }
  691. ///////////////////// GEOMETRY
  692. _Geometry *_Geometry::singleton = NULL;
  693. _Geometry *_Geometry::get_singleton() {
  694. return singleton;
  695. }
  696. Vector<Plane> _Geometry::build_box_planes(const Vector3 &p_extents) {
  697. return Geometry::build_box_planes(p_extents);
  698. }
  699. Vector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  700. return Geometry::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  701. }
  702. Vector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  703. return Geometry::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  704. }
  705. bool _Geometry::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  706. return Geometry::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  707. }
  708. real_t _Geometry::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  709. return Geometry::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  710. }
  711. 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) {
  712. Vector2 result;
  713. if (Geometry::segment_intersects_segment_2d(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  714. return result;
  715. } else {
  716. return Variant();
  717. };
  718. };
  719. Variant _Geometry::line_intersects_line_2d(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b) {
  720. Vector2 result;
  721. if (Geometry::line_intersects_line_2d(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  722. return result;
  723. } else {
  724. return Variant();
  725. }
  726. }
  727. Vector<Vector2> _Geometry::get_closest_points_between_segments_2d(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  728. Vector2 r1, r2;
  729. Geometry::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  730. Vector<Vector2> r;
  731. r.resize(2);
  732. r.set(0, r1);
  733. r.set(1, r2);
  734. return r;
  735. }
  736. Vector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  737. Vector3 r1, r2;
  738. Geometry::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  739. Vector<Vector3> r;
  740. r.resize(2);
  741. r.set(0, r1);
  742. r.set(1, r2);
  743. return r;
  744. }
  745. Vector2 _Geometry::get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  746. Vector2 s[2] = { p_a, p_b };
  747. return Geometry::get_closest_point_to_segment_2d(p_point, s);
  748. }
  749. Vector3 _Geometry::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  750. Vector3 s[2] = { p_a, p_b };
  751. return Geometry::get_closest_point_to_segment(p_point, s);
  752. }
  753. Vector2 _Geometry::get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  754. Vector2 s[2] = { p_a, p_b };
  755. return Geometry::get_closest_point_to_segment_uncapped_2d(p_point, s);
  756. }
  757. Vector3 _Geometry::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  758. Vector3 s[2] = { p_a, p_b };
  759. return Geometry::get_closest_point_to_segment_uncapped(p_point, s);
  760. }
  761. 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) {
  762. Vector3 res;
  763. if (Geometry::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res))
  764. return res;
  765. else
  766. return Variant();
  767. }
  768. 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) {
  769. Vector3 res;
  770. if (Geometry::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res))
  771. return res;
  772. else
  773. return Variant();
  774. }
  775. bool _Geometry::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  776. return Geometry::is_point_in_triangle(s, a, b, c);
  777. }
  778. Vector<Vector3> _Geometry::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  779. Vector<Vector3> r;
  780. Vector3 res, norm;
  781. if (!Geometry::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm))
  782. return r;
  783. r.resize(2);
  784. r.set(0, res);
  785. r.set(1, norm);
  786. return r;
  787. }
  788. Vector<Vector3> _Geometry::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  789. Vector<Vector3> r;
  790. Vector3 res, norm;
  791. if (!Geometry::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm))
  792. return r;
  793. r.resize(2);
  794. r.set(0, res);
  795. r.set(1, norm);
  796. return r;
  797. }
  798. Vector<Vector3> _Geometry::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  799. Vector<Vector3> r;
  800. Vector3 res, norm;
  801. if (!Geometry::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm))
  802. return r;
  803. r.resize(2);
  804. r.set(0, res);
  805. r.set(1, norm);
  806. return r;
  807. }
  808. bool _Geometry::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  809. return Geometry::is_polygon_clockwise(p_polygon);
  810. }
  811. bool _Geometry::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  812. return Geometry::is_point_in_polygon(p_point, p_polygon);
  813. }
  814. Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  815. return Geometry::triangulate_polygon(p_polygon);
  816. }
  817. Vector<int> _Geometry::triangulate_delaunay_2d(const Vector<Vector2> &p_points) {
  818. return Geometry::triangulate_delaunay_2d(p_points);
  819. }
  820. Vector<Point2> _Geometry::convex_hull_2d(const Vector<Point2> &p_points) {
  821. return Geometry::convex_hull_2d(p_points);
  822. }
  823. Vector<Vector3> _Geometry::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  824. return Geometry::clip_polygon(p_points, p_plane);
  825. }
  826. Array _Geometry::merge_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  827. Vector<Vector<Point2>> polys = Geometry::merge_polygons_2d(p_polygon_a, p_polygon_b);
  828. Array ret;
  829. for (int i = 0; i < polys.size(); ++i) {
  830. ret.push_back(polys[i]);
  831. }
  832. return ret;
  833. }
  834. Array _Geometry::clip_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  835. Vector<Vector<Point2>> polys = Geometry::clip_polygons_2d(p_polygon_a, p_polygon_b);
  836. Array ret;
  837. for (int i = 0; i < polys.size(); ++i) {
  838. ret.push_back(polys[i]);
  839. }
  840. return ret;
  841. }
  842. Array _Geometry::intersect_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  843. Vector<Vector<Point2>> polys = Geometry::intersect_polygons_2d(p_polygon_a, p_polygon_b);
  844. Array ret;
  845. for (int i = 0; i < polys.size(); ++i) {
  846. ret.push_back(polys[i]);
  847. }
  848. return ret;
  849. }
  850. Array _Geometry::exclude_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  851. Vector<Vector<Point2>> polys = Geometry::exclude_polygons_2d(p_polygon_a, p_polygon_b);
  852. Array ret;
  853. for (int i = 0; i < polys.size(); ++i) {
  854. ret.push_back(polys[i]);
  855. }
  856. return ret;
  857. }
  858. Array _Geometry::clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  859. Vector<Vector<Point2>> polys = Geometry::clip_polyline_with_polygon_2d(p_polyline, p_polygon);
  860. Array ret;
  861. for (int i = 0; i < polys.size(); ++i) {
  862. ret.push_back(polys[i]);
  863. }
  864. return ret;
  865. }
  866. Array _Geometry::intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  867. Vector<Vector<Point2>> polys = Geometry::intersect_polyline_with_polygon_2d(p_polyline, p_polygon);
  868. Array ret;
  869. for (int i = 0; i < polys.size(); ++i) {
  870. ret.push_back(polys[i]);
  871. }
  872. return ret;
  873. }
  874. Array _Geometry::offset_polygon_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  875. Vector<Vector<Point2>> polys = Geometry::offset_polygon_2d(p_polygon, p_delta, Geometry::PolyJoinType(p_join_type));
  876. Array ret;
  877. for (int i = 0; i < polys.size(); ++i) {
  878. ret.push_back(polys[i]);
  879. }
  880. return ret;
  881. }
  882. Array _Geometry::offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  883. Vector<Vector<Point2>> polys = Geometry::offset_polyline_2d(p_polygon, p_delta, Geometry::PolyJoinType(p_join_type), Geometry::PolyEndType(p_end_type));
  884. Array ret;
  885. for (int i = 0; i < polys.size(); ++i) {
  886. ret.push_back(polys[i]);
  887. }
  888. return ret;
  889. }
  890. Dictionary _Geometry::make_atlas(const Vector<Size2> &p_rects) {
  891. Dictionary ret;
  892. Vector<Size2i> rects;
  893. for (int i = 0; i < p_rects.size(); i++) {
  894. rects.push_back(p_rects[i]);
  895. };
  896. Vector<Point2i> result;
  897. Size2i size;
  898. Geometry::make_atlas(rects, result, size);
  899. Size2 r_size = size;
  900. Vector<Point2> r_result;
  901. for (int i = 0; i < result.size(); i++) {
  902. r_result.push_back(result[i]);
  903. };
  904. ret["points"] = r_result;
  905. ret["size"] = r_size;
  906. return ret;
  907. };
  908. int _Geometry::get_uv84_normal_bit(const Vector3 &p_vector) {
  909. return Geometry::get_uv84_normal_bit(p_vector);
  910. }
  911. void _Geometry::_bind_methods() {
  912. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry::build_box_planes);
  913. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  914. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  915. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &_Geometry::is_point_in_circle);
  916. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &_Geometry::segment_intersects_circle);
  917. ClassDB::bind_method(D_METHOD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d);
  918. ClassDB::bind_method(D_METHOD("line_intersects_line_2d", "from_a", "dir_a", "from_b", "dir_b"), &_Geometry::line_intersects_line_2d);
  919. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments_2d", "p1", "q1", "p2", "q2"), &_Geometry::get_closest_points_between_segments_2d);
  920. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry::get_closest_points_between_segments);
  921. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_2d);
  922. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment);
  923. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped_2d);
  924. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped);
  925. ClassDB::bind_method(D_METHOD("get_uv84_normal_bit", "normal"), &_Geometry::get_uv84_normal_bit);
  926. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry::ray_intersects_triangle);
  927. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry::segment_intersects_triangle);
  928. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &_Geometry::segment_intersects_sphere);
  929. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry::segment_intersects_cylinder);
  930. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &_Geometry::segment_intersects_convex);
  931. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle);
  932. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &_Geometry::is_polygon_clockwise);
  933. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &_Geometry::is_point_in_polygon);
  934. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &_Geometry::triangulate_polygon);
  935. ClassDB::bind_method(D_METHOD("triangulate_delaunay_2d", "points"), &_Geometry::triangulate_delaunay_2d);
  936. ClassDB::bind_method(D_METHOD("convex_hull_2d", "points"), &_Geometry::convex_hull_2d);
  937. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &_Geometry::clip_polygon);
  938. ClassDB::bind_method(D_METHOD("merge_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::merge_polygons_2d);
  939. ClassDB::bind_method(D_METHOD("clip_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::clip_polygons_2d);
  940. ClassDB::bind_method(D_METHOD("intersect_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::intersect_polygons_2d);
  941. ClassDB::bind_method(D_METHOD("exclude_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::exclude_polygons_2d);
  942. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon_2d", "polyline", "polygon"), &_Geometry::clip_polyline_with_polygon_2d);
  943. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon_2d", "polyline", "polygon"), &_Geometry::intersect_polyline_with_polygon_2d);
  944. ClassDB::bind_method(D_METHOD("offset_polygon_2d", "polygon", "delta", "join_type"), &_Geometry::offset_polygon_2d, DEFVAL(JOIN_SQUARE));
  945. ClassDB::bind_method(D_METHOD("offset_polyline_2d", "polyline", "delta", "join_type", "end_type"), &_Geometry::offset_polyline_2d, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  946. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &_Geometry::make_atlas);
  947. BIND_ENUM_CONSTANT(OPERATION_UNION);
  948. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  949. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  950. BIND_ENUM_CONSTANT(OPERATION_XOR);
  951. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  952. BIND_ENUM_CONSTANT(JOIN_ROUND);
  953. BIND_ENUM_CONSTANT(JOIN_MITER);
  954. BIND_ENUM_CONSTANT(END_POLYGON);
  955. BIND_ENUM_CONSTANT(END_JOINED);
  956. BIND_ENUM_CONSTANT(END_BUTT);
  957. BIND_ENUM_CONSTANT(END_SQUARE);
  958. BIND_ENUM_CONSTANT(END_ROUND);
  959. }
  960. _Geometry::_Geometry() {
  961. singleton = this;
  962. }
  963. ///////////////////////// FILE
  964. Error _File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  965. Error err = open(p_path, p_mode_flags);
  966. if (err)
  967. return err;
  968. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  969. err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  970. if (err) {
  971. memdelete(fae);
  972. close();
  973. return err;
  974. }
  975. f = fae;
  976. return OK;
  977. }
  978. Error _File::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  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_password(f, p_pass, (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_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  993. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  994. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  995. Error err = fac->_open(p_path, p_mode_flags);
  996. if (err) {
  997. memdelete(fac);
  998. return err;
  999. }
  1000. f = fac;
  1001. return OK;
  1002. }
  1003. Error _File::open(const String &p_path, ModeFlags p_mode_flags) {
  1004. close();
  1005. Error err;
  1006. f = FileAccess::open(p_path, p_mode_flags, &err);
  1007. if (f)
  1008. f->set_endian_swap(eswap);
  1009. return err;
  1010. }
  1011. void _File::close() {
  1012. if (f)
  1013. memdelete(f);
  1014. f = NULL;
  1015. }
  1016. bool _File::is_open() const {
  1017. return f != NULL;
  1018. }
  1019. String _File::get_path() const {
  1020. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  1021. return f->get_path();
  1022. }
  1023. String _File::get_path_absolute() const {
  1024. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  1025. return f->get_path_absolute();
  1026. }
  1027. void _File::seek(int64_t p_position) {
  1028. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1029. f->seek(p_position);
  1030. }
  1031. void _File::seek_end(int64_t p_position) {
  1032. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1033. f->seek_end(p_position);
  1034. }
  1035. int64_t _File::get_position() const {
  1036. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1037. return f->get_position();
  1038. }
  1039. int64_t _File::get_len() const {
  1040. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1041. return f->get_len();
  1042. }
  1043. bool _File::eof_reached() const {
  1044. ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use.");
  1045. return f->eof_reached();
  1046. }
  1047. uint8_t _File::get_8() const {
  1048. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1049. return f->get_8();
  1050. }
  1051. uint16_t _File::get_16() const {
  1052. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1053. return f->get_16();
  1054. }
  1055. uint32_t _File::get_32() const {
  1056. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1057. return f->get_32();
  1058. }
  1059. uint64_t _File::get_64() const {
  1060. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1061. return f->get_64();
  1062. }
  1063. float _File::get_float() const {
  1064. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1065. return f->get_float();
  1066. }
  1067. double _File::get_double() const {
  1068. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1069. return f->get_double();
  1070. }
  1071. real_t _File::get_real() const {
  1072. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  1073. return f->get_real();
  1074. }
  1075. Vector<uint8_t> _File::get_buffer(int p_length) const {
  1076. Vector<uint8_t> data;
  1077. ERR_FAIL_COND_V_MSG(!f, data, "File must be opened before use.");
  1078. ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0.");
  1079. if (p_length == 0)
  1080. return data;
  1081. Error err = data.resize(p_length);
  1082. ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");
  1083. uint8_t *w = data.ptrw();
  1084. int len = f->get_buffer(&w[0], p_length);
  1085. ERR_FAIL_COND_V(len < 0, Vector<uint8_t>());
  1086. if (len < p_length)
  1087. data.resize(p_length);
  1088. return data;
  1089. }
  1090. String _File::get_as_text() const {
  1091. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  1092. String text;
  1093. size_t original_pos = f->get_position();
  1094. f->seek(0);
  1095. String l = get_line();
  1096. while (!eof_reached()) {
  1097. text += l + "\n";
  1098. l = get_line();
  1099. }
  1100. text += l;
  1101. f->seek(original_pos);
  1102. return text;
  1103. }
  1104. String _File::get_md5(const String &p_path) const {
  1105. return FileAccess::get_md5(p_path);
  1106. }
  1107. String _File::get_sha256(const String &p_path) const {
  1108. return FileAccess::get_sha256(p_path);
  1109. }
  1110. String _File::get_line() const {
  1111. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  1112. return f->get_line();
  1113. }
  1114. Vector<String> _File::get_csv_line(const String &p_delim) const {
  1115. ERR_FAIL_COND_V_MSG(!f, Vector<String>(), "File must be opened before use.");
  1116. return f->get_csv_line(p_delim);
  1117. }
  1118. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  1119. * It's not about the current CPU type but file formats.
  1120. * this flags get reset to false (little endian) on each open
  1121. */
  1122. void _File::set_endian_swap(bool p_swap) {
  1123. eswap = p_swap;
  1124. if (f)
  1125. f->set_endian_swap(p_swap);
  1126. }
  1127. bool _File::get_endian_swap() {
  1128. return eswap;
  1129. }
  1130. Error _File::get_error() const {
  1131. if (!f)
  1132. return ERR_UNCONFIGURED;
  1133. return f->get_error();
  1134. }
  1135. void _File::store_8(uint8_t p_dest) {
  1136. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1137. f->store_8(p_dest);
  1138. }
  1139. void _File::store_16(uint16_t p_dest) {
  1140. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1141. f->store_16(p_dest);
  1142. }
  1143. void _File::store_32(uint32_t p_dest) {
  1144. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1145. f->store_32(p_dest);
  1146. }
  1147. void _File::store_64(uint64_t p_dest) {
  1148. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1149. f->store_64(p_dest);
  1150. }
  1151. void _File::store_float(float p_dest) {
  1152. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1153. f->store_float(p_dest);
  1154. }
  1155. void _File::store_double(double p_dest) {
  1156. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1157. f->store_double(p_dest);
  1158. }
  1159. void _File::store_real(real_t p_real) {
  1160. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1161. f->store_real(p_real);
  1162. }
  1163. void _File::store_string(const String &p_string) {
  1164. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1165. f->store_string(p_string);
  1166. }
  1167. void _File::store_pascal_string(const String &p_string) {
  1168. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1169. f->store_pascal_string(p_string);
  1170. };
  1171. String _File::get_pascal_string() {
  1172. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  1173. return f->get_pascal_string();
  1174. };
  1175. void _File::store_line(const String &p_string) {
  1176. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1177. f->store_line(p_string);
  1178. }
  1179. void _File::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  1180. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1181. f->store_csv_line(p_values, p_delim);
  1182. }
  1183. void _File::store_buffer(const Vector<uint8_t> &p_buffer) {
  1184. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1185. int len = p_buffer.size();
  1186. if (len == 0)
  1187. return;
  1188. const uint8_t *r = p_buffer.ptr();
  1189. f->store_buffer(&r[0], len);
  1190. }
  1191. bool _File::file_exists(const String &p_name) const {
  1192. return FileAccess::exists(p_name);
  1193. }
  1194. void _File::store_var(const Variant &p_var, bool p_full_objects) {
  1195. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1196. int len;
  1197. Error err = encode_variant(p_var, NULL, len, p_full_objects);
  1198. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1199. Vector<uint8_t> buff;
  1200. buff.resize(len);
  1201. uint8_t *w = buff.ptrw();
  1202. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1203. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1204. store_32(len);
  1205. store_buffer(buff);
  1206. }
  1207. Variant _File::get_var(bool p_allow_objects) const {
  1208. ERR_FAIL_COND_V_MSG(!f, Variant(), "File must be opened before use.");
  1209. uint32_t len = get_32();
  1210. Vector<uint8_t> buff = get_buffer(len);
  1211. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  1212. const uint8_t *r = buff.ptr();
  1213. Variant v;
  1214. Error err = decode_variant(v, &r[0], len, NULL, p_allow_objects);
  1215. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant.");
  1216. return v;
  1217. }
  1218. uint64_t _File::get_modified_time(const String &p_file) const {
  1219. return FileAccess::get_modified_time(p_file);
  1220. }
  1221. void _File::_bind_methods() {
  1222. ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
  1223. ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
  1224. ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &_File::open_compressed, DEFVAL(0));
  1225. ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open);
  1226. ClassDB::bind_method(D_METHOD("close"), &_File::close);
  1227. ClassDB::bind_method(D_METHOD("get_path"), &_File::get_path);
  1228. ClassDB::bind_method(D_METHOD("get_path_absolute"), &_File::get_path_absolute);
  1229. ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open);
  1230. ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek);
  1231. ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0));
  1232. ClassDB::bind_method(D_METHOD("get_position"), &_File::get_position);
  1233. ClassDB::bind_method(D_METHOD("get_len"), &_File::get_len);
  1234. ClassDB::bind_method(D_METHOD("eof_reached"), &_File::eof_reached);
  1235. ClassDB::bind_method(D_METHOD("get_8"), &_File::get_8);
  1236. ClassDB::bind_method(D_METHOD("get_16"), &_File::get_16);
  1237. ClassDB::bind_method(D_METHOD("get_32"), &_File::get_32);
  1238. ClassDB::bind_method(D_METHOD("get_64"), &_File::get_64);
  1239. ClassDB::bind_method(D_METHOD("get_float"), &_File::get_float);
  1240. ClassDB::bind_method(D_METHOD("get_double"), &_File::get_double);
  1241. ClassDB::bind_method(D_METHOD("get_real"), &_File::get_real);
  1242. ClassDB::bind_method(D_METHOD("get_buffer", "len"), &_File::get_buffer);
  1243. ClassDB::bind_method(D_METHOD("get_line"), &_File::get_line);
  1244. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
  1245. ClassDB::bind_method(D_METHOD("get_as_text"), &_File::get_as_text);
  1246. ClassDB::bind_method(D_METHOD("get_md5", "path"), &_File::get_md5);
  1247. ClassDB::bind_method(D_METHOD("get_sha256", "path"), &_File::get_sha256);
  1248. ClassDB::bind_method(D_METHOD("get_endian_swap"), &_File::get_endian_swap);
  1249. ClassDB::bind_method(D_METHOD("set_endian_swap", "enable"), &_File::set_endian_swap);
  1250. ClassDB::bind_method(D_METHOD("get_error"), &_File::get_error);
  1251. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &_File::get_var, DEFVAL(false));
  1252. ClassDB::bind_method(D_METHOD("store_8", "value"), &_File::store_8);
  1253. ClassDB::bind_method(D_METHOD("store_16", "value"), &_File::store_16);
  1254. ClassDB::bind_method(D_METHOD("store_32", "value"), &_File::store_32);
  1255. ClassDB::bind_method(D_METHOD("store_64", "value"), &_File::store_64);
  1256. ClassDB::bind_method(D_METHOD("store_float", "value"), &_File::store_float);
  1257. ClassDB::bind_method(D_METHOD("store_double", "value"), &_File::store_double);
  1258. ClassDB::bind_method(D_METHOD("store_real", "value"), &_File::store_real);
  1259. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &_File::store_buffer);
  1260. ClassDB::bind_method(D_METHOD("store_line", "line"), &_File::store_line);
  1261. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &_File::store_csv_line, DEFVAL(","));
  1262. ClassDB::bind_method(D_METHOD("store_string", "string"), &_File::store_string);
  1263. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &_File::store_var, DEFVAL(false));
  1264. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &_File::store_pascal_string);
  1265. ClassDB::bind_method(D_METHOD("get_pascal_string"), &_File::get_pascal_string);
  1266. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists);
  1267. ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time);
  1268. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "endian_swap"), "set_endian_swap", "get_endian_swap");
  1269. BIND_ENUM_CONSTANT(READ);
  1270. BIND_ENUM_CONSTANT(WRITE);
  1271. BIND_ENUM_CONSTANT(READ_WRITE);
  1272. BIND_ENUM_CONSTANT(WRITE_READ);
  1273. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  1274. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  1275. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  1276. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  1277. }
  1278. _File::_File() {
  1279. f = NULL;
  1280. eswap = false;
  1281. }
  1282. _File::~_File() {
  1283. if (f)
  1284. memdelete(f);
  1285. }
  1286. ///////////////////////////////////////////////////////
  1287. Error _Directory::open(const String &p_path) {
  1288. Error err;
  1289. DirAccess *alt = DirAccess::open(p_path, &err);
  1290. if (!alt)
  1291. return err;
  1292. if (d)
  1293. memdelete(d);
  1294. d = alt;
  1295. return OK;
  1296. }
  1297. Error _Directory::list_dir_begin(bool p_skip_navigational, bool p_skip_hidden) {
  1298. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1299. _list_skip_navigational = p_skip_navigational;
  1300. _list_skip_hidden = p_skip_hidden;
  1301. return d->list_dir_begin();
  1302. }
  1303. String _Directory::get_next() {
  1304. ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use.");
  1305. String next = d->get_next();
  1306. while (next != "" && ((_list_skip_navigational && (next == "." || next == "..")) || (_list_skip_hidden && d->current_is_hidden()))) {
  1307. next = d->get_next();
  1308. }
  1309. return next;
  1310. }
  1311. bool _Directory::current_is_dir() const {
  1312. ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use.");
  1313. return d->current_is_dir();
  1314. }
  1315. void _Directory::list_dir_end() {
  1316. ERR_FAIL_COND_MSG(!d, "Directory must be opened before use.");
  1317. d->list_dir_end();
  1318. }
  1319. int _Directory::get_drive_count() {
  1320. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1321. return d->get_drive_count();
  1322. }
  1323. String _Directory::get_drive(int p_drive) {
  1324. ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use.");
  1325. return d->get_drive(p_drive);
  1326. }
  1327. int _Directory::get_current_drive() {
  1328. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1329. return d->get_current_drive();
  1330. }
  1331. Error _Directory::change_dir(String p_dir) {
  1332. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1333. return d->change_dir(p_dir);
  1334. }
  1335. String _Directory::get_current_dir() {
  1336. ERR_FAIL_COND_V_MSG(!d, "", "Directory must be opened before use.");
  1337. return d->get_current_dir();
  1338. }
  1339. Error _Directory::make_dir(String p_dir) {
  1340. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1341. if (!p_dir.is_rel_path()) {
  1342. DirAccess *d = DirAccess::create_for_path(p_dir);
  1343. Error err = d->make_dir(p_dir);
  1344. memdelete(d);
  1345. return err;
  1346. }
  1347. return d->make_dir(p_dir);
  1348. }
  1349. Error _Directory::make_dir_recursive(String p_dir) {
  1350. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1351. if (!p_dir.is_rel_path()) {
  1352. DirAccess *d = DirAccess::create_for_path(p_dir);
  1353. Error err = d->make_dir_recursive(p_dir);
  1354. memdelete(d);
  1355. return err;
  1356. }
  1357. return d->make_dir_recursive(p_dir);
  1358. }
  1359. bool _Directory::file_exists(String p_file) {
  1360. ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use.");
  1361. if (!p_file.is_rel_path()) {
  1362. return FileAccess::exists(p_file);
  1363. }
  1364. return d->file_exists(p_file);
  1365. }
  1366. bool _Directory::dir_exists(String p_dir) {
  1367. ERR_FAIL_COND_V_MSG(!d, false, "Directory must be opened before use.");
  1368. if (!p_dir.is_rel_path()) {
  1369. DirAccess *d = DirAccess::create_for_path(p_dir);
  1370. bool exists = d->dir_exists(p_dir);
  1371. memdelete(d);
  1372. return exists;
  1373. } else {
  1374. return d->dir_exists(p_dir);
  1375. }
  1376. }
  1377. int _Directory::get_space_left() {
  1378. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1379. return d->get_space_left() / 1024 * 1024; //return value in megabytes, given binding is int
  1380. }
  1381. Error _Directory::copy(String p_from, String p_to) {
  1382. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1383. return d->copy(p_from, p_to);
  1384. }
  1385. Error _Directory::rename(String p_from, String p_to) {
  1386. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1387. if (!p_from.is_rel_path()) {
  1388. DirAccess *d = DirAccess::create_for_path(p_from);
  1389. Error err = d->rename(p_from, p_to);
  1390. memdelete(d);
  1391. return err;
  1392. }
  1393. return d->rename(p_from, p_to);
  1394. }
  1395. Error _Directory::remove(String p_name) {
  1396. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory must be opened before use.");
  1397. if (!p_name.is_rel_path()) {
  1398. DirAccess *d = DirAccess::create_for_path(p_name);
  1399. Error err = d->remove(p_name);
  1400. memdelete(d);
  1401. return err;
  1402. }
  1403. return d->remove(p_name);
  1404. }
  1405. void _Directory::_bind_methods() {
  1406. ClassDB::bind_method(D_METHOD("open", "path"), &_Directory::open);
  1407. ClassDB::bind_method(D_METHOD("list_dir_begin", "skip_navigational", "skip_hidden"), &_Directory::list_dir_begin, DEFVAL(false), DEFVAL(false));
  1408. ClassDB::bind_method(D_METHOD("get_next"), &_Directory::get_next);
  1409. ClassDB::bind_method(D_METHOD("current_is_dir"), &_Directory::current_is_dir);
  1410. ClassDB::bind_method(D_METHOD("list_dir_end"), &_Directory::list_dir_end);
  1411. ClassDB::bind_method(D_METHOD("get_drive_count"), &_Directory::get_drive_count);
  1412. ClassDB::bind_method(D_METHOD("get_drive", "idx"), &_Directory::get_drive);
  1413. ClassDB::bind_method(D_METHOD("get_current_drive"), &_Directory::get_current_drive);
  1414. ClassDB::bind_method(D_METHOD("change_dir", "todir"), &_Directory::change_dir);
  1415. ClassDB::bind_method(D_METHOD("get_current_dir"), &_Directory::get_current_dir);
  1416. ClassDB::bind_method(D_METHOD("make_dir", "path"), &_Directory::make_dir);
  1417. ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &_Directory::make_dir_recursive);
  1418. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_Directory::file_exists);
  1419. ClassDB::bind_method(D_METHOD("dir_exists", "path"), &_Directory::dir_exists);
  1420. //ClassDB::bind_method(D_METHOD("get_modified_time","file"),&_Directory::get_modified_time);
  1421. ClassDB::bind_method(D_METHOD("get_space_left"), &_Directory::get_space_left);
  1422. ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy);
  1423. ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename);
  1424. ClassDB::bind_method(D_METHOD("remove", "path"), &_Directory::remove);
  1425. }
  1426. _Directory::_Directory() {
  1427. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1428. }
  1429. _Directory::~_Directory() {
  1430. if (d)
  1431. memdelete(d);
  1432. }
  1433. _Marshalls *_Marshalls::singleton = NULL;
  1434. _Marshalls *_Marshalls::get_singleton() {
  1435. return singleton;
  1436. }
  1437. String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  1438. int len;
  1439. Error err = encode_variant(p_var, NULL, len, p_full_objects);
  1440. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1441. Vector<uint8_t> buff;
  1442. buff.resize(len);
  1443. uint8_t *w = buff.ptrw();
  1444. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1445. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1446. String ret = CryptoCore::b64_encode_str(&w[0], len);
  1447. ERR_FAIL_COND_V(ret == "", ret);
  1448. return ret;
  1449. };
  1450. Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  1451. int strlen = p_str.length();
  1452. CharString cstr = p_str.ascii();
  1453. Vector<uint8_t> buf;
  1454. buf.resize(strlen / 4 * 3 + 1);
  1455. uint8_t *w = buf.ptrw();
  1456. size_t len = 0;
  1457. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  1458. Variant v;
  1459. Error err = decode_variant(v, &w[0], len, NULL, p_allow_objects);
  1460. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  1461. return v;
  1462. };
  1463. String _Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  1464. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  1465. ERR_FAIL_COND_V(ret == "", ret);
  1466. return ret;
  1467. };
  1468. Vector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
  1469. int strlen = p_str.length();
  1470. CharString cstr = p_str.ascii();
  1471. size_t arr_len = 0;
  1472. Vector<uint8_t> buf;
  1473. {
  1474. buf.resize(strlen / 4 * 3 + 1);
  1475. uint8_t *w = buf.ptrw();
  1476. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  1477. }
  1478. buf.resize(arr_len);
  1479. return buf;
  1480. };
  1481. String _Marshalls::utf8_to_base64(const String &p_str) {
  1482. CharString cstr = p_str.utf8();
  1483. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  1484. ERR_FAIL_COND_V(ret == "", ret);
  1485. return ret;
  1486. };
  1487. String _Marshalls::base64_to_utf8(const String &p_str) {
  1488. int strlen = p_str.length();
  1489. CharString cstr = p_str.ascii();
  1490. Vector<uint8_t> buf;
  1491. buf.resize(strlen / 4 * 3 + 1 + 1);
  1492. uint8_t *w = buf.ptrw();
  1493. size_t len = 0;
  1494. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  1495. w[len] = 0;
  1496. String ret = String::utf8((char *)&w[0]);
  1497. return ret;
  1498. };
  1499. void _Marshalls::_bind_methods() {
  1500. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &_Marshalls::variant_to_base64, DEFVAL(false));
  1501. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &_Marshalls::base64_to_variant, DEFVAL(false));
  1502. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &_Marshalls::raw_to_base64);
  1503. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &_Marshalls::base64_to_raw);
  1504. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &_Marshalls::utf8_to_base64);
  1505. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &_Marshalls::base64_to_utf8);
  1506. };
  1507. ////////////////
  1508. void _Semaphore::wait() {
  1509. semaphore.wait();
  1510. }
  1511. Error _Semaphore::try_wait() {
  1512. return semaphore.try_wait() ? OK : ERR_BUSY;
  1513. }
  1514. void _Semaphore::post() {
  1515. semaphore.post();
  1516. }
  1517. void _Semaphore::_bind_methods() {
  1518. ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait);
  1519. ClassDB::bind_method(D_METHOD("try_wait"), &_Semaphore::try_wait);
  1520. ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
  1521. }
  1522. ///////////////
  1523. void _Mutex::lock() {
  1524. mutex.lock();
  1525. }
  1526. Error _Mutex::try_lock() {
  1527. return mutex.try_lock();
  1528. }
  1529. void _Mutex::unlock() {
  1530. mutex.unlock();
  1531. }
  1532. void _Mutex::_bind_methods() {
  1533. ClassDB::bind_method(D_METHOD("lock"), &_Mutex::lock);
  1534. ClassDB::bind_method(D_METHOD("try_lock"), &_Mutex::try_lock);
  1535. ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock);
  1536. }
  1537. ///////////////
  1538. void _Thread::_start_func(void *ud) {
  1539. Ref<_Thread> *tud = (Ref<_Thread> *)ud;
  1540. Ref<_Thread> t = *tud;
  1541. memdelete(tud);
  1542. Callable::CallError ce;
  1543. const Variant *arg[1] = { &t->userdata };
  1544. Thread::set_name(t->target_method);
  1545. t->ret = t->target_instance->call(t->target_method, arg, 1, ce);
  1546. if (ce.error != Callable::CallError::CALL_OK) {
  1547. String reason;
  1548. switch (ce.error) {
  1549. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1550. reason = "Invalid Argument #" + itos(ce.argument);
  1551. } break;
  1552. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1553. reason = "Too Many Arguments";
  1554. } break;
  1555. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1556. reason = "Too Few Arguments";
  1557. } break;
  1558. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1559. reason = "Method Not Found";
  1560. } break;
  1561. default: {
  1562. }
  1563. }
  1564. ERR_FAIL_MSG("Could not call function '" + t->target_method.operator String() + "' to start thread " + t->get_id() + ": " + reason + ".");
  1565. }
  1566. }
  1567. Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {
  1568. ERR_FAIL_COND_V_MSG(active, ERR_ALREADY_IN_USE, "Thread already started.");
  1569. ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
  1570. ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
  1571. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  1572. ret = Variant();
  1573. target_method = p_method;
  1574. target_instance = p_instance;
  1575. userdata = p_userdata;
  1576. active = true;
  1577. Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
  1578. Thread::Settings s;
  1579. s.priority = (Thread::Priority)p_priority;
  1580. thread = Thread::create(_start_func, ud, s);
  1581. if (!thread) {
  1582. active = false;
  1583. target_method = StringName();
  1584. target_instance = NULL;
  1585. userdata = Variant();
  1586. return ERR_CANT_CREATE;
  1587. }
  1588. return OK;
  1589. }
  1590. String _Thread::get_id() const {
  1591. if (!thread)
  1592. return String();
  1593. return itos(thread->get_id());
  1594. }
  1595. bool _Thread::is_active() const {
  1596. return active;
  1597. }
  1598. Variant _Thread::wait_to_finish() {
  1599. ERR_FAIL_COND_V_MSG(!thread, Variant(), "Thread must exist to wait for its completion.");
  1600. ERR_FAIL_COND_V_MSG(!active, Variant(), "Thread must be active to wait for its completion.");
  1601. Thread::wait_to_finish(thread);
  1602. Variant r = ret;
  1603. active = false;
  1604. target_method = StringName();
  1605. target_instance = NULL;
  1606. userdata = Variant();
  1607. if (thread)
  1608. memdelete(thread);
  1609. thread = NULL;
  1610. return r;
  1611. }
  1612. void _Thread::_bind_methods() {
  1613. ClassDB::bind_method(D_METHOD("start", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
  1614. ClassDB::bind_method(D_METHOD("get_id"), &_Thread::get_id);
  1615. ClassDB::bind_method(D_METHOD("is_active"), &_Thread::is_active);
  1616. ClassDB::bind_method(D_METHOD("wait_to_finish"), &_Thread::wait_to_finish);
  1617. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  1618. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  1619. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  1620. }
  1621. _Thread::_Thread() {
  1622. active = false;
  1623. thread = NULL;
  1624. target_instance = NULL;
  1625. }
  1626. _Thread::~_Thread() {
  1627. ERR_FAIL_COND_MSG(active, "Reference to a Thread object was lost while the thread is still running...");
  1628. }
  1629. /////////////////////////////////////
  1630. PackedStringArray _ClassDB::get_class_list() const {
  1631. List<StringName> classes;
  1632. ClassDB::get_class_list(&classes);
  1633. PackedStringArray ret;
  1634. ret.resize(classes.size());
  1635. int idx = 0;
  1636. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  1637. ret.set(idx++, E->get());
  1638. }
  1639. return ret;
  1640. }
  1641. PackedStringArray _ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  1642. List<StringName> classes;
  1643. ClassDB::get_inheriters_from_class(p_class, &classes);
  1644. PackedStringArray ret;
  1645. ret.resize(classes.size());
  1646. int idx = 0;
  1647. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  1648. ret.set(idx++, E->get());
  1649. }
  1650. return ret;
  1651. }
  1652. StringName _ClassDB::get_parent_class(const StringName &p_class) const {
  1653. return ClassDB::get_parent_class(p_class);
  1654. }
  1655. bool _ClassDB::class_exists(const StringName &p_class) const {
  1656. return ClassDB::class_exists(p_class);
  1657. }
  1658. bool _ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  1659. return ClassDB::is_parent_class(p_class, p_inherits);
  1660. }
  1661. bool _ClassDB::can_instance(const StringName &p_class) const {
  1662. return ClassDB::can_instance(p_class);
  1663. }
  1664. Variant _ClassDB::instance(const StringName &p_class) const {
  1665. Object *obj = ClassDB::instance(p_class);
  1666. if (!obj)
  1667. return Variant();
  1668. Reference *r = Object::cast_to<Reference>(obj);
  1669. if (r) {
  1670. return REF(r);
  1671. } else {
  1672. return obj;
  1673. }
  1674. }
  1675. bool _ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  1676. return ClassDB::has_signal(p_class, p_signal);
  1677. }
  1678. Dictionary _ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  1679. MethodInfo signal;
  1680. if (ClassDB::get_signal(p_class, p_signal, &signal)) {
  1681. return signal.operator Dictionary();
  1682. } else {
  1683. return Dictionary();
  1684. }
  1685. }
  1686. Array _ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  1687. List<MethodInfo> signals;
  1688. ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1689. Array ret;
  1690. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  1691. ret.push_back(E->get().operator Dictionary());
  1692. }
  1693. return ret;
  1694. }
  1695. Array _ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  1696. List<PropertyInfo> plist;
  1697. ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1698. Array ret;
  1699. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1700. ret.push_back(E->get().operator Dictionary());
  1701. }
  1702. return ret;
  1703. }
  1704. Variant _ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  1705. Variant ret;
  1706. ClassDB::get_property(p_object, p_property, ret);
  1707. return ret;
  1708. }
  1709. Error _ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1710. Variant ret;
  1711. bool valid;
  1712. if (!ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1713. return ERR_UNAVAILABLE;
  1714. } else if (!valid) {
  1715. return ERR_INVALID_DATA;
  1716. }
  1717. return OK;
  1718. }
  1719. bool _ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  1720. return ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1721. }
  1722. Array _ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  1723. List<MethodInfo> methods;
  1724. ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1725. Array ret;
  1726. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1727. #ifdef DEBUG_METHODS_ENABLED
  1728. ret.push_back(E->get().operator Dictionary());
  1729. #else
  1730. Dictionary dict;
  1731. dict["name"] = E->get().name;
  1732. ret.push_back(dict);
  1733. #endif
  1734. }
  1735. return ret;
  1736. }
  1737. PackedStringArray _ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1738. List<String> constants;
  1739. ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1740. PackedStringArray ret;
  1741. ret.resize(constants.size());
  1742. int idx = 0;
  1743. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1744. ret.set(idx++, E->get());
  1745. }
  1746. return ret;
  1747. }
  1748. bool _ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1749. bool success;
  1750. ClassDB::get_integer_constant(p_class, p_name, &success);
  1751. return success;
  1752. }
  1753. int _ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1754. bool found;
  1755. int c = ClassDB::get_integer_constant(p_class, p_name, &found);
  1756. ERR_FAIL_COND_V(!found, 0);
  1757. return c;
  1758. }
  1759. StringName _ClassDB::get_category(const StringName &p_node) const {
  1760. return ClassDB::get_category(p_node);
  1761. }
  1762. bool _ClassDB::is_class_enabled(StringName p_class) const {
  1763. return ClassDB::is_class_enabled(p_class);
  1764. }
  1765. void _ClassDB::_bind_methods() {
  1766. ClassDB::bind_method(D_METHOD("get_class_list"), &_ClassDB::get_class_list);
  1767. ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &_ClassDB::get_inheriters_from_class);
  1768. ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &_ClassDB::get_parent_class);
  1769. ClassDB::bind_method(D_METHOD("class_exists", "class"), &_ClassDB::class_exists);
  1770. ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &_ClassDB::is_parent_class);
  1771. ClassDB::bind_method(D_METHOD("can_instance", "class"), &_ClassDB::can_instance);
  1772. ClassDB::bind_method(D_METHOD("instance", "class"), &_ClassDB::instance);
  1773. ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &_ClassDB::has_signal);
  1774. ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &_ClassDB::get_signal);
  1775. ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &_ClassDB::get_signal_list, DEFVAL(false));
  1776. ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &_ClassDB::get_property_list, DEFVAL(false));
  1777. ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &_ClassDB::get_property);
  1778. ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &_ClassDB::set_property);
  1779. ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &_ClassDB::has_method, DEFVAL(false));
  1780. ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &_ClassDB::get_method_list, DEFVAL(false));
  1781. ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &_ClassDB::get_integer_constant_list, DEFVAL(false));
  1782. ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &_ClassDB::has_integer_constant);
  1783. ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &_ClassDB::get_integer_constant);
  1784. ClassDB::bind_method(D_METHOD("class_get_category", "class"), &_ClassDB::get_category);
  1785. ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &_ClassDB::is_class_enabled);
  1786. }
  1787. _ClassDB::_ClassDB() {
  1788. }
  1789. _ClassDB::~_ClassDB() {
  1790. }
  1791. ///////////////////////////////
  1792. void _Engine::set_iterations_per_second(int p_ips) {
  1793. Engine::get_singleton()->set_iterations_per_second(p_ips);
  1794. }
  1795. int _Engine::get_iterations_per_second() const {
  1796. return Engine::get_singleton()->get_iterations_per_second();
  1797. }
  1798. void _Engine::set_physics_jitter_fix(float p_threshold) {
  1799. Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1800. }
  1801. float _Engine::get_physics_jitter_fix() const {
  1802. return Engine::get_singleton()->get_physics_jitter_fix();
  1803. }
  1804. float _Engine::get_physics_interpolation_fraction() const {
  1805. return Engine::get_singleton()->get_physics_interpolation_fraction();
  1806. }
  1807. void _Engine::set_target_fps(int p_fps) {
  1808. Engine::get_singleton()->set_target_fps(p_fps);
  1809. }
  1810. int _Engine::get_target_fps() const {
  1811. return Engine::get_singleton()->get_target_fps();
  1812. }
  1813. float _Engine::get_frames_per_second() const {
  1814. return Engine::get_singleton()->get_frames_per_second();
  1815. }
  1816. uint64_t _Engine::get_physics_frames() const {
  1817. return Engine::get_singleton()->get_physics_frames();
  1818. }
  1819. uint64_t _Engine::get_idle_frames() const {
  1820. return Engine::get_singleton()->get_idle_frames();
  1821. }
  1822. void _Engine::set_time_scale(float p_scale) {
  1823. Engine::get_singleton()->set_time_scale(p_scale);
  1824. }
  1825. float _Engine::get_time_scale() {
  1826. return Engine::get_singleton()->get_time_scale();
  1827. }
  1828. int _Engine::get_frames_drawn() {
  1829. return Engine::get_singleton()->get_frames_drawn();
  1830. }
  1831. MainLoop *_Engine::get_main_loop() const {
  1832. //needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1833. return OS::get_singleton()->get_main_loop();
  1834. }
  1835. Dictionary _Engine::get_version_info() const {
  1836. return Engine::get_singleton()->get_version_info();
  1837. }
  1838. Dictionary _Engine::get_author_info() const {
  1839. return Engine::get_singleton()->get_author_info();
  1840. }
  1841. Array _Engine::get_copyright_info() const {
  1842. return Engine::get_singleton()->get_copyright_info();
  1843. }
  1844. Dictionary _Engine::get_donor_info() const {
  1845. return Engine::get_singleton()->get_donor_info();
  1846. }
  1847. Dictionary _Engine::get_license_info() const {
  1848. return Engine::get_singleton()->get_license_info();
  1849. }
  1850. String _Engine::get_license_text() const {
  1851. return Engine::get_singleton()->get_license_text();
  1852. }
  1853. bool _Engine::is_in_physics_frame() const {
  1854. return Engine::get_singleton()->is_in_physics_frame();
  1855. }
  1856. bool _Engine::has_singleton(const String &p_name) const {
  1857. return Engine::get_singleton()->has_singleton(p_name);
  1858. }
  1859. Object *_Engine::get_singleton_object(const String &p_name) const {
  1860. return Engine::get_singleton()->get_singleton_object(p_name);
  1861. }
  1862. void _Engine::set_editor_hint(bool p_enabled) {
  1863. Engine::get_singleton()->set_editor_hint(p_enabled);
  1864. }
  1865. bool _Engine::is_editor_hint() const {
  1866. return Engine::get_singleton()->is_editor_hint();
  1867. }
  1868. void _Engine::_bind_methods() {
  1869. ClassDB::bind_method(D_METHOD("set_iterations_per_second", "iterations_per_second"), &_Engine::set_iterations_per_second);
  1870. ClassDB::bind_method(D_METHOD("get_iterations_per_second"), &_Engine::get_iterations_per_second);
  1871. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &_Engine::set_physics_jitter_fix);
  1872. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &_Engine::get_physics_jitter_fix);
  1873. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &_Engine::get_physics_interpolation_fraction);
  1874. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &_Engine::set_target_fps);
  1875. ClassDB::bind_method(D_METHOD("get_target_fps"), &_Engine::get_target_fps);
  1876. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &_Engine::set_time_scale);
  1877. ClassDB::bind_method(D_METHOD("get_time_scale"), &_Engine::get_time_scale);
  1878. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &_Engine::get_frames_drawn);
  1879. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &_Engine::get_frames_per_second);
  1880. ClassDB::bind_method(D_METHOD("get_physics_frames"), &_Engine::get_physics_frames);
  1881. ClassDB::bind_method(D_METHOD("get_idle_frames"), &_Engine::get_idle_frames);
  1882. ClassDB::bind_method(D_METHOD("get_main_loop"), &_Engine::get_main_loop);
  1883. ClassDB::bind_method(D_METHOD("get_version_info"), &_Engine::get_version_info);
  1884. ClassDB::bind_method(D_METHOD("get_author_info"), &_Engine::get_author_info);
  1885. ClassDB::bind_method(D_METHOD("get_copyright_info"), &_Engine::get_copyright_info);
  1886. ClassDB::bind_method(D_METHOD("get_donor_info"), &_Engine::get_donor_info);
  1887. ClassDB::bind_method(D_METHOD("get_license_info"), &_Engine::get_license_info);
  1888. ClassDB::bind_method(D_METHOD("get_license_text"), &_Engine::get_license_text);
  1889. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &_Engine::is_in_physics_frame);
  1890. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &_Engine::has_singleton);
  1891. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &_Engine::get_singleton_object);
  1892. ClassDB::bind_method(D_METHOD("set_editor_hint", "enabled"), &_Engine::set_editor_hint);
  1893. ClassDB::bind_method(D_METHOD("is_editor_hint"), &_Engine::is_editor_hint);
  1894. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_hint"), "set_editor_hint", "is_editor_hint");
  1895. ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations_per_second"), "set_iterations_per_second", "get_iterations_per_second");
  1896. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  1897. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1898. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1899. }
  1900. _Engine *_Engine::singleton = NULL;
  1901. _Engine::_Engine() {
  1902. singleton = this;
  1903. }
  1904. void JSONParseResult::_bind_methods() {
  1905. ClassDB::bind_method(D_METHOD("get_error"), &JSONParseResult::get_error);
  1906. ClassDB::bind_method(D_METHOD("get_error_string"), &JSONParseResult::get_error_string);
  1907. ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParseResult::get_error_line);
  1908. ClassDB::bind_method(D_METHOD("get_result"), &JSONParseResult::get_result);
  1909. ClassDB::bind_method(D_METHOD("set_error", "error"), &JSONParseResult::set_error);
  1910. ClassDB::bind_method(D_METHOD("set_error_string", "error_string"), &JSONParseResult::set_error_string);
  1911. ClassDB::bind_method(D_METHOD("set_error_line", "error_line"), &JSONParseResult::set_error_line);
  1912. ClassDB::bind_method(D_METHOD("set_result", "result"), &JSONParseResult::set_result);
  1913. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "error", PROPERTY_HINT_NONE, "Error", PROPERTY_USAGE_CLASS_IS_ENUM), "set_error", "get_error");
  1914. ADD_PROPERTY(PropertyInfo(Variant::STRING, "error_string"), "set_error_string", "get_error_string");
  1915. ADD_PROPERTY(PropertyInfo(Variant::INT, "error_line"), "set_error_line", "get_error_line");
  1916. ADD_PROPERTY(PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "set_result", "get_result");
  1917. }
  1918. void JSONParseResult::set_error(Error p_error) {
  1919. error = p_error;
  1920. }
  1921. Error JSONParseResult::get_error() const {
  1922. return error;
  1923. }
  1924. void JSONParseResult::set_error_string(const String &p_error_string) {
  1925. error_string = p_error_string;
  1926. }
  1927. String JSONParseResult::get_error_string() const {
  1928. return error_string;
  1929. }
  1930. void JSONParseResult::set_error_line(int p_error_line) {
  1931. error_line = p_error_line;
  1932. }
  1933. int JSONParseResult::get_error_line() const {
  1934. return error_line;
  1935. }
  1936. void JSONParseResult::set_result(const Variant &p_result) {
  1937. result = p_result;
  1938. }
  1939. Variant JSONParseResult::get_result() const {
  1940. return result;
  1941. }
  1942. void _JSON::_bind_methods() {
  1943. ClassDB::bind_method(D_METHOD("print", "value", "indent", "sort_keys"), &_JSON::print, DEFVAL(String()), DEFVAL(false));
  1944. ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse);
  1945. }
  1946. String _JSON::print(const Variant &p_value, const String &p_indent, bool p_sort_keys) {
  1947. return JSON::print(p_value, p_indent, p_sort_keys);
  1948. }
  1949. Ref<JSONParseResult> _JSON::parse(const String &p_json) {
  1950. Ref<JSONParseResult> result;
  1951. result.instance();
  1952. result->error = JSON::parse(p_json, result->result, result->error_string, result->error_line);
  1953. if (result->error != OK) {
  1954. ERR_PRINT(vformat("Error parsing JSON at line %s: %s", result->error_line, result->error_string));
  1955. }
  1956. return result;
  1957. }
  1958. _JSON *_JSON::singleton = NULL;
  1959. _JSON::_JSON() {
  1960. singleton = this;
  1961. }