core_bind.cpp 81 KB

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