core_bind.cpp 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  1. /*************************************************************************/
  2. /* core_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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, bool p_bold, bool p_italic) const {
  194. return ::OS::get_singleton()->get_system_font_path(p_font_name, p_bold, p_italic);
  195. }
  196. String OS::get_executable_path() const {
  197. return ::OS::get_singleton()->get_executable_path();
  198. }
  199. Error OS::shell_open(String p_uri) {
  200. if (p_uri.begins_with("res://")) {
  201. 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()`.");
  202. } else if (p_uri.begins_with("user://")) {
  203. 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()`.");
  204. }
  205. return ::OS::get_singleton()->shell_open(p_uri);
  206. }
  207. int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r_output, bool p_read_stderr, bool p_open_console) {
  208. List<String> args;
  209. for (int i = 0; i < p_arguments.size(); i++) {
  210. args.push_back(p_arguments[i]);
  211. }
  212. String pipe;
  213. int exitcode = 0;
  214. Error err = ::OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr, nullptr, p_open_console);
  215. r_output.push_back(pipe);
  216. if (err != OK) {
  217. return -1;
  218. }
  219. return exitcode;
  220. }
  221. int OS::create_instance(const Vector<String> &p_arguments) {
  222. List<String> args;
  223. for (int i = 0; i < p_arguments.size(); i++) {
  224. args.push_back(p_arguments[i]);
  225. }
  226. ::OS::ProcessID pid = 0;
  227. Error err = ::OS::get_singleton()->create_instance(args, &pid);
  228. if (err != OK) {
  229. return -1;
  230. }
  231. return pid;
  232. }
  233. int OS::create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console) {
  234. List<String> args;
  235. for (int i = 0; i < p_arguments.size(); i++) {
  236. args.push_back(p_arguments[i]);
  237. }
  238. ::OS::ProcessID pid = 0;
  239. Error err = ::OS::get_singleton()->create_process(p_path, args, &pid, p_open_console);
  240. if (err != OK) {
  241. return -1;
  242. }
  243. return pid;
  244. }
  245. Error OS::kill(int p_pid) {
  246. return ::OS::get_singleton()->kill(p_pid);
  247. }
  248. bool OS::is_process_running(int p_pid) const {
  249. return ::OS::get_singleton()->is_process_running(p_pid);
  250. }
  251. int OS::get_process_id() const {
  252. return ::OS::get_singleton()->get_process_id();
  253. }
  254. bool OS::has_environment(const String &p_var) const {
  255. return ::OS::get_singleton()->has_environment(p_var);
  256. }
  257. String OS::get_environment(const String &p_var) const {
  258. return ::OS::get_singleton()->get_environment(p_var);
  259. }
  260. bool OS::set_environment(const String &p_var, const String &p_value) const {
  261. return ::OS::get_singleton()->set_environment(p_var, p_value);
  262. }
  263. String OS::get_name() const {
  264. return ::OS::get_singleton()->get_name();
  265. }
  266. Vector<String> OS::get_cmdline_args() {
  267. List<String> cmdline = ::OS::get_singleton()->get_cmdline_args();
  268. Vector<String> cmdlinev;
  269. for (const String &E : cmdline) {
  270. cmdlinev.push_back(E);
  271. }
  272. return cmdlinev;
  273. }
  274. Vector<String> OS::get_cmdline_user_args() {
  275. List<String> cmdline = ::OS::get_singleton()->get_cmdline_user_args();
  276. Vector<String> cmdlinev;
  277. for (const String &E : cmdline) {
  278. cmdlinev.push_back(E);
  279. }
  280. return cmdlinev;
  281. }
  282. void OS::set_restart_on_exit(bool p_restart, const Vector<String> &p_restart_arguments) {
  283. List<String> args_list;
  284. for (const String &restart_argument : p_restart_arguments) {
  285. args_list.push_back(restart_argument);
  286. }
  287. ::OS::get_singleton()->set_restart_on_exit(p_restart, args_list);
  288. }
  289. bool OS::is_restart_on_exit_set() const {
  290. return ::OS::get_singleton()->is_restart_on_exit_set();
  291. }
  292. Vector<String> OS::get_restart_on_exit_arguments() const {
  293. List<String> args = ::OS::get_singleton()->get_restart_on_exit_arguments();
  294. Vector<String> args_vector;
  295. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  296. args_vector.push_back(E->get());
  297. }
  298. return args_vector;
  299. }
  300. String OS::get_locale() const {
  301. return ::OS::get_singleton()->get_locale();
  302. }
  303. String OS::get_locale_language() const {
  304. return ::OS::get_singleton()->get_locale_language();
  305. }
  306. String OS::get_model_name() const {
  307. return ::OS::get_singleton()->get_model_name();
  308. }
  309. Error OS::set_thread_name(const String &p_name) {
  310. return ::Thread::set_name(p_name);
  311. }
  312. ::Thread::ID OS::get_thread_caller_id() const {
  313. return ::Thread::get_caller_id();
  314. };
  315. ::Thread::ID OS::get_main_thread_id() const {
  316. return ::Thread::get_main_id();
  317. };
  318. bool OS::has_feature(const String &p_feature) const {
  319. return ::OS::get_singleton()->has_feature(p_feature);
  320. }
  321. uint64_t OS::get_static_memory_usage() const {
  322. return ::OS::get_singleton()->get_static_memory_usage();
  323. }
  324. uint64_t OS::get_static_memory_peak_usage() const {
  325. return ::OS::get_singleton()->get_static_memory_peak_usage();
  326. }
  327. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  328. void OS::delay_usec(int p_usec) const {
  329. ERR_FAIL_COND_MSG(
  330. p_usec < 0,
  331. vformat("Can't sleep for %d microseconds. The delay provided must be greater than or equal to 0 microseconds.", p_usec));
  332. ::OS::get_singleton()->delay_usec(p_usec);
  333. }
  334. /** This method uses a signed argument for better error reporting as it's used from the scripting API. */
  335. void OS::delay_msec(int p_msec) const {
  336. ERR_FAIL_COND_MSG(
  337. p_msec < 0,
  338. vformat("Can't sleep for %d milliseconds. The delay provided must be greater than or equal to 0 milliseconds.", p_msec));
  339. ::OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  340. }
  341. bool OS::can_use_threads() const {
  342. return ::OS::get_singleton()->can_use_threads();
  343. }
  344. bool OS::is_userfs_persistent() const {
  345. return ::OS::get_singleton()->is_userfs_persistent();
  346. }
  347. int OS::get_processor_count() const {
  348. return ::OS::get_singleton()->get_processor_count();
  349. }
  350. String OS::get_processor_name() const {
  351. return ::OS::get_singleton()->get_processor_name();
  352. }
  353. bool OS::is_stdout_verbose() const {
  354. return ::OS::get_singleton()->is_stdout_verbose();
  355. }
  356. void OS::dump_memory_to_file(const String &p_file) {
  357. ::OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
  358. }
  359. struct OSCoreBindImg {
  360. String path;
  361. Size2 size;
  362. int fmt = 0;
  363. ObjectID id;
  364. int vram = 0;
  365. bool operator<(const OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
  366. };
  367. void OS::print_all_textures_by_size() {
  368. List<OSCoreBindImg> imgs;
  369. uint64_t total = 0;
  370. {
  371. List<Ref<Resource>> rsrc;
  372. ResourceCache::get_cached_resources(&rsrc);
  373. for (Ref<Resource> &res : rsrc) {
  374. if (!res->is_class("Texture")) {
  375. continue;
  376. }
  377. Size2 size = res->call("get_size");
  378. int fmt = res->call("get_format");
  379. OSCoreBindImg img;
  380. img.size = size;
  381. img.fmt = fmt;
  382. img.path = res->get_path();
  383. img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt));
  384. img.id = res->get_instance_id();
  385. total += img.vram;
  386. imgs.push_back(img);
  387. }
  388. }
  389. imgs.sort();
  390. if (imgs.size() == 0) {
  391. print_line("No textures seem used in this project.");
  392. } else {
  393. print_line("Textures currently in use, sorted by VRAM usage:\n"
  394. "Path - VRAM usage (Dimensions)");
  395. }
  396. for (const OSCoreBindImg &img : imgs) {
  397. print_line(vformat("%s - %s %s",
  398. img.path,
  399. String::humanize_size(img.vram),
  400. img.size));
  401. }
  402. print_line(vformat("Total VRAM usage: %s.", String::humanize_size(total)));
  403. }
  404. void OS::print_resources_by_type(const Vector<String> &p_types) {
  405. ERR_FAIL_COND_MSG(p_types.size() == 0,
  406. "At least one type should be provided to print resources by type.");
  407. print_line(vformat("Resources currently in use for the following types: %s", p_types));
  408. RBMap<String, int> type_count;
  409. List<Ref<Resource>> resources;
  410. ResourceCache::get_cached_resources(&resources);
  411. for (const Ref<Resource> &r : resources) {
  412. bool found = false;
  413. for (int i = 0; i < p_types.size(); i++) {
  414. if (r->is_class(p_types[i])) {
  415. found = true;
  416. }
  417. }
  418. if (!found) {
  419. continue;
  420. }
  421. if (!type_count.has(r->get_class())) {
  422. type_count[r->get_class()] = 0;
  423. }
  424. type_count[r->get_class()]++;
  425. print_line(vformat("%s: %s", r->get_class(), r->get_path()));
  426. List<StringName> metas;
  427. r->get_meta_list(&metas);
  428. for (const StringName &meta : metas) {
  429. print_line(vformat(" %s: %s", meta, r->get_meta(meta)));
  430. }
  431. }
  432. for (const KeyValue<String, int> &E : type_count) {
  433. print_line(vformat("%s count: %d", E.key, E.value));
  434. }
  435. }
  436. void OS::print_all_resources(const String &p_to_file) {
  437. ::OS::get_singleton()->print_all_resources(p_to_file);
  438. }
  439. void OS::print_resources_in_use(bool p_short) {
  440. ::OS::get_singleton()->print_resources_in_use(p_short);
  441. }
  442. void OS::dump_resources_to_file(const String &p_file) {
  443. ::OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  444. }
  445. Error OS::move_to_trash(const String &p_path) const {
  446. return ::OS::get_singleton()->move_to_trash(p_path);
  447. }
  448. String OS::get_user_data_dir() const {
  449. return ::OS::get_singleton()->get_user_data_dir();
  450. }
  451. String OS::get_config_dir() const {
  452. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  453. return ::OS::get_singleton()->get_config_path();
  454. }
  455. String OS::get_data_dir() const {
  456. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  457. return ::OS::get_singleton()->get_data_path();
  458. }
  459. String OS::get_cache_dir() const {
  460. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  461. return ::OS::get_singleton()->get_cache_path();
  462. }
  463. bool OS::is_debug_build() const {
  464. #ifdef DEBUG_ENABLED
  465. return true;
  466. #else
  467. return false;
  468. #endif
  469. }
  470. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  471. return ::OS::get_singleton()->get_system_dir(::OS::SystemDir(p_dir), p_shared_storage);
  472. }
  473. String OS::get_keycode_string(Key p_code) const {
  474. return ::keycode_get_string(p_code);
  475. }
  476. bool OS::is_keycode_unicode(char32_t p_unicode) const {
  477. return ::keycode_has_unicode((Key)p_unicode);
  478. }
  479. Key OS::find_keycode_from_string(const String &p_code) const {
  480. return find_keycode(p_code);
  481. }
  482. bool OS::request_permission(const String &p_name) {
  483. return ::OS::get_singleton()->request_permission(p_name);
  484. }
  485. bool OS::request_permissions() {
  486. return ::OS::get_singleton()->request_permissions();
  487. }
  488. Vector<String> OS::get_granted_permissions() const {
  489. return ::OS::get_singleton()->get_granted_permissions();
  490. }
  491. String OS::get_unique_id() const {
  492. return ::OS::get_singleton()->get_unique_id();
  493. }
  494. OS *OS::singleton = nullptr;
  495. void OS::_bind_methods() {
  496. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &OS::get_connected_midi_inputs);
  497. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &OS::open_midi_inputs);
  498. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &OS::close_midi_inputs);
  499. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &OS::alert, DEFVAL("Alert!"));
  500. ClassDB::bind_method(D_METHOD("crash", "message"), &OS::crash);
  501. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &OS::set_low_processor_usage_mode);
  502. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &OS::is_in_low_processor_usage_mode);
  503. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &OS::set_low_processor_usage_mode_sleep_usec);
  504. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &OS::get_low_processor_usage_mode_sleep_usec);
  505. ClassDB::bind_method(D_METHOD("get_processor_count"), &OS::get_processor_count);
  506. ClassDB::bind_method(D_METHOD("get_processor_name"), &OS::get_processor_name);
  507. ClassDB::bind_method(D_METHOD("get_system_fonts"), &OS::get_system_fonts);
  508. ClassDB::bind_method(D_METHOD("get_system_font_path", "font_name", "bold", "italic"), &OS::get_system_font_path, DEFVAL(false), DEFVAL(false));
  509. ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
  510. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
  511. ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
  512. ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
  513. ClassDB::bind_method(D_METHOD("kill", "pid"), &OS::kill);
  514. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &OS::shell_open);
  515. ClassDB::bind_method(D_METHOD("is_process_running", "pid"), &OS::is_process_running);
  516. ClassDB::bind_method(D_METHOD("get_process_id"), &OS::get_process_id);
  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("has_environment", "variable"), &OS::has_environment);
  520. ClassDB::bind_method(D_METHOD("get_name"), &OS::get_name);
  521. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &OS::get_cmdline_args);
  522. ClassDB::bind_method(D_METHOD("get_cmdline_user_args"), &OS::get_cmdline_user_args);
  523. ClassDB::bind_method(D_METHOD("set_restart_on_exit", "restart", "arguments"), &OS::set_restart_on_exit, DEFVAL(Vector<String>()));
  524. ClassDB::bind_method(D_METHOD("is_restart_on_exit_set"), &OS::is_restart_on_exit_set);
  525. ClassDB::bind_method(D_METHOD("get_restart_on_exit_arguments"), &OS::get_restart_on_exit_arguments);
  526. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &OS::delay_usec);
  527. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &OS::delay_msec);
  528. ClassDB::bind_method(D_METHOD("get_locale"), &OS::get_locale);
  529. ClassDB::bind_method(D_METHOD("get_locale_language"), &OS::get_locale_language);
  530. ClassDB::bind_method(D_METHOD("get_model_name"), &OS::get_model_name);
  531. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &OS::is_userfs_persistent);
  532. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &OS::is_stdout_verbose);
  533. ClassDB::bind_method(D_METHOD("can_use_threads"), &OS::can_use_threads);
  534. ClassDB::bind_method(D_METHOD("is_debug_build"), &OS::is_debug_build);
  535. ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &OS::dump_memory_to_file);
  536. ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &OS::dump_resources_to_file);
  537. ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &OS::print_resources_in_use, DEFVAL(false));
  538. ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &OS::print_all_resources, DEFVAL(""));
  539. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &OS::get_static_memory_usage);
  540. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &OS::get_static_memory_peak_usage);
  541. ClassDB::bind_method(D_METHOD("move_to_trash", "path"), &OS::move_to_trash);
  542. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &OS::get_user_data_dir);
  543. ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &OS::get_system_dir, DEFVAL(true));
  544. ClassDB::bind_method(D_METHOD("get_config_dir"), &OS::get_config_dir);
  545. ClassDB::bind_method(D_METHOD("get_data_dir"), &OS::get_data_dir);
  546. ClassDB::bind_method(D_METHOD("get_cache_dir"), &OS::get_cache_dir);
  547. ClassDB::bind_method(D_METHOD("get_unique_id"), &OS::get_unique_id);
  548. ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &OS::print_all_textures_by_size);
  549. ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &OS::print_resources_by_type);
  550. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &OS::get_keycode_string);
  551. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &OS::is_keycode_unicode);
  552. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &OS::find_keycode_from_string);
  553. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &OS::set_use_file_access_save_and_swap);
  554. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &OS::set_thread_name);
  555. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &OS::get_thread_caller_id);
  556. ClassDB::bind_method(D_METHOD("get_main_thread_id"), &OS::get_main_thread_id);
  557. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &OS::has_feature);
  558. ClassDB::bind_method(D_METHOD("request_permission", "name"), &OS::request_permission);
  559. ClassDB::bind_method(D_METHOD("request_permissions"), &OS::request_permissions);
  560. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &OS::get_granted_permissions);
  561. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  562. 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");
  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(VIDEO_DRIVER_VULKAN);
  568. BIND_ENUM_CONSTANT(VIDEO_DRIVER_OPENGL_3);
  569. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  570. BIND_ENUM_CONSTANT(DAY_MONDAY);
  571. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  572. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  573. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  574. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  575. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  576. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  577. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  578. BIND_ENUM_CONSTANT(MONTH_MARCH);
  579. BIND_ENUM_CONSTANT(MONTH_APRIL);
  580. BIND_ENUM_CONSTANT(MONTH_MAY);
  581. BIND_ENUM_CONSTANT(MONTH_JUNE);
  582. BIND_ENUM_CONSTANT(MONTH_JULY);
  583. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  584. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  585. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  586. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  587. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  588. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  589. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  590. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  591. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  592. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  593. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  594. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  595. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  596. }
  597. ////// Geometry2D //////
  598. Geometry2D *Geometry2D::singleton = nullptr;
  599. Geometry2D *Geometry2D::get_singleton() {
  600. return singleton;
  601. }
  602. bool Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  603. return ::Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  604. }
  605. real_t Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  606. return ::Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  607. }
  608. 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) {
  609. Vector2 result;
  610. if (::Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  611. return result;
  612. } else {
  613. return Variant();
  614. }
  615. }
  616. 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) {
  617. Vector2 result;
  618. if (::Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  619. return result;
  620. } else {
  621. return Variant();
  622. }
  623. }
  624. Vector<Vector2> Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  625. Vector2 r1, r2;
  626. ::Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  627. Vector<Vector2> r = { r1, r2 };
  628. return r;
  629. }
  630. Vector2 Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  631. Vector2 s[2] = { p_a, p_b };
  632. return ::Geometry2D::get_closest_point_to_segment(p_point, s);
  633. }
  634. Vector2 Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  635. Vector2 s[2] = { p_a, p_b };
  636. return ::Geometry2D::get_closest_point_to_segment_uncapped(p_point, s);
  637. }
  638. bool Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  639. return ::Geometry2D::is_point_in_triangle(s, a, b, c);
  640. }
  641. bool Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  642. return ::Geometry2D::is_polygon_clockwise(p_polygon);
  643. }
  644. bool Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  645. return ::Geometry2D::is_point_in_polygon(p_point, p_polygon);
  646. }
  647. Vector<int> Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  648. return ::Geometry2D::triangulate_polygon(p_polygon);
  649. }
  650. Vector<int> Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
  651. return ::Geometry2D::triangulate_delaunay(p_points);
  652. }
  653. Vector<Point2> Geometry2D::convex_hull(const Vector<Point2> &p_points) {
  654. return ::Geometry2D::convex_hull(p_points);
  655. }
  656. TypedArray<PackedVector2Array> Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  657. Vector<Vector<Point2>> polys = ::Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
  658. Array ret;
  659. for (int i = 0; i < polys.size(); ++i) {
  660. ret.push_back(polys[i]);
  661. }
  662. return ret;
  663. }
  664. TypedArray<PackedVector2Array> Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  665. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
  666. TypedArray<PackedVector2Array> ret;
  667. for (int i = 0; i < polys.size(); ++i) {
  668. ret.push_back(polys[i]);
  669. }
  670. return ret;
  671. }
  672. TypedArray<PackedVector2Array> Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  673. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
  674. Array ret;
  675. for (int i = 0; i < polys.size(); ++i) {
  676. ret.push_back(polys[i]);
  677. }
  678. return ret;
  679. }
  680. TypedArray<PackedVector2Array> Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  681. Vector<Vector<Point2>> polys = ::Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b);
  682. Array ret;
  683. for (int i = 0; i < polys.size(); ++i) {
  684. ret.push_back(polys[i]);
  685. }
  686. return ret;
  687. }
  688. TypedArray<PackedVector2Array> Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  689. Vector<Vector<Point2>> polys = ::Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
  690. Array ret;
  691. for (int i = 0; i < polys.size(); ++i) {
  692. ret.push_back(polys[i]);
  693. }
  694. return ret;
  695. }
  696. TypedArray<PackedVector2Array> Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  697. Vector<Vector<Point2>> polys = ::Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon);
  698. Array ret;
  699. for (int i = 0; i < polys.size(); ++i) {
  700. ret.push_back(polys[i]);
  701. }
  702. return ret;
  703. }
  704. TypedArray<PackedVector2Array> Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  705. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polygon(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type));
  706. Array ret;
  707. for (int i = 0; i < polys.size(); ++i) {
  708. ret.push_back(polys[i]);
  709. }
  710. return ret;
  711. }
  712. TypedArray<PackedVector2Array> Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  713. Vector<Vector<Point2>> polys = ::Geometry2D::offset_polyline(p_polygon, p_delta, ::Geometry2D::PolyJoinType(p_join_type), ::Geometry2D::PolyEndType(p_end_type));
  714. Array ret;
  715. for (int i = 0; i < polys.size(); ++i) {
  716. ret.push_back(polys[i]);
  717. }
  718. return ret;
  719. }
  720. Dictionary Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
  721. Dictionary ret;
  722. Vector<Size2i> rects;
  723. for (int i = 0; i < p_rects.size(); i++) {
  724. rects.push_back(p_rects[i]);
  725. }
  726. Vector<Point2i> result;
  727. Size2i size;
  728. ::Geometry2D::make_atlas(rects, result, size);
  729. Size2 r_size = size;
  730. Vector<Point2> r_result;
  731. for (int i = 0; i < result.size(); i++) {
  732. r_result.push_back(result[i]);
  733. }
  734. ret["points"] = r_result;
  735. ret["size"] = r_size;
  736. return ret;
  737. }
  738. void Geometry2D::_bind_methods() {
  739. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &Geometry2D::is_point_in_circle);
  740. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &Geometry2D::segment_intersects_circle);
  741. ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &Geometry2D::segment_intersects_segment);
  742. ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &Geometry2D::line_intersects_line);
  743. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &Geometry2D::get_closest_points_between_segments);
  744. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment);
  745. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry2D::get_closest_point_to_segment_uncapped);
  746. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &Geometry2D::point_is_inside_triangle);
  747. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &Geometry2D::is_polygon_clockwise);
  748. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &Geometry2D::is_point_in_polygon);
  749. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &Geometry2D::triangulate_polygon);
  750. ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &Geometry2D::triangulate_delaunay);
  751. ClassDB::bind_method(D_METHOD("convex_hull", "points"), &Geometry2D::convex_hull);
  752. ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &Geometry2D::merge_polygons);
  753. ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &Geometry2D::clip_polygons);
  754. ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &Geometry2D::intersect_polygons);
  755. ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &Geometry2D::exclude_polygons);
  756. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::clip_polyline_with_polygon);
  757. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &Geometry2D::intersect_polyline_with_polygon);
  758. ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
  759. ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  760. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &Geometry2D::make_atlas);
  761. BIND_ENUM_CONSTANT(OPERATION_UNION);
  762. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  763. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  764. BIND_ENUM_CONSTANT(OPERATION_XOR);
  765. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  766. BIND_ENUM_CONSTANT(JOIN_ROUND);
  767. BIND_ENUM_CONSTANT(JOIN_MITER);
  768. BIND_ENUM_CONSTANT(END_POLYGON);
  769. BIND_ENUM_CONSTANT(END_JOINED);
  770. BIND_ENUM_CONSTANT(END_BUTT);
  771. BIND_ENUM_CONSTANT(END_SQUARE);
  772. BIND_ENUM_CONSTANT(END_ROUND);
  773. }
  774. ////// Geometry3D //////
  775. Geometry3D *Geometry3D::singleton = nullptr;
  776. Geometry3D *Geometry3D::get_singleton() {
  777. return singleton;
  778. }
  779. TypedArray<Plane> Geometry3D::build_box_planes(const Vector3 &p_extents) {
  780. Variant ret = ::Geometry3D::build_box_planes(p_extents);
  781. return ret;
  782. }
  783. TypedArray<Plane> Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  784. Variant ret = ::Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  785. return ret;
  786. }
  787. TypedArray<Plane> Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  788. Variant ret = ::Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  789. return ret;
  790. }
  791. Vector<Vector3> Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  792. Vector3 r1, r2;
  793. ::Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  794. Vector<Vector3> r = { r1, r2 };
  795. return r;
  796. }
  797. Vector3 Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  798. Vector3 s[2] = { p_a, p_b };
  799. return ::Geometry3D::get_closest_point_to_segment(p_point, s);
  800. }
  801. Vector3 Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  802. Vector3 s[2] = { p_a, p_b };
  803. return ::Geometry3D::get_closest_point_to_segment_uncapped(p_point, s);
  804. }
  805. 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) {
  806. Vector3 res;
  807. if (::Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  808. return res;
  809. } else {
  810. return Variant();
  811. }
  812. }
  813. 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) {
  814. Vector3 res;
  815. if (::Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  816. return res;
  817. } else {
  818. return Variant();
  819. }
  820. }
  821. Vector<Vector3> Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  822. Vector<Vector3> r;
  823. Vector3 res, norm;
  824. if (!::Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  825. return r;
  826. }
  827. r.resize(2);
  828. r.set(0, res);
  829. r.set(1, norm);
  830. return r;
  831. }
  832. Vector<Vector3> Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  833. Vector<Vector3> r;
  834. Vector3 res, norm;
  835. if (!::Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  836. return r;
  837. }
  838. r.resize(2);
  839. r.set(0, res);
  840. r.set(1, norm);
  841. return r;
  842. }
  843. Vector<Vector3> Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  844. Vector<Vector3> r;
  845. Vector3 res, norm;
  846. if (!::Geometry3D::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm)) {
  847. return r;
  848. }
  849. r.resize(2);
  850. r.set(0, res);
  851. r.set(1, norm);
  852. return r;
  853. }
  854. Vector<Vector3> Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  855. return ::Geometry3D::clip_polygon(p_points, p_plane);
  856. }
  857. void Geometry3D::_bind_methods() {
  858. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &Geometry3D::build_box_planes);
  859. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  860. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  861. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &Geometry3D::get_closest_points_between_segments);
  862. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment);
  863. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &Geometry3D::get_closest_point_to_segment_uncapped);
  864. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &Geometry3D::ray_intersects_triangle);
  865. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &Geometry3D::segment_intersects_triangle);
  866. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &Geometry3D::segment_intersects_sphere);
  867. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &Geometry3D::segment_intersects_cylinder);
  868. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &Geometry3D::segment_intersects_convex);
  869. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &Geometry3D::clip_polygon);
  870. }
  871. ////// File //////
  872. Error File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  873. Error err = open(p_path, p_mode_flags);
  874. if (err) {
  875. return err;
  876. }
  877. Ref<FileAccessEncrypted> fae;
  878. fae.instantiate();
  879. err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  880. if (err) {
  881. close();
  882. return err;
  883. }
  884. f = fae;
  885. return OK;
  886. }
  887. Error File::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  888. Error err = open(p_path, p_mode_flags);
  889. if (err) {
  890. return err;
  891. }
  892. Ref<FileAccessEncrypted> fae;
  893. fae.instantiate();
  894. err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  895. if (err) {
  896. close();
  897. return err;
  898. }
  899. f = fae;
  900. return OK;
  901. }
  902. Error File::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  903. Ref<FileAccessCompressed> fac;
  904. fac.instantiate();
  905. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  906. Error err = fac->_open(p_path, p_mode_flags);
  907. if (err) {
  908. return err;
  909. }
  910. f = fac;
  911. return OK;
  912. }
  913. Error File::open(const String &p_path, ModeFlags p_mode_flags) {
  914. Error err;
  915. f = FileAccess::open(p_path, p_mode_flags, &err);
  916. if (f.is_valid()) {
  917. f->set_big_endian(big_endian);
  918. }
  919. return err;
  920. }
  921. void File::flush() {
  922. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before flushing.");
  923. f->flush();
  924. }
  925. void File::close() {
  926. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened.");
  927. f.unref();
  928. }
  929. bool File::is_open() const {
  930. return f != nullptr;
  931. }
  932. String File::get_path() const {
  933. ERR_FAIL_COND_V_MSG(f.is_null(), "", "File must be opened before use, or is lacking read-write permission.");
  934. return f->get_path();
  935. }
  936. String File::get_path_absolute() const {
  937. ERR_FAIL_COND_V_MSG(f.is_null(), "", "File must be opened before use, or is lacking read-write permission.");
  938. return f->get_path_absolute();
  939. }
  940. void File::seek(int64_t p_position) {
  941. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  942. ERR_FAIL_COND_MSG(p_position < 0, "Seek position must be a positive integer.");
  943. f->seek(p_position);
  944. }
  945. void File::seek_end(int64_t p_position) {
  946. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  947. f->seek_end(p_position);
  948. }
  949. uint64_t File::get_position() const {
  950. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  951. return f->get_position();
  952. }
  953. uint64_t File::get_length() const {
  954. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  955. return f->get_length();
  956. }
  957. bool File::eof_reached() const {
  958. ERR_FAIL_COND_V_MSG(f.is_null(), false, "File must be opened before use, or is lacking read-write permission.");
  959. return f->eof_reached();
  960. }
  961. uint8_t File::get_8() const {
  962. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  963. return f->get_8();
  964. }
  965. uint16_t File::get_16() const {
  966. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  967. return f->get_16();
  968. }
  969. uint32_t File::get_32() const {
  970. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  971. return f->get_32();
  972. }
  973. uint64_t File::get_64() const {
  974. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  975. return f->get_64();
  976. }
  977. float File::get_float() const {
  978. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  979. return f->get_float();
  980. }
  981. double File::get_double() const {
  982. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  983. return f->get_double();
  984. }
  985. real_t File::get_real() const {
  986. ERR_FAIL_COND_V_MSG(f.is_null(), 0, "File must be opened before use, or is lacking read-write permission.");
  987. return f->get_real();
  988. }
  989. Vector<uint8_t> File::get_buffer(int64_t p_length) const {
  990. Vector<uint8_t> data;
  991. ERR_FAIL_COND_V_MSG(f.is_null(), data, "File must be opened before use, or is lacking read-write permission.");
  992. ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0.");
  993. if (p_length == 0) {
  994. return data;
  995. }
  996. Error err = data.resize(p_length);
  997. ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");
  998. uint8_t *w = data.ptrw();
  999. int64_t len = f->get_buffer(&w[0], p_length);
  1000. if (len < p_length) {
  1001. data.resize(len);
  1002. }
  1003. return data;
  1004. }
  1005. String File::get_as_text(bool p_skip_cr) const {
  1006. ERR_FAIL_COND_V_MSG(f.is_null(), String(), "File must be opened before use, or is lacking read-write permission.");
  1007. uint64_t original_pos = f->get_position();
  1008. const_cast<FileAccess *>(*f)->seek(0);
  1009. String text = f->get_as_utf8_string(p_skip_cr);
  1010. const_cast<FileAccess *>(*f)->seek(original_pos);
  1011. return text;
  1012. }
  1013. String File::get_md5(const String &p_path) const {
  1014. return FileAccess::get_md5(p_path);
  1015. }
  1016. String File::get_sha256(const String &p_path) const {
  1017. return FileAccess::get_sha256(p_path);
  1018. }
  1019. String File::get_line() const {
  1020. ERR_FAIL_COND_V_MSG(f.is_null(), String(), "File must be opened before use, or is lacking read-write permission.");
  1021. return f->get_line();
  1022. }
  1023. Vector<String> File::get_csv_line(const String &p_delim) const {
  1024. ERR_FAIL_COND_V_MSG(f.is_null(), Vector<String>(), "File must be opened before use, or is lacking read-write permission.");
  1025. return f->get_csv_line(p_delim);
  1026. }
  1027. /**< use this for files WRITTEN in _big_ endian machines (i.e. amiga/mac)
  1028. * It's not about the current CPU type but file formats.
  1029. * These flags get reset to false (little endian) on each open
  1030. */
  1031. void File::set_big_endian(bool p_big_endian) {
  1032. big_endian = p_big_endian;
  1033. if (f.is_valid()) {
  1034. f->set_big_endian(p_big_endian);
  1035. }
  1036. }
  1037. bool File::is_big_endian() {
  1038. return big_endian;
  1039. }
  1040. Error File::get_error() const {
  1041. if (f.is_null()) {
  1042. return ERR_UNCONFIGURED;
  1043. }
  1044. return f->get_error();
  1045. }
  1046. void File::store_8(uint8_t p_dest) {
  1047. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1048. f->store_8(p_dest);
  1049. }
  1050. void File::store_16(uint16_t p_dest) {
  1051. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1052. f->store_16(p_dest);
  1053. }
  1054. void File::store_32(uint32_t p_dest) {
  1055. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1056. f->store_32(p_dest);
  1057. }
  1058. void File::store_64(uint64_t p_dest) {
  1059. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1060. f->store_64(p_dest);
  1061. }
  1062. void File::store_float(float p_dest) {
  1063. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1064. f->store_float(p_dest);
  1065. }
  1066. void File::store_double(double p_dest) {
  1067. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1068. f->store_double(p_dest);
  1069. }
  1070. void File::store_real(real_t p_real) {
  1071. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1072. f->store_real(p_real);
  1073. }
  1074. void File::store_string(const String &p_string) {
  1075. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1076. f->store_string(p_string);
  1077. }
  1078. void File::store_pascal_string(const String &p_string) {
  1079. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1080. f->store_pascal_string(p_string);
  1081. }
  1082. String File::get_pascal_string() {
  1083. ERR_FAIL_COND_V_MSG(f.is_null(), "", "File must be opened before use, or is lacking read-write permission.");
  1084. return f->get_pascal_string();
  1085. }
  1086. void File::store_line(const String &p_string) {
  1087. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1088. f->store_line(p_string);
  1089. }
  1090. void File::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  1091. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1092. f->store_csv_line(p_values, p_delim);
  1093. }
  1094. void File::store_buffer(const Vector<uint8_t> &p_buffer) {
  1095. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1096. uint64_t len = p_buffer.size();
  1097. if (len == 0) {
  1098. return;
  1099. }
  1100. const uint8_t *r = p_buffer.ptr();
  1101. f->store_buffer(&r[0], len);
  1102. }
  1103. bool File::file_exists(const String &p_name) {
  1104. return FileAccess::exists(p_name);
  1105. }
  1106. void File::store_var(const Variant &p_var, bool p_full_objects) {
  1107. ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use, or is lacking read-write permission.");
  1108. int len;
  1109. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1110. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1111. Vector<uint8_t> buff;
  1112. buff.resize(len);
  1113. uint8_t *w = buff.ptrw();
  1114. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1115. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1116. store_32(len);
  1117. store_buffer(buff);
  1118. }
  1119. Variant File::get_var(bool p_allow_objects) const {
  1120. ERR_FAIL_COND_V_MSG(f.is_null(), Variant(), "File must be opened before use, or is lacking read-write permission.");
  1121. uint32_t len = get_32();
  1122. Vector<uint8_t> buff = get_buffer(len);
  1123. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  1124. const uint8_t *r = buff.ptr();
  1125. Variant v;
  1126. Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects);
  1127. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant.");
  1128. return v;
  1129. }
  1130. uint64_t File::get_modified_time(const String &p_file) const {
  1131. return FileAccess::get_modified_time(p_file);
  1132. }
  1133. void File::_bind_methods() {
  1134. ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &File::open_encrypted);
  1135. ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &File::open_encrypted_pass);
  1136. ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &File::open_compressed, DEFVAL(0));
  1137. ClassDB::bind_method(D_METHOD("open", "path", "flags"), &File::open);
  1138. ClassDB::bind_method(D_METHOD("flush"), &File::flush);
  1139. ClassDB::bind_method(D_METHOD("close"), &File::close);
  1140. ClassDB::bind_method(D_METHOD("get_path"), &File::get_path);
  1141. ClassDB::bind_method(D_METHOD("get_path_absolute"), &File::get_path_absolute);
  1142. ClassDB::bind_method(D_METHOD("is_open"), &File::is_open);
  1143. ClassDB::bind_method(D_METHOD("seek", "position"), &File::seek);
  1144. ClassDB::bind_method(D_METHOD("seek_end", "position"), &File::seek_end, DEFVAL(0));
  1145. ClassDB::bind_method(D_METHOD("get_position"), &File::get_position);
  1146. ClassDB::bind_method(D_METHOD("get_length"), &File::get_length);
  1147. ClassDB::bind_method(D_METHOD("eof_reached"), &File::eof_reached);
  1148. ClassDB::bind_method(D_METHOD("get_8"), &File::get_8);
  1149. ClassDB::bind_method(D_METHOD("get_16"), &File::get_16);
  1150. ClassDB::bind_method(D_METHOD("get_32"), &File::get_32);
  1151. ClassDB::bind_method(D_METHOD("get_64"), &File::get_64);
  1152. ClassDB::bind_method(D_METHOD("get_float"), &File::get_float);
  1153. ClassDB::bind_method(D_METHOD("get_double"), &File::get_double);
  1154. ClassDB::bind_method(D_METHOD("get_real"), &File::get_real);
  1155. ClassDB::bind_method(D_METHOD("get_buffer", "length"), &File::get_buffer);
  1156. ClassDB::bind_method(D_METHOD("get_line"), &File::get_line);
  1157. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &File::get_csv_line, DEFVAL(","));
  1158. ClassDB::bind_method(D_METHOD("get_as_text", "skip_cr"), &File::get_as_text, DEFVAL(false));
  1159. ClassDB::bind_method(D_METHOD("get_md5", "path"), &File::get_md5);
  1160. ClassDB::bind_method(D_METHOD("get_sha256", "path"), &File::get_sha256);
  1161. ClassDB::bind_method(D_METHOD("is_big_endian"), &File::is_big_endian);
  1162. ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &File::set_big_endian);
  1163. ClassDB::bind_method(D_METHOD("get_error"), &File::get_error);
  1164. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &File::get_var, DEFVAL(false));
  1165. ClassDB::bind_method(D_METHOD("store_8", "value"), &File::store_8);
  1166. ClassDB::bind_method(D_METHOD("store_16", "value"), &File::store_16);
  1167. ClassDB::bind_method(D_METHOD("store_32", "value"), &File::store_32);
  1168. ClassDB::bind_method(D_METHOD("store_64", "value"), &File::store_64);
  1169. ClassDB::bind_method(D_METHOD("store_float", "value"), &File::store_float);
  1170. ClassDB::bind_method(D_METHOD("store_double", "value"), &File::store_double);
  1171. ClassDB::bind_method(D_METHOD("store_real", "value"), &File::store_real);
  1172. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &File::store_buffer);
  1173. ClassDB::bind_method(D_METHOD("store_line", "line"), &File::store_line);
  1174. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &File::store_csv_line, DEFVAL(","));
  1175. ClassDB::bind_method(D_METHOD("store_string", "string"), &File::store_string);
  1176. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &File::store_var, DEFVAL(false));
  1177. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &File::store_pascal_string);
  1178. ClassDB::bind_method(D_METHOD("get_pascal_string"), &File::get_pascal_string);
  1179. ClassDB::bind_static_method("File", D_METHOD("file_exists", "path"), &File::file_exists);
  1180. ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &File::get_modified_time);
  1181. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian");
  1182. BIND_ENUM_CONSTANT(READ);
  1183. BIND_ENUM_CONSTANT(WRITE);
  1184. BIND_ENUM_CONSTANT(READ_WRITE);
  1185. BIND_ENUM_CONSTANT(WRITE_READ);
  1186. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  1187. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  1188. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  1189. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  1190. }
  1191. ////// Directory //////
  1192. Error Directory::open(const String &p_path) {
  1193. Error err;
  1194. Ref<DirAccess> alt = DirAccess::open(p_path, &err);
  1195. if (alt.is_null()) {
  1196. return err;
  1197. }
  1198. d = alt;
  1199. dir_open = true;
  1200. return OK;
  1201. }
  1202. bool Directory::is_open() const {
  1203. return d.is_valid() && dir_open;
  1204. }
  1205. Error Directory::list_dir_begin() {
  1206. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1207. return d->list_dir_begin();
  1208. }
  1209. String Directory::get_next() {
  1210. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1211. String next = d->get_next();
  1212. while (!next.is_empty() && ((!include_navigational && (next == "." || next == "..")) || (!include_hidden && d->current_is_hidden()))) {
  1213. next = d->get_next();
  1214. }
  1215. return next;
  1216. }
  1217. bool Directory::current_is_dir() const {
  1218. ERR_FAIL_COND_V_MSG(!is_open(), false, "Directory must be opened before use.");
  1219. return d->current_is_dir();
  1220. }
  1221. void Directory::list_dir_end() {
  1222. ERR_FAIL_COND_MSG(!is_open(), "Directory must be opened before use.");
  1223. d->list_dir_end();
  1224. }
  1225. PackedStringArray Directory::get_files() {
  1226. return _get_contents(false);
  1227. }
  1228. PackedStringArray Directory::get_directories() {
  1229. return _get_contents(true);
  1230. }
  1231. PackedStringArray Directory::_get_contents(bool p_directories) {
  1232. PackedStringArray ret;
  1233. ERR_FAIL_COND_V_MSG(!is_open(), ret, "Directory must be opened before use.");
  1234. list_dir_begin();
  1235. String s = get_next();
  1236. while (!s.is_empty()) {
  1237. if (current_is_dir() == p_directories) {
  1238. ret.append(s);
  1239. }
  1240. s = get_next();
  1241. }
  1242. ret.sort();
  1243. return ret;
  1244. }
  1245. void Directory::set_include_navigational(bool p_enable) {
  1246. include_navigational = p_enable;
  1247. }
  1248. bool Directory::get_include_navigational() const {
  1249. return include_navigational;
  1250. }
  1251. void Directory::set_include_hidden(bool p_enable) {
  1252. include_hidden = p_enable;
  1253. }
  1254. bool Directory::get_include_hidden() const {
  1255. return include_hidden;
  1256. }
  1257. int Directory::get_drive_count() {
  1258. ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use.");
  1259. return d->get_drive_count();
  1260. }
  1261. String Directory::get_drive(int p_drive) {
  1262. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1263. return d->get_drive(p_drive);
  1264. }
  1265. int Directory::get_current_drive() {
  1266. ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use.");
  1267. return d->get_current_drive();
  1268. }
  1269. Error Directory::change_dir(String p_dir) {
  1270. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_UNCONFIGURED, "Directory is not configured properly.");
  1271. Error err = d->change_dir(p_dir);
  1272. if (err != OK) {
  1273. return err;
  1274. }
  1275. dir_open = true;
  1276. return OK;
  1277. }
  1278. String Directory::get_current_dir() {
  1279. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1280. return d->get_current_dir();
  1281. }
  1282. Error Directory::make_dir(String p_dir) {
  1283. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_UNCONFIGURED, "Directory is not configured properly.");
  1284. if (!p_dir.is_relative_path()) {
  1285. Ref<DirAccess> da = DirAccess::create_for_path(p_dir);
  1286. return da->make_dir(p_dir);
  1287. }
  1288. return d->make_dir(p_dir);
  1289. }
  1290. Error Directory::make_dir_recursive(String p_dir) {
  1291. ERR_FAIL_COND_V_MSG(d.is_null(), ERR_UNCONFIGURED, "Directory is not configured properly.");
  1292. if (!p_dir.is_relative_path()) {
  1293. Ref<DirAccess> da = DirAccess::create_for_path(p_dir);
  1294. return da->make_dir_recursive(p_dir);
  1295. }
  1296. return d->make_dir_recursive(p_dir);
  1297. }
  1298. bool Directory::file_exists(String p_file) {
  1299. ERR_FAIL_COND_V_MSG(d.is_null(), false, "Directory is not configured properly.");
  1300. if (!p_file.is_relative_path()) {
  1301. return FileAccess::exists(p_file);
  1302. }
  1303. return d->file_exists(p_file);
  1304. }
  1305. bool Directory::dir_exists(String p_dir) {
  1306. ERR_FAIL_COND_V_MSG(d.is_null(), false, "Directory is not configured properly.");
  1307. if (!p_dir.is_relative_path()) {
  1308. return DirAccess::exists(p_dir);
  1309. }
  1310. return d->dir_exists(p_dir);
  1311. }
  1312. uint64_t Directory::get_space_left() {
  1313. ERR_FAIL_COND_V_MSG(d.is_null(), 0, "Directory must be opened before use.");
  1314. return d->get_space_left() / 1024 * 1024; // Truncate to closest MiB.
  1315. }
  1316. Error Directory::copy(String p_from, String p_to) {
  1317. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1318. return d->copy(p_from, p_to);
  1319. }
  1320. Error Directory::rename(String p_from, String p_to) {
  1321. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1322. ERR_FAIL_COND_V_MSG(p_from.is_empty() || p_from == "." || p_from == "..", ERR_INVALID_PARAMETER, "Invalid path to rename.");
  1323. if (!p_from.is_relative_path()) {
  1324. Ref<DirAccess> da = DirAccess::create_for_path(p_from);
  1325. ERR_FAIL_COND_V_MSG(!da->file_exists(p_from) && !da->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
  1326. return da->rename(p_from, p_to);
  1327. }
  1328. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from) && !d->dir_exists(p_from), ERR_DOES_NOT_EXIST, "File or directory does not exist.");
  1329. return d->rename(p_from, p_to);
  1330. }
  1331. Error Directory::remove(String p_name) {
  1332. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1333. if (!p_name.is_relative_path()) {
  1334. Ref<DirAccess> da = DirAccess::create_for_path(p_name);
  1335. return da->remove(p_name);
  1336. }
  1337. return d->remove(p_name);
  1338. }
  1339. void Directory::_bind_methods() {
  1340. ClassDB::bind_method(D_METHOD("open", "path"), &Directory::open);
  1341. ClassDB::bind_method(D_METHOD("list_dir_begin"), &Directory::list_dir_begin, DEFVAL(false), DEFVAL(false));
  1342. ClassDB::bind_method(D_METHOD("get_next"), &Directory::get_next);
  1343. ClassDB::bind_method(D_METHOD("current_is_dir"), &Directory::current_is_dir);
  1344. ClassDB::bind_method(D_METHOD("list_dir_end"), &Directory::list_dir_end);
  1345. ClassDB::bind_method(D_METHOD("get_files"), &Directory::get_files);
  1346. ClassDB::bind_method(D_METHOD("get_directories"), &Directory::get_directories);
  1347. ClassDB::bind_method(D_METHOD("get_drive_count"), &Directory::get_drive_count);
  1348. ClassDB::bind_method(D_METHOD("get_drive", "idx"), &Directory::get_drive);
  1349. ClassDB::bind_method(D_METHOD("get_current_drive"), &Directory::get_current_drive);
  1350. ClassDB::bind_method(D_METHOD("change_dir", "todir"), &Directory::change_dir);
  1351. ClassDB::bind_method(D_METHOD("get_current_dir"), &Directory::get_current_dir);
  1352. ClassDB::bind_method(D_METHOD("make_dir", "path"), &Directory::make_dir);
  1353. ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &Directory::make_dir_recursive);
  1354. ClassDB::bind_method(D_METHOD("file_exists", "path"), &Directory::file_exists);
  1355. ClassDB::bind_method(D_METHOD("dir_exists", "path"), &Directory::dir_exists);
  1356. ClassDB::bind_method(D_METHOD("get_space_left"), &Directory::get_space_left);
  1357. ClassDB::bind_method(D_METHOD("copy", "from", "to"), &Directory::copy);
  1358. ClassDB::bind_method(D_METHOD("rename", "from", "to"), &Directory::rename);
  1359. ClassDB::bind_method(D_METHOD("remove", "path"), &Directory::remove);
  1360. ClassDB::bind_method(D_METHOD("set_include_navigational", "enable"), &Directory::set_include_navigational);
  1361. ClassDB::bind_method(D_METHOD("get_include_navigational"), &Directory::get_include_navigational);
  1362. ClassDB::bind_method(D_METHOD("set_include_hidden", "enable"), &Directory::set_include_hidden);
  1363. ClassDB::bind_method(D_METHOD("get_include_hidden"), &Directory::get_include_hidden);
  1364. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_navigational"), "set_include_navigational", "get_include_navigational");
  1365. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_hidden"), "set_include_hidden", "get_include_hidden");
  1366. }
  1367. Directory::Directory() {
  1368. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1369. }
  1370. ////// Marshalls //////
  1371. Marshalls *Marshalls::singleton = nullptr;
  1372. Marshalls *Marshalls::get_singleton() {
  1373. return singleton;
  1374. }
  1375. String Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  1376. int len;
  1377. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1378. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1379. Vector<uint8_t> buff;
  1380. buff.resize(len);
  1381. uint8_t *w = buff.ptrw();
  1382. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1383. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1384. String ret = CryptoCore::b64_encode_str(&w[0], len);
  1385. ERR_FAIL_COND_V(ret.is_empty(), ret);
  1386. return ret;
  1387. }
  1388. Variant Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  1389. int strlen = p_str.length();
  1390. CharString cstr = p_str.ascii();
  1391. Vector<uint8_t> buf;
  1392. buf.resize(strlen / 4 * 3 + 1);
  1393. uint8_t *w = buf.ptrw();
  1394. size_t len = 0;
  1395. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  1396. Variant v;
  1397. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  1398. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  1399. return v;
  1400. }
  1401. String Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  1402. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  1403. ERR_FAIL_COND_V(ret.is_empty(), ret);
  1404. return ret;
  1405. }
  1406. Vector<uint8_t> Marshalls::base64_to_raw(const String &p_str) {
  1407. int strlen = p_str.length();
  1408. CharString cstr = p_str.ascii();
  1409. size_t arr_len = 0;
  1410. Vector<uint8_t> buf;
  1411. {
  1412. buf.resize(strlen / 4 * 3 + 1);
  1413. uint8_t *w = buf.ptrw();
  1414. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  1415. }
  1416. buf.resize(arr_len);
  1417. return buf;
  1418. }
  1419. String Marshalls::utf8_to_base64(const String &p_str) {
  1420. CharString cstr = p_str.utf8();
  1421. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  1422. ERR_FAIL_COND_V(ret.is_empty(), ret);
  1423. return ret;
  1424. }
  1425. String Marshalls::base64_to_utf8(const String &p_str) {
  1426. int strlen = p_str.length();
  1427. CharString cstr = p_str.ascii();
  1428. Vector<uint8_t> buf;
  1429. buf.resize(strlen / 4 * 3 + 1 + 1);
  1430. uint8_t *w = buf.ptrw();
  1431. size_t len = 0;
  1432. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  1433. w[len] = 0;
  1434. String ret = String::utf8((char *)&w[0]);
  1435. return ret;
  1436. }
  1437. void Marshalls::_bind_methods() {
  1438. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &Marshalls::variant_to_base64, DEFVAL(false));
  1439. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &Marshalls::base64_to_variant, DEFVAL(false));
  1440. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &Marshalls::raw_to_base64);
  1441. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &Marshalls::base64_to_raw);
  1442. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &Marshalls::utf8_to_base64);
  1443. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &Marshalls::base64_to_utf8);
  1444. }
  1445. ////// Semaphore //////
  1446. void Semaphore::wait() {
  1447. semaphore.wait();
  1448. }
  1449. Error Semaphore::try_wait() {
  1450. return semaphore.try_wait() ? OK : ERR_BUSY;
  1451. }
  1452. void Semaphore::post() {
  1453. semaphore.post();
  1454. }
  1455. void Semaphore::_bind_methods() {
  1456. ClassDB::bind_method(D_METHOD("wait"), &Semaphore::wait);
  1457. ClassDB::bind_method(D_METHOD("try_wait"), &Semaphore::try_wait);
  1458. ClassDB::bind_method(D_METHOD("post"), &Semaphore::post);
  1459. }
  1460. ////// Mutex //////
  1461. void Mutex::lock() {
  1462. mutex.lock();
  1463. }
  1464. Error Mutex::try_lock() {
  1465. return mutex.try_lock();
  1466. }
  1467. void Mutex::unlock() {
  1468. mutex.unlock();
  1469. }
  1470. void Mutex::_bind_methods() {
  1471. ClassDB::bind_method(D_METHOD("lock"), &Mutex::lock);
  1472. ClassDB::bind_method(D_METHOD("try_lock"), &Mutex::try_lock);
  1473. ClassDB::bind_method(D_METHOD("unlock"), &Mutex::unlock);
  1474. }
  1475. ////// Thread //////
  1476. void Thread::_start_func(void *ud) {
  1477. Ref<Thread> *tud = (Ref<Thread> *)ud;
  1478. Ref<Thread> t = *tud;
  1479. memdelete(tud);
  1480. Object *target_instance = t->target_callable.get_object();
  1481. if (!target_instance) {
  1482. t->running.clear();
  1483. 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()));
  1484. }
  1485. String func_name = t->target_callable.is_custom() ? t->target_callable.get_custom()->get_as_text() : String(t->target_callable.get_method());
  1486. ::Thread::set_name(func_name);
  1487. Callable::CallError ce;
  1488. t->target_callable.callp(nullptr, 0, t->ret, ce);
  1489. if (ce.error != Callable::CallError::CALL_OK) {
  1490. t->running.clear();
  1491. 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) + ".");
  1492. }
  1493. t->running.clear();
  1494. }
  1495. Error Thread::start(const Callable &p_callable, Priority p_priority) {
  1496. ERR_FAIL_COND_V_MSG(is_started(), ERR_ALREADY_IN_USE, "Thread already started.");
  1497. ERR_FAIL_COND_V(!p_callable.is_valid(), ERR_INVALID_PARAMETER);
  1498. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  1499. ret = Variant();
  1500. target_callable = p_callable;
  1501. running.set();
  1502. Ref<Thread> *ud = memnew(Ref<Thread>(this));
  1503. ::Thread::Settings s;
  1504. s.priority = (::Thread::Priority)p_priority;
  1505. thread.start(_start_func, ud, s);
  1506. return OK;
  1507. }
  1508. String Thread::get_id() const {
  1509. return itos(thread.get_id());
  1510. }
  1511. bool Thread::is_started() const {
  1512. return thread.is_started();
  1513. }
  1514. bool Thread::is_alive() const {
  1515. return running.is_set();
  1516. }
  1517. Variant Thread::wait_to_finish() {
  1518. ERR_FAIL_COND_V_MSG(!is_started(), Variant(), "Thread must have been started to wait for its completion.");
  1519. thread.wait_to_finish();
  1520. Variant r = ret;
  1521. target_callable = Callable();
  1522. return r;
  1523. }
  1524. void Thread::_bind_methods() {
  1525. ClassDB::bind_method(D_METHOD("start", "callable", "priority"), &Thread::start, DEFVAL(PRIORITY_NORMAL));
  1526. ClassDB::bind_method(D_METHOD("get_id"), &Thread::get_id);
  1527. ClassDB::bind_method(D_METHOD("is_started"), &Thread::is_started);
  1528. ClassDB::bind_method(D_METHOD("is_alive"), &Thread::is_alive);
  1529. ClassDB::bind_method(D_METHOD("wait_to_finish"), &Thread::wait_to_finish);
  1530. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  1531. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  1532. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  1533. }
  1534. namespace special {
  1535. ////// ClassDB //////
  1536. PackedStringArray ClassDB::get_class_list() const {
  1537. List<StringName> classes;
  1538. ::ClassDB::get_class_list(&classes);
  1539. PackedStringArray ret;
  1540. ret.resize(classes.size());
  1541. int idx = 0;
  1542. for (const StringName &E : classes) {
  1543. ret.set(idx++, E);
  1544. }
  1545. return ret;
  1546. }
  1547. PackedStringArray ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  1548. List<StringName> classes;
  1549. ::ClassDB::get_inheriters_from_class(p_class, &classes);
  1550. PackedStringArray ret;
  1551. ret.resize(classes.size());
  1552. int idx = 0;
  1553. for (const StringName &E : classes) {
  1554. ret.set(idx++, E);
  1555. }
  1556. return ret;
  1557. }
  1558. StringName ClassDB::get_parent_class(const StringName &p_class) const {
  1559. return ::ClassDB::get_parent_class(p_class);
  1560. }
  1561. bool ClassDB::class_exists(const StringName &p_class) const {
  1562. return ::ClassDB::class_exists(p_class);
  1563. }
  1564. bool ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  1565. return ::ClassDB::is_parent_class(p_class, p_inherits);
  1566. }
  1567. bool ClassDB::can_instantiate(const StringName &p_class) const {
  1568. return ::ClassDB::can_instantiate(p_class);
  1569. }
  1570. Variant ClassDB::instantiate(const StringName &p_class) const {
  1571. Object *obj = ::ClassDB::instantiate(p_class);
  1572. if (!obj) {
  1573. return Variant();
  1574. }
  1575. RefCounted *r = Object::cast_to<RefCounted>(obj);
  1576. if (r) {
  1577. return Ref<RefCounted>(r);
  1578. } else {
  1579. return obj;
  1580. }
  1581. }
  1582. bool ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  1583. return ::ClassDB::has_signal(p_class, p_signal);
  1584. }
  1585. Dictionary ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  1586. MethodInfo signal;
  1587. if (::ClassDB::get_signal(p_class, p_signal, &signal)) {
  1588. return signal.operator Dictionary();
  1589. } else {
  1590. return Dictionary();
  1591. }
  1592. }
  1593. TypedArray<Dictionary> ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  1594. List<MethodInfo> signals;
  1595. ::ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1596. TypedArray<Dictionary> ret;
  1597. for (const MethodInfo &E : signals) {
  1598. ret.push_back(E.operator Dictionary());
  1599. }
  1600. return ret;
  1601. }
  1602. TypedArray<Dictionary> ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  1603. List<PropertyInfo> plist;
  1604. ::ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1605. TypedArray<Dictionary> ret;
  1606. for (const PropertyInfo &E : plist) {
  1607. ret.push_back(E.operator Dictionary());
  1608. }
  1609. return ret;
  1610. }
  1611. Variant ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  1612. Variant ret;
  1613. ::ClassDB::get_property(p_object, p_property, ret);
  1614. return ret;
  1615. }
  1616. Error ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1617. Variant ret;
  1618. bool valid;
  1619. if (!::ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1620. return ERR_UNAVAILABLE;
  1621. } else if (!valid) {
  1622. return ERR_INVALID_DATA;
  1623. }
  1624. return OK;
  1625. }
  1626. bool ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  1627. return ::ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1628. }
  1629. TypedArray<Dictionary> ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  1630. List<MethodInfo> methods;
  1631. ::ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1632. TypedArray<Dictionary> ret;
  1633. for (const MethodInfo &E : methods) {
  1634. #ifdef DEBUG_METHODS_ENABLED
  1635. ret.push_back(E.operator Dictionary());
  1636. #else
  1637. Dictionary dict;
  1638. dict["name"] = E.name;
  1639. ret.push_back(dict);
  1640. #endif
  1641. }
  1642. return ret;
  1643. }
  1644. PackedStringArray ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1645. List<String> constants;
  1646. ::ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1647. PackedStringArray ret;
  1648. ret.resize(constants.size());
  1649. int idx = 0;
  1650. for (const String &E : constants) {
  1651. ret.set(idx++, E);
  1652. }
  1653. return ret;
  1654. }
  1655. bool ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1656. bool success;
  1657. ::ClassDB::get_integer_constant(p_class, p_name, &success);
  1658. return success;
  1659. }
  1660. int64_t ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1661. bool found;
  1662. int64_t c = ::ClassDB::get_integer_constant(p_class, p_name, &found);
  1663. ERR_FAIL_COND_V(!found, 0);
  1664. return c;
  1665. }
  1666. bool ClassDB::has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1667. return ::ClassDB::has_enum(p_class, p_name, p_no_inheritance);
  1668. }
  1669. PackedStringArray ClassDB::get_enum_list(const StringName &p_class, bool p_no_inheritance) const {
  1670. List<StringName> enums;
  1671. ::ClassDB::get_enum_list(p_class, &enums, p_no_inheritance);
  1672. PackedStringArray ret;
  1673. ret.resize(enums.size());
  1674. int idx = 0;
  1675. for (const StringName &E : enums) {
  1676. ret.set(idx++, E);
  1677. }
  1678. return ret;
  1679. }
  1680. PackedStringArray ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, bool p_no_inheritance) const {
  1681. List<StringName> constants;
  1682. ::ClassDB::get_enum_constants(p_class, p_enum, &constants, p_no_inheritance);
  1683. PackedStringArray ret;
  1684. ret.resize(constants.size());
  1685. int idx = 0;
  1686. for (const StringName &E : constants) {
  1687. ret.set(idx++, E);
  1688. }
  1689. return ret;
  1690. }
  1691. StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) const {
  1692. return ::ClassDB::get_integer_constant_enum(p_class, p_name, p_no_inheritance);
  1693. }
  1694. bool ClassDB::is_class_enabled(StringName p_class) const {
  1695. return ::ClassDB::is_class_enabled(p_class);
  1696. }
  1697. void ClassDB::_bind_methods() {
  1698. ::ClassDB::bind_method(D_METHOD("get_class_list"), &ClassDB::get_class_list);
  1699. ::ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &ClassDB::get_inheriters_from_class);
  1700. ::ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &ClassDB::get_parent_class);
  1701. ::ClassDB::bind_method(D_METHOD("class_exists", "class"), &ClassDB::class_exists);
  1702. ::ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &ClassDB::is_parent_class);
  1703. ::ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &ClassDB::can_instantiate);
  1704. ::ClassDB::bind_method(D_METHOD("instantiate", "class"), &ClassDB::instantiate);
  1705. ::ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &ClassDB::has_signal);
  1706. ::ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &ClassDB::get_signal);
  1707. ::ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &ClassDB::get_signal_list, DEFVAL(false));
  1708. ::ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &ClassDB::get_property_list, DEFVAL(false));
  1709. ::ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &ClassDB::get_property);
  1710. ::ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &ClassDB::set_property);
  1711. ::ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &ClassDB::has_method, DEFVAL(false));
  1712. ::ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &ClassDB::get_method_list, DEFVAL(false));
  1713. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &ClassDB::get_integer_constant_list, DEFVAL(false));
  1714. ::ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &ClassDB::has_integer_constant);
  1715. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &ClassDB::get_integer_constant);
  1716. ::ClassDB::bind_method(D_METHOD("class_has_enum", "class", "name", "no_inheritance"), &ClassDB::has_enum, DEFVAL(false));
  1717. ::ClassDB::bind_method(D_METHOD("class_get_enum_list", "class", "no_inheritance"), &ClassDB::get_enum_list, DEFVAL(false));
  1718. ::ClassDB::bind_method(D_METHOD("class_get_enum_constants", "class", "enum", "no_inheritance"), &ClassDB::get_enum_constants, DEFVAL(false));
  1719. ::ClassDB::bind_method(D_METHOD("class_get_integer_constant_enum", "class", "name", "no_inheritance"), &ClassDB::get_integer_constant_enum, DEFVAL(false));
  1720. ::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);
  1721. }
  1722. } // namespace special
  1723. ////// Engine //////
  1724. void Engine::set_physics_ticks_per_second(int p_ips) {
  1725. ::Engine::get_singleton()->set_physics_ticks_per_second(p_ips);
  1726. }
  1727. int Engine::get_physics_ticks_per_second() const {
  1728. return ::Engine::get_singleton()->get_physics_ticks_per_second();
  1729. }
  1730. void Engine::set_physics_jitter_fix(double p_threshold) {
  1731. ::Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1732. }
  1733. double Engine::get_physics_jitter_fix() const {
  1734. return ::Engine::get_singleton()->get_physics_jitter_fix();
  1735. }
  1736. double Engine::get_physics_interpolation_fraction() const {
  1737. return ::Engine::get_singleton()->get_physics_interpolation_fraction();
  1738. }
  1739. void Engine::set_target_fps(int p_fps) {
  1740. ::Engine::get_singleton()->set_target_fps(p_fps);
  1741. }
  1742. int Engine::get_target_fps() const {
  1743. return ::Engine::get_singleton()->get_target_fps();
  1744. }
  1745. double Engine::get_frames_per_second() const {
  1746. return ::Engine::get_singleton()->get_frames_per_second();
  1747. }
  1748. uint64_t Engine::get_physics_frames() const {
  1749. return ::Engine::get_singleton()->get_physics_frames();
  1750. }
  1751. uint64_t Engine::get_process_frames() const {
  1752. return ::Engine::get_singleton()->get_process_frames();
  1753. }
  1754. void Engine::set_time_scale(double p_scale) {
  1755. ::Engine::get_singleton()->set_time_scale(p_scale);
  1756. }
  1757. double Engine::get_time_scale() {
  1758. return ::Engine::get_singleton()->get_time_scale();
  1759. }
  1760. int Engine::get_frames_drawn() {
  1761. return ::Engine::get_singleton()->get_frames_drawn();
  1762. }
  1763. MainLoop *Engine::get_main_loop() const {
  1764. // Needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1765. return ::OS::get_singleton()->get_main_loop();
  1766. }
  1767. Dictionary Engine::get_version_info() const {
  1768. return ::Engine::get_singleton()->get_version_info();
  1769. }
  1770. Dictionary Engine::get_author_info() const {
  1771. return ::Engine::get_singleton()->get_author_info();
  1772. }
  1773. TypedArray<Dictionary> Engine::get_copyright_info() const {
  1774. return ::Engine::get_singleton()->get_copyright_info();
  1775. }
  1776. Dictionary Engine::get_donor_info() const {
  1777. return ::Engine::get_singleton()->get_donor_info();
  1778. }
  1779. Dictionary Engine::get_license_info() const {
  1780. return ::Engine::get_singleton()->get_license_info();
  1781. }
  1782. String Engine::get_license_text() const {
  1783. return ::Engine::get_singleton()->get_license_text();
  1784. }
  1785. String Engine::get_architecture_name() const {
  1786. return ::Engine::get_singleton()->get_architecture_name();
  1787. }
  1788. bool Engine::is_in_physics_frame() const {
  1789. return ::Engine::get_singleton()->is_in_physics_frame();
  1790. }
  1791. bool Engine::has_singleton(const StringName &p_name) const {
  1792. return ::Engine::get_singleton()->has_singleton(p_name);
  1793. }
  1794. Object *Engine::get_singleton_object(const StringName &p_name) const {
  1795. return ::Engine::get_singleton()->get_singleton_object(p_name);
  1796. }
  1797. void Engine::register_singleton(const StringName &p_name, Object *p_object) {
  1798. ERR_FAIL_COND_MSG(has_singleton(p_name), "Singleton already registered: " + String(p_name));
  1799. ERR_FAIL_COND_MSG(!String(p_name).is_valid_identifier(), "Singleton name is not a valid identifier: " + p_name);
  1800. ::Engine::Singleton s;
  1801. s.class_name = p_name;
  1802. s.name = p_name;
  1803. s.ptr = p_object;
  1804. s.user_created = true;
  1805. ::Engine::get_singleton()->add_singleton(s);
  1806. }
  1807. void Engine::unregister_singleton(const StringName &p_name) {
  1808. ERR_FAIL_COND_MSG(!has_singleton(p_name), "Attempt to remove unregistered singleton: " + String(p_name));
  1809. ERR_FAIL_COND_MSG(!::Engine::get_singleton()->is_singleton_user_created(p_name), "Attempt to remove non-user created singleton: " + String(p_name));
  1810. ::Engine::get_singleton()->remove_singleton(p_name);
  1811. }
  1812. Vector<String> Engine::get_singleton_list() const {
  1813. List<::Engine::Singleton> singletons;
  1814. ::Engine::get_singleton()->get_singletons(&singletons);
  1815. Vector<String> ret;
  1816. for (List<::Engine::Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  1817. ret.push_back(E->get().name);
  1818. }
  1819. return ret;
  1820. }
  1821. void Engine::register_script_language(ScriptLanguage *p_language) {
  1822. ScriptServer::register_language(p_language);
  1823. }
  1824. int Engine::get_script_language_count() {
  1825. return ScriptServer::get_language_count();
  1826. }
  1827. ScriptLanguage *Engine::get_script_language(int p_index) const {
  1828. return ScriptServer::get_language(p_index);
  1829. }
  1830. void Engine::set_editor_hint(bool p_enabled) {
  1831. ::Engine::get_singleton()->set_editor_hint(p_enabled);
  1832. }
  1833. bool Engine::is_editor_hint() const {
  1834. return ::Engine::get_singleton()->is_editor_hint();
  1835. }
  1836. String Engine::get_write_movie_path() const {
  1837. return ::Engine::get_singleton()->get_write_movie_path();
  1838. }
  1839. void Engine::set_print_error_messages(bool p_enabled) {
  1840. ::Engine::get_singleton()->set_print_error_messages(p_enabled);
  1841. }
  1842. bool Engine::is_printing_error_messages() const {
  1843. return ::Engine::get_singleton()->is_printing_error_messages();
  1844. }
  1845. void Engine::_bind_methods() {
  1846. ClassDB::bind_method(D_METHOD("set_physics_ticks_per_second", "physics_ticks_per_second"), &Engine::set_physics_ticks_per_second);
  1847. ClassDB::bind_method(D_METHOD("get_physics_ticks_per_second"), &Engine::get_physics_ticks_per_second);
  1848. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &Engine::set_physics_jitter_fix);
  1849. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &Engine::get_physics_jitter_fix);
  1850. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &Engine::get_physics_interpolation_fraction);
  1851. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &Engine::set_target_fps);
  1852. ClassDB::bind_method(D_METHOD("get_target_fps"), &Engine::get_target_fps);
  1853. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &Engine::set_time_scale);
  1854. ClassDB::bind_method(D_METHOD("get_time_scale"), &Engine::get_time_scale);
  1855. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &Engine::get_frames_drawn);
  1856. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &Engine::get_frames_per_second);
  1857. ClassDB::bind_method(D_METHOD("get_physics_frames"), &Engine::get_physics_frames);
  1858. ClassDB::bind_method(D_METHOD("get_process_frames"), &Engine::get_process_frames);
  1859. ClassDB::bind_method(D_METHOD("get_main_loop"), &Engine::get_main_loop);
  1860. ClassDB::bind_method(D_METHOD("get_version_info"), &Engine::get_version_info);
  1861. ClassDB::bind_method(D_METHOD("get_author_info"), &Engine::get_author_info);
  1862. ClassDB::bind_method(D_METHOD("get_copyright_info"), &Engine::get_copyright_info);
  1863. ClassDB::bind_method(D_METHOD("get_donor_info"), &Engine::get_donor_info);
  1864. ClassDB::bind_method(D_METHOD("get_license_info"), &Engine::get_license_info);
  1865. ClassDB::bind_method(D_METHOD("get_license_text"), &Engine::get_license_text);
  1866. ClassDB::bind_method(D_METHOD("get_architecture_name"), &Engine::get_architecture_name);
  1867. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &Engine::is_in_physics_frame);
  1868. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &Engine::has_singleton);
  1869. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &Engine::get_singleton_object);
  1870. ClassDB::bind_method(D_METHOD("register_singleton", "name", "instance"), &Engine::register_singleton);
  1871. ClassDB::bind_method(D_METHOD("unregister_singleton", "name"), &Engine::unregister_singleton);
  1872. ClassDB::bind_method(D_METHOD("get_singleton_list"), &Engine::get_singleton_list);
  1873. ClassDB::bind_method(D_METHOD("register_script_language", "language"), &Engine::register_script_language);
  1874. ClassDB::bind_method(D_METHOD("get_script_language_count"), &Engine::get_script_language_count);
  1875. ClassDB::bind_method(D_METHOD("get_script_language", "index"), &Engine::get_script_language);
  1876. ClassDB::bind_method(D_METHOD("is_editor_hint"), &Engine::is_editor_hint);
  1877. ClassDB::bind_method(D_METHOD("get_write_movie_path"), &Engine::get_write_movie_path);
  1878. ClassDB::bind_method(D_METHOD("set_print_error_messages", "enabled"), &Engine::set_print_error_messages);
  1879. ClassDB::bind_method(D_METHOD("is_printing_error_messages"), &Engine::is_printing_error_messages);
  1880. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "print_error_messages"), "set_print_error_messages", "is_printing_error_messages");
  1881. ADD_PROPERTY(PropertyInfo(Variant::INT, "physics_ticks_per_second"), "set_physics_ticks_per_second", "get_physics_ticks_per_second");
  1882. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  1883. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1884. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1885. }
  1886. Engine *Engine::singleton = nullptr;
  1887. ////// EngineDebugger //////
  1888. bool EngineDebugger::is_active() {
  1889. return ::EngineDebugger::is_active();
  1890. }
  1891. void EngineDebugger::register_profiler(const StringName &p_name, Ref<EngineProfiler> p_profiler) {
  1892. ERR_FAIL_COND(p_profiler.is_null());
  1893. ERR_FAIL_COND_MSG(p_profiler->is_bound(), "Profiler already registered.");
  1894. ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler name already in use: " + p_name);
  1895. Error err = p_profiler->bind(p_name);
  1896. ERR_FAIL_COND_MSG(err != OK, "Profiler failed to register with error: " + itos(err));
  1897. profilers.insert(p_name, p_profiler);
  1898. }
  1899. void EngineDebugger::unregister_profiler(const StringName &p_name) {
  1900. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
  1901. profilers[p_name]->unbind();
  1902. profilers.erase(p_name);
  1903. }
  1904. bool EngineDebugger::is_profiling(const StringName &p_name) {
  1905. return ::EngineDebugger::is_profiling(p_name);
  1906. }
  1907. bool EngineDebugger::has_profiler(const StringName &p_name) {
  1908. return ::EngineDebugger::has_profiler(p_name);
  1909. }
  1910. void EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  1911. ::EngineDebugger::profiler_add_frame_data(p_name, p_data);
  1912. }
  1913. void EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  1914. if (::EngineDebugger::get_singleton()) {
  1915. ::EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
  1916. }
  1917. }
  1918. void EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1919. ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), "Capture already registered: " + p_name);
  1920. captures.insert(p_name, p_callable);
  1921. Callable &c = captures[p_name];
  1922. ::EngineDebugger::Capture capture(&c, &EngineDebugger::call_capture);
  1923. ::EngineDebugger::register_message_capture(p_name, capture);
  1924. }
  1925. void EngineDebugger::unregister_message_capture(const StringName &p_name) {
  1926. ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
  1927. ::EngineDebugger::unregister_message_capture(p_name);
  1928. captures.erase(p_name);
  1929. }
  1930. bool EngineDebugger::has_capture(const StringName &p_name) {
  1931. return ::EngineDebugger::has_capture(p_name);
  1932. }
  1933. void EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
  1934. ERR_FAIL_COND_MSG(!::EngineDebugger::is_active(), "Can't send message. No active debugger");
  1935. ::EngineDebugger::get_singleton()->send_message(p_msg, p_data);
  1936. }
  1937. Error EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  1938. Callable &capture = *(Callable *)p_user;
  1939. if (capture.is_null()) {
  1940. return FAILED;
  1941. }
  1942. Variant cmd = p_cmd, data = p_data;
  1943. const Variant *args[2] = { &cmd, &data };
  1944. Variant retval;
  1945. Callable::CallError err;
  1946. capture.callp(args, 2, retval, err);
  1947. 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));
  1948. ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, "Error calling 'capture' to callable: " + String(capture) + ". Return type is not bool.");
  1949. r_captured = retval;
  1950. return OK;
  1951. }
  1952. EngineDebugger::~EngineDebugger() {
  1953. for (const KeyValue<StringName, Callable> &E : captures) {
  1954. ::EngineDebugger::unregister_message_capture(E.key);
  1955. }
  1956. captures.clear();
  1957. }
  1958. EngineDebugger *EngineDebugger::singleton = nullptr;
  1959. void EngineDebugger::_bind_methods() {
  1960. ClassDB::bind_method(D_METHOD("is_active"), &EngineDebugger::is_active);
  1961. ClassDB::bind_method(D_METHOD("register_profiler", "name", "profiler"), &EngineDebugger::register_profiler);
  1962. ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &EngineDebugger::unregister_profiler);
  1963. ClassDB::bind_method(D_METHOD("is_profiling", "name"), &EngineDebugger::is_profiling);
  1964. ClassDB::bind_method(D_METHOD("has_profiler", "name"), &EngineDebugger::has_profiler);
  1965. ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &EngineDebugger::profiler_add_frame_data);
  1966. ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &EngineDebugger::profiler_enable, DEFVAL(Array()));
  1967. ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &EngineDebugger::register_message_capture);
  1968. ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &EngineDebugger::unregister_message_capture);
  1969. ClassDB::bind_method(D_METHOD("has_capture", "name"), &EngineDebugger::has_capture);
  1970. ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &EngineDebugger::send_message);
  1971. }
  1972. } // namespace core_bind