core_bind.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. /**************************************************************************/
  2. /* core_bind.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_bind.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/crypto/crypto_core.h"
  34. #include "core/debugger/engine_debugger.h"
  35. #include "core/debugger/script_debugger.h"
  36. #include "core/io/file_access_compressed.h"
  37. #include "core/io/file_access_encrypted.h"
  38. #include "core/io/marshalls.h"
  39. #include "core/math/geometry_2d.h"
  40. #include "core/math/geometry_3d.h"
  41. #include "core/os/keyboard.h"
  42. #include "core/os/thread_safe.h"
  43. #include "core/variant/typed_array.h"
  44. namespace core_bind {
  45. ////// ResourceLoader //////
  46. ResourceLoader *ResourceLoader::singleton = nullptr;
  47. Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, CacheMode p_cache_mode) {
  48. return ::ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads, ResourceFormatLoader::CacheMode(p_cache_mode));
  49. }
  50. ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
  51. float progress = 0;
  52. ::ResourceLoader::ThreadLoadStatus tls = ::ResourceLoader::load_threaded_get_status(p_path, &progress);
  53. // Default array should never be modified, it causes the hash of the method to change.
  54. if (!ClassDB::is_default_array_arg(r_progress)) {
  55. r_progress.resize(1);
  56. r_progress[0] = progress;
  57. }
  58. return (ThreadLoadStatus)tls;
  59. }
  60. Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path) {
  61. Error error;
  62. Ref<Resource> res = ::ResourceLoader::load_threaded_get(p_path, &error);
  63. return res;
  64. }
  65. Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, CacheMode p_cache_mode) {
  66. Error err = OK;
  67. Ref<Resource> ret = ::ResourceLoader::load(p_path, p_type_hint, ResourceFormatLoader::CacheMode(p_cache_mode), &err);
  68. ERR_FAIL_COND_V_MSG(err != OK, ret, vformat("Error loading resource: '%s'.", p_path));
  69. return ret;
  70. }
  71. Vector<String> ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
  72. List<String> exts;
  73. ::ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
  74. Vector<String> ret;
  75. for (const String &E : exts) {
  76. ret.push_back(E);
  77. }
  78. return ret;
  79. }
  80. void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) {
  81. ::ResourceLoader::add_resource_format_loader(p_format_loader, p_at_front);
  82. }
  83. void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) {
  84. ::ResourceLoader::remove_resource_format_loader(p_format_loader);
  85. }
  86. void ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  87. ::ResourceLoader::set_abort_on_missing_resources(p_abort);
  88. }
  89. PackedStringArray ResourceLoader::get_dependencies(const String &p_path) {
  90. List<String> deps;
  91. ::ResourceLoader::get_dependencies(p_path, &deps);
  92. PackedStringArray ret;
  93. for (const String &E : deps) {
  94. ret.push_back(E);
  95. }
  96. return ret;
  97. }
  98. bool ResourceLoader::has_cached(const String &p_path) {
  99. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  100. return ResourceCache::has(local_path);
  101. }
  102. Ref<Resource> ResourceLoader::get_cached_ref(const String &p_path) {
  103. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  104. return ResourceCache::get_ref(local_path);
  105. }
  106. bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  107. return ::ResourceLoader::exists(p_path, p_type_hint);
  108. }
  109. ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
  110. return ::ResourceLoader::get_resource_uid(p_path);
  111. }
  112. Vector<String> ResourceLoader::list_directory(const String &p_directory) {
  113. return ::ResourceLoader::list_directory(p_directory);
  114. }
  115. void ResourceLoader::_bind_methods() {
  116. 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));
  117. ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &ResourceLoader::load_threaded_get_status, DEFVAL_ARRAY);
  118. ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &ResourceLoader::load_threaded_get);
  119. ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
  120. ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &ResourceLoader::get_recognized_extensions_for_type);
  121. ClassDB::bind_method(D_METHOD("add_resource_format_loader", "format_loader", "at_front"), &ResourceLoader::add_resource_format_loader, DEFVAL(false));
  122. ClassDB::bind_method(D_METHOD("remove_resource_format_loader", "format_loader"), &ResourceLoader::remove_resource_format_loader);
  123. ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &ResourceLoader::set_abort_on_missing_resources);
  124. ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &ResourceLoader::get_dependencies);
  125. ClassDB::bind_method(D_METHOD("has_cached", "path"), &ResourceLoader::has_cached);
  126. ClassDB::bind_method(D_METHOD("get_cached_ref", "path"), &ResourceLoader::get_cached_ref);
  127. ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &ResourceLoader::exists, DEFVAL(""));
  128. ClassDB::bind_method(D_METHOD("get_resource_uid", "path"), &ResourceLoader::get_resource_uid);
  129. ClassDB::bind_method(D_METHOD("list_directory", "directory_path"), &ResourceLoader::list_directory);
  130. BIND_ENUM_CONSTANT(THREAD_LOAD_INVALID_RESOURCE);
  131. BIND_ENUM_CONSTANT(THREAD_LOAD_IN_PROGRESS);
  132. BIND_ENUM_CONSTANT(THREAD_LOAD_FAILED);
  133. BIND_ENUM_CONSTANT(THREAD_LOAD_LOADED);
  134. BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE);
  135. BIND_ENUM_CONSTANT(CACHE_MODE_REUSE);
  136. BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);
  137. BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE_DEEP);
  138. BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE_DEEP);
  139. }
  140. ////// ResourceSaver //////
  141. Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, BitField<SaverFlags> p_flags) {
  142. return ::ResourceSaver::save(p_resource, p_path, p_flags);
  143. }
  144. Vector<String> ResourceSaver::get_recognized_extensions(const Ref<Resource> &p_resource) {
  145. List<String> exts;
  146. ::ResourceSaver::get_recognized_extensions(p_resource, &exts);
  147. Vector<String> ret;
  148. for (const String &E : exts) {
  149. ret.push_back(E);
  150. }
  151. return ret;
  152. }
  153. void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
  154. ::ResourceSaver::add_resource_format_saver(p_format_saver, p_at_front);
  155. }
  156. void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
  157. ::ResourceSaver::remove_resource_format_saver(p_format_saver);
  158. }
  159. ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
  160. return ::ResourceSaver::get_resource_id_for_path(p_path, p_generate);
  161. }
  162. ResourceSaver *ResourceSaver::singleton = nullptr;
  163. void ResourceSaver::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("save", "resource", "path", "flags"), &ResourceSaver::save, DEFVAL(""), DEFVAL((uint32_t)FLAG_NONE));
  165. ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &ResourceSaver::get_recognized_extensions);
  166. ClassDB::bind_method(D_METHOD("add_resource_format_saver", "format_saver", "at_front"), &ResourceSaver::add_resource_format_saver, DEFVAL(false));
  167. ClassDB::bind_method(D_METHOD("remove_resource_format_saver", "format_saver"), &ResourceSaver::remove_resource_format_saver);
  168. ClassDB::bind_method(D_METHOD("get_resource_id_for_path", "path", "generate"), &ResourceSaver::get_resource_id_for_path, DEFVAL(false));
  169. BIND_BITFIELD_FLAG(FLAG_NONE);
  170. BIND_BITFIELD_FLAG(FLAG_RELATIVE_PATHS);
  171. BIND_BITFIELD_FLAG(FLAG_BUNDLE_RESOURCES);
  172. BIND_BITFIELD_FLAG(FLAG_CHANGE_PATH);
  173. BIND_BITFIELD_FLAG(FLAG_OMIT_EDITOR_PROPERTIES);
  174. BIND_BITFIELD_FLAG(FLAG_SAVE_BIG_ENDIAN);
  175. BIND_BITFIELD_FLAG(FLAG_COMPRESS);
  176. BIND_BITFIELD_FLAG(FLAG_REPLACE_SUBRESOURCE_PATHS);
  177. }
  178. ////// OS //////
  179. PackedByteArray OS::get_entropy(int p_bytes) {
  180. PackedByteArray pba;
  181. pba.resize(p_bytes);
  182. Error err = ::OS::get_singleton()->get_entropy(pba.ptrw(), p_bytes);
  183. ERR_FAIL_COND_V(err != OK, PackedByteArray());
  184. return pba;
  185. }
  186. String OS::get_system_ca_certificates() {
  187. return ::OS::get_singleton()->get_system_ca_certificates();
  188. }
  189. PackedStringArray OS::get_connected_midi_inputs() {
  190. return ::OS::get_singleton()->get_connected_midi_inputs();
  191. }
  192. void OS::open_midi_inputs() {
  193. ::OS::get_singleton()->open_midi_inputs();
  194. }
  195. void OS::close_midi_inputs() {
  196. ::OS::get_singleton()->close_midi_inputs();
  197. }
  198. void OS::set_use_file_access_save_and_swap(bool p_enable) {
  199. FileAccess::set_backup_save(p_enable);
  200. }
  201. void OS::set_low_processor_usage_mode(bool p_enabled) {
  202. ::OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  203. }
  204. bool OS::is_in_low_processor_usage_mode() const {
  205. return ::OS::get_singleton()->is_in_low_processor_usage_mode();
  206. }
  207. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  208. ::OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(p_usec);
  209. }
  210. int OS::get_low_processor_usage_mode_sleep_usec() const {
  211. return ::OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
  212. }
  213. void OS::set_delta_smoothing(bool p_enabled) {
  214. ::OS::get_singleton()->set_delta_smoothing(p_enabled);
  215. }
  216. bool OS::is_delta_smoothing_enabled() const {
  217. return ::OS::get_singleton()->is_delta_smoothing_enabled();
  218. }
  219. void OS::alert(const String &p_alert, const String &p_title) {
  220. ::OS::get_singleton()->alert(p_alert, p_title);
  221. }
  222. void OS::crash(const String &p_message) {
  223. CRASH_NOW_MSG(p_message);
  224. }
  225. Vector<String> OS::get_system_fonts() const {
  226. return ::OS::get_singleton()->get_system_fonts();
  227. }
  228. String OS::get_system_font_path(const String &p_font_name, int p_weight, int p_stretch, bool p_italic) const {
  229. return ::OS::get_singleton()->get_system_font_path(p_font_name, p_weight, p_stretch, p_italic);
  230. }
  231. Vector<String> OS::get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale, const String &p_script, int p_weight, int p_stretch, bool p_italic) const {
  232. return ::OS::get_singleton()->get_system_font_path_for_text(p_font_name, p_text, p_locale, p_script, p_weight, p_stretch, p_italic);
  233. }
  234. String OS::get_executable_path() const {
  235. return ::OS::get_singleton()->get_executable_path();
  236. }
  237. Error OS::shell_open(const String &p_uri) {
  238. if (p_uri.begins_with("res://")) {
  239. 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()`.");
  240. } else if (p_uri.begins_with("user://")) {
  241. 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()`.");
  242. }
  243. return ::OS::get_singleton()->shell_open(p_uri);
  244. }
  245. Error OS::shell_show_in_file_manager(const String &p_path, bool p_open_folder) {
  246. if (p_path.begins_with("res://")) {
  247. WARN_PRINT("Attempting to explore file path with the \"res://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_show_in_file_manager()`.");
  248. } else if (p_path.begins_with("user://")) {
  249. WARN_PRINT("Attempting to explore file path with the \"user://\" protocol. Use `ProjectSettings.globalize_path()` to convert a Godot-specific path to a system path before opening it with `OS.shell_show_in_file_manager()`.");
  250. }
  251. return ::OS::get_singleton()->shell_show_in_file_manager(p_path, p_open_folder);
  252. }
  253. String OS::read_string_from_stdin(int64_t p_buffer_size) {
  254. return ::OS::get_singleton()->get_stdin_string(p_buffer_size);
  255. }
  256. PackedByteArray OS::read_buffer_from_stdin(int64_t p_buffer_size) {
  257. return ::OS::get_singleton()->get_stdin_buffer(p_buffer_size);
  258. }
  259. OS::StdHandleType OS::get_stdin_type() const {
  260. return (OS::StdHandleType)::OS::get_singleton()->get_stdin_type();
  261. }
  262. OS::StdHandleType OS::get_stdout_type() const {
  263. return (OS::StdHandleType)::OS::get_singleton()->get_stdout_type();
  264. }
  265. OS::StdHandleType OS::get_stderr_type() const {
  266. return (OS::StdHandleType)::OS::get_singleton()->get_stderr_type();
  267. }
  268. int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) {
  269. List<String> args;
  270. for (const String &arg : p_arguments) {
  271. args.push_back(arg);
  272. }
  273. String pipe;
  274. int exitcode = 0;
  275. Error err = ::OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr, nullptr, p_open_console);
  276. // Default array should never be modified, it causes the hash of the method to change.
  277. if (!ClassDB::is_default_array_arg(r_output)) {
  278. r_output.push_back(pipe);
  279. }
  280. if (err != OK) {
  281. return -1;
  282. }
  283. return exitcode;
  284. }
  285. Dictionary OS::execute_with_pipe(const String &p_path, const Vector<String> &p_arguments, bool p_blocking) {
  286. List<String> args;
  287. for (const String &arg : p_arguments) {
  288. args.push_back(arg);
  289. }
  290. return ::OS::get_singleton()->execute_with_pipe(p_path, args, p_blocking);
  291. }
  292. int OS::create_instance(const Vector<String> &p_arguments) {
  293. List<String> args;
  294. for (const String &arg : p_arguments) {
  295. args.push_back(arg);
  296. }
  297. ::OS::ProcessID pid = 0;
  298. Error err = ::OS::get_singleton()->create_instance(args, &pid);
  299. if (err != OK) {
  300. return -1;
  301. }
  302. return pid;
  303. }
  304. int OS::create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console) {
  305. List<String> args;
  306. for (const String &arg : p_arguments) {
  307. args.push_back(arg);
  308. }
  309. ::OS::ProcessID pid = 0;
  310. Error err = ::OS::get_singleton()->create_process(p_path, args, &pid, p_open_console);
  311. if (err != OK) {
  312. return -1;
  313. }
  314. return pid;
  315. }
  316. Error OS::kill(int p_pid) {
  317. return ::OS::get_singleton()->kill(p_pid);
  318. }
  319. bool OS::is_process_running(int p_pid) const {
  320. return ::OS::get_singleton()->is_process_running(p_pid);
  321. }
  322. int OS::get_process_exit_code(int p_pid) const {
  323. return ::OS::get_singleton()->get_process_exit_code(p_pid);
  324. }
  325. int OS::get_process_id() const {
  326. return ::OS::get_singleton()->get_process_id();
  327. }
  328. bool OS::has_environment(const String &p_var) const {
  329. return ::OS::get_singleton()->has_environment(p_var);
  330. }
  331. String OS::get_environment(const String &p_var) const {
  332. return ::OS::get_singleton()->get_environment(p_var);
  333. }
  334. void OS::set_environment(const String &p_var, const String &p_value) const {
  335. ::OS::get_singleton()->set_environment(p_var, p_value);
  336. }
  337. void OS::unset_environment(const String &p_var) const {
  338. ::OS::get_singleton()->unset_environment(p_var);
  339. }
  340. String OS::get_name() const {
  341. return ::OS::get_singleton()->get_name();
  342. }
  343. String OS::get_distribution_name() const {
  344. return ::OS::get_singleton()->get_distribution_name();
  345. }
  346. String OS::get_version() const {
  347. return ::OS::get_singleton()->get_version();
  348. }
  349. String OS::get_version_alias() const {
  350. return ::OS::get_singleton()->get_version_alias();
  351. }
  352. Vector<String> OS::get_video_adapter_driver_info() const {
  353. return ::OS::get_singleton()->get_video_adapter_driver_info();
  354. }
  355. Vector<String> OS::get_cmdline_args() {
  356. List<String> cmdline = ::OS::get_singleton()->get_cmdline_args();
  357. Vector<String> cmdlinev;
  358. for (const String &E : cmdline) {
  359. cmdlinev.push_back(E);
  360. }
  361. return cmdlinev;
  362. }
  363. Vector<String> OS::get_cmdline_user_args() {
  364. List<String> cmdline = ::OS::get_singleton()->get_cmdline_user_args();
  365. Vector<String> cmdlinev;
  366. for (const String &E : cmdline) {
  367. cmdlinev.push_back(E);
  368. }
  369. return cmdlinev;
  370. }
  371. void OS::set_restart_on_exit(bool p_restart, const Vector<String> &p_restart_arguments) {
  372. List<String> args_list;
  373. for (const String &restart_argument : p_restart_arguments) {
  374. args_list.push_back(restart_argument);
  375. }
  376. ::OS::get_singleton()->set_restart_on_exit(p_restart, args_list);
  377. }
  378. bool OS::is_restart_on_exit_set() const {
  379. return ::OS::get_singleton()->is_restart_on_exit_set();
  380. }
  381. Vector<String> OS::get_restart_on_exit_arguments() const {
  382. List<String> args = ::OS::get_singleton()->get_restart_on_exit_arguments();
  383. Vector<String> args_vector;
  384. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  385. args_vector.push_back(E->get());
  386. }
  387. return args_vector;
  388. }
  389. String OS::get_locale() const {
  390. return ::OS::get_singleton()->get_locale();
  391. }
  392. String OS::get_locale_language() const {
  393. return ::OS::get_singleton()->get_locale_language();
  394. }
  395. String OS::get_model_name() const {
  396. return ::OS::get_singleton()->get_model_name();
  397. }
  398. Error OS::set_thread_name(const String &p_name) {
  399. return ::Thread::set_name(p_name);
  400. }
  401. ::Thread::ID OS::get_thread_caller_id() const {
  402. return ::Thread::get_caller_id();
  403. }
  404. ::Thread::ID OS::get_main_thread_id() const {
  405. return ::Thread::get_main_id();
  406. }
  407. bool OS::has_feature(const String &p_feature) const {
  408. const bool *value_ptr = feature_cache.getptr(p_feature);
  409. if (value_ptr) {
  410. return *value_ptr;
  411. } else {
  412. const bool has = ::OS::get_singleton()->has_feature(p_feature);
  413. feature_cache[p_feature] = has;
  414. return has;
  415. }
  416. }
  417. bool OS::is_sandboxed() const {
  418. return ::OS::get_singleton()->is_sandboxed();
  419. }
  420. uint64_t OS::get_static_memory_usage() const {
  421. return ::OS::get_singleton()->get_static_memory_usage();
  422. }
  423. uint64_t OS::get_static_memory_peak_usage() const {
  424. return ::OS::get_singleton()->get_static_memory_peak_usage();
  425. }
  426. Dictionary OS::get_memory_info() const {
  427. return ::OS::get_singleton()->get_memory_info();
  428. }
  429. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  430. void OS::delay_usec(int p_usec) const {
  431. ERR_FAIL_COND_MSG(
  432. p_usec < 0,
  433. vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
  434. ::OS::get_singleton()->delay_usec(p_usec);
  435. }
  436. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  437. void OS::delay_msec(int p_msec) const {
  438. ERR_FAIL_COND_MSG(
  439. p_msec < 0,
  440. vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
  441. ::OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  442. }
  443. bool OS::is_userfs_persistent() const {
  444. return ::OS::get_singleton()->is_userfs_persistent();
  445. }
  446. int OS::get_processor_count() const {
  447. return ::OS::get_singleton()->get_processor_count();
  448. }
  449. String OS::get_processor_name() const {
  450. return ::OS::get_singleton()->get_processor_name();
  451. }
  452. bool OS::is_stdout_verbose() const {
  453. return ::OS::get_singleton()->is_stdout_verbose();
  454. }
  455. Error OS::move_to_trash(const String &p_path) const {
  456. return ::OS::get_singleton()->move_to_trash(p_path);
  457. }
  458. String OS::get_user_data_dir() const {
  459. return ::OS::get_singleton()->get_user_data_dir();
  460. }
  461. String OS::get_config_dir() const {
  462. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  463. return ::OS::get_singleton()->get_config_path();
  464. }
  465. String OS::get_data_dir() const {
  466. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  467. return ::OS::get_singleton()->get_data_path();
  468. }
  469. String OS::get_cache_dir() const {
  470. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  471. return ::OS::get_singleton()->get_cache_path();
  472. }
  473. String OS::get_temp_dir() const {
  474. // Exposed as `get_temp_dir()` instead of `get_temp_path()` for consistency with other exposed OS methods.
  475. return ::OS::get_singleton()->get_temp_path();
  476. }
  477. bool OS::is_debug_build() const {
  478. #ifdef DEBUG_ENABLED
  479. return true;
  480. #else
  481. return false;
  482. #endif
  483. }
  484. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  485. return ::OS::get_singleton()->get_system_dir(::OS::SystemDir(p_dir), p_shared_storage);
  486. }
  487. String OS::get_keycode_string(Key p_code) const {
  488. return ::keycode_get_string(p_code);
  489. }
  490. bool OS::is_keycode_unicode(char32_t p_unicode) const {
  491. return ::keycode_has_unicode((Key)p_unicode);
  492. }
  493. Key OS::find_keycode_from_string(const String &p_code) const {
  494. return find_keycode(p_code);
  495. }
  496. bool OS::request_permission(const String &p_name) {
  497. return ::OS::get_singleton()->request_permission(p_name);
  498. }
  499. bool OS::request_permissions() {
  500. return ::OS::get_singleton()->request_permissions();
  501. }
  502. Vector<String> OS::get_granted_permissions() const {
  503. return ::OS::get_singleton()->get_granted_permissions();
  504. }
  505. void OS::revoke_granted_permissions() {
  506. ::OS::get_singleton()->revoke_granted_permissions();
  507. }
  508. String OS::get_unique_id() const {
  509. return ::OS::get_singleton()->get_unique_id();
  510. }
  511. OS *OS::singleton = nullptr;
  512. void OS::_bind_methods() {
  513. ClassDB::bind_method(D_METHOD("get_entropy", "size"), &OS::get_entropy);
  514. ClassDB::bind_method(D_METHOD("get_system_ca_certificates"), &OS::get_system_ca_certificates);
  515. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &OS::get_connected_midi_inputs);
  516. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &OS::open_midi_inputs);
  517. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &OS::close_midi_inputs);
  518. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &OS::alert, DEFVAL("Alert!"));
  519. ClassDB::bind_method(D_METHOD("crash", "message"), &OS::crash);
  520. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &OS::set_low_processor_usage_mode);
  521. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &OS::is_in_low_processor_usage_mode);
  522. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &OS::set_low_processor_usage_mode_sleep_usec);
  523. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &OS::get_low_processor_usage_mode_sleep_usec);
  524. ClassDB::bind_method(D_METHOD("set_delta_smoothing", "delta_smoothing_enabled"), &OS::set_delta_smoothing);
  525. ClassDB::bind_method(D_METHOD("is_delta_smoothing_enabled"), &OS::is_delta_smoothing_enabled);
  526. ClassDB::bind_method(D_METHOD("get_processor_count"), &OS::get_processor_count);
  527. ClassDB::bind_method(D_METHOD("get_processor_name"), &OS::get_processor_name);
  528. ClassDB::bind_method(D_METHOD("get_system_fonts"), &OS::get_system_fonts);
  529. ClassDB::bind_method(D_METHOD("get_system_font_path", "font_name", "weight", "stretch", "italic"), &OS::get_system_font_path, DEFVAL(400), DEFVAL(100), DEFVAL(false));
  530. ClassDB::bind_method(D_METHOD("get_system_font_path_for_text", "font_name", "text", "locale", "script", "weight", "stretch", "italic"), &OS::get_system_font_path_for_text, DEFVAL(String()), DEFVAL(String()), DEFVAL(400), DEFVAL(100), DEFVAL(false));
  531. ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
  532. ClassDB::bind_method(D_METHOD("read_string_from_stdin", "buffer_size"), &OS::read_string_from_stdin);
  533. ClassDB::bind_method(D_METHOD("read_buffer_from_stdin", "buffer_size"), &OS::read_buffer_from_stdin);
  534. ClassDB::bind_method(D_METHOD("get_stdin_type"), &OS::get_stdin_type);
  535. ClassDB::bind_method(D_METHOD("get_stdout_type"), &OS::get_stdout_type);
  536. ClassDB::bind_method(D_METHOD("get_stderr_type"), &OS::get_stderr_type);
  537. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL_ARRAY, DEFVAL(false), DEFVAL(false));
  538. ClassDB::bind_method(D_METHOD("execute_with_pipe", "path", "arguments", "blocking"), &OS::execute_with_pipe, DEFVAL(true));
  539. ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
  540. ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
  541. ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
  542. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &OS::shell_open);
  543. ClassDB::bind_method(D_METHOD("shell_show_in_file_manager", "file_or_dir_path", "open_folder"), &OS::shell_show_in_file_manager, DEFVAL(true));
  544. ClassDB::bind_method(D_METHOD("is_process_running", "pid"), &OS::is_process_running);
  545. ClassDB::bind_method(D_METHOD("get_process_exit_code", "pid"), &OS::get_process_exit_code);
  546. ClassDB::bind_method(D_METHOD("get_process_id"), &OS::get_process_id);
  547. ClassDB::bind_method(D_METHOD("has_environment", "variable"), &OS::has_environment);
  548. ClassDB::bind_method(D_METHOD("get_environment", "variable"), &OS::get_environment);
  549. ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &OS::set_environment);
  550. ClassDB::bind_method(D_METHOD("unset_environment", "variable"), &OS::unset_environment);
  551. ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name);
  552. ClassDB::bind_method(D_METHOD("get_distribution_name"), &OS::get_distribution_name);
  553. ClassDB::bind_method(D_METHOD("get_version"), &OS::get_version);
  554. ClassDB::bind_method(D_METHOD("get_version_alias"), &OS::get_version_alias);
  555. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &OS::get_cmdline_args);
  556. ClassDB::bind_method(D_METHOD("get_cmdline_user_args"), &OS::get_cmdline_user_args);
  557. ClassDB::bind_method(D_METHOD("get_video_adapter_driver_info"), &OS::get_video_adapter_driver_info);
  558. ClassDB::bind_method(D_METHOD("set_restart_on_exit", "restart", "arguments"), &OS::set_restart_on_exit, DEFVAL(Vector<String>()));
  559. ClassDB::bind_method(D_METHOD("is_restart_on_exit_set"), &OS::is_restart_on_exit_set);
  560. ClassDB::bind_method(D_METHOD("get_restart_on_exit_arguments"), &OS::get_restart_on_exit_arguments);
  561. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &OS::delay_usec);
  562. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &OS::delay_msec);
  563. ClassDB::bind_method(D_METHOD("get_locale"), &OS::get_locale);
  564. ClassDB::bind_method(D_METHOD("get_locale_language"), &OS::get_locale_language);
  565. ClassDB::bind_method(D_METHOD("get_model_name"), &OS::get_model_name);
  566. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &OS::is_userfs_persistent);
  567. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &OS::is_stdout_verbose);
  568. ClassDB::bind_method(D_METHOD("is_debug_build"), &OS::is_debug_build);
  569. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &OS::get_static_memory_usage);
  570. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &OS::get_static_memory_peak_usage);
  571. ClassDB::bind_method(D_METHOD("get_memory_info"), &OS::get_memory_info);
  572. ClassDB::bind_method(D_METHOD("move_to_trash", "path"), &OS::move_to_trash);
  573. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &OS::get_user_data_dir);
  574. ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &OS::get_system_dir, DEFVAL(true));
  575. ClassDB::bind_method(D_METHOD("get_config_dir"), &OS::get_config_dir);
  576. ClassDB::bind_method(D_METHOD("get_data_dir"), &OS::get_data_dir);
  577. ClassDB::bind_method(D_METHOD("get_cache_dir"), &OS::get_cache_dir);
  578. ClassDB::bind_method(D_METHOD("get_temp_dir"), &OS::get_temp_dir);
  579. ClassDB::bind_method(D_METHOD("get_unique_id"), &OS::get_unique_id);
  580. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &OS::get_keycode_string);
  581. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &OS::is_keycode_unicode);
  582. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &OS::find_keycode_from_string);
  583. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &OS::set_use_file_access_save_and_swap);
  584. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &OS::set_thread_name);
  585. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &OS::get_thread_caller_id);
  586. ClassDB::bind_method(D_METHOD("get_main_thread_id"), &OS::get_main_thread_id);
  587. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &OS::has_feature);
  588. ClassDB::bind_method(D_METHOD("is_sandboxed"), &OS::is_sandboxed);
  589. ClassDB::bind_method(D_METHOD("request_permission", "name"), &OS::request_permission);
  590. ClassDB::bind_method(D_METHOD("request_permissions"), &OS::request_permissions);
  591. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &OS::get_granted_permissions);
  592. ClassDB::bind_method(D_METHOD("revoke_granted_permissions"), &OS::revoke_granted_permissions);
  593. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  594. 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");
  595. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "delta_smoothing"), "set_delta_smoothing", "is_delta_smoothing_enabled");
  596. // Those default values need to be specified for the docs generator,
  597. // to avoid using values from the documentation writer's own OS instance.
  598. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  599. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  600. BIND_ENUM_CONSTANT(RENDERING_DRIVER_VULKAN);
  601. BIND_ENUM_CONSTANT(RENDERING_DRIVER_OPENGL3);
  602. BIND_ENUM_CONSTANT(RENDERING_DRIVER_D3D12);
  603. BIND_ENUM_CONSTANT(RENDERING_DRIVER_METAL);
  604. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  605. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  606. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  607. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  608. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  609. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  610. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  611. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  612. BIND_ENUM_CONSTANT(STD_HANDLE_INVALID);
  613. BIND_ENUM_CONSTANT(STD_HANDLE_CONSOLE);
  614. BIND_ENUM_CONSTANT(STD_HANDLE_FILE);
  615. BIND_ENUM_CONSTANT(STD_HANDLE_PIPE);
  616. BIND_ENUM_CONSTANT(STD_HANDLE_UNKNOWN);
  617. }
  618. ////// Geometry2D //////
  619. Geometry2D *Geometry2D::singleton = nullptr;
  620. Geometry2D *Geometry2D::get_singleton() {
  621. return singleton;
  622. }
  623. bool Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  624. return ::Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  625. }
  626. real_t Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  627. return ::Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  628. }
  629. 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) {
  630. Vector2 result;
  631. if (::Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  632. return result;
  633. } else {
  634. return Variant();
  635. }
  636. }
  637. 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) {
  638. Vector2 result;
  639. if (::Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  640. return result;
  641. } else {
  642. return Variant();
  643. }
  644. }
  645. Vector<Vector2> Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  646. Vector2 r1, r2;
  647. ::Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  648. Vector<Vector2> r = { r1, r2 };
  649. return r;
  650. }
  651. Vector2 Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  652. Vector2 s[2] = { p_a, p_b };
  653. return ::Geometry2D::get_closest_point_to_segment(p_point, s);
  654. }
  655. Vector2 Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  656. Vector2 s[2] = { p_a, p_b };
  657. return ::Geometry2D::get_closest_point_to_segment_uncapped(p_point, s);
  658. }
  659. bool Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  660. return ::Geometry2D::is_point_in_triangle(s, a, b, c);
  661. }
  662. bool Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  663. return ::Geometry2D::is_polygon_clockwise(p_polygon);
  664. }
  665. bool Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  666. return ::Geometry2D::is_point_in_polygon(p_point, p_polygon);
  667. }
  668. Vector<int> Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  669. return ::Geometry2D::triangulate_polygon(p_polygon);
  670. }
  671. Vector<int> Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
  672. return ::Geometry2D::triangulate_delaunay(p_points);
  673. }
  674. Vector<Point2> Geometry2D::convex_hull(const Vector<Point2> &p_points) {
  675. return ::Geometry2D::convex_hull(p_points);
  676. }
  677. TypedArray<PackedVector2Array> Geometry2D::decompose_polygon_in_convex(const Vector<Vector2> &p_polygon) {
  678. Vector<Vector<Point2>> decomp = ::Geometry2D::decompose_polygon_in_convex(p_polygon);
  679. TypedArray<PackedVector2Array> ret;
  680. for (int i = 0; i < decomp.size(); ++i) {
  681. ret.push_back(decomp[i]);
  682. }
  683. return ret;
  684. }
  685. TypedArray<PackedVector2Array> Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  686. Vector<Vector<Point2>> polys = ::Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
  687. TypedArray<PackedVector2Array> ret;
  688. for (int i = 0; i < polys.size(); ++i) {
  689. ret.push_back(polys[i]);
  690. }
  691. return ret;
  692. }
  693. TypedArray<PackedVector2Array> Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  694. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
  695. TypedArray<PackedVector2Array> ret;
  696. for (int i = 0; i < polys.size(); ++i) {
  697. ret.push_back(polys[i]);
  698. }
  699. return ret;
  700. }
  701. TypedArray<PackedVector2Array> Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  702. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
  703. TypedArray<PackedVector2Array> ret;
  704. for (int i = 0; i < polys.size(); ++i) {
  705. ret.push_back(polys[i]);
  706. }
  707. return ret;
  708. }
  709. TypedArray<PackedVector2Array> Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  710. Vector<Vector<Point2>> polys = ::Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b);
  711. TypedArray<PackedVector2Array> ret;
  712. for (int i = 0; i < polys.size(); ++i) {
  713. ret.push_back(polys[i]);
  714. }
  715. return ret;
  716. }
  717. TypedArray<PackedVector2Array> Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  718. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
  719. TypedArray<PackedVector2Array> ret;
  720. for (int i = 0; i < polys.size(); ++i) {
  721. ret.push_back(polys[i]);
  722. }
  723. return ret;
  724. }
  725. TypedArray<PackedVector2Array> Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  726. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon);
  727. TypedArray<PackedVector2Array> ret;
  728. for (int i = 0; i < polys.size(); ++i) {
  729. ret.push_back(polys[i]);
  730. }
  731. return ret;
  732. }
  733. TypedArray<PackedVector2Array> Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  734. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polygon(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type));
  735. TypedArray<PackedVector2Array> ret;
  736. for (int i = 0; i < polys.size(); ++i) {
  737. ret.push_back(polys[i]);
  738. }
  739. return ret;
  740. }
  741. TypedArray<PackedVector2Array> Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  742. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polyline(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type), ::Geometry2D::PolyEndType(p_end_type));
  743. TypedArray<PackedVector2Array> ret;
  744. for (int i = 0; i < polys.size(); ++i) {
  745. ret.push_back(polys[i]);
  746. }
  747. return ret;
  748. }
  749. Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
  750. Dictionary ret;
  751. Vector<Size2i> rects;
  752. for (int i = 0; i < p_rects.size(); i++) {
  753. rects.push_back(p_rects[i]);
  754. }
  755. Vector<Point2i> result;
  756. Size2i size;
  757. ::Geometry2D::make_atlas(rects, result, size);
  758. Vector<Point2> r_result;
  759. for (int i = 0; i < result.size(); i++) {
  760. r_result.push_back(result[i]);
  761. }
  762. ret["points"] = r_result;
  763. ret["size"] = size;
  764. return ret;
  765. }
  766. TypedArray<Point2i> Geometry2D::bresenham_line(const Point2i &p_from, const Point2i &p_to) {
  767. Vector<Point2i> points = ::Geometry2D::bresenham_line(p_from, p_to);
  768. TypedArray<Point2i> result;
  769. result.resize(points.size());
  770. for (int i = 0; i < points.size(); i++) {
  771. result[i] = points[i];
  772. }
  773. return result;
  774. }
  775. void Geometry2D::_bind_methods() {
  776. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &Geometry2D::is_point_in_circle);
  777. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &Geometry2D::segment_intersects_circle);
  778. ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &Geometry2D::segment_intersects_segment);
  779. ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &Geometry2D::line_intersects_line);
  780. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &Geometry2D::get_closest_points_between_segments);
  781. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment);
  782. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment_uncapped);
  783. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &Geometry2D::point_is_inside_triangle);
  784. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &Geometry2D::is_polygon_clockwise);
  785. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &Geometry2D::is_point_in_polygon);
  786. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &Geometry2D::triangulate_polygon);
  787. ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &Geometry2D::triangulate_delaunay);
  788. ClassDB::bind_method(D_METHOD("convex_hull", "points"), &Geometry2D::convex_hull);
  789. ClassDB::bind_method(D_METHOD("decompose_polygon_in_convex", "polygon"), &Geometry2D::decompose_polygon_in_convex);
  790. ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &Geometry2D::merge_polygons);
  791. ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &Geometry2D::clip_polygons);
  792. ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &Geometry2D::intersect_polygons);
  793. ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &Geometry2D::exclude_polygons);
  794. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::clip_polyline_with_polygon);
  795. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::intersect_polyline_with_polygon);
  796. ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
  797. ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  798. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &Geometry2D::make_atlas);
  799. ClassDB::bind_method(D_METHOD("bresenham_line", "from", "to"), &Geometry2D::bresenham_line);
  800. BIND_ENUM_CONSTANT(OPERATION_UNION);
  801. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  802. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  803. BIND_ENUM_CONSTANT(OPERATION_XOR);
  804. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  805. BIND_ENUM_CONSTANT(JOIN_ROUND);
  806. BIND_ENUM_CONSTANT(JOIN_MITER);
  807. BIND_ENUM_CONSTANT(END_POLYGON);
  808. BIND_ENUM_CONSTANT(END_JOINED);
  809. BIND_ENUM_CONSTANT(END_BUTT);
  810. BIND_ENUM_CONSTANT(END_SQUARE);
  811. BIND_ENUM_CONSTANT(END_ROUND);
  812. }
  813. ////// Geometry3D //////
  814. Geometry3D *Geometry3D::singleton = nullptr;
  815. Geometry3D *Geometry3D::get_singleton() {
  816. return singleton;
  817. }
  818. Vector<Vector3> Geometry3D::compute_convex_mesh_points(const TypedArray<Plane> &p_planes) {
  819. Vector<Plane> planes_vec;
  820. int size = p_planes.size();
  821. planes_vec.resize(size);
  822. for (int i = 0; i < size; ++i) {
  823. planes_vec.set(i, p_planes[i]);
  824. }
  825. Variant ret = ::Geometry3D::compute_convex_mesh_points(planes_vec.ptr(), size);
  826. return ret;
  827. }
  828. TypedArray<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
  829. Variant ret = ::Geometry3D::build_box_planes(p_extents);
  830. return ret;
  831. }
  832. TypedArray<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  833. Variant ret = ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  834. return ret;
  835. }
  836. TypedArray<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  837. Variant ret = ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  838. return ret;
  839. }
  840. Vector<Vector3> Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  841. Vector3 r1, r2;
  842. ::Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  843. Vector<Vector3> r = { r1, r2 };
  844. return r;
  845. }
  846. Vector3 Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  847. Vector3 s[2] = { p_a, p_b };
  848. return ::Geometry3D::get_closest_point_to_segment(p_point, s);
  849. }
  850. Vector3 Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  851. Vector3 s[2] = { p_a, p_b };
  852. return ::Geometry3D::get_closest_point_to_segment_uncapped(p_point, s);
  853. }
  854. Vector3 Geometry3D::get_triangle_barycentric_coords(const Vector3 &p_point, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
  855. Vector3 res = ::Geometry3D::triangle_get_barycentric_coords(p_v0, p_v1, p_v2, p_point);
  856. return res;
  857. }
  858. 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) {
  859. Vector3 res;
  860. if (::Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  861. return res;
  862. } else {
  863. return Variant();
  864. }
  865. }
  866. 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) {
  867. Vector3 res;
  868. if (::Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  869. return res;
  870. } else {
  871. return Variant();
  872. }
  873. }
  874. Vector<Vector3> Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  875. Vector<Vector3> r;
  876. Vector3 res, norm;
  877. if (!::Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  878. return r;
  879. }
  880. r.resize(2);
  881. r.set(0, res);
  882. r.set(1, norm);
  883. return r;
  884. }
  885. Vector<Vector3> Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  886. Vector<Vector3> r;
  887. Vector3 res, norm;
  888. if (!::Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  889. return r;
  890. }
  891. r.resize(2);
  892. r.set(0, res);
  893. r.set(1, norm);
  894. return r;
  895. }
  896. Vector<Vector3> Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const TypedArray<Plane> &p_planes) {
  897. Vector<Vector3> r;
  898. Vector3 res, norm;
  899. Vector<Plane> planes = Variant(p_planes);
  900. if (!::Geometry3D::segment_intersects_convex(p_from, p_to, planes.ptr(), planes.size(), &res, &norm)) {
  901. return r;
  902. }
  903. r.resize(2);
  904. r.set(0, res);
  905. r.set(1, norm);
  906. return r;
  907. }
  908. Vector<Vector3> Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  909. return ::Geometry3D::clip_polygon(p_points, p_plane);
  910. }
  911. Vector<int32_t> Geometry3D::tetrahedralize_delaunay(const Vector<Vector3> &p_points) {
  912. return ::Geometry3D::tetrahedralize_delaunay(p_points);
  913. }
  914. void Geometry3D::_bind_methods() {
  915. ClassDB::bind_method(D_METHOD("compute_convex_mesh_points", "planes"), &Geometry3D::compute_convex_mesh_points);
  916. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &Geometry3D::build_box_planes);
  917. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  918. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  919. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &Geometry3D::get_closest_points_between_segments);
  920. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment);
  921. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment_uncapped);
  922. ClassDB::bind_method(D_METHOD("get_triangle_barycentric_coords", "point", "a", "b", "c"), &Geometry3D::get_triangle_barycentric_coords);
  923. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &Geometry3D::ray_intersects_triangle);
  924. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &Geometry3D::segment_intersects_triangle);
  925. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &Geometry3D::segment_intersects_sphere);
  926. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &Geometry3D::segment_intersects_cylinder);
  927. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &Geometry3D::segment_intersects_convex);
  928. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &Geometry3D::clip_polygon);
  929. ClassDB::bind_method(D_METHOD("tetrahedralize_delaunay", "points"), &Geometry3D::tetrahedralize_delaunay);
  930. }
  931. ////// Marshalls //////
  932. Marshalls *Marshalls::singleton = nullptr;
  933. Marshalls *Marshalls::get_singleton() {
  934. return singleton;
  935. }
  936. String Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  937. int len;
  938. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  939. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  940. Vector<uint8_t> buff;
  941. buff.resize(len);
  942. uint8_t *w = buff.ptrw();
  943. err = encode_variant(p_var, &w[0], len, p_full_objects);
  944. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  945. String ret = CryptoCore::b64_encode_str(&w[0], len);
  946. ERR_FAIL_COND_V(ret.is_empty(), ret);
  947. return ret;
  948. }
  949. Variant Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  950. int strlen = p_str.length();
  951. CharString cstr = p_str.ascii();
  952. Vector<uint8_t> buf;
  953. buf.resize(strlen / 4 * 3 + 1);
  954. uint8_t *w = buf.ptrw();
  955. size_t len = 0;
  956. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  957. Variant v;
  958. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  959. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  960. return v;
  961. }
  962. String Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  963. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  964. ERR_FAIL_COND_V(ret.is_empty(), ret);
  965. return ret;
  966. }
  967. Vector<uint8_t> Marshalls::base64_to_raw(const String &p_str) {
  968. int strlen = p_str.length();
  969. CharString cstr = p_str.ascii();
  970. size_t arr_len = 0;
  971. Vector<uint8_t> buf;
  972. {
  973. buf.resize(strlen / 4 * 3 + 1);
  974. uint8_t *w = buf.ptrw();
  975. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  976. }
  977. buf.resize(arr_len);
  978. return buf;
  979. }
  980. String Marshalls::utf8_to_base64(const String &p_str) {
  981. CharString cstr = p_str.utf8();
  982. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  983. ERR_FAIL_COND_V(ret.is_empty(), ret);
  984. return ret;
  985. }
  986. String Marshalls::base64_to_utf8(const String &p_str) {
  987. int strlen = p_str.length();
  988. CharString cstr = p_str.ascii();
  989. Vector<uint8_t> buf;
  990. buf.resize(strlen / 4 * 3 + 1 + 1);
  991. uint8_t *w = buf.ptrw();
  992. size_t len = 0;
  993. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  994. w[len] = 0;
  995. String ret = String::utf8((char *)&w[0]);
  996. return ret;
  997. }
  998. void Marshalls::_bind_methods() {
  999. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &Marshalls::variant_to_base64, DEFVAL(false));
  1000. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &Marshalls::base64_to_variant, DEFVAL(false));
  1001. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &Marshalls::raw_to_base64);
  1002. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &Marshalls::base64_to_raw);
  1003. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &Marshalls::utf8_to_base64);
  1004. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &Marshalls::base64_to_utf8);
  1005. }
  1006. ////// Semaphore //////
  1007. void Semaphore::wait() {
  1008. semaphore.wait();
  1009. }
  1010. bool Semaphore::try_wait() {
  1011. return semaphore.try_wait();
  1012. }
  1013. void Semaphore::post(int p_count) {
  1014. ERR_FAIL_COND(p_count <= 0);
  1015. semaphore.post(p_count);
  1016. }
  1017. void Semaphore::_bind_methods() {
  1018. ClassDB::bind_method(D_METHOD("wait"), &Semaphore::wait);
  1019. ClassDB::bind_method(D_METHOD("try_wait"), &Semaphore::try_wait);
  1020. ClassDB::bind_method(D_METHOD("post", "count"), &Semaphore::post, DEFVAL(1));
  1021. }
  1022. ////// Mutex //////
  1023. void Mutex::lock() {
  1024. mutex.lock();
  1025. }
  1026. bool Mutex::try_lock() {
  1027. return mutex.try_lock();
  1028. }
  1029. void Mutex::unlock() {
  1030. mutex.unlock();
  1031. }
  1032. void Mutex::_bind_methods() {
  1033. ClassDB::bind_method(D_METHOD("lock"), &Mutex::lock);
  1034. ClassDB::bind_method(D_METHOD("try_lock"), &Mutex::try_lock);
  1035. ClassDB::bind_method(D_METHOD("unlock"), &Mutex::unlock);
  1036. }
  1037. ////// Thread //////
  1038. void Thread::_start_func(void *ud) {
  1039. Ref<Thread> *tud = (Ref<Thread> *)ud;
  1040. Ref<Thread> t = *tud;
  1041. memdelete(tud);
  1042. if (!t->target_callable.is_valid()) {
  1043. t->running.clear();
  1044. 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()));
  1045. }
  1046. // Finding out a suitable name for the thread can involve querying a node, if the target is one.
  1047. // We know this is safe (unless the user is causing life cycle race conditions, which would be a bug on their part).
  1048. set_current_thread_safe_for_nodes(true);
  1049. String func_name = t->target_callable.is_custom() ? t->target_callable.get_custom()->get_as_text() : String(t->target_callable.get_method());
  1050. set_current_thread_safe_for_nodes(false);
  1051. ::Thread::set_name(func_name);
  1052. // To avoid a circular reference between the thread and the script which can possibly contain a reference
  1053. // to the thread, we will do the call (keeping a reference up to that point) and then break chains with it.
  1054. // When the call returns, we will reference the thread again if possible.
  1055. ObjectID th_instance_id = t->get_instance_id();
  1056. Callable target_callable = t->target_callable;
  1057. t = Ref<Thread>();
  1058. Callable::CallError ce;
  1059. Variant ret;
  1060. target_callable.callp(nullptr, 0, ret, ce);
  1061. // If script properly kept a reference to the thread, we should be able to re-reference it now
  1062. // (well, or if the call failed, since we had to break chains anyway because the outcome isn't known upfront).
  1063. t = Ref<Thread>(ObjectDB::get_instance(th_instance_id));
  1064. if (t.is_valid()) {
  1065. t->ret = ret;
  1066. t->running.clear();
  1067. } else {
  1068. // We could print a warning here, but the Thread object will be eventually destroyed
  1069. // noticing wait_to_finish() hasn't been called on it, and it will print a warning itself.
  1070. }
  1071. if (ce.error != Callable::CallError::CALL_OK) {
  1072. ERR_FAIL_MSG(vformat("Could not call function '%s' to start thread %d: %s.", func_name, t->get_id(), Variant::get_callable_error_text(t->target_callable, nullptr, 0, ce)));
  1073. }
  1074. }
  1075. Error Thread::start(const Callable &p_callable, Priority p_priority) {
  1076. ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
  1077. ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
  1078. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  1079. ret = Variant();
  1080. target_callable = p_callable;
  1081. running.set();
  1082. Ref<Thread> *ud = memnew(Ref<Thread>(this));
  1083. ::Thread::Settings s;
  1084. s.priority = (::Thread::Priority)p_priority;
  1085. thread.start(_start_func, ud, s);
  1086. return OK;
  1087. }
  1088. String Thread::get_id() const {
  1089. return itos(thread.get_id());
  1090. }
  1091. bool Thread::is_started() const {
  1092. return thread.is_started();
  1093. }
  1094. bool Thread::is_alive() const {
  1095. return running.is_set();
  1096. }
  1097. Variant Thread::wait_to_finish() {
  1098. ERR_FAIL_COND_V_MSG(!is_started(), Variant(), "Thread must have been started to wait for its completion.");
  1099. thread.wait_to_finish();
  1100. Variant r = ret;
  1101. target_callable = Callable();
  1102. return r;
  1103. }
  1104. void Thread::set_thread_safety_checks_enabled(bool p_enabled) {
  1105. ERR_FAIL_COND_MSG(::Thread::is_main_thread(), "This call is forbidden on the main thread.");
  1106. set_current_thread_safe_for_nodes(!p_enabled);
  1107. }
  1108. void Thread::_bind_methods() {
  1109. ClassDB::bind_method(D_METHOD("start", "callable", "priority"), &Thread::start, DEFVAL(PRIORITY_NORMAL));
  1110. ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
  1111. ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
  1112. ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive);
  1113. ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish);
  1114. ClassDB::bind_static_method("Thread", D_METHOD("set_thread_safety_checks_enabled", "enabled"), &Thread::set_thread_safety_checks_enabled);
  1115. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  1116. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  1117. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  1118. }
  1119. namespace special {
  1120. ////// ClassDB //////
  1121. PackedStringArray ClassDB::get_class_list() const {
  1122. List<StringName> classes;
  1123. ::ClassDB::get_class_list(&classes);
  1124. PackedStringArray ret;
  1125. ret.resize(classes.size());
  1126. int idx = 0;
  1127. for (const StringName &E : classes) {
  1128. ret.set(idx++, E);
  1129. }
  1130. return ret;
  1131. }
  1132. PackedStringArray ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  1133. List<StringName> classes;
  1134. ::ClassDB::get_inheriters_from_class(p_class, &classes);
  1135. PackedStringArray ret;
  1136. ret.resize(classes.size());
  1137. int idx = 0;
  1138. for (const StringName &E : classes) {
  1139. ret.set(idx++, E);
  1140. }
  1141. return ret;
  1142. }
  1143. StringName ClassDB::get_parent_class(const StringName &p_class) const {
  1144. return ::ClassDB::get_parent_class(p_class);
  1145. }
  1146. bool ClassDB::class_exists(const StringName &p_class) const {
  1147. return ::ClassDB::class_exists(p_class);
  1148. }
  1149. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  1150. return ::ClassDB::is_parent_class(p_class, p_inherits);
  1151. }
  1152. bool ClassDB::can_instantiate(const StringName &p_class) const {
  1153. return ::ClassDB::can_instantiate(p_class);
  1154. }
  1155. Variant ClassDB::instantiate(const StringName &p_class) const {
  1156. Object *obj = ::ClassDB::instantiate(p_class);
  1157. if (!obj) {
  1158. return Variant();
  1159. }
  1160. RefCounted *r = Object::cast_to<RefCounted>(obj);
  1161. if (r) {
  1162. return Ref<RefCounted>(r);
  1163. } else {
  1164. return obj;
  1165. }
  1166. }
  1167. ClassDB::APIType ClassDB::class_get_api_type(const StringName &p_class) const {
  1168. ::ClassDB::APIType api_type = ::ClassDB::get_api_type(p_class);
  1169. return (APIType)api_type;
  1170. }
  1171. bool ClassDB::class_has_signal(const StringName &p_class, const StringName &p_signal) const {
  1172. return ::ClassDB::has_signal(p_class, p_signal);
  1173. }
  1174. Dictionary ClassDB::class_get_signal(const StringName &p_class, const StringName &p_signal) const {
  1175. MethodInfo signal;
  1176. if (::ClassDB::get_signal(p_class, p_signal, &signal)) {
  1177. return signal.operator Dictionary();
  1178. } else {
  1179. return Dictionary();
  1180. }
  1181. }
  1182. TypedArray<Dictionary> ClassDB::class_get_signal_list(const StringName &p_class, bool p_no_inheritance) const {
  1183. List<MethodInfo> signals;
  1184. ::ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1185. TypedArray<Dictionary> ret;
  1186. for (const MethodInfo &E : signals) {
  1187. ret.push_back(E.operator Dictionary());
  1188. }
  1189. return ret;
  1190. }
  1191. TypedArray<Dictionary> ClassDB::class_get_property_list(const StringName &p_class, bool p_no_inheritance) const {
  1192. List<PropertyInfo> plist;
  1193. ::ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1194. TypedArray<Dictionary> ret;
  1195. for (const PropertyInfo &E : plist) {
  1196. ret.push_back(E.operator Dictionary());
  1197. }
  1198. return ret;
  1199. }
  1200. StringName ClassDB::class_get_property_getter(const StringName &p_class, const StringName &p_property) {
  1201. return ::ClassDB::get_property_getter(p_class, p_property);
  1202. }
  1203. StringName ClassDB::class_get_property_setter(const StringName &p_class, const StringName &p_property) {
  1204. return ::ClassDB::get_property_setter(p_class, p_property);
  1205. }
  1206. Variant ClassDB::class_get_property(Object *p_object, const StringName &p_property) const {
  1207. Variant ret;
  1208. ::ClassDB::get_property(p_object, p_property, ret);
  1209. return ret;
  1210. }
  1211. Error ClassDB::class_set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1212. Variant ret;
  1213. bool valid;
  1214. if (!::ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1215. return ERR_UNAVAILABLE;
  1216. } else if (!valid) {
  1217. return ERR_INVALID_DATA;
  1218. }
  1219. return OK;
  1220. }
  1221. Variant ClassDB::class_get_property_default_value(const StringName &p_class, const StringName &p_property) const {
  1222. bool valid;
  1223. Variant ret = ::ClassDB::class_get_default_property_value(p_class, p_property, &valid);
  1224. if (valid) {
  1225. return ret;
  1226. }
  1227. return Variant();
  1228. }
  1229. bool ClassDB::class_has_method(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) const {
  1230. return ::ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1231. }
  1232. int ClassDB::class_get_method_argument_count(const StringName &p_class, const StringName &p_method, bool p_no_inheritance) const {
  1233. return ::ClassDB::get_method_argument_count(p_class, p_method, nullptr, p_no_inheritance);
  1234. }
  1235. TypedArray<Dictionary> ClassDB::class_get_method_list(const StringName &p_class, bool p_no_inheritance) const {
  1236. List<MethodInfo> methods;
  1237. ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1238. TypedArray<Dictionary> ret;
  1239. for (const MethodInfo &E : methods) {
  1240. #ifdef DEBUG_METHODS_ENABLED
  1241. ret.push_back(E.operator Dictionary());
  1242. #else
  1243. Dictionary dict;
  1244. dict["name"] = E.name;
  1245. ret.push_back(dict);
  1246. #endif
  1247. }
  1248. return ret;
  1249. }
  1250. Variant ClassDB::class_call_static(const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) {
  1251. if (p_argcount < 2) {
  1252. r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1253. return Variant::NIL;
  1254. }
  1255. if (!p_arguments[0]->is_string() || !p_arguments[1]->is_string()) {
  1256. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1257. return Variant::NIL;
  1258. }
  1259. StringName class_ = *p_arguments[0];
  1260. StringName method = *p_arguments[1];
  1261. const MethodBind *bind = ::ClassDB::get_method(class_, method);
  1262. ERR_FAIL_NULL_V_MSG(bind, Variant::NIL, "Cannot find static method.");
  1263. ERR_FAIL_COND_V_MSG(!bind->is_static(), Variant::NIL, "Method is not static.");
  1264. return bind->call(nullptr, p_arguments + 2, p_argcount - 2, r_call_error);
  1265. }
  1266. PackedStringArray ClassDB::class_get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1267. List<String> constants;
  1268. ::ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1269. PackedStringArray ret;
  1270. ret.resize(constants.size());
  1271. int idx = 0;
  1272. for (const String &E : constants) {
  1273. ret.set(idx++, E);
  1274. }
  1275. return ret;
  1276. }
  1277. bool ClassDB::class_has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1278. bool success;
  1279. ::ClassDB::get_integer_constant(p_class, p_name, &success);
  1280. return success;
  1281. }
  1282. int64_t ClassDB::class_get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1283. bool found;
  1284. int64_t c = ::ClassDB::get_integer_constant(p_class, p_name, &found);
  1285. ERR_FAIL_COND_V(!found, 0);
  1286. return c;
  1287. }
  1288. bool ClassDB::class_has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1289. return ::ClassDB::has_enum(p_class, p_name, p_no_inheritance);
  1290. }
  1291. PackedStringArray ClassDB::class_get_enum_list(const StringName &p_class, bool p_no_inheritance) const {
  1292. List<StringName> enums;
  1293. ::ClassDB::get_enum_list(p_class, &enums, p_no_inheritance);
  1294. PackedStringArray ret;
  1295. ret.resize(enums.size());
  1296. int idx = 0;
  1297. for (const StringName &E : enums) {
  1298. ret.set(idx++, E);
  1299. }
  1300. return ret;
  1301. }
  1302. PackedStringArray ClassDB::class_get_enum_constants(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
  1303. List<StringName> constants;
  1304. ::ClassDB::get_enum_constants(p_class, p_enum, &constants, p_no_inheritance);
  1305. PackedStringArray ret;
  1306. ret.resize(constants.size());
  1307. int idx = 0;
  1308. for (const StringName &E : constants) {
  1309. ret.set(idx++, E);
  1310. }
  1311. return ret;
  1312. }
  1313. StringName ClassDB::class_get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1314. return ::ClassDB::get_integer_constant_enum(p_class, p_name, p_no_inheritance);
  1315. }
  1316. bool ClassDB::is_class_enum_bitfield(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
  1317. return ::ClassDB::is_enum_bitfield(p_class, p_enum, p_no_inheritance);
  1318. }
  1319. bool ClassDB::is_class_enabled(const StringName &p_class) const {
  1320. return ::ClassDB::is_class_enabled(p_class);
  1321. }
  1322. #ifdef TOOLS_ENABLED
  1323. void ClassDB::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1324. const String pf = p_function;
  1325. bool first_argument_is_class = false;
  1326. if (p_idx == 0) {
  1327. first_argument_is_class = (pf == "get_inheriters_from_class" || pf == "get_parent_class" ||
  1328. pf == "class_exists" || pf == "can_instantiate" || pf == "instantiate" ||
  1329. pf == "class_has_signal" || pf == "class_get_signal" || pf == "class_get_signal_list" ||
  1330. pf == "class_get_property_list" || pf == "class_get_property" || pf == "class_set_property" ||
  1331. pf == "class_has_method" || pf == "class_get_method_list" ||
  1332. pf == "class_get_integer_constant_list" || pf == "class_has_integer_constant" || pf == "class_get_integer_constant" ||
  1333. pf == "class_has_enum" || pf == "class_get_enum_list" || pf == "class_get_enum_constants" || pf == "class_get_integer_constant_enum" ||
  1334. pf == "is_class_enabled" || pf == "is_class_enum_bitfield" || pf == "class_get_api_type");
  1335. }
  1336. if (first_argument_is_class || pf == "is_parent_class") {
  1337. for (const String &E : get_class_list()) {
  1338. r_options->push_back(E.quote());
  1339. }
  1340. }
  1341. Object::get_argument_options(p_function, p_idx, r_options);
  1342. }
  1343. #endif
  1344. void ClassDB::_bind_methods() {
  1345. ::ClassDB::bind_method(D_METHOD("get_class_list"), &ClassDB::get_class_list);
  1346. ::ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &ClassDB::get_inheriters_from_class);
  1347. ::ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &ClassDB::get_parent_class);
  1348. ::ClassDB::bind_method(D_METHOD("class_exists", "class"), &ClassDB::class_exists);
  1349. ::ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &ClassDB::is_parent_class);
  1350. ::ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &ClassDB::can_instantiate);
  1351. ::ClassDB::bind_method(D_METHOD("instantiate", "class"), &ClassDB::instantiate);
  1352. ::ClassDB::bind_method(D_METHOD("class_get_api_type", "class"), &ClassDB::class_get_api_type);
  1353. ::ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &ClassDB::class_has_signal);
  1354. ::ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &ClassDB::class_get_signal);
  1355. ::ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &ClassDB::class_get_signal_list, DEFVAL(false));
  1356. ::ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &ClassDB::class_get_property_list, DEFVAL(false));
  1357. ::ClassDB::bind_method(D_METHOD("class_get_property_getter", "class", "property"), &ClassDB::class_get_property_getter);
  1358. ::ClassDB::bind_method(D_METHOD("class_get_property_setter", "class", "property"), &ClassDB::class_get_property_setter);
  1359. ::ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &ClassDB::class_get_property);
  1360. ::ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &ClassDB::class_set_property);
  1361. ::ClassDB::bind_method(D_METHOD("class_get_property_default_value", "class", "property"), &ClassDB::class_get_property_default_value);
  1362. ::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::class_has_method, DEFVAL(false));
  1363. ::ClassDB::bind_method(D_METHOD("class_get_method_argument_count", "class", "method", "no_inheritance"), &ClassDB::class_get_method_argument_count, DEFVAL(false));
  1364. ::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::class_get_method_list, DEFVAL(false));
  1365. ::ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "class_call_static", &ClassDB::class_call_static, MethodInfo("class_call_static", PropertyInfo(Variant::STRING_NAME, "class"), PropertyInfo(Variant::STRING_NAME, "method")));
  1366. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::class_get_integer_constant_list, DEFVAL(false));
  1367. ::ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &ClassDB::class_has_integer_constant);
  1368. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &ClassDB::class_get_integer_constant);
  1369. ::ClassDB::bind_method(D_METHOD("class_has_enum", "class", "name", "no_inheritance"), &ClassDB::class_has_enum, DEFVAL(false));
  1370. ::ClassDB::bind_method(D_METHOD("class_get_enum_list", "class", "no_inheritance"), &ClassDB::class_get_enum_list, DEFVAL(false));
  1371. ::ClassDB::bind_method(D_METHOD("class_get_enum_constants", "class", "enum", "no_inheritance"), &ClassDB::class_get_enum_constants, DEFVAL(false));
  1372. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_enum", "class", "name", "no_inheritance"), &ClassDB::class_get_integer_constant_enum, DEFVAL(false));
  1373. ::ClassDB::bind_method(D_METHOD("is_class_enum_bitfield", "class", "enum", "no_inheritance"), &ClassDB::is_class_enum_bitfield, DEFVAL(false));
  1374. ::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);
  1375. BIND_ENUM_CONSTANT(API_CORE);
  1376. BIND_ENUM_CONSTANT(API_EDITOR);
  1377. BIND_ENUM_CONSTANT(API_EXTENSION);
  1378. BIND_ENUM_CONSTANT(API_EDITOR_EXTENSION);
  1379. BIND_ENUM_CONSTANT(API_NONE);
  1380. }
  1381. } // namespace special
  1382. ////// Engine //////
  1383. void Engine::set_physics_ticks_per_second(int p_ips) {
  1384. ::Engine::get_singleton()->set_physics_ticks_per_second(p_ips);
  1385. }
  1386. int Engine::get_physics_ticks_per_second() const {
  1387. return ::Engine::get_singleton()->get_physics_ticks_per_second();
  1388. }
  1389. void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) {
  1390. ::Engine::get_singleton()->set_max_physics_steps_per_frame(p_max_physics_steps);
  1391. }
  1392. int Engine::get_max_physics_steps_per_frame() const {
  1393. return ::Engine::get_singleton()->get_max_physics_steps_per_frame();
  1394. }
  1395. void Engine::set_physics_jitter_fix(double p_threshold) {
  1396. ::Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1397. }
  1398. double Engine::get_physics_jitter_fix() const {
  1399. return ::Engine::get_singleton()->get_physics_jitter_fix();
  1400. }
  1401. double Engine::get_physics_interpolation_fraction() const {
  1402. return ::Engine::get_singleton()->get_physics_interpolation_fraction();
  1403. }
  1404. void Engine::set_max_fps(int p_fps) {
  1405. ::Engine::get_singleton()->set_max_fps(p_fps);
  1406. }
  1407. int Engine::get_max_fps() const {
  1408. return ::Engine::get_singleton()->get_max_fps();
  1409. }
  1410. double Engine::get_frames_per_second() const {
  1411. return ::Engine::get_singleton()->get_frames_per_second();
  1412. }
  1413. uint64_t Engine::get_physics_frames() const {
  1414. return ::Engine::get_singleton()->get_physics_frames();
  1415. }
  1416. uint64_t Engine::get_process_frames() const {
  1417. return ::Engine::get_singleton()->get_process_frames();
  1418. }
  1419. void Engine::set_time_scale(double p_scale) {
  1420. ::Engine::get_singleton()->set_time_scale(p_scale);
  1421. }
  1422. double Engine::get_time_scale() {
  1423. return ::Engine::get_singleton()->get_time_scale();
  1424. }
  1425. int Engine::get_frames_drawn() {
  1426. return ::Engine::get_singleton()->get_frames_drawn();
  1427. }
  1428. MainLoop *Engine::get_main_loop() const {
  1429. // Needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1430. return ::OS::get_singleton()->get_main_loop();
  1431. }
  1432. Dictionary Engine::get_version_info() const {
  1433. return ::Engine::get_singleton()->get_version_info();
  1434. }
  1435. Dictionary Engine::get_author_info() const {
  1436. return ::Engine::get_singleton()->get_author_info();
  1437. }
  1438. TypedArray<Dictionary> Engine::get_copyright_info() const {
  1439. return ::Engine::get_singleton()->get_copyright_info();
  1440. }
  1441. Dictionary Engine::get_donor_info() const {
  1442. return ::Engine::get_singleton()->get_donor_info();
  1443. }
  1444. Dictionary Engine::get_license_info() const {
  1445. return ::Engine::get_singleton()->get_license_info();
  1446. }
  1447. String Engine::get_license_text() const {
  1448. return ::Engine::get_singleton()->get_license_text();
  1449. }
  1450. String Engine::get_architecture_name() const {
  1451. return ::Engine::get_singleton()->get_architecture_name();
  1452. }
  1453. bool Engine::is_in_physics_frame() const {
  1454. return ::Engine::get_singleton()->is_in_physics_frame();
  1455. }
  1456. bool Engine::has_singleton(const StringName &p_name) const {
  1457. return ::Engine::get_singleton()->has_singleton(p_name);
  1458. }
  1459. Object *Engine::get_singleton_object(const StringName &p_name) const {
  1460. return ::Engine::get_singleton()->get_singleton_object(p_name);
  1461. }
  1462. void Engine::register_singleton(const StringName &p_name, Object *p_object) {
  1463. ERR_FAIL_COND_MSG(has_singleton(p_name), vformat("Singleton already registered: '%s'.", String(p_name)));
  1464. ERR_FAIL_COND_MSG(!String(p_name).is_valid_ascii_identifier(), vformat("Singleton name is not a valid identifier: '%s'.", p_name));
  1465. ::Engine::Singleton s;
  1466. s.class_name = p_name;
  1467. s.name = p_name;
  1468. s.ptr = p_object;
  1469. s.user_created = true;
  1470. ::Engine::get_singleton()->add_singleton(s);
  1471. }
  1472. void Engine::unregister_singleton(const StringName &p_name) {
  1473. ERR_FAIL_COND_MSG(!has_singleton(p_name), vformat("Attempt to remove unregistered singleton: '%s'.", String(p_name)));
  1474. ERR_FAIL_COND_MSG(!::Engine::get_singleton()->is_singleton_user_created(p_name), vformat("Attempt to remove non-user created singleton: '%s'.", String(p_name)));
  1475. ::Engine::get_singleton()->remove_singleton(p_name);
  1476. }
  1477. Vector<String> Engine::get_singleton_list() const {
  1478. List<::Engine::Singleton> singletons;
  1479. ::Engine::get_singleton()->get_singletons(&singletons);
  1480. Vector<String> ret;
  1481. for (List<::Engine::Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  1482. ret.push_back(E->get().name);
  1483. }
  1484. return ret;
  1485. }
  1486. Error Engine::register_script_language(ScriptLanguage *p_language) {
  1487. return ScriptServer::register_language(p_language);
  1488. }
  1489. Error Engine::unregister_script_language(const ScriptLanguage *p_language) {
  1490. return ScriptServer::unregister_language(p_language);
  1491. }
  1492. int Engine::get_script_language_count() {
  1493. return ScriptServer::get_language_count();
  1494. }
  1495. ScriptLanguage *Engine::get_script_language(int p_index) const {
  1496. return ScriptServer::get_language(p_index);
  1497. }
  1498. void Engine::set_editor_hint(bool p_enabled) {
  1499. ::Engine::get_singleton()->set_editor_hint(p_enabled);
  1500. }
  1501. bool Engine::is_editor_hint() const {
  1502. return ::Engine::get_singleton()->is_editor_hint();
  1503. }
  1504. String Engine::get_write_movie_path() const {
  1505. return ::Engine::get_singleton()->get_write_movie_path();
  1506. }
  1507. void Engine::set_print_to_stdout(bool p_enabled) {
  1508. ::Engine::get_singleton()->set_print_to_stdout(p_enabled);
  1509. }
  1510. bool Engine::is_printing_to_stdout() const {
  1511. return ::Engine::get_singleton()->is_printing_to_stdout();
  1512. }
  1513. void Engine::set_print_error_messages(bool p_enabled) {
  1514. ::Engine::get_singleton()->set_print_error_messages(p_enabled);
  1515. }
  1516. bool Engine::is_printing_error_messages() const {
  1517. return ::Engine::get_singleton()->is_printing_error_messages();
  1518. }
  1519. #ifdef TOOLS_ENABLED
  1520. void Engine::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1521. const String pf = p_function;
  1522. if (p_idx == 0 && (pf == "has_singleton" || pf == "get_singleton" || pf == "unregister_singleton")) {
  1523. for (const String &E : get_singleton_list()) {
  1524. r_options->push_back(E.quote());
  1525. }
  1526. }
  1527. Object::get_argument_options(p_function, p_idx, r_options);
  1528. }
  1529. #endif
  1530. void Engine::_bind_methods() {
  1531. ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second);
  1532. ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second);
  1533. ClassDB::bind_method(D_METHOD("set_max_physics_steps_per_frame", "max_physics_steps"), &Engine::set_max_physics_steps_per_frame);
  1534. ClassDB::bind_method(D_METHOD("get_max_physics_steps_per_frame"), &Engine::get_max_physics_steps_per_frame);
  1535. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix);
  1536. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix);
  1537. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction);
  1538. ClassDB::bind_method(D_METHOD("set_max_fps", "max_fps"), &Engine::set_max_fps);
  1539. ClassDB::bind_method(D_METHOD("get_max_fps"), &Engine::get_max_fps);
  1540. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &Engine::set_time_scale);
  1541. ClassDB::bind_method(D_METHOD("get_time_scale"), &Engine::get_time_scale);
  1542. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &Engine::get_frames_drawn);
  1543. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &Engine::get_frames_per_second);
  1544. ClassDB::bind_method(D_METHOD("get_physics_frames"), &Engine::get_physics_frames);
  1545. ClassDB::bind_method(D_METHOD("get_process_frames"), &Engine::get_process_frames);
  1546. ClassDB::bind_method(D_METHOD("get_main_loop"), &Engine::get_main_loop);
  1547. ClassDB::bind_method(D_METHOD("get_version_info"), &Engine::get_version_info);
  1548. ClassDB::bind_method(D_METHOD("get_author_info"), &Engine::get_author_info);
  1549. ClassDB::bind_method(D_METHOD("get_copyright_info"), &Engine::get_copyright_info);
  1550. ClassDB::bind_method(D_METHOD("get_donor_info"), &Engine::get_donor_info);
  1551. ClassDB::bind_method(D_METHOD("get_license_info"), &Engine::get_license_info);
  1552. ClassDB::bind_method(D_METHOD("get_license_text"), &Engine::get_license_text);
  1553. ClassDB::bind_method(D_METHOD("get_architecture_name"), &Engine::get_architecture_name);
  1554. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &Engine::is_in_physics_frame);
  1555. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &Engine::has_singleton);
  1556. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &Engine::get_singleton_object);
  1557. ClassDB::bind_method(D_METHOD("register_singleton", "name", "instance"), &Engine::register_singleton);
  1558. ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton);
  1559. ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list);
  1560. ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language);
  1561. ClassDB::bind_method(D_METHOD("unregister_script_language", "language"), &Engine::unregister_script_language);
  1562. ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count);
  1563. ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language);
  1564. ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint);
  1565. ClassDB::bind_method(D_METHOD("get_write_movie_path"), &Engine::get_write_movie_path);
  1566. ClassDB::bind_method(D_METHOD("set_print_to_stdout", "enabled"), &Engine::set_print_to_stdout);
  1567. ClassDB::bind_method(D_METHOD("is_printing_to_stdout"), &Engine::is_printing_to_stdout);
  1568. ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages);
  1569. ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &Engine::is_printing_error_messages);
  1570. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
  1571. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_to_stdout"), "set_print_to_stdout", "is_printing_to_stdout");
  1572. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second");
  1573. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_physics_steps_per_frame"), "set_max_physics_steps_per_frame", "get_max_physics_steps_per_frame");
  1574. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_fps"), "set_max_fps", "get_max_fps");
  1575. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1576. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1577. }
  1578. Engine *Engine::singleton = nullptr;
  1579. ////// EngineDebugger //////
  1580. bool EngineDebugger::is_active() {
  1581. return ::EngineDebugger::is_active();
  1582. }
  1583. void EngineDebugger::register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler) {
  1584. ERR_FAIL_COND(p_profiler.is_null());
  1585. ERR_FAIL_COND_MSG(p_profiler->is_bound(), "Profiler already registered.");
  1586. ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), vformat("Profiler name already in use: '%s'.", p_name));
  1587. Error err = p_profiler->bind(p_name);
  1588. ERR_FAIL_COND_MSG(err != OK, vformat("Profiler failed to register with error: %d.", err));
  1589. profilers.insert(p_name, p_profiler);
  1590. }
  1591. void EngineDebugger::unregister_profiler(const StringName &p_name) {
  1592. ERR_FAIL_COND_MSG(!profilers.has(p_name), vformat("Profiler not registered: '%s'.", p_name));
  1593. profilers[p_name]->unbind();
  1594. profilers.erase(p_name);
  1595. }
  1596. bool EngineDebugger::is_profiling(const StringName &p_name) {
  1597. return ::EngineDebugger::is_profiling(p_name);
  1598. }
  1599. bool EngineDebugger::has_profiler(const StringName &p_name) {
  1600. return ::EngineDebugger::has_profiler(p_name);
  1601. }
  1602. void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  1603. ::EngineDebugger::profiler_add_frame_data(p_name, p_data);
  1604. }
  1605. void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  1606. if (::EngineDebugger::get_singleton()) {
  1607. ::EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
  1608. }
  1609. }
  1610. void EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1611. ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), vformat("Capture already registered: '%s'.", p_name));
  1612. captures.insert(p_name, p_callable);
  1613. Callable &c = captures[p_name];
  1614. ::EngineDebugger::Capture capture(&c, &EngineDebugger::call_capture);
  1615. ::EngineDebugger::register_message_capture(p_name, capture);
  1616. }
  1617. void EngineDebugger::unregister_message_capture(const StringName &p_name) {
  1618. ERR_FAIL_COND_MSG(!captures.has(p_name), vformat("Capture not registered: '%s'.", p_name));
  1619. ::EngineDebugger::unregister_message_capture(p_name);
  1620. captures.erase(p_name);
  1621. }
  1622. bool EngineDebugger::has_capture(const StringName &p_name) {
  1623. return ::EngineDebugger::has_capture(p_name);
  1624. }
  1625. void EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
  1626. ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send message. No active debugger");
  1627. ::EngineDebugger::get_singleton()->send_message(p_msg, p_data);
  1628. }
  1629. void EngineDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) {
  1630. ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send debug. No active debugger");
  1631. ::EngineDebugger::get_singleton()->debug(p_can_continue, p_is_error_breakpoint);
  1632. }
  1633. void EngineDebugger::script_debug(ScriptLanguage *p_lang, bool p_can_continue, bool p_is_error_breakpoint) {
  1634. ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't send debug. No active debugger");
  1635. ::EngineDebugger::get_script_debugger()->debug(p_lang, p_can_continue, p_is_error_breakpoint);
  1636. }
  1637. Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  1638. Callable &capture = *(Callable *)p_user;
  1639. if (!capture.is_valid()) {
  1640. return FAILED;
  1641. }
  1642. Variant cmd = p_cmd, data = p_data;
  1643. const Variant *args[2] = { &cmd, &data };
  1644. Variant retval;
  1645. Callable::CallError err;
  1646. capture.callp(args, 2, retval, err);
  1647. ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, FAILED, vformat("Error calling 'capture' to callable: %s.", Variant::get_callable_error_text(capture, args, 2, err)));
  1648. ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, vformat("Error calling 'capture' to callable: '%s'. Return type is not bool.", String(capture)));
  1649. r_captured = retval;
  1650. return OK;
  1651. }
  1652. void EngineDebugger::line_poll() {
  1653. ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't poll. No active debugger");
  1654. ::EngineDebugger::get_singleton()->line_poll();
  1655. }
  1656. void EngineDebugger::set_lines_left(int p_lines) {
  1657. ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't set lines left. No active debugger");
  1658. ::EngineDebugger::get_script_debugger()->set_lines_left(p_lines);
  1659. }
  1660. int EngineDebugger::get_lines_left() const {
  1661. ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), 0, "Can't get lines left. No active debugger");
  1662. return ::EngineDebugger::get_script_debugger()->get_lines_left();
  1663. }
  1664. void EngineDebugger::set_depth(int p_depth) {
  1665. ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't set depth. No active debugger");
  1666. ::EngineDebugger::get_script_debugger()->set_depth(p_depth);
  1667. }
  1668. int EngineDebugger::get_depth() const {
  1669. ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), 0, "Can't get depth. No active debugger");
  1670. return ::EngineDebugger::get_script_debugger()->get_depth();
  1671. }
  1672. bool EngineDebugger::is_breakpoint(int p_line, const StringName &p_source) const {
  1673. ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), false, "Can't check breakpoint. No active debugger");
  1674. return ::EngineDebugger::get_script_debugger()->is_breakpoint(p_line, p_source);
  1675. }
  1676. bool EngineDebugger::is_skipping_breakpoints() const {
  1677. ERR_FAIL_COND_V_MSG(!::EngineDebugger::get_script_debugger(), false, "Can't check skipping breakpoint. No active debugger");
  1678. return ::EngineDebugger::get_script_debugger()->is_skipping_breakpoints();
  1679. }
  1680. void EngineDebugger::insert_breakpoint(int p_line, const StringName &p_source) {
  1681. ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't insert breakpoint. No active debugger");
  1682. ::EngineDebugger::get_script_debugger()->insert_breakpoint(p_line, p_source);
  1683. }
  1684. void EngineDebugger::remove_breakpoint(int p_line, const StringName &p_source) {
  1685. ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't remove breakpoint. No active debugger");
  1686. ::EngineDebugger::get_script_debugger()->remove_breakpoint(p_line, p_source);
  1687. }
  1688. void EngineDebugger::clear_breakpoints() {
  1689. ERR_FAIL_COND_MSG(!::EngineDebugger::get_script_debugger(), "Can't clear breakpoints. No active debugger");
  1690. ::EngineDebugger::get_script_debugger()->clear_breakpoints();
  1691. }
  1692. EngineDebugger::~EngineDebugger() {
  1693. for (const KeyValue<StringName, Callable> &E : captures) {
  1694. ::EngineDebugger::unregister_message_capture(E.key);
  1695. }
  1696. captures.clear();
  1697. }
  1698. EngineDebugger *EngineDebugger::singleton = nullptr;
  1699. void EngineDebugger::_bind_methods() {
  1700. ClassDB::bind_method(D_METHOD("is_active"), &EngineDebugger::is_active);
  1701. ClassDB::bind_method(D_METHOD("register_profiler", "name", "profiler"), &EngineDebugger::register_profiler);
  1702. ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &EngineDebugger::unregister_profiler);
  1703. ClassDB::bind_method(D_METHOD("is_profiling", "name"), &EngineDebugger::is_profiling);
  1704. ClassDB::bind_method(D_METHOD("has_profiler", "name"), &EngineDebugger::has_profiler);
  1705. ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &EngineDebugger::profiler_add_frame_data);
  1706. ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &EngineDebugger::profiler_enable, DEFVAL(Array()));
  1707. ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &EngineDebugger::register_message_capture);
  1708. ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &EngineDebugger::unregister_message_capture);
  1709. ClassDB::bind_method(D_METHOD("has_capture", "name"), &EngineDebugger::has_capture);
  1710. ClassDB::bind_method(D_METHOD("line_poll"), &EngineDebugger::line_poll);
  1711. ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &EngineDebugger::send_message);
  1712. ClassDB::bind_method(D_METHOD("debug", "can_continue", "is_error_breakpoint"), &EngineDebugger::debug, DEFVAL(true), DEFVAL(false));
  1713. ClassDB::bind_method(D_METHOD("script_debug", "language", "can_continue", "is_error_breakpoint"), &EngineDebugger::script_debug, DEFVAL(true), DEFVAL(false));
  1714. ClassDB::bind_method(D_METHOD("set_lines_left", "lines"), &EngineDebugger::set_lines_left);
  1715. ClassDB::bind_method(D_METHOD("get_lines_left"), &EngineDebugger::get_lines_left);
  1716. ClassDB::bind_method(D_METHOD("set_depth", "depth"), &EngineDebugger::set_depth);
  1717. ClassDB::bind_method(D_METHOD("get_depth"), &EngineDebugger::get_depth);
  1718. ClassDB::bind_method(D_METHOD("is_breakpoint", "line", "source"), &EngineDebugger::is_breakpoint);
  1719. ClassDB::bind_method(D_METHOD("is_skipping_breakpoints"), &EngineDebugger::is_skipping_breakpoints);
  1720. ClassDB::bind_method(D_METHOD("insert_breakpoint", "line", "source"), &EngineDebugger::insert_breakpoint);
  1721. ClassDB::bind_method(D_METHOD("remove_breakpoint", "line", "source"), &EngineDebugger::remove_breakpoint);
  1722. ClassDB::bind_method(D_METHOD("clear_breakpoints"), &EngineDebugger::clear_breakpoints);
  1723. }
  1724. } // namespace core_bind