core_bind.cpp 96 KB

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