core_bind.cpp 86 KB

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