core_bind.cpp 80 KB

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