core_bind.cpp 73 KB

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