core_bind.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109
  1. /*************************************************************************/
  2. /* core_bind.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/io/file_access_compressed.h"
  32. #include "core/io/file_access_encrypted.h"
  33. #include "core/io/json.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/math/geometry.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "core/project_settings.h"
  39. #include "thirdparty/misc/base64.h"
  40. /**
  41. * Time constants borrowed from loc_time.h
  42. */
  43. #define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
  44. #define SECS_DAY (24L * 60L * 60L)
  45. #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
  46. #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
  47. #define SECOND_KEY "second"
  48. #define MINUTE_KEY "minute"
  49. #define HOUR_KEY "hour"
  50. #define DAY_KEY "day"
  51. #define MONTH_KEY "month"
  52. #define YEAR_KEY "year"
  53. #define WEEKDAY_KEY "weekday"
  54. #define DST_KEY "dst"
  55. /// Table of number of days in each month (for regular year and leap year)
  56. static const unsigned int MONTH_DAYS_TABLE[2][12] = {
  57. { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  58. { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  59. };
  60. _ResourceLoader *_ResourceLoader::singleton = NULL;
  61. Ref<ResourceInteractiveLoader> _ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint) {
  62. return ResourceLoader::load_interactive(p_path, p_type_hint);
  63. }
  64. RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache) {
  65. Error err = OK;
  66. RES ret = ResourceLoader::load(p_path, p_type_hint, p_no_cache, &err);
  67. if (err != OK) {
  68. ERR_EXPLAIN("Error loading resource: '" + p_path + "'");
  69. ERR_FAIL_COND_V(err != OK, ret);
  70. }
  71. return ret;
  72. }
  73. PoolVector<String> _ResourceLoader::get_recognized_extensions_for_type(const String &p_type) {
  74. List<String> exts;
  75. ResourceLoader::get_recognized_extensions_for_type(p_type, &exts);
  76. PoolVector<String> ret;
  77. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  78. ret.push_back(E->get());
  79. }
  80. return ret;
  81. }
  82. void _ResourceLoader::set_abort_on_missing_resources(bool p_abort) {
  83. ResourceLoader::set_abort_on_missing_resources(p_abort);
  84. }
  85. PoolStringArray _ResourceLoader::get_dependencies(const String &p_path) {
  86. List<String> deps;
  87. ResourceLoader::get_dependencies(p_path, &deps);
  88. PoolStringArray ret;
  89. for (List<String>::Element *E = deps.front(); E; E = E->next()) {
  90. ret.push_back(E->get());
  91. }
  92. return ret;
  93. };
  94. #ifndef DISABLE_DEPRECATED
  95. bool _ResourceLoader::has(const String &p_path) {
  96. WARN_PRINTS("ResourceLoader.has() is deprecated, please replace it with the equivalent has_cached() or the new exists().");
  97. return has_cached(p_path);
  98. }
  99. #endif // DISABLE_DEPRECATED
  100. bool _ResourceLoader::has_cached(const String &p_path) {
  101. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  102. return ResourceCache::has(local_path);
  103. }
  104. bool _ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  105. return ResourceLoader::exists(p_path, p_type_hint);
  106. }
  107. void _ResourceLoader::_bind_methods() {
  108. ClassDB::bind_method(D_METHOD("load_interactive", "path", "type_hint"), &_ResourceLoader::load_interactive, DEFVAL(""));
  109. ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "no_cache"), &_ResourceLoader::load, DEFVAL(""), DEFVAL(false));
  110. ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type);
  111. ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources);
  112. ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
  113. ClassDB::bind_method(D_METHOD("has_cached", "path"), &_ResourceLoader::has_cached);
  114. ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &_ResourceLoader::exists, DEFVAL(""));
  115. #ifndef DISABLE_DEPRECATED
  116. ClassDB::bind_method(D_METHOD("has", "path"), &_ResourceLoader::has);
  117. #endif // DISABLE_DEPRECATED
  118. }
  119. _ResourceLoader::_ResourceLoader() {
  120. singleton = this;
  121. }
  122. Error _ResourceSaver::save(const String &p_path, const RES &p_resource, SaverFlags p_flags) {
  123. ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
  124. return ResourceSaver::save(p_path, p_resource, p_flags);
  125. }
  126. PoolVector<String> _ResourceSaver::get_recognized_extensions(const RES &p_resource) {
  127. ERR_FAIL_COND_V(p_resource.is_null(), PoolVector<String>());
  128. List<String> exts;
  129. ResourceSaver::get_recognized_extensions(p_resource, &exts);
  130. PoolVector<String> ret;
  131. for (List<String>::Element *E = exts.front(); E; E = E->next()) {
  132. ret.push_back(E->get());
  133. }
  134. return ret;
  135. }
  136. _ResourceSaver *_ResourceSaver::singleton = NULL;
  137. void _ResourceSaver::_bind_methods() {
  138. ClassDB::bind_method(D_METHOD("save", "path", "resource", "flags"), &_ResourceSaver::save, DEFVAL(0));
  139. ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &_ResourceSaver::get_recognized_extensions);
  140. BIND_ENUM_CONSTANT(FLAG_RELATIVE_PATHS);
  141. BIND_ENUM_CONSTANT(FLAG_BUNDLE_RESOURCES);
  142. BIND_ENUM_CONSTANT(FLAG_CHANGE_PATH);
  143. BIND_ENUM_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
  144. BIND_ENUM_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
  145. BIND_ENUM_CONSTANT(FLAG_COMPRESS);
  146. BIND_ENUM_CONSTANT(FLAG_REPLACE_SUBRESOURCE_PATHS);
  147. }
  148. _ResourceSaver::_ResourceSaver() {
  149. singleton = this;
  150. }
  151. /////////////////OS
  152. Point2 _OS::get_mouse_position() const {
  153. return OS::get_singleton()->get_mouse_position();
  154. }
  155. void _OS::set_window_title(const String &p_title) {
  156. OS::get_singleton()->set_window_title(p_title);
  157. }
  158. int _OS::get_mouse_button_state() const {
  159. return OS::get_singleton()->get_mouse_button_state();
  160. }
  161. String _OS::get_unique_id() const {
  162. return OS::get_singleton()->get_unique_id();
  163. }
  164. bool _OS::has_touchscreen_ui_hint() const {
  165. return OS::get_singleton()->has_touchscreen_ui_hint();
  166. }
  167. void _OS::set_clipboard(const String &p_text) {
  168. OS::get_singleton()->set_clipboard(p_text);
  169. }
  170. String _OS::get_clipboard() const {
  171. return OS::get_singleton()->get_clipboard();
  172. }
  173. int _OS::get_video_driver_count() const {
  174. return OS::get_singleton()->get_video_driver_count();
  175. }
  176. String _OS::get_video_driver_name(VideoDriver p_driver) const {
  177. return OS::get_singleton()->get_video_driver_name((int)p_driver);
  178. }
  179. _OS::VideoDriver _OS::get_current_video_driver() const {
  180. return (VideoDriver)OS::get_singleton()->get_current_video_driver();
  181. }
  182. int _OS::get_audio_driver_count() const {
  183. return OS::get_singleton()->get_audio_driver_count();
  184. }
  185. String _OS::get_audio_driver_name(int p_driver) const {
  186. return OS::get_singleton()->get_audio_driver_name(p_driver);
  187. }
  188. PoolStringArray _OS::get_connected_midi_inputs() {
  189. return OS::get_singleton()->get_connected_midi_inputs();
  190. }
  191. void _OS::open_midi_inputs() {
  192. return OS::get_singleton()->open_midi_inputs();
  193. }
  194. void _OS::close_midi_inputs() {
  195. return OS::get_singleton()->close_midi_inputs();
  196. }
  197. void _OS::set_video_mode(const Size2 &p_size, bool p_fullscreen, bool p_resizeable, int p_screen) {
  198. OS::VideoMode vm;
  199. vm.width = p_size.width;
  200. vm.height = p_size.height;
  201. vm.fullscreen = p_fullscreen;
  202. vm.resizable = p_resizeable;
  203. OS::get_singleton()->set_video_mode(vm, p_screen);
  204. }
  205. Size2 _OS::get_video_mode(int p_screen) const {
  206. OS::VideoMode vm;
  207. vm = OS::get_singleton()->get_video_mode(p_screen);
  208. return Size2(vm.width, vm.height);
  209. }
  210. bool _OS::is_video_mode_fullscreen(int p_screen) const {
  211. OS::VideoMode vm;
  212. vm = OS::get_singleton()->get_video_mode(p_screen);
  213. return vm.fullscreen;
  214. }
  215. int _OS::get_screen_count() const {
  216. return OS::get_singleton()->get_screen_count();
  217. }
  218. int _OS::get_current_screen() const {
  219. return OS::get_singleton()->get_current_screen();
  220. }
  221. void _OS::set_current_screen(int p_screen) {
  222. OS::get_singleton()->set_current_screen(p_screen);
  223. }
  224. Point2 _OS::get_screen_position(int p_screen) const {
  225. return OS::get_singleton()->get_screen_position(p_screen);
  226. }
  227. Size2 _OS::get_screen_size(int p_screen) const {
  228. return OS::get_singleton()->get_screen_size(p_screen);
  229. }
  230. int _OS::get_screen_dpi(int p_screen) const {
  231. return OS::get_singleton()->get_screen_dpi(p_screen);
  232. }
  233. Point2 _OS::get_window_position() const {
  234. return OS::get_singleton()->get_window_position();
  235. }
  236. void _OS::set_window_position(const Point2 &p_position) {
  237. OS::get_singleton()->set_window_position(p_position);
  238. }
  239. Size2 _OS::get_window_size() const {
  240. return OS::get_singleton()->get_window_size();
  241. }
  242. Size2 _OS::get_real_window_size() const {
  243. return OS::get_singleton()->get_real_window_size();
  244. }
  245. void _OS::set_window_size(const Size2 &p_size) {
  246. OS::get_singleton()->set_window_size(p_size);
  247. }
  248. Rect2 _OS::get_window_safe_area() const {
  249. return OS::get_singleton()->get_window_safe_area();
  250. }
  251. void _OS::set_window_fullscreen(bool p_enabled) {
  252. OS::get_singleton()->set_window_fullscreen(p_enabled);
  253. }
  254. bool _OS::is_window_fullscreen() const {
  255. return OS::get_singleton()->is_window_fullscreen();
  256. }
  257. void _OS::set_window_resizable(bool p_enabled) {
  258. OS::get_singleton()->set_window_resizable(p_enabled);
  259. }
  260. bool _OS::is_window_resizable() const {
  261. return OS::get_singleton()->is_window_resizable();
  262. }
  263. void _OS::set_window_minimized(bool p_enabled) {
  264. OS::get_singleton()->set_window_minimized(p_enabled);
  265. }
  266. bool _OS::is_window_minimized() const {
  267. return OS::get_singleton()->is_window_minimized();
  268. }
  269. void _OS::set_window_maximized(bool p_enabled) {
  270. OS::get_singleton()->set_window_maximized(p_enabled);
  271. }
  272. bool _OS::is_window_maximized() const {
  273. return OS::get_singleton()->is_window_maximized();
  274. }
  275. void _OS::set_window_always_on_top(bool p_enabled) {
  276. OS::get_singleton()->set_window_always_on_top(p_enabled);
  277. }
  278. bool _OS::is_window_always_on_top() const {
  279. return OS::get_singleton()->is_window_always_on_top();
  280. }
  281. void _OS::set_borderless_window(bool p_borderless) {
  282. OS::get_singleton()->set_borderless_window(p_borderless);
  283. }
  284. bool _OS::get_window_per_pixel_transparency_enabled() const {
  285. return OS::get_singleton()->get_window_per_pixel_transparency_enabled();
  286. }
  287. void _OS::set_window_per_pixel_transparency_enabled(bool p_enabled) {
  288. OS::get_singleton()->set_window_per_pixel_transparency_enabled(p_enabled);
  289. }
  290. bool _OS::get_borderless_window() const {
  291. return OS::get_singleton()->get_borderless_window();
  292. }
  293. void _OS::set_ime_active(const bool p_active) {
  294. OS::get_singleton()->set_ime_active(p_active);
  295. }
  296. void _OS::set_ime_position(const Point2 &p_pos) {
  297. OS::get_singleton()->set_ime_position(p_pos);
  298. }
  299. Point2 _OS::get_ime_selection() const {
  300. return OS::get_singleton()->get_ime_selection();
  301. }
  302. String _OS::get_ime_text() const {
  303. return OS::get_singleton()->get_ime_text();
  304. }
  305. void _OS::set_use_file_access_save_and_swap(bool p_enable) {
  306. FileAccess::set_backup_save(p_enable);
  307. }
  308. bool _OS::is_video_mode_resizable(int p_screen) const {
  309. OS::VideoMode vm;
  310. vm = OS::get_singleton()->get_video_mode(p_screen);
  311. return vm.resizable;
  312. }
  313. Array _OS::get_fullscreen_mode_list(int p_screen) const {
  314. List<OS::VideoMode> vmlist;
  315. OS::get_singleton()->get_fullscreen_mode_list(&vmlist, p_screen);
  316. Array vmarr;
  317. for (List<OS::VideoMode>::Element *E = vmlist.front(); E; E = E->next()) {
  318. vmarr.push_back(Size2(E->get().width, E->get().height));
  319. }
  320. return vmarr;
  321. }
  322. void _OS::set_low_processor_usage_mode(bool p_enabled) {
  323. OS::get_singleton()->set_low_processor_usage_mode(p_enabled);
  324. }
  325. bool _OS::is_in_low_processor_usage_mode() const {
  326. return OS::get_singleton()->is_in_low_processor_usage_mode();
  327. }
  328. String _OS::get_executable_path() const {
  329. return OS::get_singleton()->get_executable_path();
  330. }
  331. Error _OS::shell_open(String p_uri) {
  332. return OS::get_singleton()->shell_open(p_uri);
  333. };
  334. int _OS::execute(const String &p_path, const Vector<String> &p_arguments, bool p_blocking, Array p_output) {
  335. OS::ProcessID pid = -2;
  336. List<String> args;
  337. for (int i = 0; i < p_arguments.size(); i++)
  338. args.push_back(p_arguments[i]);
  339. String pipe;
  340. Error err = OS::get_singleton()->execute(p_path, args, p_blocking, &pid, &pipe);
  341. p_output.clear();
  342. p_output.push_back(pipe);
  343. if (err != OK)
  344. return -1;
  345. else
  346. return pid;
  347. }
  348. Error _OS::kill(int p_pid) {
  349. return OS::get_singleton()->kill(p_pid);
  350. }
  351. int _OS::get_process_id() const {
  352. return OS::get_singleton()->get_process_id();
  353. };
  354. bool _OS::has_environment(const String &p_var) const {
  355. return OS::get_singleton()->has_environment(p_var);
  356. }
  357. String _OS::get_environment(const String &p_var) const {
  358. return OS::get_singleton()->get_environment(p_var);
  359. }
  360. String _OS::get_name() const {
  361. return OS::get_singleton()->get_name();
  362. }
  363. Vector<String> _OS::get_cmdline_args() {
  364. List<String> cmdline = OS::get_singleton()->get_cmdline_args();
  365. Vector<String> cmdlinev;
  366. for (List<String>::Element *E = cmdline.front(); E; E = E->next()) {
  367. cmdlinev.push_back(E->get());
  368. }
  369. return cmdlinev;
  370. }
  371. String _OS::get_locale() const {
  372. return OS::get_singleton()->get_locale();
  373. }
  374. String _OS::get_latin_keyboard_variant() const {
  375. switch (OS::get_singleton()->get_latin_keyboard_variant()) {
  376. case OS::LATIN_KEYBOARD_QWERTY: return "QWERTY";
  377. case OS::LATIN_KEYBOARD_QWERTZ: return "QWERTZ";
  378. case OS::LATIN_KEYBOARD_AZERTY: return "AZERTY";
  379. case OS::LATIN_KEYBOARD_QZERTY: return "QZERTY";
  380. case OS::LATIN_KEYBOARD_DVORAK: return "DVORAK";
  381. case OS::LATIN_KEYBOARD_NEO: return "NEO";
  382. case OS::LATIN_KEYBOARD_COLEMAK: return "COLEMAK";
  383. default: return "ERROR";
  384. }
  385. }
  386. String _OS::get_model_name() const {
  387. return OS::get_singleton()->get_model_name();
  388. }
  389. bool _OS::is_ok_left_and_cancel_right() const {
  390. return OS::get_singleton()->get_swap_ok_cancel();
  391. }
  392. Error _OS::set_thread_name(const String &p_name) {
  393. return Thread::set_name(p_name);
  394. };
  395. void _OS::set_use_vsync(bool p_enable) {
  396. OS::get_singleton()->set_use_vsync(p_enable);
  397. }
  398. bool _OS::is_vsync_enabled() const {
  399. return OS::get_singleton()->is_vsync_enabled();
  400. }
  401. _OS::PowerState _OS::get_power_state() {
  402. return _OS::PowerState(OS::get_singleton()->get_power_state());
  403. }
  404. int _OS::get_power_seconds_left() {
  405. return OS::get_singleton()->get_power_seconds_left();
  406. }
  407. int _OS::get_power_percent_left() {
  408. return OS::get_singleton()->get_power_percent_left();
  409. }
  410. bool _OS::has_feature(const String &p_feature) const {
  411. return OS::get_singleton()->has_feature(p_feature);
  412. }
  413. /*
  414. enum Weekday {
  415. DAY_SUNDAY,
  416. DAY_MONDAY,
  417. DAY_TUESDAY,
  418. DAY_WEDNESDAY,
  419. DAY_THURSDAY,
  420. DAY_FRIDAY,
  421. DAY_SATURDAY
  422. };
  423. enum Month {
  424. MONTH_JANUARY,
  425. MONTH_FEBRUARY,
  426. MONTH_MARCH,
  427. MONTH_APRIL,
  428. MONTH_MAY,
  429. MONTH_JUNE,
  430. MONTH_JULY,
  431. MONTH_AUGUST,
  432. MONTH_SEPTEMBER,
  433. MONTH_OCTOBER,
  434. MONTH_NOVEMBER,
  435. MONTH_DECEMBER
  436. };
  437. */
  438. /*
  439. struct Date {
  440. int year;
  441. Month month;
  442. int day;
  443. Weekday weekday;
  444. bool dst;
  445. };
  446. struct Time {
  447. int hour;
  448. int min;
  449. int sec;
  450. };
  451. */
  452. uint64_t _OS::get_static_memory_usage() const {
  453. return OS::get_singleton()->get_static_memory_usage();
  454. }
  455. uint64_t _OS::get_static_memory_peak_usage() const {
  456. return OS::get_singleton()->get_static_memory_peak_usage();
  457. }
  458. uint64_t _OS::get_dynamic_memory_usage() const {
  459. return OS::get_singleton()->get_dynamic_memory_usage();
  460. }
  461. void _OS::set_icon(const Ref<Image> &p_icon) {
  462. OS::get_singleton()->set_icon(p_icon);
  463. }
  464. int _OS::get_exit_code() const {
  465. return OS::get_singleton()->get_exit_code();
  466. }
  467. void _OS::set_exit_code(int p_code) {
  468. OS::get_singleton()->set_exit_code(p_code);
  469. }
  470. /**
  471. * Get current datetime with consideration for utc and
  472. * dst
  473. */
  474. Dictionary _OS::get_datetime(bool utc) const {
  475. Dictionary dated = get_date(utc);
  476. Dictionary timed = get_time(utc);
  477. List<Variant> keys;
  478. timed.get_key_list(&keys);
  479. for (int i = 0; i < keys.size(); i++) {
  480. dated[keys[i]] = timed[keys[i]];
  481. }
  482. return dated;
  483. }
  484. Dictionary _OS::get_date(bool utc) const {
  485. OS::Date date = OS::get_singleton()->get_date(utc);
  486. Dictionary dated;
  487. dated[YEAR_KEY] = date.year;
  488. dated[MONTH_KEY] = date.month;
  489. dated[DAY_KEY] = date.day;
  490. dated[WEEKDAY_KEY] = date.weekday;
  491. dated[DST_KEY] = date.dst;
  492. return dated;
  493. }
  494. Dictionary _OS::get_time(bool utc) const {
  495. OS::Time time = OS::get_singleton()->get_time(utc);
  496. Dictionary timed;
  497. timed[HOUR_KEY] = time.hour;
  498. timed[MINUTE_KEY] = time.min;
  499. timed[SECOND_KEY] = time.sec;
  500. return timed;
  501. }
  502. /**
  503. * Get an epoch time value from a dictionary of time values
  504. * @p datetime must be populated with the following keys:
  505. * day, hour, minute, month, second, year. (dst is ignored).
  506. *
  507. * You can pass the output from
  508. * get_datetime_from_unix_time directly into this function
  509. *
  510. * @param datetime dictionary of date and time values to convert
  511. *
  512. * @return epoch calculated
  513. */
  514. int64_t _OS::get_unix_time_from_datetime(Dictionary datetime) const {
  515. // Bunch of conversion constants
  516. static const unsigned int SECONDS_PER_MINUTE = 60;
  517. static const unsigned int MINUTES_PER_HOUR = 60;
  518. static const unsigned int HOURS_PER_DAY = 24;
  519. static const unsigned int SECONDS_PER_HOUR = MINUTES_PER_HOUR * SECONDS_PER_MINUTE;
  520. static const unsigned int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
  521. // Get all time values from the dictionary, set to zero if it doesn't exist.
  522. // Risk incorrect calculation over throwing errors
  523. unsigned int second = ((datetime.has(SECOND_KEY)) ? static_cast<unsigned int>(datetime[SECOND_KEY]) : 0);
  524. unsigned int minute = ((datetime.has(MINUTE_KEY)) ? static_cast<unsigned int>(datetime[MINUTE_KEY]) : 0);
  525. unsigned int hour = ((datetime.has(HOUR_KEY)) ? static_cast<unsigned int>(datetime[HOUR_KEY]) : 0);
  526. unsigned int day = ((datetime.has(DAY_KEY)) ? static_cast<unsigned int>(datetime[DAY_KEY]) : 1);
  527. unsigned int month = ((datetime.has(MONTH_KEY)) ? static_cast<unsigned int>(datetime[MONTH_KEY]) : 1);
  528. unsigned int year = ((datetime.has(YEAR_KEY)) ? static_cast<unsigned int>(datetime[YEAR_KEY]) : 0);
  529. /// How many days come before each month (0-12)
  530. static const unsigned short int DAYS_PAST_THIS_YEAR_TABLE[2][13] = {
  531. /* Normal years. */
  532. { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  533. /* Leap years. */
  534. { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  535. };
  536. ERR_EXPLAIN("Invalid second value of: " + itos(second));
  537. ERR_FAIL_COND_V(second > 59, 0);
  538. ERR_EXPLAIN("Invalid minute value of: " + itos(minute));
  539. ERR_FAIL_COND_V(minute > 59, 0);
  540. ERR_EXPLAIN("Invalid hour value of: " + itos(hour));
  541. ERR_FAIL_COND_V(hour > 23, 0);
  542. ERR_EXPLAIN("Invalid month value of: " + itos(month));
  543. ERR_FAIL_COND_V(month > 12 || month == 0, 0);
  544. // Do this check after month is tested as valid
  545. ERR_EXPLAIN("Invalid day value of: " + itos(day) + " which is larger than " + itos(MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1]) + " or 0");
  546. ERR_FAIL_COND_V(day > MONTH_DAYS_TABLE[LEAPYEAR(year)][month - 1] || day == 0, 0);
  547. // Calculate all the seconds from months past in this year
  548. uint64_t SECONDS_FROM_MONTHS_PAST_THIS_YEAR = DAYS_PAST_THIS_YEAR_TABLE[LEAPYEAR(year)][month - 1] * SECONDS_PER_DAY;
  549. int64_t SECONDS_FROM_YEARS_PAST = 0;
  550. if (year >= EPOCH_YR) {
  551. for (unsigned int iyear = EPOCH_YR; iyear < year; iyear++) {
  552. SECONDS_FROM_YEARS_PAST += YEARSIZE(iyear) * SECONDS_PER_DAY;
  553. }
  554. } else {
  555. for (unsigned int iyear = EPOCH_YR - 1; iyear >= year; iyear--) {
  556. SECONDS_FROM_YEARS_PAST -= YEARSIZE(iyear) * SECONDS_PER_DAY;
  557. }
  558. }
  559. int64_t epoch =
  560. second +
  561. minute * SECONDS_PER_MINUTE +
  562. hour * SECONDS_PER_HOUR +
  563. // Subtract 1 from day, since the current day isn't over yet
  564. // and we cannot count all 24 hours.
  565. (day - 1) * SECONDS_PER_DAY +
  566. SECONDS_FROM_MONTHS_PAST_THIS_YEAR +
  567. SECONDS_FROM_YEARS_PAST;
  568. return epoch;
  569. }
  570. /**
  571. * Get a dictionary of time values when given epoch time
  572. *
  573. * Dictionary Time values will be a union if values from #get_time
  574. * and #get_date dictionaries (with the exception of dst =
  575. * day light standard time, as it cannot be determined from epoch)
  576. *
  577. * @param unix_time_val epoch time to convert
  578. *
  579. * @return dictionary of date and time values
  580. */
  581. Dictionary _OS::get_datetime_from_unix_time(int64_t unix_time_val) const {
  582. OS::Date date;
  583. OS::Time time;
  584. long dayclock, dayno;
  585. int year = EPOCH_YR;
  586. if (unix_time_val >= 0) {
  587. dayno = unix_time_val / SECS_DAY;
  588. dayclock = unix_time_val % SECS_DAY;
  589. /* day 0 was a thursday */
  590. date.weekday = static_cast<OS::Weekday>((dayno + 4) % 7);
  591. while (dayno >= YEARSIZE(year)) {
  592. dayno -= YEARSIZE(year);
  593. year++;
  594. }
  595. } else {
  596. dayno = (unix_time_val - SECS_DAY + 1) / SECS_DAY;
  597. dayclock = unix_time_val - dayno * SECS_DAY;
  598. date.weekday = static_cast<OS::Weekday>((dayno - 3) % 7 + 7);
  599. do {
  600. year--;
  601. dayno += YEARSIZE(year);
  602. } while (dayno < 0);
  603. }
  604. time.sec = dayclock % 60;
  605. time.min = (dayclock % 3600) / 60;
  606. time.hour = dayclock / 3600;
  607. date.year = year;
  608. size_t imonth = 0;
  609. while ((unsigned long)dayno >= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth]) {
  610. dayno -= MONTH_DAYS_TABLE[LEAPYEAR(year)][imonth];
  611. imonth++;
  612. }
  613. /// Add 1 to month to make sure months are indexed starting at 1
  614. date.month = static_cast<OS::Month>(imonth + 1);
  615. date.day = dayno + 1;
  616. Dictionary timed;
  617. timed[HOUR_KEY] = time.hour;
  618. timed[MINUTE_KEY] = time.min;
  619. timed[SECOND_KEY] = time.sec;
  620. timed[YEAR_KEY] = date.year;
  621. timed[MONTH_KEY] = date.month;
  622. timed[DAY_KEY] = date.day;
  623. timed[WEEKDAY_KEY] = date.weekday;
  624. return timed;
  625. }
  626. Dictionary _OS::get_time_zone_info() const {
  627. OS::TimeZoneInfo info = OS::get_singleton()->get_time_zone_info();
  628. Dictionary infod;
  629. infod["bias"] = info.bias;
  630. infod["name"] = info.name;
  631. return infod;
  632. }
  633. uint64_t _OS::get_unix_time() const {
  634. return OS::get_singleton()->get_unix_time();
  635. }
  636. uint64_t _OS::get_system_time_secs() const {
  637. return OS::get_singleton()->get_system_time_secs();
  638. }
  639. uint64_t _OS::get_system_time_msecs() const {
  640. return OS::get_singleton()->get_system_time_msecs();
  641. }
  642. void _OS::delay_usec(uint32_t p_usec) const {
  643. OS::get_singleton()->delay_usec(p_usec);
  644. }
  645. void _OS::delay_msec(uint32_t p_msec) const {
  646. OS::get_singleton()->delay_usec(int64_t(p_msec) * 1000);
  647. }
  648. uint32_t _OS::get_ticks_msec() const {
  649. return OS::get_singleton()->get_ticks_msec();
  650. }
  651. uint64_t _OS::get_ticks_usec() const {
  652. return OS::get_singleton()->get_ticks_usec();
  653. }
  654. uint32_t _OS::get_splash_tick_msec() const {
  655. return OS::get_singleton()->get_splash_tick_msec();
  656. }
  657. bool _OS::can_use_threads() const {
  658. return OS::get_singleton()->can_use_threads();
  659. }
  660. bool _OS::can_draw() const {
  661. return OS::get_singleton()->can_draw();
  662. }
  663. bool _OS::is_userfs_persistent() const {
  664. return OS::get_singleton()->is_userfs_persistent();
  665. }
  666. int _OS::get_processor_count() const {
  667. return OS::get_singleton()->get_processor_count();
  668. }
  669. bool _OS::is_stdout_verbose() const {
  670. return OS::get_singleton()->is_stdout_verbose();
  671. }
  672. void _OS::dump_memory_to_file(const String &p_file) {
  673. OS::get_singleton()->dump_memory_to_file(p_file.utf8().get_data());
  674. }
  675. struct _OSCoreBindImg {
  676. String path;
  677. Size2 size;
  678. int fmt;
  679. ObjectID id;
  680. int vram;
  681. bool operator<(const _OSCoreBindImg &p_img) const { return vram == p_img.vram ? id < p_img.id : vram > p_img.vram; }
  682. };
  683. void _OS::print_all_textures_by_size() {
  684. List<_OSCoreBindImg> imgs;
  685. int total = 0;
  686. {
  687. List<Ref<Resource> > rsrc;
  688. ResourceCache::get_cached_resources(&rsrc);
  689. for (List<Ref<Resource> >::Element *E = rsrc.front(); E; E = E->next()) {
  690. if (!E->get()->is_class("ImageTexture"))
  691. continue;
  692. Size2 size = E->get()->call("get_size");
  693. int fmt = E->get()->call("get_format");
  694. _OSCoreBindImg img;
  695. img.size = size;
  696. img.fmt = fmt;
  697. img.path = E->get()->get_path();
  698. img.vram = Image::get_image_data_size(img.size.width, img.size.height, Image::Format(img.fmt));
  699. img.id = E->get()->get_instance_id();
  700. total += img.vram;
  701. imgs.push_back(img);
  702. }
  703. }
  704. imgs.sort();
  705. for (List<_OSCoreBindImg>::Element *E = imgs.front(); E; E = E->next()) {
  706. total -= E->get().vram;
  707. }
  708. }
  709. void _OS::print_resources_by_type(const Vector<String> &p_types) {
  710. Map<String, int> type_count;
  711. List<Ref<Resource> > resources;
  712. ResourceCache::get_cached_resources(&resources);
  713. List<Ref<Resource> > rsrc;
  714. ResourceCache::get_cached_resources(&rsrc);
  715. for (List<Ref<Resource> >::Element *E = rsrc.front(); E; E = E->next()) {
  716. Ref<Resource> r = E->get();
  717. bool found = false;
  718. for (int i = 0; i < p_types.size(); i++) {
  719. if (r->is_class(p_types[i]))
  720. found = true;
  721. }
  722. if (!found)
  723. continue;
  724. if (!type_count.has(r->get_class())) {
  725. type_count[r->get_class()] = 0;
  726. }
  727. type_count[r->get_class()]++;
  728. }
  729. };
  730. bool _OS::has_virtual_keyboard() const {
  731. return OS::get_singleton()->has_virtual_keyboard();
  732. }
  733. void _OS::show_virtual_keyboard(const String &p_existing_text) {
  734. OS::get_singleton()->show_virtual_keyboard(p_existing_text, Rect2());
  735. }
  736. void _OS::hide_virtual_keyboard() {
  737. OS::get_singleton()->hide_virtual_keyboard();
  738. }
  739. int _OS::get_virtual_keyboard_height() {
  740. return OS::get_singleton()->get_virtual_keyboard_height();
  741. }
  742. void _OS::print_all_resources(const String &p_to_file) {
  743. OS::get_singleton()->print_all_resources(p_to_file);
  744. }
  745. void _OS::print_resources_in_use(bool p_short) {
  746. OS::get_singleton()->print_resources_in_use(p_short);
  747. }
  748. void _OS::dump_resources_to_file(const String &p_file) {
  749. OS::get_singleton()->dump_resources_to_file(p_file.utf8().get_data());
  750. }
  751. String _OS::get_user_data_dir() const {
  752. return OS::get_singleton()->get_user_data_dir();
  753. };
  754. Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  755. return OS::get_singleton()->native_video_play(p_path, p_volume, p_audio_track, p_subtitle_track);
  756. };
  757. bool _OS::native_video_is_playing() {
  758. return OS::get_singleton()->native_video_is_playing();
  759. };
  760. void _OS::native_video_pause() {
  761. OS::get_singleton()->native_video_pause();
  762. };
  763. void _OS::native_video_unpause() {
  764. OS::get_singleton()->native_video_unpause();
  765. };
  766. void _OS::native_video_stop() {
  767. OS::get_singleton()->native_video_stop();
  768. };
  769. void _OS::request_attention() {
  770. OS::get_singleton()->request_attention();
  771. }
  772. void _OS::center_window() {
  773. OS::get_singleton()->center_window();
  774. }
  775. void _OS::move_window_to_foreground() {
  776. OS::get_singleton()->move_window_to_foreground();
  777. }
  778. bool _OS::is_debug_build() const {
  779. #ifdef DEBUG_ENABLED
  780. return true;
  781. #else
  782. return false;
  783. #endif
  784. }
  785. void _OS::set_screen_orientation(ScreenOrientation p_orientation) {
  786. OS::get_singleton()->set_screen_orientation(OS::ScreenOrientation(p_orientation));
  787. }
  788. _OS::ScreenOrientation _OS::get_screen_orientation() const {
  789. return ScreenOrientation(OS::get_singleton()->get_screen_orientation());
  790. }
  791. void _OS::set_keep_screen_on(bool p_enabled) {
  792. OS::get_singleton()->set_keep_screen_on(p_enabled);
  793. }
  794. bool _OS::is_keep_screen_on() const {
  795. return OS::get_singleton()->is_keep_screen_on();
  796. }
  797. String _OS::get_system_dir(SystemDir p_dir) const {
  798. return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
  799. }
  800. String _OS::get_scancode_string(uint32_t p_code) const {
  801. return keycode_get_string(p_code);
  802. }
  803. bool _OS::is_scancode_unicode(uint32_t p_unicode) const {
  804. return keycode_has_unicode(p_unicode);
  805. }
  806. int _OS::find_scancode_from_string(const String &p_code) const {
  807. return find_keycode(p_code);
  808. }
  809. void _OS::alert(const String &p_alert, const String &p_title) {
  810. OS::get_singleton()->alert(p_alert, p_title);
  811. }
  812. bool _OS::request_permission(const String &p_name) {
  813. return OS::get_singleton()->request_permission(p_name);
  814. }
  815. _OS *_OS::singleton = NULL;
  816. void _OS::_bind_methods() {
  817. //ClassDB::bind_method(D_METHOD("get_mouse_position"),&_OS::get_mouse_position);
  818. //ClassDB::bind_method(D_METHOD("is_mouse_grab_enabled"),&_OS::is_mouse_grab_enabled);
  819. ClassDB::bind_method(D_METHOD("set_clipboard", "clipboard"), &_OS::set_clipboard);
  820. ClassDB::bind_method(D_METHOD("get_clipboard"), &_OS::get_clipboard);
  821. //will not delete for now, just unexpose
  822. //ClassDB::bind_method(D_METHOD("set_video_mode","size","fullscreen","resizable","screen"),&_OS::set_video_mode,DEFVAL(0));
  823. //ClassDB::bind_method(D_METHOD("get_video_mode_size","screen"),&_OS::get_video_mode,DEFVAL(0));
  824. //ClassDB::bind_method(D_METHOD("is_video_mode_fullscreen","screen"),&_OS::is_video_mode_fullscreen,DEFVAL(0));
  825. //ClassDB::bind_method(D_METHOD("is_video_mode_resizable","screen"),&_OS::is_video_mode_resizable,DEFVAL(0));
  826. //ClassDB::bind_method(D_METHOD("get_fullscreen_mode_list","screen"),&_OS::get_fullscreen_mode_list,DEFVAL(0));
  827. ClassDB::bind_method(D_METHOD("get_video_driver_count"), &_OS::get_video_driver_count);
  828. ClassDB::bind_method(D_METHOD("get_video_driver_name", "driver"), &_OS::get_video_driver_name);
  829. ClassDB::bind_method(D_METHOD("get_current_video_driver"), &_OS::get_current_video_driver);
  830. ClassDB::bind_method(D_METHOD("get_audio_driver_count"), &_OS::get_audio_driver_count);
  831. ClassDB::bind_method(D_METHOD("get_audio_driver_name", "driver"), &_OS::get_audio_driver_name);
  832. ClassDB::bind_method(D_METHOD("get_connected_midi_inputs"), &_OS::get_connected_midi_inputs);
  833. ClassDB::bind_method(D_METHOD("open_midi_inputs"), &_OS::open_midi_inputs);
  834. ClassDB::bind_method(D_METHOD("close_midi_inputs"), &_OS::close_midi_inputs);
  835. ClassDB::bind_method(D_METHOD("get_screen_count"), &_OS::get_screen_count);
  836. ClassDB::bind_method(D_METHOD("get_current_screen"), &_OS::get_current_screen);
  837. ClassDB::bind_method(D_METHOD("set_current_screen", "screen"), &_OS::set_current_screen);
  838. ClassDB::bind_method(D_METHOD("get_screen_position", "screen"), &_OS::get_screen_position, DEFVAL(-1));
  839. ClassDB::bind_method(D_METHOD("get_screen_size", "screen"), &_OS::get_screen_size, DEFVAL(-1));
  840. ClassDB::bind_method(D_METHOD("get_screen_dpi", "screen"), &_OS::get_screen_dpi, DEFVAL(-1));
  841. ClassDB::bind_method(D_METHOD("get_window_position"), &_OS::get_window_position);
  842. ClassDB::bind_method(D_METHOD("set_window_position", "position"), &_OS::set_window_position);
  843. ClassDB::bind_method(D_METHOD("get_window_size"), &_OS::get_window_size);
  844. ClassDB::bind_method(D_METHOD("set_window_size", "size"), &_OS::set_window_size);
  845. ClassDB::bind_method(D_METHOD("get_window_safe_area"), &_OS::get_window_safe_area);
  846. ClassDB::bind_method(D_METHOD("set_window_fullscreen", "enabled"), &_OS::set_window_fullscreen);
  847. ClassDB::bind_method(D_METHOD("is_window_fullscreen"), &_OS::is_window_fullscreen);
  848. ClassDB::bind_method(D_METHOD("set_window_resizable", "enabled"), &_OS::set_window_resizable);
  849. ClassDB::bind_method(D_METHOD("is_window_resizable"), &_OS::is_window_resizable);
  850. ClassDB::bind_method(D_METHOD("set_window_minimized", "enabled"), &_OS::set_window_minimized);
  851. ClassDB::bind_method(D_METHOD("is_window_minimized"), &_OS::is_window_minimized);
  852. ClassDB::bind_method(D_METHOD("set_window_maximized", "enabled"), &_OS::set_window_maximized);
  853. ClassDB::bind_method(D_METHOD("is_window_maximized"), &_OS::is_window_maximized);
  854. ClassDB::bind_method(D_METHOD("set_window_always_on_top", "enabled"), &_OS::set_window_always_on_top);
  855. ClassDB::bind_method(D_METHOD("is_window_always_on_top"), &_OS::is_window_always_on_top);
  856. ClassDB::bind_method(D_METHOD("request_attention"), &_OS::request_attention);
  857. ClassDB::bind_method(D_METHOD("get_real_window_size"), &_OS::get_real_window_size);
  858. ClassDB::bind_method(D_METHOD("center_window"), &_OS::center_window);
  859. ClassDB::bind_method(D_METHOD("move_window_to_foreground"), &_OS::move_window_to_foreground);
  860. ClassDB::bind_method(D_METHOD("set_borderless_window", "borderless"), &_OS::set_borderless_window);
  861. ClassDB::bind_method(D_METHOD("get_borderless_window"), &_OS::get_borderless_window);
  862. ClassDB::bind_method(D_METHOD("get_window_per_pixel_transparency_enabled"), &_OS::get_window_per_pixel_transparency_enabled);
  863. ClassDB::bind_method(D_METHOD("set_window_per_pixel_transparency_enabled", "enabled"), &_OS::set_window_per_pixel_transparency_enabled);
  864. ClassDB::bind_method(D_METHOD("set_ime_active", "active"), &_OS::set_ime_active);
  865. ClassDB::bind_method(D_METHOD("set_ime_position", "position"), &_OS::set_ime_position);
  866. ClassDB::bind_method(D_METHOD("get_ime_selection"), &_OS::get_ime_selection);
  867. ClassDB::bind_method(D_METHOD("get_ime_text"), &_OS::get_ime_text);
  868. ClassDB::bind_method(D_METHOD("set_screen_orientation", "orientation"), &_OS::set_screen_orientation);
  869. ClassDB::bind_method(D_METHOD("get_screen_orientation"), &_OS::get_screen_orientation);
  870. ClassDB::bind_method(D_METHOD("set_keep_screen_on", "enabled"), &_OS::set_keep_screen_on);
  871. ClassDB::bind_method(D_METHOD("is_keep_screen_on"), &_OS::is_keep_screen_on);
  872. ClassDB::bind_method(D_METHOD("has_touchscreen_ui_hint"), &_OS::has_touchscreen_ui_hint);
  873. ClassDB::bind_method(D_METHOD("set_window_title", "title"), &_OS::set_window_title);
  874. ClassDB::bind_method(D_METHOD("set_low_processor_usage_mode", "enable"), &_OS::set_low_processor_usage_mode);
  875. ClassDB::bind_method(D_METHOD("is_in_low_processor_usage_mode"), &_OS::is_in_low_processor_usage_mode);
  876. ClassDB::bind_method(D_METHOD("get_processor_count"), &_OS::get_processor_count);
  877. ClassDB::bind_method(D_METHOD("get_executable_path"), &_OS::get_executable_path);
  878. ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "blocking", "output"), &_OS::execute, DEFVAL(Array()));
  879. ClassDB::bind_method(D_METHOD("kill", "pid"), &_OS::kill);
  880. ClassDB::bind_method(D_METHOD("shell_open", "uri"), &_OS::shell_open);
  881. ClassDB::bind_method(D_METHOD("get_process_id"), &_OS::get_process_id);
  882. ClassDB::bind_method(D_METHOD("get_environment", "environment"), &_OS::get_environment);
  883. ClassDB::bind_method(D_METHOD("has_environment", "environment"), &_OS::has_environment);
  884. ClassDB::bind_method(D_METHOD("get_name"), &_OS::get_name);
  885. ClassDB::bind_method(D_METHOD("get_cmdline_args"), &_OS::get_cmdline_args);
  886. ClassDB::bind_method(D_METHOD("get_datetime", "utc"), &_OS::get_datetime, DEFVAL(false));
  887. ClassDB::bind_method(D_METHOD("get_date", "utc"), &_OS::get_date, DEFVAL(false));
  888. ClassDB::bind_method(D_METHOD("get_time", "utc"), &_OS::get_time, DEFVAL(false));
  889. ClassDB::bind_method(D_METHOD("get_time_zone_info"), &_OS::get_time_zone_info);
  890. ClassDB::bind_method(D_METHOD("get_unix_time"), &_OS::get_unix_time);
  891. ClassDB::bind_method(D_METHOD("get_datetime_from_unix_time", "unix_time_val"), &_OS::get_datetime_from_unix_time);
  892. ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime", "datetime"), &_OS::get_unix_time_from_datetime);
  893. ClassDB::bind_method(D_METHOD("get_system_time_secs"), &_OS::get_system_time_secs);
  894. ClassDB::bind_method(D_METHOD("get_system_time_msecs"), &_OS::get_system_time_msecs);
  895. ClassDB::bind_method(D_METHOD("set_icon", "icon"), &_OS::set_icon);
  896. ClassDB::bind_method(D_METHOD("get_exit_code"), &_OS::get_exit_code);
  897. ClassDB::bind_method(D_METHOD("set_exit_code", "code"), &_OS::set_exit_code);
  898. ClassDB::bind_method(D_METHOD("delay_usec", "usec"), &_OS::delay_usec);
  899. ClassDB::bind_method(D_METHOD("delay_msec", "msec"), &_OS::delay_msec);
  900. ClassDB::bind_method(D_METHOD("get_ticks_msec"), &_OS::get_ticks_msec);
  901. ClassDB::bind_method(D_METHOD("get_ticks_usec"), &_OS::get_ticks_usec);
  902. ClassDB::bind_method(D_METHOD("get_splash_tick_msec"), &_OS::get_splash_tick_msec);
  903. ClassDB::bind_method(D_METHOD("get_locale"), &_OS::get_locale);
  904. ClassDB::bind_method(D_METHOD("get_latin_keyboard_variant"), &_OS::get_latin_keyboard_variant);
  905. ClassDB::bind_method(D_METHOD("get_model_name"), &_OS::get_model_name);
  906. ClassDB::bind_method(D_METHOD("can_draw"), &_OS::can_draw);
  907. ClassDB::bind_method(D_METHOD("is_userfs_persistent"), &_OS::is_userfs_persistent);
  908. ClassDB::bind_method(D_METHOD("is_stdout_verbose"), &_OS::is_stdout_verbose);
  909. ClassDB::bind_method(D_METHOD("can_use_threads"), &_OS::can_use_threads);
  910. ClassDB::bind_method(D_METHOD("is_debug_build"), &_OS::is_debug_build);
  911. //ClassDB::bind_method(D_METHOD("get_mouse_button_state"),&_OS::get_mouse_button_state);
  912. ClassDB::bind_method(D_METHOD("dump_memory_to_file", "file"), &_OS::dump_memory_to_file);
  913. ClassDB::bind_method(D_METHOD("dump_resources_to_file", "file"), &_OS::dump_resources_to_file);
  914. ClassDB::bind_method(D_METHOD("has_virtual_keyboard"), &_OS::has_virtual_keyboard);
  915. ClassDB::bind_method(D_METHOD("show_virtual_keyboard", "existing_text"), &_OS::show_virtual_keyboard, DEFVAL(""));
  916. ClassDB::bind_method(D_METHOD("hide_virtual_keyboard"), &_OS::hide_virtual_keyboard);
  917. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_height"), &_OS::get_virtual_keyboard_height);
  918. ClassDB::bind_method(D_METHOD("print_resources_in_use", "short"), &_OS::print_resources_in_use, DEFVAL(false));
  919. ClassDB::bind_method(D_METHOD("print_all_resources", "tofile"), &_OS::print_all_resources, DEFVAL(""));
  920. ClassDB::bind_method(D_METHOD("get_static_memory_usage"), &_OS::get_static_memory_usage);
  921. ClassDB::bind_method(D_METHOD("get_static_memory_peak_usage"), &_OS::get_static_memory_peak_usage);
  922. ClassDB::bind_method(D_METHOD("get_dynamic_memory_usage"), &_OS::get_dynamic_memory_usage);
  923. ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir);
  924. ClassDB::bind_method(D_METHOD("get_system_dir", "dir"), &_OS::get_system_dir);
  925. ClassDB::bind_method(D_METHOD("get_unique_id"), &_OS::get_unique_id);
  926. ClassDB::bind_method(D_METHOD("is_ok_left_and_cancel_right"), &_OS::is_ok_left_and_cancel_right);
  927. ClassDB::bind_method(D_METHOD("print_all_textures_by_size"), &_OS::print_all_textures_by_size);
  928. ClassDB::bind_method(D_METHOD("print_resources_by_type", "types"), &_OS::print_resources_by_type);
  929. ClassDB::bind_method(D_METHOD("native_video_play", "path", "volume", "audio_track", "subtitle_track"), &_OS::native_video_play);
  930. ClassDB::bind_method(D_METHOD("native_video_is_playing"), &_OS::native_video_is_playing);
  931. ClassDB::bind_method(D_METHOD("native_video_stop"), &_OS::native_video_stop);
  932. ClassDB::bind_method(D_METHOD("native_video_pause"), &_OS::native_video_pause);
  933. ClassDB::bind_method(D_METHOD("native_video_unpause"), &_OS::native_video_unpause);
  934. ClassDB::bind_method(D_METHOD("get_scancode_string", "code"), &_OS::get_scancode_string);
  935. ClassDB::bind_method(D_METHOD("is_scancode_unicode", "code"), &_OS::is_scancode_unicode);
  936. ClassDB::bind_method(D_METHOD("find_scancode_from_string", "string"), &_OS::find_scancode_from_string);
  937. ClassDB::bind_method(D_METHOD("set_use_file_access_save_and_swap", "enabled"), &_OS::set_use_file_access_save_and_swap);
  938. ClassDB::bind_method(D_METHOD("alert", "text", "title"), &_OS::alert, DEFVAL("Alert!"));
  939. ClassDB::bind_method(D_METHOD("set_thread_name", "name"), &_OS::set_thread_name);
  940. ClassDB::bind_method(D_METHOD("set_use_vsync", "enable"), &_OS::set_use_vsync);
  941. ClassDB::bind_method(D_METHOD("is_vsync_enabled"), &_OS::is_vsync_enabled);
  942. ClassDB::bind_method(D_METHOD("has_feature", "tag_name"), &_OS::has_feature);
  943. ClassDB::bind_method(D_METHOD("get_power_state"), &_OS::get_power_state);
  944. ClassDB::bind_method(D_METHOD("get_power_seconds_left"), &_OS::get_power_seconds_left);
  945. ClassDB::bind_method(D_METHOD("get_power_percent_left"), &_OS::get_power_percent_left);
  946. ClassDB::bind_method(D_METHOD("request_permission", "name"), &_OS::request_permission);
  947. ADD_PROPERTY(PropertyInfo(Variant::STRING, "clipboard"), "set_clipboard", "get_clipboard");
  948. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_screen"), "set_current_screen", "get_current_screen");
  949. ADD_PROPERTY(PropertyInfo(Variant::INT, "exit_code"), "set_exit_code", "get_exit_code");
  950. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vsync_enabled"), "set_use_vsync", "is_vsync_enabled");
  951. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "low_processor_usage_mode"), "set_low_processor_usage_mode", "is_in_low_processor_usage_mode");
  952. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_screen_on"), "set_keep_screen_on", "is_keep_screen_on");
  953. ADD_PROPERTY(PropertyInfo(Variant::INT, "screen_orientation", PROPERTY_HINT_ENUM, "Landscape,Portrait,Reverse Landscape,Reverse Portrait,Sensor Landscape,Sensor Portrait,Sensor"), "set_screen_orientation", "get_screen_orientation");
  954. ADD_GROUP("Window", "window_");
  955. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_borderless"), "set_borderless_window", "get_borderless_window");
  956. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_per_pixel_transparency_enabled"), "set_window_per_pixel_transparency_enabled", "get_window_per_pixel_transparency_enabled");
  957. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_fullscreen"), "set_window_fullscreen", "is_window_fullscreen");
  958. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_maximized"), "set_window_maximized", "is_window_maximized");
  959. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_minimized"), "set_window_minimized", "is_window_minimized");
  960. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "window_resizable"), "set_window_resizable", "is_window_resizable");
  961. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "window_position"), "set_window_position", "get_window_position");
  962. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "window_size"), "set_window_size", "get_window_size");
  963. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES2);
  964. BIND_ENUM_CONSTANT(VIDEO_DRIVER_GLES3);
  965. BIND_ENUM_CONSTANT(DAY_SUNDAY);
  966. BIND_ENUM_CONSTANT(DAY_MONDAY);
  967. BIND_ENUM_CONSTANT(DAY_TUESDAY);
  968. BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
  969. BIND_ENUM_CONSTANT(DAY_THURSDAY);
  970. BIND_ENUM_CONSTANT(DAY_FRIDAY);
  971. BIND_ENUM_CONSTANT(DAY_SATURDAY);
  972. BIND_ENUM_CONSTANT(MONTH_JANUARY);
  973. BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
  974. BIND_ENUM_CONSTANT(MONTH_MARCH);
  975. BIND_ENUM_CONSTANT(MONTH_APRIL);
  976. BIND_ENUM_CONSTANT(MONTH_MAY);
  977. BIND_ENUM_CONSTANT(MONTH_JUNE);
  978. BIND_ENUM_CONSTANT(MONTH_JULY);
  979. BIND_ENUM_CONSTANT(MONTH_AUGUST);
  980. BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
  981. BIND_ENUM_CONSTANT(MONTH_OCTOBER);
  982. BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
  983. BIND_ENUM_CONSTANT(MONTH_DECEMBER);
  984. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_LANDSCAPE);
  985. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_PORTRAIT);
  986. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
  987. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
  988. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  989. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR_PORTRAIT);
  990. BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR);
  991. BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
  992. BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
  993. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
  994. BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
  995. BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
  996. BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
  997. BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
  998. BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);
  999. BIND_ENUM_CONSTANT(POWERSTATE_UNKNOWN);
  1000. BIND_ENUM_CONSTANT(POWERSTATE_ON_BATTERY);
  1001. BIND_ENUM_CONSTANT(POWERSTATE_NO_BATTERY);
  1002. BIND_ENUM_CONSTANT(POWERSTATE_CHARGING);
  1003. BIND_ENUM_CONSTANT(POWERSTATE_CHARGED);
  1004. }
  1005. _OS::_OS() {
  1006. singleton = this;
  1007. }
  1008. ///////////////////// GEOMETRY
  1009. _Geometry *_Geometry::singleton = NULL;
  1010. _Geometry *_Geometry::get_singleton() {
  1011. return singleton;
  1012. }
  1013. PoolVector<Plane> _Geometry::build_box_planes(const Vector3 &p_extents) {
  1014. return Geometry::build_box_planes(p_extents);
  1015. }
  1016. PoolVector<Plane> _Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) {
  1017. return Geometry::build_cylinder_planes(p_radius, p_height, p_sides, p_axis);
  1018. }
  1019. PoolVector<Plane> _Geometry::build_capsule_planes(float p_radius, float p_height, int p_sides, int p_lats, Vector3::Axis p_axis) {
  1020. return Geometry::build_capsule_planes(p_radius, p_height, p_sides, p_lats, p_axis);
  1021. }
  1022. real_t _Geometry::segment_intersects_circle(const Vector2 &p_from, const Vector2 &p_to, const Vector2 &p_circle_pos, real_t p_circle_radius) {
  1023. return Geometry::segment_intersects_circle(p_from, p_to, p_circle_pos, p_circle_radius);
  1024. }
  1025. Variant _Geometry::segment_intersects_segment_2d(const Vector2 &p_from_a, const Vector2 &p_to_a, const Vector2 &p_from_b, const Vector2 &p_to_b) {
  1026. Vector2 result;
  1027. if (Geometry::segment_intersects_segment_2d(p_from_a, p_to_a, p_from_b, p_to_b, &result)) {
  1028. return result;
  1029. } else {
  1030. return Variant();
  1031. };
  1032. };
  1033. Variant _Geometry::line_intersects_line_2d(const Vector2 &p_from_a, const Vector2 &p_dir_a, const Vector2 &p_from_b, const Vector2 &p_dir_b) {
  1034. Vector2 result;
  1035. if (Geometry::line_intersects_line_2d(p_from_a, p_dir_a, p_from_b, p_dir_b, result)) {
  1036. return result;
  1037. } else {
  1038. return Variant();
  1039. }
  1040. }
  1041. PoolVector<Vector2> _Geometry::get_closest_points_between_segments_2d(const Vector2 &p1, const Vector2 &q1, const Vector2 &p2, const Vector2 &q2) {
  1042. Vector2 r1, r2;
  1043. Geometry::get_closest_points_between_segments(p1, q1, p2, q2, r1, r2);
  1044. PoolVector<Vector2> r;
  1045. r.resize(2);
  1046. r.set(0, r1);
  1047. r.set(1, r2);
  1048. return r;
  1049. }
  1050. PoolVector<Vector3> _Geometry::get_closest_points_between_segments(const Vector3 &p1, const Vector3 &p2, const Vector3 &q1, const Vector3 &q2) {
  1051. Vector3 r1, r2;
  1052. Geometry::get_closest_points_between_segments(p1, p2, q1, q2, r1, r2);
  1053. PoolVector<Vector3> r;
  1054. r.resize(2);
  1055. r.set(0, r1);
  1056. r.set(1, r2);
  1057. return r;
  1058. }
  1059. Vector2 _Geometry::get_closest_point_to_segment_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  1060. Vector2 s[2] = { p_a, p_b };
  1061. return Geometry::get_closest_point_to_segment_2d(p_point, s);
  1062. }
  1063. Vector3 _Geometry::get_closest_point_to_segment(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  1064. Vector3 s[2] = { p_a, p_b };
  1065. return Geometry::get_closest_point_to_segment(p_point, s);
  1066. }
  1067. Vector2 _Geometry::get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 &p_a, const Vector2 &p_b) {
  1068. Vector2 s[2] = { p_a, p_b };
  1069. return Geometry::get_closest_point_to_segment_uncapped_2d(p_point, s);
  1070. }
  1071. Vector3 _Geometry::get_closest_point_to_segment_uncapped(const Vector3 &p_point, const Vector3 &p_a, const Vector3 &p_b) {
  1072. Vector3 s[2] = { p_a, p_b };
  1073. return Geometry::get_closest_point_to_segment_uncapped(p_point, s);
  1074. }
  1075. Variant _Geometry::ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
  1076. Vector3 res;
  1077. if (Geometry::ray_intersects_triangle(p_from, p_dir, p_v0, p_v1, p_v2, &res))
  1078. return res;
  1079. else
  1080. return Variant();
  1081. }
  1082. Variant _Geometry::segment_intersects_triangle(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_v0, const Vector3 &p_v1, const Vector3 &p_v2) {
  1083. Vector3 res;
  1084. if (Geometry::segment_intersects_triangle(p_from, p_to, p_v0, p_v1, p_v2, &res))
  1085. return res;
  1086. else
  1087. return Variant();
  1088. }
  1089. bool _Geometry::point_is_inside_triangle(const Vector2 &s, const Vector2 &a, const Vector2 &b, const Vector2 &c) const {
  1090. return Geometry::is_point_in_triangle(s, a, b, c);
  1091. }
  1092. PoolVector<Vector3> _Geometry::segment_intersects_sphere(const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_sphere_pos, real_t p_sphere_radius) {
  1093. PoolVector<Vector3> r;
  1094. Vector3 res, norm;
  1095. if (!Geometry::segment_intersects_sphere(p_from, p_to, p_sphere_pos, p_sphere_radius, &res, &norm))
  1096. return r;
  1097. r.resize(2);
  1098. r.set(0, res);
  1099. r.set(1, norm);
  1100. return r;
  1101. }
  1102. PoolVector<Vector3> _Geometry::segment_intersects_cylinder(const Vector3 &p_from, const Vector3 &p_to, float p_height, float p_radius) {
  1103. PoolVector<Vector3> r;
  1104. Vector3 res, norm;
  1105. if (!Geometry::segment_intersects_cylinder(p_from, p_to, p_height, p_radius, &res, &norm))
  1106. return r;
  1107. r.resize(2);
  1108. r.set(0, res);
  1109. r.set(1, norm);
  1110. return r;
  1111. }
  1112. PoolVector<Vector3> _Geometry::segment_intersects_convex(const Vector3 &p_from, const Vector3 &p_to, const Vector<Plane> &p_planes) {
  1113. PoolVector<Vector3> r;
  1114. Vector3 res, norm;
  1115. if (!Geometry::segment_intersects_convex(p_from, p_to, p_planes.ptr(), p_planes.size(), &res, &norm))
  1116. return r;
  1117. r.resize(2);
  1118. r.set(0, res);
  1119. r.set(1, norm);
  1120. return r;
  1121. }
  1122. bool _Geometry::is_polygon_clockwise(const Vector<Vector2> &p_polygon) {
  1123. return Geometry::is_polygon_clockwise(p_polygon);
  1124. }
  1125. Vector<int> _Geometry::triangulate_polygon(const Vector<Vector2> &p_polygon) {
  1126. return Geometry::triangulate_polygon(p_polygon);
  1127. }
  1128. Vector<int> _Geometry::triangulate_delaunay_2d(const Vector<Vector2> &p_points) {
  1129. return Geometry::triangulate_delaunay_2d(p_points);
  1130. }
  1131. Vector<Point2> _Geometry::convex_hull_2d(const Vector<Point2> &p_points) {
  1132. return Geometry::convex_hull_2d(p_points);
  1133. }
  1134. Vector<Vector3> _Geometry::clip_polygon(const Vector<Vector3> &p_points, const Plane &p_plane) {
  1135. return Geometry::clip_polygon(p_points, p_plane);
  1136. }
  1137. Array _Geometry::merge_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1138. Vector<Vector<Point2> > polys = Geometry::merge_polygons_2d(p_polygon_a, p_polygon_b);
  1139. Array ret;
  1140. for (int i = 0; i < polys.size(); ++i) {
  1141. ret.push_back(polys[i]);
  1142. }
  1143. return ret;
  1144. }
  1145. Array _Geometry::clip_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1146. Vector<Vector<Point2> > polys = Geometry::clip_polygons_2d(p_polygon_a, p_polygon_b);
  1147. Array ret;
  1148. for (int i = 0; i < polys.size(); ++i) {
  1149. ret.push_back(polys[i]);
  1150. }
  1151. return ret;
  1152. }
  1153. Array _Geometry::intersect_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1154. Vector<Vector<Point2> > polys = Geometry::intersect_polygons_2d(p_polygon_a, p_polygon_b);
  1155. Array ret;
  1156. for (int i = 0; i < polys.size(); ++i) {
  1157. ret.push_back(polys[i]);
  1158. }
  1159. return ret;
  1160. }
  1161. Array _Geometry::exclude_polygons_2d(const Vector<Vector2> &p_polygon_a, const Vector<Vector2> &p_polygon_b) {
  1162. Vector<Vector<Point2> > polys = Geometry::exclude_polygons_2d(p_polygon_a, p_polygon_b);
  1163. Array ret;
  1164. for (int i = 0; i < polys.size(); ++i) {
  1165. ret.push_back(polys[i]);
  1166. }
  1167. return ret;
  1168. }
  1169. Array _Geometry::clip_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  1170. Vector<Vector<Point2> > polys = Geometry::clip_polyline_with_polygon_2d(p_polyline, p_polygon);
  1171. Array ret;
  1172. for (int i = 0; i < polys.size(); ++i) {
  1173. ret.push_back(polys[i]);
  1174. }
  1175. return ret;
  1176. }
  1177. Array _Geometry::intersect_polyline_with_polygon_2d(const Vector<Vector2> &p_polyline, const Vector<Vector2> &p_polygon) {
  1178. Vector<Vector<Point2> > polys = Geometry::intersect_polyline_with_polygon_2d(p_polyline, p_polygon);
  1179. Array ret;
  1180. for (int i = 0; i < polys.size(); ++i) {
  1181. ret.push_back(polys[i]);
  1182. }
  1183. return ret;
  1184. }
  1185. Array _Geometry::offset_polygon_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type) {
  1186. Vector<Vector<Point2> > polys = Geometry::offset_polygon_2d(p_polygon, p_delta, Geometry::PolyJoinType(p_join_type));
  1187. Array ret;
  1188. for (int i = 0; i < polys.size(); ++i) {
  1189. ret.push_back(polys[i]);
  1190. }
  1191. return ret;
  1192. }
  1193. Array _Geometry::offset_polyline_2d(const Vector<Vector2> &p_polygon, real_t p_delta, PolyJoinType p_join_type, PolyEndType p_end_type) {
  1194. Vector<Vector<Point2> > polys = Geometry::offset_polyline_2d(p_polygon, p_delta, Geometry::PolyJoinType(p_join_type), Geometry::PolyEndType(p_end_type));
  1195. Array ret;
  1196. for (int i = 0; i < polys.size(); ++i) {
  1197. ret.push_back(polys[i]);
  1198. }
  1199. return ret;
  1200. }
  1201. Vector<Point2> _Geometry::transform_points_2d(const Vector<Point2> &p_points, const Transform2D &p_mat) {
  1202. return Geometry::transform_points_2d(p_points, p_mat);
  1203. }
  1204. Dictionary _Geometry::make_atlas(const Vector<Size2> &p_rects) {
  1205. Dictionary ret;
  1206. Vector<Size2i> rects;
  1207. for (int i = 0; i < p_rects.size(); i++) {
  1208. rects.push_back(p_rects[i]);
  1209. };
  1210. Vector<Point2i> result;
  1211. Size2i size;
  1212. Geometry::make_atlas(rects, result, size);
  1213. Size2 r_size = size;
  1214. Vector<Point2> r_result;
  1215. for (int i = 0; i < result.size(); i++) {
  1216. r_result.push_back(result[i]);
  1217. };
  1218. ret["points"] = r_result;
  1219. ret["size"] = r_size;
  1220. return ret;
  1221. };
  1222. int _Geometry::get_uv84_normal_bit(const Vector3 &p_vector) {
  1223. return Geometry::get_uv84_normal_bit(p_vector);
  1224. }
  1225. void _Geometry::_bind_methods() {
  1226. ClassDB::bind_method(D_METHOD("build_box_planes", "extents"), &_Geometry::build_box_planes);
  1227. ClassDB::bind_method(D_METHOD("build_cylinder_planes", "radius", "height", "sides", "axis"), &_Geometry::build_cylinder_planes, DEFVAL(Vector3::AXIS_Z));
  1228. ClassDB::bind_method(D_METHOD("build_capsule_planes", "radius", "height", "sides", "lats", "axis"), &_Geometry::build_capsule_planes, DEFVAL(Vector3::AXIS_Z));
  1229. ClassDB::bind_method(D_METHOD("segment_intersects_circle", "segment_from", "segment_to", "circle_position", "circle_radius"), &_Geometry::segment_intersects_circle);
  1230. ClassDB::bind_method(D_METHOD("segment_intersects_segment_2d", "from_a", "to_a", "from_b", "to_b"), &_Geometry::segment_intersects_segment_2d);
  1231. ClassDB::bind_method(D_METHOD("line_intersects_line_2d", "from_a", "dir_a", "from_b", "dir_b"), &_Geometry::line_intersects_line_2d);
  1232. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments_2d", "p1", "q1", "p2", "q2"), &_Geometry::get_closest_points_between_segments_2d);
  1233. ClassDB::bind_method(D_METHOD("get_closest_points_between_segments", "p1", "p2", "q1", "q2"), &_Geometry::get_closest_points_between_segments);
  1234. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_2d);
  1235. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment);
  1236. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped_2d", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped_2d);
  1237. ClassDB::bind_method(D_METHOD("get_closest_point_to_segment_uncapped", "point", "s1", "s2"), &_Geometry::get_closest_point_to_segment_uncapped);
  1238. ClassDB::bind_method(D_METHOD("get_uv84_normal_bit", "normal"), &_Geometry::get_uv84_normal_bit);
  1239. ClassDB::bind_method(D_METHOD("ray_intersects_triangle", "from", "dir", "a", "b", "c"), &_Geometry::ray_intersects_triangle);
  1240. ClassDB::bind_method(D_METHOD("segment_intersects_triangle", "from", "to", "a", "b", "c"), &_Geometry::segment_intersects_triangle);
  1241. ClassDB::bind_method(D_METHOD("segment_intersects_sphere", "from", "to", "sphere_position", "sphere_radius"), &_Geometry::segment_intersects_sphere);
  1242. ClassDB::bind_method(D_METHOD("segment_intersects_cylinder", "from", "to", "height", "radius"), &_Geometry::segment_intersects_cylinder);
  1243. ClassDB::bind_method(D_METHOD("segment_intersects_convex", "from", "to", "planes"), &_Geometry::segment_intersects_convex);
  1244. ClassDB::bind_method(D_METHOD("point_is_inside_triangle", "point", "a", "b", "c"), &_Geometry::point_is_inside_triangle);
  1245. ClassDB::bind_method(D_METHOD("is_polygon_clockwise", "polygon"), &_Geometry::is_polygon_clockwise);
  1246. ClassDB::bind_method(D_METHOD("triangulate_polygon", "polygon"), &_Geometry::triangulate_polygon);
  1247. ClassDB::bind_method(D_METHOD("triangulate_delaunay_2d", "points"), &_Geometry::triangulate_delaunay_2d);
  1248. ClassDB::bind_method(D_METHOD("convex_hull_2d", "points"), &_Geometry::convex_hull_2d);
  1249. ClassDB::bind_method(D_METHOD("clip_polygon", "points", "plane"), &_Geometry::clip_polygon);
  1250. ClassDB::bind_method(D_METHOD("merge_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::merge_polygons_2d);
  1251. ClassDB::bind_method(D_METHOD("clip_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::clip_polygons_2d);
  1252. ClassDB::bind_method(D_METHOD("intersect_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::intersect_polygons_2d);
  1253. ClassDB::bind_method(D_METHOD("exclude_polygons_2d", "polygon_a", "polygon_b"), &_Geometry::exclude_polygons_2d);
  1254. ClassDB::bind_method(D_METHOD("clip_polyline_with_polygon_2d", "polyline", "polygon"), &_Geometry::clip_polyline_with_polygon_2d);
  1255. ClassDB::bind_method(D_METHOD("intersect_polyline_with_polygon_2d", "polyline", "polygon"), &_Geometry::intersect_polyline_with_polygon_2d);
  1256. ClassDB::bind_method(D_METHOD("offset_polygon_2d", "polygon", "delta", "join_type"), &_Geometry::offset_polygon_2d, DEFVAL(JOIN_SQUARE));
  1257. ClassDB::bind_method(D_METHOD("offset_polyline_2d", "polyline", "delta", "join_type", "end_type"), &_Geometry::offset_polyline_2d, DEFVAL(JOIN_SQUARE), DEFVAL(END_SQUARE));
  1258. ClassDB::bind_method(D_METHOD("transform_points_2d", "points", "transform"), &_Geometry::transform_points_2d);
  1259. ClassDB::bind_method(D_METHOD("make_atlas", "sizes"), &_Geometry::make_atlas);
  1260. BIND_ENUM_CONSTANT(OPERATION_UNION);
  1261. BIND_ENUM_CONSTANT(OPERATION_DIFFERENCE);
  1262. BIND_ENUM_CONSTANT(OPERATION_INTERSECTION);
  1263. BIND_ENUM_CONSTANT(OPERATION_XOR);
  1264. BIND_ENUM_CONSTANT(JOIN_SQUARE);
  1265. BIND_ENUM_CONSTANT(JOIN_ROUND);
  1266. BIND_ENUM_CONSTANT(JOIN_MITER);
  1267. BIND_ENUM_CONSTANT(END_POLYGON);
  1268. BIND_ENUM_CONSTANT(END_JOINED);
  1269. BIND_ENUM_CONSTANT(END_BUTT);
  1270. BIND_ENUM_CONSTANT(END_SQUARE);
  1271. BIND_ENUM_CONSTANT(END_ROUND);
  1272. }
  1273. _Geometry::_Geometry() {
  1274. singleton = this;
  1275. }
  1276. ///////////////////////// FILE
  1277. Error _File::open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key) {
  1278. Error err = open(p_path, p_mode_flags);
  1279. if (err)
  1280. return err;
  1281. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  1282. err = fae->open_and_parse(f, p_key, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  1283. if (err) {
  1284. memdelete(fae);
  1285. close();
  1286. return err;
  1287. }
  1288. f = fae;
  1289. return OK;
  1290. }
  1291. Error _File::open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass) {
  1292. Error err = open(p_path, p_mode_flags);
  1293. if (err)
  1294. return err;
  1295. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  1296. err = fae->open_and_parse_password(f, p_pass, (p_mode_flags == WRITE) ? FileAccessEncrypted::MODE_WRITE_AES256 : FileAccessEncrypted::MODE_READ);
  1297. if (err) {
  1298. memdelete(fae);
  1299. close();
  1300. return err;
  1301. }
  1302. f = fae;
  1303. return OK;
  1304. }
  1305. Error _File::open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode) {
  1306. FileAccessCompressed *fac = memnew(FileAccessCompressed);
  1307. fac->configure("GCPF", (Compression::Mode)p_compress_mode);
  1308. Error err = fac->_open(p_path, p_mode_flags);
  1309. if (err) {
  1310. memdelete(fac);
  1311. return err;
  1312. }
  1313. f = fac;
  1314. return OK;
  1315. }
  1316. Error _File::open(const String &p_path, ModeFlags p_mode_flags) {
  1317. close();
  1318. Error err;
  1319. f = FileAccess::open(p_path, p_mode_flags, &err);
  1320. if (f)
  1321. f->set_endian_swap(eswap);
  1322. return err;
  1323. }
  1324. void _File::close() {
  1325. if (f)
  1326. memdelete(f);
  1327. f = NULL;
  1328. }
  1329. bool _File::is_open() const {
  1330. return f != NULL;
  1331. }
  1332. String _File::get_path() const {
  1333. ERR_FAIL_COND_V(!f, "");
  1334. return f->get_path();
  1335. }
  1336. String _File::get_path_absolute() const {
  1337. ERR_FAIL_COND_V(!f, "");
  1338. return f->get_path_absolute();
  1339. }
  1340. void _File::seek(int64_t p_position) {
  1341. ERR_FAIL_COND(!f);
  1342. f->seek(p_position);
  1343. }
  1344. void _File::seek_end(int64_t p_position) {
  1345. ERR_FAIL_COND(!f);
  1346. f->seek_end(p_position);
  1347. }
  1348. int64_t _File::get_position() const {
  1349. ERR_FAIL_COND_V(!f, 0);
  1350. return f->get_position();
  1351. }
  1352. int64_t _File::get_len() const {
  1353. ERR_FAIL_COND_V(!f, 0);
  1354. return f->get_len();
  1355. }
  1356. bool _File::eof_reached() const {
  1357. ERR_FAIL_COND_V(!f, false);
  1358. return f->eof_reached();
  1359. }
  1360. uint8_t _File::get_8() const {
  1361. ERR_FAIL_COND_V(!f, 0);
  1362. return f->get_8();
  1363. }
  1364. uint16_t _File::get_16() const {
  1365. ERR_FAIL_COND_V(!f, 0);
  1366. return f->get_16();
  1367. }
  1368. uint32_t _File::get_32() const {
  1369. ERR_FAIL_COND_V(!f, 0);
  1370. return f->get_32();
  1371. }
  1372. uint64_t _File::get_64() const {
  1373. ERR_FAIL_COND_V(!f, 0);
  1374. return f->get_64();
  1375. }
  1376. float _File::get_float() const {
  1377. ERR_FAIL_COND_V(!f, 0);
  1378. return f->get_float();
  1379. }
  1380. double _File::get_double() const {
  1381. ERR_FAIL_COND_V(!f, 0);
  1382. return f->get_double();
  1383. }
  1384. real_t _File::get_real() const {
  1385. ERR_FAIL_COND_V(!f, 0);
  1386. return f->get_real();
  1387. }
  1388. PoolVector<uint8_t> _File::get_buffer(int p_length) const {
  1389. PoolVector<uint8_t> data;
  1390. ERR_FAIL_COND_V(!f, data);
  1391. ERR_FAIL_COND_V(p_length < 0, data);
  1392. if (p_length == 0)
  1393. return data;
  1394. Error err = data.resize(p_length);
  1395. ERR_FAIL_COND_V(err != OK, data);
  1396. PoolVector<uint8_t>::Write w = data.write();
  1397. int len = f->get_buffer(&w[0], p_length);
  1398. ERR_FAIL_COND_V(len < 0, PoolVector<uint8_t>());
  1399. w = PoolVector<uint8_t>::Write();
  1400. if (len < p_length)
  1401. data.resize(p_length);
  1402. return data;
  1403. }
  1404. String _File::get_as_text() const {
  1405. ERR_FAIL_COND_V(!f, String());
  1406. String text;
  1407. size_t original_pos = f->get_position();
  1408. f->seek(0);
  1409. String l = get_line();
  1410. while (!eof_reached()) {
  1411. text += l + "\n";
  1412. l = get_line();
  1413. }
  1414. text += l;
  1415. f->seek(original_pos);
  1416. return text;
  1417. }
  1418. String _File::get_md5(const String &p_path) const {
  1419. return FileAccess::get_md5(p_path);
  1420. }
  1421. String _File::get_sha256(const String &p_path) const {
  1422. return FileAccess::get_sha256(p_path);
  1423. }
  1424. String _File::get_line() const {
  1425. ERR_FAIL_COND_V(!f, String());
  1426. return f->get_line();
  1427. }
  1428. Vector<String> _File::get_csv_line(const String &p_delim) const {
  1429. ERR_FAIL_COND_V(!f, Vector<String>());
  1430. return f->get_csv_line(p_delim);
  1431. }
  1432. /**< use this for files WRITTEN in _big_ endian machines (ie, amiga/mac)
  1433. * It's not about the current CPU type but file formats.
  1434. * this flags get reset to false (little endian) on each open
  1435. */
  1436. void _File::set_endian_swap(bool p_swap) {
  1437. eswap = p_swap;
  1438. if (f)
  1439. f->set_endian_swap(p_swap);
  1440. }
  1441. bool _File::get_endian_swap() {
  1442. return eswap;
  1443. }
  1444. Error _File::get_error() const {
  1445. if (!f)
  1446. return ERR_UNCONFIGURED;
  1447. return f->get_error();
  1448. }
  1449. void _File::store_8(uint8_t p_dest) {
  1450. ERR_FAIL_COND(!f);
  1451. f->store_8(p_dest);
  1452. }
  1453. void _File::store_16(uint16_t p_dest) {
  1454. ERR_FAIL_COND(!f);
  1455. f->store_16(p_dest);
  1456. }
  1457. void _File::store_32(uint32_t p_dest) {
  1458. ERR_FAIL_COND(!f);
  1459. f->store_32(p_dest);
  1460. }
  1461. void _File::store_64(uint64_t p_dest) {
  1462. ERR_FAIL_COND(!f);
  1463. f->store_64(p_dest);
  1464. }
  1465. void _File::store_float(float p_dest) {
  1466. ERR_FAIL_COND(!f);
  1467. f->store_float(p_dest);
  1468. }
  1469. void _File::store_double(double p_dest) {
  1470. ERR_FAIL_COND(!f);
  1471. f->store_double(p_dest);
  1472. }
  1473. void _File::store_real(real_t p_real) {
  1474. ERR_FAIL_COND(!f);
  1475. f->store_real(p_real);
  1476. }
  1477. void _File::store_string(const String &p_string) {
  1478. ERR_FAIL_COND(!f);
  1479. f->store_string(p_string);
  1480. }
  1481. void _File::store_pascal_string(const String &p_string) {
  1482. ERR_FAIL_COND(!f);
  1483. f->store_pascal_string(p_string);
  1484. };
  1485. String _File::get_pascal_string() {
  1486. ERR_FAIL_COND_V(!f, "");
  1487. return f->get_pascal_string();
  1488. };
  1489. void _File::store_line(const String &p_string) {
  1490. ERR_FAIL_COND(!f);
  1491. f->store_line(p_string);
  1492. }
  1493. void _File::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
  1494. ERR_FAIL_COND(!f);
  1495. f->store_csv_line(p_values, p_delim);
  1496. }
  1497. void _File::store_buffer(const PoolVector<uint8_t> &p_buffer) {
  1498. ERR_FAIL_COND(!f);
  1499. int len = p_buffer.size();
  1500. if (len == 0)
  1501. return;
  1502. PoolVector<uint8_t>::Read r = p_buffer.read();
  1503. f->store_buffer(&r[0], len);
  1504. }
  1505. bool _File::file_exists(const String &p_name) const {
  1506. return FileAccess::exists(p_name);
  1507. }
  1508. void _File::store_var(const Variant &p_var, bool p_full_objects) {
  1509. ERR_FAIL_COND(!f);
  1510. int len;
  1511. Error err = encode_variant(p_var, NULL, len, p_full_objects);
  1512. ERR_FAIL_COND(err != OK);
  1513. PoolVector<uint8_t> buff;
  1514. buff.resize(len);
  1515. PoolVector<uint8_t>::Write w = buff.write();
  1516. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1517. ERR_FAIL_COND(err != OK);
  1518. w = PoolVector<uint8_t>::Write();
  1519. store_32(len);
  1520. store_buffer(buff);
  1521. }
  1522. Variant _File::get_var(bool p_allow_objects) const {
  1523. ERR_FAIL_COND_V(!f, Variant());
  1524. uint32_t len = get_32();
  1525. PoolVector<uint8_t> buff = get_buffer(len);
  1526. ERR_FAIL_COND_V((uint32_t)buff.size() != len, Variant());
  1527. PoolVector<uint8_t>::Read r = buff.read();
  1528. Variant v;
  1529. Error err = decode_variant(v, &r[0], len, NULL, p_allow_objects);
  1530. ERR_FAIL_COND_V(err != OK, Variant());
  1531. return v;
  1532. }
  1533. uint64_t _File::get_modified_time(const String &p_file) const {
  1534. return FileAccess::get_modified_time(p_file);
  1535. }
  1536. void _File::_bind_methods() {
  1537. ClassDB::bind_method(D_METHOD("open_encrypted", "path", "mode_flags", "key"), &_File::open_encrypted);
  1538. ClassDB::bind_method(D_METHOD("open_encrypted_with_pass", "path", "mode_flags", "pass"), &_File::open_encrypted_pass);
  1539. ClassDB::bind_method(D_METHOD("open_compressed", "path", "mode_flags", "compression_mode"), &_File::open_compressed, DEFVAL(0));
  1540. ClassDB::bind_method(D_METHOD("open", "path", "flags"), &_File::open);
  1541. ClassDB::bind_method(D_METHOD("close"), &_File::close);
  1542. ClassDB::bind_method(D_METHOD("get_path"), &_File::get_path);
  1543. ClassDB::bind_method(D_METHOD("get_path_absolute"), &_File::get_path_absolute);
  1544. ClassDB::bind_method(D_METHOD("is_open"), &_File::is_open);
  1545. ClassDB::bind_method(D_METHOD("seek", "position"), &_File::seek);
  1546. ClassDB::bind_method(D_METHOD("seek_end", "position"), &_File::seek_end, DEFVAL(0));
  1547. ClassDB::bind_method(D_METHOD("get_position"), &_File::get_position);
  1548. ClassDB::bind_method(D_METHOD("get_len"), &_File::get_len);
  1549. ClassDB::bind_method(D_METHOD("eof_reached"), &_File::eof_reached);
  1550. ClassDB::bind_method(D_METHOD("get_8"), &_File::get_8);
  1551. ClassDB::bind_method(D_METHOD("get_16"), &_File::get_16);
  1552. ClassDB::bind_method(D_METHOD("get_32"), &_File::get_32);
  1553. ClassDB::bind_method(D_METHOD("get_64"), &_File::get_64);
  1554. ClassDB::bind_method(D_METHOD("get_float"), &_File::get_float);
  1555. ClassDB::bind_method(D_METHOD("get_double"), &_File::get_double);
  1556. ClassDB::bind_method(D_METHOD("get_real"), &_File::get_real);
  1557. ClassDB::bind_method(D_METHOD("get_buffer", "len"), &_File::get_buffer);
  1558. ClassDB::bind_method(D_METHOD("get_line"), &_File::get_line);
  1559. ClassDB::bind_method(D_METHOD("get_csv_line", "delim"), &_File::get_csv_line, DEFVAL(","));
  1560. ClassDB::bind_method(D_METHOD("get_as_text"), &_File::get_as_text);
  1561. ClassDB::bind_method(D_METHOD("get_md5", "path"), &_File::get_md5);
  1562. ClassDB::bind_method(D_METHOD("get_sha256", "path"), &_File::get_sha256);
  1563. ClassDB::bind_method(D_METHOD("get_endian_swap"), &_File::get_endian_swap);
  1564. ClassDB::bind_method(D_METHOD("set_endian_swap", "enable"), &_File::set_endian_swap);
  1565. ClassDB::bind_method(D_METHOD("get_error"), &_File::get_error);
  1566. ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &_File::get_var, DEFVAL(false));
  1567. ClassDB::bind_method(D_METHOD("store_8", "value"), &_File::store_8);
  1568. ClassDB::bind_method(D_METHOD("store_16", "value"), &_File::store_16);
  1569. ClassDB::bind_method(D_METHOD("store_32", "value"), &_File::store_32);
  1570. ClassDB::bind_method(D_METHOD("store_64", "value"), &_File::store_64);
  1571. ClassDB::bind_method(D_METHOD("store_float", "value"), &_File::store_float);
  1572. ClassDB::bind_method(D_METHOD("store_double", "value"), &_File::store_double);
  1573. ClassDB::bind_method(D_METHOD("store_real", "value"), &_File::store_real);
  1574. ClassDB::bind_method(D_METHOD("store_buffer", "buffer"), &_File::store_buffer);
  1575. ClassDB::bind_method(D_METHOD("store_line", "line"), &_File::store_line);
  1576. ClassDB::bind_method(D_METHOD("store_csv_line", "values", "delim"), &_File::store_csv_line, DEFVAL(","));
  1577. ClassDB::bind_method(D_METHOD("store_string", "string"), &_File::store_string);
  1578. ClassDB::bind_method(D_METHOD("store_var", "value", "full_objects"), &_File::store_var, DEFVAL(false));
  1579. ClassDB::bind_method(D_METHOD("store_pascal_string", "string"), &_File::store_pascal_string);
  1580. ClassDB::bind_method(D_METHOD("get_pascal_string"), &_File::get_pascal_string);
  1581. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists);
  1582. ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time);
  1583. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "endian_swap"), "set_endian_swap", "get_endian_swap");
  1584. BIND_ENUM_CONSTANT(READ);
  1585. BIND_ENUM_CONSTANT(WRITE);
  1586. BIND_ENUM_CONSTANT(READ_WRITE);
  1587. BIND_ENUM_CONSTANT(WRITE_READ);
  1588. BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
  1589. BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
  1590. BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
  1591. BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
  1592. }
  1593. _File::_File() {
  1594. f = NULL;
  1595. eswap = false;
  1596. }
  1597. _File::~_File() {
  1598. if (f)
  1599. memdelete(f);
  1600. }
  1601. ///////////////////////////////////////////////////////
  1602. Error _Directory::open(const String &p_path) {
  1603. Error err;
  1604. DirAccess *alt = DirAccess::open(p_path, &err);
  1605. if (!alt)
  1606. return err;
  1607. if (d)
  1608. memdelete(d);
  1609. d = alt;
  1610. return OK;
  1611. }
  1612. Error _Directory::list_dir_begin(bool p_skip_navigational, bool p_skip_hidden) {
  1613. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1614. _list_skip_navigational = p_skip_navigational;
  1615. _list_skip_hidden = p_skip_hidden;
  1616. return d->list_dir_begin();
  1617. }
  1618. String _Directory::get_next() {
  1619. ERR_FAIL_COND_V(!d, "");
  1620. String next = d->get_next();
  1621. while (next != "" && ((_list_skip_navigational && (next == "." || next == "..")) || (_list_skip_hidden && d->current_is_hidden()))) {
  1622. next = d->get_next();
  1623. }
  1624. return next;
  1625. }
  1626. bool _Directory::current_is_dir() const {
  1627. ERR_FAIL_COND_V(!d, false);
  1628. return d->current_is_dir();
  1629. }
  1630. void _Directory::list_dir_end() {
  1631. ERR_FAIL_COND(!d);
  1632. return d->list_dir_end();
  1633. }
  1634. int _Directory::get_drive_count() {
  1635. ERR_FAIL_COND_V(!d, 0);
  1636. return d->get_drive_count();
  1637. }
  1638. String _Directory::get_drive(int p_drive) {
  1639. ERR_FAIL_COND_V(!d, "");
  1640. return d->get_drive(p_drive);
  1641. }
  1642. int _Directory::get_current_drive() {
  1643. ERR_FAIL_COND_V(!d, 0);
  1644. return d->get_current_drive();
  1645. }
  1646. Error _Directory::change_dir(String p_dir) {
  1647. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1648. return d->change_dir(p_dir);
  1649. }
  1650. String _Directory::get_current_dir() {
  1651. ERR_FAIL_COND_V(!d, "");
  1652. return d->get_current_dir();
  1653. }
  1654. Error _Directory::make_dir(String p_dir) {
  1655. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1656. if (!p_dir.is_rel_path()) {
  1657. DirAccess *d = DirAccess::create_for_path(p_dir);
  1658. Error err = d->make_dir(p_dir);
  1659. memdelete(d);
  1660. return err;
  1661. }
  1662. return d->make_dir(p_dir);
  1663. }
  1664. Error _Directory::make_dir_recursive(String p_dir) {
  1665. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1666. if (!p_dir.is_rel_path()) {
  1667. DirAccess *d = DirAccess::create_for_path(p_dir);
  1668. Error err = d->make_dir_recursive(p_dir);
  1669. memdelete(d);
  1670. return err;
  1671. }
  1672. return d->make_dir_recursive(p_dir);
  1673. }
  1674. bool _Directory::file_exists(String p_file) {
  1675. ERR_FAIL_COND_V(!d, false);
  1676. if (!p_file.is_rel_path()) {
  1677. return FileAccess::exists(p_file);
  1678. }
  1679. return d->file_exists(p_file);
  1680. }
  1681. bool _Directory::dir_exists(String p_dir) {
  1682. ERR_FAIL_COND_V(!d, false);
  1683. if (!p_dir.is_rel_path()) {
  1684. DirAccess *d = DirAccess::create_for_path(p_dir);
  1685. bool exists = d->dir_exists(p_dir);
  1686. memdelete(d);
  1687. return exists;
  1688. } else {
  1689. return d->dir_exists(p_dir);
  1690. }
  1691. }
  1692. int _Directory::get_space_left() {
  1693. ERR_FAIL_COND_V(!d, 0);
  1694. return d->get_space_left() / 1024 * 1024; //return value in megabytes, given binding is int
  1695. }
  1696. Error _Directory::copy(String p_from, String p_to) {
  1697. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1698. return d->copy(p_from, p_to);
  1699. }
  1700. Error _Directory::rename(String p_from, String p_to) {
  1701. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1702. if (!p_from.is_rel_path()) {
  1703. DirAccess *d = DirAccess::create_for_path(p_from);
  1704. Error err = d->rename(p_from, p_to);
  1705. memdelete(d);
  1706. return err;
  1707. }
  1708. return d->rename(p_from, p_to);
  1709. }
  1710. Error _Directory::remove(String p_name) {
  1711. ERR_FAIL_COND_V(!d, ERR_UNCONFIGURED);
  1712. if (!p_name.is_rel_path()) {
  1713. DirAccess *d = DirAccess::create_for_path(p_name);
  1714. Error err = d->remove(p_name);
  1715. memdelete(d);
  1716. return err;
  1717. }
  1718. return d->remove(p_name);
  1719. }
  1720. void _Directory::_bind_methods() {
  1721. ClassDB::bind_method(D_METHOD("open", "path"), &_Directory::open);
  1722. ClassDB::bind_method(D_METHOD("list_dir_begin", "skip_navigational", "skip_hidden"), &_Directory::list_dir_begin, DEFVAL(false), DEFVAL(false));
  1723. ClassDB::bind_method(D_METHOD("get_next"), &_Directory::get_next);
  1724. ClassDB::bind_method(D_METHOD("current_is_dir"), &_Directory::current_is_dir);
  1725. ClassDB::bind_method(D_METHOD("list_dir_end"), &_Directory::list_dir_end);
  1726. ClassDB::bind_method(D_METHOD("get_drive_count"), &_Directory::get_drive_count);
  1727. ClassDB::bind_method(D_METHOD("get_drive", "idx"), &_Directory::get_drive);
  1728. ClassDB::bind_method(D_METHOD("get_current_drive"), &_Directory::get_current_drive);
  1729. ClassDB::bind_method(D_METHOD("change_dir", "todir"), &_Directory::change_dir);
  1730. ClassDB::bind_method(D_METHOD("get_current_dir"), &_Directory::get_current_dir);
  1731. ClassDB::bind_method(D_METHOD("make_dir", "path"), &_Directory::make_dir);
  1732. ClassDB::bind_method(D_METHOD("make_dir_recursive", "path"), &_Directory::make_dir_recursive);
  1733. ClassDB::bind_method(D_METHOD("file_exists", "path"), &_Directory::file_exists);
  1734. ClassDB::bind_method(D_METHOD("dir_exists", "path"), &_Directory::dir_exists);
  1735. //ClassDB::bind_method(D_METHOD("get_modified_time","file"),&_Directory::get_modified_time);
  1736. ClassDB::bind_method(D_METHOD("get_space_left"), &_Directory::get_space_left);
  1737. ClassDB::bind_method(D_METHOD("copy", "from", "to"), &_Directory::copy);
  1738. ClassDB::bind_method(D_METHOD("rename", "from", "to"), &_Directory::rename);
  1739. ClassDB::bind_method(D_METHOD("remove", "path"), &_Directory::remove);
  1740. }
  1741. _Directory::_Directory() {
  1742. d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1743. }
  1744. _Directory::~_Directory() {
  1745. if (d)
  1746. memdelete(d);
  1747. }
  1748. _Marshalls *_Marshalls::singleton = NULL;
  1749. _Marshalls *_Marshalls::get_singleton() {
  1750. return singleton;
  1751. }
  1752. String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects) {
  1753. int len;
  1754. Error err = encode_variant(p_var, NULL, len, p_full_objects);
  1755. ERR_FAIL_COND_V(err != OK, "");
  1756. PoolVector<uint8_t> buff;
  1757. buff.resize(len);
  1758. PoolVector<uint8_t>::Write w = buff.write();
  1759. err = encode_variant(p_var, &w[0], len, p_full_objects);
  1760. ERR_FAIL_COND_V(err != OK, "");
  1761. int b64len = len / 3 * 4 + 4 + 1;
  1762. PoolVector<uint8_t> b64buff;
  1763. b64buff.resize(b64len);
  1764. PoolVector<uint8_t>::Write w64 = b64buff.write();
  1765. int strlen = base64_encode((char *)(&w64[0]), (char *)(&w[0]), len);
  1766. //OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen);
  1767. w64[strlen] = 0;
  1768. String ret = (char *)&w64[0];
  1769. return ret;
  1770. };
  1771. Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects) {
  1772. int strlen = p_str.length();
  1773. CharString cstr = p_str.ascii();
  1774. PoolVector<uint8_t> buf;
  1775. buf.resize(strlen / 4 * 3 + 1);
  1776. PoolVector<uint8_t>::Write w = buf.write();
  1777. int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
  1778. Variant v;
  1779. Error err = decode_variant(v, &w[0], len, NULL, p_allow_objects);
  1780. ERR_FAIL_COND_V(err != OK, Variant());
  1781. return v;
  1782. };
  1783. String _Marshalls::raw_to_base64(const PoolVector<uint8_t> &p_arr) {
  1784. int len = p_arr.size();
  1785. PoolVector<uint8_t>::Read r = p_arr.read();
  1786. int b64len = len / 3 * 4 + 4 + 1;
  1787. PoolVector<uint8_t> b64buff;
  1788. b64buff.resize(b64len);
  1789. PoolVector<uint8_t>::Write w64 = b64buff.write();
  1790. int strlen = base64_encode((char *)(&w64[0]), (char *)(&r[0]), len);
  1791. w64[strlen] = 0;
  1792. String ret = (char *)&w64[0];
  1793. return ret;
  1794. };
  1795. PoolVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
  1796. int strlen = p_str.length();
  1797. CharString cstr = p_str.ascii();
  1798. int arr_len;
  1799. PoolVector<uint8_t> buf;
  1800. {
  1801. buf.resize(strlen / 4 * 3 + 1);
  1802. PoolVector<uint8_t>::Write w = buf.write();
  1803. arr_len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
  1804. };
  1805. buf.resize(arr_len);
  1806. // conversion from PoolVector<uint8_t> to raw array?
  1807. return buf;
  1808. };
  1809. String _Marshalls::utf8_to_base64(const String &p_str) {
  1810. CharString cstr = p_str.utf8();
  1811. int len = cstr.length();
  1812. int b64len = len / 3 * 4 + 4 + 1;
  1813. PoolVector<uint8_t> b64buff;
  1814. b64buff.resize(b64len);
  1815. PoolVector<uint8_t>::Write w64 = b64buff.write();
  1816. int strlen = base64_encode((char *)(&w64[0]), (char *)cstr.get_data(), len);
  1817. w64[strlen] = 0;
  1818. String ret = (char *)&w64[0];
  1819. return ret;
  1820. };
  1821. String _Marshalls::base64_to_utf8(const String &p_str) {
  1822. int strlen = p_str.length();
  1823. CharString cstr = p_str.ascii();
  1824. PoolVector<uint8_t> buf;
  1825. buf.resize(strlen / 4 * 3 + 1 + 1);
  1826. PoolVector<uint8_t>::Write w = buf.write();
  1827. int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
  1828. w[len] = 0;
  1829. String ret = String::utf8((char *)&w[0]);
  1830. return ret;
  1831. };
  1832. void _Marshalls::_bind_methods() {
  1833. ClassDB::bind_method(D_METHOD("variant_to_base64", "variant", "full_objects"), &_Marshalls::variant_to_base64, DEFVAL(false));
  1834. ClassDB::bind_method(D_METHOD("base64_to_variant", "base64_str", "allow_objects"), &_Marshalls::base64_to_variant, DEFVAL(false));
  1835. ClassDB::bind_method(D_METHOD("raw_to_base64", "array"), &_Marshalls::raw_to_base64);
  1836. ClassDB::bind_method(D_METHOD("base64_to_raw", "base64_str"), &_Marshalls::base64_to_raw);
  1837. ClassDB::bind_method(D_METHOD("utf8_to_base64", "utf8_str"), &_Marshalls::utf8_to_base64);
  1838. ClassDB::bind_method(D_METHOD("base64_to_utf8", "base64_str"), &_Marshalls::base64_to_utf8);
  1839. };
  1840. ////////////////
  1841. Error _Semaphore::wait() {
  1842. return semaphore->wait();
  1843. }
  1844. Error _Semaphore::post() {
  1845. return semaphore->post();
  1846. }
  1847. void _Semaphore::_bind_methods() {
  1848. ClassDB::bind_method(D_METHOD("wait"), &_Semaphore::wait);
  1849. ClassDB::bind_method(D_METHOD("post"), &_Semaphore::post);
  1850. }
  1851. _Semaphore::_Semaphore() {
  1852. semaphore = Semaphore::create();
  1853. }
  1854. _Semaphore::~_Semaphore() {
  1855. memdelete(semaphore);
  1856. }
  1857. ///////////////
  1858. void _Mutex::lock() {
  1859. mutex->lock();
  1860. }
  1861. Error _Mutex::try_lock() {
  1862. return mutex->try_lock();
  1863. }
  1864. void _Mutex::unlock() {
  1865. mutex->unlock();
  1866. }
  1867. void _Mutex::_bind_methods() {
  1868. ClassDB::bind_method(D_METHOD("lock"), &_Mutex::lock);
  1869. ClassDB::bind_method(D_METHOD("try_lock"), &_Mutex::try_lock);
  1870. ClassDB::bind_method(D_METHOD("unlock"), &_Mutex::unlock);
  1871. }
  1872. _Mutex::_Mutex() {
  1873. mutex = Mutex::create();
  1874. }
  1875. _Mutex::~_Mutex() {
  1876. memdelete(mutex);
  1877. }
  1878. ///////////////
  1879. void _Thread::_start_func(void *ud) {
  1880. Ref<_Thread> *tud = (Ref<_Thread> *)ud;
  1881. Ref<_Thread> t = *tud;
  1882. memdelete(tud);
  1883. Variant::CallError ce;
  1884. const Variant *arg[1] = { &t->userdata };
  1885. Thread::set_name(t->target_method);
  1886. t->ret = t->target_instance->call(t->target_method, arg, 1, ce);
  1887. if (ce.error != Variant::CallError::CALL_OK) {
  1888. String reason;
  1889. switch (ce.error) {
  1890. case Variant::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1891. reason = "Invalid Argument #" + itos(ce.argument);
  1892. } break;
  1893. case Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
  1894. reason = "Too Many Arguments";
  1895. } break;
  1896. case Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: {
  1897. reason = "Too Few Arguments";
  1898. } break;
  1899. case Variant::CallError::CALL_ERROR_INVALID_METHOD: {
  1900. reason = "Method Not Found";
  1901. } break;
  1902. default: {
  1903. }
  1904. }
  1905. ERR_EXPLAIN("Could not call function '" + t->target_method.operator String() + "'' starting thread ID: " + t->get_id() + " Reason: " + reason);
  1906. ERR_FAIL();
  1907. }
  1908. }
  1909. Error _Thread::start(Object *p_instance, const StringName &p_method, const Variant &p_userdata, Priority p_priority) {
  1910. ERR_FAIL_COND_V(active, ERR_ALREADY_IN_USE);
  1911. ERR_FAIL_COND_V(!p_instance, ERR_INVALID_PARAMETER);
  1912. ERR_FAIL_COND_V(p_method == StringName(), ERR_INVALID_PARAMETER);
  1913. ERR_FAIL_INDEX_V(p_priority, PRIORITY_MAX, ERR_INVALID_PARAMETER);
  1914. ret = Variant();
  1915. target_method = p_method;
  1916. target_instance = p_instance;
  1917. userdata = p_userdata;
  1918. active = true;
  1919. Ref<_Thread> *ud = memnew(Ref<_Thread>(this));
  1920. Thread::Settings s;
  1921. s.priority = (Thread::Priority)p_priority;
  1922. thread = Thread::create(_start_func, ud, s);
  1923. if (!thread) {
  1924. active = false;
  1925. target_method = StringName();
  1926. target_instance = NULL;
  1927. userdata = Variant();
  1928. return ERR_CANT_CREATE;
  1929. }
  1930. return OK;
  1931. }
  1932. String _Thread::get_id() const {
  1933. if (!thread)
  1934. return String();
  1935. return itos(thread->get_id());
  1936. }
  1937. bool _Thread::is_active() const {
  1938. return active;
  1939. }
  1940. Variant _Thread::wait_to_finish() {
  1941. ERR_FAIL_COND_V(!thread, Variant());
  1942. ERR_FAIL_COND_V(!active, Variant());
  1943. Thread::wait_to_finish(thread);
  1944. Variant r = ret;
  1945. active = false;
  1946. target_method = StringName();
  1947. target_instance = NULL;
  1948. userdata = Variant();
  1949. thread = NULL;
  1950. return r;
  1951. }
  1952. void _Thread::_bind_methods() {
  1953. ClassDB::bind_method(D_METHOD("start", "instance", "method", "userdata", "priority"), &_Thread::start, DEFVAL(Variant()), DEFVAL(PRIORITY_NORMAL));
  1954. ClassDB::bind_method(D_METHOD("get_id"), &_Thread::get_id);
  1955. ClassDB::bind_method(D_METHOD("is_active"), &_Thread::is_active);
  1956. ClassDB::bind_method(D_METHOD("wait_to_finish"), &_Thread::wait_to_finish);
  1957. BIND_ENUM_CONSTANT(PRIORITY_LOW);
  1958. BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
  1959. BIND_ENUM_CONSTANT(PRIORITY_HIGH);
  1960. }
  1961. _Thread::_Thread() {
  1962. active = false;
  1963. thread = NULL;
  1964. target_instance = NULL;
  1965. }
  1966. _Thread::~_Thread() {
  1967. if (active) {
  1968. ERR_EXPLAIN("Reference to a Thread object object was lost while the thread is still running...");
  1969. }
  1970. ERR_FAIL_COND(active);
  1971. }
  1972. /////////////////////////////////////
  1973. PoolStringArray _ClassDB::get_class_list() const {
  1974. List<StringName> classes;
  1975. ClassDB::get_class_list(&classes);
  1976. PoolStringArray ret;
  1977. ret.resize(classes.size());
  1978. int idx = 0;
  1979. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  1980. ret.set(idx++, E->get());
  1981. }
  1982. return ret;
  1983. }
  1984. PoolStringArray _ClassDB::get_inheriters_from_class(const StringName &p_class) const {
  1985. List<StringName> classes;
  1986. ClassDB::get_inheriters_from_class(p_class, &classes);
  1987. PoolStringArray ret;
  1988. ret.resize(classes.size());
  1989. int idx = 0;
  1990. for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
  1991. ret.set(idx++, E->get());
  1992. }
  1993. return ret;
  1994. }
  1995. StringName _ClassDB::get_parent_class(const StringName &p_class) const {
  1996. return ClassDB::get_parent_class(p_class);
  1997. }
  1998. bool _ClassDB::class_exists(const StringName &p_class) const {
  1999. return ClassDB::class_exists(p_class);
  2000. }
  2001. bool _ClassDB::is_parent_class(const StringName &p_class, const StringName &p_inherits) const {
  2002. return ClassDB::is_parent_class(p_class, p_inherits);
  2003. }
  2004. bool _ClassDB::can_instance(const StringName &p_class) const {
  2005. return ClassDB::can_instance(p_class);
  2006. }
  2007. Variant _ClassDB::instance(const StringName &p_class) const {
  2008. Object *obj = ClassDB::instance(p_class);
  2009. if (!obj)
  2010. return Variant();
  2011. Reference *r = Object::cast_to<Reference>(obj);
  2012. if (r) {
  2013. return REF(r);
  2014. } else {
  2015. return obj;
  2016. }
  2017. }
  2018. bool _ClassDB::has_signal(StringName p_class, StringName p_signal) const {
  2019. return ClassDB::has_signal(p_class, p_signal);
  2020. }
  2021. Dictionary _ClassDB::get_signal(StringName p_class, StringName p_signal) const {
  2022. MethodInfo signal;
  2023. if (ClassDB::get_signal(p_class, p_signal, &signal)) {
  2024. return signal.operator Dictionary();
  2025. } else {
  2026. return Dictionary();
  2027. }
  2028. }
  2029. Array _ClassDB::get_signal_list(StringName p_class, bool p_no_inheritance) const {
  2030. List<MethodInfo> signals;
  2031. ClassDB::get_signal_list(p_class, &signals, p_no_inheritance);
  2032. Array ret;
  2033. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  2034. ret.push_back(E->get().operator Dictionary());
  2035. }
  2036. return ret;
  2037. }
  2038. Array _ClassDB::get_property_list(StringName p_class, bool p_no_inheritance) const {
  2039. List<PropertyInfo> plist;
  2040. ClassDB::get_property_list(p_class, &plist, p_no_inheritance);
  2041. Array ret;
  2042. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  2043. ret.push_back(E->get().operator Dictionary());
  2044. }
  2045. return ret;
  2046. }
  2047. Variant _ClassDB::get_property(Object *p_object, const StringName &p_property) const {
  2048. Variant ret;
  2049. ClassDB::get_property(p_object, p_property, ret);
  2050. return ret;
  2051. }
  2052. Error _ClassDB::set_property(Object *p_object, const StringName &p_property, const Variant &p_value) const {
  2053. Variant ret;
  2054. bool valid;
  2055. if (!ClassDB::set_property(p_object, p_property, p_value, &valid)) {
  2056. return ERR_UNAVAILABLE;
  2057. } else if (!valid) {
  2058. return ERR_INVALID_DATA;
  2059. }
  2060. return OK;
  2061. }
  2062. bool _ClassDB::has_method(StringName p_class, StringName p_method, bool p_no_inheritance) const {
  2063. return ClassDB::has_method(p_class, p_method, p_no_inheritance);
  2064. }
  2065. Array _ClassDB::get_method_list(StringName p_class, bool p_no_inheritance) const {
  2066. List<MethodInfo> methods;
  2067. ClassDB::get_method_list(p_class, &methods, p_no_inheritance);
  2068. Array ret;
  2069. for (List<MethodInfo>::Element *E = methods.front(); E; E = E->next()) {
  2070. #ifdef DEBUG_METHODS_ENABLED
  2071. ret.push_back(E->get().operator Dictionary());
  2072. #else
  2073. Dictionary dict;
  2074. dict["name"] = E->get().name;
  2075. ret.push_back(dict);
  2076. #endif
  2077. }
  2078. return ret;
  2079. }
  2080. PoolStringArray _ClassDB::get_integer_constant_list(const StringName &p_class, bool p_no_inheritance) const {
  2081. List<String> constants;
  2082. ClassDB::get_integer_constant_list(p_class, &constants, p_no_inheritance);
  2083. PoolStringArray ret;
  2084. ret.resize(constants.size());
  2085. int idx = 0;
  2086. for (List<String>::Element *E = constants.front(); E; E = E->next()) {
  2087. ret.set(idx++, E->get());
  2088. }
  2089. return ret;
  2090. }
  2091. bool _ClassDB::has_integer_constant(const StringName &p_class, const StringName &p_name) const {
  2092. bool success;
  2093. ClassDB::get_integer_constant(p_class, p_name, &success);
  2094. return success;
  2095. }
  2096. int _ClassDB::get_integer_constant(const StringName &p_class, const StringName &p_name) const {
  2097. bool found;
  2098. int c = ClassDB::get_integer_constant(p_class, p_name, &found);
  2099. ERR_FAIL_COND_V(!found, 0);
  2100. return c;
  2101. }
  2102. StringName _ClassDB::get_category(const StringName &p_node) const {
  2103. return ClassDB::get_category(p_node);
  2104. }
  2105. bool _ClassDB::is_class_enabled(StringName p_class) const {
  2106. return ClassDB::is_class_enabled(p_class);
  2107. }
  2108. void _ClassDB::_bind_methods() {
  2109. ClassDB::bind_method(D_METHOD("get_class_list"), &_ClassDB::get_class_list);
  2110. ClassDB::bind_method(D_METHOD("get_inheriters_from_class", "class"), &_ClassDB::get_inheriters_from_class);
  2111. ClassDB::bind_method(D_METHOD("get_parent_class", "class"), &_ClassDB::get_parent_class);
  2112. ClassDB::bind_method(D_METHOD("class_exists", "class"), &_ClassDB::class_exists);
  2113. ClassDB::bind_method(D_METHOD("is_parent_class", "class", "inherits"), &_ClassDB::is_parent_class);
  2114. ClassDB::bind_method(D_METHOD("can_instance", "class"), &_ClassDB::can_instance);
  2115. ClassDB::bind_method(D_METHOD("instance", "class"), &_ClassDB::instance);
  2116. ClassDB::bind_method(D_METHOD("class_has_signal", "class", "signal"), &_ClassDB::has_signal);
  2117. ClassDB::bind_method(D_METHOD("class_get_signal", "class", "signal"), &_ClassDB::get_signal);
  2118. ClassDB::bind_method(D_METHOD("class_get_signal_list", "class", "no_inheritance"), &_ClassDB::get_signal_list, DEFVAL(false));
  2119. ClassDB::bind_method(D_METHOD("class_get_property_list", "class", "no_inheritance"), &_ClassDB::get_property_list, DEFVAL(false));
  2120. ClassDB::bind_method(D_METHOD("class_get_property", "object", "property"), &_ClassDB::get_property);
  2121. ClassDB::bind_method(D_METHOD("class_set_property", "object", "property", "value"), &_ClassDB::set_property);
  2122. ClassDB::bind_method(D_METHOD("class_has_method", "class", "method", "no_inheritance"), &_ClassDB::has_method, DEFVAL(false));
  2123. ClassDB::bind_method(D_METHOD("class_get_method_list", "class", "no_inheritance"), &_ClassDB::get_method_list, DEFVAL(false));
  2124. ClassDB::bind_method(D_METHOD("class_get_integer_constant_list", "class", "no_inheritance"), &_ClassDB::get_integer_constant_list, DEFVAL(false));
  2125. ClassDB::bind_method(D_METHOD("class_has_integer_constant", "class", "name"), &_ClassDB::has_integer_constant);
  2126. ClassDB::bind_method(D_METHOD("class_get_integer_constant", "class", "name"), &_ClassDB::get_integer_constant);
  2127. ClassDB::bind_method(D_METHOD("class_get_category", "class"), &_ClassDB::get_category);
  2128. ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &_ClassDB::is_class_enabled);
  2129. }
  2130. _ClassDB::_ClassDB() {
  2131. }
  2132. _ClassDB::~_ClassDB() {
  2133. }
  2134. ///////////////////////////////
  2135. void _Engine::set_iterations_per_second(int p_ips) {
  2136. Engine::get_singleton()->set_iterations_per_second(p_ips);
  2137. }
  2138. int _Engine::get_iterations_per_second() const {
  2139. return Engine::get_singleton()->get_iterations_per_second();
  2140. }
  2141. void _Engine::set_physics_jitter_fix(float p_threshold) {
  2142. Engine::get_singleton()->set_physics_jitter_fix(p_threshold);
  2143. }
  2144. float _Engine::get_physics_jitter_fix() const {
  2145. return Engine::get_singleton()->get_physics_jitter_fix();
  2146. }
  2147. void _Engine::set_target_fps(int p_fps) {
  2148. Engine::get_singleton()->set_target_fps(p_fps);
  2149. }
  2150. int _Engine::get_target_fps() const {
  2151. return Engine::get_singleton()->get_target_fps();
  2152. }
  2153. float _Engine::get_frames_per_second() const {
  2154. return Engine::get_singleton()->get_frames_per_second();
  2155. }
  2156. void _Engine::set_time_scale(float p_scale) {
  2157. Engine::get_singleton()->set_time_scale(p_scale);
  2158. }
  2159. float _Engine::get_time_scale() {
  2160. return Engine::get_singleton()->get_time_scale();
  2161. }
  2162. int _Engine::get_frames_drawn() {
  2163. return Engine::get_singleton()->get_frames_drawn();
  2164. }
  2165. MainLoop *_Engine::get_main_loop() const {
  2166. //needs to remain in OS, since it's actually OS that interacts with it, but it's better exposed here
  2167. return OS::get_singleton()->get_main_loop();
  2168. }
  2169. Dictionary _Engine::get_version_info() const {
  2170. return Engine::get_singleton()->get_version_info();
  2171. }
  2172. Dictionary _Engine::get_author_info() const {
  2173. return Engine::get_singleton()->get_author_info();
  2174. }
  2175. Array _Engine::get_copyright_info() const {
  2176. return Engine::get_singleton()->get_copyright_info();
  2177. }
  2178. Dictionary _Engine::get_donor_info() const {
  2179. return Engine::get_singleton()->get_donor_info();
  2180. }
  2181. Dictionary _Engine::get_license_info() const {
  2182. return Engine::get_singleton()->get_license_info();
  2183. }
  2184. String _Engine::get_license_text() const {
  2185. return Engine::get_singleton()->get_license_text();
  2186. }
  2187. bool _Engine::is_in_physics_frame() const {
  2188. return Engine::get_singleton()->is_in_physics_frame();
  2189. }
  2190. bool _Engine::has_singleton(const String &p_name) const {
  2191. return Engine::get_singleton()->has_singleton(p_name);
  2192. }
  2193. Object *_Engine::get_singleton_object(const String &p_name) const {
  2194. return Engine::get_singleton()->get_singleton_object(p_name);
  2195. }
  2196. void _Engine::set_editor_hint(bool p_enabled) {
  2197. Engine::get_singleton()->set_editor_hint(p_enabled);
  2198. }
  2199. bool _Engine::is_editor_hint() const {
  2200. return Engine::get_singleton()->is_editor_hint();
  2201. }
  2202. void _Engine::_bind_methods() {
  2203. ClassDB::bind_method(D_METHOD("set_iterations_per_second", "iterations_per_second"), &_Engine::set_iterations_per_second);
  2204. ClassDB::bind_method(D_METHOD("get_iterations_per_second"), &_Engine::get_iterations_per_second);
  2205. ClassDB::bind_method(D_METHOD("set_physics_jitter_fix", "physics_jitter_fix"), &_Engine::set_physics_jitter_fix);
  2206. ClassDB::bind_method(D_METHOD("get_physics_jitter_fix"), &_Engine::get_physics_jitter_fix);
  2207. ClassDB::bind_method(D_METHOD("set_target_fps", "target_fps"), &_Engine::set_target_fps);
  2208. ClassDB::bind_method(D_METHOD("get_target_fps"), &_Engine::get_target_fps);
  2209. ClassDB::bind_method(D_METHOD("set_time_scale", "time_scale"), &_Engine::set_time_scale);
  2210. ClassDB::bind_method(D_METHOD("get_time_scale"), &_Engine::get_time_scale);
  2211. ClassDB::bind_method(D_METHOD("get_frames_drawn"), &_Engine::get_frames_drawn);
  2212. ClassDB::bind_method(D_METHOD("get_frames_per_second"), &_Engine::get_frames_per_second);
  2213. ClassDB::bind_method(D_METHOD("get_main_loop"), &_Engine::get_main_loop);
  2214. ClassDB::bind_method(D_METHOD("get_version_info"), &_Engine::get_version_info);
  2215. ClassDB::bind_method(D_METHOD("get_author_info"), &_Engine::get_author_info);
  2216. ClassDB::bind_method(D_METHOD("get_copyright_info"), &_Engine::get_copyright_info);
  2217. ClassDB::bind_method(D_METHOD("get_donor_info"), &_Engine::get_donor_info);
  2218. ClassDB::bind_method(D_METHOD("get_license_info"), &_Engine::get_license_info);
  2219. ClassDB::bind_method(D_METHOD("get_license_text"), &_Engine::get_license_text);
  2220. ClassDB::bind_method(D_METHOD("is_in_physics_frame"), &_Engine::is_in_physics_frame);
  2221. ClassDB::bind_method(D_METHOD("has_singleton", "name"), &_Engine::has_singleton);
  2222. ClassDB::bind_method(D_METHOD("get_singleton", "name"), &_Engine::get_singleton_object);
  2223. ClassDB::bind_method(D_METHOD("set_editor_hint", "enabled"), &_Engine::set_editor_hint);
  2224. ClassDB::bind_method(D_METHOD("is_editor_hint"), &_Engine::is_editor_hint);
  2225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_hint"), "set_editor_hint", "is_editor_hint");
  2226. ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations_per_second"), "set_iterations_per_second", "get_iterations_per_second");
  2227. ADD_PROPERTY(PropertyInfo(Variant::INT, "target_fps"), "set_target_fps", "get_target_fps");
  2228. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_scale"), "set_time_scale", "get_time_scale");
  2229. ADD_PROPERTY(PropertyInfo(Variant::REAL, "physics_jitter_fix"), "set_physics_jitter_fix", "get_physics_jitter_fix");
  2230. }
  2231. _Engine *_Engine::singleton = NULL;
  2232. _Engine::_Engine() {
  2233. singleton = this;
  2234. }
  2235. void JSONParseResult::_bind_methods() {
  2236. ClassDB::bind_method(D_METHOD("get_error"), &JSONParseResult::get_error);
  2237. ClassDB::bind_method(D_METHOD("get_error_string"), &JSONParseResult::get_error_string);
  2238. ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParseResult::get_error_line);
  2239. ClassDB::bind_method(D_METHOD("get_result"), &JSONParseResult::get_result);
  2240. ClassDB::bind_method(D_METHOD("set_error", "error"), &JSONParseResult::set_error);
  2241. ClassDB::bind_method(D_METHOD("set_error_string", "error_string"), &JSONParseResult::set_error_string);
  2242. ClassDB::bind_method(D_METHOD("set_error_line", "error_line"), &JSONParseResult::set_error_line);
  2243. ClassDB::bind_method(D_METHOD("set_result", "result"), &JSONParseResult::set_result);
  2244. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "error", PROPERTY_HINT_NONE, "Error", PROPERTY_USAGE_CLASS_IS_ENUM), "set_error", "get_error");
  2245. ADD_PROPERTY(PropertyInfo(Variant::STRING, "error_string"), "set_error_string", "get_error_string");
  2246. ADD_PROPERTY(PropertyInfo(Variant::INT, "error_line"), "set_error_line", "get_error_line");
  2247. ADD_PROPERTY(PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), "set_result", "get_result");
  2248. }
  2249. void JSONParseResult::set_error(Error p_error) {
  2250. error = p_error;
  2251. }
  2252. Error JSONParseResult::get_error() const {
  2253. return error;
  2254. }
  2255. void JSONParseResult::set_error_string(const String &p_error_string) {
  2256. error_string = p_error_string;
  2257. }
  2258. String JSONParseResult::get_error_string() const {
  2259. return error_string;
  2260. }
  2261. void JSONParseResult::set_error_line(int p_error_line) {
  2262. error_line = p_error_line;
  2263. }
  2264. int JSONParseResult::get_error_line() const {
  2265. return error_line;
  2266. }
  2267. void JSONParseResult::set_result(const Variant &p_result) {
  2268. result = p_result;
  2269. }
  2270. Variant JSONParseResult::get_result() const {
  2271. return result;
  2272. }
  2273. void _JSON::_bind_methods() {
  2274. ClassDB::bind_method(D_METHOD("print", "value", "indent", "sort_keys"), &_JSON::print, DEFVAL(String()), DEFVAL(false));
  2275. ClassDB::bind_method(D_METHOD("parse", "json"), &_JSON::parse);
  2276. }
  2277. String _JSON::print(const Variant &p_value, const String &p_indent, bool p_sort_keys) {
  2278. return JSON::print(p_value, p_indent, p_sort_keys);
  2279. }
  2280. Ref<JSONParseResult> _JSON::parse(const String &p_json) {
  2281. Ref<JSONParseResult> result;
  2282. result.instance();
  2283. result->error = JSON::parse(p_json, result->result, result->error_string, result->error_line);
  2284. return result;
  2285. }
  2286. _JSON *_JSON::singleton = NULL;
  2287. _JSON::_JSON() {
  2288. singleton = this;
  2289. }