2
0

core_bind.cpp 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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. String _OS::get_config_dir() const {
  347. // Exposed as `get_config_dir()` instead of `get_config_path()` for consistency with other exposed OS methods.
  348. return OS::get_singleton()->get_config_path();
  349. }
  350. String _OS::get_data_dir() const {
  351. // Exposed as `get_data_dir()` instead of `get_data_path()` for consistency with other exposed OS methods.
  352. return OS::get_singleton()->get_data_path();
  353. }
  354. String _OS::get_cache_dir() const {
  355. // Exposed as `get_cache_dir()` instead of `get_cache_path()` for consistency with other exposed OS methods.
  356. return OS::get_singleton()->get_cache_path();
  357. }
  358. bool _OS::is_debug_build() const {
  359. #ifdef DEBUG_ENABLED
  360. return true;
  361. #else
  362. return false;
  363. #endif
  364. }
  365. String _OS::get_system_dir(SystemDir p_dir) const {
  366. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
  367. }
  368. String _OS::get_keycode_string(uint32_t p_code) const {
  369. return keycode_get_string(p_code);
  370. }
  371. bool _OS::is_keycode_unicode(uint32_t p_unicode) const {
  372. return keycode_has_unicode(p_unicode);
  373. }
  374. int _OS::find_keycode_from_string(const String &p_code) const {
  375. return find_keycode(p_code);
  376. }
  377. bool _OS::request_permission(const String &p_name) {
  378. return OS::get_singleton()->request_permission(p_name);
  379. }
  380. bool _OS::request_permissions() {
  381. return OS::get_singleton()->request_permissions();
  382. }
  383. Vector<String> _OS::get_granted_permissions() const {
  384. return OS::get_singleton()->get_granted_permissions();
  385. }
  386. String _OS::get_unique_id() const {
  387. return OS::get_singleton()->get_unique_id();
  388. }
  389. _OS *_OS::singleton = nullptr;
  390. void _OS::_bind_methods() {
  391. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &_OS::get_connected_midi_inputs);
  392. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &_OS::open_midi_inputs);
  393. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &_OS::close_midi_inputs);
  394. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
  395. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
  396. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode_sleep_usec", "usec"), &_OS::set_low_processor_usage_mode_sleep_usec);
  397. ClassDB::bind_method(D_METHOD("get_low_processor_usage_mode_sleep_usec"), &_OS::get_low_processor_usage_mode_sleep_usec);
  398. ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count);
  399. ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
  400. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr"), &_OS::execute, DEFVAL(Array()), DEFVAL(false));
  401. ClassDB::bind_method(D_METHOD("create_process", "path", "arguments"), &_OS::create_process);
  402. ClassDB::bind_method(D_METHOD("kill", "pid"), &_OS::kill);
  403. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &_OS::shell_open);
  404. ClassDB::bind_method(D_METHOD("get_process_id"), &_OS::get_process_id);
  405. ClassDB::bind_method(D_METHOD("get_environment", "variable"), &_OS::get_environment);
  406. ClassDB::bind_method(D_METHOD("set_environment", "variable", "value"), &_OS::set_environment);
  407. ClassDB::bind_method(D_METHOD("has_environment", "variable"), &_OS::has_environment);
  408. ClassDB::bind_method(D_METHOD("get_name"), &_OS::get_name);
  409. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &_OS::get_cmdline_args);
  410. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &_OS::delay_usec);
  411. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &_OS::delay_msec);
  412. ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
  413. ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
  414. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &_OS::is_userfs_persistent);
  415. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &_OS::is_stdout_verbose);
  416. ClassDB::bind_method(D_METHOD("can_use_threads"), &_OS::can_use_threads);
  417. ClassDB::bind_method(D_METHOD("is_debug_build"), &_OS::is_debug_build);
  418. ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
  419. ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
  420. ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
  421. ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
  422. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &_OS::get_static_memory_usage);
  423. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
  424. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir);
  425. ClassDB::bind_method(D_METHOD("get_external_data_dir"), &_OS::get_external_data_dir);
  426. ClassDB::bind_method(D_METHOD("get_system_dir", "dir"), &_OS::get_system_dir);
  427. ClassDB::bind_method(D_METHOD("get_config_dir"), &_OS::get_config_dir);
  428. ClassDB::bind_method(D_METHOD("get_data_dir"), &_OS::get_data_dir);
  429. ClassDB::bind_method(D_METHOD("get_cache_dir"), &_OS::get_cache_dir);
  430. ClassDB::bind_method(D_METHOD("get_unique_id"), &_OS::get_unique_id);
  431. ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
  432. ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
  433. ClassDB::bind_method(D_METHOD("get_keycode_string", "code"), &_OS::get_keycode_string);
  434. ClassDB::bind_method(D_METHOD("is_keycode_unicode", "code"), &_OS::is_keycode_unicode);
  435. ClassDB::bind_method(D_METHOD("find_keycode_from_string", "string"), &_OS::find_keycode_from_string);
  436. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
  437. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &_OS::set_thread_name);
  438. ClassDB::bind_method(D_METHOD("get_thread_caller_id"), &_OS::get_thread_caller_id);
  439. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &_OS::has_feature);
  440. ClassDB::bind_method(D_METHOD("request_permission", "name"), &_OS::request_permission);
  441. ClassDB::bind_method(D_METHOD("request_permissions"), &_OS::request_permissions);
  442. ClassDB::bind_method(D_METHOD("get_granted_permissions"), &_OS::get_granted_permissions);
  443. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  444. 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");
  445. // Those default values need to be specified for the docs generator,
  446. // to avoid using values from the documentation writer's own OS instance.
  447. ADD_PROPERTY_DEFAULT("exit_code", 0);
  448. ADD_PROPERTY_DEFAULT("low_processor_usage_mode", false);
  449. ADD_PROPERTY_DEFAULT("low_processor_usage_mode_sleep_usec", 6900);
  450. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES2);
  451. BIND_ENUM_CONSTANT(VIDEO_DRIVER_VULKAN);
  452. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  453. BIND_ENUM_CONSTANT(DAY_MONDAY);
  454. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  455. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  456. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  457. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  458. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  459. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  460. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  461. BIND_ENUM_CONSTANT(MONTH_MARCH);
  462. BIND_ENUM_CONSTANT(MONTH_APRIL);
  463. BIND_ENUM_CONSTANT(MONTH_MAY);
  464. BIND_ENUM_CONSTANT(MONTH_JUNE);
  465. BIND_ENUM_CONSTANT(MONTH_JULY);
  466. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  467. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  468. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  469. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  470. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  471. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  472. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  473. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  474. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  475. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  476. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  477. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  478. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  479. }
  480. ////// _Geometry2D //////
  481. _Geometry2D *_Geometry2D::singleton = nullptr;
  482. _Geometry2D *_Geometry2D::get_singleton() {
  483. return singleton;
  484. }
  485. bool _Geometry2D::is_point_in_circle(const Vector2 &p_point, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  486. return Geometry2D::is_point_in_circle(p_point, p_circle_pos, p_circle_radius);
  487. }
  488. real_t _Geometry2D::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  489. return Geometry2D::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  490. }
  491. 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) {
  492. Vector2 result;
  493. if (Geometry2D::segment_intersects_segment(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  494. return result;
  495. } else {
  496. return Variant();
  497. }
  498. }
  499. 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) {
  500. Vector2 result;
  501. if (Geometry2D::line_intersects_line(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  502. return result;
  503. } else {
  504. return Variant();
  505. }
  506. }
  507. Vector<Vector2> _Geometry2D::get_closest_points_between_segments(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  508. Vector2 r1, r2;
  509. Geometry2D::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  510. Vector<Vector2> r;
  511. r.resize(2);
  512. r.set(0, r1);
  513. r.set(1, r2);
  514. return r;
  515. }
  516. Vector2 _Geometry2D::get_closest_point_to_segment(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  517. Vector2 s[2] = { p_a, p_b };
  518. return Geometry2D::get_closest_point_to_segment(p_point, s);
  519. }
  520. Vector2 _Geometry2D::get_closest_point_to_segment_uncapped(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  521. Vector2 s[2] = { p_a, p_b };
  522. return Geometry2D::get_closest_point_to_segment_uncapped(p_point, s);
  523. }
  524. bool _Geometry2D::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  525. return Geometry2D::is_point_in_triangle(s, a, b, c);
  526. }
  527. bool _Geometry2D::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  528. return Geometry2D::is_polygon_clockwise(p_polygon);
  529. }
  530. bool _Geometry2D::is_point_in_polygon(const Point2 &p_point, const Vector<Vector2> &p_polygon) {
  531. return Geometry2D::is_point_in_polygon(p_point, p_polygon);
  532. }
  533. Vector<int> _Geometry2D::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  534. return Geometry2D::triangulate_polygon(p_polygon);
  535. }
  536. Vector<int> _Geometry2D::triangulate_delaunay(const Vector<Vector2> &p_points) {
  537. return Geometry2D::triangulate_delaunay(p_points);
  538. }
  539. Vector<Point2> _Geometry2D::convex_hull(const Vector<Point2> &p_points) {
  540. return Geometry2D::convex_hull(p_points);
  541. }
  542. Array _Geometry2D::merge_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  543. Vector<Vector<Point2>> polys = Geometry2D::merge_polygons(p_polygon_a, p_polygon_b);
  544. Array ret;
  545. for (int i = 0; i < polys.size(); ++i) {
  546. ret.push_back(polys[i]);
  547. }
  548. return ret;
  549. }
  550. Array _Geometry2D::clip_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  551. Vector<Vector<Point2>> polys = Geometry2D::clip_polygons(p_polygon_a, p_polygon_b);
  552. Array ret;
  553. for (int i = 0; i < polys.size(); ++i) {
  554. ret.push_back(polys[i]);
  555. }
  556. return ret;
  557. }
  558. Array _Geometry2D::intersect_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  559. Vector<Vector<Point2>> polys = Geometry2D::intersect_polygons(p_polygon_a, p_polygon_b);
  560. Array ret;
  561. for (int i = 0; i < polys.size(); ++i) {
  562. ret.push_back(polys[i]);
  563. }
  564. return ret;
  565. }
  566. Array _Geometry2D::exclude_polygons(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  567. Vector<Vector<Point2>> polys = Geometry2D::exclude_polygons(p_polygon_a, p_polygon_b);
  568. Array ret;
  569. for (int i = 0; i < polys.size(); ++i) {
  570. ret.push_back(polys[i]);
  571. }
  572. return ret;
  573. }
  574. Array _Geometry2D::clip_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  575. Vector<Vector<Point2>> polys = Geometry2D::clip_polyline_with_polygon(p_polyline, p_polygon);
  576. Array ret;
  577. for (int i = 0; i < polys.size(); ++i) {
  578. ret.push_back(polys[i]);
  579. }
  580. return ret;
  581. }
  582. Array _Geometry2D::intersect_polyline_with_polygon(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  583. Vector<Vector<Point2>> polys = Geometry2D::intersect_polyline_with_polygon(p_polyline, p_polygon);
  584. Array ret;
  585. for (int i = 0; i < polys.size(); ++i) {
  586. ret.push_back(polys[i]);
  587. }
  588. return ret;
  589. }
  590. Array _Geometry2D::offset_polygon(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  591. Vector<Vector<Point2>> polys = Geometry2D::offset_polygon(p_polygon, p_delta, Geometry2D::PolyJoinType(p_join_type));
  592. Array ret;
  593. for (int i = 0; i < polys.size(); ++i) {
  594. ret.push_back(polys[i]);
  595. }
  596. return ret;
  597. }
  598. Array _Geometry2D::offset_polyline(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  599. Vector<Vector<Point2>> polys = Geometry2D::offset_polyline(p_polygon, p_delta, Geometry2D::PolyJoinType(p_join_type), Geometry2D::PolyEndType(p_end_type));
  600. Array ret;
  601. for (int i = 0; i < polys.size(); ++i) {
  602. ret.push_back(polys[i]);
  603. }
  604. return ret;
  605. }
  606. Dictionary _Geometry2D::make_atlas(const Vector<Size2> &p_rects) {
  607. Dictionary ret;
  608. Vector<Size2i> rects;
  609. for (int i = 0; i < p_rects.size(); i++) {
  610. rects.push_back(p_rects[i]);
  611. }
  612. Vector<Point2i> result;
  613. Size2i size;
  614. Geometry2D::make_atlas(rects, result, size);
  615. Size2 r_size = size;
  616. Vector<Point2> r_result;
  617. for (int i = 0; i < result.size(); i++) {
  618. r_result.push_back(result[i]);
  619. }
  620. ret["points"] = r_result;
  621. ret["size"] = r_size;
  622. return ret;
  623. }
  624. void _Geometry2D::_bind_methods() {
  625. ClassDB::bind_method(D_METHOD("is_point_in_circle", "point", "circle_position", "circle_radius"), &_Geometry2D::is_point_in_circle);
  626. ClassDB::bind_method(D_METHOD("segment_intersects_segment", "from_a", "to_a", "from_b", "to_b"), &_Geometry2D::segment_intersects_segment);
  627. ClassDB::bind_method(D_METHOD("line_intersects_line", "from_a", "dir_a", "from_b", "dir_b"), &_Geometry2D::line_intersects_line);
  628. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "q1", "p2", "q2"), &_Geometry2D::get_closest_points_between_segments);
  629. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry2D::get_closest_point_to_segment);
  630. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry2D::get_closest_point_to_segment_uncapped);
  631. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry2D::point_is_inside_triangle);
  632. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &_Geometry2D::is_polygon_clockwise);
  633. ClassDB::bind_method(D_METHOD("is_point_in_polygon", "point", "polygon"), &_Geometry2D::is_point_in_polygon);
  634. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &_Geometry2D::triangulate_polygon);
  635. ClassDB::bind_method(D_METHOD("triangulate_delaunay", "points"), &_Geometry2D::triangulate_delaunay);
  636. ClassDB::bind_method(D_METHOD("convex_hull", "points"), &_Geometry2D::convex_hull);
  637. ClassDB::bind_method(D_METHOD("merge_polygons", "polygon_a", "polygon_b"), &_Geometry2D::merge_polygons);
  638. ClassDB::bind_method(D_METHOD("clip_polygons", "polygon_a", "polygon_b"), &_Geometry2D::clip_polygons);
  639. ClassDB::bind_method(D_METHOD("intersect_polygons", "polygon_a", "polygon_b"), &_Geometry2D::intersect_polygons);
  640. ClassDB::bind_method(D_METHOD("exclude_polygons", "polygon_a", "polygon_b"), &_Geometry2D::exclude_polygons);
  641. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon", "polyline", "polygon"), &_Geometry2D::clip_polyline_with_polygon);
  642. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon", "polyline", "polygon"), &_Geometry2D::intersect_polyline_with_polygon);
  643. ClassDB::bind_method(D_METHOD("offset_polygon", "polygon", "delta", "join_type"), &_Geometry2D::offset_polygon, DEFVAL(JOIN_SQUARE));
  644. ClassDB::bind_method(D_METHOD("offset_polyline", "polyline", "delta", "join_type", "end_type"), &_Geometry2D::offset_polyline, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  645. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &_Geometry2D::make_atlas);
  646. BIND_ENUM_CONSTANT(OPERATION_UNION);
  647. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  648. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  649. BIND_ENUM_CONSTANT(OPERATION_XOR);
  650. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  651. BIND_ENUM_CONSTANT(JOIN_ROUND);
  652. BIND_ENUM_CONSTANT(JOIN_MITER);
  653. BIND_ENUM_CONSTANT(END_POLYGON);
  654. BIND_ENUM_CONSTANT(END_JOINED);
  655. BIND_ENUM_CONSTANT(END_BUTT);
  656. BIND_ENUM_CONSTANT(END_SQUARE);
  657. BIND_ENUM_CONSTANT(END_ROUND);
  658. }
  659. ////// _Geometry3D //////
  660. _Geometry3D *_Geometry3D::singleton = nullptr;
  661. _Geometry3D *_Geometry3D::get_singleton() {
  662. return singleton;
  663. }
  664. Vector<Plane> _Geometry3D::build_box_planes(const Vector3 &p_extents) {
  665. return Geometry3D::build_box_planes(p_extents);
  666. }
  667. Vector<Plane> _Geometry3D::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  668. return Geometry3D::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  669. }
  670. Vector<Plane> _Geometry3D::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  671. return Geometry3D::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  672. }
  673. Vector<Vector3> _Geometry3D::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  674. Vector3 r1, r2;
  675. Geometry3D::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  676. Vector<Vector3> r;
  677. r.resize(2);
  678. r.set(0, r1);
  679. r.set(1, r2);
  680. return r;
  681. }
  682. Vector3 _Geometry3D::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  683. Vector3 s[2] = { p_a, p_b };
  684. return Geometry3D::get_closest_point_to_segment(p_point, s);
  685. }
  686. Vector3 _Geometry3D::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  687. Vector3 s[2] = { p_a, p_b };
  688. return Geometry3D::get_closest_point_to_segment_uncapped(p_point, s);
  689. }
  690. 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) {
  691. Vector3 res;
  692. if (Geometry3D::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res)) {
  693. return res;
  694. } else {
  695. return Variant();
  696. }
  697. }
  698. 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) {
  699. Vector3 res;
  700. if (Geometry3D::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res)) {
  701. return res;
  702. } else {
  703. return Variant();
  704. }
  705. }
  706. Vector<Vector3> _Geometry3D::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  707. Vector<Vector3> r;
  708. Vector3 res, norm;
  709. if (!Geometry3D::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm)) {
  710. return r;
  711. }
  712. r.resize(2);
  713. r.set(0, res);
  714. r.set(1, norm);
  715. return r;
  716. }
  717. Vector<Vector3> _Geometry3D::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  718. Vector<Vector3> r;
  719. Vector3 res, norm;
  720. if (!Geometry3D::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm)) {
  721. return r;
  722. }
  723. r.resize(2);
  724. r.set(0, res);
  725. r.set(1, norm);
  726. return r;
  727. }
  728. Vector<Vector3> _Geometry3D::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  729. Vector<Vector3> r;
  730. Vector3 res, norm;
  731. if (!Geometry3D::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm)) {
  732. return r;
  733. }
  734. r.resize(2);
  735. r.set(0, res);
  736. r.set(1, norm);
  737. return r;
  738. }
  739. Vector<Vector3> _Geometry3D::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  740. return Geometry3D::clip_polygon(p_points, p_plane);
  741. }
  742. void _Geometry3D::_bind_methods() {
  743. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry3D::build_box_planes);
  744. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry3D::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  745. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry3D::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  746. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry3D::get_closest_points_between_segments);
  747. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry3D::get_closest_point_to_segment);
  748. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry3D::get_closest_point_to_segment_uncapped);
  749. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry3D::ray_intersects_triangle);
  750. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry3D::segment_intersects_triangle);
  751. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &_Geometry3D::segment_intersects_sphere);
  752. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry3D::segment_intersects_cylinder);
  753. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &_Geometry3D::segment_intersects_convex);
  754. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &_Geometry3D::clip_polygon);
  755. }
  756. ////// _File //////
  757. Error _File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  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(f, p_key, (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_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  773. Error err = open(p_path, p_mode_flags);
  774. if (err) {
  775. return err;
  776. }
  777. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  778. err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  779. if (err) {
  780. memdelete(fae);
  781. close();
  782. return err;
  783. }
  784. f = fae;
  785. return OK;
  786. }
  787. Error _File::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  788. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  789. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  790. Error err = fac->_open(p_path, p_mode_flags);
  791. if (err) {
  792. memdelete(fac);
  793. return err;
  794. }
  795. f = fac;
  796. return OK;
  797. }
  798. Error _File::open(const String &p_path, ModeFlags p_mode_flags) {
  799. close();
  800. Error err;
  801. f = FileAccess::open(p_path, p_mode_flags, &err);
  802. if (f) {
  803. f->set_big_endian(big_endian);
  804. }
  805. return err;
  806. }
  807. void _File::flush() {
  808. ERR_FAIL_COND_MSG(!f, "File must be opened before flushing.");
  809. f->flush();
  810. }
  811. void _File::close() {
  812. if (f) {
  813. memdelete(f);
  814. }
  815. f = nullptr;
  816. }
  817. bool _File::is_open() const {
  818. return f != nullptr;
  819. }
  820. String _File::get_path() const {
  821. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  822. return f->get_path();
  823. }
  824. String _File::get_path_absolute() const {
  825. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  826. return f->get_path_absolute();
  827. }
  828. void _File::seek(int64_t p_position) {
  829. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  830. ERR_FAIL_COND_MSG(p_position < 0, "Seek position must be a positive integer.");
  831. f->seek(p_position);
  832. }
  833. void _File::seek_end(int64_t p_position) {
  834. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  835. f->seek_end(p_position);
  836. }
  837. uint64_t _File::get_position() const {
  838. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  839. return f->get_position();
  840. }
  841. uint64_t _File::get_length() const {
  842. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  843. return f->get_length();
  844. }
  845. bool _File::eof_reached() const {
  846. ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use.");
  847. return f->eof_reached();
  848. }
  849. uint8_t _File::get_8() const {
  850. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  851. return f->get_8();
  852. }
  853. uint16_t _File::get_16() const {
  854. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  855. return f->get_16();
  856. }
  857. uint32_t _File::get_32() const {
  858. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  859. return f->get_32();
  860. }
  861. uint64_t _File::get_64() const {
  862. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  863. return f->get_64();
  864. }
  865. float _File::get_float() const {
  866. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  867. return f->get_float();
  868. }
  869. double _File::get_double() const {
  870. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  871. return f->get_double();
  872. }
  873. real_t _File::get_real() const {
  874. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  875. return f->get_real();
  876. }
  877. Vector<uint8_t> _File::get_buffer(int64_t p_length) const {
  878. Vector<uint8_t> data;
  879. ERR_FAIL_COND_V_MSG(!f, data, "File must be opened before use.");
  880. ERR_FAIL_COND_V_MSG(p_length < 0, data, "Length of buffer cannot be smaller than 0.");
  881. if (p_length == 0) {
  882. return data;
  883. }
  884. Error err = data.resize(p_length);
  885. ERR_FAIL_COND_V_MSG(err != OK, data, "Can't resize data to " + itos(p_length) + " elements.");
  886. uint8_t *w = data.ptrw();
  887. int64_t len = f->get_buffer(&w[0], p_length);
  888. if (len < p_length) {
  889. data.resize(len);
  890. }
  891. return data;
  892. }
  893. String _File::get_as_text() const {
  894. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  895. String text;
  896. uint64_t original_pos = f->get_position();
  897. f->seek(0);
  898. String l = get_line();
  899. while (!eof_reached()) {
  900. text += l + "\n";
  901. l = get_line();
  902. }
  903. text += l;
  904. f->seek(original_pos);
  905. return text;
  906. }
  907. String _File::get_md5(const String &p_path) const {
  908. return FileAccess::get_md5(p_path);
  909. }
  910. String _File::get_sha256(const String &p_path) const {
  911. return FileAccess::get_sha256(p_path);
  912. }
  913. String _File::get_line() const {
  914. ERR_FAIL_COND_V_MSG(!f, String(), "File must be opened before use.");
  915. return f->get_line();
  916. }
  917. Vector<String> _File::get_csv_line(const String &p_delim) const {
  918. ERR_FAIL_COND_V_MSG(!f, Vector<String>(), "File must be opened before use.");
  919. return f->get_csv_line(p_delim);
  920. }
  921. /**< use this for files WRITTEN in _big_ endian machines (i.e. amiga/mac)
  922. * It's not about the current CPU type but file formats.
  923. * These flags get reset to false (little endian) on each open
  924. */
  925. void _File::set_big_endian(bool p_big_endian) {
  926. big_endian = p_big_endian;
  927. if (f) {
  928. f->set_big_endian(p_big_endian);
  929. }
  930. }
  931. bool _File::is_big_endian() {
  932. return big_endian;
  933. }
  934. Error _File::get_error() const {
  935. if (!f) {
  936. return ERR_UNCONFIGURED;
  937. }
  938. return f->get_error();
  939. }
  940. void _File::store_8(uint8_t p_dest) {
  941. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  942. f->store_8(p_dest);
  943. }
  944. void _File::store_16(uint16_t p_dest) {
  945. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  946. f->store_16(p_dest);
  947. }
  948. void _File::store_32(uint32_t p_dest) {
  949. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  950. f->store_32(p_dest);
  951. }
  952. void _File::store_64(uint64_t p_dest) {
  953. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  954. f->store_64(p_dest);
  955. }
  956. void _File::store_float(float p_dest) {
  957. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  958. f->store_float(p_dest);
  959. }
  960. void _File::store_double(double p_dest) {
  961. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  962. f->store_double(p_dest);
  963. }
  964. void _File::store_real(real_t p_real) {
  965. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  966. f->store_real(p_real);
  967. }
  968. void _File::store_string(const String &p_string) {
  969. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  970. f->store_string(p_string);
  971. }
  972. void _File::store_pascal_string(const String &p_string) {
  973. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  974. f->store_pascal_string(p_string);
  975. }
  976. String _File::get_pascal_string() {
  977. ERR_FAIL_COND_V_MSG(!f, "", "File must be opened before use.");
  978. return f->get_pascal_string();
  979. }
  980. void _File::store_line(const String &p_string) {
  981. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  982. f->store_line(p_string);
  983. }
  984. void _File::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  985. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  986. f->store_csv_line(p_values, p_delim);
  987. }
  988. void _File::store_buffer(const Vector<uint8_t> &p_buffer) {
  989. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  990. uint64_t len = p_buffer.size();
  991. if (len == 0) {
  992. return;
  993. }
  994. const uint8_t *r = p_buffer.ptr();
  995. f->store_buffer(&r[0], len);
  996. }
  997. bool _File::file_exists(const String &p_name) const {
  998. return FileAccess::exists(p_name);
  999. }
  1000. void _File::store_var(const Variant &p_var, bool p_full_objects) {
  1001. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  1002. int len;
  1003. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1004. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1005. Vector<uint8_t> buff;
  1006. buff.resize(len);
  1007. uint8_t *w = buff.ptrw();
  1008. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1009. ERR_FAIL_COND_MSG(err != OK, "Error when trying to encode Variant.");
  1010. store_32(len);
  1011. store_buffer(buff);
  1012. }
  1013. Variant _File::get_var(bool p_allow_objects) const {
  1014. ERR_FAIL_COND_V_MSG(!f, Variant(), "File must be opened before use.");
  1015. uint32_t len = get_32();
  1016. Vector<uint8_t> buff = get_buffer(len);
  1017. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  1018. const uint8_t *r = buff.ptr();
  1019. Variant v;
  1020. Error err = decode_variant(v, &r[0], len, nullptr, p_allow_objects);
  1021. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to encode Variant.");
  1022. return v;
  1023. }
  1024. uint64_t _File::get_modified_time(const String &p_file) const {
  1025. return FileAccess::get_modified_time(p_file);
  1026. }
  1027. void _File::_bind_methods() {
  1028. ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
  1029. ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
  1030. ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &_File::open_compressed, DEFVAL(0));
  1031. ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open);
  1032. ClassDB::bind_method(D_METHOD("flush"), &_File::flush);
  1033. ClassDB::bind_method(D_METHOD("close"), &_File::close);
  1034. ClassDB::bind_method(D_METHOD("get_path"), &_File::get_path);
  1035. ClassDB::bind_method(D_METHOD("get_path_absolute"), &_File::get_path_absolute);
  1036. ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open);
  1037. ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek);
  1038. ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0));
  1039. ClassDB::bind_method(D_METHOD("get_position"), &_File::get_position);
  1040. ClassDB::bind_method(D_METHOD("get_length"), &_File::get_length);
  1041. ClassDB::bind_method(D_METHOD("eof_reached"), &_File::eof_reached);
  1042. ClassDB::bind_method(D_METHOD("get_8"), &_File::get_8);
  1043. ClassDB::bind_method(D_METHOD("get_16"), &_File::get_16);
  1044. ClassDB::bind_method(D_METHOD("get_32"), &_File::get_32);
  1045. ClassDB::bind_method(D_METHOD("get_64"), &_File::get_64);
  1046. ClassDB::bind_method(D_METHOD("get_float"), &_File::get_float);
  1047. ClassDB::bind_method(D_METHOD("get_double"), &_File::get_double);
  1048. ClassDB::bind_method(D_METHOD("get_real"), &_File::get_real);
  1049. ClassDB::bind_method(D_METHOD("get_buffer", "length"), &_File::get_buffer);
  1050. ClassDB::bind_method(D_METHOD("get_line"), &_File::get_line);
  1051. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
  1052. ClassDB::bind_method(D_METHOD("get_as_text"), &_File::get_as_text);
  1053. ClassDB::bind_method(D_METHOD("get_md5", "path"), &_File::get_md5);
  1054. ClassDB::bind_method(D_METHOD("get_sha256", "path"), &_File::get_sha256);
  1055. ClassDB::bind_method(D_METHOD("is_big_endian"), &_File::is_big_endian);
  1056. ClassDB::bind_method(D_METHOD("set_big_endian", "big_endian"), &_File::set_big_endian);
  1057. ClassDB::bind_method(D_METHOD("get_error"), &_File::get_error);
  1058. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &_File::get_var, DEFVAL(false));
  1059. ClassDB::bind_method(D_METHOD("store_8", "value"), &_File::store_8);
  1060. ClassDB::bind_method(D_METHOD("store_16", "value"), &_File::store_16);
  1061. ClassDB::bind_method(D_METHOD("store_32", "value"), &_File::store_32);
  1062. ClassDB::bind_method(D_METHOD("store_64", "value"), &_File::store_64);
  1063. ClassDB::bind_method(D_METHOD("store_float", "value"), &_File::store_float);
  1064. ClassDB::bind_method(D_METHOD("store_double", "value"), &_File::store_double);
  1065. ClassDB::bind_method(D_METHOD("store_real", "value"), &_File::store_real);
  1066. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &_File::store_buffer);
  1067. ClassDB::bind_method(D_METHOD("store_line", "line"), &_File::store_line);
  1068. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &_File::store_csv_line, DEFVAL(","));
  1069. ClassDB::bind_method(D_METHOD("store_string", "string"), &_File::store_string);
  1070. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &_File::store_var, DEFVAL(false));
  1071. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &_File::store_pascal_string);
  1072. ClassDB::bind_method(D_METHOD("get_pascal_string"), &_File::get_pascal_string);
  1073. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists);
  1074. ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time);
  1075. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian");
  1076. BIND_ENUM_CONSTANT(READ);
  1077. BIND_ENUM_CONSTANT(WRITE);
  1078. BIND_ENUM_CONSTANT(READ_WRITE);
  1079. BIND_ENUM_CONSTANT(WRITE_READ);
  1080. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  1081. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  1082. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  1083. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  1084. }
  1085. _File::~_File() {
  1086. if (f) {
  1087. memdelete(f);
  1088. }
  1089. }
  1090. ////// _Directory //////
  1091. Error _Directory::open(const String &p_path) {
  1092. Error err;
  1093. DirAccess *alt = DirAccess::open(p_path, &err);
  1094. if (!alt) {
  1095. return err;
  1096. }
  1097. if (d) {
  1098. memdelete(d);
  1099. }
  1100. d = alt;
  1101. dir_open = true;
  1102. return OK;
  1103. }
  1104. bool _Directory::is_open() const {
  1105. return d && dir_open;
  1106. }
  1107. Error _Directory::list_dir_begin(bool p_show_navigational, bool p_show_hidden) {
  1108. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1109. _list_skip_navigational = !p_show_navigational;
  1110. _list_skip_hidden = !p_show_hidden;
  1111. return d->list_dir_begin();
  1112. }
  1113. String _Directory::get_next() {
  1114. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1115. String next = d->get_next();
  1116. while (next != "" && ((_list_skip_navigational && (next == "." || next == "..")) || (_list_skip_hidden && d->current_is_hidden()))) {
  1117. next = d->get_next();
  1118. }
  1119. return next;
  1120. }
  1121. bool _Directory::current_is_dir() const {
  1122. ERR_FAIL_COND_V_MSG(!is_open(), false, "Directory must be opened before use.");
  1123. return d->current_is_dir();
  1124. }
  1125. void _Directory::list_dir_end() {
  1126. ERR_FAIL_COND_MSG(!is_open(), "Directory must be opened before use.");
  1127. d->list_dir_end();
  1128. }
  1129. int _Directory::get_drive_count() {
  1130. ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use.");
  1131. return d->get_drive_count();
  1132. }
  1133. String _Directory::get_drive(int p_drive) {
  1134. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1135. return d->get_drive(p_drive);
  1136. }
  1137. int _Directory::get_current_drive() {
  1138. ERR_FAIL_COND_V_MSG(!is_open(), 0, "Directory must be opened before use.");
  1139. return d->get_current_drive();
  1140. }
  1141. Error _Directory::change_dir(String p_dir) {
  1142. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
  1143. Error err = d->change_dir(p_dir);
  1144. if (err != OK) {
  1145. return err;
  1146. }
  1147. dir_open = true;
  1148. return OK;
  1149. }
  1150. String _Directory::get_current_dir() {
  1151. ERR_FAIL_COND_V_MSG(!is_open(), "", "Directory must be opened before use.");
  1152. return d->get_current_dir();
  1153. }
  1154. Error _Directory::make_dir(String p_dir) {
  1155. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
  1156. if (!p_dir.is_rel_path()) {
  1157. DirAccess *d = DirAccess::create_for_path(p_dir);
  1158. Error err = d->make_dir(p_dir);
  1159. memdelete(d);
  1160. return err;
  1161. }
  1162. return d->make_dir(p_dir);
  1163. }
  1164. Error _Directory::make_dir_recursive(String p_dir) {
  1165. ERR_FAIL_COND_V_MSG(!d, ERR_UNCONFIGURED, "Directory is not configured properly.");
  1166. if (!p_dir.is_rel_path()) {
  1167. DirAccess *d = DirAccess::create_for_path(p_dir);
  1168. Error err = d->make_dir_recursive(p_dir);
  1169. memdelete(d);
  1170. return err;
  1171. }
  1172. return d->make_dir_recursive(p_dir);
  1173. }
  1174. bool _Directory::file_exists(String p_file) {
  1175. ERR_FAIL_COND_V_MSG(!d, false, "Directory is not configured properly.");
  1176. if (!p_file.is_rel_path()) {
  1177. return FileAccess::exists(p_file);
  1178. }
  1179. return d->file_exists(p_file);
  1180. }
  1181. bool _Directory::dir_exists(String p_dir) {
  1182. ERR_FAIL_COND_V_MSG(!d, false, "Directory is not configured properly.");
  1183. if (!p_dir.is_rel_path()) {
  1184. DirAccess *d = DirAccess::create_for_path(p_dir);
  1185. bool exists = d->dir_exists(p_dir);
  1186. memdelete(d);
  1187. return exists;
  1188. }
  1189. return d->dir_exists(p_dir);
  1190. }
  1191. uint64_t _Directory::get_space_left() {
  1192. ERR_FAIL_COND_V_MSG(!d, 0, "Directory must be opened before use.");
  1193. return d->get_space_left() / 1024 * 1024; // Truncate to closest MiB.
  1194. }
  1195. Error _Directory::copy(String p_from, String p_to) {
  1196. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1197. return d->copy(p_from, p_to);
  1198. }
  1199. Error _Directory::rename(String p_from, String p_to) {
  1200. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1201. if (!p_from.is_rel_path()) {
  1202. DirAccess *d = DirAccess::create_for_path(p_from);
  1203. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from), ERR_DOES_NOT_EXIST, "File does not exist.");
  1204. Error err = d->rename(p_from, p_to);
  1205. memdelete(d);
  1206. return err;
  1207. }
  1208. ERR_FAIL_COND_V_MSG(!d->file_exists(p_from), ERR_DOES_NOT_EXIST, "File does not exist.");
  1209. return d->rename(p_from, p_to);
  1210. }
  1211. Error _Directory::remove(String p_name) {
  1212. ERR_FAIL_COND_V_MSG(!is_open(), ERR_UNCONFIGURED, "Directory must be opened before use.");
  1213. if (!p_name.is_rel_path()) {
  1214. DirAccess *d = DirAccess::create_for_path(p_name);
  1215. Error err = d->remove(p_name);
  1216. memdelete(d);
  1217. return err;
  1218. }
  1219. return d->remove(p_name);
  1220. }
  1221. void _Directory::_bind_methods() {
  1222. ClassDB::bind_method(D_METHOD("open", "path"), &_Directory::open);
  1223. ClassDB::bind_method(D_METHOD("list_dir_begin", "show_navigational", "show_hidden"), &_Directory::list_dir_begin, DEFVAL(false), DEFVAL(false));
  1224. ClassDB::bind_method(D_METHOD("get_next"), &_Directory::get_next);
  1225. ClassDB::bind_method(D_METHOD("current_is_dir"), &_Directory::current_is_dir);
  1226. ClassDB::bind_method(D_METHOD("list_dir_end"), &_Directory::list_dir_end);
  1227. ClassDB::bind_method(D_METHOD("get_drive_count"), &_Directory::get_drive_count);
  1228. ClassDB::bind_method(D_METHOD("get_drive", "idx"), &_Directory::get_drive);
  1229. ClassDB::bind_method(D_METHOD("get_current_drive"), &_Directory::get_current_drive);
  1230. ClassDB::bind_method(D_METHOD("change_dir", "todir"), &_Directory::change_dir);
  1231. ClassDB::bind_method(D_METHOD("get_current_dir"), &_Directory::get_current_dir);
  1232. ClassDB::bind_method(D_METHOD("make_dir", "path"), &_Directory::make_dir);
  1233. ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &_Directory::make_dir_recursive);
  1234. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_Directory::file_exists);
  1235. ClassDB::bind_method(D_METHOD("dir_exists", "path"), &_Directory::dir_exists);
  1236. //ClassDB::bind_method(D_METHOD("get_modified_time","file"),&_Directory::get_modified_time);
  1237. ClassDB::bind_method(D_METHOD("get_space_left"), &_Directory::get_space_left);
  1238. ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy);
  1239. ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename);
  1240. ClassDB::bind_method(D_METHOD("remove", "path"), &_Directory::remove);
  1241. }
  1242. _Directory::_Directory() {
  1243. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1244. }
  1245. _Directory::~_Directory() {
  1246. if (d) {
  1247. memdelete(d);
  1248. }
  1249. }
  1250. ////// _Marshalls //////
  1251. _Marshalls *_Marshalls::singleton = nullptr;
  1252. _Marshalls *_Marshalls::get_singleton() {
  1253. return singleton;
  1254. }
  1255. String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  1256. int len;
  1257. Error err = encode_variant(p_var, nullptr, len, p_full_objects);
  1258. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1259. Vector<uint8_t> buff;
  1260. buff.resize(len);
  1261. uint8_t *w = buff.ptrw();
  1262. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1263. ERR_FAIL_COND_V_MSG(err != OK, "", "Error when trying to encode Variant.");
  1264. String ret = CryptoCore::b64_encode_str(&w[0], len);
  1265. ERR_FAIL_COND_V(ret == "", ret);
  1266. return ret;
  1267. }
  1268. Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  1269. int strlen = p_str.length();
  1270. CharString cstr = p_str.ascii();
  1271. Vector<uint8_t> buf;
  1272. buf.resize(strlen / 4 * 3 + 1);
  1273. uint8_t *w = buf.ptrw();
  1274. size_t len = 0;
  1275. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
  1276. Variant v;
  1277. Error err = decode_variant(v, &w[0], len, nullptr, p_allow_objects);
  1278. ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant.");
  1279. return v;
  1280. }
  1281. String _Marshalls::raw_to_base64(const Vector<uint8_t> &p_arr) {
  1282. String ret = CryptoCore::b64_encode_str(p_arr.ptr(), p_arr.size());
  1283. ERR_FAIL_COND_V(ret == "", ret);
  1284. return ret;
  1285. }
  1286. Vector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
  1287. int strlen = p_str.length();
  1288. CharString cstr = p_str.ascii();
  1289. size_t arr_len = 0;
  1290. Vector<uint8_t> buf;
  1291. {
  1292. buf.resize(strlen / 4 * 3 + 1);
  1293. uint8_t *w = buf.ptrw();
  1294. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, Vector<uint8_t>());
  1295. }
  1296. buf.resize(arr_len);
  1297. return buf;
  1298. }
  1299. String _Marshalls::utf8_to_base64(const String &p_str) {
  1300. CharString cstr = p_str.utf8();
  1301. String ret = CryptoCore::b64_encode_str((unsigned char *)cstr.get_data(), cstr.length());
  1302. ERR_FAIL_COND_V(ret == "", ret);
  1303. return ret;
  1304. }
  1305. String _Marshalls::base64_to_utf8(const String &p_str) {
  1306. int strlen = p_str.length();
  1307. CharString cstr = p_str.ascii();
  1308. Vector<uint8_t> buf;
  1309. buf.resize(strlen / 4 * 3 + 1 + 1);
  1310. uint8_t *w = buf.ptrw();
  1311. size_t len = 0;
  1312. ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
  1313. w[len] = 0;
  1314. String ret = String::utf8((char *)&w[0]);
  1315. return ret;
  1316. }
  1317. void _Marshalls::_bind_methods() {
  1318. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &_Marshalls::variant_to_base64, DEFVAL(false));
  1319. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &_Marshalls::base64_to_variant, DEFVAL(false));
  1320. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &_Marshalls::raw_to_base64);
  1321. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &_Marshalls::base64_to_raw);
  1322. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &_Marshalls::utf8_to_base64);
  1323. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &_Marshalls::base64_to_utf8);
  1324. }
  1325. ////// _Semaphore //////
  1326. void _Semaphore::wait() {
  1327. semaphore.wait();
  1328. }
  1329. Error _Semaphore::try_wait() {
  1330. return semaphore.try_wait() ? OK : ERR_BUSY;
  1331. }
  1332. void _Semaphore::post() {
  1333. semaphore.post();
  1334. }
  1335. void _Semaphore::_bind_methods() {
  1336. ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait);
  1337. ClassDB::bind_method(D_METHOD("try_wait"), &_Semaphore::try_wait);
  1338. ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
  1339. }
  1340. ////// _Mutex //////
  1341. void _Mutex::lock() {
  1342. mutex.lock();
  1343. }
  1344. Error _Mutex::try_lock() {
  1345. return mutex.try_lock();
  1346. }
  1347. void _Mutex::unlock() {
  1348. mutex.unlock();
  1349. }
  1350. void _Mutex::_bind_methods() {
  1351. ClassDB::bind_method(D_METHOD("lock"), &_Mutex::lock);
  1352. ClassDB::bind_method(D_METHOD("try_lock"), &_Mutex::try_lock);
  1353. ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock);
  1354. }
  1355. ////// _Thread //////
  1356. void _Thread::_start_func(void *ud) {
  1357. Ref<_Thread> *tud = (Ref<_Thread> *)ud;
  1358. Ref<_Thread> t = *tud;
  1359. memdelete(tud);
  1360. Callable::CallError ce;
  1361. const Variant *arg[1] = { &t->userdata };
  1362. Thread::set_name(t->target_method);
  1363. t->ret = t->target_instance->call(t->target_method, arg, 1, ce);
  1364. if (ce.error != Callable::CallError::CALL_OK) {
  1365. String reason;
  1366. switch (ce.error) {
  1367. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1368. reason = "Invalid Argument #" + itos(ce.argument);
  1369. } break;
  1370. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1371. reason = "Too Many Arguments";
  1372. } break;
  1373. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1374. reason = "Too Few Arguments";
  1375. } break;
  1376. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1377. reason = "Method Not Found";
  1378. } break;
  1379. default: {
  1380. }
  1381. }
  1382. ERR_FAIL_MSG("Could not call function '" + t->target_method.operator String() + "' to start thread " + t->get_id() + ": " + reason + ".");
  1383. }
  1384. }
  1385. Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {
  1386. ERR_FAIL_COND_V_MSG(active.is_set(), ERR_ALREADY_IN_USE, "Thread already started.");
  1387. ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
  1388. ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
  1389. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  1390. ret = Variant();
  1391. target_method = p_method;
  1392. target_instance = p_instance;
  1393. userdata = p_userdata;
  1394. active.set();
  1395. Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
  1396. Thread::Settings s;
  1397. s.priority = (Thread::Priority)p_priority;
  1398. thread.start(_start_func, ud, s);
  1399. return OK;
  1400. }
  1401. String _Thread::get_id() const {
  1402. return itos(thread.get_id());
  1403. }
  1404. bool _Thread::is_active() const {
  1405. return active.is_set();
  1406. }
  1407. Variant _Thread::wait_to_finish() {
  1408. ERR_FAIL_COND_V_MSG(!active.is_set(), Variant(), "Thread must be active to wait for its completion.");
  1409. thread.wait_to_finish();
  1410. Variant r = ret;
  1411. active.clear();
  1412. target_method = StringName();
  1413. target_instance = nullptr;
  1414. userdata = Variant();
  1415. return r;
  1416. }
  1417. void _Thread::_bind_methods() {
  1418. ClassDB::bind_method(D_METHOD("start", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
  1419. ClassDB::bind_method(D_METHOD("get_id"), &_Thread::get_id);
  1420. ClassDB::bind_method(D_METHOD("is_active"), &_Thread::is_active);
  1421. ClassDB::bind_method(D_METHOD("wait_to_finish"), &_Thread::wait_to_finish);
  1422. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  1423. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  1424. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  1425. }
  1426. ////// _ClassDB //////
  1427. PackedStringArray _ClassDB::get_class_list() const {
  1428. List<StringName> classes;
  1429. ClassDB::get_class_list(&classes);
  1430. PackedStringArray ret;
  1431. ret.resize(classes.size());
  1432. int idx = 0;
  1433. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  1434. ret.set(idx++, E->get());
  1435. }
  1436. return ret;
  1437. }
  1438. PackedStringArray _ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  1439. List<StringName> classes;
  1440. ClassDB::get_inheriters_from_class(p_class, &classes);
  1441. PackedStringArray ret;
  1442. ret.resize(classes.size());
  1443. int idx = 0;
  1444. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  1445. ret.set(idx++, E->get());
  1446. }
  1447. return ret;
  1448. }
  1449. StringName _ClassDB::get_parent_class(const StringName &p_class) const {
  1450. return ClassDB::get_parent_class(p_class);
  1451. }
  1452. bool _ClassDB::class_exists(const StringName &p_class) const {
  1453. return ClassDB::class_exists(p_class);
  1454. }
  1455. bool _ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  1456. return ClassDB::is_parent_class(p_class, p_inherits);
  1457. }
  1458. bool _ClassDB::can_instantiate(const StringName &p_class) const {
  1459. return ClassDB::can_instantiate(p_class);
  1460. }
  1461. Variant _ClassDB::instantiate(const StringName &p_class) const {
  1462. Object *obj = ClassDB::instantiate(p_class);
  1463. if (!obj) {
  1464. return Variant();
  1465. }
  1466. RefCounted *r = Object::cast_to<RefCounted>(obj);
  1467. if (r) {
  1468. return REF(r);
  1469. } else {
  1470. return obj;
  1471. }
  1472. }
  1473. bool _ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  1474. return ClassDB::has_signal(p_class, p_signal);
  1475. }
  1476. Dictionary _ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  1477. MethodInfo signal;
  1478. if (ClassDB::get_signal(p_class, p_signal, &signal)) {
  1479. return signal.operator Dictionary();
  1480. } else {
  1481. return Dictionary();
  1482. }
  1483. }
  1484. Array _ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  1485. List<MethodInfo> signals;
  1486. ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  1487. Array ret;
  1488. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  1489. ret.push_back(E->get().operator Dictionary());
  1490. }
  1491. return ret;
  1492. }
  1493. Array _ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  1494. List<PropertyInfo> plist;
  1495. ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  1496. Array ret;
  1497. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  1498. ret.push_back(E->get().operator Dictionary());
  1499. }
  1500. return ret;
  1501. }
  1502. Variant _ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  1503. Variant ret;
  1504. ClassDB::get_property(p_object, p_property, ret);
  1505. return ret;
  1506. }
  1507. Error _ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  1508. Variant ret;
  1509. bool valid;
  1510. if (!ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  1511. return ERR_UNAVAILABLE;
  1512. } else if (!valid) {
  1513. return ERR_INVALID_DATA;
  1514. }
  1515. return OK;
  1516. }
  1517. bool _ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  1518. return ClassDB::has_method(p_class, p_method, p_no_inheritance);
  1519. }
  1520. Array _ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  1521. List<MethodInfo> methods;
  1522. ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  1523. Array ret;
  1524. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  1525. #ifdef DEBUG_METHODS_ENABLED
  1526. ret.push_back(E->get().operator Dictionary());
  1527. #else
  1528. Dictionary dict;
  1529. dict["name"] = E->get().name;
  1530. ret.push_back(dict);
  1531. #endif
  1532. }
  1533. return ret;
  1534. }
  1535. PackedStringArray _ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  1536. List<String> constants;
  1537. ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  1538. PackedStringArray ret;
  1539. ret.resize(constants.size());
  1540. int idx = 0;
  1541. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  1542. ret.set(idx++, E->get());
  1543. }
  1544. return ret;
  1545. }
  1546. bool _ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1547. bool success;
  1548. ClassDB::get_integer_constant(p_class, p_name, &success);
  1549. return success;
  1550. }
  1551. int _ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  1552. bool found;
  1553. int c = ClassDB::get_integer_constant(p_class, p_name, &found);
  1554. ERR_FAIL_COND_V(!found, 0);
  1555. return c;
  1556. }
  1557. StringName _ClassDB::get_category(const StringName &p_node) const {
  1558. return ClassDB::get_category(p_node);
  1559. }
  1560. bool _ClassDB::is_class_enabled(StringName p_class) const {
  1561. return ClassDB::is_class_enabled(p_class);
  1562. }
  1563. void _ClassDB::_bind_methods() {
  1564. ClassDB::bind_method(D_METHOD("get_class_list"), &_ClassDB::get_class_list);
  1565. ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &_ClassDB::get_inheriters_from_class);
  1566. ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &_ClassDB::get_parent_class);
  1567. ClassDB::bind_method(D_METHOD("class_exists", "class"), &_ClassDB::class_exists);
  1568. ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &_ClassDB::is_parent_class);
  1569. ClassDB::bind_method(D_METHOD("can_instantiate", "class"), &_ClassDB::can_instantiate);
  1570. ClassDB::bind_method(D_METHOD("instantiate", "class"), &_ClassDB::instantiate);
  1571. ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &_ClassDB::has_signal);
  1572. ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &_ClassDB::get_signal);
  1573. ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &_ClassDB::get_signal_list, DEFVAL(false));
  1574. ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &_ClassDB::get_property_list, DEFVAL(false));
  1575. ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &_ClassDB::get_property);
  1576. ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &_ClassDB::set_property);
  1577. ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &_ClassDB::has_method, DEFVAL(false));
  1578. ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &_ClassDB::get_method_list, DEFVAL(false));
  1579. ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &_ClassDB::get_integer_constant_list, DEFVAL(false));
  1580. ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &_ClassDB::has_integer_constant);
  1581. ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &_ClassDB::get_integer_constant);
  1582. ClassDB::bind_method(D_METHOD("class_get_category", "class"), &_ClassDB::get_category);
  1583. ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &_ClassDB::is_class_enabled);
  1584. }
  1585. ////// _Engine //////
  1586. void _Engine::set_iterations_per_second(int p_ips) {
  1587. Engine::get_singleton()->set_iterations_per_second(p_ips);
  1588. }
  1589. int _Engine::get_iterations_per_second() const {
  1590. return Engine::get_singleton()->get_iterations_per_second();
  1591. }
  1592. void _Engine::set_physics_jitter_fix(float p_threshold) {
  1593. Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  1594. }
  1595. float _Engine::get_physics_jitter_fix() const {
  1596. return Engine::get_singleton()->get_physics_jitter_fix();
  1597. }
  1598. float _Engine::get_physics_interpolation_fraction() const {
  1599. return Engine::get_singleton()->get_physics_interpolation_fraction();
  1600. }
  1601. void _Engine::set_target_fps(int p_fps) {
  1602. Engine::get_singleton()->set_target_fps(p_fps);
  1603. }
  1604. int _Engine::get_target_fps() const {
  1605. return Engine::get_singleton()->get_target_fps();
  1606. }
  1607. float _Engine::get_frames_per_second() const {
  1608. return Engine::get_singleton()->get_frames_per_second();
  1609. }
  1610. uint64_t _Engine::get_physics_frames() const {
  1611. return Engine::get_singleton()->get_physics_frames();
  1612. }
  1613. uint64_t _Engine::get_process_frames() const {
  1614. return Engine::get_singleton()->get_process_frames();
  1615. }
  1616. void _Engine::set_time_scale(float p_scale) {
  1617. Engine::get_singleton()->set_time_scale(p_scale);
  1618. }
  1619. float _Engine::get_time_scale() {
  1620. return Engine::get_singleton()->get_time_scale();
  1621. }
  1622. int _Engine::get_frames_drawn() {
  1623. return Engine::get_singleton()->get_frames_drawn();
  1624. }
  1625. MainLoop *_Engine::get_main_loop() const {
  1626. //needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  1627. return OS::get_singleton()->get_main_loop();
  1628. }
  1629. Dictionary _Engine::get_version_info() const {
  1630. return Engine::get_singleton()->get_version_info();
  1631. }
  1632. Dictionary _Engine::get_author_info() const {
  1633. return Engine::get_singleton()->get_author_info();
  1634. }
  1635. Array _Engine::get_copyright_info() const {
  1636. return Engine::get_singleton()->get_copyright_info();
  1637. }
  1638. Dictionary _Engine::get_donor_info() const {
  1639. return Engine::get_singleton()->get_donor_info();
  1640. }
  1641. Dictionary _Engine::get_license_info() const {
  1642. return Engine::get_singleton()->get_license_info();
  1643. }
  1644. String _Engine::get_license_text() const {
  1645. return Engine::get_singleton()->get_license_text();
  1646. }
  1647. bool _Engine::is_in_physics_frame() const {
  1648. return Engine::get_singleton()->is_in_physics_frame();
  1649. }
  1650. bool _Engine::has_singleton(const String &p_name) const {
  1651. return Engine::get_singleton()->has_singleton(p_name);
  1652. }
  1653. Object *_Engine::get_singleton_object(const String &p_name) const {
  1654. return Engine::get_singleton()->get_singleton_object(p_name);
  1655. }
  1656. void _Engine::set_editor_hint(bool p_enabled) {
  1657. Engine::get_singleton()->set_editor_hint(p_enabled);
  1658. }
  1659. bool _Engine::is_editor_hint() const {
  1660. return Engine::get_singleton()->is_editor_hint();
  1661. }
  1662. void _Engine::_bind_methods() {
  1663. ClassDB::bind_method(D_METHOD("set_iterations_per_second", "iterations_per_second"), &_Engine::set_iterations_per_second);
  1664. ClassDB::bind_method(D_METHOD("get_iterations_per_second"), &_Engine::get_iterations_per_second);
  1665. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &_Engine::set_physics_jitter_fix);
  1666. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &_Engine::get_physics_jitter_fix);
  1667. ClassDB::bind_method(D_METHOD("get_physics_interpolation_fraction"), &_Engine::get_physics_interpolation_fraction);
  1668. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &_Engine::set_target_fps);
  1669. ClassDB::bind_method(D_METHOD("get_target_fps"), &_Engine::get_target_fps);
  1670. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &_Engine::set_time_scale);
  1671. ClassDB::bind_method(D_METHOD("get_time_scale"), &_Engine::get_time_scale);
  1672. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &_Engine::get_frames_drawn);
  1673. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &_Engine::get_frames_per_second);
  1674. ClassDB::bind_method(D_METHOD("get_physics_frames"), &_Engine::get_physics_frames);
  1675. ClassDB::bind_method(D_METHOD("get_process_frames"), &_Engine::get_process_frames);
  1676. ClassDB::bind_method(D_METHOD("get_main_loop"), &_Engine::get_main_loop);
  1677. ClassDB::bind_method(D_METHOD("get_version_info"), &_Engine::get_version_info);
  1678. ClassDB::bind_method(D_METHOD("get_author_info"), &_Engine::get_author_info);
  1679. ClassDB::bind_method(D_METHOD("get_copyright_info"), &_Engine::get_copyright_info);
  1680. ClassDB::bind_method(D_METHOD("get_donor_info"), &_Engine::get_donor_info);
  1681. ClassDB::bind_method(D_METHOD("get_license_info"), &_Engine::get_license_info);
  1682. ClassDB::bind_method(D_METHOD("get_license_text"), &_Engine::get_license_text);
  1683. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &_Engine::is_in_physics_frame);
  1684. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &_Engine::has_singleton);
  1685. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &_Engine::get_singleton_object);
  1686. ClassDB::bind_method(D_METHOD("set_editor_hint", "enabled"), &_Engine::set_editor_hint);
  1687. ClassDB::bind_method(D_METHOD("is_editor_hint"), &_Engine::is_editor_hint);
  1688. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_hint"), "set_editor_hint", "is_editor_hint");
  1689. ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations_per_second"), "set_iterations_per_second", "get_iterations_per_second");
  1690. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  1691. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_scale"), "set_time_scale", "get_time_scale");
  1692. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  1693. }
  1694. _Engine *_Engine::singleton = nullptr;
  1695. ////// _EngineDebugger //////
  1696. void _EngineDebugger::_bind_methods() {
  1697. ClassDB::bind_method(D_METHOD("is_active"), &_EngineDebugger::is_active);
  1698. ClassDB::bind_method(D_METHOD("register_profiler", "name", "toggle", "add", "tick"), &_EngineDebugger::register_profiler);
  1699. ClassDB::bind_method(D_METHOD("unregister_profiler", "name"), &_EngineDebugger::unregister_profiler);
  1700. ClassDB::bind_method(D_METHOD("is_profiling", "name"), &_EngineDebugger::is_profiling);
  1701. ClassDB::bind_method(D_METHOD("has_profiler", "name"), &_EngineDebugger::has_profiler);
  1702. ClassDB::bind_method(D_METHOD("profiler_add_frame_data", "name", "data"), &_EngineDebugger::profiler_add_frame_data);
  1703. ClassDB::bind_method(D_METHOD("profiler_enable", "name", "enable", "arguments"), &_EngineDebugger::profiler_enable, DEFVAL(Array()));
  1704. ClassDB::bind_method(D_METHOD("register_message_capture", "name", "callable"), &_EngineDebugger::register_message_capture);
  1705. ClassDB::bind_method(D_METHOD("unregister_message_capture", "name"), &_EngineDebugger::unregister_message_capture);
  1706. ClassDB::bind_method(D_METHOD("has_capture", "name"), &_EngineDebugger::has_capture);
  1707. ClassDB::bind_method(D_METHOD("send_message", "message", "data"), &_EngineDebugger::send_message);
  1708. }
  1709. bool _EngineDebugger::is_active() {
  1710. return EngineDebugger::is_active();
  1711. }
  1712. void _EngineDebugger::register_profiler(const StringName &p_name, const Callable &p_toggle, const Callable &p_add, const Callable &p_tick) {
  1713. ERR_FAIL_COND_MSG(profilers.has(p_name) || has_profiler(p_name), "Profiler already registered: " + p_name);
  1714. profilers.insert(p_name, ProfilerCallable(p_toggle, p_add, p_tick));
  1715. ProfilerCallable &p = profilers[p_name];
  1716. EngineDebugger::Profiler profiler(
  1717. &p,
  1718. &_EngineDebugger::call_toggle,
  1719. &_EngineDebugger::call_add,
  1720. &_EngineDebugger::call_tick);
  1721. EngineDebugger::register_profiler(p_name, profiler);
  1722. }
  1723. void _EngineDebugger::unregister_profiler(const StringName &p_name) {
  1724. ERR_FAIL_COND_MSG(!profilers.has(p_name), "Profiler not registered: " + p_name);
  1725. EngineDebugger::unregister_profiler(p_name);
  1726. profilers.erase(p_name);
  1727. }
  1728. bool _EngineDebugger::_EngineDebugger::is_profiling(const StringName &p_name) {
  1729. return EngineDebugger::is_profiling(p_name);
  1730. }
  1731. bool _EngineDebugger::has_profiler(const StringName &p_name) {
  1732. return EngineDebugger::has_profiler(p_name);
  1733. }
  1734. void _EngineDebugger::profiler_add_frame_data(const StringName &p_name, const Array &p_data) {
  1735. EngineDebugger::profiler_add_frame_data(p_name, p_data);
  1736. }
  1737. void _EngineDebugger::profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts) {
  1738. if (EngineDebugger::get_singleton()) {
  1739. EngineDebugger::get_singleton()->profiler_enable(p_name, p_enabled, p_opts);
  1740. }
  1741. }
  1742. void _EngineDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1743. ERR_FAIL_COND_MSG(captures.has(p_name) || has_capture(p_name), "Capture already registered: " + p_name);
  1744. captures.insert(p_name, p_callable);
  1745. Callable &c = captures[p_name];
  1746. EngineDebugger::Capture capture(&c, &_EngineDebugger::call_capture);
  1747. EngineDebugger::register_message_capture(p_name, capture);
  1748. }
  1749. void _EngineDebugger::unregister_message_capture(const StringName &p_name) {
  1750. ERR_FAIL_COND_MSG(!captures.has(p_name), "Capture not registered: " + p_name);
  1751. EngineDebugger::unregister_message_capture(p_name);
  1752. captures.erase(p_name);
  1753. }
  1754. bool _EngineDebugger::has_capture(const StringName &p_name) {
  1755. return EngineDebugger::has_capture(p_name);
  1756. }
  1757. void _EngineDebugger::send_message(const String &p_msg, const Array &p_data) {
  1758. ERR_FAIL_COND_MSG(!EngineDebugger::is_active(), "Can't send message. No active debugger");
  1759. EngineDebugger::get_singleton()->send_message(p_msg, p_data);
  1760. }
  1761. void _EngineDebugger::call_toggle(void *p_user, bool p_enable, const Array &p_opts) {
  1762. Callable &toggle = ((ProfilerCallable *)p_user)->callable_toggle;
  1763. if (toggle.is_null()) {
  1764. return;
  1765. }
  1766. Variant enable = p_enable, opts = p_opts;
  1767. const Variant *args[2] = { &enable, &opts };
  1768. Variant retval;
  1769. Callable::CallError err;
  1770. toggle.call(args, 2, retval, err);
  1771. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'toggle' to callable: " + Variant::get_callable_error_text(toggle, args, 2, err));
  1772. }
  1773. void _EngineDebugger::call_add(void *p_user, const Array &p_data) {
  1774. Callable &add = ((ProfilerCallable *)p_user)->callable_add;
  1775. if (add.is_null()) {
  1776. return;
  1777. }
  1778. Variant data = p_data;
  1779. const Variant *args[1] = { &data };
  1780. Variant retval;
  1781. Callable::CallError err;
  1782. add.call(args, 1, retval, err);
  1783. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'add' to callable: " + Variant::get_callable_error_text(add, args, 1, err));
  1784. }
  1785. void _EngineDebugger::call_tick(void *p_user, float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time) {
  1786. Callable &tick = ((ProfilerCallable *)p_user)->callable_tick;
  1787. if (tick.is_null()) {
  1788. return;
  1789. }
  1790. Variant frame_time = p_frame_time, idle_time = p_idle_time, physics_time = p_physics_time, physics_frame_time = p_physics_frame_time;
  1791. const Variant *args[4] = { &frame_time, &idle_time, &physics_time, &physics_frame_time };
  1792. Variant retval;
  1793. Callable::CallError err;
  1794. tick.call(args, 4, retval, err);
  1795. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'tick' to callable: " + Variant::get_callable_error_text(tick, args, 4, err));
  1796. }
  1797. Error _EngineDebugger::call_capture(void *p_user, const String &p_cmd, const Array &p_data, bool &r_captured) {
  1798. Callable &capture = *(Callable *)p_user;
  1799. if (capture.is_null()) {
  1800. return FAILED;
  1801. }
  1802. Variant cmd = p_cmd, data = p_data;
  1803. const Variant *args[2] = { &cmd, &data };
  1804. Variant retval;
  1805. Callable::CallError err;
  1806. capture.call(args, 2, retval, err);
  1807. 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));
  1808. ERR_FAIL_COND_V_MSG(retval.get_type() != Variant::BOOL, FAILED, "Error calling 'capture' to callable: " + String(capture) + ". Return type is not bool.");
  1809. r_captured = retval;
  1810. return OK;
  1811. }
  1812. _EngineDebugger::~_EngineDebugger() {
  1813. for (Map<StringName, Callable>::Element *E = captures.front(); E; E = E->next()) {
  1814. EngineDebugger::unregister_message_capture(E->key());
  1815. }
  1816. captures.clear();
  1817. for (Map<StringName, ProfilerCallable>::Element *E = profilers.front(); E; E = E->next()) {
  1818. EngineDebugger::unregister_profiler(E->key());
  1819. }
  1820. profilers.clear();
  1821. }
  1822. _EngineDebugger *_EngineDebugger::singleton = nullptr;