core_bind.cpp 69 KB

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