core_bind.cpp 70 KB

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