2
0

core_bind.cpp 85 KB

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