core_bind.cpp 81 KB

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