core_bind.cpp 83 KB

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