core_bind.cpp 89 KB

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