bindings_generator.cpp 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682
  1. /*************************************************************************/
  2. /* bindings_generator.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "bindings_generator.h"
  31. #if defined(DEBUG_METHODS_ENABLED) && defined(TOOLS_ENABLED)
  32. #include "core/config/engine.h"
  33. #include "core/core_constants.h"
  34. #include "core/io/compression.h"
  35. #include "core/os/dir_access.h"
  36. #include "core/os/file_access.h"
  37. #include "core/os/os.h"
  38. #include "core/string/ucaps.h"
  39. #include "../glue/cs_glue_version.gen.h"
  40. #include "../godotsharp_defs.h"
  41. #include "../mono_gd/gd_mono_marshal.h"
  42. #include "../utils/path_utils.h"
  43. #include "../utils/string_utils.h"
  44. #define CS_INDENT " " // 4 whitespaces
  45. #define INDENT1 CS_INDENT
  46. #define INDENT2 INDENT1 INDENT1
  47. #define INDENT3 INDENT2 INDENT1
  48. #define INDENT4 INDENT3 INDENT1
  49. #define INDENT5 INDENT4 INDENT1
  50. #define MEMBER_BEGIN "\n" INDENT2
  51. #define OPEN_BLOCK "{\n"
  52. #define CLOSE_BLOCK "}\n"
  53. #define OPEN_BLOCK_L2 INDENT2 OPEN_BLOCK INDENT3
  54. #define OPEN_BLOCK_L3 INDENT3 OPEN_BLOCK INDENT4
  55. #define CLOSE_BLOCK_L2 INDENT2 CLOSE_BLOCK
  56. #define CLOSE_BLOCK_L3 INDENT3 CLOSE_BLOCK
  57. #define CS_FIELD_MEMORYOWN "memoryOwn"
  58. #define CS_PARAM_METHODBIND "method"
  59. #define CS_PARAM_INSTANCE "ptr"
  60. #define CS_SMETHOD_GETINSTANCE "GetPtr"
  61. #define CS_METHOD_CALL "Call"
  62. #define GLUE_HEADER_FILE "glue_header.h"
  63. #define ICALL_PREFIX "godot_icall_"
  64. #define SINGLETON_ICALL_SUFFIX "_get_singleton"
  65. #define ICALL_GET_METHODBIND "__ClassDB_get_method"
  66. #define C_LOCAL_RET "ret"
  67. #define C_LOCAL_VARARG_RET "vararg_ret"
  68. #define C_LOCAL_PTRCALL_ARGS "call_args"
  69. #define C_MACRO_OBJECT_CONSTRUCT "GODOTSHARP_INSTANCE_OBJECT"
  70. #define C_NS_MONOUTILS "GDMonoUtils"
  71. #define C_NS_MONOINTERNALS "GDMonoInternals"
  72. #define C_METHOD_TIE_MANAGED_TO_UNMANAGED C_NS_MONOINTERNALS "::tie_managed_to_unmanaged"
  73. #define C_METHOD_UNMANAGED_GET_MANAGED C_NS_MONOUTILS "::unmanaged_get_managed"
  74. #define C_NS_MONOMARSHAL "GDMonoMarshal"
  75. #define C_METHOD_MANAGED_TO_VARIANT C_NS_MONOMARSHAL "::mono_object_to_variant"
  76. #define C_METHOD_MANAGED_FROM_VARIANT C_NS_MONOMARSHAL "::variant_to_mono_object"
  77. #define C_METHOD_MONOSTR_TO_GODOT C_NS_MONOMARSHAL "::mono_string_to_godot"
  78. #define C_METHOD_MONOSTR_FROM_GODOT C_NS_MONOMARSHAL "::mono_string_from_godot"
  79. #define C_METHOD_MONOARRAY_TO(m_type) C_NS_MONOMARSHAL "::mono_array_to_" #m_type
  80. #define C_METHOD_MONOARRAY_FROM(m_type) C_NS_MONOMARSHAL "::" #m_type "_to_mono_array"
  81. #define C_METHOD_MANAGED_TO_CALLABLE C_NS_MONOMARSHAL "::managed_to_callable"
  82. #define C_METHOD_MANAGED_FROM_CALLABLE C_NS_MONOMARSHAL "::callable_to_managed"
  83. #define C_METHOD_MANAGED_TO_SIGNAL C_NS_MONOMARSHAL "::signal_info_to_callable"
  84. #define C_METHOD_MANAGED_FROM_SIGNAL C_NS_MONOMARSHAL "::callable_to_signal_info"
  85. #define BINDINGS_GENERATOR_VERSION UINT32_C(13)
  86. const char *BindingsGenerator::TypeInterface::DEFAULT_VARARG_C_IN("\t%0 %1_in = %1;\n");
  87. static String fix_doc_description(const String &p_bbcode) {
  88. // This seems to be the correct way to do this. It's the same EditorHelp does.
  89. return p_bbcode.dedent()
  90. .replace("\t", "")
  91. .replace("\r", "")
  92. .strip_edges();
  93. }
  94. static String snake_to_pascal_case(const String &p_identifier, bool p_input_is_upper = false) {
  95. String ret;
  96. Vector<String> parts = p_identifier.split("_", true);
  97. for (int i = 0; i < parts.size(); i++) {
  98. String part = parts[i];
  99. if (part.length()) {
  100. part[0] = _find_upper(part[0]);
  101. if (p_input_is_upper) {
  102. for (int j = 1; j < part.length(); j++) {
  103. part[j] = _find_lower(part[j]);
  104. }
  105. }
  106. ret += part;
  107. } else {
  108. if (i == 0 || i == (parts.size() - 1)) {
  109. // Preserve underscores at the beginning and end
  110. ret += "_";
  111. } else {
  112. // Preserve contiguous underscores
  113. if (parts[i - 1].length()) {
  114. ret += "__";
  115. } else {
  116. ret += "_";
  117. }
  118. }
  119. }
  120. }
  121. return ret;
  122. }
  123. static String snake_to_camel_case(const String &p_identifier, bool p_input_is_upper = false) {
  124. String ret;
  125. Vector<String> parts = p_identifier.split("_", true);
  126. for (int i = 0; i < parts.size(); i++) {
  127. String part = parts[i];
  128. if (part.length()) {
  129. if (i != 0) {
  130. part[0] = _find_upper(part[0]);
  131. }
  132. if (p_input_is_upper) {
  133. for (int j = i != 0 ? 1 : 0; j < part.length(); j++) {
  134. part[j] = _find_lower(part[j]);
  135. }
  136. }
  137. ret += part;
  138. } else {
  139. if (i == 0 || i == (parts.size() - 1)) {
  140. // Preserve underscores at the beginning and end
  141. ret += "_";
  142. } else {
  143. // Preserve contiguous underscores
  144. if (parts[i - 1].length()) {
  145. ret += "__";
  146. } else {
  147. ret += "_";
  148. }
  149. }
  150. }
  151. }
  152. return ret;
  153. }
  154. String BindingsGenerator::bbcode_to_xml(const String &p_bbcode, const TypeInterface *p_itype) {
  155. // Based on the version in EditorHelp
  156. if (p_bbcode.is_empty()) {
  157. return String();
  158. }
  159. DocTools *doc = EditorHelp::get_doc_data();
  160. String bbcode = p_bbcode;
  161. StringBuilder xml_output;
  162. xml_output.append("<para>");
  163. List<String> tag_stack;
  164. bool code_tag = false;
  165. int pos = 0;
  166. while (pos < bbcode.length()) {
  167. int brk_pos = bbcode.find("[", pos);
  168. if (brk_pos < 0) {
  169. brk_pos = bbcode.length();
  170. }
  171. if (brk_pos > pos) {
  172. String text = bbcode.substr(pos, brk_pos - pos);
  173. if (code_tag || tag_stack.size() > 0) {
  174. xml_output.append(text.xml_escape());
  175. } else {
  176. Vector<String> lines = text.split("\n");
  177. for (int i = 0; i < lines.size(); i++) {
  178. if (i != 0) {
  179. xml_output.append("<para>");
  180. }
  181. xml_output.append(lines[i].xml_escape());
  182. if (i != lines.size() - 1) {
  183. xml_output.append("</para>\n");
  184. }
  185. }
  186. }
  187. }
  188. if (brk_pos == bbcode.length()) {
  189. break; // nothing else to add
  190. }
  191. int brk_end = bbcode.find("]", brk_pos + 1);
  192. if (brk_end == -1) {
  193. String text = bbcode.substr(brk_pos, bbcode.length() - brk_pos);
  194. if (code_tag || tag_stack.size() > 0) {
  195. xml_output.append(text.xml_escape());
  196. } else {
  197. Vector<String> lines = text.split("\n");
  198. for (int i = 0; i < lines.size(); i++) {
  199. if (i != 0) {
  200. xml_output.append("<para>");
  201. }
  202. xml_output.append(lines[i].xml_escape());
  203. if (i != lines.size() - 1) {
  204. xml_output.append("</para>\n");
  205. }
  206. }
  207. }
  208. break;
  209. }
  210. String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  211. if (tag.begins_with("/")) {
  212. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1, tag.length());
  213. if (!tag_ok) {
  214. xml_output.append("[");
  215. pos = brk_pos + 1;
  216. continue;
  217. }
  218. tag_stack.pop_front();
  219. pos = brk_end + 1;
  220. code_tag = false;
  221. if (tag == "/url") {
  222. xml_output.append("</a>");
  223. } else if (tag == "/code") {
  224. xml_output.append("</c>");
  225. } else if (tag == "/codeblock") {
  226. xml_output.append("</code>");
  227. }
  228. } else if (code_tag) {
  229. xml_output.append("[");
  230. pos = brk_pos + 1;
  231. } else if (tag.begins_with("method ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ")) {
  232. String link_target = tag.substr(tag.find(" ") + 1, tag.length());
  233. String link_tag = tag.substr(0, tag.find(" "));
  234. Vector<String> link_target_parts = link_target.split(".");
  235. if (link_target_parts.size() <= 0 || link_target_parts.size() > 2) {
  236. ERR_PRINT("Invalid reference format: '" + tag + "'.");
  237. xml_output.append("<c>");
  238. xml_output.append(tag);
  239. xml_output.append("</c>");
  240. pos = brk_end + 1;
  241. continue;
  242. }
  243. const TypeInterface *target_itype;
  244. StringName target_cname;
  245. if (link_target_parts.size() == 2) {
  246. target_itype = _get_type_or_null(TypeReference(link_target_parts[0]));
  247. if (!target_itype) {
  248. target_itype = _get_type_or_null(TypeReference("_" + link_target_parts[0]));
  249. }
  250. target_cname = link_target_parts[1];
  251. } else {
  252. target_itype = p_itype;
  253. target_cname = link_target_parts[0];
  254. }
  255. if (link_tag == "method") {
  256. if (!target_itype || !target_itype->is_object_type) {
  257. if (OS::get_singleton()->is_stdout_verbose()) {
  258. if (target_itype) {
  259. OS::get_singleton()->print("Cannot resolve method reference for non-Godot.Object type in documentation: %s\n", link_target.utf8().get_data());
  260. } else {
  261. OS::get_singleton()->print("Cannot resolve type from method reference in documentation: %s\n", link_target.utf8().get_data());
  262. }
  263. }
  264. // TODO Map what we can
  265. xml_output.append("<c>");
  266. xml_output.append(link_target);
  267. xml_output.append("</c>");
  268. } else {
  269. const MethodInterface *target_imethod = target_itype->find_method_by_name(target_cname);
  270. if (target_imethod) {
  271. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  272. xml_output.append(target_itype->proxy_name);
  273. xml_output.append(".");
  274. xml_output.append(target_imethod->proxy_name);
  275. xml_output.append("\"/>");
  276. }
  277. }
  278. } else if (link_tag == "member") {
  279. if (!target_itype || !target_itype->is_object_type) {
  280. if (OS::get_singleton()->is_stdout_verbose()) {
  281. if (target_itype) {
  282. OS::get_singleton()->print("Cannot resolve member reference for non-Godot.Object type in documentation: %s\n", link_target.utf8().get_data());
  283. } else {
  284. OS::get_singleton()->print("Cannot resolve type from member reference in documentation: %s\n", link_target.utf8().get_data());
  285. }
  286. }
  287. // TODO Map what we can
  288. xml_output.append("<c>");
  289. xml_output.append(link_target);
  290. xml_output.append("</c>");
  291. } else {
  292. const PropertyInterface *target_iprop = target_itype->find_property_by_name(target_cname);
  293. if (target_iprop) {
  294. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  295. xml_output.append(target_itype->proxy_name);
  296. xml_output.append(".");
  297. xml_output.append(target_iprop->proxy_name);
  298. xml_output.append("\"/>");
  299. }
  300. }
  301. } else if (link_tag == "signal") {
  302. // We do not declare signals in any way in C#, so there is nothing to reference
  303. xml_output.append("<c>");
  304. xml_output.append(link_target);
  305. xml_output.append("</c>");
  306. } else if (link_tag == "enum") {
  307. StringName search_cname = !target_itype ? target_cname :
  308. StringName(target_itype->name + "." + (String)target_cname);
  309. const Map<StringName, TypeInterface>::Element *enum_match = enum_types.find(search_cname);
  310. if (!enum_match && search_cname != target_cname) {
  311. enum_match = enum_types.find(target_cname);
  312. }
  313. if (enum_match) {
  314. const TypeInterface &target_enum_itype = enum_match->value();
  315. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  316. xml_output.append(target_enum_itype.proxy_name); // Includes nesting class if any
  317. xml_output.append("\"/>");
  318. } else {
  319. ERR_PRINT("Cannot resolve enum reference in documentation: '" + link_target + "'.");
  320. xml_output.append("<c>");
  321. xml_output.append(link_target);
  322. xml_output.append("</c>");
  323. }
  324. } else if (link_tag == "const") {
  325. if (!target_itype || !target_itype->is_object_type) {
  326. if (OS::get_singleton()->is_stdout_verbose()) {
  327. if (target_itype) {
  328. OS::get_singleton()->print("Cannot resolve constant reference for non-Godot.Object type in documentation: %s\n", link_target.utf8().get_data());
  329. } else {
  330. OS::get_singleton()->print("Cannot resolve type from constant reference in documentation: %s\n", link_target.utf8().get_data());
  331. }
  332. }
  333. // TODO Map what we can
  334. xml_output.append("<c>");
  335. xml_output.append(link_target);
  336. xml_output.append("</c>");
  337. } else if (!target_itype && target_cname == name_cache.type_at_GlobalScope) {
  338. String target_name = (String)target_cname;
  339. // Try to find as a global constant
  340. const ConstantInterface *target_iconst = find_constant_by_name(target_name, global_constants);
  341. if (target_iconst) {
  342. // Found global constant
  343. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE "." BINDINGS_GLOBAL_SCOPE_CLASS ".");
  344. xml_output.append(target_iconst->proxy_name);
  345. xml_output.append("\"/>");
  346. } else {
  347. // Try to find as global enum constant
  348. const EnumInterface *target_ienum = nullptr;
  349. for (const List<EnumInterface>::Element *E = global_enums.front(); E; E = E->next()) {
  350. target_ienum = &E->get();
  351. target_iconst = find_constant_by_name(target_name, target_ienum->constants);
  352. if (target_iconst) {
  353. break;
  354. }
  355. }
  356. if (target_iconst) {
  357. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  358. xml_output.append(target_ienum->cname);
  359. xml_output.append(".");
  360. xml_output.append(target_iconst->proxy_name);
  361. xml_output.append("\"/>");
  362. } else {
  363. ERR_PRINT("Cannot resolve global constant reference in documentation: '" + link_target + "'.");
  364. xml_output.append("<c>");
  365. xml_output.append(link_target);
  366. xml_output.append("</c>");
  367. }
  368. }
  369. } else {
  370. String target_name = (String)target_cname;
  371. // Try to find the constant in the current class
  372. const ConstantInterface *target_iconst = find_constant_by_name(target_name, target_itype->constants);
  373. if (target_iconst) {
  374. // Found constant in current class
  375. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  376. xml_output.append(target_itype->proxy_name);
  377. xml_output.append(".");
  378. xml_output.append(target_iconst->proxy_name);
  379. xml_output.append("\"/>");
  380. } else {
  381. // Try to find as enum constant in the current class
  382. const EnumInterface *target_ienum = nullptr;
  383. for (const List<EnumInterface>::Element *E = target_itype->enums.front(); E; E = E->next()) {
  384. target_ienum = &E->get();
  385. target_iconst = find_constant_by_name(target_name, target_ienum->constants);
  386. if (target_iconst) {
  387. break;
  388. }
  389. }
  390. if (target_iconst) {
  391. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  392. xml_output.append(target_itype->proxy_name);
  393. xml_output.append(".");
  394. xml_output.append(target_ienum->cname);
  395. xml_output.append(".");
  396. xml_output.append(target_iconst->proxy_name);
  397. xml_output.append("\"/>");
  398. } else {
  399. ERR_PRINT("Cannot resolve constant reference in documentation: '" + link_target + "'.");
  400. xml_output.append("<c>");
  401. xml_output.append(link_target);
  402. xml_output.append("</c>");
  403. }
  404. }
  405. }
  406. }
  407. pos = brk_end + 1;
  408. } else if (doc->class_list.has(tag)) {
  409. if (tag == "Array" || tag == "Dictionary") {
  410. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE_COLLECTIONS ".");
  411. xml_output.append(tag);
  412. xml_output.append("\"/>");
  413. } else if (tag == "bool" || tag == "int") {
  414. xml_output.append("<see cref=\"");
  415. xml_output.append(tag);
  416. xml_output.append("\"/>");
  417. } else if (tag == "float") {
  418. xml_output.append("<see cref=\""
  419. #ifdef REAL_T_IS_DOUBLE
  420. "double"
  421. #else
  422. "float"
  423. #endif
  424. "\"/>");
  425. } else if (tag == "Variant") {
  426. // We use System.Object for Variant, so there is no Variant type in C#
  427. xml_output.append("<c>Variant</c>");
  428. } else if (tag == "String") {
  429. xml_output.append("<see cref=\"string\"/>");
  430. } else if (tag == "Nil") {
  431. xml_output.append("<see langword=\"null\"/>");
  432. } else if (tag.begins_with("@")) {
  433. // @GlobalScope, @GDScript, etc
  434. xml_output.append("<c>");
  435. xml_output.append(tag);
  436. xml_output.append("</c>");
  437. } else if (tag == "PackedByteArray") {
  438. xml_output.append("<see cref=\"T:byte[]\"/>");
  439. } else if (tag == "PackedInt32Array") {
  440. xml_output.append("<see cref=\"T:int[]\"/>");
  441. } else if (tag == "PackedInt64Array") {
  442. xml_output.append("<see cref=\"T:long[]\"/>");
  443. } else if (tag == "PackedFloat32Array") {
  444. xml_output.append("<see cref=\"T:float[]\"/>");
  445. } else if (tag == "PackedFloat64Array") {
  446. xml_output.append("<see cref=\"T:double[]\"/>");
  447. } else if (tag == "PackedStringArray") {
  448. xml_output.append("<see cref=\"T:string[]\"/>");
  449. } else if (tag == "PackedVector2Array") {
  450. xml_output.append("<see cref=\"T:" BINDINGS_NAMESPACE ".Vector2[]\"/>");
  451. } else if (tag == "PackedVector3Array") {
  452. xml_output.append("<see cref=\"T:" BINDINGS_NAMESPACE ".Vector3[]\"/>");
  453. } else if (tag == "PackedColorArray") {
  454. xml_output.append("<see cref=\"T:" BINDINGS_NAMESPACE ".Color[]\"/>");
  455. } else {
  456. const TypeInterface *target_itype = _get_type_or_null(TypeReference(tag));
  457. if (!target_itype) {
  458. target_itype = _get_type_or_null(TypeReference("_" + tag));
  459. }
  460. if (target_itype) {
  461. xml_output.append("<see cref=\"" BINDINGS_NAMESPACE ".");
  462. xml_output.append(target_itype->proxy_name);
  463. xml_output.append("\"/>");
  464. } else {
  465. ERR_PRINT("Cannot resolve type reference in documentation: '" + tag + "'.");
  466. xml_output.append("<c>");
  467. xml_output.append(tag);
  468. xml_output.append("</c>");
  469. }
  470. }
  471. pos = brk_end + 1;
  472. } else if (tag == "b") {
  473. // bold is not supported in xml comments
  474. pos = brk_end + 1;
  475. tag_stack.push_front(tag);
  476. } else if (tag == "i") {
  477. // italics is not supported in xml comments
  478. pos = brk_end + 1;
  479. tag_stack.push_front(tag);
  480. } else if (tag == "code") {
  481. xml_output.append("<c>");
  482. code_tag = true;
  483. pos = brk_end + 1;
  484. tag_stack.push_front(tag);
  485. } else if (tag == "codeblock") {
  486. xml_output.append("<code>");
  487. code_tag = true;
  488. pos = brk_end + 1;
  489. tag_stack.push_front(tag);
  490. } else if (tag == "kbd") {
  491. // keyboard combinations are not supported in xml comments
  492. pos = brk_end + 1;
  493. tag_stack.push_front(tag);
  494. } else if (tag == "center") {
  495. // center alignment is not supported in xml comments
  496. pos = brk_end + 1;
  497. tag_stack.push_front(tag);
  498. } else if (tag == "br") {
  499. xml_output.append("\n"); // FIXME: Should use <para> instead. Luckily this tag isn't used for now.
  500. pos = brk_end + 1;
  501. } else if (tag == "u") {
  502. // underline is not supported in xml comments
  503. pos = brk_end + 1;
  504. tag_stack.push_front(tag);
  505. } else if (tag == "s") {
  506. // strikethrough is not supported in xml comments
  507. pos = brk_end + 1;
  508. tag_stack.push_front(tag);
  509. } else if (tag == "url") {
  510. int end = bbcode.find("[", brk_end);
  511. if (end == -1) {
  512. end = bbcode.length();
  513. }
  514. String url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  515. xml_output.append("<a href=\"");
  516. xml_output.append(url);
  517. xml_output.append("\">");
  518. xml_output.append(url);
  519. pos = brk_end + 1;
  520. tag_stack.push_front(tag);
  521. } else if (tag.begins_with("url=")) {
  522. String url = tag.substr(4, tag.length());
  523. xml_output.append("<a href=\"");
  524. xml_output.append(url);
  525. xml_output.append("\">");
  526. pos = brk_end + 1;
  527. tag_stack.push_front("url");
  528. } else if (tag == "img") {
  529. int end = bbcode.find("[", brk_end);
  530. if (end == -1) {
  531. end = bbcode.length();
  532. }
  533. String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
  534. // Not supported. Just append the bbcode.
  535. xml_output.append("[img]");
  536. xml_output.append(image);
  537. xml_output.append("[/img]");
  538. pos = end;
  539. tag_stack.push_front(tag);
  540. } else if (tag.begins_with("color=")) {
  541. // Not supported.
  542. pos = brk_end + 1;
  543. tag_stack.push_front("color");
  544. } else if (tag.begins_with("font=")) {
  545. // Not supported.
  546. pos = brk_end + 1;
  547. tag_stack.push_front("font");
  548. } else {
  549. xml_output.append("["); // ignore
  550. pos = brk_pos + 1;
  551. }
  552. }
  553. xml_output.append("</para>");
  554. return xml_output.as_string();
  555. }
  556. int BindingsGenerator::_determine_enum_prefix(const EnumInterface &p_ienum) {
  557. CRASH_COND(p_ienum.constants.is_empty());
  558. const ConstantInterface &front_iconstant = p_ienum.constants.front()->get();
  559. Vector<String> front_parts = front_iconstant.name.split("_", /* p_allow_empty: */ true);
  560. int candidate_len = front_parts.size() - 1;
  561. if (candidate_len == 0) {
  562. return 0;
  563. }
  564. for (const List<ConstantInterface>::Element *E = p_ienum.constants.front()->next(); E; E = E->next()) {
  565. const ConstantInterface &iconstant = E->get();
  566. Vector<String> parts = iconstant.name.split("_", /* p_allow_empty: */ true);
  567. int i;
  568. for (i = 0; i < candidate_len && i < parts.size(); i++) {
  569. if (front_parts[i] != parts[i]) {
  570. // HARDCODED: Some Flag enums have the prefix 'FLAG_' for everything except 'FLAGS_DEFAULT' (same for 'METHOD_FLAG_' and'METHOD_FLAGS_DEFAULT').
  571. bool hardcoded_exc = (i == candidate_len - 1 && ((front_parts[i] == "FLAGS" && parts[i] == "FLAG") || (front_parts[i] == "FLAG" && parts[i] == "FLAGS")));
  572. if (!hardcoded_exc) {
  573. break;
  574. }
  575. }
  576. }
  577. candidate_len = i;
  578. if (candidate_len == 0) {
  579. return 0;
  580. }
  581. }
  582. return candidate_len;
  583. }
  584. void BindingsGenerator::_apply_prefix_to_enum_constants(BindingsGenerator::EnumInterface &p_ienum, int p_prefix_length) {
  585. if (p_prefix_length > 0) {
  586. for (List<ConstantInterface>::Element *E = p_ienum.constants.front(); E; E = E->next()) {
  587. int curr_prefix_length = p_prefix_length;
  588. ConstantInterface &curr_const = E->get();
  589. String constant_name = curr_const.name;
  590. Vector<String> parts = constant_name.split("_", /* p_allow_empty: */ true);
  591. if (parts.size() <= curr_prefix_length) {
  592. continue;
  593. }
  594. if (parts[curr_prefix_length][0] >= '0' && parts[curr_prefix_length][0] <= '9') {
  595. // The name of enum constants may begin with a numeric digit when strip from the enum prefix,
  596. // so we make the prefix for this constant one word shorter in those cases.
  597. for (curr_prefix_length = curr_prefix_length - 1; curr_prefix_length > 0; curr_prefix_length--) {
  598. if (parts[curr_prefix_length][0] < '0' || parts[curr_prefix_length][0] > '9') {
  599. break;
  600. }
  601. }
  602. }
  603. constant_name = "";
  604. for (int i = curr_prefix_length; i < parts.size(); i++) {
  605. if (i > curr_prefix_length) {
  606. constant_name += "_";
  607. }
  608. constant_name += parts[i];
  609. }
  610. curr_const.proxy_name = snake_to_pascal_case(constant_name, true);
  611. }
  612. }
  613. }
  614. void BindingsGenerator::_generate_method_icalls(const TypeInterface &p_itype) {
  615. for (const List<MethodInterface>::Element *E = p_itype.methods.front(); E; E = E->next()) {
  616. const MethodInterface &imethod = E->get();
  617. if (imethod.is_virtual) {
  618. continue;
  619. }
  620. const TypeInterface *return_type = _get_type_or_placeholder(imethod.return_type);
  621. String im_sig = "IntPtr " CS_PARAM_METHODBIND ", ";
  622. String im_unique_sig = imethod.return_type.cname.operator String() + ",IntPtr,IntPtr";
  623. im_sig += "IntPtr " CS_PARAM_INSTANCE;
  624. // Get arguments information
  625. int i = 0;
  626. for (const List<ArgumentInterface>::Element *F = imethod.arguments.front(); F; F = F->next()) {
  627. const TypeInterface *arg_type = _get_type_or_placeholder(F->get().type);
  628. im_sig += ", ";
  629. im_sig += arg_type->im_type_in;
  630. im_sig += " arg";
  631. im_sig += itos(i + 1);
  632. im_unique_sig += ",";
  633. im_unique_sig += get_unique_sig(*arg_type);
  634. i++;
  635. }
  636. String im_type_out = return_type->im_type_out;
  637. if (return_type->ret_as_byref_arg) {
  638. // Doesn't affect the unique signature
  639. im_type_out = "void";
  640. im_sig += ", ";
  641. im_sig += return_type->im_type_out;
  642. im_sig += " argRet";
  643. i++;
  644. }
  645. // godot_icall_{argc}_{icallcount}
  646. String icall_method = ICALL_PREFIX;
  647. icall_method += itos(imethod.arguments.size());
  648. icall_method += "_";
  649. icall_method += itos(method_icalls.size());
  650. InternalCall im_icall = InternalCall(p_itype.api_type, icall_method, im_type_out, im_sig, im_unique_sig);
  651. List<InternalCall>::Element *match = method_icalls.find(im_icall);
  652. if (match) {
  653. if (p_itype.api_type != ClassDB::API_EDITOR) {
  654. match->get().editor_only = false;
  655. }
  656. method_icalls_map.insert(&E->get(), &match->get());
  657. } else {
  658. List<InternalCall>::Element *added = method_icalls.push_back(im_icall);
  659. method_icalls_map.insert(&E->get(), &added->get());
  660. }
  661. }
  662. }
  663. void BindingsGenerator::_generate_array_extensions(StringBuilder &p_output) {
  664. p_output.append("using System;\n\n");
  665. p_output.append("namespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK);
  666. // The class where we put the extensions doesn't matter, so just use "GD".
  667. p_output.append(INDENT1 "public static partial class " BINDINGS_GLOBAL_SCOPE_CLASS "\n" INDENT1 "{");
  668. #define ARRAY_IS_EMPTY(m_type) \
  669. p_output.append("\n" INDENT2 "/// <summary>\n"); \
  670. p_output.append(INDENT2 "/// Returns true if this " #m_type " array is empty or doesn't exist.\n"); \
  671. p_output.append(INDENT2 "/// </summary>\n"); \
  672. p_output.append(INDENT2 "/// <param name=\"instance\">The " #m_type " array check.</param>\n"); \
  673. p_output.append(INDENT2 "/// <returns>Whether or not the array is empty.</returns>\n"); \
  674. p_output.append(INDENT2 "public static bool IsEmpty(this " #m_type "[] instance)\n"); \
  675. p_output.append(INDENT2 OPEN_BLOCK); \
  676. p_output.append(INDENT3 "return instance == null || instance.Length == 0;\n"); \
  677. p_output.append(INDENT2 CLOSE_BLOCK);
  678. #define ARRAY_JOIN(m_type) \
  679. p_output.append("\n" INDENT2 "/// <summary>\n"); \
  680. p_output.append(INDENT2 "/// Converts this " #m_type " array to a string delimited by the given string.\n"); \
  681. p_output.append(INDENT2 "/// </summary>\n"); \
  682. p_output.append(INDENT2 "/// <param name=\"instance\">The " #m_type " array to convert.</param>\n"); \
  683. p_output.append(INDENT2 "/// <param name=\"delimiter\">The delimiter to use between items.</param>\n"); \
  684. p_output.append(INDENT2 "/// <returns>A single string with all items.</returns>\n"); \
  685. p_output.append(INDENT2 "public static string Join(this " #m_type "[] instance, string delimiter = \", \")\n"); \
  686. p_output.append(INDENT2 OPEN_BLOCK); \
  687. p_output.append(INDENT3 "return String.Join(delimiter, instance);\n"); \
  688. p_output.append(INDENT2 CLOSE_BLOCK);
  689. #define ARRAY_STRINGIFY(m_type) \
  690. p_output.append("\n" INDENT2 "/// <summary>\n"); \
  691. p_output.append(INDENT2 "/// Converts this " #m_type " array to a string with brackets.\n"); \
  692. p_output.append(INDENT2 "/// </summary>\n"); \
  693. p_output.append(INDENT2 "/// <param name=\"instance\">The " #m_type " array to convert.</param>\n"); \
  694. p_output.append(INDENT2 "/// <returns>A single string with all items.</returns>\n"); \
  695. p_output.append(INDENT2 "public static string Stringify(this " #m_type "[] instance)\n"); \
  696. p_output.append(INDENT2 OPEN_BLOCK); \
  697. p_output.append(INDENT3 "return \"[\" + instance.Join() + \"]\";\n"); \
  698. p_output.append(INDENT2 CLOSE_BLOCK);
  699. #define ARRAY_ALL(m_type) \
  700. ARRAY_IS_EMPTY(m_type) \
  701. ARRAY_JOIN(m_type) \
  702. ARRAY_STRINGIFY(m_type)
  703. ARRAY_ALL(byte);
  704. ARRAY_ALL(int);
  705. ARRAY_ALL(long);
  706. ARRAY_ALL(float);
  707. ARRAY_ALL(double);
  708. ARRAY_ALL(string);
  709. ARRAY_ALL(Color);
  710. ARRAY_ALL(Vector2);
  711. ARRAY_ALL(Vector2i);
  712. ARRAY_ALL(Vector3);
  713. ARRAY_ALL(Vector3i);
  714. #undef ARRAY_ALL
  715. #undef ARRAY_IS_EMPTY
  716. #undef ARRAY_JOIN
  717. #undef ARRAY_STRINGIFY
  718. p_output.append(INDENT1 CLOSE_BLOCK); // End of GD class.
  719. p_output.append(CLOSE_BLOCK); // End of namespace.
  720. }
  721. void BindingsGenerator::_generate_global_constants(StringBuilder &p_output) {
  722. // Constants (in partial GD class)
  723. p_output.append("\n#pragma warning disable CS1591 // Disable warning: "
  724. "'Missing XML comment for publicly visible type or member'\n");
  725. p_output.append("namespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK);
  726. p_output.append(INDENT1 "public static partial class " BINDINGS_GLOBAL_SCOPE_CLASS "\n" INDENT1 "{");
  727. for (const List<ConstantInterface>::Element *E = global_constants.front(); E; E = E->next()) {
  728. const ConstantInterface &iconstant = E->get();
  729. if (iconstant.const_doc && iconstant.const_doc->description.size()) {
  730. String xml_summary = bbcode_to_xml(fix_doc_description(iconstant.const_doc->description), nullptr);
  731. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  732. if (summary_lines.size()) {
  733. p_output.append(MEMBER_BEGIN "/// <summary>\n");
  734. for (int i = 0; i < summary_lines.size(); i++) {
  735. p_output.append(INDENT2 "/// ");
  736. p_output.append(summary_lines[i]);
  737. p_output.append("\n");
  738. }
  739. p_output.append(INDENT2 "/// </summary>");
  740. }
  741. }
  742. p_output.append(MEMBER_BEGIN "public const int ");
  743. p_output.append(iconstant.proxy_name);
  744. p_output.append(" = ");
  745. p_output.append(itos(iconstant.value));
  746. p_output.append(";");
  747. }
  748. if (!global_constants.is_empty()) {
  749. p_output.append("\n");
  750. }
  751. p_output.append(INDENT1 CLOSE_BLOCK); // end of GD class
  752. // Enums
  753. for (List<EnumInterface>::Element *E = global_enums.front(); E; E = E->next()) {
  754. const EnumInterface &ienum = E->get();
  755. CRASH_COND(ienum.constants.is_empty());
  756. String enum_proxy_name = ienum.cname.operator String();
  757. bool enum_in_static_class = false;
  758. if (enum_proxy_name.find(".") > 0) {
  759. enum_in_static_class = true;
  760. String enum_class_name = enum_proxy_name.get_slicec('.', 0);
  761. enum_proxy_name = enum_proxy_name.get_slicec('.', 1);
  762. CRASH_COND(enum_class_name != "Variant"); // Hard-coded...
  763. _log("Declaring global enum '%s' inside static class '%s'\n", enum_proxy_name.utf8().get_data(), enum_class_name.utf8().get_data());
  764. p_output.append("\n" INDENT1 "public static partial class ");
  765. p_output.append(enum_class_name);
  766. p_output.append("\n" INDENT1 OPEN_BLOCK);
  767. }
  768. p_output.append("\n" INDENT1 "public enum ");
  769. p_output.append(enum_proxy_name);
  770. p_output.append("\n" INDENT1 OPEN_BLOCK);
  771. for (const List<ConstantInterface>::Element *F = ienum.constants.front(); F; F = F->next()) {
  772. const ConstantInterface &iconstant = F->get();
  773. if (iconstant.const_doc && iconstant.const_doc->description.size()) {
  774. String xml_summary = bbcode_to_xml(fix_doc_description(iconstant.const_doc->description), nullptr);
  775. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  776. if (summary_lines.size()) {
  777. p_output.append(INDENT2 "/// <summary>\n");
  778. for (int i = 0; i < summary_lines.size(); i++) {
  779. p_output.append(INDENT2 "/// ");
  780. p_output.append(summary_lines[i]);
  781. p_output.append("\n");
  782. }
  783. p_output.append(INDENT2 "/// </summary>\n");
  784. }
  785. }
  786. p_output.append(INDENT2);
  787. p_output.append(iconstant.proxy_name);
  788. p_output.append(" = ");
  789. p_output.append(itos(iconstant.value));
  790. p_output.append(F != ienum.constants.back() ? ",\n" : "\n");
  791. }
  792. p_output.append(INDENT1 CLOSE_BLOCK);
  793. if (enum_in_static_class) {
  794. p_output.append(INDENT1 CLOSE_BLOCK);
  795. }
  796. }
  797. p_output.append(CLOSE_BLOCK); // end of namespace
  798. p_output.append("\n#pragma warning restore CS1591\n");
  799. }
  800. Error BindingsGenerator::generate_cs_core_project(const String &p_proj_dir) {
  801. ERR_FAIL_COND_V(!initialized, ERR_UNCONFIGURED);
  802. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  803. ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
  804. if (!DirAccess::exists(p_proj_dir)) {
  805. Error err = da->make_dir_recursive(p_proj_dir);
  806. ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Cannot create directory '" + p_proj_dir + "'.");
  807. }
  808. da->change_dir(p_proj_dir);
  809. da->make_dir("Generated");
  810. da->make_dir("Generated/GodotObjects");
  811. String base_gen_dir = path::join(p_proj_dir, "Generated");
  812. String godot_objects_gen_dir = path::join(base_gen_dir, "GodotObjects");
  813. Vector<String> compile_items;
  814. // Generate source file for global scope constants and enums
  815. {
  816. StringBuilder constants_source;
  817. _generate_global_constants(constants_source);
  818. String output_file = path::join(base_gen_dir, BINDINGS_GLOBAL_SCOPE_CLASS "_constants.cs");
  819. Error save_err = _save_file(output_file, constants_source);
  820. if (save_err != OK) {
  821. return save_err;
  822. }
  823. compile_items.push_back(output_file);
  824. }
  825. // Generate source file for array extensions
  826. {
  827. StringBuilder extensions_source;
  828. _generate_array_extensions(extensions_source);
  829. String output_file = path::join(base_gen_dir, BINDINGS_GLOBAL_SCOPE_CLASS "_extensions.cs");
  830. Error save_err = _save_file(output_file, extensions_source);
  831. if (save_err != OK) {
  832. return save_err;
  833. }
  834. compile_items.push_back(output_file);
  835. }
  836. for (OrderedHashMap<StringName, TypeInterface>::Element E = obj_types.front(); E; E = E.next()) {
  837. const TypeInterface &itype = E.get();
  838. if (itype.api_type == ClassDB::API_EDITOR) {
  839. continue;
  840. }
  841. String output_file = path::join(godot_objects_gen_dir, itype.proxy_name + ".cs");
  842. Error err = _generate_cs_type(itype, output_file);
  843. if (err == ERR_SKIP) {
  844. continue;
  845. }
  846. if (err != OK) {
  847. return err;
  848. }
  849. compile_items.push_back(output_file);
  850. }
  851. // Generate sources from compressed files
  852. StringBuilder cs_icalls_content;
  853. cs_icalls_content.append("using System;\n"
  854. "using System.Runtime.CompilerServices;\n"
  855. "\n");
  856. cs_icalls_content.append("namespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK);
  857. cs_icalls_content.append(INDENT1 "internal static class " BINDINGS_CLASS_NATIVECALLS "\n" INDENT1 "{");
  858. cs_icalls_content.append(MEMBER_BEGIN "internal static ulong godot_api_hash = ");
  859. cs_icalls_content.append(String::num_uint64(GDMono::get_singleton()->get_api_core_hash()) + ";\n");
  860. cs_icalls_content.append(MEMBER_BEGIN "internal static uint bindings_version = ");
  861. cs_icalls_content.append(String::num_uint64(BINDINGS_GENERATOR_VERSION) + ";\n");
  862. cs_icalls_content.append(MEMBER_BEGIN "internal static uint cs_glue_version = ");
  863. cs_icalls_content.append(String::num_uint64(CS_GLUE_VERSION) + ";\n");
  864. #define ADD_INTERNAL_CALL(m_icall) \
  865. if (!m_icall.editor_only) { \
  866. cs_icalls_content.append(MEMBER_BEGIN "[MethodImpl(MethodImplOptions.InternalCall)]\n"); \
  867. cs_icalls_content.append(INDENT2 "internal static extern "); \
  868. cs_icalls_content.append(m_icall.im_type_out + " "); \
  869. cs_icalls_content.append(m_icall.name + "("); \
  870. cs_icalls_content.append(m_icall.im_sig + ");\n"); \
  871. }
  872. for (const List<InternalCall>::Element *E = core_custom_icalls.front(); E; E = E->next()) {
  873. ADD_INTERNAL_CALL(E->get());
  874. }
  875. for (const List<InternalCall>::Element *E = method_icalls.front(); E; E = E->next()) {
  876. ADD_INTERNAL_CALL(E->get());
  877. }
  878. #undef ADD_INTERNAL_CALL
  879. cs_icalls_content.append(INDENT1 CLOSE_BLOCK CLOSE_BLOCK);
  880. String internal_methods_file = path::join(base_gen_dir, BINDINGS_CLASS_NATIVECALLS ".cs");
  881. Error err = _save_file(internal_methods_file, cs_icalls_content);
  882. if (err != OK) {
  883. return err;
  884. }
  885. compile_items.push_back(internal_methods_file);
  886. StringBuilder includes_props_content;
  887. includes_props_content.append("<Project>\n"
  888. " <ItemGroup>\n");
  889. for (int i = 0; i < compile_items.size(); i++) {
  890. String include = path::relative_to(compile_items[i], p_proj_dir).replace("/", "\\");
  891. includes_props_content.append(" <Compile Include=\"" + include + "\" />\n");
  892. }
  893. includes_props_content.append(" </ItemGroup>\n"
  894. "</Project>\n");
  895. String includes_props_file = path::join(base_gen_dir, "GeneratedIncludes.props");
  896. err = _save_file(includes_props_file, includes_props_content);
  897. if (err != OK) {
  898. return err;
  899. }
  900. return OK;
  901. }
  902. Error BindingsGenerator::generate_cs_editor_project(const String &p_proj_dir) {
  903. ERR_FAIL_COND_V(!initialized, ERR_UNCONFIGURED);
  904. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  905. ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
  906. if (!DirAccess::exists(p_proj_dir)) {
  907. Error err = da->make_dir_recursive(p_proj_dir);
  908. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  909. }
  910. da->change_dir(p_proj_dir);
  911. da->make_dir("Generated");
  912. da->make_dir("Generated/GodotObjects");
  913. String base_gen_dir = path::join(p_proj_dir, "Generated");
  914. String godot_objects_gen_dir = path::join(base_gen_dir, "GodotObjects");
  915. Vector<String> compile_items;
  916. for (OrderedHashMap<StringName, TypeInterface>::Element E = obj_types.front(); E; E = E.next()) {
  917. const TypeInterface &itype = E.get();
  918. if (itype.api_type != ClassDB::API_EDITOR) {
  919. continue;
  920. }
  921. String output_file = path::join(godot_objects_gen_dir, itype.proxy_name + ".cs");
  922. Error err = _generate_cs_type(itype, output_file);
  923. if (err == ERR_SKIP) {
  924. continue;
  925. }
  926. if (err != OK) {
  927. return err;
  928. }
  929. compile_items.push_back(output_file);
  930. }
  931. StringBuilder cs_icalls_content;
  932. cs_icalls_content.append("using System;\n"
  933. "using System.Runtime.CompilerServices;\n"
  934. "\n");
  935. cs_icalls_content.append("namespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK);
  936. cs_icalls_content.append(INDENT1 "internal static class " BINDINGS_CLASS_NATIVECALLS_EDITOR "\n" INDENT1 OPEN_BLOCK);
  937. cs_icalls_content.append(INDENT2 "internal static ulong godot_api_hash = ");
  938. cs_icalls_content.append(String::num_uint64(GDMono::get_singleton()->get_api_editor_hash()) + ";\n");
  939. cs_icalls_content.append(INDENT2 "internal static uint bindings_version = ");
  940. cs_icalls_content.append(String::num_uint64(BINDINGS_GENERATOR_VERSION) + ";\n");
  941. cs_icalls_content.append(INDENT2 "internal static uint cs_glue_version = ");
  942. cs_icalls_content.append(String::num_uint64(CS_GLUE_VERSION) + ";\n");
  943. cs_icalls_content.append("\n");
  944. #define ADD_INTERNAL_CALL(m_icall) \
  945. if (m_icall.editor_only) { \
  946. cs_icalls_content.append(INDENT2 "[MethodImpl(MethodImplOptions.InternalCall)]\n"); \
  947. cs_icalls_content.append(INDENT2 "internal static extern "); \
  948. cs_icalls_content.append(m_icall.im_type_out + " "); \
  949. cs_icalls_content.append(m_icall.name + "("); \
  950. cs_icalls_content.append(m_icall.im_sig + ");\n"); \
  951. }
  952. for (const List<InternalCall>::Element *E = editor_custom_icalls.front(); E; E = E->next()) {
  953. ADD_INTERNAL_CALL(E->get());
  954. }
  955. for (const List<InternalCall>::Element *E = method_icalls.front(); E; E = E->next()) {
  956. ADD_INTERNAL_CALL(E->get());
  957. }
  958. #undef ADD_INTERNAL_CALL
  959. cs_icalls_content.append(INDENT1 CLOSE_BLOCK CLOSE_BLOCK);
  960. String internal_methods_file = path::join(base_gen_dir, BINDINGS_CLASS_NATIVECALLS_EDITOR ".cs");
  961. Error err = _save_file(internal_methods_file, cs_icalls_content);
  962. if (err != OK) {
  963. return err;
  964. }
  965. compile_items.push_back(internal_methods_file);
  966. StringBuilder includes_props_content;
  967. includes_props_content.append("<Project>\n"
  968. " <ItemGroup>\n");
  969. for (int i = 0; i < compile_items.size(); i++) {
  970. String include = path::relative_to(compile_items[i], p_proj_dir).replace("/", "\\");
  971. includes_props_content.append(" <Compile Include=\"" + include + "\" />\n");
  972. }
  973. includes_props_content.append(" </ItemGroup>\n"
  974. "</Project>\n");
  975. String includes_props_file = path::join(base_gen_dir, "GeneratedIncludes.props");
  976. err = _save_file(includes_props_file, includes_props_content);
  977. if (err != OK) {
  978. return err;
  979. }
  980. return OK;
  981. }
  982. Error BindingsGenerator::generate_cs_api(const String &p_output_dir) {
  983. ERR_FAIL_COND_V(!initialized, ERR_UNCONFIGURED);
  984. String output_dir = path::abspath(path::realpath(p_output_dir));
  985. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  986. ERR_FAIL_COND_V(!da, ERR_CANT_CREATE);
  987. if (!DirAccess::exists(output_dir)) {
  988. Error err = da->make_dir_recursive(output_dir);
  989. ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE);
  990. }
  991. Error proj_err;
  992. // Generate GodotSharp source files
  993. String core_proj_dir = output_dir.plus_file(CORE_API_ASSEMBLY_NAME);
  994. proj_err = generate_cs_core_project(core_proj_dir);
  995. if (proj_err != OK) {
  996. ERR_PRINT("Generation of the Core API C# project failed.");
  997. return proj_err;
  998. }
  999. // Generate GodotSharpEditor source files
  1000. String editor_proj_dir = output_dir.plus_file(EDITOR_API_ASSEMBLY_NAME);
  1001. proj_err = generate_cs_editor_project(editor_proj_dir);
  1002. if (proj_err != OK) {
  1003. ERR_PRINT("Generation of the Editor API C# project failed.");
  1004. return proj_err;
  1005. }
  1006. _log("The Godot API sources were successfully generated\n");
  1007. return OK;
  1008. }
  1009. // FIXME: There are some members that hide other inherited members.
  1010. // - In the case of both members being the same kind, the new one must be declared
  1011. // explicitly as 'new' to avoid the warning (and we must print a message about it).
  1012. // - In the case of both members being of a different kind, then the new one must
  1013. // be renamed to avoid the name collision (and we must print a warning about it).
  1014. // - Csc warning e.g.:
  1015. // ObjectType/LineEdit.cs(140,38): warning CS0108: 'LineEdit.FocusMode' hides inherited member 'Control.FocusMode'. Use the new keyword if hiding was intended.
  1016. Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const String &p_output_file) {
  1017. CRASH_COND(!itype.is_object_type);
  1018. bool is_derived_type = itype.base_name != StringName();
  1019. if (!is_derived_type) {
  1020. // Some Godot.Object assertions
  1021. CRASH_COND(itype.cname != name_cache.type_Object);
  1022. CRASH_COND(!itype.is_instantiable);
  1023. CRASH_COND(itype.api_type != ClassDB::API_CORE);
  1024. CRASH_COND(itype.is_reference);
  1025. CRASH_COND(itype.is_singleton);
  1026. }
  1027. List<InternalCall> &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls;
  1028. _log("Generating %s.cs...\n", itype.proxy_name.utf8().get_data());
  1029. String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); // Used only for derived types
  1030. StringBuilder output;
  1031. output.append("using System;\n"); // IntPtr
  1032. output.append("using System.Diagnostics;\n"); // DebuggerBrowsable
  1033. output.append("\n"
  1034. "#pragma warning disable CS1591 // Disable warning: "
  1035. "'Missing XML comment for publicly visible type or member'\n"
  1036. "#pragma warning disable CS1573 // Disable warning: "
  1037. "'Parameter has no matching param tag in the XML comment'\n");
  1038. output.append("\nnamespace " BINDINGS_NAMESPACE "\n" OPEN_BLOCK);
  1039. const DocData::ClassDoc *class_doc = itype.class_doc;
  1040. if (class_doc && class_doc->description.size()) {
  1041. String xml_summary = bbcode_to_xml(fix_doc_description(class_doc->description), &itype);
  1042. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  1043. if (summary_lines.size()) {
  1044. output.append(INDENT1 "/// <summary>\n");
  1045. for (int i = 0; i < summary_lines.size(); i++) {
  1046. output.append(INDENT1 "/// ");
  1047. output.append(summary_lines[i]);
  1048. output.append("\n");
  1049. }
  1050. output.append(INDENT1 "/// </summary>\n");
  1051. }
  1052. }
  1053. output.append(INDENT1 "public ");
  1054. if (itype.is_singleton) {
  1055. output.append("static partial class ");
  1056. } else {
  1057. output.append(itype.is_instantiable ? "partial class " : "abstract partial class ");
  1058. }
  1059. output.append(itype.proxy_name);
  1060. if (itype.is_singleton) {
  1061. output.append("\n");
  1062. } else if (is_derived_type) {
  1063. if (obj_types.has(itype.base_name)) {
  1064. output.append(" : ");
  1065. output.append(obj_types[itype.base_name].proxy_name);
  1066. output.append("\n");
  1067. } else {
  1068. ERR_PRINT("Base type '" + itype.base_name.operator String() + "' does not exist, for class '" + itype.name + "'.");
  1069. return ERR_INVALID_DATA;
  1070. }
  1071. }
  1072. output.append(INDENT1 "{");
  1073. // Add constants
  1074. for (const List<ConstantInterface>::Element *E = itype.constants.front(); E; E = E->next()) {
  1075. const ConstantInterface &iconstant = E->get();
  1076. if (iconstant.const_doc && iconstant.const_doc->description.size()) {
  1077. String xml_summary = bbcode_to_xml(fix_doc_description(iconstant.const_doc->description), &itype);
  1078. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  1079. if (summary_lines.size()) {
  1080. output.append(MEMBER_BEGIN "/// <summary>\n");
  1081. for (int i = 0; i < summary_lines.size(); i++) {
  1082. output.append(INDENT2 "/// ");
  1083. output.append(summary_lines[i]);
  1084. output.append("\n");
  1085. }
  1086. output.append(INDENT2 "/// </summary>");
  1087. }
  1088. }
  1089. output.append(MEMBER_BEGIN "public const int ");
  1090. output.append(iconstant.proxy_name);
  1091. output.append(" = ");
  1092. output.append(itos(iconstant.value));
  1093. output.append(";");
  1094. }
  1095. if (itype.constants.size()) {
  1096. output.append("\n");
  1097. }
  1098. // Add enums
  1099. for (const List<EnumInterface>::Element *E = itype.enums.front(); E; E = E->next()) {
  1100. const EnumInterface &ienum = E->get();
  1101. ERR_FAIL_COND_V(ienum.constants.is_empty(), ERR_BUG);
  1102. output.append(MEMBER_BEGIN "public enum ");
  1103. output.append(ienum.cname.operator String());
  1104. output.append(MEMBER_BEGIN OPEN_BLOCK);
  1105. for (const List<ConstantInterface>::Element *F = ienum.constants.front(); F; F = F->next()) {
  1106. const ConstantInterface &iconstant = F->get();
  1107. if (iconstant.const_doc && iconstant.const_doc->description.size()) {
  1108. String xml_summary = bbcode_to_xml(fix_doc_description(iconstant.const_doc->description), &itype);
  1109. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  1110. if (summary_lines.size()) {
  1111. output.append(INDENT3 "/// <summary>\n");
  1112. for (int i = 0; i < summary_lines.size(); i++) {
  1113. output.append(INDENT3 "/// ");
  1114. output.append(summary_lines[i]);
  1115. output.append("\n");
  1116. }
  1117. output.append(INDENT3 "/// </summary>\n");
  1118. }
  1119. }
  1120. output.append(INDENT3);
  1121. output.append(iconstant.proxy_name);
  1122. output.append(" = ");
  1123. output.append(itos(iconstant.value));
  1124. output.append(F != ienum.constants.back() ? ",\n" : "\n");
  1125. }
  1126. output.append(INDENT2 CLOSE_BLOCK);
  1127. }
  1128. // Add properties
  1129. for (const List<PropertyInterface>::Element *E = itype.properties.front(); E; E = E->next()) {
  1130. const PropertyInterface &iprop = E->get();
  1131. Error prop_err = _generate_cs_property(itype, iprop, output);
  1132. ERR_FAIL_COND_V_MSG(prop_err != OK, prop_err,
  1133. "Failed to generate property '" + iprop.cname.operator String() +
  1134. "' for class '" + itype.name + "'.");
  1135. }
  1136. if (itype.is_singleton) {
  1137. // Add the type name and the singleton pointer as static fields
  1138. output.append(MEMBER_BEGIN "private static Godot.Object singleton;\n");
  1139. output.append(MEMBER_BEGIN "public static Godot.Object Singleton\n" INDENT2 "{\n" INDENT3
  1140. "get\n" INDENT3 "{\n" INDENT4 "if (singleton == null)\n" INDENT5
  1141. "singleton = Engine.GetSingleton(typeof(");
  1142. output.append(itype.proxy_name);
  1143. output.append(").Name);\n" INDENT4 "return singleton;\n" INDENT3 "}\n" INDENT2 "}\n");
  1144. output.append(MEMBER_BEGIN "private static StringName " BINDINGS_NATIVE_NAME_FIELD " = \"");
  1145. output.append(itype.name);
  1146. output.append("\";\n");
  1147. output.append(INDENT2 "internal static IntPtr " BINDINGS_PTR_FIELD " = ");
  1148. output.append(itype.api_type == ClassDB::API_EDITOR ? BINDINGS_CLASS_NATIVECALLS_EDITOR : BINDINGS_CLASS_NATIVECALLS);
  1149. output.append("." ICALL_PREFIX);
  1150. output.append(itype.name);
  1151. output.append(SINGLETON_ICALL_SUFFIX "();\n");
  1152. } else if (is_derived_type) {
  1153. // Add member fields
  1154. output.append(MEMBER_BEGIN "private static StringName " BINDINGS_NATIVE_NAME_FIELD " = \"");
  1155. output.append(itype.name);
  1156. output.append("\";\n");
  1157. // Add default constructor
  1158. if (itype.is_instantiable) {
  1159. output.append(MEMBER_BEGIN "public ");
  1160. output.append(itype.proxy_name);
  1161. output.append("() : this(");
  1162. output.append(itype.memory_own ? "true" : "false");
  1163. // The default constructor may also be called by the engine when instancing existing native objects
  1164. // The engine will initialize the pointer field of the managed side before calling the constructor
  1165. // This is why we only allocate a new native object from the constructor if the pointer field is not set
  1166. output.append(")\n" OPEN_BLOCK_L2 "if (" BINDINGS_PTR_FIELD " == IntPtr.Zero)\n" INDENT4 BINDINGS_PTR_FIELD " = ");
  1167. output.append(itype.api_type == ClassDB::API_EDITOR ? BINDINGS_CLASS_NATIVECALLS_EDITOR : BINDINGS_CLASS_NATIVECALLS);
  1168. output.append("." + ctor_method);
  1169. output.append("(this);\n" INDENT3 "_InitializeGodotScriptInstanceInternals();\n" CLOSE_BLOCK_L2);
  1170. } else {
  1171. // Hide the constructor
  1172. output.append(MEMBER_BEGIN "internal ");
  1173. output.append(itype.proxy_name);
  1174. output.append("() {}\n");
  1175. }
  1176. // Add.. em.. trick constructor. Sort of.
  1177. output.append(MEMBER_BEGIN "internal ");
  1178. output.append(itype.proxy_name);
  1179. output.append("(bool " CS_FIELD_MEMORYOWN ") : base(" CS_FIELD_MEMORYOWN ") {}\n");
  1180. }
  1181. int method_bind_count = 0;
  1182. for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) {
  1183. const MethodInterface &imethod = E->get();
  1184. Error method_err = _generate_cs_method(itype, imethod, method_bind_count, output);
  1185. ERR_FAIL_COND_V_MSG(method_err != OK, method_err,
  1186. "Failed to generate method '" + imethod.name + "' for class '" + itype.name + "'.");
  1187. }
  1188. for (const List<SignalInterface>::Element *E = itype.signals_.front(); E; E = E->next()) {
  1189. const SignalInterface &isignal = E->get();
  1190. Error method_err = _generate_cs_signal(itype, isignal, output);
  1191. ERR_FAIL_COND_V_MSG(method_err != OK, method_err,
  1192. "Failed to generate signal '" + isignal.name + "' for class '" + itype.name + "'.");
  1193. }
  1194. if (itype.is_singleton) {
  1195. InternalCall singleton_icall = InternalCall(itype.api_type, ICALL_PREFIX + itype.name + SINGLETON_ICALL_SUFFIX, "IntPtr");
  1196. if (!find_icall_by_name(singleton_icall.name, custom_icalls)) {
  1197. custom_icalls.push_back(singleton_icall);
  1198. }
  1199. }
  1200. if (is_derived_type && itype.is_instantiable) {
  1201. InternalCall ctor_icall = InternalCall(itype.api_type, ctor_method, "IntPtr", itype.proxy_name + " obj");
  1202. if (!find_icall_by_name(ctor_icall.name, custom_icalls)) {
  1203. custom_icalls.push_back(ctor_icall);
  1204. }
  1205. }
  1206. output.append(INDENT1 CLOSE_BLOCK /* class */
  1207. CLOSE_BLOCK /* namespace */);
  1208. output.append("\n"
  1209. "#pragma warning restore CS1591\n"
  1210. "#pragma warning restore CS1573\n");
  1211. return _save_file(p_output_file, output);
  1212. }
  1213. Error BindingsGenerator::_generate_cs_property(const BindingsGenerator::TypeInterface &p_itype, const PropertyInterface &p_iprop, StringBuilder &p_output) {
  1214. const MethodInterface *setter = p_itype.find_method_by_name(p_iprop.setter);
  1215. // Search it in base types too
  1216. const TypeInterface *current_type = &p_itype;
  1217. while (!setter && current_type->base_name != StringName()) {
  1218. OrderedHashMap<StringName, TypeInterface>::Element base_match = obj_types.find(current_type->base_name);
  1219. ERR_FAIL_COND_V_MSG(!base_match, ERR_BUG, "Type not found '" + current_type->base_name + "'. Inherited by '" + current_type->name + "'.");
  1220. current_type = &base_match.get();
  1221. setter = current_type->find_method_by_name(p_iprop.setter);
  1222. }
  1223. const MethodInterface *getter = p_itype.find_method_by_name(p_iprop.getter);
  1224. // Search it in base types too
  1225. current_type = &p_itype;
  1226. while (!getter && current_type->base_name != StringName()) {
  1227. OrderedHashMap<StringName, TypeInterface>::Element base_match = obj_types.find(current_type->base_name);
  1228. ERR_FAIL_COND_V_MSG(!base_match, ERR_BUG, "Type not found '" + current_type->base_name + "'. Inherited by '" + current_type->name + "'.");
  1229. current_type = &base_match.get();
  1230. getter = current_type->find_method_by_name(p_iprop.getter);
  1231. }
  1232. ERR_FAIL_COND_V(!setter && !getter, ERR_BUG);
  1233. if (setter) {
  1234. int setter_argc = p_iprop.index != -1 ? 2 : 1;
  1235. ERR_FAIL_COND_V(setter->arguments.size() != setter_argc, ERR_BUG);
  1236. }
  1237. if (getter) {
  1238. int getter_argc = p_iprop.index != -1 ? 1 : 0;
  1239. ERR_FAIL_COND_V(getter->arguments.size() != getter_argc, ERR_BUG);
  1240. }
  1241. if (getter && setter) {
  1242. const ArgumentInterface &setter_first_arg = setter->arguments.back()->get();
  1243. if (getter->return_type.cname != setter_first_arg.type.cname) {
  1244. // Special case for Node::set_name
  1245. bool whitelisted = getter->return_type.cname == name_cache.type_StringName &&
  1246. setter_first_arg.type.cname == name_cache.type_String;
  1247. ERR_FAIL_COND_V_MSG(!whitelisted, ERR_BUG,
  1248. "Return type from getter doesn't match first argument of setter for property: '" +
  1249. p_itype.name + "." + String(p_iprop.cname) + "'.");
  1250. }
  1251. }
  1252. const TypeReference &proptype_name = getter ? getter->return_type : setter->arguments.back()->get().type;
  1253. const TypeInterface *prop_itype = _get_type_or_null(proptype_name);
  1254. ERR_FAIL_NULL_V(prop_itype, ERR_BUG); // Property type not found
  1255. ERR_FAIL_COND_V_MSG(prop_itype->is_singleton, ERR_BUG,
  1256. "Property type is a singleton: '" + p_itype.name + "." + String(p_iprop.cname) + "'.");
  1257. if (p_itype.api_type == ClassDB::API_CORE) {
  1258. ERR_FAIL_COND_V_MSG(prop_itype->api_type == ClassDB::API_EDITOR, ERR_BUG,
  1259. "Property '" + p_itype.name + "." + String(p_iprop.cname) + "' has type '" + prop_itype->name +
  1260. "' from the editor API. Core API cannot have dependencies on the editor API.");
  1261. }
  1262. if (p_iprop.prop_doc && p_iprop.prop_doc->description.size()) {
  1263. String xml_summary = bbcode_to_xml(fix_doc_description(p_iprop.prop_doc->description), &p_itype);
  1264. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  1265. if (summary_lines.size()) {
  1266. p_output.append(MEMBER_BEGIN "/// <summary>\n");
  1267. for (int i = 0; i < summary_lines.size(); i++) {
  1268. p_output.append(INDENT2 "/// ");
  1269. p_output.append(summary_lines[i]);
  1270. p_output.append("\n");
  1271. }
  1272. p_output.append(INDENT2 "/// </summary>");
  1273. }
  1274. }
  1275. p_output.append(MEMBER_BEGIN "public ");
  1276. if (p_itype.is_singleton) {
  1277. p_output.append("static ");
  1278. }
  1279. p_output.append(prop_itype->cs_type);
  1280. p_output.append(" ");
  1281. p_output.append(p_iprop.proxy_name);
  1282. p_output.append("\n" INDENT2 OPEN_BLOCK);
  1283. if (getter) {
  1284. p_output.append(INDENT3 "get\n"
  1285. // TODO Remove this once we make accessor methods private/internal (they will no longer be marked as obsolete after that)
  1286. "#pragma warning disable CS0618 // Disable warning about obsolete method\n"
  1287. OPEN_BLOCK_L3);
  1288. p_output.append("return ");
  1289. p_output.append(getter->proxy_name + "(");
  1290. if (p_iprop.index != -1) {
  1291. const ArgumentInterface &idx_arg = getter->arguments.front()->get();
  1292. if (idx_arg.type.cname != name_cache.type_int) {
  1293. // Assume the index parameter is an enum
  1294. const TypeInterface *idx_arg_type = _get_type_or_null(idx_arg.type);
  1295. CRASH_COND(idx_arg_type == nullptr);
  1296. p_output.append("(" + idx_arg_type->proxy_name + ")" + itos(p_iprop.index));
  1297. } else {
  1298. p_output.append(itos(p_iprop.index));
  1299. }
  1300. }
  1301. p_output.append(");\n"
  1302. CLOSE_BLOCK_L3
  1303. // TODO Remove this once we make accessor methods private/internal (they will no longer be marked as obsolete after that)
  1304. "#pragma warning restore CS0618\n");
  1305. }
  1306. if (setter) {
  1307. p_output.append(INDENT3 "set\n"
  1308. // TODO Remove this once we make accessor methods private/internal (they will no longer be marked as obsolete after that)
  1309. "#pragma warning disable CS0618 // Disable warning about obsolete method\n"
  1310. OPEN_BLOCK_L3);
  1311. p_output.append(setter->proxy_name + "(");
  1312. if (p_iprop.index != -1) {
  1313. const ArgumentInterface &idx_arg = setter->arguments.front()->get();
  1314. if (idx_arg.type.cname != name_cache.type_int) {
  1315. // Assume the index parameter is an enum
  1316. const TypeInterface *idx_arg_type = _get_type_or_null(idx_arg.type);
  1317. CRASH_COND(idx_arg_type == nullptr);
  1318. p_output.append("(" + idx_arg_type->proxy_name + ")" + itos(p_iprop.index) + ", ");
  1319. } else {
  1320. p_output.append(itos(p_iprop.index) + ", ");
  1321. }
  1322. }
  1323. p_output.append("value);\n"
  1324. CLOSE_BLOCK_L3
  1325. // TODO Remove this once we make accessor methods private/internal (they will no longer be marked as obsolete after that)
  1326. "#pragma warning restore CS0618\n");
  1327. }
  1328. p_output.append(CLOSE_BLOCK_L2);
  1329. return OK;
  1330. }
  1331. Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::MethodInterface &p_imethod, int &p_method_bind_count, StringBuilder &p_output) {
  1332. const TypeInterface *return_type = _get_type_or_placeholder(p_imethod.return_type);
  1333. ERR_FAIL_COND_V_MSG(return_type->is_singleton, ERR_BUG,
  1334. "Method return type is a singleton: '" + p_itype.name + "." + p_imethod.name + "'.");
  1335. if (p_itype.api_type == ClassDB::API_CORE) {
  1336. ERR_FAIL_COND_V_MSG(return_type->api_type == ClassDB::API_EDITOR, ERR_BUG,
  1337. "Method '" + p_itype.name + "." + p_imethod.name + "' has return type '" + return_type->name +
  1338. "' from the editor API. Core API cannot have dependencies on the editor API.");
  1339. }
  1340. String method_bind_field = "__method_bind_" + itos(p_method_bind_count);
  1341. String arguments_sig;
  1342. String cs_in_statements;
  1343. String icall_params = method_bind_field + ", ";
  1344. icall_params += sformat(p_itype.cs_in, "this");
  1345. StringBuilder default_args_doc;
  1346. // Retrieve information from the arguments
  1347. for (const List<ArgumentInterface>::Element *F = p_imethod.arguments.front(); F; F = F->next()) {
  1348. const ArgumentInterface &iarg = F->get();
  1349. const TypeInterface *arg_type = _get_type_or_placeholder(iarg.type);
  1350. ERR_FAIL_COND_V_MSG(arg_type->is_singleton, ERR_BUG,
  1351. "Argument type is a singleton: '" + iarg.name + "' of method '" + p_itype.name + "." + p_imethod.name + "'.");
  1352. if (p_itype.api_type == ClassDB::API_CORE) {
  1353. ERR_FAIL_COND_V_MSG(arg_type->api_type == ClassDB::API_EDITOR, ERR_BUG,
  1354. "Argument '" + iarg.name + "' of method '" + p_itype.name + "." + p_imethod.name + "' has type '" +
  1355. arg_type->name + "' from the editor API. Core API cannot have dependencies on the editor API.");
  1356. }
  1357. if (iarg.default_argument.size()) {
  1358. CRASH_COND_MSG(!_arg_default_value_is_assignable_to_type(iarg.def_param_value, *arg_type),
  1359. "Invalid default value for parameter '" + iarg.name + "' of method '" + p_itype.name + "." + p_imethod.name + "'.");
  1360. }
  1361. // Add the current arguments to the signature
  1362. // If the argument has a default value which is not a constant, we will make it Nullable
  1363. {
  1364. if (F != p_imethod.arguments.front()) {
  1365. arguments_sig += ", ";
  1366. }
  1367. if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) {
  1368. arguments_sig += "Nullable<";
  1369. }
  1370. arguments_sig += arg_type->cs_type;
  1371. if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) {
  1372. arguments_sig += "> ";
  1373. } else {
  1374. arguments_sig += " ";
  1375. }
  1376. arguments_sig += iarg.name;
  1377. if (iarg.default_argument.size()) {
  1378. if (iarg.def_param_mode != ArgumentInterface::CONSTANT) {
  1379. arguments_sig += " = null";
  1380. } else {
  1381. arguments_sig += " = " + sformat(iarg.default_argument, arg_type->cs_type);
  1382. }
  1383. }
  1384. }
  1385. icall_params += ", ";
  1386. if (iarg.default_argument.size() && iarg.def_param_mode != ArgumentInterface::CONSTANT) {
  1387. // The default value of an argument must be constant. Otherwise we make it Nullable and do the following:
  1388. // Type arg_in = arg.HasValue ? arg.Value : <non-const default value>;
  1389. String arg_in = iarg.name;
  1390. arg_in += "_in";
  1391. cs_in_statements += arg_type->cs_type;
  1392. cs_in_statements += " ";
  1393. cs_in_statements += arg_in;
  1394. cs_in_statements += " = ";
  1395. cs_in_statements += iarg.name;
  1396. if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) {
  1397. cs_in_statements += ".HasValue ? ";
  1398. } else {
  1399. cs_in_statements += " != null ? ";
  1400. }
  1401. cs_in_statements += iarg.name;
  1402. if (iarg.def_param_mode == ArgumentInterface::NULLABLE_VAL) {
  1403. cs_in_statements += ".Value : ";
  1404. } else {
  1405. cs_in_statements += " : ";
  1406. }
  1407. String def_arg = sformat(iarg.default_argument, arg_type->cs_type);
  1408. cs_in_statements += def_arg;
  1409. cs_in_statements += ";\n" INDENT3;
  1410. icall_params += arg_type->cs_in.is_empty() ? arg_in : sformat(arg_type->cs_in, arg_in);
  1411. // Apparently the name attribute must not include the @
  1412. String param_tag_name = iarg.name.begins_with("@") ? iarg.name.substr(1, iarg.name.length()) : iarg.name;
  1413. default_args_doc.append(MEMBER_BEGIN "/// <param name=\"" + param_tag_name + "\">If the parameter is null, then the default value is " + def_arg + "</param>");
  1414. } else {
  1415. icall_params += arg_type->cs_in.is_empty() ? iarg.name : sformat(arg_type->cs_in, iarg.name);
  1416. }
  1417. }
  1418. // Generate method
  1419. {
  1420. if (!p_imethod.is_virtual && !p_imethod.requires_object_call) {
  1421. p_output.append(MEMBER_BEGIN "[DebuggerBrowsable(DebuggerBrowsableState.Never)]" MEMBER_BEGIN "private static readonly IntPtr ");
  1422. p_output.append(method_bind_field);
  1423. p_output.append(" = Object." ICALL_GET_METHODBIND "(" BINDINGS_NATIVE_NAME_FIELD ", \"");
  1424. p_output.append(p_imethod.name);
  1425. p_output.append("\");\n");
  1426. }
  1427. if (p_imethod.method_doc && p_imethod.method_doc->description.size()) {
  1428. String xml_summary = bbcode_to_xml(fix_doc_description(p_imethod.method_doc->description), &p_itype);
  1429. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  1430. if (summary_lines.size()) {
  1431. p_output.append(MEMBER_BEGIN "/// <summary>\n");
  1432. for (int i = 0; i < summary_lines.size(); i++) {
  1433. p_output.append(INDENT2 "/// ");
  1434. p_output.append(summary_lines[i]);
  1435. p_output.append("\n");
  1436. }
  1437. p_output.append(INDENT2 "/// </summary>");
  1438. }
  1439. }
  1440. if (default_args_doc.get_string_length()) {
  1441. p_output.append(default_args_doc.as_string());
  1442. }
  1443. if (!p_imethod.is_internal) {
  1444. // TODO: This alone adds ~0.2 MB of bloat to the core API assembly. It would be
  1445. // better to generate a table in the C++ glue instead. That way the strings wouldn't
  1446. // add that much extra bloat as they're already used in engine code. Also, it would
  1447. // probably be much faster than looking up the attributes when fetching methods.
  1448. p_output.append(MEMBER_BEGIN "[GodotMethod(\"");
  1449. p_output.append(p_imethod.name);
  1450. p_output.append("\")]");
  1451. }
  1452. if (p_imethod.is_deprecated) {
  1453. if (p_imethod.deprecation_message.is_empty()) {
  1454. WARN_PRINT("An empty deprecation message is discouraged. Method: '" + p_imethod.proxy_name + "'.");
  1455. }
  1456. p_output.append(MEMBER_BEGIN "[Obsolete(\"");
  1457. p_output.append(p_imethod.deprecation_message);
  1458. p_output.append("\")]");
  1459. }
  1460. p_output.append(MEMBER_BEGIN);
  1461. p_output.append(p_imethod.is_internal ? "internal " : "public ");
  1462. if (p_itype.is_singleton) {
  1463. p_output.append("static ");
  1464. } else if (p_imethod.is_virtual) {
  1465. p_output.append("virtual ");
  1466. }
  1467. p_output.append(return_type->cs_type + " ");
  1468. p_output.append(p_imethod.proxy_name + "(");
  1469. p_output.append(arguments_sig + ")\n" OPEN_BLOCK_L2);
  1470. if (p_imethod.is_virtual) {
  1471. // Godot virtual method must be overridden, therefore we return a default value by default.
  1472. if (return_type->cname == name_cache.type_void) {
  1473. p_output.append("return;\n" CLOSE_BLOCK_L2);
  1474. } else {
  1475. p_output.append("return default(");
  1476. p_output.append(return_type->cs_type);
  1477. p_output.append(");\n" CLOSE_BLOCK_L2);
  1478. }
  1479. return OK; // Won't increment method bind count
  1480. }
  1481. if (p_imethod.requires_object_call) {
  1482. // Fallback to Godot's object.Call(string, params)
  1483. p_output.append(CS_METHOD_CALL "(\"");
  1484. p_output.append(p_imethod.name);
  1485. p_output.append("\"");
  1486. for (const List<ArgumentInterface>::Element *F = p_imethod.arguments.front(); F; F = F->next()) {
  1487. p_output.append(", ");
  1488. p_output.append(F->get().name);
  1489. }
  1490. p_output.append(");\n" CLOSE_BLOCK_L2);
  1491. return OK; // Won't increment method bind count
  1492. }
  1493. const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&p_imethod);
  1494. ERR_FAIL_NULL_V(match, ERR_BUG);
  1495. const InternalCall *im_icall = match->value();
  1496. String im_call = im_icall->editor_only ? BINDINGS_CLASS_NATIVECALLS_EDITOR : BINDINGS_CLASS_NATIVECALLS;
  1497. im_call += ".";
  1498. im_call += im_icall->name;
  1499. if (p_imethod.arguments.size()) {
  1500. p_output.append(cs_in_statements);
  1501. }
  1502. if (return_type->cname == name_cache.type_void) {
  1503. p_output.append(im_call + "(" + icall_params + ");\n");
  1504. } else if (return_type->cs_out.is_empty()) {
  1505. p_output.append("return " + im_call + "(" + icall_params + ");\n");
  1506. } else {
  1507. p_output.append(sformat(return_type->cs_out, im_call, icall_params, return_type->cs_type, return_type->im_type_out));
  1508. p_output.append("\n");
  1509. }
  1510. p_output.append(CLOSE_BLOCK_L2);
  1511. }
  1512. p_method_bind_count++;
  1513. return OK;
  1514. }
  1515. Error BindingsGenerator::_generate_cs_signal(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::SignalInterface &p_isignal, StringBuilder &p_output) {
  1516. String arguments_sig;
  1517. // Retrieve information from the arguments
  1518. for (const List<ArgumentInterface>::Element *F = p_isignal.arguments.front(); F; F = F->next()) {
  1519. const ArgumentInterface &iarg = F->get();
  1520. const TypeInterface *arg_type = _get_type_or_placeholder(iarg.type);
  1521. ERR_FAIL_COND_V_MSG(arg_type->is_singleton, ERR_BUG,
  1522. "Argument type is a singleton: '" + iarg.name + "' of signal '" + p_itype.name + "." + p_isignal.name + "'.");
  1523. if (p_itype.api_type == ClassDB::API_CORE) {
  1524. ERR_FAIL_COND_V_MSG(arg_type->api_type == ClassDB::API_EDITOR, ERR_BUG,
  1525. "Argument '" + iarg.name + "' of signal '" + p_itype.name + "." + p_isignal.name + "' has type '" +
  1526. arg_type->name + "' from the editor API. Core API cannot have dependencies on the editor API.");
  1527. }
  1528. // Add the current arguments to the signature
  1529. if (F != p_isignal.arguments.front()) {
  1530. arguments_sig += ", ";
  1531. }
  1532. arguments_sig += arg_type->cs_type;
  1533. arguments_sig += " ";
  1534. arguments_sig += iarg.name;
  1535. }
  1536. // Generate signal
  1537. {
  1538. if (p_isignal.method_doc && p_isignal.method_doc->description.size()) {
  1539. String xml_summary = bbcode_to_xml(fix_doc_description(p_isignal.method_doc->description), &p_itype);
  1540. Vector<String> summary_lines = xml_summary.length() ? xml_summary.split("\n") : Vector<String>();
  1541. if (summary_lines.size()) {
  1542. p_output.append(MEMBER_BEGIN "/// <summary>\n");
  1543. for (int i = 0; i < summary_lines.size(); i++) {
  1544. p_output.append(INDENT2 "/// ");
  1545. p_output.append(summary_lines[i]);
  1546. p_output.append("\n");
  1547. }
  1548. p_output.append(INDENT2 "/// </summary>");
  1549. }
  1550. }
  1551. if (p_isignal.is_deprecated) {
  1552. if (p_isignal.deprecation_message.is_empty()) {
  1553. WARN_PRINT("An empty deprecation message is discouraged. Signal: '" + p_isignal.proxy_name + "'.");
  1554. }
  1555. p_output.append(MEMBER_BEGIN "[Obsolete(\"");
  1556. p_output.append(p_isignal.deprecation_message);
  1557. p_output.append("\")]");
  1558. }
  1559. String delegate_name = p_isignal.proxy_name;
  1560. delegate_name += "Handler"; // Delegate name is [SignalName]Handler
  1561. // Generate delegate
  1562. p_output.append(MEMBER_BEGIN "public delegate void ");
  1563. p_output.append(delegate_name);
  1564. p_output.append("(");
  1565. p_output.append(arguments_sig);
  1566. p_output.append(");\n");
  1567. // TODO:
  1568. // Could we assume the StringName instance of signal name will never be freed (it's stored in ClassDB) before the managed world is unloaded?
  1569. // If so, we could store the pointer we get from `data_unique_pointer()` instead of allocating StringName here.
  1570. // Cached signal name (StringName)
  1571. p_output.append(MEMBER_BEGIN "[DebuggerBrowsable(DebuggerBrowsableState.Never)]" MEMBER_BEGIN "private static StringName __signal_name_");
  1572. p_output.append(p_isignal.name);
  1573. p_output.append(" = \"");
  1574. p_output.append(p_isignal.name);
  1575. p_output.append("\";\n");
  1576. // Generate event
  1577. p_output.append(MEMBER_BEGIN "[Signal]" MEMBER_BEGIN "public ");
  1578. if (p_itype.is_singleton) {
  1579. p_output.append("static ");
  1580. }
  1581. p_output.append("event ");
  1582. p_output.append(delegate_name);
  1583. p_output.append(" ");
  1584. p_output.append(p_isignal.proxy_name);
  1585. p_output.append("\n" OPEN_BLOCK_L2);
  1586. if (p_itype.is_singleton) {
  1587. p_output.append("add => Singleton.Connect(__signal_name_");
  1588. } else {
  1589. p_output.append("add => Connect(__signal_name_");
  1590. }
  1591. p_output.append(p_isignal.name);
  1592. p_output.append(", new Callable(value));\n");
  1593. if (p_itype.is_singleton) {
  1594. p_output.append(INDENT3 "remove => Singleton.Disconnect(__signal_name_");
  1595. } else {
  1596. p_output.append(INDENT3 "remove => Disconnect(__signal_name_");
  1597. }
  1598. p_output.append(p_isignal.name);
  1599. p_output.append(", new Callable(value));\n");
  1600. p_output.append(CLOSE_BLOCK_L2);
  1601. }
  1602. return OK;
  1603. }
  1604. Error BindingsGenerator::generate_glue(const String &p_output_dir) {
  1605. ERR_FAIL_COND_V(!initialized, ERR_UNCONFIGURED);
  1606. bool dir_exists = DirAccess::exists(p_output_dir);
  1607. ERR_FAIL_COND_V_MSG(!dir_exists, ERR_FILE_BAD_PATH, "The output directory does not exist.");
  1608. StringBuilder output;
  1609. output.append("/* THIS FILE IS GENERATED DO NOT EDIT */\n");
  1610. output.append("#include \"" GLUE_HEADER_FILE "\"\n");
  1611. output.append("\n#ifdef MONO_GLUE_ENABLED\n");
  1612. generated_icall_funcs.clear();
  1613. for (OrderedHashMap<StringName, TypeInterface>::Element type_elem = obj_types.front(); type_elem; type_elem = type_elem.next()) {
  1614. const TypeInterface &itype = type_elem.get();
  1615. bool is_derived_type = itype.base_name != StringName();
  1616. if (!is_derived_type) {
  1617. // Some Object assertions
  1618. CRASH_COND(itype.cname != name_cache.type_Object);
  1619. CRASH_COND(!itype.is_instantiable);
  1620. CRASH_COND(itype.api_type != ClassDB::API_CORE);
  1621. CRASH_COND(itype.is_singleton);
  1622. }
  1623. List<InternalCall> &custom_icalls = itype.api_type == ClassDB::API_EDITOR ? editor_custom_icalls : core_custom_icalls;
  1624. OS::get_singleton()->print("Generating %s...\n", itype.name.utf8().get_data());
  1625. String ctor_method(ICALL_PREFIX + itype.proxy_name + "_Ctor"); // Used only for derived types
  1626. for (const List<MethodInterface>::Element *E = itype.methods.front(); E; E = E->next()) {
  1627. const MethodInterface &imethod = E->get();
  1628. Error method_err = _generate_glue_method(itype, imethod, output);
  1629. ERR_FAIL_COND_V_MSG(method_err != OK, method_err,
  1630. "Failed to generate method '" + imethod.name + "' for class '" + itype.name + "'.");
  1631. }
  1632. if (itype.is_singleton) {
  1633. String singleton_icall_name = ICALL_PREFIX + itype.name + SINGLETON_ICALL_SUFFIX;
  1634. InternalCall singleton_icall = InternalCall(itype.api_type, singleton_icall_name, "IntPtr");
  1635. if (!find_icall_by_name(singleton_icall.name, custom_icalls)) {
  1636. custom_icalls.push_back(singleton_icall);
  1637. }
  1638. output.append("Object* ");
  1639. output.append(singleton_icall_name);
  1640. output.append("() " OPEN_BLOCK "\treturn Engine::get_singleton()->get_singleton_object(\"");
  1641. output.append(itype.proxy_name);
  1642. output.append("\");\n" CLOSE_BLOCK "\n");
  1643. }
  1644. if (is_derived_type && itype.is_instantiable) {
  1645. InternalCall ctor_icall = InternalCall(itype.api_type, ctor_method, "IntPtr", itype.proxy_name + " obj");
  1646. if (!find_icall_by_name(ctor_icall.name, custom_icalls)) {
  1647. custom_icalls.push_back(ctor_icall);
  1648. }
  1649. output.append("Object* ");
  1650. output.append(ctor_method);
  1651. output.append("(MonoObject* obj) " OPEN_BLOCK
  1652. "\t" C_MACRO_OBJECT_CONSTRUCT "(instance, \"");
  1653. output.append(itype.name);
  1654. output.append("\");\n"
  1655. "\t" C_METHOD_TIE_MANAGED_TO_UNMANAGED "(obj, instance);\n"
  1656. "\treturn instance;\n" CLOSE_BLOCK "\n");
  1657. }
  1658. }
  1659. output.append("namespace GodotSharpBindings\n" OPEN_BLOCK "\n");
  1660. output.append("uint64_t get_core_api_hash() { return ");
  1661. output.append(String::num_uint64(GDMono::get_singleton()->get_api_core_hash()) + "U; }\n");
  1662. output.append("#ifdef TOOLS_ENABLED\n"
  1663. "uint64_t get_editor_api_hash() { return ");
  1664. output.append(String::num_uint64(GDMono::get_singleton()->get_api_editor_hash()) + "U; }\n");
  1665. output.append("#endif // TOOLS_ENABLED\n");
  1666. output.append("uint32_t get_bindings_version() { return ");
  1667. output.append(String::num_uint64(BINDINGS_GENERATOR_VERSION) + "; }\n");
  1668. output.append("uint32_t get_cs_glue_version() { return ");
  1669. output.append(String::num_uint64(CS_GLUE_VERSION) + "; }\n");
  1670. output.append("\nvoid register_generated_icalls() " OPEN_BLOCK);
  1671. output.append("\tgodot_register_glue_header_icalls();\n");
  1672. #define ADD_INTERNAL_CALL_REGISTRATION(m_icall) \
  1673. { \
  1674. output.append("\tGDMonoUtils::add_internal_call("); \
  1675. output.append("\"" BINDINGS_NAMESPACE "."); \
  1676. output.append(m_icall.editor_only ? BINDINGS_CLASS_NATIVECALLS_EDITOR : BINDINGS_CLASS_NATIVECALLS); \
  1677. output.append("::"); \
  1678. output.append(m_icall.name); \
  1679. output.append("\", "); \
  1680. output.append(m_icall.name); \
  1681. output.append(");\n"); \
  1682. }
  1683. bool tools_sequence = false;
  1684. for (const List<InternalCall>::Element *E = core_custom_icalls.front(); E; E = E->next()) {
  1685. if (tools_sequence) {
  1686. if (!E->get().editor_only) {
  1687. tools_sequence = false;
  1688. output.append("#endif\n");
  1689. }
  1690. } else {
  1691. if (E->get().editor_only) {
  1692. output.append("#ifdef TOOLS_ENABLED\n");
  1693. tools_sequence = true;
  1694. }
  1695. }
  1696. ADD_INTERNAL_CALL_REGISTRATION(E->get());
  1697. }
  1698. if (tools_sequence) {
  1699. tools_sequence = false;
  1700. output.append("#endif\n");
  1701. }
  1702. output.append("#ifdef TOOLS_ENABLED\n");
  1703. for (const List<InternalCall>::Element *E = editor_custom_icalls.front(); E; E = E->next())
  1704. ADD_INTERNAL_CALL_REGISTRATION(E->get());
  1705. output.append("#endif // TOOLS_ENABLED\n");
  1706. for (const List<InternalCall>::Element *E = method_icalls.front(); E; E = E->next()) {
  1707. if (tools_sequence) {
  1708. if (!E->get().editor_only) {
  1709. tools_sequence = false;
  1710. output.append("#endif\n");
  1711. }
  1712. } else {
  1713. if (E->get().editor_only) {
  1714. output.append("#ifdef TOOLS_ENABLED\n");
  1715. tools_sequence = true;
  1716. }
  1717. }
  1718. ADD_INTERNAL_CALL_REGISTRATION(E->get());
  1719. }
  1720. if (tools_sequence) {
  1721. tools_sequence = false;
  1722. output.append("#endif\n");
  1723. }
  1724. #undef ADD_INTERNAL_CALL_REGISTRATION
  1725. output.append(CLOSE_BLOCK "\n} // namespace GodotSharpBindings\n");
  1726. output.append("\n#endif // MONO_GLUE_ENABLED\n");
  1727. Error save_err = _save_file(path::join(p_output_dir, "mono_glue.gen.cpp"), output);
  1728. if (save_err != OK) {
  1729. return save_err;
  1730. }
  1731. OS::get_singleton()->print("Mono glue generated successfully\n");
  1732. return OK;
  1733. }
  1734. uint32_t BindingsGenerator::get_version() {
  1735. return BINDINGS_GENERATOR_VERSION;
  1736. }
  1737. Error BindingsGenerator::_save_file(const String &p_path, const StringBuilder &p_content) {
  1738. FileAccessRef file = FileAccess::open(p_path, FileAccess::WRITE);
  1739. ERR_FAIL_COND_V_MSG(!file, ERR_FILE_CANT_WRITE, "Cannot open file: '" + p_path + "'.");
  1740. file->store_string(p_content.as_string());
  1741. file->close();
  1742. return OK;
  1743. }
  1744. Error BindingsGenerator::_generate_glue_method(const BindingsGenerator::TypeInterface &p_itype, const BindingsGenerator::MethodInterface &p_imethod, StringBuilder &p_output) {
  1745. if (p_imethod.is_virtual) {
  1746. return OK; // Ignore
  1747. }
  1748. bool ret_void = p_imethod.return_type.cname == name_cache.type_void;
  1749. const TypeInterface *return_type = _get_type_or_placeholder(p_imethod.return_type);
  1750. String argc_str = itos(p_imethod.arguments.size());
  1751. String c_func_sig = "MethodBind* " CS_PARAM_METHODBIND ", " + p_itype.c_type_in + " " CS_PARAM_INSTANCE;
  1752. String c_in_statements;
  1753. String c_args_var_content;
  1754. // Get arguments information
  1755. int i = 0;
  1756. for (const List<ArgumentInterface>::Element *F = p_imethod.arguments.front(); F; F = F->next()) {
  1757. const ArgumentInterface &iarg = F->get();
  1758. const TypeInterface *arg_type = _get_type_or_placeholder(iarg.type);
  1759. String c_param_name = "arg" + itos(i + 1);
  1760. if (p_imethod.is_vararg) {
  1761. if (i < p_imethod.arguments.size() - 1) {
  1762. c_in_statements += sformat(arg_type->c_in.size() ? arg_type->c_in : TypeInterface::DEFAULT_VARARG_C_IN, "Variant", c_param_name);
  1763. c_in_statements += "\t" C_LOCAL_PTRCALL_ARGS ".set(";
  1764. c_in_statements += itos(i);
  1765. c_in_statements += sformat(", &%s_in);\n", c_param_name);
  1766. }
  1767. } else {
  1768. if (i > 0) {
  1769. c_args_var_content += ", ";
  1770. }
  1771. if (arg_type->c_in.size()) {
  1772. c_in_statements += sformat(arg_type->c_in, arg_type->c_type, c_param_name);
  1773. }
  1774. c_args_var_content += sformat(arg_type->c_arg_in, c_param_name);
  1775. }
  1776. c_func_sig += ", ";
  1777. c_func_sig += arg_type->c_type_in;
  1778. c_func_sig += " ";
  1779. c_func_sig += c_param_name;
  1780. i++;
  1781. }
  1782. if (return_type->ret_as_byref_arg) {
  1783. c_func_sig += ", ";
  1784. c_func_sig += return_type->c_type_in;
  1785. c_func_sig += " ";
  1786. c_func_sig += "arg_ret";
  1787. i++;
  1788. }
  1789. const Map<const MethodInterface *, const InternalCall *>::Element *match = method_icalls_map.find(&p_imethod);
  1790. ERR_FAIL_NULL_V(match, ERR_BUG);
  1791. const InternalCall *im_icall = match->value();
  1792. String icall_method = im_icall->name;
  1793. if (!generated_icall_funcs.find(im_icall)) {
  1794. generated_icall_funcs.push_back(im_icall);
  1795. if (im_icall->editor_only) {
  1796. p_output.append("#ifdef TOOLS_ENABLED\n");
  1797. }
  1798. // Generate icall function
  1799. p_output.append((ret_void || return_type->ret_as_byref_arg) ? "void " : return_type->c_type_out + " ");
  1800. p_output.append(icall_method);
  1801. p_output.append("(");
  1802. p_output.append(c_func_sig);
  1803. p_output.append(") " OPEN_BLOCK);
  1804. if (!ret_void) {
  1805. String ptrcall_return_type;
  1806. String initialization;
  1807. if (p_imethod.is_vararg && return_type->cname != name_cache.type_Variant) {
  1808. // VarArg methods always return Variant, but there are some cases in which MethodInfo provides
  1809. // a specific return type. We trust this information is valid. We need a temporary local to keep
  1810. // the Variant alive until the method returns. Otherwise, if the returned Variant holds a RefPtr,
  1811. // it could be deleted too early. This is the case with GDScript.new() which returns OBJECT.
  1812. // Alternatively, we could just return Variant, but that would result in a worse API.
  1813. p_output.append("\tVariant " C_LOCAL_VARARG_RET ";\n");
  1814. }
  1815. if (return_type->is_object_type) {
  1816. ptrcall_return_type = return_type->is_reference ? "Ref<Reference>" : return_type->c_type;
  1817. initialization = return_type->is_reference ? "" : " = nullptr";
  1818. } else {
  1819. ptrcall_return_type = return_type->c_type;
  1820. }
  1821. p_output.append("\t" + ptrcall_return_type);
  1822. p_output.append(" " C_LOCAL_RET);
  1823. p_output.append(initialization + ";\n");
  1824. String fail_ret = return_type->c_type_out.ends_with("*") && !return_type->ret_as_byref_arg ? "nullptr" : return_type->c_type_out + "()";
  1825. if (return_type->ret_as_byref_arg) {
  1826. p_output.append("\tif (" CS_PARAM_INSTANCE " == nullptr) { *arg_ret = ");
  1827. p_output.append(fail_ret);
  1828. p_output.append("; ERR_FAIL_MSG(\"Parameter ' " CS_PARAM_INSTANCE " ' is null.\"); }\n");
  1829. } else {
  1830. p_output.append("\tERR_FAIL_NULL_V(" CS_PARAM_INSTANCE ", ");
  1831. p_output.append(fail_ret);
  1832. p_output.append(");\n");
  1833. }
  1834. } else {
  1835. p_output.append("\tERR_FAIL_NULL(" CS_PARAM_INSTANCE ");\n");
  1836. }
  1837. if (p_imethod.arguments.size()) {
  1838. if (p_imethod.is_vararg) {
  1839. String vararg_arg = "arg" + argc_str;
  1840. String real_argc_str = itos(p_imethod.arguments.size() - 1); // Arguments count without vararg
  1841. p_output.append("\tint vararg_length = mono_array_length(");
  1842. p_output.append(vararg_arg);
  1843. p_output.append(");\n\tint total_length = ");
  1844. p_output.append(real_argc_str);
  1845. p_output.append(" + vararg_length;\n"
  1846. "\tArgumentsVector<Variant> varargs(vararg_length);\n"
  1847. "\tArgumentsVector<const Variant *> " C_LOCAL_PTRCALL_ARGS "(total_length);\n");
  1848. p_output.append(c_in_statements);
  1849. p_output.append("\tfor (int i = 0; i < vararg_length; i++) " OPEN_BLOCK
  1850. "\t\tMonoObject* elem = mono_array_get(");
  1851. p_output.append(vararg_arg);
  1852. p_output.append(", MonoObject*, i);\n"
  1853. "\t\tvarargs.set(i, GDMonoMarshal::mono_object_to_variant(elem));\n"
  1854. "\t\t" C_LOCAL_PTRCALL_ARGS ".set(");
  1855. p_output.append(real_argc_str);
  1856. p_output.append(" + i, &varargs.get(i));\n\t" CLOSE_BLOCK);
  1857. } else {
  1858. p_output.append(c_in_statements);
  1859. p_output.append("\tconst void* " C_LOCAL_PTRCALL_ARGS "[");
  1860. p_output.append(argc_str + "] = { ");
  1861. p_output.append(c_args_var_content + " };\n");
  1862. }
  1863. }
  1864. if (p_imethod.is_vararg) {
  1865. p_output.append("\tCallable::CallError vcall_error;\n\t");
  1866. if (!ret_void) {
  1867. // See the comment on the C_LOCAL_VARARG_RET declaration
  1868. if (return_type->cname != name_cache.type_Variant) {
  1869. p_output.append(C_LOCAL_VARARG_RET " = ");
  1870. } else {
  1871. p_output.append(C_LOCAL_RET " = ");
  1872. }
  1873. }
  1874. p_output.append(CS_PARAM_METHODBIND "->call(" CS_PARAM_INSTANCE ", ");
  1875. p_output.append(p_imethod.arguments.size() ? C_LOCAL_PTRCALL_ARGS ".ptr()" : "nullptr");
  1876. p_output.append(", total_length, vcall_error);\n");
  1877. if (!ret_void) {
  1878. // See the comment on the C_LOCAL_VARARG_RET declaration
  1879. if (return_type->cname != name_cache.type_Variant) {
  1880. p_output.append("\t" C_LOCAL_RET " = " C_LOCAL_VARARG_RET ";\n");
  1881. }
  1882. }
  1883. } else {
  1884. p_output.append("\t" CS_PARAM_METHODBIND "->ptrcall(" CS_PARAM_INSTANCE ", ");
  1885. p_output.append(p_imethod.arguments.size() ? C_LOCAL_PTRCALL_ARGS ", " : "nullptr, ");
  1886. p_output.append(!ret_void ? "&" C_LOCAL_RET ");\n" : "nullptr);\n");
  1887. }
  1888. if (!ret_void) {
  1889. if (return_type->c_out.is_empty()) {
  1890. p_output.append("\treturn " C_LOCAL_RET ";\n");
  1891. } else if (return_type->ret_as_byref_arg) {
  1892. p_output.append(sformat(return_type->c_out, return_type->c_type_out, C_LOCAL_RET, return_type->name, "arg_ret"));
  1893. } else {
  1894. p_output.append(sformat(return_type->c_out, return_type->c_type_out, C_LOCAL_RET, return_type->name));
  1895. }
  1896. }
  1897. p_output.append(CLOSE_BLOCK "\n");
  1898. if (im_icall->editor_only) {
  1899. p_output.append("#endif // TOOLS_ENABLED\n");
  1900. }
  1901. }
  1902. return OK;
  1903. }
  1904. const BindingsGenerator::TypeInterface *BindingsGenerator::_get_type_or_null(const TypeReference &p_typeref) {
  1905. const Map<StringName, TypeInterface>::Element *builtin_type_match = builtin_types.find(p_typeref.cname);
  1906. if (builtin_type_match) {
  1907. return &builtin_type_match->get();
  1908. }
  1909. const OrderedHashMap<StringName, TypeInterface>::Element obj_type_match = obj_types.find(p_typeref.cname);
  1910. if (obj_type_match) {
  1911. return &obj_type_match.get();
  1912. }
  1913. if (p_typeref.is_enum) {
  1914. const Map<StringName, TypeInterface>::Element *enum_match = enum_types.find(p_typeref.cname);
  1915. if (enum_match) {
  1916. return &enum_match->get();
  1917. }
  1918. // Enum not found. Most likely because none of its constants were bound, so it's empty. That's fine. Use int instead.
  1919. const Map<StringName, TypeInterface>::Element *int_match = builtin_types.find(name_cache.type_int);
  1920. ERR_FAIL_NULL_V(int_match, nullptr);
  1921. return &int_match->get();
  1922. }
  1923. return nullptr;
  1924. }
  1925. const BindingsGenerator::TypeInterface *BindingsGenerator::_get_type_or_placeholder(const TypeReference &p_typeref) {
  1926. const TypeInterface *found = _get_type_or_null(p_typeref);
  1927. if (found) {
  1928. return found;
  1929. }
  1930. ERR_PRINT(String() + "Type not found. Creating placeholder: '" + p_typeref.cname.operator String() + "'.");
  1931. const Map<StringName, TypeInterface>::Element *match = placeholder_types.find(p_typeref.cname);
  1932. if (match) {
  1933. return &match->get();
  1934. }
  1935. TypeInterface placeholder;
  1936. TypeInterface::create_placeholder_type(placeholder, p_typeref.cname);
  1937. return &placeholder_types.insert(placeholder.cname, placeholder)->get();
  1938. }
  1939. StringName BindingsGenerator::_get_int_type_name_from_meta(GodotTypeInfo::Metadata p_meta) {
  1940. switch (p_meta) {
  1941. case GodotTypeInfo::METADATA_INT_IS_INT8:
  1942. return "sbyte";
  1943. break;
  1944. case GodotTypeInfo::METADATA_INT_IS_INT16:
  1945. return "short";
  1946. break;
  1947. case GodotTypeInfo::METADATA_INT_IS_INT32:
  1948. return "int";
  1949. break;
  1950. case GodotTypeInfo::METADATA_INT_IS_INT64:
  1951. return "long";
  1952. break;
  1953. case GodotTypeInfo::METADATA_INT_IS_UINT8:
  1954. return "byte";
  1955. break;
  1956. case GodotTypeInfo::METADATA_INT_IS_UINT16:
  1957. return "ushort";
  1958. break;
  1959. case GodotTypeInfo::METADATA_INT_IS_UINT32:
  1960. return "uint";
  1961. break;
  1962. case GodotTypeInfo::METADATA_INT_IS_UINT64:
  1963. return "ulong";
  1964. break;
  1965. default:
  1966. // Assume INT32
  1967. return "int";
  1968. }
  1969. }
  1970. StringName BindingsGenerator::_get_float_type_name_from_meta(GodotTypeInfo::Metadata p_meta) {
  1971. switch (p_meta) {
  1972. case GodotTypeInfo::METADATA_REAL_IS_FLOAT:
  1973. return "float";
  1974. break;
  1975. case GodotTypeInfo::METADATA_REAL_IS_DOUBLE:
  1976. return "double";
  1977. break;
  1978. default:
  1979. // Assume real_t (float or double depending of REAL_T_IS_DOUBLE)
  1980. #ifdef REAL_T_IS_DOUBLE
  1981. return "double";
  1982. #else
  1983. return "float";
  1984. #endif
  1985. }
  1986. }
  1987. bool BindingsGenerator::_arg_default_value_is_assignable_to_type(const Variant &p_val, const TypeInterface &p_arg_type) {
  1988. if (p_arg_type.name == name_cache.type_Variant) {
  1989. // Variant can take anything
  1990. return true;
  1991. }
  1992. switch (p_val.get_type()) {
  1993. case Variant::NIL:
  1994. return p_arg_type.is_object_type ||
  1995. name_cache.is_nullable_type(p_arg_type.name);
  1996. case Variant::BOOL:
  1997. return p_arg_type.name == name_cache.type_bool;
  1998. case Variant::INT:
  1999. return p_arg_type.name == name_cache.type_sbyte ||
  2000. p_arg_type.name == name_cache.type_short ||
  2001. p_arg_type.name == name_cache.type_int ||
  2002. p_arg_type.name == name_cache.type_byte ||
  2003. p_arg_type.name == name_cache.type_ushort ||
  2004. p_arg_type.name == name_cache.type_uint ||
  2005. p_arg_type.name == name_cache.type_long ||
  2006. p_arg_type.name == name_cache.type_ulong ||
  2007. p_arg_type.name == name_cache.type_float ||
  2008. p_arg_type.name == name_cache.type_double ||
  2009. p_arg_type.is_enum;
  2010. case Variant::FLOAT:
  2011. return p_arg_type.name == name_cache.type_float ||
  2012. p_arg_type.name == name_cache.type_double;
  2013. case Variant::STRING:
  2014. case Variant::STRING_NAME:
  2015. return p_arg_type.name == name_cache.type_String ||
  2016. p_arg_type.name == name_cache.type_StringName ||
  2017. p_arg_type.name == name_cache.type_NodePath;
  2018. case Variant::NODE_PATH:
  2019. return p_arg_type.name == name_cache.type_NodePath;
  2020. case Variant::TRANSFORM:
  2021. case Variant::TRANSFORM2D:
  2022. case Variant::BASIS:
  2023. case Variant::QUAT:
  2024. case Variant::PLANE:
  2025. case Variant::AABB:
  2026. case Variant::COLOR:
  2027. case Variant::VECTOR2:
  2028. case Variant::RECT2:
  2029. case Variant::VECTOR3:
  2030. case Variant::RID:
  2031. case Variant::ARRAY:
  2032. case Variant::DICTIONARY:
  2033. case Variant::PACKED_BYTE_ARRAY:
  2034. case Variant::PACKED_INT32_ARRAY:
  2035. case Variant::PACKED_INT64_ARRAY:
  2036. case Variant::PACKED_FLOAT32_ARRAY:
  2037. case Variant::PACKED_FLOAT64_ARRAY:
  2038. case Variant::PACKED_STRING_ARRAY:
  2039. case Variant::PACKED_VECTOR2_ARRAY:
  2040. case Variant::PACKED_VECTOR3_ARRAY:
  2041. case Variant::PACKED_COLOR_ARRAY:
  2042. case Variant::CALLABLE:
  2043. case Variant::SIGNAL:
  2044. return p_arg_type.name == Variant::get_type_name(p_val.get_type());
  2045. case Variant::OBJECT:
  2046. return p_arg_type.is_object_type;
  2047. case Variant::VECTOR2I:
  2048. return p_arg_type.name == name_cache.type_Vector2 ||
  2049. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  2050. case Variant::RECT2I:
  2051. return p_arg_type.name == name_cache.type_Rect2 ||
  2052. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  2053. case Variant::VECTOR3I:
  2054. return p_arg_type.name == name_cache.type_Vector3 ||
  2055. p_arg_type.name == Variant::get_type_name(p_val.get_type());
  2056. default:
  2057. CRASH_NOW_MSG("Unexpected Variant type: " + itos(p_val.get_type()));
  2058. break;
  2059. }
  2060. return false;
  2061. }
  2062. bool BindingsGenerator::_populate_object_type_interfaces() {
  2063. obj_types.clear();
  2064. List<StringName> class_list;
  2065. ClassDB::get_class_list(&class_list);
  2066. class_list.sort_custom<StringName::AlphCompare>();
  2067. while (class_list.size()) {
  2068. StringName type_cname = class_list.front()->get();
  2069. ClassDB::APIType api_type = ClassDB::get_api_type(type_cname);
  2070. if (api_type == ClassDB::API_NONE) {
  2071. class_list.pop_front();
  2072. continue;
  2073. }
  2074. if (!ClassDB::is_class_exposed(type_cname)) {
  2075. _log("Ignoring type '%s' because it's not exposed\n", String(type_cname).utf8().get_data());
  2076. class_list.pop_front();
  2077. continue;
  2078. }
  2079. if (!ClassDB::is_class_enabled(type_cname)) {
  2080. _log("Ignoring type '%s' because it's not enabled\n", String(type_cname).utf8().get_data());
  2081. class_list.pop_front();
  2082. continue;
  2083. }
  2084. ClassDB::ClassInfo *class_info = ClassDB::classes.getptr(type_cname);
  2085. TypeInterface itype = TypeInterface::create_object_type(type_cname, api_type);
  2086. itype.base_name = ClassDB::get_parent_class(type_cname);
  2087. itype.is_singleton = Engine::get_singleton()->has_singleton(itype.proxy_name);
  2088. itype.is_instantiable = class_info->creation_func && !itype.is_singleton;
  2089. itype.is_reference = ClassDB::is_parent_class(type_cname, name_cache.type_Reference);
  2090. itype.memory_own = itype.is_reference;
  2091. itype.c_out = "\treturn ";
  2092. itype.c_out += C_METHOD_UNMANAGED_GET_MANAGED;
  2093. itype.c_out += itype.is_reference ? "(%1.ptr());\n" : "(%1);\n";
  2094. itype.cs_in = itype.is_singleton ? BINDINGS_PTR_FIELD : "Object." CS_SMETHOD_GETINSTANCE "(%0)";
  2095. itype.c_type = "Object*";
  2096. itype.c_type_in = itype.c_type;
  2097. itype.c_type_out = "MonoObject*";
  2098. itype.cs_type = itype.proxy_name;
  2099. itype.im_type_in = "IntPtr";
  2100. itype.im_type_out = itype.proxy_name;
  2101. // Populate properties
  2102. List<PropertyInfo> property_list;
  2103. ClassDB::get_property_list(type_cname, &property_list, true);
  2104. Map<StringName, StringName> accessor_methods;
  2105. for (const List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) {
  2106. const PropertyInfo &property = E->get();
  2107. if (property.usage & PROPERTY_USAGE_GROUP || property.usage & PROPERTY_USAGE_SUBGROUP || property.usage & PROPERTY_USAGE_CATEGORY) {
  2108. continue;
  2109. }
  2110. if (property.name.find("/") >= 0) {
  2111. // Ignore properties with '/' (slash) in the name. These are only meant for use in the inspector.
  2112. continue;
  2113. }
  2114. PropertyInterface iprop;
  2115. iprop.cname = property.name;
  2116. iprop.setter = ClassDB::get_property_setter(type_cname, iprop.cname);
  2117. iprop.getter = ClassDB::get_property_getter(type_cname, iprop.cname);
  2118. if (iprop.setter != StringName()) {
  2119. accessor_methods[iprop.setter] = iprop.cname;
  2120. }
  2121. if (iprop.getter != StringName()) {
  2122. accessor_methods[iprop.getter] = iprop.cname;
  2123. }
  2124. bool valid = false;
  2125. iprop.index = ClassDB::get_property_index(type_cname, iprop.cname, &valid);
  2126. ERR_FAIL_COND_V_MSG(!valid, false, "Invalid property: '" + itype.name + "." + String(iprop.cname) + "'.");
  2127. iprop.proxy_name = escape_csharp_keyword(snake_to_pascal_case(iprop.cname));
  2128. // Prevent the property and its enclosing type from sharing the same name
  2129. if (iprop.proxy_name == itype.proxy_name) {
  2130. _log("Name of property '%s' is ambiguous with the name of its enclosing class '%s'. Renaming property to '%s_'\n",
  2131. iprop.proxy_name.utf8().get_data(), itype.proxy_name.utf8().get_data(), iprop.proxy_name.utf8().get_data());
  2132. iprop.proxy_name += "_";
  2133. }
  2134. iprop.prop_doc = nullptr;
  2135. for (int i = 0; i < itype.class_doc->properties.size(); i++) {
  2136. const DocData::PropertyDoc &prop_doc = itype.class_doc->properties[i];
  2137. if (prop_doc.name == iprop.cname) {
  2138. iprop.prop_doc = &prop_doc;
  2139. break;
  2140. }
  2141. }
  2142. itype.properties.push_back(iprop);
  2143. }
  2144. // Populate methods
  2145. List<MethodInfo> virtual_method_list;
  2146. ClassDB::get_virtual_methods(type_cname, &virtual_method_list, true);
  2147. List<MethodInfo> method_list;
  2148. ClassDB::get_method_list(type_cname, &method_list, true);
  2149. method_list.sort();
  2150. for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) {
  2151. const MethodInfo &method_info = E->get();
  2152. int argc = method_info.arguments.size();
  2153. if (method_info.name.is_empty()) {
  2154. continue;
  2155. }
  2156. String cname = method_info.name;
  2157. if (blacklisted_methods.find(itype.cname) && blacklisted_methods[itype.cname].find(cname)) {
  2158. continue;
  2159. }
  2160. MethodInterface imethod;
  2161. imethod.name = method_info.name;
  2162. imethod.cname = cname;
  2163. if (method_info.flags & METHOD_FLAG_VIRTUAL) {
  2164. imethod.is_virtual = true;
  2165. }
  2166. PropertyInfo return_info = method_info.return_val;
  2167. MethodBind *m = imethod.is_virtual ? nullptr : ClassDB::get_method(type_cname, method_info.name);
  2168. imethod.is_vararg = m && m->is_vararg();
  2169. if (!m && !imethod.is_virtual) {
  2170. ERR_FAIL_COND_V_MSG(!virtual_method_list.find(method_info), false,
  2171. "Missing MethodBind for non-virtual method: '" + itype.name + "." + imethod.name + "'.");
  2172. // A virtual method without the virtual flag. This is a special case.
  2173. // There is no method bind, so let's fallback to Godot's object.Call(string, params)
  2174. imethod.requires_object_call = true;
  2175. // The method Object.free is registered as a virtual method, but without the virtual flag.
  2176. // This is because this method is not supposed to be overridden, but called.
  2177. // We assume the return type is void.
  2178. imethod.return_type.cname = name_cache.type_void;
  2179. // Actually, more methods like this may be added in the future, which could return
  2180. // something different. Let's put this check to notify us if that ever happens.
  2181. if (itype.cname != name_cache.type_Object || imethod.name != "free") {
  2182. WARN_PRINT("Notification: New unexpected virtual non-overridable method found."
  2183. " We only expected Object.free, but found '" +
  2184. itype.name + "." + imethod.name + "'.");
  2185. }
  2186. } else if (return_info.type == Variant::INT && return_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2187. imethod.return_type.cname = return_info.class_name;
  2188. imethod.return_type.is_enum = true;
  2189. } else if (return_info.class_name != StringName()) {
  2190. imethod.return_type.cname = return_info.class_name;
  2191. bool bad_reference_hint = !imethod.is_virtual && return_info.hint != PROPERTY_HINT_RESOURCE_TYPE &&
  2192. ClassDB::is_parent_class(return_info.class_name, name_cache.type_Reference);
  2193. ERR_FAIL_COND_V_MSG(bad_reference_hint, false,
  2194. String() + "Return type is reference but hint is not '" _STR(PROPERTY_HINT_RESOURCE_TYPE) "'." +
  2195. " Are you returning a reference type by pointer? Method: '" + itype.name + "." + imethod.name + "'.");
  2196. } else if (return_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  2197. imethod.return_type.cname = return_info.hint_string;
  2198. } else if (return_info.type == Variant::NIL && return_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  2199. imethod.return_type.cname = name_cache.type_Variant;
  2200. } else if (return_info.type == Variant::NIL) {
  2201. imethod.return_type.cname = name_cache.type_void;
  2202. } else {
  2203. if (return_info.type == Variant::INT) {
  2204. imethod.return_type.cname = _get_int_type_name_from_meta(m ? m->get_argument_meta(-1) : GodotTypeInfo::METADATA_NONE);
  2205. } else if (return_info.type == Variant::FLOAT) {
  2206. imethod.return_type.cname = _get_float_type_name_from_meta(m ? m->get_argument_meta(-1) : GodotTypeInfo::METADATA_NONE);
  2207. } else {
  2208. imethod.return_type.cname = Variant::get_type_name(return_info.type);
  2209. }
  2210. }
  2211. for (int i = 0; i < argc; i++) {
  2212. PropertyInfo arginfo = method_info.arguments[i];
  2213. String orig_arg_name = arginfo.name;
  2214. ArgumentInterface iarg;
  2215. iarg.name = orig_arg_name;
  2216. if (arginfo.type == Variant::INT && arginfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2217. iarg.type.cname = arginfo.class_name;
  2218. iarg.type.is_enum = true;
  2219. } else if (arginfo.class_name != StringName()) {
  2220. iarg.type.cname = arginfo.class_name;
  2221. } else if (arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  2222. iarg.type.cname = arginfo.hint_string;
  2223. } else if (arginfo.type == Variant::NIL) {
  2224. iarg.type.cname = name_cache.type_Variant;
  2225. } else {
  2226. if (arginfo.type == Variant::INT) {
  2227. iarg.type.cname = _get_int_type_name_from_meta(m ? m->get_argument_meta(i) : GodotTypeInfo::METADATA_NONE);
  2228. } else if (arginfo.type == Variant::FLOAT) {
  2229. iarg.type.cname = _get_float_type_name_from_meta(m ? m->get_argument_meta(i) : GodotTypeInfo::METADATA_NONE);
  2230. } else {
  2231. iarg.type.cname = Variant::get_type_name(arginfo.type);
  2232. }
  2233. }
  2234. iarg.name = escape_csharp_keyword(snake_to_camel_case(iarg.name));
  2235. if (m && m->has_default_argument(i)) {
  2236. bool defval_ok = _arg_default_value_from_variant(m->get_default_argument(i), iarg);
  2237. ERR_FAIL_COND_V_MSG(!defval_ok, false,
  2238. "Cannot determine default value for argument '" + orig_arg_name + "' of method '" + itype.name + "." + imethod.name + "'.");
  2239. }
  2240. imethod.add_argument(iarg);
  2241. }
  2242. if (imethod.is_vararg) {
  2243. ArgumentInterface ivararg;
  2244. ivararg.type.cname = name_cache.type_VarArg;
  2245. ivararg.name = "@args";
  2246. imethod.add_argument(ivararg);
  2247. }
  2248. imethod.proxy_name = escape_csharp_keyword(snake_to_pascal_case(imethod.name));
  2249. // Prevent the method and its enclosing type from sharing the same name
  2250. if (imethod.proxy_name == itype.proxy_name) {
  2251. _log("Name of method '%s' is ambiguous with the name of its enclosing class '%s'. Renaming method to '%s_'\n",
  2252. imethod.proxy_name.utf8().get_data(), itype.proxy_name.utf8().get_data(), imethod.proxy_name.utf8().get_data());
  2253. imethod.proxy_name += "_";
  2254. }
  2255. Map<StringName, StringName>::Element *accessor = accessor_methods.find(imethod.cname);
  2256. if (accessor) {
  2257. const PropertyInterface *accessor_property = itype.find_property_by_name(accessor->value());
  2258. // We only deprecate an accessor method if it's in the same class as the property. It's easier this way, but also
  2259. // we don't know if an accessor method in a different class could have other purposes, so better leave those untouched.
  2260. imethod.is_deprecated = true;
  2261. imethod.deprecation_message = imethod.proxy_name + " is deprecated. Use the " + accessor_property->proxy_name + " property instead.";
  2262. }
  2263. if (itype.class_doc) {
  2264. for (int i = 0; i < itype.class_doc->methods.size(); i++) {
  2265. if (itype.class_doc->methods[i].name == imethod.name) {
  2266. imethod.method_doc = &itype.class_doc->methods[i];
  2267. break;
  2268. }
  2269. }
  2270. }
  2271. ERR_FAIL_COND_V_MSG(itype.find_property_by_name(imethod.cname), false,
  2272. "Method name conflicts with property: '" + itype.name + "." + imethod.name + "'.");
  2273. // Classes starting with an underscore are ignored unless they're used as a property setter or getter
  2274. if (!imethod.is_virtual && imethod.name[0] == '_') {
  2275. for (const List<PropertyInterface>::Element *F = itype.properties.front(); F; F = F->next()) {
  2276. const PropertyInterface &iprop = F->get();
  2277. if (iprop.setter == imethod.name || iprop.getter == imethod.name) {
  2278. imethod.is_internal = true;
  2279. itype.methods.push_back(imethod);
  2280. break;
  2281. }
  2282. }
  2283. } else {
  2284. itype.methods.push_back(imethod);
  2285. }
  2286. }
  2287. // Populate signals
  2288. const HashMap<StringName, MethodInfo> &signal_map = class_info->signal_map;
  2289. const StringName *k = nullptr;
  2290. while ((k = signal_map.next(k))) {
  2291. SignalInterface isignal;
  2292. const MethodInfo &method_info = signal_map.get(*k);
  2293. isignal.name = method_info.name;
  2294. isignal.cname = method_info.name;
  2295. int argc = method_info.arguments.size();
  2296. for (int i = 0; i < argc; i++) {
  2297. PropertyInfo arginfo = method_info.arguments[i];
  2298. String orig_arg_name = arginfo.name;
  2299. ArgumentInterface iarg;
  2300. iarg.name = orig_arg_name;
  2301. if (arginfo.type == Variant::INT && arginfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  2302. iarg.type.cname = arginfo.class_name;
  2303. iarg.type.is_enum = true;
  2304. } else if (arginfo.class_name != StringName()) {
  2305. iarg.type.cname = arginfo.class_name;
  2306. } else if (arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  2307. iarg.type.cname = arginfo.hint_string;
  2308. } else if (arginfo.type == Variant::NIL) {
  2309. iarg.type.cname = name_cache.type_Variant;
  2310. } else {
  2311. if (arginfo.type == Variant::INT) {
  2312. iarg.type.cname = _get_int_type_name_from_meta(GodotTypeInfo::METADATA_NONE);
  2313. } else if (arginfo.type == Variant::FLOAT) {
  2314. iarg.type.cname = _get_float_type_name_from_meta(GodotTypeInfo::METADATA_NONE);
  2315. } else {
  2316. iarg.type.cname = Variant::get_type_name(arginfo.type);
  2317. }
  2318. }
  2319. iarg.name = escape_csharp_keyword(snake_to_camel_case(iarg.name));
  2320. isignal.add_argument(iarg);
  2321. }
  2322. isignal.proxy_name = escape_csharp_keyword(snake_to_pascal_case(isignal.name));
  2323. // Prevent the signal and its enclosing type from sharing the same name
  2324. if (isignal.proxy_name == itype.proxy_name) {
  2325. _log("Name of signal '%s' is ambiguous with the name of its enclosing class '%s'. Renaming signal to '%s_'\n",
  2326. isignal.proxy_name.utf8().get_data(), itype.proxy_name.utf8().get_data(), isignal.proxy_name.utf8().get_data());
  2327. isignal.proxy_name += "_";
  2328. }
  2329. if (itype.find_property_by_proxy_name(isignal.proxy_name) || itype.find_method_by_proxy_name(isignal.proxy_name)) {
  2330. // ClassDB allows signal names that conflict with method or property names.
  2331. // While registering a signal with a conflicting name is considered wrong,
  2332. // it may still happen and it may take some time until someone fixes the name.
  2333. // We can't allow the bindings to be in a broken state while we wait for a fix;
  2334. // that's why we must handle this possibility by renaming the signal.
  2335. isignal.proxy_name += "Signal";
  2336. }
  2337. if (itype.class_doc) {
  2338. for (int i = 0; i < itype.class_doc->signals.size(); i++) {
  2339. const DocData::MethodDoc &signal_doc = itype.class_doc->signals[i];
  2340. if (signal_doc.name == isignal.name) {
  2341. isignal.method_doc = &signal_doc;
  2342. break;
  2343. }
  2344. }
  2345. }
  2346. itype.signals_.push_back(isignal);
  2347. }
  2348. // Populate enums and constants
  2349. List<String> constants;
  2350. ClassDB::get_integer_constant_list(type_cname, &constants, true);
  2351. const HashMap<StringName, List<StringName>> &enum_map = class_info->enum_map;
  2352. k = nullptr;
  2353. while ((k = enum_map.next(k))) {
  2354. StringName enum_proxy_cname = *k;
  2355. String enum_proxy_name = enum_proxy_cname.operator String();
  2356. if (itype.find_property_by_proxy_name(enum_proxy_cname)) {
  2357. // We have several conflicts between enums and PascalCase properties,
  2358. // so we append 'Enum' to the enum name in those cases.
  2359. enum_proxy_name += "Enum";
  2360. enum_proxy_cname = StringName(enum_proxy_name);
  2361. }
  2362. EnumInterface ienum(enum_proxy_cname);
  2363. const List<StringName> &enum_constants = enum_map.get(*k);
  2364. for (const List<StringName>::Element *E = enum_constants.front(); E; E = E->next()) {
  2365. const StringName &constant_cname = E->get();
  2366. String constant_name = constant_cname.operator String();
  2367. int *value = class_info->constant_map.getptr(constant_cname);
  2368. ERR_FAIL_NULL_V(value, false);
  2369. constants.erase(constant_name);
  2370. ConstantInterface iconstant(constant_name, snake_to_pascal_case(constant_name, true), *value);
  2371. iconstant.const_doc = nullptr;
  2372. for (int i = 0; i < itype.class_doc->constants.size(); i++) {
  2373. const DocData::ConstantDoc &const_doc = itype.class_doc->constants[i];
  2374. if (const_doc.name == iconstant.name) {
  2375. iconstant.const_doc = &const_doc;
  2376. break;
  2377. }
  2378. }
  2379. ienum.constants.push_back(iconstant);
  2380. }
  2381. int prefix_length = _determine_enum_prefix(ienum);
  2382. _apply_prefix_to_enum_constants(ienum, prefix_length);
  2383. itype.enums.push_back(ienum);
  2384. TypeInterface enum_itype;
  2385. enum_itype.is_enum = true;
  2386. enum_itype.name = itype.name + "." + String(*k);
  2387. enum_itype.cname = StringName(enum_itype.name);
  2388. enum_itype.proxy_name = itype.proxy_name + "." + enum_proxy_name;
  2389. TypeInterface::postsetup_enum_type(enum_itype);
  2390. enum_types.insert(enum_itype.cname, enum_itype);
  2391. }
  2392. for (const List<String>::Element *E = constants.front(); E; E = E->next()) {
  2393. const String &constant_name = E->get();
  2394. int *value = class_info->constant_map.getptr(StringName(E->get()));
  2395. ERR_FAIL_NULL_V(value, false);
  2396. ConstantInterface iconstant(constant_name, snake_to_pascal_case(constant_name, true), *value);
  2397. iconstant.const_doc = nullptr;
  2398. for (int i = 0; i < itype.class_doc->constants.size(); i++) {
  2399. const DocData::ConstantDoc &const_doc = itype.class_doc->constants[i];
  2400. if (const_doc.name == iconstant.name) {
  2401. iconstant.const_doc = &const_doc;
  2402. break;
  2403. }
  2404. }
  2405. itype.constants.push_back(iconstant);
  2406. }
  2407. obj_types.insert(itype.cname, itype);
  2408. class_list.pop_front();
  2409. }
  2410. return true;
  2411. }
  2412. bool BindingsGenerator::_arg_default_value_from_variant(const Variant &p_val, ArgumentInterface &r_iarg) {
  2413. r_iarg.def_param_value = p_val;
  2414. r_iarg.default_argument = p_val.operator String();
  2415. switch (p_val.get_type()) {
  2416. case Variant::NIL:
  2417. // Either Object type or Variant
  2418. r_iarg.default_argument = "null";
  2419. break;
  2420. // Atomic types
  2421. case Variant::BOOL:
  2422. r_iarg.default_argument = bool(p_val) ? "true" : "false";
  2423. break;
  2424. case Variant::INT:
  2425. if (r_iarg.type.cname != name_cache.type_int) {
  2426. r_iarg.default_argument = "(%s)" + r_iarg.default_argument;
  2427. }
  2428. break;
  2429. case Variant::FLOAT:
  2430. #ifndef REAL_T_IS_DOUBLE
  2431. r_iarg.default_argument += "f";
  2432. #endif
  2433. break;
  2434. case Variant::STRING:
  2435. case Variant::STRING_NAME:
  2436. case Variant::NODE_PATH:
  2437. if (r_iarg.type.cname == name_cache.type_StringName || r_iarg.type.cname == name_cache.type_NodePath) {
  2438. r_iarg.default_argument = "(%s)\"" + r_iarg.default_argument + "\"";
  2439. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF;
  2440. } else {
  2441. CRASH_COND(r_iarg.type.cname != name_cache.type_String);
  2442. r_iarg.default_argument = "\"" + r_iarg.default_argument + "\"";
  2443. }
  2444. break;
  2445. case Variant::TRANSFORM:
  2446. if (p_val.operator Transform() == Transform()) {
  2447. r_iarg.default_argument.clear();
  2448. }
  2449. r_iarg.default_argument = "new %s(" + r_iarg.default_argument + ")";
  2450. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL;
  2451. break;
  2452. case Variant::PLANE:
  2453. case Variant::AABB:
  2454. case Variant::COLOR:
  2455. r_iarg.default_argument = "new Color(1, 1, 1, 1)";
  2456. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL;
  2457. break;
  2458. case Variant::VECTOR2:
  2459. case Variant::VECTOR2I:
  2460. case Variant::RECT2:
  2461. case Variant::RECT2I:
  2462. case Variant::VECTOR3:
  2463. case Variant::VECTOR3I:
  2464. r_iarg.default_argument = "new %s" + r_iarg.default_argument;
  2465. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL;
  2466. break;
  2467. case Variant::OBJECT:
  2468. ERR_FAIL_COND_V_MSG(!p_val.is_zero(), false,
  2469. "Parameter of type '" + String(r_iarg.type.cname) + "' can only have null/zero as the default value.");
  2470. r_iarg.default_argument = "null";
  2471. break;
  2472. case Variant::DICTIONARY:
  2473. r_iarg.default_argument = "new %s()";
  2474. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF;
  2475. break;
  2476. case Variant::RID:
  2477. ERR_FAIL_COND_V_MSG(r_iarg.type.cname != name_cache.type_RID, false,
  2478. "Parameter of type '" + String(r_iarg.type.cname) + "' cannot have a default value of type '" + String(name_cache.type_RID) + "'.");
  2479. ERR_FAIL_COND_V_MSG(!p_val.is_zero(), false,
  2480. "Parameter of type '" + String(r_iarg.type.cname) + "' can only have null/zero as the default value.");
  2481. r_iarg.default_argument = "null";
  2482. break;
  2483. case Variant::ARRAY:
  2484. case Variant::PACKED_BYTE_ARRAY:
  2485. case Variant::PACKED_INT32_ARRAY:
  2486. case Variant::PACKED_INT64_ARRAY:
  2487. case Variant::PACKED_FLOAT32_ARRAY:
  2488. case Variant::PACKED_FLOAT64_ARRAY:
  2489. case Variant::PACKED_STRING_ARRAY:
  2490. case Variant::PACKED_VECTOR2_ARRAY:
  2491. case Variant::PACKED_VECTOR3_ARRAY:
  2492. case Variant::PACKED_COLOR_ARRAY:
  2493. r_iarg.default_argument = "new %s {}";
  2494. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF;
  2495. break;
  2496. case Variant::TRANSFORM2D:
  2497. case Variant::BASIS:
  2498. case Variant::QUAT:
  2499. r_iarg.default_argument = Variant::get_type_name(p_val.get_type()) + ".Identity";
  2500. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_VAL;
  2501. break;
  2502. case Variant::CALLABLE:
  2503. case Variant::SIGNAL:
  2504. CRASH_NOW_MSG("Parameter of type '" + String(r_iarg.type.cname) + "' cannot have a default value.");
  2505. break;
  2506. default:
  2507. CRASH_NOW_MSG("Unexpected Variant type: " + itos(p_val.get_type()));
  2508. break;
  2509. }
  2510. if (r_iarg.def_param_mode == ArgumentInterface::CONSTANT && r_iarg.type.cname == name_cache.type_Variant && r_iarg.default_argument != "null") {
  2511. r_iarg.def_param_mode = ArgumentInterface::NULLABLE_REF;
  2512. }
  2513. return true;
  2514. }
  2515. void BindingsGenerator::_populate_builtin_type_interfaces() {
  2516. builtin_types.clear();
  2517. TypeInterface itype;
  2518. #define INSERT_STRUCT_TYPE(m_type) \
  2519. { \
  2520. itype = TypeInterface::create_value_type(String(#m_type)); \
  2521. itype.c_in = "\t%0 %1_in = MARSHALLED_IN(" #m_type ", %1);\n"; \
  2522. itype.c_out = "\t*%3 = MARSHALLED_OUT(" #m_type ", %1);\n"; \
  2523. itype.c_arg_in = "&%s_in"; \
  2524. itype.c_type_in = "GDMonoMarshal::M_" #m_type "*"; \
  2525. itype.c_type_out = "GDMonoMarshal::M_" #m_type; \
  2526. itype.cs_in = "ref %s"; \
  2527. /* in cs_out, im_type_out (%3) includes the 'out ' part */ \
  2528. itype.cs_out = "%0(%1, %3 argRet); return argRet;"; \
  2529. itype.im_type_out = "out " + itype.cs_type; \
  2530. itype.ret_as_byref_arg = true; \
  2531. builtin_types.insert(itype.cname, itype); \
  2532. }
  2533. INSERT_STRUCT_TYPE(Vector2)
  2534. INSERT_STRUCT_TYPE(Vector2i)
  2535. INSERT_STRUCT_TYPE(Rect2)
  2536. INSERT_STRUCT_TYPE(Rect2i)
  2537. INSERT_STRUCT_TYPE(Transform2D)
  2538. INSERT_STRUCT_TYPE(Vector3)
  2539. INSERT_STRUCT_TYPE(Vector3i)
  2540. INSERT_STRUCT_TYPE(Basis)
  2541. INSERT_STRUCT_TYPE(Quat)
  2542. INSERT_STRUCT_TYPE(Transform)
  2543. INSERT_STRUCT_TYPE(AABB)
  2544. INSERT_STRUCT_TYPE(Color)
  2545. INSERT_STRUCT_TYPE(Plane)
  2546. #undef INSERT_STRUCT_TYPE
  2547. // bool
  2548. itype = TypeInterface::create_value_type(String("bool"));
  2549. {
  2550. // MonoBoolean <---> bool
  2551. itype.c_in = "\t%0 %1_in = (%0)%1;\n";
  2552. itype.c_out = "\treturn (%0)%1;\n";
  2553. itype.c_type = "bool";
  2554. itype.c_type_in = "MonoBoolean";
  2555. itype.c_type_out = itype.c_type_in;
  2556. itype.c_arg_in = "&%s_in";
  2557. }
  2558. itype.im_type_in = itype.name;
  2559. itype.im_type_out = itype.name;
  2560. builtin_types.insert(itype.cname, itype);
  2561. // Integer types
  2562. {
  2563. // C interface for 'uint32_t' is the same as that of enums. Remember to apply
  2564. // any of the changes done here to 'TypeInterface::postsetup_enum_type' as well.
  2565. #define INSERT_INT_TYPE(m_name, m_c_type_in_out, m_c_type) \
  2566. { \
  2567. itype = TypeInterface::create_value_type(String(m_name)); \
  2568. { \
  2569. itype.c_in = "\t%0 %1_in = (%0)%1;\n"; \
  2570. itype.c_out = "\treturn (%0)%1;\n"; \
  2571. itype.c_type = #m_c_type; \
  2572. itype.c_arg_in = "&%s_in"; \
  2573. } \
  2574. itype.c_type_in = #m_c_type_in_out; \
  2575. itype.c_type_out = itype.c_type_in; \
  2576. itype.im_type_in = itype.name; \
  2577. itype.im_type_out = itype.name; \
  2578. builtin_types.insert(itype.cname, itype); \
  2579. }
  2580. // The expected type for all integers in ptrcall is 'int64_t', so that's what we use for 'c_type'
  2581. INSERT_INT_TYPE("sbyte", int8_t, int64_t);
  2582. INSERT_INT_TYPE("short", int16_t, int64_t);
  2583. INSERT_INT_TYPE("int", int32_t, int64_t);
  2584. INSERT_INT_TYPE("long", int64_t, int64_t);
  2585. INSERT_INT_TYPE("byte", uint8_t, int64_t);
  2586. INSERT_INT_TYPE("ushort", uint16_t, int64_t);
  2587. INSERT_INT_TYPE("uint", uint32_t, int64_t);
  2588. INSERT_INT_TYPE("ulong", uint64_t, int64_t);
  2589. }
  2590. // Floating point types
  2591. {
  2592. // float
  2593. itype = TypeInterface();
  2594. itype.name = "float";
  2595. itype.cname = itype.name;
  2596. itype.proxy_name = "float";
  2597. {
  2598. // The expected type for 'float' in ptrcall is 'double'
  2599. itype.c_in = "\t%0 %1_in = (%0)%1;\n";
  2600. itype.c_out = "\treturn (%0)%1;\n";
  2601. itype.c_type = "double";
  2602. itype.c_type_in = "float";
  2603. itype.c_type_out = "float";
  2604. itype.c_arg_in = "&%s_in";
  2605. }
  2606. itype.cs_type = itype.proxy_name;
  2607. itype.im_type_in = itype.proxy_name;
  2608. itype.im_type_out = itype.proxy_name;
  2609. builtin_types.insert(itype.cname, itype);
  2610. // double
  2611. itype = TypeInterface();
  2612. itype.name = "double";
  2613. itype.cname = itype.name;
  2614. itype.proxy_name = "double";
  2615. {
  2616. itype.c_type = "double";
  2617. itype.c_type_in = "double";
  2618. itype.c_type_out = "double";
  2619. itype.c_arg_in = "&%s";
  2620. }
  2621. itype.cs_type = itype.proxy_name;
  2622. itype.im_type_in = itype.proxy_name;
  2623. itype.im_type_out = itype.proxy_name;
  2624. builtin_types.insert(itype.cname, itype);
  2625. }
  2626. // String
  2627. itype = TypeInterface();
  2628. itype.name = "String";
  2629. itype.cname = itype.name;
  2630. itype.proxy_name = "string";
  2631. itype.c_in = "\t%0 %1_in = " C_METHOD_MONOSTR_TO_GODOT "(%1);\n";
  2632. itype.c_out = "\treturn " C_METHOD_MONOSTR_FROM_GODOT "(%1);\n";
  2633. itype.c_arg_in = "&%s_in";
  2634. itype.c_type = itype.name;
  2635. itype.c_type_in = "MonoString*";
  2636. itype.c_type_out = "MonoString*";
  2637. itype.cs_type = itype.proxy_name;
  2638. itype.im_type_in = itype.proxy_name;
  2639. itype.im_type_out = itype.proxy_name;
  2640. builtin_types.insert(itype.cname, itype);
  2641. // StringName
  2642. itype = TypeInterface();
  2643. itype.name = "StringName";
  2644. itype.cname = itype.name;
  2645. itype.proxy_name = "StringName";
  2646. itype.c_in = "\t%0 %1_in = %1 ? *%1 : StringName();\n";
  2647. itype.c_out = "\treturn memnew(StringName(%1));\n";
  2648. itype.c_arg_in = "&%s_in";
  2649. itype.c_type = itype.name;
  2650. itype.c_type_in = itype.c_type + "*";
  2651. itype.c_type_out = itype.c_type + "*";
  2652. itype.cs_type = itype.proxy_name;
  2653. itype.cs_in = "StringName." CS_SMETHOD_GETINSTANCE "(%0)";
  2654. itype.cs_out = "return new %2(%0(%1));";
  2655. itype.im_type_in = "IntPtr";
  2656. itype.im_type_out = "IntPtr";
  2657. builtin_types.insert(itype.cname, itype);
  2658. // NodePath
  2659. itype = TypeInterface();
  2660. itype.name = "NodePath";
  2661. itype.cname = itype.name;
  2662. itype.proxy_name = "NodePath";
  2663. itype.c_out = "\treturn memnew(NodePath(%1));\n";
  2664. itype.c_type = itype.name;
  2665. itype.c_type_in = itype.c_type + "*";
  2666. itype.c_type_out = itype.c_type + "*";
  2667. itype.cs_type = itype.proxy_name;
  2668. itype.cs_in = "NodePath." CS_SMETHOD_GETINSTANCE "(%0)";
  2669. itype.cs_out = "return new %2(%0(%1));";
  2670. itype.im_type_in = "IntPtr";
  2671. itype.im_type_out = "IntPtr";
  2672. builtin_types.insert(itype.cname, itype);
  2673. // RID
  2674. itype = TypeInterface();
  2675. itype.name = "RID";
  2676. itype.cname = itype.name;
  2677. itype.proxy_name = "RID";
  2678. itype.c_out = "\treturn memnew(RID(%1));\n";
  2679. itype.c_type = itype.name;
  2680. itype.c_type_in = itype.c_type + "*";
  2681. itype.c_type_out = itype.c_type + "*";
  2682. itype.cs_type = itype.proxy_name;
  2683. itype.cs_in = "RID." CS_SMETHOD_GETINSTANCE "(%0)";
  2684. itype.cs_out = "return new %2(%0(%1));";
  2685. itype.im_type_in = "IntPtr";
  2686. itype.im_type_out = "IntPtr";
  2687. builtin_types.insert(itype.cname, itype);
  2688. // Variant
  2689. itype = TypeInterface();
  2690. itype.name = "Variant";
  2691. itype.cname = itype.name;
  2692. itype.proxy_name = "object";
  2693. itype.c_in = "\t%0 %1_in = " C_METHOD_MANAGED_TO_VARIANT "(%1);\n";
  2694. itype.c_out = "\treturn " C_METHOD_MANAGED_FROM_VARIANT "(%1);\n";
  2695. itype.c_arg_in = "&%s_in";
  2696. itype.c_type = itype.name;
  2697. itype.c_type_in = "MonoObject*";
  2698. itype.c_type_out = "MonoObject*";
  2699. itype.cs_type = itype.proxy_name;
  2700. itype.im_type_in = "object";
  2701. itype.im_type_out = itype.proxy_name;
  2702. builtin_types.insert(itype.cname, itype);
  2703. // Callable
  2704. itype = TypeInterface::create_value_type(String("Callable"));
  2705. itype.c_in = "\t%0 %1_in = " C_METHOD_MANAGED_TO_CALLABLE "(*%1);\n";
  2706. itype.c_out = "\t*%3 = " C_METHOD_MANAGED_FROM_CALLABLE "(%1);\n";
  2707. itype.c_arg_in = "&%s_in";
  2708. itype.c_type_in = "GDMonoMarshal::M_Callable*";
  2709. itype.c_type_out = "GDMonoMarshal::M_Callable";
  2710. itype.cs_in = "ref %s";
  2711. /* in cs_out, im_type_out (%3) includes the 'out ' part */
  2712. itype.cs_out = "%0(%1, %3 argRet); return argRet;";
  2713. itype.im_type_out = "out " + itype.cs_type;
  2714. itype.ret_as_byref_arg = true;
  2715. builtin_types.insert(itype.cname, itype);
  2716. // Signal
  2717. itype = TypeInterface();
  2718. itype.name = "Signal";
  2719. itype.cname = itype.name;
  2720. itype.proxy_name = "SignalInfo";
  2721. itype.c_in = "\t%0 %1_in = " C_METHOD_MANAGED_TO_SIGNAL "(*%1);\n";
  2722. itype.c_out = "\t*%3 = " C_METHOD_MANAGED_FROM_SIGNAL "(%1);\n";
  2723. itype.c_arg_in = "&%s_in";
  2724. itype.c_type = itype.name;
  2725. itype.c_type_in = "GDMonoMarshal::M_SignalInfo*";
  2726. itype.c_type_out = "GDMonoMarshal::M_SignalInfo";
  2727. itype.cs_in = "ref %s";
  2728. /* in cs_out, im_type_out (%3) includes the 'out ' part */
  2729. itype.cs_out = "%0(%1, %3 argRet); return argRet;";
  2730. itype.cs_type = itype.proxy_name;
  2731. itype.im_type_in = "ref " + itype.cs_type;
  2732. itype.im_type_out = "out " + itype.cs_type;
  2733. itype.ret_as_byref_arg = true;
  2734. builtin_types.insert(itype.cname, itype);
  2735. // VarArg (fictitious type to represent variable arguments)
  2736. itype = TypeInterface();
  2737. itype.name = "VarArg";
  2738. itype.cname = itype.name;
  2739. itype.proxy_name = "object[]";
  2740. itype.c_in = "\t%0 %1_in = " C_METHOD_MONOARRAY_TO(Array) "(%1);\n";
  2741. itype.c_arg_in = "&%s_in";
  2742. itype.c_type = "Array";
  2743. itype.c_type_in = "MonoArray*";
  2744. itype.cs_type = "params object[]";
  2745. itype.im_type_in = "object[]";
  2746. builtin_types.insert(itype.cname, itype);
  2747. #define INSERT_ARRAY_FULL(m_name, m_type, m_proxy_t) \
  2748. { \
  2749. itype = TypeInterface(); \
  2750. itype.name = #m_name; \
  2751. itype.cname = itype.name; \
  2752. itype.proxy_name = #m_proxy_t "[]"; \
  2753. itype.c_in = "\t%0 %1_in = " C_METHOD_MONOARRAY_TO(m_type) "(%1);\n"; \
  2754. itype.c_out = "\treturn " C_METHOD_MONOARRAY_FROM(m_type) "(%1);\n"; \
  2755. itype.c_arg_in = "&%s_in"; \
  2756. itype.c_type = #m_type; \
  2757. itype.c_type_in = "MonoArray*"; \
  2758. itype.c_type_out = "MonoArray*"; \
  2759. itype.cs_type = itype.proxy_name; \
  2760. itype.im_type_in = itype.proxy_name; \
  2761. itype.im_type_out = itype.proxy_name; \
  2762. builtin_types.insert(itype.name, itype); \
  2763. }
  2764. #define INSERT_ARRAY(m_type, m_proxy_t) INSERT_ARRAY_FULL(m_type, m_type, m_proxy_t)
  2765. INSERT_ARRAY(PackedInt32Array, int);
  2766. INSERT_ARRAY(PackedInt64Array, long);
  2767. INSERT_ARRAY_FULL(PackedByteArray, PackedByteArray, byte);
  2768. INSERT_ARRAY(PackedFloat32Array, float);
  2769. INSERT_ARRAY(PackedFloat64Array, double);
  2770. INSERT_ARRAY(PackedStringArray, string);
  2771. INSERT_ARRAY(PackedColorArray, Color);
  2772. INSERT_ARRAY(PackedVector2Array, Vector2);
  2773. INSERT_ARRAY(PackedVector3Array, Vector3);
  2774. #undef INSERT_ARRAY
  2775. // Array
  2776. itype = TypeInterface();
  2777. itype.name = "Array";
  2778. itype.cname = itype.name;
  2779. itype.proxy_name = itype.name;
  2780. itype.c_out = "\treturn memnew(Array(%1));\n";
  2781. itype.c_type = itype.name;
  2782. itype.c_type_in = itype.c_type + "*";
  2783. itype.c_type_out = itype.c_type + "*";
  2784. itype.cs_type = BINDINGS_NAMESPACE_COLLECTIONS "." + itype.proxy_name;
  2785. itype.cs_in = "%0." CS_SMETHOD_GETINSTANCE "()";
  2786. itype.cs_out = "return new " + itype.cs_type + "(%0(%1));";
  2787. itype.im_type_in = "IntPtr";
  2788. itype.im_type_out = "IntPtr";
  2789. builtin_types.insert(itype.cname, itype);
  2790. // Dictionary
  2791. itype = TypeInterface();
  2792. itype.name = "Dictionary";
  2793. itype.cname = itype.name;
  2794. itype.proxy_name = itype.name;
  2795. itype.c_out = "\treturn memnew(Dictionary(%1));\n";
  2796. itype.c_type = itype.name;
  2797. itype.c_type_in = itype.c_type + "*";
  2798. itype.c_type_out = itype.c_type + "*";
  2799. itype.cs_type = BINDINGS_NAMESPACE_COLLECTIONS "." + itype.proxy_name;
  2800. itype.cs_in = "%0." CS_SMETHOD_GETINSTANCE "()";
  2801. itype.cs_out = "return new " + itype.cs_type + "(%0(%1));";
  2802. itype.im_type_in = "IntPtr";
  2803. itype.im_type_out = "IntPtr";
  2804. builtin_types.insert(itype.cname, itype);
  2805. // void (fictitious type to represent the return type of methods that do not return anything)
  2806. itype = TypeInterface();
  2807. itype.name = "void";
  2808. itype.cname = itype.name;
  2809. itype.proxy_name = itype.name;
  2810. itype.c_type = itype.name;
  2811. itype.c_type_in = itype.c_type;
  2812. itype.c_type_out = itype.c_type;
  2813. itype.cs_type = itype.proxy_name;
  2814. itype.im_type_in = itype.proxy_name;
  2815. itype.im_type_out = itype.proxy_name;
  2816. builtin_types.insert(itype.cname, itype);
  2817. }
  2818. void BindingsGenerator::_populate_global_constants() {
  2819. int global_constants_count = CoreConstants::get_global_constant_count();
  2820. if (global_constants_count > 0) {
  2821. Map<String, DocData::ClassDoc>::Element *match = EditorHelp::get_doc_data()->class_list.find("@GlobalScope");
  2822. CRASH_COND_MSG(!match, "Could not find '@GlobalScope' in DocData.");
  2823. const DocData::ClassDoc &global_scope_doc = match->value();
  2824. for (int i = 0; i < global_constants_count; i++) {
  2825. String constant_name = CoreConstants::get_global_constant_name(i);
  2826. const DocData::ConstantDoc *const_doc = nullptr;
  2827. for (int j = 0; j < global_scope_doc.constants.size(); j++) {
  2828. const DocData::ConstantDoc &curr_const_doc = global_scope_doc.constants[j];
  2829. if (curr_const_doc.name == constant_name) {
  2830. const_doc = &curr_const_doc;
  2831. break;
  2832. }
  2833. }
  2834. int constant_value = CoreConstants::get_global_constant_value(i);
  2835. StringName enum_name = CoreConstants::get_global_constant_enum(i);
  2836. ConstantInterface iconstant(constant_name, snake_to_pascal_case(constant_name, true), constant_value);
  2837. iconstant.const_doc = const_doc;
  2838. if (enum_name != StringName()) {
  2839. EnumInterface ienum(enum_name);
  2840. List<EnumInterface>::Element *enum_match = global_enums.find(ienum);
  2841. if (enum_match) {
  2842. enum_match->get().constants.push_back(iconstant);
  2843. } else {
  2844. ienum.constants.push_back(iconstant);
  2845. global_enums.push_back(ienum);
  2846. }
  2847. } else {
  2848. global_constants.push_back(iconstant);
  2849. }
  2850. }
  2851. for (List<EnumInterface>::Element *E = global_enums.front(); E; E = E->next()) {
  2852. EnumInterface &ienum = E->get();
  2853. TypeInterface enum_itype;
  2854. enum_itype.is_enum = true;
  2855. enum_itype.name = ienum.cname.operator String();
  2856. enum_itype.cname = ienum.cname;
  2857. enum_itype.proxy_name = enum_itype.name;
  2858. TypeInterface::postsetup_enum_type(enum_itype);
  2859. enum_types.insert(enum_itype.cname, enum_itype);
  2860. int prefix_length = _determine_enum_prefix(ienum);
  2861. // HARDCODED: The Error enum have the prefix 'ERR_' for everything except 'OK' and 'FAILED'.
  2862. if (ienum.cname == name_cache.enum_Error) {
  2863. if (prefix_length > 0) { // Just in case it ever changes
  2864. ERR_PRINT("Prefix for enum '" _STR(Error) "' is not empty.");
  2865. }
  2866. prefix_length = 1; // 'ERR_'
  2867. }
  2868. _apply_prefix_to_enum_constants(ienum, prefix_length);
  2869. }
  2870. }
  2871. // HARDCODED
  2872. List<StringName> hardcoded_enums;
  2873. hardcoded_enums.push_back("Vector2.Axis");
  2874. hardcoded_enums.push_back("Vector2i.Axis");
  2875. hardcoded_enums.push_back("Vector3.Axis");
  2876. hardcoded_enums.push_back("Vector3i.Axis");
  2877. for (List<StringName>::Element *E = hardcoded_enums.front(); E; E = E->next()) {
  2878. // These enums are not generated and must be written manually (e.g.: Vector3.Axis)
  2879. // Here, we assume core types do not begin with underscore
  2880. TypeInterface enum_itype;
  2881. enum_itype.is_enum = true;
  2882. enum_itype.name = E->get().operator String();
  2883. enum_itype.cname = E->get();
  2884. enum_itype.proxy_name = enum_itype.name;
  2885. TypeInterface::postsetup_enum_type(enum_itype);
  2886. enum_types.insert(enum_itype.cname, enum_itype);
  2887. }
  2888. }
  2889. void BindingsGenerator::_initialize_blacklisted_methods() {
  2890. blacklisted_methods["Object"].push_back("to_string"); // there is already ToString
  2891. blacklisted_methods["Object"].push_back("_to_string"); // override ToString instead
  2892. blacklisted_methods["Object"].push_back("_init"); // never called in C# (TODO: implement it)
  2893. }
  2894. void BindingsGenerator::_log(const char *p_format, ...) {
  2895. if (log_print_enabled) {
  2896. va_list list;
  2897. va_start(list, p_format);
  2898. OS::get_singleton()->print("%s", str_format(p_format, list).utf8().get_data());
  2899. va_end(list);
  2900. }
  2901. }
  2902. void BindingsGenerator::_initialize() {
  2903. initialized = false;
  2904. EditorHelp::generate_doc();
  2905. enum_types.clear();
  2906. _initialize_blacklisted_methods();
  2907. bool obj_type_ok = _populate_object_type_interfaces();
  2908. ERR_FAIL_COND_MSG(!obj_type_ok, "Failed to generate object type interfaces");
  2909. _populate_builtin_type_interfaces();
  2910. _populate_global_constants();
  2911. // Generate internal calls (after populating type interfaces and global constants)
  2912. core_custom_icalls.clear();
  2913. editor_custom_icalls.clear();
  2914. for (OrderedHashMap<StringName, TypeInterface>::Element E = obj_types.front(); E; E = E.next()) {
  2915. _generate_method_icalls(E.get());
  2916. }
  2917. initialized = true;
  2918. }
  2919. void BindingsGenerator::handle_cmdline_args(const List<String> &p_cmdline_args) {
  2920. const int NUM_OPTIONS = 2;
  2921. String generate_all_glue_option = "--generate-mono-glue";
  2922. String generate_cs_glue_option = "--generate-mono-cs-glue";
  2923. String generate_cpp_glue_option = "--generate-mono-cpp-glue";
  2924. String glue_dir_path;
  2925. String cs_dir_path;
  2926. String cpp_dir_path;
  2927. int options_left = NUM_OPTIONS;
  2928. const List<String>::Element *elem = p_cmdline_args.front();
  2929. while (elem && options_left) {
  2930. if (elem->get() == generate_all_glue_option) {
  2931. const List<String>::Element *path_elem = elem->next();
  2932. if (path_elem) {
  2933. glue_dir_path = path_elem->get();
  2934. elem = elem->next();
  2935. } else {
  2936. ERR_PRINT(generate_all_glue_option + ": No output directory specified (expected path to '{GODOT_ROOT}/modules/mono/glue').");
  2937. }
  2938. --options_left;
  2939. } else if (elem->get() == generate_cs_glue_option) {
  2940. const List<String>::Element *path_elem = elem->next();
  2941. if (path_elem) {
  2942. cs_dir_path = path_elem->get();
  2943. elem = elem->next();
  2944. } else {
  2945. ERR_PRINT(generate_cs_glue_option + ": No output directory specified.");
  2946. }
  2947. --options_left;
  2948. } else if (elem->get() == generate_cpp_glue_option) {
  2949. const List<String>::Element *path_elem = elem->next();
  2950. if (path_elem) {
  2951. cpp_dir_path = path_elem->get();
  2952. elem = elem->next();
  2953. } else {
  2954. ERR_PRINT(generate_cpp_glue_option + ": No output directory specified.");
  2955. }
  2956. --options_left;
  2957. }
  2958. elem = elem->next();
  2959. }
  2960. if (glue_dir_path.length() || cs_dir_path.length() || cpp_dir_path.length()) {
  2961. BindingsGenerator bindings_generator;
  2962. bindings_generator.set_log_print_enabled(true);
  2963. if (!bindings_generator.initialized) {
  2964. ERR_PRINT("Failed to initialize the bindings generator");
  2965. ::exit(0);
  2966. }
  2967. if (glue_dir_path.length()) {
  2968. if (bindings_generator.generate_glue(glue_dir_path) != OK) {
  2969. ERR_PRINT(generate_all_glue_option + ": Failed to generate the C++ glue.");
  2970. }
  2971. if (bindings_generator.generate_cs_api(glue_dir_path.plus_file(API_SOLUTION_NAME)) != OK) {
  2972. ERR_PRINT(generate_all_glue_option + ": Failed to generate the C# API.");
  2973. }
  2974. }
  2975. if (cs_dir_path.length()) {
  2976. if (bindings_generator.generate_cs_api(cs_dir_path) != OK) {
  2977. ERR_PRINT(generate_cs_glue_option + ": Failed to generate the C# API.");
  2978. }
  2979. }
  2980. if (cpp_dir_path.length()) {
  2981. if (bindings_generator.generate_glue(cpp_dir_path) != OK) {
  2982. ERR_PRINT(generate_cpp_glue_option + ": Failed to generate the C++ glue.");
  2983. }
  2984. }
  2985. // Exit once done
  2986. ::exit(0);
  2987. }
  2988. }
  2989. #endif