core_bind.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  1. /*************************************************************************/
  2. /* core_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/config/project_settings.h"
  32. #include "core/crypto/crypto_core.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/io/file_access_compressed.h"
  35. #include "core/io/file_access_encrypted.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/math/geometry_2d.h"
  38. #include "core/math/geometry_3d.h"
  39. #include "core/os/keyboard.h"
  40. #include "core/os/os.h"
  41. ////// _ResourceLoader //////
  42. _ResourceLoader *_ResourceLoader::singleton = nullptr;
  43. Error _ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads) {
  44. return ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads);
  45. }
  46. _ResourceLoader::ThreadLoadStatus _ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
  47. float progress = 0;
  48. ResourceLoader::ThreadLoadStatus tls = ResourceLoader::load_threaded_get_status(p_path, &progress);
  49. r_progress.resize(1);
  50. r_progress[0] = progress;
  51. return (ThreadLoadStatus)tls;
  52. }
  53. RES _ResourceLoader::load_threaded_get(const String &p_path) {
  54. Error error;
  55. RES res = ResourceLoader::load_threaded_get(p_path, &error);
  56. return res;
  57. }
  58. RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, CacheMode p_cache_mode) {
  59. Error err = OK;
  60. RES ret = ResourceLoader::load(p_path, p_type_hint, ResourceFormatLoader::CacheMode(p_cache_mode), &err);
  61. ERR_FAIL_COND_V_MSG(err != OK, ret, "Error loading resource: '" + p_path + "'.");
  62. return ret;
  63. }
  64. Vector<String> _ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
  65. List<String> exts;
  66. ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
  67. Vector<String> ret;
  68. for (const String &E : exts) {
  69. ret.push_back(E);
  70. }
  71. return ret;
  72. }
  73. void _ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  74. ResourceLoader::set_abort_on_missing_resources(p_abort);
  75. }
  76. PackedStringArray _ResourceLoader::get_dependencies(const String &p_path) {
  77. List<String> deps;
  78. ResourceLoader::get_dependencies(p_path, &deps);
  79. PackedStringArray ret;
  80. for (const String &E : deps) {
  81. ret.push_back(E);
  82. }
  83. return ret;
  84. }
  85. bool _ResourceLoader::has_cached(const String &p_path) {
  86. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  87. return ResourceCache::has(local_path);
  88. }
  89. bool _ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  90. return ResourceLoader::exists(p_path, p_type_hint);
  91. }
  92. void _ResourceLoader::_bind_methods() {
  93. ClassDB::bind_method(D_METHOD("load_threaded_request", "path", "type_hint", "use_sub_threads"), &_ResourceLoader::load_threaded_request, DEFVAL(""), DEFVAL(false));
  94. ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &_ResourceLoader::load_threaded_get_status, DEFVAL(Array()));
  95. ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &_ResourceLoader::load_threaded_get);
  96. ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &_ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
  97. ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type);
  98. ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources);
  99. ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
  100. ClassDB::bind_method(D_METHOD("has_cached", "path"), &_ResourceLoader::has_cached);
  101. ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &_ResourceLoader::exists, DEFVAL(""));
  102. BIND_ENUM_CONSTANT(THREAD_LOAD_INVALID_RESOURCE);
  103. BIND_ENUM_CONSTANT(THREAD_LOAD_IN_PROGRESS);
  104. BIND_ENUM_CONSTANT(THREAD_LOAD_FAILED);
  105. BIND_ENUM_CONSTANT(THREAD_LOAD_LOADED);
  106. BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE);
  107. BIND_ENUM_CONSTANT(CACHE_MODE_REUSE);
  108. BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);
  109. }
  110. ////// _ResourceSaver //////
  111. Error _ResourceSaver::save(const String &p_path, const RES &p_resource, SaverFlags p_flags) {
  112. ERR_FAIL_COND_V_MSG(p_resource.is_null(), ERR_INVALID_PARAMETER, "Can't save empty resource to path '" + String(p_path) + "'.");
  113. return ResourceSaver::save(p_path, p_resource, p_flags);
  114. }
  115. Vector<String> _ResourceSaver::get_recognized_extensions(const RES &p_resource) {
  116. ERR_FAIL_COND_V_MSG(p_resource.is_null(), Vector<String>(), "It's not a reference to a valid Resource object.");
  117. List<String> exts;
  118. ResourceSaver::get_recognized_extensions(p_resource, &exts);
  119. Vector<String> ret;
  120. for (const String &E : exts) {
  121. ret.push_back(E);
  122. }
  123. return ret;
  124. }
  125. _ResourceSaver *_ResourceSaver::singleton = nullptr;
  126. void _ResourceSaver::_bind_methods() {
  127. ClassDB::bind_method(D_METHOD("save", "path", "resource", "flags"), &_ResourceSaver::save, DEFVAL(0));
  128. ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &_ResourceSaver::get_recognized_extensions);
  129. BIND_ENUM_CONSTANT(FLAG_RELATIVE_PATHS);
  130. BIND_ENUM_CONSTANT(FLAG_BUNDLE_RESOURCES);
  131. BIND_ENUM_CONSTANT(FLAG_CHANGE_PATH);
  132. BIND_ENUM_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
  133. BIND_ENUM_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
  134. BIND_ENUM_CONSTANT(FLAG_COMPRESS);
  135. BIND_ENUM_CONSTANT(FLAG_REPLACE_SUBRESOURCE_PATHS);
  136. }
  137. ////// _OS //////
  138. PackedStringArray _OS::get_connected_midi_inputs() {
  139. return OS::get_singleton()->get_connected_midi_inputs();
  140. }
  141. void _OS::open_midi_inputs() {
  142. OS::get_singleton()->open_midi_inputs();
  143. }
  144. void _OS::close_midi_inputs() {
  145. OS::get_singleton()->close_midi_inputs();
  146. }
  147. void _OS::set_use_file_access_save_and_swap(bool p_enable) {
  148. FileAccess::set_backup_save(p_enable);
  149. }
  150. void _OS::set_low_processor_usage_mode(bool p_enabled) {
  151. OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  152. }
  153. bool _OS::is_in_low_processor_usage_mode() const {
  154. return OS::get_singleton()->is_in_low_processor_usage_mode();
  155. }
  156. void _OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  157. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(p_usec);
  158. }
  159. int _OS::get_low_processor_usage_mode_sleep_usec() const {
  160. return OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
  161. }
  162. void _OS::alert(const String &p_alert, const String &p_title) {
  163. OS::get_singleton()->alert(p_alert, p_title);
  164. }
  165. String _OS::get_executable_path() const {
  166. return OS::get_singleton()->get_executable_path();
  167. }
  168. Error _OS::shell_open(String p_uri) {
  169. if (p_uri.begins_with("res://")) {
  170. WARN_PRINT("Attempting to open an URL with the \"res://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_open()`.");
  171. } else if (p_uri.begins_with("user://")) {
  172. WARN_PRINT("Attempting to open an URL with the \"user://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_open()`.");
  173. }
  174. return OS::get_singleton()->shell_open(p_uri);
  175. }
  176. int _OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr) {
  177. List<String> args;
  178. for (int i = 0; i < p_arguments.size(); i++) {
  179. args.push_back(p_arguments[i]);
  180. }
  181. String pipe;
  182. int exitcode = 0;
  183. Error err = OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr);
  184. r_output.push_back(pipe);
  185. if (err != OK) {
  186. return -1;
  187. }
  188. return exitcode;
  189. }
  190. int _OS::create_process(const String &p_path, const Vector<String> &p_arguments) {
  191. List<String> args;
  192. for (int i = 0; i < p_arguments.size(); i++) {
  193. args.push_back(p_arguments[i]);
  194. }
  195. OS::ProcessID pid = 0;
  196. Error err = OS::get_singleton()->create_process(p_path, args, &pid);
  197. if (err != OK) {
  198. return -1;
  199. }
  200. return pid;
  201. }
  202. Error _OS::kill(int p_pid) {
  203. return OS::get_singleton()->kill(p_pid);
  204. }
  205. int _OS::get_process_id() const {
  206. return OS::get_singleton()->get_process_id();
  207. }
  208. bool _OS::has_environment(const String &p_var) const {
  209. return OS::get_singleton()->has_environment(p_var);
  210. }
  211. String _OS::get_environment(const String &p_var) const {
  212. return OS::get_singleton()->get_environment(p_var);
  213. }
  214. bool _OS::set_environment(const String &p_var, const String &p_value) const {
  215. return OS::get_singleton()->set_environment(p_var, p_value);
  216. }
  217. String _OS::get_name() const {
  218. return OS::get_singleton()->get_name();
  219. }
  220. Vector<String> _OS::get_cmdline_args() {
  221. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  222. Vector<String> cmdlinev;
  223. for (const String &E : cmdline) {
  224. cmdlinev.push_back(E);
  225. }
  226. return cmdlinev;
  227. }
  228. String _OS::get_locale() const {
  229. return OS::get_singleton()->get_locale();
  230. }
  231. String _OS::get_model_name() const {
  232. return OS::get_singleton()->get_model_name();
  233. }
  234. Error _OS::set_thread_name(const String &p_name) {
  235. return Thread::set_name(p_name);
  236. }
  237. Thread::ID _OS::get_thread_caller_id() const {
  238. return Thread::get_caller_id();
  239. };
  240. bool _OS::has_feature(const String &p_feature) const {
  241. return OS::get_singleton()->has_feature(p_feature);
  242. }
  243. uint64_t _OS::get_static_memory_usage() const {
  244. return OS::get_singleton()->get_static_memory_usage();
  245. }
  246. uint64_t _OS::get_static_memory_peak_usage() const {
  247. return OS::get_singleton()->get_static_memory_peak_usage();
  248. }
  249. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  250. void _OS::delay_usec(int p_usec) const {
  251. ERR_FAIL_COND_MSG(
  252. p_usec < 0,
  253. vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
  254. OS::get_singleton()->delay_usec(p_usec);
  255. }
  256. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  257. void _OS::delay_msec(int p_msec) const {
  258. ERR_FAIL_COND_MSG(
  259. p_msec < 0,
  260. vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
  261. OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  262. }
  263. bool _OS::can_use_threads() const {
  264. return OS::get_singleton()->can_use_threads();
  265. }
  266. bool _OS::is_userfs_persistent() const {
  267. return OS::get_singleton()->is_userfs_persistent();
  268. }
  269. int _OS::get_processor_count() const {
  270. return OS::get_singleton()->get_processor_count();
  271. }
  272. bool _OS::is_stdout_verbose() const {
  273. return OS::get_singleton()->is_stdout_verbose();
  274. }
  275. void _OS::dump_memory_to_file(const String &p_file) {
  276. OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
  277. }
  278. struct _OSCoreBindImg {
  279. String path;
  280. Size2 size;
  281. int fmt = 0;
  282. ObjectID id;
  283. int vram = 0;
  284. bool operator<(const _OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
  285. };
  286. void _OS::print_all_textures_by_size() {
  287. List<_OSCoreBindImg> imgs;
  288. uint64_t total = 0;
  289. {
  290. List<Ref<Resource>> rsrc;
  291. ResourceCache::get_cached_resources(&rsrc);
  292. for (Ref<Resource> &res : rsrc) {
  293. if (!res->is_class("ImageTexture")) {
  294. continue;
  295. }
  296. Size2 size = res->call("get_size");
  297. int fmt = res->call("get_format");
  298. _OSCoreBindImg img;
  299. img.size = size;
  300. img.fmt = fmt;
  301. img.path = res->get_path();
  302. img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt));
  303. img.id = res->get_instance_id();
  304. total += img.vram;
  305. imgs.push_back(img);
  306. }
  307. }
  308. imgs.sort();
  309. for (_OSCoreBindImg &E : imgs) {
  310. total -= E.vram;
  311. }
  312. }
  313. void _OS::print_resources_by_type(const Vector<String> &p_types) {
  314. Map<String, int> type_count;
  315. List<Ref<Resource>> resources;
  316. ResourceCache::get_cached_resources(&resources);
  317. for (const Ref<Resource> &r : resources) {
  318. bool found = false;
  319. for (int i = 0; i < p_types.size(); i++) {
  320. if (r->is_class(p_types[i])) {
  321. found = true;
  322. }
  323. }
  324. if (!found) {
  325. continue;
  326. }
  327. if (!type_count.has(r->get_class())) {
  328. type_count[r->get_class()] = 0;
  329. }
  330. type_count[r->get_class()]++;
  331. }
  332. }
  333. void _OS::print_all_resources(const String &p_to_file) {
  334. OS::get_singleton()->print_all_resources(p_to_file);
  335. }
  336. void _OS::print_resources_in_use(bool p_short) {
  337. OS::get_singleton()->print_resources_in_use(p_short);
  338. }
  339. void _OS::dump_resources_to_file(const String &p_file) {
  340. OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  341. }
  342. String _OS::get_user_data_dir() const {
  343. return OS::get_singleton()->get_user_data_dir();
  344. }
  345. String _OS::get_external_data_dir() const {
  346. return OS::get_singleton()->get_external_data_dir();
  347. }
  348. String _OS::get_config_dir() const {
  349. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  350. return OS::get_singleton()->get_config_path();
  351. }
  352. String _OS::get_data_dir() const {
  353. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  354. return OS::get_singleton()->get_data_path();
  355. }
  356. String _OS::get_cache_dir() const {
  357. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  358. return OS::get_singleton()->get_cache_path();
  359. }
  360. bool _OS::is_debug_build() const {
  361. #ifdef DEBUG_ENABLED
  362. return true;
  363. #else
  364. return false;
  365. #endif
  366. }
  367. String _OS::get_system_dir(SystemDir p_dir) const {
  368. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
  369. }
  370. String _OS::get_keycode_string(uint32_t p_code) const {
  371. return keycode_get_string(p_code);
  372. }
  373. bool _OS::is_keycode_unicode(uint32_t p_unicode) const {
  374. return keycode_has_unicode(p_unicode);
  375. }
  376. int _OS::find_keycode_from_string(const String &p_code) const {
  377. return find_keycode(p_code);
  378. }
  379. bool _OS::request_permission(const String &p_name) {
  380. return OS::get_singleton()->request_permission(p_name);
  381. }
  382. bool _OS::request_permissions() {
  383. return OS::get_singleton()->request_permissions();
  384. }
  385. Vector<String> _OS::get_granted_permissions() const {
  386. return OS::get_singleton()->get_granted_permissions();
  387. }
  388. String _OS::get_unique_id() const {
  389. return OS::get_singleton()->get_unique_id();
  390. }
  391. _OS *_OS::singleton = nullptr;
  392. void _OS::_bind_methods() {
  393. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &_OS::get_connected_midi_inputs);
  394. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &_OS::open_midi_inputs);
  395. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &_OS::close_midi_inputs);
  396. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &_OS::alert, DEFVAL("Alert!"));
  397. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
  398. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
  399. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &_OS::set_low_processor_usage_mode_sleep_usec);
  400. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &_OS::get_low_processor_usage_mode_sleep_usec);
  401. ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count);
  402. ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
  403. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr"), &_OS::execute, DEFVAL(Array()), DEFVAL(false));
  404. ClassDB::bind_method(D_METHOD("create_process", "path", "arguments"), &_OS::create_process);
  405. ClassDB::bind_method(D_METHOD("kill", "pid"), &_OS::kill);
  406. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &_OS::shell_open);
  407. ClassDB::bind_method(D_METHOD("get_process_id"), &_OS::get_process_id);
  408. ClassDB::bind_method(D_METHOD("get_environment", "variable"), &_OS::get_environment);
  409. ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &_OS::set_environment);
  410. ClassDB::bind_method(D_METHOD("has_environment", "variable"), &_OS::has_environment);
  411. ClassDB::bind_method(D_METHOD("get_name"), &_OS::get_name);
  412. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &_OS::get_cmdline_args);
  413. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &_OS::delay_usec);
  414. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &_OS::delay_msec);
  415. ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
  416. ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
  417. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &_OS::is_userfs_persistent);
  418. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &_OS::is_stdout_verbose);
  419. ClassDB::bind_method(D_METHOD("can_use_threads"), &_OS::can_use_threads);
  420. ClassDB::bind_method(D_METHOD("is_debug_build"), &_OS::is_debug_build);
  421. ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
  422. ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
  423. ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
  424. ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
  425. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &_OS::get_static_memory_usage);
  426. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
  427. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir);
  428. ClassDB::bind_method(D_METHOD("get_external_data_dir"), &_OS::get_external_data_dir);
  429. ClassDB::bind_method(D_METHOD("get_system_dir", "dir"), &_OS::get_system_dir);
  430. ClassDB::bind_method(D_METHOD("get_config_dir"), &_OS::get_config_dir);
  431. ClassDB::bind_method(D_METHOD("get_data_dir"), &_OS::get_data_dir);
  432. ClassDB::bind_method(D_METHOD("get_cache_dir"), &_OS::get_cache_dir);
  433. ClassDB::bind_method(D_METHOD("get_unique_id"), &_OS::get_unique_id);
  434. ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
  435. ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
  436. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &_OS::get_keycode_string);
  437. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &_OS::is_keycode_unicode);
  438. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &_OS::find_keycode_from_string);
  439. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
  440. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &_OS::set_thread_name);
  441. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &_OS::get_thread_caller_id);
  442. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &_OS::has_feature);
  443. ClassDB::bind_method(D_METHOD("request_permission", "name"), &_OS::request_permission);
  444. ClassDB::bind_method(D_METHOD("request_permissions"), &_OS::request_permissions);
  445. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &_OS::get_granted_permissions);
  446. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  447. 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");
  448. // Those default values need to be specified for the docs generator,
  449. // to avoid using values from the documentation writer's own OS instance.
  450. ADD_PROPERTY_DEFAULT("exit_code", 0);
  451. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  452. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  453. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES2);
  454. BIND_ENUM_CONSTANT(VIDEO_DRIVER_VULKAN);
  455. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  456. BIND_ENUM_CONSTANT(DAY_MONDAY);
  457. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  458. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  459. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  460. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  461. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  462. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  463. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  464. BIND_ENUM_CONSTANT(MONTH_MARCH);
  465. BIND_ENUM_CONSTANT(MONTH_APRIL);
  466. BIND_ENUM_CONSTANT(MONTH_MAY);
  467. BIND_ENUM_CONSTANT(MONTH_JUNE);
  468. BIND_ENUM_CONSTANT(MONTH_JULY);
  469. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  470. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  471. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  472. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  473. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  474. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  475. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  476. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  477. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  478. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  479. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  480. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  481. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  482. }
  483. ////// _Geometry2D //////
  484. _Geometry2D *_Geometry2D::singleton = nullptr;
  485. _Geometry2D *_Geometry2D::get_singleton() {
  486. return singleton;
  487. }
  488. bool _Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  489. return Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  490. }
  491. real_t _Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  492. return Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  493. }
  494. Variant _Geometry2D::segment_intersects_segment(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b) {
  495. Vector2 result;
  496. if (Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  497. return result;
  498. } else {
  499. return Variant();
  500. }
  501. }
  502. Variant _Geometry2D::line_intersects_line(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b) {
  503. Vector2 result;
  504. if (Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  505. return result;
  506. } else {
  507. return Variant();
  508. }
  509. }
  510. Vector<Vector2> _Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  511. Vector2 r1, r2;
  512. Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  513. Vector<Vector2> r;
  514. r.resize(2);
  515. r.set(0, r1);
  516. r.set(1, r2);
  517. return r;
  518. }
  519. Vector2 _Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  520. Vector2 s[2] = { p_a, p_b };
  521. return Geometry2D::get_closest_point_to_segment(p_point, s);
  522. }
  523. Vector2 _Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  524. Vector2 s[2] = { p_a, p_b };
  525. return Geometry2D::get_closest_point_to_segment_uncapped(p_point, s);
  526. }
  527. bool _Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  528. return Geometry2D::is_point_in_triangle(s, a, b, c);
  529. }
  530. bool _Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  531. return Geometry2D::is_polygon_clockwise(p_polygon);
  532. }
  533. bool _Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  534. return Geometry2D::is_point_in_polygon(p_point, p_polygon);
  535. }
  536. Vector<int> _Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  537. return Geometry2D::triangulate_polygon(p_polygon);
  538. }
  539. Vector<int> _Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
  540. return Geometry2D::triangulate_delaunay(p_points);
  541. }
  542. Vector<Point2> _Geometry2D::convex_hull(const Vector<Point2> &p_points) {
  543. return Geometry2D::convex_hull(p_points);
  544. }
  545. Array _Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  546. Vector<Vector<Point2>> polys = Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
  547. Array ret;
  548. for (int i = 0; i < polys.size(); ++i) {
  549. ret.push_back(polys[i]);
  550. }
  551. return ret;
  552. }
  553. Array _Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  554. Vector<Vector<Point2>> polys = Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
  555. Array ret;
  556. for (int i = 0; i < polys.size(); ++i) {
  557. ret.push_back(polys[i]);
  558. }
  559. return ret;
  560. }
  561. Array _Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  562. Vector<Vector<Point2>> polys = Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
  563. Array ret;
  564. for (int i = 0; i < polys.size(); ++i) {
  565. ret.push_back(polys[i]);
  566. }
  567. return ret;
  568. }
  569. Array _Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  570. Vector<Vector<Point2>> polys = Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b);
  571. Array ret;
  572. for (int i = 0; i < polys.size(); ++i) {
  573. ret.push_back(polys[i]);
  574. }
  575. return ret;
  576. }
  577. Array _Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  578. Vector<Vector<Point2>> polys = Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
  579. Array ret;
  580. for (int i = 0; i < polys.size(); ++i) {
  581. ret.push_back(polys[i]);
  582. }
  583. return ret;
  584. }
  585. Array _Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  586. Vector<Vector<Point2>> polys = Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon);
  587. Array ret;
  588. for (int i = 0; i < polys.size(); ++i) {
  589. ret.push_back(polys[i]);
  590. }
  591. return ret;
  592. }
  593. Array _Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  594. Vector<Vector<Point2>> polys = Geometry2D::offset_polygon(p_polygon, p_delta, Geometry2D::PolyJoinType(p_join_type));
  595. Array ret;
  596. for (int i = 0; i < polys.size(); ++i) {
  597. ret.push_back(polys[i]);
  598. }
  599. return ret;
  600. }
  601. Array _Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  602. Vector<Vector<Point2>> polys = Geometry2D::offset_polyline(p_polygon, p_delta, Geometry2D::PolyJoinType(p_join_type), Geometry2D::PolyEndType(p_end_type));
  603. Array ret;
  604. for (int i = 0; i < polys.size(); ++i) {
  605. ret.push_back(polys[i]);
  606. }
  607. return ret;
  608. }
  609. Dictionary _Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
  610. Dictionary ret;
  611. Vector<Size2i> rects;
  612. for (int i = 0; i < p_rects.size(); i++) {
  613. rects.push_back(p_rects[i]);
  614. }
  615. Vector<Point2i> result;
  616. Size2i size;
  617. Geometry2D::make_atlas(rects, result, size);
  618. Size2 r_size = size;
  619. Vector<Point2> r_result;
  620. for (int i = 0; i < result.size(); i++) {
  621. r_result.push_back(result[i]);
  622. }
  623. ret["points"] = r_result;
  624. ret["size"] = r_size;
  625. return ret;
  626. }
  627. void _Geometry2D::_bind_methods() {
  628. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &_Geometry2D::is_point_in_circle);
  629. ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &_Geometry2D::segment_intersects_segment);
  630. ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &_Geometry2D::line_intersects_line);
  631. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &_Geometry2D::get_closest_points_between_segments);
  632. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry2D::get_closest_point_to_segment);
  633. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry2D::get_closest_point_to_segment_uncapped);
  634. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry2D::point_is_inside_triangle);
  635. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &_Geometry2D::is_polygon_clockwise);
  636. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &_Geometry2D::is_point_in_polygon);
  637. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &_Geometry2D::triangulate_polygon);
  638. ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &_Geometry2D::triangulate_delaunay);
  639. ClassDB::bind_method(D_METHOD("convex_hull", "points"), &_Geometry2D::convex_hull);
  640. ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &_Geometry2D::merge_polygons);
  641. ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &_Geometry2D::clip_polygons);
  642. ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &_Geometry2D::intersect_polygons);
  643. ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &_Geometry2D::exclude_polygons);
  644. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &_Geometry2D::clip_polyline_with_polygon);
  645. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &_Geometry2D::intersect_polyline_with_polygon);
  646. ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &_Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
  647. ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &_Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  648. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &_Geometry2D::make_atlas);
  649. BIND_ENUM_CONSTANT(OPERATION_UNION);
  650. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  651. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  652. BIND_ENUM_CONSTANT(OPERATION_XOR);
  653. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  654. BIND_ENUM_CONSTANT(JOIN_ROUND);
  655. BIND_ENUM_CONSTANT(JOIN_MITER);
  656. BIND_ENUM_CONSTANT(END_POLYGON);
  657. BIND_ENUM_CONSTANT(END_JOINED);
  658. BIND_ENUM_CONSTANT(END_BUTT);
  659. BIND_ENUM_CONSTANT(END_SQUARE);
  660. BIND_ENUM_CONSTANT(END_ROUND);
  661. }
  662. ////// _Geometry3D //////
  663. _Geometry3D *_Geometry3D::singleton = nullptr;
  664. _Geometry3D *_Geometry3D::get_singleton() {
  665. return singleton;
  666. }
  667. Vector<Plane> _Geometry3D::build_box_planes(const Vector3 &p_extents) {
  668. return Geometry3D::build_box_planes(p_extents);
  669. }
  670. Vector<Plane> _Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  671. return Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  672. }
  673. Vector<Plane> _Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  674. return Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  675. }
  676. Vector<Vector3> _Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  677. Vector3 r1, r2;
  678. Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  679. Vector<Vector3> r;
  680. r.resize(2);
  681. r.set(0, r1);
  682. r.set(1, r2);
  683. return r;
  684. }
  685. Vector3 _Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  686. Vector3 s[2] = { p_a, p_b };
  687. return Geometry3D::get_closest_point_to_segment(p_point, s);
  688. }
  689. Vector3 _Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  690. Vector3 s[2] = { p_a, p_b };
  691. return Geometry3D::get_closest_point_to_segment_uncapped(p_point, s);
  692. }
  693. Variant _Geometry3D::ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
  694. Vector3 res;
  695. if (Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  696. return res;
  697. } else {
  698. return Variant();
  699. }
  700. }
  701. Variant _Geometry3D::segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
  702. Vector3 res;
  703. if (Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  704. return res;
  705. } else {
  706. return Variant();
  707. }
  708. }
  709. Vector<Vector3> _Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  710. Vector<Vector3> r;
  711. Vector3 res, norm;
  712. if (!Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  713. return r;
  714. }
  715. r.resize(2);
  716. r.set(0, res);
  717. r.set(1, norm);
  718. return r;
  719. }
  720. Vector<Vector3> _Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  721. Vector<Vector3> r;
  722. Vector3 res, norm;
  723. if (!Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  724. return r;
  725. }
  726. r.resize(2);
  727. r.set(0, res);
  728. r.set(1, norm);
  729. return r;
  730. }
  731. Vector<Vector3> _Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  732. Vector<Vector3> r;
  733. Vector3 res, norm;
  734. if (!Geometry3D::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm)) {
  735. return r;
  736. }
  737. r.resize(2);
  738. r.set(0, res);
  739. r.set(1, norm);
  740. return r;
  741. }
  742. Vector<Vector3> _Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  743. return Geometry3D::clip_polygon(p_points, p_plane);
  744. }
  745. void _Geometry3D::_bind_methods() {
  746. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry3D::build_box_planes);
  747. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  748. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  749. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry3D::get_closest_points_between_segments);
  750. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry3D::get_closest_point_to_segment);
  751. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry3D::get_closest_point_to_segment_uncapped);
  752. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry3D::ray_intersects_triangle);
  753. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry3D::segment_intersects_triangle);
  754. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &_Geometry3D::segment_intersects_sphere);
  755. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry3D::segment_intersects_cylinder);
  756. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &_Geometry3D::segment_intersects_convex);
  757. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &_Geometry3D::clip_polygon);
  758. }
  759. ////// _File //////
  760. Error _File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  761. Error err = open(p_path, p_mode_flags);
  762. if (err) {
  763. return err;
  764. }
  765. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  766. err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  767. if (err) {
  768. memdelete(fae);
  769. close();
  770. return err;
  771. }
  772. f = fae;
  773. return OK;
  774. }
  775. Error _File::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  776. Error err = open(p_path, p_mode_flags);
  777. if (err) {
  778. return err;
  779. }
  780. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  781. err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  782. if (err) {
  783. memdelete(fae);
  784. close();
  785. return err;
  786. }
  787. f = fae;
  788. return OK;
  789. }
  790. Error _File::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  791. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  792. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  793. Error err = fac->_open(p_path, p_mode_flags);
  794. if (err) {
  795. memdelete(fac);
  796. return err;
  797. }
  798. f = fac;
  799. return OK;
  800. }
  801. Error _File::open(const String &p_path, ModeFlags p_mode_flags) {
  802. close();
  803. Error err;
  804. f = FileAccess::open(p_path, p_mode_flags, &err);
  805. if (f) {
  806. f->set_big_endian(big_endian);
  807. }
  808. return err;
  809. }
  810. void _File::flush() {
  811. ERR_FAIL_COND_MSG(!f, "File must be opened before flushing.");
  812. f->flush();
  813. }
  814. void _File::close() {
  815. if (f) {
  816. memdelete(f);
  817. }
  818. f = nullptr;
  819. }
  820. bool _File::is_open() const {
  821. return f != nullptr;
  822. }
  823. String _File::get_path() const {
  824. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  825. return f->get_path();
  826. }
  827. String _File::get_path_absolute() const {
  828. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  829. return f->get_path_absolute();
  830. }
  831. void _File::seek(int64_t p_position) {
  832. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  833. ERR_FAIL_COND_MSG(p_position < 0, "Seek position must be a positive integer.");
  834. f->seek(p_position);
  835. }
  836. void _File::seek_end(int64_t p_position) {
  837. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  838. f->seek_end(p_position);
  839. }
  840. uint64_t _File::get_position() const {
  841. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  842. return f->get_position();
  843. }
  844. uint64_t _File::get_length() const {
  845. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  846. return f->get_length();
  847. }
  848. bool _File::eof_reached() const {
  849. ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use.");
  850. return f->eof_reached();
  851. }
  852. uint8_t _File::get_8() const {
  853. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  854. return f->get_8();
  855. }
  856. uint16_t _File::get_16() const {
  857. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  858. return f->get_16();
  859. }
  860. uint32_t _File::get_32() const {
  861. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  862. return f->get_32();
  863. }
  864. uint64_t _File::get_64() const {
  865. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  866. return f->get_64();
  867. }
  868. float _File::get_float() const {
  869. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  870. return f->get_float();
  871. }
  872. double _File::get_double() const {
  873. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  874. return f->get_double();
  875. }
  876. real_t _File::get_real() const {
  877. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  878. return f->get_real();
  879. }
  880. Vector<uint8_t> _File::get_buffer(int64_t p_length) const {
  881. Vector<uint8_t> data;
  882. ERR_FAIL_COND_V_MSG(!f, data, "File must be opened before use.");
  883. ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0.");
  884. if (p_length == 0) {
  885. return data;
  886. }
  887. Error err = data.resize(p_length);
  888. ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");
  889. uint8_t *w = data.ptrw();
  890. int64_t len = f->get_buffer(&w[0], p_length);
  891. if (len < p_length) {
  892. data.resize(len);
  893. }
  894. return data;
  895. }
  896. String _File::get_as_text() const {
  897. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  898. String text;
  899. uint64_t original_pos = f->get_position();
  900. f->seek(0);
  901. String l = get_line();
  902. while (!eof_reached()) {
  903. text += l + "\n";
  904. l = get_line();
  905. }
  906. text += l;
  907. f->seek(original_pos);
  908. return text;
  909. }
  910. String _File::get_md5(const String &p_path) const {
  911. return FileAccess::get_md5(p_path);
  912. }
  913. String _File::get_sha256(const String &p_path) const {
  914. return FileAccess::get_sha256(p_path);
  915. }
  916. String _File::get_line() const {
  917. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  918. return f->get_line();
  919. }
  920. Vector<String> _File::get_csv_line(const String &p_delim) const {
  921. ERR_FAIL_COND_V_MSG(!f, Vector<String>(), "File must be opened before use.");
  922. return f->get_csv_line(p_delim);
  923. }
  924. /**< use this for files WRITTEN in _big_ endian machines (i.e. amiga/mac)
  925. * It's not about the current CPU type but file formats.
  926. * These flags get reset to false (little endian) on each open
  927. */
  928. void _File::set_big_endian(bool p_big_endian) {
  929. big_endian = p_big_endian;
  930. if (f) {
  931. f->set_big_endian(p_big_endian);
  932. }
  933. }
  934. bool _File::is_big_endian() {
  935. return big_endian;
  936. }
  937. Error _File::get_error() const {
  938. if (!f) {
  939. return ERR_UNCONFIGURED;
  940. }
  941. return f->get_error();
  942. }
  943. void _File::store_8(uint8_t p_dest) {
  944. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  945. f->store_8(p_dest);
  946. }
  947. void _File::store_16(uint16_t p_dest) {
  948. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  949. f->store_16(p_dest);
  950. }
  951. void _File::store_32(uint32_t p_dest) {
  952. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  953. f->store_32(p_dest);
  954. }
  955. void _File::store_64(uint64_t p_dest) {
  956. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  957. f->store_64(p_dest);
  958. }
  959. void _File::store_float(float p_dest) {
  960. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  961. f->store_float(p_dest);
  962. }
  963. void _File::store_double(double p_dest) {
  964. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  965. f->store_double(p_dest);
  966. }
  967. void _File::store_real(real_t p_real) {
  968. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  969. f->store_real(p_real);
  970. }
  971. void _File::store_string(const String &p_string) {
  972. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  973. f->store_string(p_string);
  974. }
  975. void _File::store_pascal_string(const String &p_string) {
  976. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  977. f->store_pascal_string(p_string);
  978. }
  979. String _File::get_pascal_string() {
  980. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  981. return f->get_pascal_string();
  982. }
  983. void _File::store_line(const String &p_string) {
  984. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  985. f->store_line(p_string);
  986. }
  987. void _File::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  988. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  989. f->store_csv_line(p_values, p_delim);
  990. }
  991. void _File::store_buffer(const Vector<uint8_t> &p_buffer) {
  992. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  993. uint64_t len = p_buffer.size();
  994. if (len == 0) {
  995. return;
  996. }
  997. const uint8_t *r = p_buffer.ptr();
  998. f->store_buffer(&r[0], len);
  999. }
  1000. bool _File::file_exists(const String &p_name) const {
  1001. return FileAccess::exists(p_name);
  1002. }
  1003. void _File::store_var(const Variant &p_var, bool p_full_objects) {
  1004. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1005. int len;
  1006. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1007. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1008. Vector<uint8_t> buff;
  1009. buff.resize(len);
  1010. uint8_t *w = buff.ptrw();
  1011. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1012. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1013. store_32(len);
  1014. store_buffer(buff);
  1015. }
  1016. Variant _File::get_var(bool p_allow_objects) const {
  1017. ERR_FAIL_COND_V_MSG(!f, Variant(), "File must be opened before use.");
  1018. uint32_t len = get_32();
  1019. Vector<uint8_t> buff = get_buffer(len);
  1020. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  1021. const uint8_t *r = buff.ptr();
  1022. Variant v;
  1023. Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects);
  1024. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant.");
  1025. return v;
  1026. }
  1027. uint64_t _File::get_modified_time(const String &p_file) const {
  1028. return FileAccess::get_modified_time(p_file);
  1029. }
  1030. void _File::_bind_methods() {
  1031. ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
  1032. ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
  1033. ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &_File::open_compressed, DEFVAL(0));
  1034. ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open);
  1035. ClassDB::bind_method(D_METHOD("flush"), &_File::flush);
  1036. ClassDB::bind_method(D_METHOD("close"), &_File::close);
  1037. ClassDB::bind_method(D_METHOD("get_path"), &_File::get_path);
  1038. ClassDB::bind_method(D_METHOD("get_path_absolute"), &_File::get_path_absolute);
  1039. ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open);
  1040. ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek);
  1041. ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0));
  1042. ClassDB::bind_method(D_METHOD("get_position"), &_File::get_position);
  1043. ClassDB::bind_method(D_METHOD("get_length"), &_File::get_length);
  1044. ClassDB::bind_method(D_METHOD("eof_reached"), &_File::eof_reached);
  1045. ClassDB::bind_method(D_METHOD("get_8"), &_File::get_8);
  1046. ClassDB::bind_method(D_METHOD("get_16"), &_File::get_16);
  1047. ClassDB::bind_method(D_METHOD("get_32"), &_File::get_32);
  1048. ClassDB::bind_method(D_METHOD("get_64"), &_File::get_64);
  1049. ClassDB::bind_method(D_METHOD("get_float"), &_File::get_float);
  1050. ClassDB::bind_method(D_METHOD("get_double"), &_File::get_double);
  1051. ClassDB::bind_method(D_METHOD("get_real"), &_File::get_real);
  1052. ClassDB::bind_method(D_METHOD("get_buffer", "length"), &_File::get_buffer);
  1053. ClassDB::bind_method(D_METHOD("get_line"), &_File::get_line);
  1054. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
  1055. ClassDB::bind_method(D_METHOD("get_as_text"), &_File::get_as_text);
  1056. ClassDB::bind_method(D_METHOD("get_md5", "path"), &_File::get_md5);
  1057. ClassDB::bind_method(D_METHOD("get_sha256", "path"), &_File::get_sha256);
  1058. ClassDB::bind_method(D_METHOD("is_big_endian"), &_File::is_big_endian);
  1059. ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &_File::set_big_endian);
  1060. ClassDB::bind_method(D_METHOD("get_error"), &_File::get_error);
  1061. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &_File::get_var, DEFVAL(false));
  1062. ClassDB::bind_method(D_METHOD("store_8", "value"), &_File::store_8);
  1063. ClassDB::bind_method(D_METHOD("store_16", "value"), &_File::store_16);
  1064. ClassDB::bind_method(D_METHOD("store_32", "value"), &_File::store_32);
  1065. ClassDB::bind_method(D_METHOD("store_64", "value"), &_File::store_64);
  1066. ClassDB::bind_method(D_METHOD("store_float", "value"), &_File::store_float);
  1067. ClassDB::bind_method(D_METHOD("store_double", "value"), &_File::store_double);
  1068. ClassDB::bind_method(D_METHOD("store_real", "value"), &_File::store_real);
  1069. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &_File::store_buffer);
  1070. ClassDB::bind_method(D_METHOD("store_line", "line"), &_File::store_line);
  1071. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &_File::store_csv_line, DEFVAL(","));
  1072. ClassDB::bind_method(D_METHOD("store_string", "string"), &_File::store_string);
  1073. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &_File::store_var, DEFVAL(false));
  1074. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &_File::store_pascal_string);
  1075. ClassDB::bind_method(D_METHOD("get_pascal_string"), &_File::get_pascal_string);
  1076. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists);
  1077. ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time);
  1078. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian");
  1079. BIND_ENUM_CONSTANT(READ);
  1080. BIND_ENUM_CONSTANT(WRITE);
  1081. BIND_ENUM_CONSTANT(READ_WRITE);
  1082. BIND_ENUM_CONSTANT(WRITE_READ);
  1083. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  1084. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  1085. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  1086. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  1087. }
  1088. _File::~_File() {
  1089. if (f) {
  1090. memdelete(f);
  1091. }
  1092. }
  1093. ////// _Directory //////
  1094. Error _Directory::open(const String &p_path) {
  1095. Error err;
  1096. DirAccess *alt = DirAccess::open(p_path, &err);
  1097. if (!alt) {
  1098. return err;
  1099. }
  1100. if (d) {
  1101. memdelete(d);
  1102. }
  1103. d = alt;
  1104. dir_open = true;
  1105. return OK;
  1106. }
  1107. bool _Directory::is_open() const {
  1108. return d && dir_open;
  1109. }
  1110. Error _Directory::list_dir_begin(bool p_show_navigational, bool p_show_hidden) {
  1111. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1112. _list_skip_navigational = !p_show_navigational;
  1113. _list_skip_hidden = !p_show_hidden;
  1114. return d->list_dir_begin();
  1115. }
  1116. String _Directory::get_next() {
  1117. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1118. String next = d->get_next();
  1119. while (next != "" && ((_list_skip_navigational && (next == "." || next == "..")) || (_list_skip_hidden && d->current_is_hidden()))) {
  1120. next = d->get_next();
  1121. }
  1122. return next;
  1123. }
  1124. bool _Directory::current_is_dir() const {
  1125. ERR_FAIL_COND_V_MSG(!is_open(), false, "Directory must be opened before use.");
  1126. return d->current_is_dir();
  1127. }
  1128. void _Directory::list_dir_end() {
  1129. ERR_FAIL_COND_MSG(!is_open(), "Directory must be opened before use.");
  1130. d->list_dir_end();
  1131. }
  1132. int _Directory::get_drive_count() {
  1133. ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use.");
  1134. return d->get_drive_count();
  1135. }
  1136. String _Directory::get_drive(int p_drive) {
  1137. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1138. return d->get_drive(p_drive);
  1139. }
  1140. int _Directory::get_current_drive() {
  1141. ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use.");
  1142. return d->get_current_drive();
  1143. }
  1144. Error _Directory::change_dir(String p_dir) {
  1145. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
  1146. Error err = d->change_dir(p_dir);
  1147. if (err != OK) {
  1148. return err;
  1149. }
  1150. dir_open = true;
  1151. return OK;
  1152. }
  1153. String _Directory::get_current_dir() {
  1154. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1155. return d->get_current_dir();
  1156. }
  1157. Error _Directory::make_dir(String p_dir) {
  1158. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
  1159. if (!p_dir.is_rel_path()) {
  1160. DirAccess *d = DirAccess::create_for_path(p_dir);
  1161. Error err = d->make_dir(p_dir);
  1162. memdelete(d);
  1163. return err;
  1164. }
  1165. return d->make_dir(p_dir);
  1166. }
  1167. Error _Directory::make_dir_recursive(String p_dir) {
  1168. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
  1169. if (!p_dir.is_rel_path()) {
  1170. DirAccess *d = DirAccess::create_for_path(p_dir);
  1171. Error err = d->make_dir_recursive(p_dir);
  1172. memdelete(d);
  1173. return err;
  1174. }
  1175. return d->make_dir_recursive(p_dir);
  1176. }
  1177. bool _Directory::file_exists(String p_file) {
  1178. ERR_FAIL_COND_V_MSG(!d, false, "Directory is not configured properly.");
  1179. if (!p_file.is_rel_path()) {
  1180. return FileAccess::exists(p_file);
  1181. }
  1182. return d->file_exists(p_file);
  1183. }
  1184. bool _Directory::dir_exists(String p_dir) {
  1185. ERR_FAIL_COND_V_MSG(!d, false, "Directory is not configured properly.");
  1186. if (!p_dir.is_rel_path()) {
  1187. DirAccess *d = DirAccess::create_for_path(p_dir);
  1188. bool exists = d->dir_exists(p_dir);
  1189. memdelete(d);
  1190. return exists;
  1191. }
  1192. return d->dir_exists(p_dir);
  1193. }
  1194. uint64_t _Directory::get_space_left() {
  1195. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1196. return d->get_space_left() / 1024 * 1024; // Truncate to closest MiB.
  1197. }
  1198. Error _Directory::copy(String p_from, String p_to) {
  1199. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1200. return d->copy(p_from, p_to);
  1201. }
  1202. Error _Directory::rename(String p_from, String p_to) {
  1203. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1204. if (!p_from.is_rel_path()) {
  1205. DirAccess *d = DirAccess::create_for_path(p_from);
  1206. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from), ERR_DOES_NOT_EXIST, "File does not exist.");
  1207. Error err = d->rename(p_from, p_to);
  1208. memdelete(d);
  1209. return err;
  1210. }
  1211. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from), ERR_DOES_NOT_EXIST, "File does not exist.");
  1212. return d->rename(p_from, p_to);
  1213. }
  1214. Error _Directory::remove(String p_name) {
  1215. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1216. if (!p_name.is_rel_path()) {
  1217. DirAccess *d = DirAccess::create_for_path(p_name);
  1218. Error err = d->remove(p_name);
  1219. memdelete(d);
  1220. return err;
  1221. }
  1222. return d->remove(p_name);
  1223. }
  1224. void _Directory::_bind_methods() {
  1225. ClassDB::bind_method(D_METHOD("open", "path"), &_Directory::open);
  1226. ClassDB::bind_method(D_METHOD("list_dir_begin", "show_navigational", "show_hidden"), &_Directory::list_dir_begin, DEFVAL(false), DEFVAL(false));
  1227. ClassDB::bind_method(D_METHOD("get_next"), &_Directory::get_next);
  1228. ClassDB::bind_method(D_METHOD("current_is_dir"), &_Directory::current_is_dir);
  1229. ClassDB::bind_method(D_METHOD("list_dir_end"), &_Directory::list_dir_end);
  1230. ClassDB::bind_method(D_METHOD("get_drive_count"), &_Directory::get_drive_count);
  1231. ClassDB::bind_method(D_METHOD("get_drive", "idx"), &_Directory::get_drive);
  1232. ClassDB::bind_method(D_METHOD("get_current_drive"), &_Directory::get_current_drive);
  1233. ClassDB::bind_method(D_METHOD("change_dir", "todir"), &_Directory::change_dir);
  1234. ClassDB::bind_method(D_METHOD("get_current_dir"), &_Directory::get_current_dir);
  1235. ClassDB::bind_method(D_METHOD("make_dir", "path"), &_Directory::make_dir);
  1236. ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &_Directory::make_dir_recursive);
  1237. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_Directory::file_exists);
  1238. ClassDB::bind_method(D_METHOD("dir_exists", "path"), &_Directory::dir_exists);
  1239. //ClassDB::bind_method(D_METHOD("get_modified_time","file"),&_Directory::get_modified_time);
  1240. ClassDB::bind_method(D_METHOD("get_space_left"), &_Directory::get_space_left);
  1241. ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy);
  1242. ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename);
  1243. ClassDB::bind_method(D_METHOD("remove", "path"), &_Directory::remove);
  1244. }
  1245. _Directory::_Directory() {
  1246. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1247. }
  1248. _Directory::~_Directory() {
  1249. if (d) {
  1250. memdelete(d);
  1251. }
  1252. }
  1253. ////// _Marshalls //////
  1254. _Marshalls *_Marshalls::singleton = nullptr;
  1255. _Marshalls *_Marshalls::get_singleton() {
  1256. return singleton;
  1257. }
  1258. String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  1259. int len;
  1260. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1261. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1262. Vector<uint8_t> buff;
  1263. buff.resize(len);
  1264. uint8_t *w = buff.ptrw();
  1265. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1266. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1267. String ret = CryptoCore::b64_encode_str(&w[0], len);
  1268. ERR_FAIL_COND_V(ret == "", ret);
  1269. return ret;
  1270. }
  1271. Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  1272. int strlen = p_str.length();
  1273. CharString cstr = p_str.ascii();
  1274. Vector<uint8_t> buf;
  1275. buf.resize(strlen / 4 * 3 + 1);
  1276. uint8_t *w = buf.ptrw();
  1277. size_t len = 0;
  1278. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  1279. Variant v;
  1280. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  1281. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  1282. return v;
  1283. }
  1284. String _Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  1285. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  1286. ERR_FAIL_COND_V(ret == "", ret);
  1287. return ret;
  1288. }
  1289. Vector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
  1290. int strlen = p_str.length();
  1291. CharString cstr = p_str.ascii();
  1292. size_t arr_len = 0;
  1293. Vector<uint8_t> buf;
  1294. {
  1295. buf.resize(strlen / 4 * 3 + 1);
  1296. uint8_t *w = buf.ptrw();
  1297. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  1298. }
  1299. buf.resize(arr_len);
  1300. return buf;
  1301. }
  1302. String _Marshalls::utf8_to_base64(const String &p_str) {
  1303. CharString cstr = p_str.utf8();
  1304. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  1305. ERR_FAIL_COND_V(ret == "", ret);
  1306. return ret;
  1307. }
  1308. String _Marshalls::base64_to_utf8(const String &p_str) {
  1309. int strlen = p_str.length();
  1310. CharString cstr = p_str.ascii();
  1311. Vector<uint8_t> buf;
  1312. buf.resize(strlen / 4 * 3 + 1 + 1);
  1313. uint8_t *w = buf.ptrw();
  1314. size_t len = 0;
  1315. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  1316. w[len] = 0;
  1317. String ret = String::utf8((char *)&w[0]);
  1318. return ret;
  1319. }
  1320. void _Marshalls::_bind_methods() {
  1321. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &_Marshalls::variant_to_base64, DEFVAL(false));
  1322. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &_Marshalls::base64_to_variant, DEFVAL(false));
  1323. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &_Marshalls::raw_to_base64);
  1324. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &_Marshalls::base64_to_raw);
  1325. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &_Marshalls::utf8_to_base64);
  1326. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &_Marshalls::base64_to_utf8);
  1327. }
  1328. ////// _Semaphore //////
  1329. void _Semaphore::wait() {
  1330. semaphore.wait();
  1331. }
  1332. Error _Semaphore::try_wait() {
  1333. return semaphore.try_wait() ? OK : ERR_BUSY;
  1334. }
  1335. void _Semaphore::post() {
  1336. semaphore.post();
  1337. }
  1338. void _Semaphore::_bind_methods() {
  1339. ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait);
  1340. ClassDB::bind_method(D_METHOD("try_wait"), &_Semaphore::try_wait);
  1341. ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
  1342. }
  1343. ////// _Mutex //////
  1344. void _Mutex::lock() {
  1345. mutex.lock();
  1346. }
  1347. Error _Mutex::try_lock() {
  1348. return mutex.try_lock();
  1349. }
  1350. void _Mutex::unlock() {
  1351. mutex.unlock();
  1352. }
  1353. void _Mutex::_bind_methods() {
  1354. ClassDB::bind_method(D_METHOD("lock"), &_Mutex::lock);
  1355. ClassDB::bind_method(D_METHOD("try_lock"), &_Mutex::try_lock);
  1356. ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock);
  1357. }
  1358. ////// _Thread //////
  1359. void _Thread::_start_func(void *ud) {
  1360. Ref<_Thread> *tud = (Ref<_Thread> *)ud;
  1361. Ref<_Thread> t = *tud;
  1362. memdelete(tud);
  1363. Callable::CallError ce;
  1364. const Variant *arg[1] = { &t->userdata };
  1365. int argc = 0;
  1366. if (arg[0]->get_type() != Variant::NIL) {
  1367. // Just pass to the target function whatever came as user data
  1368. argc = 1;
  1369. } else {
  1370. // There are two cases of null user data:
  1371. // a) The target function has zero parameters and the caller is just honoring that.
  1372. // b) The target function has at least one parameter with no default and the caller is
  1373. // leveraging the fact that user data defaults to null in Thread.start().
  1374. // We care about the case of more than one parameter because, even if a thread
  1375. // function can have one at most, out mindset here is to do our best with the
  1376. // only/first one and let the call handle any other error conditions, like too
  1377. // much arguments.
  1378. // We must check if we are in case b).
  1379. int target_param_count = 0;
  1380. int target_default_arg_count = 0;
  1381. Ref<Script> script = t->target_instance->get_script();
  1382. if (script.is_valid()) {
  1383. MethodInfo mi = script->get_method_info(t->target_method);
  1384. target_param_count = mi.arguments.size();
  1385. target_default_arg_count = mi.default_arguments.size();
  1386. } else {
  1387. MethodBind *method = ClassDB::get_method(t->target_instance->get_class_name(), t->target_method);
  1388. target_param_count = method->get_argument_count();
  1389. target_default_arg_count = method->get_default_argument_count();
  1390. }
  1391. if (target_param_count >= 1 && target_default_arg_count == target_param_count) {
  1392. argc = 1;
  1393. }
  1394. }
  1395. Thread::set_name(t->target_method);
  1396. t->ret = t->target_instance->call(t->target_method, arg, argc, ce);
  1397. if (ce.error != Callable::CallError::CALL_OK) {
  1398. String reason;
  1399. switch (ce.error) {
  1400. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1401. reason = "Invalid Argument #" + itos(ce.argument);
  1402. } break;
  1403. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1404. reason = "Too Many Arguments";
  1405. } break;
  1406. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1407. reason = "Too Few Arguments";
  1408. } break;
  1409. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1410. reason = "Method Not Found";
  1411. } break;
  1412. default: {
  1413. }
  1414. }
  1415. ERR_FAIL_MSG("Could not call function '" + t->target_method.operator String() + "' to start thread " + t->get_id() + ": " + reason + ".");
  1416. }
  1417. }
  1418. Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {
  1419. ERR_FAIL_COND_V_MSG(active.is_set(), ERR_ALREADY_IN_USE, "Thread already started.");
  1420. ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
  1421. ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
  1422. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  1423. ret = Variant();
  1424. target_method = p_method;
  1425. target_instance = p_instance;
  1426. userdata = p_userdata;
  1427. active.set();
  1428. Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
  1429. Thread::Settings s;
  1430. s.priority = (Thread::Priority)p_priority;
  1431. thread.start(_start_func, ud, s);
  1432. return OK;
  1433. }
  1434. String _Thread::get_id() const {
  1435. return itos(thread.get_id());
  1436. }
  1437. bool _Thread::is_active() const {
  1438. return active.is_set();
  1439. }
  1440. Variant _Thread::wait_to_finish() {
  1441. ERR_FAIL_COND_V_MSG(!active.is_set(), Variant(), "Thread must be active to wait for its completion.");
  1442. thread.wait_to_finish();
  1443. Variant r = ret;
  1444. active.clear();
  1445. target_method = StringName();
  1446. target_instance = nullptr;
  1447. userdata = Variant();
  1448. return r;
  1449. }
  1450. void _Thread::_bind_methods() {
  1451. ClassDB::bind_method(D_METHOD("start", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
  1452. ClassDB::bind_method(D_METHOD("get_id"), &_Thread::get_id);
  1453. ClassDB::bind_method(D_METHOD("is_active"), &_Thread::is_active);
  1454. ClassDB::bind_method(D_METHOD("wait_to_finish"), &_Thread::wait_to_finish);
  1455. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  1456. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  1457. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  1458. }
  1459. ////// _ClassDB //////
  1460. PackedStringArray _ClassDB::get_class_list() const {
  1461. List<StringName> classes;
  1462. ClassDB::get_class_list(&classes);
  1463. PackedStringArray ret;
  1464. ret.resize(classes.size());
  1465. int idx = 0;
  1466. for (const StringName &E : classes) {
  1467. ret.set(idx++, E);
  1468. }
  1469. return ret;
  1470. }
  1471. PackedStringArray _ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  1472. List<StringName> classes;
  1473. ClassDB::get_inheriters_from_class(p_class, &classes);
  1474. PackedStringArray ret;
  1475. ret.resize(classes.size());
  1476. int idx = 0;
  1477. for (const StringName &E : classes) {
  1478. ret.set(idx++, E);
  1479. }
  1480. return ret;
  1481. }
  1482. StringName _ClassDB::get_parent_class(const StringName &p_class) const {
  1483. return ClassDB::get_parent_class(p_class);
  1484. }
  1485. bool _ClassDB::class_exists(const StringName &p_class) const {
  1486. return ClassDB::class_exists(p_class);
  1487. }
  1488. bool _ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  1489. return ClassDB::is_parent_class(p_class, p_inherits);
  1490. }
  1491. bool _ClassDB::can_instantiate(const StringName &p_class) const {
  1492. return ClassDB::can_instantiate(p_class);
  1493. }
  1494. Variant _ClassDB::instantiate(const StringName &p_class) const {
  1495. Object *obj = ClassDB::instantiate(p_class);
  1496. if (!obj) {
  1497. return Variant();
  1498. }
  1499. RefCounted *r = Object::cast_to<RefCounted>(obj);
  1500. if (r) {
  1501. return REF(r);
  1502. } else {
  1503. return obj;
  1504. }
  1505. }
  1506. bool _ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  1507. return ClassDB::has_signal(p_class, p_signal);
  1508. }
  1509. Dictionary _ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  1510. MethodInfo signal;
  1511. if (ClassDB::get_signal(p_class, p_signal, &signal)) {
  1512. return signal.operator Dictionary();
  1513. } else {
  1514. return Dictionary();
  1515. }
  1516. }
  1517. Array _ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  1518. List<MethodInfo> signals;
  1519. ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1520. Array ret;
  1521. for (const MethodInfo &E : signals) {
  1522. ret.push_back(E.operator Dictionary());
  1523. }
  1524. return ret;
  1525. }
  1526. Array _ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  1527. List<PropertyInfo> plist;
  1528. ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1529. Array ret;
  1530. for (const PropertyInfo &E : plist) {
  1531. ret.push_back(E.operator Dictionary());
  1532. }
  1533. return ret;
  1534. }
  1535. Variant _ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  1536. Variant ret;
  1537. ClassDB::get_property(p_object, p_property, ret);
  1538. return ret;
  1539. }
  1540. Error _ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1541. Variant ret;
  1542. bool valid;
  1543. if (!ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1544. return ERR_UNAVAILABLE;
  1545. } else if (!valid) {
  1546. return ERR_INVALID_DATA;
  1547. }
  1548. return OK;
  1549. }
  1550. bool _ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  1551. return ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1552. }
  1553. Array _ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  1554. List<MethodInfo> methods;
  1555. ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1556. Array ret;
  1557. for (const MethodInfo &E : methods) {
  1558. #ifdef DEBUG_METHODS_ENABLED
  1559. ret.push_back(E.operator Dictionary());
  1560. #else
  1561. Dictionary dict;
  1562. dict["name"] = E.name;
  1563. ret.push_back(dict);
  1564. #endif
  1565. }
  1566. return ret;
  1567. }
  1568. PackedStringArray _ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1569. List<String> constants;
  1570. ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1571. PackedStringArray ret;
  1572. ret.resize(constants.size());
  1573. int idx = 0;
  1574. for (const String &E : constants) {
  1575. ret.set(idx++, E);
  1576. }
  1577. return ret;
  1578. }
  1579. bool _ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1580. bool success;
  1581. ClassDB::get_integer_constant(p_class, p_name, &success);
  1582. return success;
  1583. }
  1584. int _ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1585. bool found;
  1586. int c = ClassDB::get_integer_constant(p_class, p_name, &found);
  1587. ERR_FAIL_COND_V(!found, 0);
  1588. return c;
  1589. }
  1590. StringName _ClassDB::get_category(const StringName &p_node) const {
  1591. return ClassDB::get_category(p_node);
  1592. }
  1593. bool _ClassDB::is_class_enabled(StringName p_class) const {
  1594. return ClassDB::is_class_enabled(p_class);
  1595. }
  1596. void _ClassDB::_bind_methods() {
  1597. ClassDB::bind_method(D_METHOD("get_class_list"), &_ClassDB::get_class_list);
  1598. ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &_ClassDB::get_inheriters_from_class);
  1599. ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &_ClassDB::get_parent_class);
  1600. ClassDB::bind_method(D_METHOD("class_exists", "class"), &_ClassDB::class_exists);
  1601. ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &_ClassDB::is_parent_class);
  1602. ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &_ClassDB::can_instantiate);
  1603. ClassDB::bind_method(D_METHOD("instantiate", "class"), &_ClassDB::instantiate);
  1604. ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &_ClassDB::has_signal);
  1605. ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &_ClassDB::get_signal);
  1606. ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &_ClassDB::get_signal_list, DEFVAL(false));
  1607. ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &_ClassDB::get_property_list, DEFVAL(false));
  1608. ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &_ClassDB::get_property);
  1609. ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &_ClassDB::set_property);
  1610. ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &_ClassDB::has_method, DEFVAL(false));
  1611. ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &_ClassDB::get_method_list, DEFVAL(false));
  1612. ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &_ClassDB::get_integer_constant_list, DEFVAL(false));
  1613. ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &_ClassDB::has_integer_constant);
  1614. ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &_ClassDB::get_integer_constant);
  1615. ClassDB::bind_method(D_METHOD("class_get_category", "class"), &_ClassDB::get_category);
  1616. ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &_ClassDB::is_class_enabled);
  1617. }
  1618. ////// _Engine //////
  1619. void _Engine::set_iterations_per_second(int p_ips) {
  1620. Engine::get_singleton()->set_iterations_per_second(p_ips);
  1621. }
  1622. int _Engine::get_iterations_per_second() const {
  1623. return Engine::get_singleton()->get_iterations_per_second();
  1624. }
  1625. void _Engine::set_physics_jitter_fix(float p_threshold) {
  1626. Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1627. }
  1628. float _Engine::get_physics_jitter_fix() const {
  1629. return Engine::get_singleton()->get_physics_jitter_fix();
  1630. }
  1631. float _Engine::get_physics_interpolation_fraction() const {
  1632. return Engine::get_singleton()->get_physics_interpolation_fraction();
  1633. }
  1634. void _Engine::set_target_fps(int p_fps) {
  1635. Engine::get_singleton()->set_target_fps(p_fps);
  1636. }
  1637. int _Engine::get_target_fps() const {
  1638. return Engine::get_singleton()->get_target_fps();
  1639. }
  1640. float _Engine::get_frames_per_second() const {
  1641. return Engine::get_singleton()->get_frames_per_second();
  1642. }
  1643. uint64_t _Engine::get_physics_frames() const {
  1644. return Engine::get_singleton()->get_physics_frames();
  1645. }
  1646. uint64_t _Engine::get_process_frames() const {
  1647. return Engine::get_singleton()->get_process_frames();
  1648. }
  1649. void _Engine::set_time_scale(float p_scale) {
  1650. Engine::get_singleton()->set_time_scale(p_scale);
  1651. }
  1652. float _Engine::get_time_scale() {
  1653. return Engine::get_singleton()->get_time_scale();
  1654. }
  1655. int _Engine::get_frames_drawn() {
  1656. return Engine::get_singleton()->get_frames_drawn();
  1657. }
  1658. MainLoop *_Engine::get_main_loop() const {
  1659. //needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1660. return OS::get_singleton()->get_main_loop();
  1661. }
  1662. Dictionary _Engine::get_version_info() const {
  1663. return Engine::get_singleton()->get_version_info();
  1664. }
  1665. Dictionary _Engine::get_author_info() const {
  1666. return Engine::get_singleton()->get_author_info();
  1667. }
  1668. Array _Engine::get_copyright_info() const {
  1669. return Engine::get_singleton()->get_copyright_info();
  1670. }
  1671. Dictionary _Engine::get_donor_info() const {
  1672. return Engine::get_singleton()->get_donor_info();
  1673. }
  1674. Dictionary _Engine::get_license_info() const {
  1675. return Engine::get_singleton()->get_license_info();
  1676. }
  1677. String _Engine::get_license_text() const {
  1678. return Engine::get_singleton()->get_license_text();
  1679. }
  1680. bool _Engine::is_in_physics_frame() const {
  1681. return Engine::get_singleton()->is_in_physics_frame();
  1682. }
  1683. bool _Engine::has_singleton(const String &p_name) const {
  1684. return Engine::get_singleton()->has_singleton(p_name);
  1685. }
  1686. Object *_Engine::get_singleton_object(const String &p_name) const {
  1687. return Engine::get_singleton()->get_singleton_object(p_name);
  1688. }
  1689. void _Engine::set_editor_hint(bool p_enabled) {
  1690. Engine::get_singleton()->set_editor_hint(p_enabled);
  1691. }
  1692. bool _Engine::is_editor_hint() const {
  1693. return Engine::get_singleton()->is_editor_hint();
  1694. }
  1695. void _Engine::set_print_error_messages(bool p_enabled) {
  1696. Engine::get_singleton()->set_print_error_messages(p_enabled);
  1697. }
  1698. bool _Engine::is_printing_error_messages() const {
  1699. return Engine::get_singleton()->is_printing_error_messages();
  1700. }
  1701. void _Engine::_bind_methods() {
  1702. ClassDB::bind_method(D_METHOD("set_iterations_per_second", "iterations_per_second"), &_Engine::set_iterations_per_second);
  1703. ClassDB::bind_method(D_METHOD("get_iterations_per_second"), &_Engine::get_iterations_per_second);
  1704. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &_Engine::set_physics_jitter_fix);
  1705. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &_Engine::get_physics_jitter_fix);
  1706. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &_Engine::get_physics_interpolation_fraction);
  1707. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &_Engine::set_target_fps);
  1708. ClassDB::bind_method(D_METHOD("get_target_fps"), &_Engine::get_target_fps);
  1709. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &_Engine::set_time_scale);
  1710. ClassDB::bind_method(D_METHOD("get_time_scale"), &_Engine::get_time_scale);
  1711. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &_Engine::get_frames_drawn);
  1712. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &_Engine::get_frames_per_second);
  1713. ClassDB::bind_method(D_METHOD("get_physics_frames"), &_Engine::get_physics_frames);
  1714. ClassDB::bind_method(D_METHOD("get_process_frames"), &_Engine::get_process_frames);
  1715. ClassDB::bind_method(D_METHOD("get_main_loop"), &_Engine::get_main_loop);
  1716. ClassDB::bind_method(D_METHOD("get_version_info"), &_Engine::get_version_info);
  1717. ClassDB::bind_method(D_METHOD("get_author_info"), &_Engine::get_author_info);
  1718. ClassDB::bind_method(D_METHOD("get_copyright_info"), &_Engine::get_copyright_info);
  1719. ClassDB::bind_method(D_METHOD("get_donor_info"), &_Engine::get_donor_info);
  1720. ClassDB::bind_method(D_METHOD("get_license_info"), &_Engine::get_license_info);
  1721. ClassDB::bind_method(D_METHOD("get_license_text"), &_Engine::get_license_text);
  1722. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &_Engine::is_in_physics_frame);
  1723. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &_Engine::has_singleton);
  1724. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &_Engine::get_singleton_object);
  1725. ClassDB::bind_method(D_METHOD("set_editor_hint", "enabled"), &_Engine::set_editor_hint);
  1726. ClassDB::bind_method(D_METHOD("is_editor_hint"), &_Engine::is_editor_hint);
  1727. ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &_Engine::set_print_error_messages);
  1728. ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &_Engine::is_printing_error_messages);
  1729. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_hint"), "set_editor_hint", "is_editor_hint");
  1730. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
  1731. ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations_per_second"), "set_iterations_per_second", "get_iterations_per_second");
  1732. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  1733. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1734. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1735. }
  1736. _Engine *_Engine::singleton = nullptr;
  1737. ////// _EngineDebugger //////
  1738. void _EngineDebugger::_bind_methods() {
  1739. ClassDB::bind_method(D_METHOD("is_active"), &_EngineDebugger::is_active);
  1740. ClassDB::bind_method(D_METHOD("register_profiler", "name", "toggle", "add", "tick"), &_EngineDebugger::register_profiler);
  1741. ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &_EngineDebugger::unregister_profiler);
  1742. ClassDB::bind_method(D_METHOD("is_profiling", "name"), &_EngineDebugger::is_profiling);
  1743. ClassDB::bind_method(D_METHOD("has_profiler", "name"), &_EngineDebugger::has_profiler);
  1744. ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &_EngineDebugger::profiler_add_frame_data);
  1745. ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &_EngineDebugger::profiler_enable, DEFVAL(Array()));
  1746. ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &_EngineDebugger::register_message_capture);
  1747. ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &_EngineDebugger::unregister_message_capture);
  1748. ClassDB::bind_method(D_METHOD("has_capture", "name"), &_EngineDebugger::has_capture);
  1749. ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &_EngineDebugger::send_message);
  1750. }
  1751. bool _EngineDebugger::is_active() {
  1752. return EngineDebugger::is_active();
  1753. }
  1754. void _EngineDebugger::register_profiler(const StringName &p_name, const Callable &p_toggle, const Callable &p_add, const Callable &p_tick) {
  1755. ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler already registered: " + p_name);
  1756. profilers.insert(p_name, ProfilerCallable(p_toggle, p_add, p_tick));
  1757. ProfilerCallable &p = profilers[p_name];
  1758. EngineDebugger::Profiler profiler(
  1759. &p,
  1760. &_EngineDebugger::call_toggle,
  1761. &_EngineDebugger::call_add,
  1762. &_EngineDebugger::call_tick);
  1763. EngineDebugger::register_profiler(p_name, profiler);
  1764. }
  1765. void _EngineDebugger::unregister_profiler(const StringName &p_name) {
  1766. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
  1767. EngineDebugger::unregister_profiler(p_name);
  1768. profilers.erase(p_name);
  1769. }
  1770. bool _EngineDebugger::_EngineDebugger::is_profiling(const StringName &p_name) {
  1771. return EngineDebugger::is_profiling(p_name);
  1772. }
  1773. bool _EngineDebugger::has_profiler(const StringName &p_name) {
  1774. return EngineDebugger::has_profiler(p_name);
  1775. }
  1776. void _EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  1777. EngineDebugger::profiler_add_frame_data(p_name, p_data);
  1778. }
  1779. void _EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  1780. if (EngineDebugger::get_singleton()) {
  1781. EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
  1782. }
  1783. }
  1784. void _EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1785. ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), "Capture already registered: " + p_name);
  1786. captures.insert(p_name, p_callable);
  1787. Callable &c = captures[p_name];
  1788. EngineDebugger::Capture capture(&c, &_EngineDebugger::call_capture);
  1789. EngineDebugger::register_message_capture(p_name, capture);
  1790. }
  1791. void _EngineDebugger::unregister_message_capture(const StringName &p_name) {
  1792. ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
  1793. EngineDebugger::unregister_message_capture(p_name);
  1794. captures.erase(p_name);
  1795. }
  1796. bool _EngineDebugger::has_capture(const StringName &p_name) {
  1797. return EngineDebugger::has_capture(p_name);
  1798. }
  1799. void _EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
  1800. ERR_FAIL_COND_MSG(!EngineDebugger::is_active(), "Can't send message. No active debugger");
  1801. EngineDebugger::get_singleton()->send_message(p_msg, p_data);
  1802. }
  1803. void _EngineDebugger::call_toggle(void *p_user, bool p_enable, const Array &p_opts) {
  1804. Callable &toggle = ((ProfilerCallable *)p_user)->callable_toggle;
  1805. if (toggle.is_null()) {
  1806. return;
  1807. }
  1808. Variant enable = p_enable, opts = p_opts;
  1809. const Variant *args[2] = { &enable, &opts };
  1810. Variant retval;
  1811. Callable::CallError err;
  1812. toggle.call(args, 2, retval, err);
  1813. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'toggle' to callable: " + Variant::get_callable_error_text(toggle, args, 2, err));
  1814. }
  1815. void _EngineDebugger::call_add(void *p_user, const Array &p_data) {
  1816. Callable &add = ((ProfilerCallable *)p_user)->callable_add;
  1817. if (add.is_null()) {
  1818. return;
  1819. }
  1820. Variant data = p_data;
  1821. const Variant *args[1] = { &data };
  1822. Variant retval;
  1823. Callable::CallError err;
  1824. add.call(args, 1, retval, err);
  1825. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'add' to callable: " + Variant::get_callable_error_text(add, args, 1, err));
  1826. }
  1827. void _EngineDebugger::call_tick(void *p_user, float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) {
  1828. Callable &tick = ((ProfilerCallable *)p_user)->callable_tick;
  1829. if (tick.is_null()) {
  1830. return;
  1831. }
  1832. Variant frame_time = p_frame_time, idle_time = p_idle_time, physics_time = p_physics_time, physics_frame_time = p_physics_frame_time;
  1833. const Variant *args[4] = { &frame_time, &idle_time, &physics_time, &physics_frame_time };
  1834. Variant retval;
  1835. Callable::CallError err;
  1836. tick.call(args, 4, retval, err);
  1837. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'tick' to callable: " + Variant::get_callable_error_text(tick, args, 4, err));
  1838. }
  1839. Error _EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  1840. Callable &capture = *(Callable *)p_user;
  1841. if (capture.is_null()) {
  1842. return FAILED;
  1843. }
  1844. Variant cmd = p_cmd, data = p_data;
  1845. const Variant *args[2] = { &cmd, &data };
  1846. Variant retval;
  1847. Callable::CallError err;
  1848. capture.call(args, 2, retval, err);
  1849. ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, FAILED, "Error calling 'capture' to callable: " + Variant::get_callable_error_text(capture, args, 2, err));
  1850. ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, "Error calling 'capture' to callable: " + String(capture) + ". Return type is not bool.");
  1851. r_captured = retval;
  1852. return OK;
  1853. }
  1854. _EngineDebugger::~_EngineDebugger() {
  1855. for (Map<StringName, Callable>::Element *E = captures.front(); E; E = E->next()) {
  1856. EngineDebugger::unregister_message_capture(E->key());
  1857. }
  1858. captures.clear();
  1859. for (Map<StringName, ProfilerCallable>::Element *E = profilers.front(); E; E = E->next()) {
  1860. EngineDebugger::unregister_profiler(E->key());
  1861. }
  1862. profilers.clear();
  1863. }
  1864. _EngineDebugger *_EngineDebugger::singleton = nullptr;