core_bind.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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. int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) {
  208. List<String> args;
  209. for (int i = 0; i < p_arguments.size(); i++) {
  210. args.push_back(p_arguments[i]);
  211. }
  212. String pipe;
  213. int exitcode = 0;
  214. Error err = ::OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr, nullptr, p_open_console);
  215. r_output.push_back(pipe);
  216. if (err != OK) {
  217. return -1;
  218. }
  219. return exitcode;
  220. }
  221. int OS::create_instance(const Vector<String> &p_arguments) {
  222. List<String> args;
  223. for (int i = 0; i < p_arguments.size(); i++) {
  224. args.push_back(p_arguments[i]);
  225. }
  226. ::OS::ProcessID pid = 0;
  227. Error err = ::OS::get_singleton()->create_instance(args, &pid);
  228. if (err != OK) {
  229. return -1;
  230. }
  231. return pid;
  232. }
  233. int OS::create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console) {
  234. List<String> args;
  235. for (int i = 0; i < p_arguments.size(); i++) {
  236. args.push_back(p_arguments[i]);
  237. }
  238. ::OS::ProcessID pid = 0;
  239. Error err = ::OS::get_singleton()->create_process(p_path, args, &pid, p_open_console);
  240. if (err != OK) {
  241. return -1;
  242. }
  243. return pid;
  244. }
  245. Error OS::kill(int p_pid) {
  246. return ::OS::get_singleton()->kill(p_pid);
  247. }
  248. bool OS::is_process_running(int p_pid) const {
  249. return ::OS::get_singleton()->is_process_running(p_pid);
  250. }
  251. int OS::get_process_id() const {
  252. return ::OS::get_singleton()->get_process_id();
  253. }
  254. bool OS::has_environment(const String &p_var) const {
  255. return ::OS::get_singleton()->has_environment(p_var);
  256. }
  257. String OS::get_environment(const String &p_var) const {
  258. return ::OS::get_singleton()->get_environment(p_var);
  259. }
  260. bool OS::set_environment(const String &p_var, const String &p_value) const {
  261. return ::OS::get_singleton()->set_environment(p_var, p_value);
  262. }
  263. String OS::get_name() const {
  264. return ::OS::get_singleton()->get_name();
  265. }
  266. String OS::get_distribution_name() const {
  267. return ::OS::get_singleton()->get_distribution_name();
  268. }
  269. String OS::get_version() const {
  270. return ::OS::get_singleton()->get_version();
  271. }
  272. Vector<String> OS::get_cmdline_args() {
  273. List<String> cmdline = ::OS::get_singleton()->get_cmdline_args();
  274. Vector<String> cmdlinev;
  275. for (const String &E : cmdline) {
  276. cmdlinev.push_back(E);
  277. }
  278. return cmdlinev;
  279. }
  280. Vector<String> OS::get_cmdline_user_args() {
  281. List<String> cmdline = ::OS::get_singleton()->get_cmdline_user_args();
  282. Vector<String> cmdlinev;
  283. for (const String &E : cmdline) {
  284. cmdlinev.push_back(E);
  285. }
  286. return cmdlinev;
  287. }
  288. void OS::set_restart_on_exit(bool p_restart, const Vector<String> &p_restart_arguments) {
  289. List<String> args_list;
  290. for (const String &restart_argument : p_restart_arguments) {
  291. args_list.push_back(restart_argument);
  292. }
  293. ::OS::get_singleton()->set_restart_on_exit(p_restart, args_list);
  294. }
  295. bool OS::is_restart_on_exit_set() const {
  296. return ::OS::get_singleton()->is_restart_on_exit_set();
  297. }
  298. Vector<String> OS::get_restart_on_exit_arguments() const {
  299. List<String> args = ::OS::get_singleton()->get_restart_on_exit_arguments();
  300. Vector<String> args_vector;
  301. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  302. args_vector.push_back(E->get());
  303. }
  304. return args_vector;
  305. }
  306. String OS::get_locale() const {
  307. return ::OS::get_singleton()->get_locale();
  308. }
  309. String OS::get_locale_language() const {
  310. return ::OS::get_singleton()->get_locale_language();
  311. }
  312. String OS::get_model_name() const {
  313. return ::OS::get_singleton()->get_model_name();
  314. }
  315. Error OS::set_thread_name(const String &p_name) {
  316. return ::Thread::set_name(p_name);
  317. }
  318. ::Thread::ID OS::get_thread_caller_id() const {
  319. return ::Thread::get_caller_id();
  320. };
  321. ::Thread::ID OS::get_main_thread_id() const {
  322. return ::Thread::get_main_id();
  323. };
  324. bool OS::has_feature(const String &p_feature) const {
  325. return ::OS::get_singleton()->has_feature(p_feature);
  326. }
  327. uint64_t OS::get_static_memory_usage() const {
  328. return ::OS::get_singleton()->get_static_memory_usage();
  329. }
  330. uint64_t OS::get_static_memory_peak_usage() const {
  331. return ::OS::get_singleton()->get_static_memory_peak_usage();
  332. }
  333. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  334. void OS::delay_usec(int p_usec) const {
  335. ERR_FAIL_COND_MSG(
  336. p_usec < 0,
  337. vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
  338. ::OS::get_singleton()->delay_usec(p_usec);
  339. }
  340. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  341. void OS::delay_msec(int p_msec) const {
  342. ERR_FAIL_COND_MSG(
  343. p_msec < 0,
  344. vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
  345. ::OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  346. }
  347. bool OS::is_userfs_persistent() const {
  348. return ::OS::get_singleton()->is_userfs_persistent();
  349. }
  350. int OS::get_processor_count() const {
  351. return ::OS::get_singleton()->get_processor_count();
  352. }
  353. String OS::get_processor_name() const {
  354. return ::OS::get_singleton()->get_processor_name();
  355. }
  356. bool OS::is_stdout_verbose() const {
  357. return ::OS::get_singleton()->is_stdout_verbose();
  358. }
  359. Error OS::move_to_trash(const String &p_path) const {
  360. return ::OS::get_singleton()->move_to_trash(p_path);
  361. }
  362. String OS::get_user_data_dir() const {
  363. return ::OS::get_singleton()->get_user_data_dir();
  364. }
  365. String OS::get_config_dir() const {
  366. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  367. return ::OS::get_singleton()->get_config_path();
  368. }
  369. String OS::get_data_dir() const {
  370. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  371. return ::OS::get_singleton()->get_data_path();
  372. }
  373. String OS::get_cache_dir() const {
  374. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  375. return ::OS::get_singleton()->get_cache_path();
  376. }
  377. bool OS::is_debug_build() const {
  378. #ifdef DEBUG_ENABLED
  379. return true;
  380. #else
  381. return false;
  382. #endif
  383. }
  384. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  385. return ::OS::get_singleton()->get_system_dir(::OS::SystemDir(p_dir), p_shared_storage);
  386. }
  387. String OS::get_keycode_string(Key p_code) const {
  388. return ::keycode_get_string(p_code);
  389. }
  390. bool OS::is_keycode_unicode(char32_t p_unicode) const {
  391. return ::keycode_has_unicode((Key)p_unicode);
  392. }
  393. Key OS::find_keycode_from_string(const String &p_code) const {
  394. return find_keycode(p_code);
  395. }
  396. bool OS::request_permission(const String &p_name) {
  397. return ::OS::get_singleton()->request_permission(p_name);
  398. }
  399. bool OS::request_permissions() {
  400. return ::OS::get_singleton()->request_permissions();
  401. }
  402. Vector<String> OS::get_granted_permissions() const {
  403. return ::OS::get_singleton()->get_granted_permissions();
  404. }
  405. String OS::get_unique_id() const {
  406. return ::OS::get_singleton()->get_unique_id();
  407. }
  408. OS *OS::singleton = nullptr;
  409. void OS::_bind_methods() {
  410. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &OS::get_connected_midi_inputs);
  411. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &OS::open_midi_inputs);
  412. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &OS::close_midi_inputs);
  413. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &OS::alert, DEFVAL("Alert!"));
  414. ClassDB::bind_method(D_METHOD("crash", "message"), &OS::crash);
  415. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &OS::set_low_processor_usage_mode);
  416. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &OS::is_in_low_processor_usage_mode);
  417. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &OS::set_low_processor_usage_mode_sleep_usec);
  418. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &OS::get_low_processor_usage_mode_sleep_usec);
  419. ClassDB::bind_method(D_METHOD("get_processor_count"), &OS::get_processor_count);
  420. ClassDB::bind_method(D_METHOD("get_processor_name"), &OS::get_processor_name);
  421. ClassDB::bind_method(D_METHOD("get_system_fonts"), &OS::get_system_fonts);
  422. ClassDB::bind_method(D_METHOD("get_system_font_path", "font_name", "bold", "italic"), &OS::get_system_font_path, DEFVAL(false), DEFVAL(false));
  423. ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
  424. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
  425. ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
  426. ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
  427. ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
  428. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &OS::shell_open);
  429. ClassDB::bind_method(D_METHOD("is_process_running", "pid"), &OS::is_process_running);
  430. ClassDB::bind_method(D_METHOD("get_process_id"), &OS::get_process_id);
  431. ClassDB::bind_method(D_METHOD("get_environment", "variable"), &OS::get_environment);
  432. ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &OS::set_environment);
  433. ClassDB::bind_method(D_METHOD("has_environment", "variable"), &OS::has_environment);
  434. ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name);
  435. ClassDB::bind_method(D_METHOD("get_distribution_name"), &OS::get_distribution_name);
  436. ClassDB::bind_method(D_METHOD("get_version"), &OS::get_version);
  437. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &OS::get_cmdline_args);
  438. ClassDB::bind_method(D_METHOD("get_cmdline_user_args"), &OS::get_cmdline_user_args);
  439. ClassDB::bind_method(D_METHOD("set_restart_on_exit", "restart", "arguments"), &OS::set_restart_on_exit, DEFVAL(Vector<String>()));
  440. ClassDB::bind_method(D_METHOD("is_restart_on_exit_set"), &OS::is_restart_on_exit_set);
  441. ClassDB::bind_method(D_METHOD("get_restart_on_exit_arguments"), &OS::get_restart_on_exit_arguments);
  442. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &OS::delay_usec);
  443. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &OS::delay_msec);
  444. ClassDB::bind_method(D_METHOD("get_locale"), &OS::get_locale);
  445. ClassDB::bind_method(D_METHOD("get_locale_language"), &OS::get_locale_language);
  446. ClassDB::bind_method(D_METHOD("get_model_name"), &OS::get_model_name);
  447. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &OS::is_userfs_persistent);
  448. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &OS::is_stdout_verbose);
  449. ClassDB::bind_method(D_METHOD("is_debug_build"), &OS::is_debug_build);
  450. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &OS::get_static_memory_usage);
  451. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &OS::get_static_memory_peak_usage);
  452. ClassDB::bind_method(D_METHOD("move_to_trash", "path"), &OS::move_to_trash);
  453. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &OS::get_user_data_dir);
  454. ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &OS::get_system_dir, DEFVAL(true));
  455. ClassDB::bind_method(D_METHOD("get_config_dir"), &OS::get_config_dir);
  456. ClassDB::bind_method(D_METHOD("get_data_dir"), &OS::get_data_dir);
  457. ClassDB::bind_method(D_METHOD("get_cache_dir"), &OS::get_cache_dir);
  458. ClassDB::bind_method(D_METHOD("get_unique_id"), &OS::get_unique_id);
  459. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &OS::get_keycode_string);
  460. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &OS::is_keycode_unicode);
  461. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &OS::find_keycode_from_string);
  462. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &OS::set_use_file_access_save_and_swap);
  463. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &OS::set_thread_name);
  464. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &OS::get_thread_caller_id);
  465. ClassDB::bind_method(D_METHOD("get_main_thread_id"), &OS::get_main_thread_id);
  466. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &OS::has_feature);
  467. ClassDB::bind_method(D_METHOD("request_permission", "name"), &OS::request_permission);
  468. ClassDB::bind_method(D_METHOD("request_permissions"), &OS::request_permissions);
  469. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &OS::get_granted_permissions);
  470. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  471. 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");
  472. // Those default values need to be specified for the docs generator,
  473. // to avoid using values from the documentation writer's own OS instance.
  474. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  475. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  476. BIND_ENUM_CONSTANT(VIDEO_DRIVER_VULKAN);
  477. BIND_ENUM_CONSTANT(VIDEO_DRIVER_OPENGL_3);
  478. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  479. BIND_ENUM_CONSTANT(DAY_MONDAY);
  480. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  481. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  482. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  483. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  484. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  485. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  486. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  487. BIND_ENUM_CONSTANT(MONTH_MARCH);
  488. BIND_ENUM_CONSTANT(MONTH_APRIL);
  489. BIND_ENUM_CONSTANT(MONTH_MAY);
  490. BIND_ENUM_CONSTANT(MONTH_JUNE);
  491. BIND_ENUM_CONSTANT(MONTH_JULY);
  492. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  493. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  494. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  495. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  496. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  497. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  498. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  499. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  500. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  501. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  502. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  503. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  504. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  505. }
  506. ////// Geometry2D //////
  507. Geometry2D *Geometry2D::singleton = nullptr;
  508. Geometry2D *Geometry2D::get_singleton() {
  509. return singleton;
  510. }
  511. bool Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  512. return ::Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  513. }
  514. real_t Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  515. return ::Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  516. }
  517. 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) {
  518. Vector2 result;
  519. if (::Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  520. return result;
  521. } else {
  522. return Variant();
  523. }
  524. }
  525. 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) {
  526. Vector2 result;
  527. if (::Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  528. return result;
  529. } else {
  530. return Variant();
  531. }
  532. }
  533. Vector<Vector2> Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  534. Vector2 r1, r2;
  535. ::Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  536. Vector<Vector2> r = { r1, r2 };
  537. return r;
  538. }
  539. Vector2 Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  540. Vector2 s[2] = { p_a, p_b };
  541. return ::Geometry2D::get_closest_point_to_segment(p_point, s);
  542. }
  543. Vector2 Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  544. Vector2 s[2] = { p_a, p_b };
  545. return ::Geometry2D::get_closest_point_to_segment_uncapped(p_point, s);
  546. }
  547. bool Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  548. return ::Geometry2D::is_point_in_triangle(s, a, b, c);
  549. }
  550. bool Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  551. return ::Geometry2D::is_polygon_clockwise(p_polygon);
  552. }
  553. bool Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  554. return ::Geometry2D::is_point_in_polygon(p_point, p_polygon);
  555. }
  556. Vector<int> Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  557. return ::Geometry2D::triangulate_polygon(p_polygon);
  558. }
  559. Vector<int> Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
  560. return ::Geometry2D::triangulate_delaunay(p_points);
  561. }
  562. Vector<Point2> Geometry2D::convex_hull(const Vector<Point2> &p_points) {
  563. return ::Geometry2D::convex_hull(p_points);
  564. }
  565. TypedArray<PackedVector2Array> Geometry2D::decompose_polygon_in_convex(const Vector<Vector2> &p_polygon) {
  566. Vector<Vector<Point2>> decomp = ::Geometry2D::decompose_polygon_in_convex(p_polygon);
  567. TypedArray<PackedVector2Array> ret;
  568. for (int i = 0; i < decomp.size(); ++i) {
  569. ret.push_back(decomp[i]);
  570. }
  571. return ret;
  572. }
  573. TypedArray<PackedVector2Array> Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  574. Vector<Vector<Point2>> polys = ::Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
  575. Array ret;
  576. for (int i = 0; i < polys.size(); ++i) {
  577. ret.push_back(polys[i]);
  578. }
  579. return ret;
  580. }
  581. TypedArray<PackedVector2Array> Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  582. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
  583. TypedArray<PackedVector2Array> 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::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  590. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
  591. Array 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::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  598. Vector<Vector<Point2>> polys = ::Geometry2D::exclude_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::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  606. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
  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::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  614. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_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::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  622. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polygon(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type));
  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_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  630. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polyline(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type), ::Geometry2D::PolyEndType(p_end_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. Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
  638. Dictionary ret;
  639. Vector<Size2i> rects;
  640. for (int i = 0; i < p_rects.size(); i++) {
  641. rects.push_back(p_rects[i]);
  642. }
  643. Vector<Point2i> result;
  644. Size2i size;
  645. ::Geometry2D::make_atlas(rects, result, size);
  646. Vector<Point2> r_result;
  647. for (int i = 0; i < result.size(); i++) {
  648. r_result.push_back(result[i]);
  649. }
  650. ret["points"] = r_result;
  651. ret["size"] = size;
  652. return ret;
  653. }
  654. void Geometry2D::_bind_methods() {
  655. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &Geometry2D::is_point_in_circle);
  656. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &Geometry2D::segment_intersects_circle);
  657. ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &Geometry2D::segment_intersects_segment);
  658. ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &Geometry2D::line_intersects_line);
  659. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &Geometry2D::get_closest_points_between_segments);
  660. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment);
  661. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment_uncapped);
  662. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &Geometry2D::point_is_inside_triangle);
  663. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &Geometry2D::is_polygon_clockwise);
  664. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &Geometry2D::is_point_in_polygon);
  665. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &Geometry2D::triangulate_polygon);
  666. ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &Geometry2D::triangulate_delaunay);
  667. ClassDB::bind_method(D_METHOD("convex_hull", "points"), &Geometry2D::convex_hull);
  668. ClassDB::bind_method(D_METHOD("decompose_polygon_in_convex", "polygon"), &Geometry2D::decompose_polygon_in_convex);
  669. ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &Geometry2D::merge_polygons);
  670. ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &Geometry2D::clip_polygons);
  671. ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &Geometry2D::intersect_polygons);
  672. ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &Geometry2D::exclude_polygons);
  673. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::clip_polyline_with_polygon);
  674. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::intersect_polyline_with_polygon);
  675. ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
  676. ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  677. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &Geometry2D::make_atlas);
  678. BIND_ENUM_CONSTANT(OPERATION_UNION);
  679. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  680. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  681. BIND_ENUM_CONSTANT(OPERATION_XOR);
  682. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  683. BIND_ENUM_CONSTANT(JOIN_ROUND);
  684. BIND_ENUM_CONSTANT(JOIN_MITER);
  685. BIND_ENUM_CONSTANT(END_POLYGON);
  686. BIND_ENUM_CONSTANT(END_JOINED);
  687. BIND_ENUM_CONSTANT(END_BUTT);
  688. BIND_ENUM_CONSTANT(END_SQUARE);
  689. BIND_ENUM_CONSTANT(END_ROUND);
  690. }
  691. ////// Geometry3D //////
  692. Geometry3D *Geometry3D::singleton = nullptr;
  693. Geometry3D *Geometry3D::get_singleton() {
  694. return singleton;
  695. }
  696. TypedArray<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
  697. Variant ret = ::Geometry3D::build_box_planes(p_extents);
  698. return ret;
  699. }
  700. TypedArray<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  701. Variant ret = ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  702. return ret;
  703. }
  704. TypedArray<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  705. Variant ret = ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  706. return ret;
  707. }
  708. Vector<Vector3> Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  709. Vector3 r1, r2;
  710. ::Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  711. Vector<Vector3> r = { r1, r2 };
  712. return r;
  713. }
  714. Vector3 Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  715. Vector3 s[2] = { p_a, p_b };
  716. return ::Geometry3D::get_closest_point_to_segment(p_point, s);
  717. }
  718. Vector3 Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  719. Vector3 s[2] = { p_a, p_b };
  720. return ::Geometry3D::get_closest_point_to_segment_uncapped(p_point, s);
  721. }
  722. 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) {
  723. Vector3 res;
  724. if (::Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  725. return res;
  726. } else {
  727. return Variant();
  728. }
  729. }
  730. 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) {
  731. Vector3 res;
  732. if (::Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  733. return res;
  734. } else {
  735. return Variant();
  736. }
  737. }
  738. Vector<Vector3> Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  739. Vector<Vector3> r;
  740. Vector3 res, norm;
  741. if (!::Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  742. return r;
  743. }
  744. r.resize(2);
  745. r.set(0, res);
  746. r.set(1, norm);
  747. return r;
  748. }
  749. Vector<Vector3> Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  750. Vector<Vector3> r;
  751. Vector3 res, norm;
  752. if (!::Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  753. return r;
  754. }
  755. r.resize(2);
  756. r.set(0, res);
  757. r.set(1, norm);
  758. return r;
  759. }
  760. Vector<Vector3> Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  761. Vector<Vector3> r;
  762. Vector3 res, norm;
  763. if (!::Geometry3D::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm)) {
  764. return r;
  765. }
  766. r.resize(2);
  767. r.set(0, res);
  768. r.set(1, norm);
  769. return r;
  770. }
  771. Vector<Vector3> Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  772. return ::Geometry3D::clip_polygon(p_points, p_plane);
  773. }
  774. void Geometry3D::_bind_methods() {
  775. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &Geometry3D::build_box_planes);
  776. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  777. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  778. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &Geometry3D::get_closest_points_between_segments);
  779. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment);
  780. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment_uncapped);
  781. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &Geometry3D::ray_intersects_triangle);
  782. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &Geometry3D::segment_intersects_triangle);
  783. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &Geometry3D::segment_intersects_sphere);
  784. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &Geometry3D::segment_intersects_cylinder);
  785. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &Geometry3D::segment_intersects_convex);
  786. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &Geometry3D::clip_polygon);
  787. }
  788. ////// Marshalls //////
  789. Marshalls *Marshalls::singleton = nullptr;
  790. Marshalls *Marshalls::get_singleton() {
  791. return singleton;
  792. }
  793. String Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  794. int len;
  795. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  796. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  797. Vector<uint8_t> buff;
  798. buff.resize(len);
  799. uint8_t *w = buff.ptrw();
  800. err = encode_variant(p_var, &w[0], len, p_full_objects);
  801. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  802. String ret = CryptoCore::b64_encode_str(&w[0], len);
  803. ERR_FAIL_COND_V(ret.is_empty(), ret);
  804. return ret;
  805. }
  806. Variant Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  807. int strlen = p_str.length();
  808. CharString cstr = p_str.ascii();
  809. Vector<uint8_t> buf;
  810. buf.resize(strlen / 4 * 3 + 1);
  811. uint8_t *w = buf.ptrw();
  812. size_t len = 0;
  813. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  814. Variant v;
  815. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  816. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  817. return v;
  818. }
  819. String Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  820. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  821. ERR_FAIL_COND_V(ret.is_empty(), ret);
  822. return ret;
  823. }
  824. Vector<uint8_t> Marshalls::base64_to_raw(const String &p_str) {
  825. int strlen = p_str.length();
  826. CharString cstr = p_str.ascii();
  827. size_t arr_len = 0;
  828. Vector<uint8_t> buf;
  829. {
  830. buf.resize(strlen / 4 * 3 + 1);
  831. uint8_t *w = buf.ptrw();
  832. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  833. }
  834. buf.resize(arr_len);
  835. return buf;
  836. }
  837. String Marshalls::utf8_to_base64(const String &p_str) {
  838. CharString cstr = p_str.utf8();
  839. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  840. ERR_FAIL_COND_V(ret.is_empty(), ret);
  841. return ret;
  842. }
  843. String Marshalls::base64_to_utf8(const String &p_str) {
  844. int strlen = p_str.length();
  845. CharString cstr = p_str.ascii();
  846. Vector<uint8_t> buf;
  847. buf.resize(strlen / 4 * 3 + 1 + 1);
  848. uint8_t *w = buf.ptrw();
  849. size_t len = 0;
  850. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  851. w[len] = 0;
  852. String ret = String::utf8((char *)&w[0]);
  853. return ret;
  854. }
  855. void Marshalls::_bind_methods() {
  856. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &Marshalls::variant_to_base64, DEFVAL(false));
  857. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &Marshalls::base64_to_variant, DEFVAL(false));
  858. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &Marshalls::raw_to_base64);
  859. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &Marshalls::base64_to_raw);
  860. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &Marshalls::utf8_to_base64);
  861. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &Marshalls::base64_to_utf8);
  862. }
  863. ////// Semaphore //////
  864. void Semaphore::wait() {
  865. semaphore.wait();
  866. }
  867. Error Semaphore::try_wait() {
  868. return semaphore.try_wait() ? OK : ERR_BUSY;
  869. }
  870. void Semaphore::post() {
  871. semaphore.post();
  872. }
  873. void Semaphore::_bind_methods() {
  874. ClassDB::bind_method(D_METHOD("wait"), &Semaphore::wait);
  875. ClassDB::bind_method(D_METHOD("try_wait"), &Semaphore::try_wait);
  876. ClassDB::bind_method(D_METHOD("post"), &Semaphore::post);
  877. }
  878. ////// Mutex //////
  879. void Mutex::lock() {
  880. mutex.lock();
  881. }
  882. Error Mutex::try_lock() {
  883. return mutex.try_lock();
  884. }
  885. void Mutex::unlock() {
  886. mutex.unlock();
  887. }
  888. void Mutex::_bind_methods() {
  889. ClassDB::bind_method(D_METHOD("lock"), &Mutex::lock);
  890. ClassDB::bind_method(D_METHOD("try_lock"), &Mutex::try_lock);
  891. ClassDB::bind_method(D_METHOD("unlock"), &Mutex::unlock);
  892. }
  893. ////// Thread //////
  894. void Thread::_start_func(void *ud) {
  895. Ref<Thread> *tud = (Ref<Thread> *)ud;
  896. Ref<Thread> t = *tud;
  897. memdelete(tud);
  898. Object *target_instance = t->target_callable.get_object();
  899. if (!target_instance) {
  900. t->running.clear();
  901. 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()));
  902. }
  903. String func_name = t->target_callable.is_custom() ? t->target_callable.get_custom()->get_as_text() : String(t->target_callable.get_method());
  904. ::Thread::set_name(func_name);
  905. Callable::CallError ce;
  906. t->target_callable.callp(nullptr, 0, t->ret, ce);
  907. if (ce.error != Callable::CallError::CALL_OK) {
  908. t->running.clear();
  909. 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) + ".");
  910. }
  911. t->running.clear();
  912. }
  913. Error Thread::start(const Callable &p_callable, Priority p_priority) {
  914. ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
  915. ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
  916. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  917. ret = Variant();
  918. target_callable = p_callable;
  919. running.set();
  920. Ref<Thread> *ud = memnew(Ref<Thread>(this));
  921. ::Thread::Settings s;
  922. s.priority = (::Thread::Priority)p_priority;
  923. thread.start(_start_func, ud, s);
  924. return OK;
  925. }
  926. String Thread::get_id() const {
  927. return itos(thread.get_id());
  928. }
  929. bool Thread::is_started() const {
  930. return thread.is_started();
  931. }
  932. bool Thread::is_alive() const {
  933. return running.is_set();
  934. }
  935. Variant Thread::wait_to_finish() {
  936. ERR_FAIL_COND_V_MSG(!is_started(), Variant(), "Thread must have been started to wait for its completion.");
  937. thread.wait_to_finish();
  938. Variant r = ret;
  939. target_callable = Callable();
  940. return r;
  941. }
  942. void Thread::_bind_methods() {
  943. ClassDB::bind_method(D_METHOD("start", "callable", "priority"), &Thread::start, DEFVAL(PRIORITY_NORMAL));
  944. ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
  945. ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
  946. ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive);
  947. ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish);
  948. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  949. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  950. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  951. }
  952. namespace special {
  953. ////// ClassDB //////
  954. PackedStringArray ClassDB::get_class_list() const {
  955. List<StringName> classes;
  956. ::ClassDB::get_class_list(&classes);
  957. PackedStringArray ret;
  958. ret.resize(classes.size());
  959. int idx = 0;
  960. for (const StringName &E : classes) {
  961. ret.set(idx++, E);
  962. }
  963. return ret;
  964. }
  965. PackedStringArray ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  966. List<StringName> classes;
  967. ::ClassDB::get_inheriters_from_class(p_class, &classes);
  968. PackedStringArray ret;
  969. ret.resize(classes.size());
  970. int idx = 0;
  971. for (const StringName &E : classes) {
  972. ret.set(idx++, E);
  973. }
  974. return ret;
  975. }
  976. StringName ClassDB::get_parent_class(const StringName &p_class) const {
  977. return ::ClassDB::get_parent_class(p_class);
  978. }
  979. bool ClassDB::class_exists(const StringName &p_class) const {
  980. return ::ClassDB::class_exists(p_class);
  981. }
  982. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  983. return ::ClassDB::is_parent_class(p_class, p_inherits);
  984. }
  985. bool ClassDB::can_instantiate(const StringName &p_class) const {
  986. return ::ClassDB::can_instantiate(p_class);
  987. }
  988. Variant ClassDB::instantiate(const StringName &p_class) const {
  989. Object *obj = ::ClassDB::instantiate(p_class);
  990. if (!obj) {
  991. return Variant();
  992. }
  993. RefCounted *r = Object::cast_to<RefCounted>(obj);
  994. if (r) {
  995. return Ref<RefCounted>(r);
  996. } else {
  997. return obj;
  998. }
  999. }
  1000. bool ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  1001. return ::ClassDB::has_signal(p_class, p_signal);
  1002. }
  1003. Dictionary ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  1004. MethodInfo signal;
  1005. if (::ClassDB::get_signal(p_class, p_signal, &signal)) {
  1006. return signal.operator Dictionary();
  1007. } else {
  1008. return Dictionary();
  1009. }
  1010. }
  1011. TypedArray<Dictionary> ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  1012. List<MethodInfo> signals;
  1013. ::ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1014. TypedArray<Dictionary> ret;
  1015. for (const MethodInfo &E : signals) {
  1016. ret.push_back(E.operator Dictionary());
  1017. }
  1018. return ret;
  1019. }
  1020. TypedArray<Dictionary> ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  1021. List<PropertyInfo> plist;
  1022. ::ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1023. TypedArray<Dictionary> ret;
  1024. for (const PropertyInfo &E : plist) {
  1025. ret.push_back(E.operator Dictionary());
  1026. }
  1027. return ret;
  1028. }
  1029. Variant ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  1030. Variant ret;
  1031. ::ClassDB::get_property(p_object, p_property, ret);
  1032. return ret;
  1033. }
  1034. Error ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1035. Variant ret;
  1036. bool valid;
  1037. if (!::ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1038. return ERR_UNAVAILABLE;
  1039. } else if (!valid) {
  1040. return ERR_INVALID_DATA;
  1041. }
  1042. return OK;
  1043. }
  1044. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  1045. return ::ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1046. }
  1047. TypedArray<Dictionary> ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  1048. List<MethodInfo> methods;
  1049. ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1050. TypedArray<Dictionary> ret;
  1051. for (const MethodInfo &E : methods) {
  1052. #ifdef DEBUG_METHODS_ENABLED
  1053. ret.push_back(E.operator Dictionary());
  1054. #else
  1055. Dictionary dict;
  1056. dict["name"] = E.name;
  1057. ret.push_back(dict);
  1058. #endif
  1059. }
  1060. return ret;
  1061. }
  1062. PackedStringArray ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1063. List<String> constants;
  1064. ::ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1065. PackedStringArray ret;
  1066. ret.resize(constants.size());
  1067. int idx = 0;
  1068. for (const String &E : constants) {
  1069. ret.set(idx++, E);
  1070. }
  1071. return ret;
  1072. }
  1073. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1074. bool success;
  1075. ::ClassDB::get_integer_constant(p_class, p_name, &success);
  1076. return success;
  1077. }
  1078. int64_t ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1079. bool found;
  1080. int64_t c = ::ClassDB::get_integer_constant(p_class, p_name, &found);
  1081. ERR_FAIL_COND_V(!found, 0);
  1082. return c;
  1083. }
  1084. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1085. return ::ClassDB::has_enum(p_class, p_name, p_no_inheritance);
  1086. }
  1087. PackedStringArray ClassDB::get_enum_list(const StringName &p_class, bool p_no_inheritance) const {
  1088. List<StringName> enums;
  1089. ::ClassDB::get_enum_list(p_class, &enums, p_no_inheritance);
  1090. PackedStringArray ret;
  1091. ret.resize(enums.size());
  1092. int idx = 0;
  1093. for (const StringName &E : enums) {
  1094. ret.set(idx++, E);
  1095. }
  1096. return ret;
  1097. }
  1098. PackedStringArray ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
  1099. List<StringName> constants;
  1100. ::ClassDB::get_enum_constants(p_class, p_enum, &constants, p_no_inheritance);
  1101. PackedStringArray ret;
  1102. ret.resize(constants.size());
  1103. int idx = 0;
  1104. for (const StringName &E : constants) {
  1105. ret.set(idx++, E);
  1106. }
  1107. return ret;
  1108. }
  1109. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1110. return ::ClassDB::get_integer_constant_enum(p_class, p_name, p_no_inheritance);
  1111. }
  1112. bool ClassDB::is_class_enabled(StringName p_class) const {
  1113. return ::ClassDB::is_class_enabled(p_class);
  1114. }
  1115. void ClassDB::_bind_methods() {
  1116. ::ClassDB::bind_method(D_METHOD("get_class_list"), &ClassDB::get_class_list);
  1117. ::ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &ClassDB::get_inheriters_from_class);
  1118. ::ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &ClassDB::get_parent_class);
  1119. ::ClassDB::bind_method(D_METHOD("class_exists", "class"), &ClassDB::class_exists);
  1120. ::ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &ClassDB::is_parent_class);
  1121. ::ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &ClassDB::can_instantiate);
  1122. ::ClassDB::bind_method(D_METHOD("instantiate", "class"), &ClassDB::instantiate);
  1123. ::ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &ClassDB::has_signal);
  1124. ::ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &ClassDB::get_signal);
  1125. ::ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &ClassDB::get_signal_list, DEFVAL(false));
  1126. ::ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &ClassDB::get_property_list, DEFVAL(false));
  1127. ::ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &ClassDB::get_property);
  1128. ::ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &ClassDB::set_property);
  1129. ::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::has_method, DEFVAL(false));
  1130. ::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::get_method_list, DEFVAL(false));
  1131. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::get_integer_constant_list, DEFVAL(false));
  1132. ::ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &ClassDB::has_integer_constant);
  1133. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &ClassDB::get_integer_constant);
  1134. ::ClassDB::bind_method(D_METHOD("class_has_enum", "class", "name", "no_inheritance"), &ClassDB::has_enum, DEFVAL(false));
  1135. ::ClassDB::bind_method(D_METHOD("class_get_enum_list", "class", "no_inheritance"), &ClassDB::get_enum_list, DEFVAL(false));
  1136. ::ClassDB::bind_method(D_METHOD("class_get_enum_constants", "class", "enum", "no_inheritance"), &ClassDB::get_enum_constants, DEFVAL(false));
  1137. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_enum", "class", "name", "no_inheritance"), &ClassDB::get_integer_constant_enum, DEFVAL(false));
  1138. ::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);
  1139. }
  1140. } // namespace special
  1141. ////// Engine //////
  1142. void Engine::set_physics_ticks_per_second(int p_ips) {
  1143. ::Engine::get_singleton()->set_physics_ticks_per_second(p_ips);
  1144. }
  1145. int Engine::get_physics_ticks_per_second() const {
  1146. return ::Engine::get_singleton()->get_physics_ticks_per_second();
  1147. }
  1148. void Engine::set_physics_jitter_fix(double p_threshold) {
  1149. ::Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1150. }
  1151. double Engine::get_physics_jitter_fix() const {
  1152. return ::Engine::get_singleton()->get_physics_jitter_fix();
  1153. }
  1154. double Engine::get_physics_interpolation_fraction() const {
  1155. return ::Engine::get_singleton()->get_physics_interpolation_fraction();
  1156. }
  1157. void Engine::set_target_fps(int p_fps) {
  1158. ::Engine::get_singleton()->set_target_fps(p_fps);
  1159. }
  1160. int Engine::get_target_fps() const {
  1161. return ::Engine::get_singleton()->get_target_fps();
  1162. }
  1163. double Engine::get_frames_per_second() const {
  1164. return ::Engine::get_singleton()->get_frames_per_second();
  1165. }
  1166. uint64_t Engine::get_physics_frames() const {
  1167. return ::Engine::get_singleton()->get_physics_frames();
  1168. }
  1169. uint64_t Engine::get_process_frames() const {
  1170. return ::Engine::get_singleton()->get_process_frames();
  1171. }
  1172. void Engine::set_time_scale(double p_scale) {
  1173. ::Engine::get_singleton()->set_time_scale(p_scale);
  1174. }
  1175. double Engine::get_time_scale() {
  1176. return ::Engine::get_singleton()->get_time_scale();
  1177. }
  1178. int Engine::get_frames_drawn() {
  1179. return ::Engine::get_singleton()->get_frames_drawn();
  1180. }
  1181. MainLoop *Engine::get_main_loop() const {
  1182. // Needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1183. return ::OS::get_singleton()->get_main_loop();
  1184. }
  1185. Dictionary Engine::get_version_info() const {
  1186. return ::Engine::get_singleton()->get_version_info();
  1187. }
  1188. Dictionary Engine::get_author_info() const {
  1189. return ::Engine::get_singleton()->get_author_info();
  1190. }
  1191. TypedArray<Dictionary> Engine::get_copyright_info() const {
  1192. return ::Engine::get_singleton()->get_copyright_info();
  1193. }
  1194. Dictionary Engine::get_donor_info() const {
  1195. return ::Engine::get_singleton()->get_donor_info();
  1196. }
  1197. Dictionary Engine::get_license_info() const {
  1198. return ::Engine::get_singleton()->get_license_info();
  1199. }
  1200. String Engine::get_license_text() const {
  1201. return ::Engine::get_singleton()->get_license_text();
  1202. }
  1203. String Engine::get_architecture_name() const {
  1204. return ::Engine::get_singleton()->get_architecture_name();
  1205. }
  1206. bool Engine::is_in_physics_frame() const {
  1207. return ::Engine::get_singleton()->is_in_physics_frame();
  1208. }
  1209. bool Engine::has_singleton(const StringName &p_name) const {
  1210. return ::Engine::get_singleton()->has_singleton(p_name);
  1211. }
  1212. Object *Engine::get_singleton_object(const StringName &p_name) const {
  1213. return ::Engine::get_singleton()->get_singleton_object(p_name);
  1214. }
  1215. void Engine::register_singleton(const StringName &p_name, Object *p_object) {
  1216. ERR_FAIL_COND_MSG(has_singleton(p_name), "Singleton already registered: " + String(p_name));
  1217. ERR_FAIL_COND_MSG(!String(p_name).is_valid_identifier(), "Singleton name is not a valid identifier: " + p_name);
  1218. ::Engine::Singleton s;
  1219. s.class_name = p_name;
  1220. s.name = p_name;
  1221. s.ptr = p_object;
  1222. s.user_created = true;
  1223. ::Engine::get_singleton()->add_singleton(s);
  1224. }
  1225. void Engine::unregister_singleton(const StringName &p_name) {
  1226. ERR_FAIL_COND_MSG(!has_singleton(p_name), "Attempt to remove unregistered singleton: " + String(p_name));
  1227. ERR_FAIL_COND_MSG(!::Engine::get_singleton()->is_singleton_user_created(p_name), "Attempt to remove non-user created singleton: " + String(p_name));
  1228. ::Engine::get_singleton()->remove_singleton(p_name);
  1229. }
  1230. Vector<String> Engine::get_singleton_list() const {
  1231. List<::Engine::Singleton> singletons;
  1232. ::Engine::get_singleton()->get_singletons(&singletons);
  1233. Vector<String> ret;
  1234. for (List<::Engine::Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  1235. ret.push_back(E->get().name);
  1236. }
  1237. return ret;
  1238. }
  1239. void Engine::register_script_language(ScriptLanguage *p_language) {
  1240. ScriptServer::register_language(p_language);
  1241. }
  1242. int Engine::get_script_language_count() {
  1243. return ScriptServer::get_language_count();
  1244. }
  1245. ScriptLanguage *Engine::get_script_language(int p_index) const {
  1246. return ScriptServer::get_language(p_index);
  1247. }
  1248. void Engine::set_editor_hint(bool p_enabled) {
  1249. ::Engine::get_singleton()->set_editor_hint(p_enabled);
  1250. }
  1251. bool Engine::is_editor_hint() const {
  1252. return ::Engine::get_singleton()->is_editor_hint();
  1253. }
  1254. String Engine::get_write_movie_path() const {
  1255. return ::Engine::get_singleton()->get_write_movie_path();
  1256. }
  1257. void Engine::set_print_error_messages(bool p_enabled) {
  1258. ::Engine::get_singleton()->set_print_error_messages(p_enabled);
  1259. }
  1260. bool Engine::is_printing_error_messages() const {
  1261. return ::Engine::get_singleton()->is_printing_error_messages();
  1262. }
  1263. void Engine::_bind_methods() {
  1264. ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second);
  1265. ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second);
  1266. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix);
  1267. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix);
  1268. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction);
  1269. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &Engine::set_target_fps);
  1270. ClassDB::bind_method(D_METHOD("get_target_fps"), &Engine::get_target_fps);
  1271. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &Engine::set_time_scale);
  1272. ClassDB::bind_method(D_METHOD("get_time_scale"), &Engine::get_time_scale);
  1273. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &Engine::get_frames_drawn);
  1274. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &Engine::get_frames_per_second);
  1275. ClassDB::bind_method(D_METHOD("get_physics_frames"), &Engine::get_physics_frames);
  1276. ClassDB::bind_method(D_METHOD("get_process_frames"), &Engine::get_process_frames);
  1277. ClassDB::bind_method(D_METHOD("get_main_loop"), &Engine::get_main_loop);
  1278. ClassDB::bind_method(D_METHOD("get_version_info"), &Engine::get_version_info);
  1279. ClassDB::bind_method(D_METHOD("get_author_info"), &Engine::get_author_info);
  1280. ClassDB::bind_method(D_METHOD("get_copyright_info"), &Engine::get_copyright_info);
  1281. ClassDB::bind_method(D_METHOD("get_donor_info"), &Engine::get_donor_info);
  1282. ClassDB::bind_method(D_METHOD("get_license_info"), &Engine::get_license_info);
  1283. ClassDB::bind_method(D_METHOD("get_license_text"), &Engine::get_license_text);
  1284. ClassDB::bind_method(D_METHOD("get_architecture_name"), &Engine::get_architecture_name);
  1285. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &Engine::is_in_physics_frame);
  1286. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &Engine::has_singleton);
  1287. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &Engine::get_singleton_object);
  1288. ClassDB::bind_method(D_METHOD("register_singleton", "name", "instance"), &Engine::register_singleton);
  1289. ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton);
  1290. ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list);
  1291. ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language);
  1292. ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count);
  1293. ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language);
  1294. ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint);
  1295. ClassDB::bind_method(D_METHOD("get_write_movie_path"), &Engine::get_write_movie_path);
  1296. ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages);
  1297. ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &Engine::is_printing_error_messages);
  1298. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
  1299. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second");
  1300. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  1301. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1302. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1303. }
  1304. Engine *Engine::singleton = nullptr;
  1305. ////// EngineDebugger //////
  1306. bool EngineDebugger::is_active() {
  1307. return ::EngineDebugger::is_active();
  1308. }
  1309. void EngineDebugger::register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler) {
  1310. ERR_FAIL_COND(p_profiler.is_null());
  1311. ERR_FAIL_COND_MSG(p_profiler->is_bound(), "Profiler already registered.");
  1312. ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler name already in use: " + p_name);
  1313. Error err = p_profiler->bind(p_name);
  1314. ERR_FAIL_COND_MSG(err != OK, "Profiler failed to register with error: " + itos(err));
  1315. profilers.insert(p_name, p_profiler);
  1316. }
  1317. void EngineDebugger::unregister_profiler(const StringName &p_name) {
  1318. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
  1319. profilers[p_name]->unbind();
  1320. profilers.erase(p_name);
  1321. }
  1322. bool EngineDebugger::is_profiling(const StringName &p_name) {
  1323. return ::EngineDebugger::is_profiling(p_name);
  1324. }
  1325. bool EngineDebugger::has_profiler(const StringName &p_name) {
  1326. return ::EngineDebugger::has_profiler(p_name);
  1327. }
  1328. void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  1329. ::EngineDebugger::profiler_add_frame_data(p_name, p_data);
  1330. }
  1331. void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  1332. if (::EngineDebugger::get_singleton()) {
  1333. ::EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
  1334. }
  1335. }
  1336. void EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1337. ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), "Capture already registered: " + p_name);
  1338. captures.insert(p_name, p_callable);
  1339. Callable &c = captures[p_name];
  1340. ::EngineDebugger::Capture capture(&c, &EngineDebugger::call_capture);
  1341. ::EngineDebugger::register_message_capture(p_name, capture);
  1342. }
  1343. void EngineDebugger::unregister_message_capture(const StringName &p_name) {
  1344. ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
  1345. ::EngineDebugger::unregister_message_capture(p_name);
  1346. captures.erase(p_name);
  1347. }
  1348. bool EngineDebugger::has_capture(const StringName &p_name) {
  1349. return ::EngineDebugger::has_capture(p_name);
  1350. }
  1351. void EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
  1352. ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send message. No active debugger");
  1353. ::EngineDebugger::get_singleton()->send_message(p_msg, p_data);
  1354. }
  1355. Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  1356. Callable &capture = *(Callable *)p_user;
  1357. if (capture.is_null()) {
  1358. return FAILED;
  1359. }
  1360. Variant cmd = p_cmd, data = p_data;
  1361. const Variant *args[2] = { &cmd, &data };
  1362. Variant retval;
  1363. Callable::CallError err;
  1364. capture.callp(args, 2, retval, err);
  1365. 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));
  1366. ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, "Error calling 'capture' to callable: " + String(capture) + ". Return type is not bool.");
  1367. r_captured = retval;
  1368. return OK;
  1369. }
  1370. EngineDebugger::~EngineDebugger() {
  1371. for (const KeyValue<StringName, Callable> &E : captures) {
  1372. ::EngineDebugger::unregister_message_capture(E.key);
  1373. }
  1374. captures.clear();
  1375. }
  1376. EngineDebugger *EngineDebugger::singleton = nullptr;
  1377. void EngineDebugger::_bind_methods() {
  1378. ClassDB::bind_method(D_METHOD("is_active"), &EngineDebugger::is_active);
  1379. ClassDB::bind_method(D_METHOD("register_profiler", "name", "profiler"), &EngineDebugger::register_profiler);
  1380. ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &EngineDebugger::unregister_profiler);
  1381. ClassDB::bind_method(D_METHOD("is_profiling", "name"), &EngineDebugger::is_profiling);
  1382. ClassDB::bind_method(D_METHOD("has_profiler", "name"), &EngineDebugger::has_profiler);
  1383. ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &EngineDebugger::profiler_add_frame_data);
  1384. ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &EngineDebugger::profiler_enable, DEFVAL(Array()));
  1385. ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &EngineDebugger::register_message_capture);
  1386. ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &EngineDebugger::unregister_message_capture);
  1387. ClassDB::bind_method(D_METHOD("has_capture", "name"), &EngineDebugger::has_capture);
  1388. ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &EngineDebugger::send_message);
  1389. }
  1390. } // namespace core_bind