core_bind.cpp 78 KB

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