core_bind.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. /*************************************************************************/
  2. /* core_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "core_bind.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/crypto/crypto_core.h"
  33. #include "core/debugger/engine_debugger.h"
  34. #include "core/io/file_access_compressed.h"
  35. #include "core/io/file_access_encrypted.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/math/geometry_2d.h"
  38. #include "core/math/geometry_3d.h"
  39. #include "core/os/keyboard.h"
  40. #include "core/variant/typed_array.h"
  41. namespace core_bind {
  42. ////// ResourceLoader //////
  43. ResourceLoader *ResourceLoader::singleton = nullptr;
  44. Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, CacheMode p_cache_mode) {
  45. return ::ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads, ResourceFormatLoader::CacheMode(p_cache_mode));
  46. }
  47. ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
  48. float progress = 0;
  49. ::ResourceLoader::ThreadLoadStatus tls = ::ResourceLoader::load_threaded_get_status(p_path, &progress);
  50. r_progress.resize(1);
  51. r_progress[0] = progress;
  52. return (ThreadLoadStatus)tls;
  53. }
  54. Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path) {
  55. Error error;
  56. Ref<Resource> res = ::ResourceLoader::load_threaded_get(p_path, &error);
  57. return res;
  58. }
  59. Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, CacheMode p_cache_mode) {
  60. Error err = OK;
  61. Ref<Resource> ret = ::ResourceLoader::load(p_path, p_type_hint, ResourceFormatLoader::CacheMode(p_cache_mode), &err);
  62. ERR_FAIL_COND_V_MSG(err != OK, ret, "Error loading resource: '" + p_path + "'.");
  63. return ret;
  64. }
  65. Vector<String> ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
  66. List<String> exts;
  67. ::ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
  68. Vector<String> ret;
  69. for (const String &E : exts) {
  70. ret.push_back(E);
  71. }
  72. return ret;
  73. }
  74. void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) {
  75. ::ResourceLoader::add_resource_format_loader(p_format_loader, p_at_front);
  76. }
  77. void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) {
  78. ::ResourceLoader::remove_resource_format_loader(p_format_loader);
  79. }
  80. void ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  81. ::ResourceLoader::set_abort_on_missing_resources(p_abort);
  82. }
  83. PackedStringArray ResourceLoader::get_dependencies(const String &p_path) {
  84. List<String> deps;
  85. ::ResourceLoader::get_dependencies(p_path, &deps);
  86. PackedStringArray ret;
  87. for (const String &E : deps) {
  88. ret.push_back(E);
  89. }
  90. return ret;
  91. }
  92. bool ResourceLoader::has_cached(const String &p_path) {
  93. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  94. return ResourceCache::has(local_path);
  95. }
  96. bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  97. return ::ResourceLoader::exists(p_path, p_type_hint);
  98. }
  99. ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
  100. return ::ResourceLoader::get_resource_uid(p_path);
  101. }
  102. void ResourceLoader::_bind_methods() {
  103. 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));
  104. ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &ResourceLoader::load_threaded_get_status, DEFVAL(Array()));
  105. ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &ResourceLoader::load_threaded_get);
  106. ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
  107. ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &ResourceLoader::get_recognized_extensions_for_type);
  108. ClassDB::bind_method(D_METHOD("add_resource_format_loader", "format_loader", "at_front"), &ResourceLoader::add_resource_format_loader, DEFVAL(false));
  109. ClassDB::bind_method(D_METHOD("remove_resource_format_loader", "format_loader"), &ResourceLoader::remove_resource_format_loader);
  110. ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &ResourceLoader::set_abort_on_missing_resources);
  111. ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &ResourceLoader::get_dependencies);
  112. ClassDB::bind_method(D_METHOD("has_cached", "path"), &ResourceLoader::has_cached);
  113. ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &ResourceLoader::exists, DEFVAL(""));
  114. ClassDB::bind_method(D_METHOD("get_resource_uid", "path"), &ResourceLoader::get_resource_uid);
  115. BIND_ENUM_CONSTANT(THREAD_LOAD_INVALID_RESOURCE);
  116. BIND_ENUM_CONSTANT(THREAD_LOAD_IN_PROGRESS);
  117. BIND_ENUM_CONSTANT(THREAD_LOAD_FAILED);
  118. BIND_ENUM_CONSTANT(THREAD_LOAD_LOADED);
  119. BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE);
  120. BIND_ENUM_CONSTANT(CACHE_MODE_REUSE);
  121. BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);
  122. }
  123. ////// ResourceSaver //////
  124. Error ResourceSaver::save(const Ref<Resource> &p_resource, const String &p_path, BitField<SaverFlags> p_flags) {
  125. ERR_FAIL_COND_V_MSG(p_resource.is_null(), ERR_INVALID_PARAMETER, "Can't save empty resource to path '" + p_path + "'.");
  126. return ::ResourceSaver::save(p_resource, p_path, p_flags);
  127. }
  128. Vector<String> ResourceSaver::get_recognized_extensions(const Ref<Resource> &p_resource) {
  129. ERR_FAIL_COND_V_MSG(p_resource.is_null(), Vector<String>(), "It's not a reference to a valid Resource object.");
  130. List<String> exts;
  131. ::ResourceSaver::get_recognized_extensions(p_resource, &exts);
  132. Vector<String> ret;
  133. for (const String &E : exts) {
  134. ret.push_back(E);
  135. }
  136. return ret;
  137. }
  138. void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
  139. ::ResourceSaver::add_resource_format_saver(p_format_saver, p_at_front);
  140. }
  141. void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
  142. ::ResourceSaver::remove_resource_format_saver(p_format_saver);
  143. }
  144. ResourceSaver *ResourceSaver::singleton = nullptr;
  145. void ResourceSaver::_bind_methods() {
  146. ClassDB::bind_method(D_METHOD("save", "resource", "path", "flags"), &ResourceSaver::save, DEFVAL(""), DEFVAL((uint32_t)FLAG_NONE));
  147. ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &ResourceSaver::get_recognized_extensions);
  148. ClassDB::bind_method(D_METHOD("add_resource_format_saver", "format_saver", "at_front"), &ResourceSaver::add_resource_format_saver, DEFVAL(false));
  149. ClassDB::bind_method(D_METHOD("remove_resource_format_saver", "format_saver"), &ResourceSaver::remove_resource_format_saver);
  150. BIND_BITFIELD_FLAG(FLAG_NONE);
  151. BIND_BITFIELD_FLAG(FLAG_RELATIVE_PATHS);
  152. BIND_BITFIELD_FLAG(FLAG_BUNDLE_RESOURCES);
  153. BIND_BITFIELD_FLAG(FLAG_CHANGE_PATH);
  154. BIND_BITFIELD_FLAG(FLAG_OMIT_EDITOR_PROPERTIES);
  155. BIND_BITFIELD_FLAG(FLAG_SAVE_BIG_ENDIAN);
  156. BIND_BITFIELD_FLAG(FLAG_COMPRESS);
  157. BIND_BITFIELD_FLAG(FLAG_REPLACE_SUBRESOURCE_PATHS);
  158. }
  159. ////// OS //////
  160. PackedStringArray OS::get_connected_midi_inputs() {
  161. return ::OS::get_singleton()->get_connected_midi_inputs();
  162. }
  163. void OS::open_midi_inputs() {
  164. ::OS::get_singleton()->open_midi_inputs();
  165. }
  166. void OS::close_midi_inputs() {
  167. ::OS::get_singleton()->close_midi_inputs();
  168. }
  169. void OS::set_use_file_access_save_and_swap(bool p_enable) {
  170. FileAccess::set_backup_save(p_enable);
  171. }
  172. void OS::set_low_processor_usage_mode(bool p_enabled) {
  173. ::OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  174. }
  175. bool OS::is_in_low_processor_usage_mode() const {
  176. return ::OS::get_singleton()->is_in_low_processor_usage_mode();
  177. }
  178. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  179. ::OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(p_usec);
  180. }
  181. int OS::get_low_processor_usage_mode_sleep_usec() const {
  182. return ::OS::get_singleton()->get_low_processor_usage_mode_sleep_usec();
  183. }
  184. void OS::alert(const String &p_alert, const String &p_title) {
  185. ::OS::get_singleton()->alert(p_alert, p_title);
  186. }
  187. void OS::crash(const String &p_message) {
  188. CRASH_NOW_MSG(p_message);
  189. }
  190. Vector<String> OS::get_system_fonts() const {
  191. return ::OS::get_singleton()->get_system_fonts();
  192. }
  193. String OS::get_system_font_path(const String &p_font_name, bool p_bold, bool p_italic) const {
  194. return ::OS::get_singleton()->get_system_font_path(p_font_name, p_bold, p_italic);
  195. }
  196. String OS::get_executable_path() const {
  197. return ::OS::get_singleton()->get_executable_path();
  198. }
  199. Error OS::shell_open(String p_uri) {
  200. if (p_uri.begins_with("res://")) {
  201. 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()`.");
  202. } else if (p_uri.begins_with("user://")) {
  203. 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()`.");
  204. }
  205. return ::OS::get_singleton()->shell_open(p_uri);
  206. }
  207. String OS::read_string_from_stdin(bool p_block) {
  208. return ::OS::get_singleton()->get_stdin_string(true);
  209. }
  210. int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) {
  211. List<String> args;
  212. for (int i = 0; i < p_arguments.size(); i++) {
  213. args.push_back(p_arguments[i]);
  214. }
  215. String pipe;
  216. int exitcode = 0;
  217. Error err = ::OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr, nullptr, p_open_console);
  218. r_output.push_back(pipe);
  219. if (err != OK) {
  220. return -1;
  221. }
  222. return exitcode;
  223. }
  224. int OS::create_instance(const Vector<String> &p_arguments) {
  225. List<String> args;
  226. for (int i = 0; i < p_arguments.size(); i++) {
  227. args.push_back(p_arguments[i]);
  228. }
  229. ::OS::ProcessID pid = 0;
  230. Error err = ::OS::get_singleton()->create_instance(args, &pid);
  231. if (err != OK) {
  232. return -1;
  233. }
  234. return pid;
  235. }
  236. int OS::create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console) {
  237. List<String> args;
  238. for (int i = 0; i < p_arguments.size(); i++) {
  239. args.push_back(p_arguments[i]);
  240. }
  241. ::OS::ProcessID pid = 0;
  242. Error err = ::OS::get_singleton()->create_process(p_path, args, &pid, p_open_console);
  243. if (err != OK) {
  244. return -1;
  245. }
  246. return pid;
  247. }
  248. Error OS::kill(int p_pid) {
  249. return ::OS::get_singleton()->kill(p_pid);
  250. }
  251. bool OS::is_process_running(int p_pid) const {
  252. return ::OS::get_singleton()->is_process_running(p_pid);
  253. }
  254. int OS::get_process_id() const {
  255. return ::OS::get_singleton()->get_process_id();
  256. }
  257. bool OS::has_environment(const String &p_var) const {
  258. return ::OS::get_singleton()->has_environment(p_var);
  259. }
  260. String OS::get_environment(const String &p_var) const {
  261. return ::OS::get_singleton()->get_environment(p_var);
  262. }
  263. bool OS::set_environment(const String &p_var, const String &p_value) const {
  264. return ::OS::get_singleton()->set_environment(p_var, p_value);
  265. }
  266. String OS::get_name() const {
  267. return ::OS::get_singleton()->get_name();
  268. }
  269. String OS::get_distribution_name() const {
  270. return ::OS::get_singleton()->get_distribution_name();
  271. }
  272. String OS::get_version() const {
  273. return ::OS::get_singleton()->get_version();
  274. }
  275. Vector<String> OS::get_video_adapter_driver_info() const {
  276. return ::OS::get_singleton()->get_video_adapter_driver_info();
  277. }
  278. Vector<String> OS::get_cmdline_args() {
  279. List<String> cmdline = ::OS::get_singleton()->get_cmdline_args();
  280. Vector<String> cmdlinev;
  281. for (const String &E : cmdline) {
  282. cmdlinev.push_back(E);
  283. }
  284. return cmdlinev;
  285. }
  286. Vector<String> OS::get_cmdline_user_args() {
  287. List<String> cmdline = ::OS::get_singleton()->get_cmdline_user_args();
  288. Vector<String> cmdlinev;
  289. for (const String &E : cmdline) {
  290. cmdlinev.push_back(E);
  291. }
  292. return cmdlinev;
  293. }
  294. void OS::set_restart_on_exit(bool p_restart, const Vector<String> &p_restart_arguments) {
  295. List<String> args_list;
  296. for (const String &restart_argument : p_restart_arguments) {
  297. args_list.push_back(restart_argument);
  298. }
  299. ::OS::get_singleton()->set_restart_on_exit(p_restart, args_list);
  300. }
  301. bool OS::is_restart_on_exit_set() const {
  302. return ::OS::get_singleton()->is_restart_on_exit_set();
  303. }
  304. Vector<String> OS::get_restart_on_exit_arguments() const {
  305. List<String> args = ::OS::get_singleton()->get_restart_on_exit_arguments();
  306. Vector<String> args_vector;
  307. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  308. args_vector.push_back(E->get());
  309. }
  310. return args_vector;
  311. }
  312. String OS::get_locale() const {
  313. return ::OS::get_singleton()->get_locale();
  314. }
  315. String OS::get_locale_language() const {
  316. return ::OS::get_singleton()->get_locale_language();
  317. }
  318. String OS::get_model_name() const {
  319. return ::OS::get_singleton()->get_model_name();
  320. }
  321. Error OS::set_thread_name(const String &p_name) {
  322. return ::Thread::set_name(p_name);
  323. }
  324. ::Thread::ID OS::get_thread_caller_id() const {
  325. return ::Thread::get_caller_id();
  326. };
  327. ::Thread::ID OS::get_main_thread_id() const {
  328. return ::Thread::get_main_id();
  329. };
  330. bool OS::has_feature(const String &p_feature) const {
  331. return ::OS::get_singleton()->has_feature(p_feature);
  332. }
  333. uint64_t OS::get_static_memory_usage() const {
  334. return ::OS::get_singleton()->get_static_memory_usage();
  335. }
  336. uint64_t OS::get_static_memory_peak_usage() const {
  337. return ::OS::get_singleton()->get_static_memory_peak_usage();
  338. }
  339. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  340. void OS::delay_usec(int p_usec) const {
  341. ERR_FAIL_COND_MSG(
  342. p_usec < 0,
  343. vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
  344. ::OS::get_singleton()->delay_usec(p_usec);
  345. }
  346. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  347. void OS::delay_msec(int p_msec) const {
  348. ERR_FAIL_COND_MSG(
  349. p_msec < 0,
  350. vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
  351. ::OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  352. }
  353. bool OS::is_userfs_persistent() const {
  354. return ::OS::get_singleton()->is_userfs_persistent();
  355. }
  356. int OS::get_processor_count() const {
  357. return ::OS::get_singleton()->get_processor_count();
  358. }
  359. String OS::get_processor_name() const {
  360. return ::OS::get_singleton()->get_processor_name();
  361. }
  362. bool OS::is_stdout_verbose() const {
  363. return ::OS::get_singleton()->is_stdout_verbose();
  364. }
  365. Error OS::move_to_trash(const String &p_path) const {
  366. return ::OS::get_singleton()->move_to_trash(p_path);
  367. }
  368. String OS::get_user_data_dir() const {
  369. return ::OS::get_singleton()->get_user_data_dir();
  370. }
  371. String OS::get_config_dir() const {
  372. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  373. return ::OS::get_singleton()->get_config_path();
  374. }
  375. String OS::get_data_dir() const {
  376. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  377. return ::OS::get_singleton()->get_data_path();
  378. }
  379. String OS::get_cache_dir() const {
  380. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  381. return ::OS::get_singleton()->get_cache_path();
  382. }
  383. bool OS::is_debug_build() const {
  384. #ifdef DEBUG_ENABLED
  385. return true;
  386. #else
  387. return false;
  388. #endif
  389. }
  390. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  391. return ::OS::get_singleton()->get_system_dir(::OS::SystemDir(p_dir), p_shared_storage);
  392. }
  393. String OS::get_keycode_string(Key p_code) const {
  394. return ::keycode_get_string(p_code);
  395. }
  396. bool OS::is_keycode_unicode(char32_t p_unicode) const {
  397. return ::keycode_has_unicode((Key)p_unicode);
  398. }
  399. Key OS::find_keycode_from_string(const String &p_code) const {
  400. return find_keycode(p_code);
  401. }
  402. bool OS::request_permission(const String &p_name) {
  403. return ::OS::get_singleton()->request_permission(p_name);
  404. }
  405. bool OS::request_permissions() {
  406. return ::OS::get_singleton()->request_permissions();
  407. }
  408. Vector<String> OS::get_granted_permissions() const {
  409. return ::OS::get_singleton()->get_granted_permissions();
  410. }
  411. String OS::get_unique_id() const {
  412. return ::OS::get_singleton()->get_unique_id();
  413. }
  414. OS *OS::singleton = nullptr;
  415. void OS::_bind_methods() {
  416. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &OS::get_connected_midi_inputs);
  417. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &OS::open_midi_inputs);
  418. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &OS::close_midi_inputs);
  419. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &OS::alert, DEFVAL("Alert!"));
  420. ClassDB::bind_method(D_METHOD("crash", "message"), &OS::crash);
  421. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &OS::set_low_processor_usage_mode);
  422. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &OS::is_in_low_processor_usage_mode);
  423. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &OS::set_low_processor_usage_mode_sleep_usec);
  424. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &OS::get_low_processor_usage_mode_sleep_usec);
  425. ClassDB::bind_method(D_METHOD("get_processor_count"), &OS::get_processor_count);
  426. ClassDB::bind_method(D_METHOD("get_processor_name"), &OS::get_processor_name);
  427. ClassDB::bind_method(D_METHOD("get_system_fonts"), &OS::get_system_fonts);
  428. ClassDB::bind_method(D_METHOD("get_system_font_path", "font_name", "bold", "italic"), &OS::get_system_font_path, DEFVAL(false), DEFVAL(false));
  429. ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
  430. ClassDB::bind_method(D_METHOD("read_string_from_stdin", "block"), &OS::read_string_from_stdin, DEFVAL(true));
  431. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
  432. ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
  433. ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
  434. ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
  435. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &OS::shell_open);
  436. ClassDB::bind_method(D_METHOD("is_process_running", "pid"), &OS::is_process_running);
  437. ClassDB::bind_method(D_METHOD("get_process_id"), &OS::get_process_id);
  438. ClassDB::bind_method(D_METHOD("get_environment", "variable"), &OS::get_environment);
  439. ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &OS::set_environment);
  440. ClassDB::bind_method(D_METHOD("has_environment", "variable"), &OS::has_environment);
  441. ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name);
  442. ClassDB::bind_method(D_METHOD("get_distribution_name"), &OS::get_distribution_name);
  443. ClassDB::bind_method(D_METHOD("get_version"), &OS::get_version);
  444. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &OS::get_cmdline_args);
  445. ClassDB::bind_method(D_METHOD("get_cmdline_user_args"), &OS::get_cmdline_user_args);
  446. ClassDB::bind_method(D_METHOD("get_video_adapter_driver_info"), &OS::get_video_adapter_driver_info);
  447. ClassDB::bind_method(D_METHOD("set_restart_on_exit", "restart", "arguments"), &OS::set_restart_on_exit, DEFVAL(Vector<String>()));
  448. ClassDB::bind_method(D_METHOD("is_restart_on_exit_set"), &OS::is_restart_on_exit_set);
  449. ClassDB::bind_method(D_METHOD("get_restart_on_exit_arguments"), &OS::get_restart_on_exit_arguments);
  450. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &OS::delay_usec);
  451. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &OS::delay_msec);
  452. ClassDB::bind_method(D_METHOD("get_locale"), &OS::get_locale);
  453. ClassDB::bind_method(D_METHOD("get_locale_language"), &OS::get_locale_language);
  454. ClassDB::bind_method(D_METHOD("get_model_name"), &OS::get_model_name);
  455. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &OS::is_userfs_persistent);
  456. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &OS::is_stdout_verbose);
  457. ClassDB::bind_method(D_METHOD("is_debug_build"), &OS::is_debug_build);
  458. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &OS::get_static_memory_usage);
  459. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &OS::get_static_memory_peak_usage);
  460. ClassDB::bind_method(D_METHOD("move_to_trash", "path"), &OS::move_to_trash);
  461. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &OS::get_user_data_dir);
  462. ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &OS::get_system_dir, DEFVAL(true));
  463. ClassDB::bind_method(D_METHOD("get_config_dir"), &OS::get_config_dir);
  464. ClassDB::bind_method(D_METHOD("get_data_dir"), &OS::get_data_dir);
  465. ClassDB::bind_method(D_METHOD("get_cache_dir"), &OS::get_cache_dir);
  466. ClassDB::bind_method(D_METHOD("get_unique_id"), &OS::get_unique_id);
  467. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &OS::get_keycode_string);
  468. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &OS::is_keycode_unicode);
  469. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &OS::find_keycode_from_string);
  470. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &OS::set_use_file_access_save_and_swap);
  471. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &OS::set_thread_name);
  472. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &OS::get_thread_caller_id);
  473. ClassDB::bind_method(D_METHOD("get_main_thread_id"), &OS::get_main_thread_id);
  474. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &OS::has_feature);
  475. ClassDB::bind_method(D_METHOD("request_permission", "name"), &OS::request_permission);
  476. ClassDB::bind_method(D_METHOD("request_permissions"), &OS::request_permissions);
  477. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &OS::get_granted_permissions);
  478. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  479. 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");
  480. // Those default values need to be specified for the docs generator,
  481. // to avoid using values from the documentation writer's own OS instance.
  482. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  483. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  484. BIND_ENUM_CONSTANT(VIDEO_DRIVER_VULKAN);
  485. BIND_ENUM_CONSTANT(VIDEO_DRIVER_OPENGL_3);
  486. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  487. BIND_ENUM_CONSTANT(DAY_MONDAY);
  488. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  489. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  490. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  491. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  492. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  493. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  494. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  495. BIND_ENUM_CONSTANT(MONTH_MARCH);
  496. BIND_ENUM_CONSTANT(MONTH_APRIL);
  497. BIND_ENUM_CONSTANT(MONTH_MAY);
  498. BIND_ENUM_CONSTANT(MONTH_JUNE);
  499. BIND_ENUM_CONSTANT(MONTH_JULY);
  500. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  501. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  502. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  503. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  504. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  505. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  506. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  507. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  508. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  509. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  510. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  511. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  512. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  513. }
  514. ////// Geometry2D //////
  515. Geometry2D *Geometry2D::singleton = nullptr;
  516. Geometry2D *Geometry2D::get_singleton() {
  517. return singleton;
  518. }
  519. bool Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  520. return ::Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  521. }
  522. real_t Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  523. return ::Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  524. }
  525. 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) {
  526. Vector2 result;
  527. if (::Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  528. return result;
  529. } else {
  530. return Variant();
  531. }
  532. }
  533. 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) {
  534. Vector2 result;
  535. if (::Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  536. return result;
  537. } else {
  538. return Variant();
  539. }
  540. }
  541. Vector<Vector2> Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  542. Vector2 r1, r2;
  543. ::Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  544. Vector<Vector2> r = { r1, r2 };
  545. return r;
  546. }
  547. Vector2 Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  548. Vector2 s[2] = { p_a, p_b };
  549. return ::Geometry2D::get_closest_point_to_segment(p_point, s);
  550. }
  551. Vector2 Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  552. Vector2 s[2] = { p_a, p_b };
  553. return ::Geometry2D::get_closest_point_to_segment_uncapped(p_point, s);
  554. }
  555. bool Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  556. return ::Geometry2D::is_point_in_triangle(s, a, b, c);
  557. }
  558. bool Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  559. return ::Geometry2D::is_polygon_clockwise(p_polygon);
  560. }
  561. bool Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  562. return ::Geometry2D::is_point_in_polygon(p_point, p_polygon);
  563. }
  564. Vector<int> Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  565. return ::Geometry2D::triangulate_polygon(p_polygon);
  566. }
  567. Vector<int> Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
  568. return ::Geometry2D::triangulate_delaunay(p_points);
  569. }
  570. Vector<Point2> Geometry2D::convex_hull(const Vector<Point2> &p_points) {
  571. return ::Geometry2D::convex_hull(p_points);
  572. }
  573. TypedArray<PackedVector2Array> Geometry2D::decompose_polygon_in_convex(const Vector<Vector2> &p_polygon) {
  574. Vector<Vector<Point2>> decomp = ::Geometry2D::decompose_polygon_in_convex(p_polygon);
  575. TypedArray<PackedVector2Array> ret;
  576. for (int i = 0; i < decomp.size(); ++i) {
  577. ret.push_back(decomp[i]);
  578. }
  579. return ret;
  580. }
  581. TypedArray<PackedVector2Array> Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  582. Vector<Vector<Point2>> polys = ::Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
  583. Array ret;
  584. for (int i = 0; i < polys.size(); ++i) {
  585. ret.push_back(polys[i]);
  586. }
  587. return ret;
  588. }
  589. TypedArray<PackedVector2Array> Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  590. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
  591. TypedArray<PackedVector2Array> ret;
  592. for (int i = 0; i < polys.size(); ++i) {
  593. ret.push_back(polys[i]);
  594. }
  595. return ret;
  596. }
  597. TypedArray<PackedVector2Array> Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  598. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
  599. Array ret;
  600. for (int i = 0; i < polys.size(); ++i) {
  601. ret.push_back(polys[i]);
  602. }
  603. return ret;
  604. }
  605. TypedArray<PackedVector2Array> Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  606. Vector<Vector<Point2>> polys = ::Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b);
  607. Array ret;
  608. for (int i = 0; i < polys.size(); ++i) {
  609. ret.push_back(polys[i]);
  610. }
  611. return ret;
  612. }
  613. TypedArray<PackedVector2Array> Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  614. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
  615. Array ret;
  616. for (int i = 0; i < polys.size(); ++i) {
  617. ret.push_back(polys[i]);
  618. }
  619. return ret;
  620. }
  621. TypedArray<PackedVector2Array> Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  622. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon);
  623. Array ret;
  624. for (int i = 0; i < polys.size(); ++i) {
  625. ret.push_back(polys[i]);
  626. }
  627. return ret;
  628. }
  629. TypedArray<PackedVector2Array> Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  630. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polygon(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type));
  631. Array ret;
  632. for (int i = 0; i < polys.size(); ++i) {
  633. ret.push_back(polys[i]);
  634. }
  635. return ret;
  636. }
  637. TypedArray<PackedVector2Array> Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  638. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polyline(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type), ::Geometry2D::PolyEndType(p_end_type));
  639. Array ret;
  640. for (int i = 0; i < polys.size(); ++i) {
  641. ret.push_back(polys[i]);
  642. }
  643. return ret;
  644. }
  645. Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
  646. Dictionary ret;
  647. Vector<Size2i> rects;
  648. for (int i = 0; i < p_rects.size(); i++) {
  649. rects.push_back(p_rects[i]);
  650. }
  651. Vector<Point2i> result;
  652. Size2i size;
  653. ::Geometry2D::make_atlas(rects, result, size);
  654. Vector<Point2> r_result;
  655. for (int i = 0; i < result.size(); i++) {
  656. r_result.push_back(result[i]);
  657. }
  658. ret["points"] = r_result;
  659. ret["size"] = size;
  660. return ret;
  661. }
  662. void Geometry2D::_bind_methods() {
  663. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &Geometry2D::is_point_in_circle);
  664. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &Geometry2D::segment_intersects_circle);
  665. ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &Geometry2D::segment_intersects_segment);
  666. ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &Geometry2D::line_intersects_line);
  667. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &Geometry2D::get_closest_points_between_segments);
  668. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment);
  669. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment_uncapped);
  670. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &Geometry2D::point_is_inside_triangle);
  671. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &Geometry2D::is_polygon_clockwise);
  672. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &Geometry2D::is_point_in_polygon);
  673. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &Geometry2D::triangulate_polygon);
  674. ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &Geometry2D::triangulate_delaunay);
  675. ClassDB::bind_method(D_METHOD("convex_hull", "points"), &Geometry2D::convex_hull);
  676. ClassDB::bind_method(D_METHOD("decompose_polygon_in_convex", "polygon"), &Geometry2D::decompose_polygon_in_convex);
  677. ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &Geometry2D::merge_polygons);
  678. ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &Geometry2D::clip_polygons);
  679. ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &Geometry2D::intersect_polygons);
  680. ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &Geometry2D::exclude_polygons);
  681. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::clip_polyline_with_polygon);
  682. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::intersect_polyline_with_polygon);
  683. ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
  684. ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  685. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &Geometry2D::make_atlas);
  686. BIND_ENUM_CONSTANT(OPERATION_UNION);
  687. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  688. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  689. BIND_ENUM_CONSTANT(OPERATION_XOR);
  690. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  691. BIND_ENUM_CONSTANT(JOIN_ROUND);
  692. BIND_ENUM_CONSTANT(JOIN_MITER);
  693. BIND_ENUM_CONSTANT(END_POLYGON);
  694. BIND_ENUM_CONSTANT(END_JOINED);
  695. BIND_ENUM_CONSTANT(END_BUTT);
  696. BIND_ENUM_CONSTANT(END_SQUARE);
  697. BIND_ENUM_CONSTANT(END_ROUND);
  698. }
  699. ////// Geometry3D //////
  700. Geometry3D *Geometry3D::singleton = nullptr;
  701. Geometry3D *Geometry3D::get_singleton() {
  702. return singleton;
  703. }
  704. TypedArray<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
  705. Variant ret = ::Geometry3D::build_box_planes(p_extents);
  706. return ret;
  707. }
  708. TypedArray<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  709. Variant ret = ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  710. return ret;
  711. }
  712. TypedArray<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  713. Variant ret = ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  714. return ret;
  715. }
  716. Vector<Vector3> Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  717. Vector3 r1, r2;
  718. ::Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  719. Vector<Vector3> r = { r1, r2 };
  720. return r;
  721. }
  722. Vector3 Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  723. Vector3 s[2] = { p_a, p_b };
  724. return ::Geometry3D::get_closest_point_to_segment(p_point, s);
  725. }
  726. Vector3 Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  727. Vector3 s[2] = { p_a, p_b };
  728. return ::Geometry3D::get_closest_point_to_segment_uncapped(p_point, s);
  729. }
  730. 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) {
  731. Vector3 res;
  732. if (::Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  733. return res;
  734. } else {
  735. return Variant();
  736. }
  737. }
  738. 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) {
  739. Vector3 res;
  740. if (::Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  741. return res;
  742. } else {
  743. return Variant();
  744. }
  745. }
  746. Vector<Vector3> Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  747. Vector<Vector3> r;
  748. Vector3 res, norm;
  749. if (!::Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  750. return r;
  751. }
  752. r.resize(2);
  753. r.set(0, res);
  754. r.set(1, norm);
  755. return r;
  756. }
  757. Vector<Vector3> Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  758. Vector<Vector3> r;
  759. Vector3 res, norm;
  760. if (!::Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  761. return r;
  762. }
  763. r.resize(2);
  764. r.set(0, res);
  765. r.set(1, norm);
  766. return r;
  767. }
  768. Vector<Vector3> Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  769. Vector<Vector3> r;
  770. Vector3 res, norm;
  771. if (!::Geometry3D::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm)) {
  772. return r;
  773. }
  774. r.resize(2);
  775. r.set(0, res);
  776. r.set(1, norm);
  777. return r;
  778. }
  779. Vector<Vector3> Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  780. return ::Geometry3D::clip_polygon(p_points, p_plane);
  781. }
  782. void Geometry3D::_bind_methods() {
  783. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &Geometry3D::build_box_planes);
  784. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  785. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  786. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &Geometry3D::get_closest_points_between_segments);
  787. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment);
  788. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment_uncapped);
  789. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &Geometry3D::ray_intersects_triangle);
  790. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &Geometry3D::segment_intersects_triangle);
  791. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &Geometry3D::segment_intersects_sphere);
  792. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &Geometry3D::segment_intersects_cylinder);
  793. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &Geometry3D::segment_intersects_convex);
  794. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &Geometry3D::clip_polygon);
  795. }
  796. ////// Marshalls //////
  797. Marshalls *Marshalls::singleton = nullptr;
  798. Marshalls *Marshalls::get_singleton() {
  799. return singleton;
  800. }
  801. String Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  802. int len;
  803. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  804. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  805. Vector<uint8_t> buff;
  806. buff.resize(len);
  807. uint8_t *w = buff.ptrw();
  808. err = encode_variant(p_var, &w[0], len, p_full_objects);
  809. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  810. String ret = CryptoCore::b64_encode_str(&w[0], len);
  811. ERR_FAIL_COND_V(ret.is_empty(), ret);
  812. return ret;
  813. }
  814. Variant Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  815. int strlen = p_str.length();
  816. CharString cstr = p_str.ascii();
  817. Vector<uint8_t> buf;
  818. buf.resize(strlen / 4 * 3 + 1);
  819. uint8_t *w = buf.ptrw();
  820. size_t len = 0;
  821. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  822. Variant v;
  823. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  824. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  825. return v;
  826. }
  827. String Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  828. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  829. ERR_FAIL_COND_V(ret.is_empty(), ret);
  830. return ret;
  831. }
  832. Vector<uint8_t> Marshalls::base64_to_raw(const String &p_str) {
  833. int strlen = p_str.length();
  834. CharString cstr = p_str.ascii();
  835. size_t arr_len = 0;
  836. Vector<uint8_t> buf;
  837. {
  838. buf.resize(strlen / 4 * 3 + 1);
  839. uint8_t *w = buf.ptrw();
  840. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  841. }
  842. buf.resize(arr_len);
  843. return buf;
  844. }
  845. String Marshalls::utf8_to_base64(const String &p_str) {
  846. CharString cstr = p_str.utf8();
  847. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  848. ERR_FAIL_COND_V(ret.is_empty(), ret);
  849. return ret;
  850. }
  851. String Marshalls::base64_to_utf8(const String &p_str) {
  852. int strlen = p_str.length();
  853. CharString cstr = p_str.ascii();
  854. Vector<uint8_t> buf;
  855. buf.resize(strlen / 4 * 3 + 1 + 1);
  856. uint8_t *w = buf.ptrw();
  857. size_t len = 0;
  858. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  859. w[len] = 0;
  860. String ret = String::utf8((char *)&w[0]);
  861. return ret;
  862. }
  863. void Marshalls::_bind_methods() {
  864. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &Marshalls::variant_to_base64, DEFVAL(false));
  865. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &Marshalls::base64_to_variant, DEFVAL(false));
  866. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &Marshalls::raw_to_base64);
  867. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &Marshalls::base64_to_raw);
  868. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &Marshalls::utf8_to_base64);
  869. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &Marshalls::base64_to_utf8);
  870. }
  871. ////// Semaphore //////
  872. void Semaphore::wait() {
  873. semaphore.wait();
  874. }
  875. Error Semaphore::try_wait() {
  876. return semaphore.try_wait() ? OK : ERR_BUSY;
  877. }
  878. void Semaphore::post() {
  879. semaphore.post();
  880. }
  881. void Semaphore::_bind_methods() {
  882. ClassDB::bind_method(D_METHOD("wait"), &Semaphore::wait);
  883. ClassDB::bind_method(D_METHOD("try_wait"), &Semaphore::try_wait);
  884. ClassDB::bind_method(D_METHOD("post"), &Semaphore::post);
  885. }
  886. ////// Mutex //////
  887. void Mutex::lock() {
  888. mutex.lock();
  889. }
  890. Error Mutex::try_lock() {
  891. return mutex.try_lock();
  892. }
  893. void Mutex::unlock() {
  894. mutex.unlock();
  895. }
  896. void Mutex::_bind_methods() {
  897. ClassDB::bind_method(D_METHOD("lock"), &Mutex::lock);
  898. ClassDB::bind_method(D_METHOD("try_lock"), &Mutex::try_lock);
  899. ClassDB::bind_method(D_METHOD("unlock"), &Mutex::unlock);
  900. }
  901. ////// Thread //////
  902. void Thread::_start_func(void *ud) {
  903. Ref<Thread> *tud = (Ref<Thread> *)ud;
  904. Ref<Thread> t = *tud;
  905. memdelete(tud);
  906. Object *target_instance = t->target_callable.get_object();
  907. if (!target_instance) {
  908. t->running.clear();
  909. 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()));
  910. }
  911. String func_name = t->target_callable.is_custom() ? t->target_callable.get_custom()->get_as_text() : String(t->target_callable.get_method());
  912. ::Thread::set_name(func_name);
  913. Callable::CallError ce;
  914. t->target_callable.callp(nullptr, 0, t->ret, ce);
  915. if (ce.error != Callable::CallError::CALL_OK) {
  916. t->running.clear();
  917. 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) + ".");
  918. }
  919. t->running.clear();
  920. }
  921. Error Thread::start(const Callable &p_callable, Priority p_priority) {
  922. ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
  923. ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
  924. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  925. ret = Variant();
  926. target_callable = p_callable;
  927. running.set();
  928. Ref<Thread> *ud = memnew(Ref<Thread>(this));
  929. ::Thread::Settings s;
  930. s.priority = (::Thread::Priority)p_priority;
  931. thread.start(_start_func, ud, s);
  932. return OK;
  933. }
  934. String Thread::get_id() const {
  935. return itos(thread.get_id());
  936. }
  937. bool Thread::is_started() const {
  938. return thread.is_started();
  939. }
  940. bool Thread::is_alive() const {
  941. return running.is_set();
  942. }
  943. Variant Thread::wait_to_finish() {
  944. ERR_FAIL_COND_V_MSG(!is_started(), Variant(), "Thread must have been started to wait for its completion.");
  945. thread.wait_to_finish();
  946. Variant r = ret;
  947. target_callable = Callable();
  948. return r;
  949. }
  950. void Thread::_bind_methods() {
  951. ClassDB::bind_method(D_METHOD("start", "callable", "priority"), &Thread::start, DEFVAL(PRIORITY_NORMAL));
  952. ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
  953. ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
  954. ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive);
  955. ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish);
  956. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  957. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  958. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  959. }
  960. namespace special {
  961. ////// ClassDB //////
  962. PackedStringArray ClassDB::get_class_list() const {
  963. List<StringName> classes;
  964. ::ClassDB::get_class_list(&classes);
  965. PackedStringArray ret;
  966. ret.resize(classes.size());
  967. int idx = 0;
  968. for (const StringName &E : classes) {
  969. ret.set(idx++, E);
  970. }
  971. return ret;
  972. }
  973. PackedStringArray ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  974. List<StringName> classes;
  975. ::ClassDB::get_inheriters_from_class(p_class, &classes);
  976. PackedStringArray ret;
  977. ret.resize(classes.size());
  978. int idx = 0;
  979. for (const StringName &E : classes) {
  980. ret.set(idx++, E);
  981. }
  982. return ret;
  983. }
  984. StringName ClassDB::get_parent_class(const StringName &p_class) const {
  985. return ::ClassDB::get_parent_class(p_class);
  986. }
  987. bool ClassDB::class_exists(const StringName &p_class) const {
  988. return ::ClassDB::class_exists(p_class);
  989. }
  990. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  991. return ::ClassDB::is_parent_class(p_class, p_inherits);
  992. }
  993. bool ClassDB::can_instantiate(const StringName &p_class) const {
  994. return ::ClassDB::can_instantiate(p_class);
  995. }
  996. Variant ClassDB::instantiate(const StringName &p_class) const {
  997. Object *obj = ::ClassDB::instantiate(p_class);
  998. if (!obj) {
  999. return Variant();
  1000. }
  1001. RefCounted *r = Object::cast_to<RefCounted>(obj);
  1002. if (r) {
  1003. return Ref<RefCounted>(r);
  1004. } else {
  1005. return obj;
  1006. }
  1007. }
  1008. bool ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  1009. return ::ClassDB::has_signal(p_class, p_signal);
  1010. }
  1011. Dictionary ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  1012. MethodInfo signal;
  1013. if (::ClassDB::get_signal(p_class, p_signal, &signal)) {
  1014. return signal.operator Dictionary();
  1015. } else {
  1016. return Dictionary();
  1017. }
  1018. }
  1019. TypedArray<Dictionary> ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  1020. List<MethodInfo> signals;
  1021. ::ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1022. TypedArray<Dictionary> ret;
  1023. for (const MethodInfo &E : signals) {
  1024. ret.push_back(E.operator Dictionary());
  1025. }
  1026. return ret;
  1027. }
  1028. TypedArray<Dictionary> ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  1029. List<PropertyInfo> plist;
  1030. ::ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1031. TypedArray<Dictionary> ret;
  1032. for (const PropertyInfo &E : plist) {
  1033. ret.push_back(E.operator Dictionary());
  1034. }
  1035. return ret;
  1036. }
  1037. Variant ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  1038. Variant ret;
  1039. ::ClassDB::get_property(p_object, p_property, ret);
  1040. return ret;
  1041. }
  1042. Error ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1043. Variant ret;
  1044. bool valid;
  1045. if (!::ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1046. return ERR_UNAVAILABLE;
  1047. } else if (!valid) {
  1048. return ERR_INVALID_DATA;
  1049. }
  1050. return OK;
  1051. }
  1052. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  1053. return ::ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1054. }
  1055. TypedArray<Dictionary> ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  1056. List<MethodInfo> methods;
  1057. ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1058. TypedArray<Dictionary> ret;
  1059. for (const MethodInfo &E : methods) {
  1060. #ifdef DEBUG_METHODS_ENABLED
  1061. ret.push_back(E.operator Dictionary());
  1062. #else
  1063. Dictionary dict;
  1064. dict["name"] = E.name;
  1065. ret.push_back(dict);
  1066. #endif
  1067. }
  1068. return ret;
  1069. }
  1070. PackedStringArray ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1071. List<String> constants;
  1072. ::ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1073. PackedStringArray ret;
  1074. ret.resize(constants.size());
  1075. int idx = 0;
  1076. for (const String &E : constants) {
  1077. ret.set(idx++, E);
  1078. }
  1079. return ret;
  1080. }
  1081. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1082. bool success;
  1083. ::ClassDB::get_integer_constant(p_class, p_name, &success);
  1084. return success;
  1085. }
  1086. int64_t ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1087. bool found;
  1088. int64_t c = ::ClassDB::get_integer_constant(p_class, p_name, &found);
  1089. ERR_FAIL_COND_V(!found, 0);
  1090. return c;
  1091. }
  1092. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1093. return ::ClassDB::has_enum(p_class, p_name, p_no_inheritance);
  1094. }
  1095. PackedStringArray ClassDB::get_enum_list(const StringName &p_class, bool p_no_inheritance) const {
  1096. List<StringName> enums;
  1097. ::ClassDB::get_enum_list(p_class, &enums, p_no_inheritance);
  1098. PackedStringArray ret;
  1099. ret.resize(enums.size());
  1100. int idx = 0;
  1101. for (const StringName &E : enums) {
  1102. ret.set(idx++, E);
  1103. }
  1104. return ret;
  1105. }
  1106. PackedStringArray ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
  1107. List<StringName> constants;
  1108. ::ClassDB::get_enum_constants(p_class, p_enum, &constants, p_no_inheritance);
  1109. PackedStringArray ret;
  1110. ret.resize(constants.size());
  1111. int idx = 0;
  1112. for (const StringName &E : constants) {
  1113. ret.set(idx++, E);
  1114. }
  1115. return ret;
  1116. }
  1117. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1118. return ::ClassDB::get_integer_constant_enum(p_class, p_name, p_no_inheritance);
  1119. }
  1120. bool ClassDB::is_class_enabled(StringName p_class) const {
  1121. return ::ClassDB::is_class_enabled(p_class);
  1122. }
  1123. void ClassDB::_bind_methods() {
  1124. ::ClassDB::bind_method(D_METHOD("get_class_list"), &ClassDB::get_class_list);
  1125. ::ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &ClassDB::get_inheriters_from_class);
  1126. ::ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &ClassDB::get_parent_class);
  1127. ::ClassDB::bind_method(D_METHOD("class_exists", "class"), &ClassDB::class_exists);
  1128. ::ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &ClassDB::is_parent_class);
  1129. ::ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &ClassDB::can_instantiate);
  1130. ::ClassDB::bind_method(D_METHOD("instantiate", "class"), &ClassDB::instantiate);
  1131. ::ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &ClassDB::has_signal);
  1132. ::ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &ClassDB::get_signal);
  1133. ::ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &ClassDB::get_signal_list, DEFVAL(false));
  1134. ::ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &ClassDB::get_property_list, DEFVAL(false));
  1135. ::ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &ClassDB::get_property);
  1136. ::ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &ClassDB::set_property);
  1137. ::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::has_method, DEFVAL(false));
  1138. ::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::get_method_list, DEFVAL(false));
  1139. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::get_integer_constant_list, DEFVAL(false));
  1140. ::ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &ClassDB::has_integer_constant);
  1141. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &ClassDB::get_integer_constant);
  1142. ::ClassDB::bind_method(D_METHOD("class_has_enum", "class", "name", "no_inheritance"), &ClassDB::has_enum, DEFVAL(false));
  1143. ::ClassDB::bind_method(D_METHOD("class_get_enum_list", "class", "no_inheritance"), &ClassDB::get_enum_list, DEFVAL(false));
  1144. ::ClassDB::bind_method(D_METHOD("class_get_enum_constants", "class", "enum", "no_inheritance"), &ClassDB::get_enum_constants, DEFVAL(false));
  1145. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_enum", "class", "name", "no_inheritance"), &ClassDB::get_integer_constant_enum, DEFVAL(false));
  1146. ::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);
  1147. }
  1148. } // namespace special
  1149. ////// Engine //////
  1150. void Engine::set_physics_ticks_per_second(int p_ips) {
  1151. ::Engine::get_singleton()->set_physics_ticks_per_second(p_ips);
  1152. }
  1153. int Engine::get_physics_ticks_per_second() const {
  1154. return ::Engine::get_singleton()->get_physics_ticks_per_second();
  1155. }
  1156. void Engine::set_physics_jitter_fix(double p_threshold) {
  1157. ::Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1158. }
  1159. double Engine::get_physics_jitter_fix() const {
  1160. return ::Engine::get_singleton()->get_physics_jitter_fix();
  1161. }
  1162. double Engine::get_physics_interpolation_fraction() const {
  1163. return ::Engine::get_singleton()->get_physics_interpolation_fraction();
  1164. }
  1165. void Engine::set_max_fps(int p_fps) {
  1166. ::Engine::get_singleton()->set_max_fps(p_fps);
  1167. }
  1168. int Engine::get_max_fps() const {
  1169. return ::Engine::get_singleton()->get_max_fps();
  1170. }
  1171. double Engine::get_frames_per_second() const {
  1172. return ::Engine::get_singleton()->get_frames_per_second();
  1173. }
  1174. uint64_t Engine::get_physics_frames() const {
  1175. return ::Engine::get_singleton()->get_physics_frames();
  1176. }
  1177. uint64_t Engine::get_process_frames() const {
  1178. return ::Engine::get_singleton()->get_process_frames();
  1179. }
  1180. void Engine::set_time_scale(double p_scale) {
  1181. ::Engine::get_singleton()->set_time_scale(p_scale);
  1182. }
  1183. double Engine::get_time_scale() {
  1184. return ::Engine::get_singleton()->get_time_scale();
  1185. }
  1186. int Engine::get_frames_drawn() {
  1187. return ::Engine::get_singleton()->get_frames_drawn();
  1188. }
  1189. MainLoop *Engine::get_main_loop() const {
  1190. // Needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1191. return ::OS::get_singleton()->get_main_loop();
  1192. }
  1193. Dictionary Engine::get_version_info() const {
  1194. return ::Engine::get_singleton()->get_version_info();
  1195. }
  1196. Dictionary Engine::get_author_info() const {
  1197. return ::Engine::get_singleton()->get_author_info();
  1198. }
  1199. TypedArray<Dictionary> Engine::get_copyright_info() const {
  1200. return ::Engine::get_singleton()->get_copyright_info();
  1201. }
  1202. Dictionary Engine::get_donor_info() const {
  1203. return ::Engine::get_singleton()->get_donor_info();
  1204. }
  1205. Dictionary Engine::get_license_info() const {
  1206. return ::Engine::get_singleton()->get_license_info();
  1207. }
  1208. String Engine::get_license_text() const {
  1209. return ::Engine::get_singleton()->get_license_text();
  1210. }
  1211. String Engine::get_architecture_name() const {
  1212. return ::Engine::get_singleton()->get_architecture_name();
  1213. }
  1214. bool Engine::is_in_physics_frame() const {
  1215. return ::Engine::get_singleton()->is_in_physics_frame();
  1216. }
  1217. bool Engine::has_singleton(const StringName &p_name) const {
  1218. return ::Engine::get_singleton()->has_singleton(p_name);
  1219. }
  1220. Object *Engine::get_singleton_object(const StringName &p_name) const {
  1221. return ::Engine::get_singleton()->get_singleton_object(p_name);
  1222. }
  1223. void Engine::register_singleton(const StringName &p_name, Object *p_object) {
  1224. ERR_FAIL_COND_MSG(has_singleton(p_name), "Singleton already registered: " + String(p_name));
  1225. ERR_FAIL_COND_MSG(!String(p_name).is_valid_identifier(), "Singleton name is not a valid identifier: " + p_name);
  1226. ::Engine::Singleton s;
  1227. s.class_name = p_name;
  1228. s.name = p_name;
  1229. s.ptr = p_object;
  1230. s.user_created = true;
  1231. ::Engine::get_singleton()->add_singleton(s);
  1232. }
  1233. void Engine::unregister_singleton(const StringName &p_name) {
  1234. ERR_FAIL_COND_MSG(!has_singleton(p_name), "Attempt to remove unregistered singleton: " + String(p_name));
  1235. ERR_FAIL_COND_MSG(!::Engine::get_singleton()->is_singleton_user_created(p_name), "Attempt to remove non-user created singleton: " + String(p_name));
  1236. ::Engine::get_singleton()->remove_singleton(p_name);
  1237. }
  1238. Vector<String> Engine::get_singleton_list() const {
  1239. List<::Engine::Singleton> singletons;
  1240. ::Engine::get_singleton()->get_singletons(&singletons);
  1241. Vector<String> ret;
  1242. for (List<::Engine::Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  1243. ret.push_back(E->get().name);
  1244. }
  1245. return ret;
  1246. }
  1247. void Engine::register_script_language(ScriptLanguage *p_language) {
  1248. ScriptServer::register_language(p_language);
  1249. }
  1250. int Engine::get_script_language_count() {
  1251. return ScriptServer::get_language_count();
  1252. }
  1253. ScriptLanguage *Engine::get_script_language(int p_index) const {
  1254. return ScriptServer::get_language(p_index);
  1255. }
  1256. void Engine::set_editor_hint(bool p_enabled) {
  1257. ::Engine::get_singleton()->set_editor_hint(p_enabled);
  1258. }
  1259. bool Engine::is_editor_hint() const {
  1260. return ::Engine::get_singleton()->is_editor_hint();
  1261. }
  1262. String Engine::get_write_movie_path() const {
  1263. return ::Engine::get_singleton()->get_write_movie_path();
  1264. }
  1265. void Engine::set_print_error_messages(bool p_enabled) {
  1266. ::Engine::get_singleton()->set_print_error_messages(p_enabled);
  1267. }
  1268. bool Engine::is_printing_error_messages() const {
  1269. return ::Engine::get_singleton()->is_printing_error_messages();
  1270. }
  1271. void Engine::_bind_methods() {
  1272. ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second);
  1273. ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second);
  1274. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix);
  1275. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix);
  1276. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction);
  1277. ClassDB::bind_method(D_METHOD("set_max_fps", "max_fps"), &Engine::set_max_fps);
  1278. ClassDB::bind_method(D_METHOD("get_max_fps"), &Engine::get_max_fps);
  1279. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &Engine::set_time_scale);
  1280. ClassDB::bind_method(D_METHOD("get_time_scale"), &Engine::get_time_scale);
  1281. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &Engine::get_frames_drawn);
  1282. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &Engine::get_frames_per_second);
  1283. ClassDB::bind_method(D_METHOD("get_physics_frames"), &Engine::get_physics_frames);
  1284. ClassDB::bind_method(D_METHOD("get_process_frames"), &Engine::get_process_frames);
  1285. ClassDB::bind_method(D_METHOD("get_main_loop"), &Engine::get_main_loop);
  1286. ClassDB::bind_method(D_METHOD("get_version_info"), &Engine::get_version_info);
  1287. ClassDB::bind_method(D_METHOD("get_author_info"), &Engine::get_author_info);
  1288. ClassDB::bind_method(D_METHOD("get_copyright_info"), &Engine::get_copyright_info);
  1289. ClassDB::bind_method(D_METHOD("get_donor_info"), &Engine::get_donor_info);
  1290. ClassDB::bind_method(D_METHOD("get_license_info"), &Engine::get_license_info);
  1291. ClassDB::bind_method(D_METHOD("get_license_text"), &Engine::get_license_text);
  1292. ClassDB::bind_method(D_METHOD("get_architecture_name"), &Engine::get_architecture_name);
  1293. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &Engine::is_in_physics_frame);
  1294. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &Engine::has_singleton);
  1295. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &Engine::get_singleton_object);
  1296. ClassDB::bind_method(D_METHOD("register_singleton", "name", "instance"), &Engine::register_singleton);
  1297. ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton);
  1298. ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list);
  1299. ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language);
  1300. ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count);
  1301. ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language);
  1302. ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint);
  1303. ClassDB::bind_method(D_METHOD("get_write_movie_path"), &Engine::get_write_movie_path);
  1304. ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages);
  1305. ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &Engine::is_printing_error_messages);
  1306. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
  1307. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second");
  1308. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_fps"), "set_max_fps", "get_max_fps");
  1309. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1310. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1311. }
  1312. Engine *Engine::singleton = nullptr;
  1313. ////// EngineDebugger //////
  1314. bool EngineDebugger::is_active() {
  1315. return ::EngineDebugger::is_active();
  1316. }
  1317. void EngineDebugger::register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler) {
  1318. ERR_FAIL_COND(p_profiler.is_null());
  1319. ERR_FAIL_COND_MSG(p_profiler->is_bound(), "Profiler already registered.");
  1320. ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler name already in use: " + p_name);
  1321. Error err = p_profiler->bind(p_name);
  1322. ERR_FAIL_COND_MSG(err != OK, "Profiler failed to register with error: " + itos(err));
  1323. profilers.insert(p_name, p_profiler);
  1324. }
  1325. void EngineDebugger::unregister_profiler(const StringName &p_name) {
  1326. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
  1327. profilers[p_name]->unbind();
  1328. profilers.erase(p_name);
  1329. }
  1330. bool EngineDebugger::is_profiling(const StringName &p_name) {
  1331. return ::EngineDebugger::is_profiling(p_name);
  1332. }
  1333. bool EngineDebugger::has_profiler(const StringName &p_name) {
  1334. return ::EngineDebugger::has_profiler(p_name);
  1335. }
  1336. void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  1337. ::EngineDebugger::profiler_add_frame_data(p_name, p_data);
  1338. }
  1339. void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  1340. if (::EngineDebugger::get_singleton()) {
  1341. ::EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
  1342. }
  1343. }
  1344. void EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1345. ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), "Capture already registered: " + p_name);
  1346. captures.insert(p_name, p_callable);
  1347. Callable &c = captures[p_name];
  1348. ::EngineDebugger::Capture capture(&c, &EngineDebugger::call_capture);
  1349. ::EngineDebugger::register_message_capture(p_name, capture);
  1350. }
  1351. void EngineDebugger::unregister_message_capture(const StringName &p_name) {
  1352. ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
  1353. ::EngineDebugger::unregister_message_capture(p_name);
  1354. captures.erase(p_name);
  1355. }
  1356. bool EngineDebugger::has_capture(const StringName &p_name) {
  1357. return ::EngineDebugger::has_capture(p_name);
  1358. }
  1359. void EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
  1360. ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send message. No active debugger");
  1361. ::EngineDebugger::get_singleton()->send_message(p_msg, p_data);
  1362. }
  1363. Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  1364. Callable &capture = *(Callable *)p_user;
  1365. if (capture.is_null()) {
  1366. return FAILED;
  1367. }
  1368. Variant cmd = p_cmd, data = p_data;
  1369. const Variant *args[2] = { &cmd, &data };
  1370. Variant retval;
  1371. Callable::CallError err;
  1372. capture.callp(args, 2, retval, err);
  1373. 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));
  1374. ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, "Error calling 'capture' to callable: " + String(capture) + ". Return type is not bool.");
  1375. r_captured = retval;
  1376. return OK;
  1377. }
  1378. EngineDebugger::~EngineDebugger() {
  1379. for (const KeyValue<StringName, Callable> &E : captures) {
  1380. ::EngineDebugger::unregister_message_capture(E.key);
  1381. }
  1382. captures.clear();
  1383. }
  1384. EngineDebugger *EngineDebugger::singleton = nullptr;
  1385. void EngineDebugger::_bind_methods() {
  1386. ClassDB::bind_method(D_METHOD("is_active"), &EngineDebugger::is_active);
  1387. ClassDB::bind_method(D_METHOD("register_profiler", "name", "profiler"), &EngineDebugger::register_profiler);
  1388. ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &EngineDebugger::unregister_profiler);
  1389. ClassDB::bind_method(D_METHOD("is_profiling", "name"), &EngineDebugger::is_profiling);
  1390. ClassDB::bind_method(D_METHOD("has_profiler", "name"), &EngineDebugger::has_profiler);
  1391. ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &EngineDebugger::profiler_add_frame_data);
  1392. ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &EngineDebugger::profiler_enable, DEFVAL(Array()));
  1393. ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &EngineDebugger::register_message_capture);
  1394. ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &EngineDebugger::unregister_message_capture);
  1395. ClassDB::bind_method(D_METHOD("has_capture", "name"), &EngineDebugger::has_capture);
  1396. ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &EngineDebugger::send_message);
  1397. }
  1398. } // namespace core_bind