core_bind.cpp 83 KB

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