core_bind.cpp 87 KB

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