main.cpp 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716
  1. /**************************************************************************/
  2. /* main.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "main.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_globals.h"
  33. #include "core/crypto/crypto.h"
  34. #include "core/debugger/engine_debugger.h"
  35. #include "core/extension/extension_api_dump.h"
  36. #include "core/extension/gdextension_interface_dump.gen.h"
  37. #include "core/extension/gdextension_manager.h"
  38. #include "core/input/input.h"
  39. #include "core/input/input_map.h"
  40. #include "core/io/dir_access.h"
  41. #include "core/io/file_access_pack.h"
  42. #include "core/io/file_access_zip.h"
  43. #include "core/io/image.h"
  44. #include "core/io/image_loader.h"
  45. #include "core/io/ip.h"
  46. #include "core/io/resource_loader.h"
  47. #include "core/object/message_queue.h"
  48. #include "core/os/os.h"
  49. #include "core/os/time.h"
  50. #include "core/register_core_types.h"
  51. #include "core/string/translation_server.h"
  52. #include "core/version.h"
  53. #include "drivers/register_driver_types.h"
  54. #include "main/app_icon.gen.h"
  55. #include "main/main_timer_sync.h"
  56. #include "main/performance.h"
  57. #include "main/splash.gen.h"
  58. #include "modules/register_module_types.h"
  59. #include "platform/register_platform_apis.h"
  60. #include "scene/main/scene_tree.h"
  61. #include "scene/main/window.h"
  62. #include "scene/property_list_helper.h"
  63. #include "scene/register_scene_types.h"
  64. #include "scene/resources/packed_scene.h"
  65. #include "scene/theme/theme_db.h"
  66. #include "servers/audio_server.h"
  67. #include "servers/camera_server.h"
  68. #include "servers/display_server.h"
  69. #include "servers/movie_writer/movie_writer.h"
  70. #include "servers/movie_writer/movie_writer_mjpeg.h"
  71. #include "servers/navigation_server_3d.h"
  72. #include "servers/navigation_server_3d_dummy.h"
  73. #include "servers/register_server_types.h"
  74. #include "servers/rendering/rendering_server_default.h"
  75. #include "servers/text/text_server_dummy.h"
  76. #include "servers/text_server.h"
  77. // 2D
  78. #include "servers/navigation_server_2d.h"
  79. #include "servers/navigation_server_2d_dummy.h"
  80. #include "servers/physics_server_2d.h"
  81. #include "servers/physics_server_2d_dummy.h"
  82. #ifndef _3D_DISABLED
  83. #include "servers/physics_server_3d.h"
  84. #include "servers/physics_server_3d_dummy.h"
  85. #include "servers/xr_server.h"
  86. #endif // _3D_DISABLED
  87. #ifdef TESTS_ENABLED
  88. #include "tests/test_main.h"
  89. #endif
  90. #ifdef TOOLS_ENABLED
  91. #include "editor/debugger/debug_adapter/debug_adapter_server.h"
  92. #include "editor/debugger/editor_debugger_node.h"
  93. #include "editor/doc_data_class_path.gen.h"
  94. #include "editor/doc_tools.h"
  95. #include "editor/editor_file_system.h"
  96. #include "editor/editor_help.h"
  97. #include "editor/editor_node.h"
  98. #include "editor/editor_paths.h"
  99. #include "editor/editor_settings.h"
  100. #include "editor/editor_translation.h"
  101. #include "editor/progress_dialog.h"
  102. #include "editor/project_manager.h"
  103. #include "editor/register_editor_types.h"
  104. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  105. #include "main/splash_editor.gen.h"
  106. #endif
  107. #ifndef DISABLE_DEPRECATED
  108. #include "editor/project_converter_3_to_4.h"
  109. #endif // DISABLE_DEPRECATED
  110. #endif // TOOLS_ENABLED
  111. #if defined(STEAMAPI_ENABLED)
  112. #include "main/steam_tracker.h"
  113. #endif
  114. #include "modules/modules_enabled.gen.h" // For mono.
  115. #if defined(MODULE_MONO_ENABLED) && defined(TOOLS_ENABLED)
  116. #include "modules/mono/editor/bindings_generator.h"
  117. #endif
  118. #ifdef MODULE_GDSCRIPT_ENABLED
  119. #include "modules/gdscript/gdscript.h"
  120. #if defined(TOOLS_ENABLED) && !defined(GDSCRIPT_NO_LSP)
  121. #include "modules/gdscript/language_server/gdscript_language_server.h"
  122. #endif // TOOLS_ENABLED && !GDSCRIPT_NO_LSP
  123. #endif // MODULE_GDSCRIPT_ENABLED
  124. /* Static members */
  125. // Singletons
  126. // Initialized in setup()
  127. static Engine *engine = nullptr;
  128. static ProjectSettings *globals = nullptr;
  129. static Input *input = nullptr;
  130. static InputMap *input_map = nullptr;
  131. static TranslationServer *translation_server = nullptr;
  132. static Performance *performance = nullptr;
  133. static PackedData *packed_data = nullptr;
  134. #ifdef MINIZIP_ENABLED
  135. static ZipArchive *zip_packed_data = nullptr;
  136. #endif
  137. static MessageQueue *message_queue = nullptr;
  138. #if defined(STEAMAPI_ENABLED)
  139. static SteamTracker *steam_tracker = nullptr;
  140. #endif
  141. // Initialized in setup2()
  142. static AudioServer *audio_server = nullptr;
  143. static CameraServer *camera_server = nullptr;
  144. static DisplayServer *display_server = nullptr;
  145. static RenderingServer *rendering_server = nullptr;
  146. static TextServerManager *tsman = nullptr;
  147. static ThemeDB *theme_db = nullptr;
  148. static NavigationServer2D *navigation_server_2d = nullptr;
  149. static PhysicsServer2DManager *physics_server_2d_manager = nullptr;
  150. static PhysicsServer2D *physics_server_2d = nullptr;
  151. static NavigationServer3D *navigation_server_3d = nullptr;
  152. #ifndef _3D_DISABLED
  153. static PhysicsServer3DManager *physics_server_3d_manager = nullptr;
  154. static PhysicsServer3D *physics_server_3d = nullptr;
  155. static XRServer *xr_server = nullptr;
  156. #endif // _3D_DISABLED
  157. // We error out if setup2() doesn't turn this true
  158. static bool _start_success = false;
  159. // Drivers
  160. String display_driver = "";
  161. String tablet_driver = "";
  162. String text_driver = "";
  163. String rendering_driver = "";
  164. String rendering_method = "";
  165. static int text_driver_idx = -1;
  166. static int audio_driver_idx = -1;
  167. // Engine config/tools
  168. static bool single_window = false;
  169. static bool editor = false;
  170. static bool project_manager = false;
  171. static bool cmdline_tool = false;
  172. static String locale;
  173. static String log_file;
  174. static bool show_help = false;
  175. static uint64_t quit_after = 0;
  176. static OS::ProcessID editor_pid = 0;
  177. #ifdef TOOLS_ENABLED
  178. static bool found_project = false;
  179. static bool auto_build_solutions = false;
  180. static String debug_server_uri;
  181. static bool wait_for_import = false;
  182. static bool restore_editor_window_layout = true;
  183. #ifndef DISABLE_DEPRECATED
  184. static int converter_max_kb_file = 4 * 1024; // 4MB
  185. static int converter_max_line_length = 100000;
  186. #endif // DISABLE_DEPRECATED
  187. HashMap<Main::CLIScope, Vector<String>> forwardable_cli_arguments;
  188. #endif
  189. static bool single_threaded_scene = false;
  190. // Display
  191. static DisplayServer::WindowMode window_mode = DisplayServer::WINDOW_MODE_WINDOWED;
  192. static DisplayServer::ScreenOrientation window_orientation = DisplayServer::SCREEN_LANDSCAPE;
  193. static DisplayServer::VSyncMode window_vsync_mode = DisplayServer::VSYNC_ENABLED;
  194. static uint32_t window_flags = 0;
  195. static Size2i window_size = Size2i(1152, 648);
  196. static int init_screen = DisplayServer::SCREEN_PRIMARY;
  197. static bool init_fullscreen = false;
  198. static bool init_maximized = false;
  199. static bool init_windowed = false;
  200. static bool init_always_on_top = false;
  201. static bool init_use_custom_pos = false;
  202. static bool init_use_custom_screen = false;
  203. static Vector2 init_custom_pos;
  204. // Debug
  205. static bool use_debug_profiler = false;
  206. #ifdef DEBUG_ENABLED
  207. static bool debug_collisions = false;
  208. static bool debug_paths = false;
  209. static bool debug_navigation = false;
  210. static bool debug_avoidance = false;
  211. static bool debug_canvas_item_redraw = false;
  212. #endif
  213. static int max_fps = -1;
  214. static int frame_delay = 0;
  215. static int audio_output_latency = 0;
  216. static bool disable_render_loop = false;
  217. static int fixed_fps = -1;
  218. static MovieWriter *movie_writer = nullptr;
  219. static bool disable_vsync = false;
  220. static bool print_fps = false;
  221. #ifdef TOOLS_ENABLED
  222. static bool editor_pseudolocalization = false;
  223. static bool dump_gdextension_interface = false;
  224. static bool dump_extension_api = false;
  225. static bool include_docs_in_extension_api_dump = false;
  226. static bool validate_extension_api = false;
  227. static String validate_extension_api_file;
  228. #endif
  229. bool profile_gpu = false;
  230. // Constants.
  231. static const String NULL_DISPLAY_DRIVER("headless");
  232. static const String NULL_AUDIO_DRIVER("Dummy");
  233. // The length of the longest column in the command-line help we should align to
  234. // (excluding the 2-space left and right margins).
  235. // Currently, this is `--export-release <preset> <path>`.
  236. static const int OPTION_COLUMN_LENGTH = 32;
  237. /* Helper methods */
  238. bool Main::is_cmdline_tool() {
  239. return cmdline_tool;
  240. }
  241. #ifdef TOOLS_ENABLED
  242. const Vector<String> &Main::get_forwardable_cli_arguments(Main::CLIScope p_scope) {
  243. return forwardable_cli_arguments[p_scope];
  244. }
  245. #endif
  246. static String unescape_cmdline(const String &p_str) {
  247. return p_str.replace("%20", " ");
  248. }
  249. static String get_full_version_string() {
  250. String hash = String(VERSION_HASH);
  251. if (!hash.is_empty()) {
  252. hash = "." + hash.left(9);
  253. }
  254. return String(VERSION_FULL_BUILD) + hash;
  255. }
  256. #if defined(TOOLS_ENABLED) && defined(MODULE_GDSCRIPT_ENABLED)
  257. static Vector<String> get_files_with_extension(const String &p_root, const String &p_extension) {
  258. Vector<String> paths;
  259. Ref<DirAccess> dir = DirAccess::open(p_root);
  260. if (dir.is_valid()) {
  261. dir->list_dir_begin();
  262. String fn = dir->get_next();
  263. while (!fn.is_empty()) {
  264. if (!dir->current_is_hidden() && fn != "." && fn != "..") {
  265. if (dir->current_is_dir()) {
  266. paths.append_array(get_files_with_extension(p_root.path_join(fn), p_extension));
  267. } else if (fn.get_extension() == p_extension) {
  268. paths.append(p_root.path_join(fn));
  269. }
  270. }
  271. fn = dir->get_next();
  272. }
  273. dir->list_dir_end();
  274. }
  275. return paths;
  276. }
  277. #endif
  278. // FIXME: Could maybe be moved to have less code in main.cpp.
  279. void initialize_physics() {
  280. #ifndef _3D_DISABLED
  281. /// 3D Physics Server
  282. physics_server_3d = PhysicsServer3DManager::get_singleton()->new_server(
  283. GLOBAL_GET(PhysicsServer3DManager::setting_property_name));
  284. if (!physics_server_3d) {
  285. // Physics server not found, Use the default physics
  286. physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server();
  287. }
  288. // Fall back to dummy if no default server has been registered.
  289. if (!physics_server_3d) {
  290. WARN_PRINT(vformat("Falling back to dummy PhysicsServer3D; 3D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer3DManager::setting_property_name));
  291. physics_server_3d = memnew(PhysicsServer3DDummy);
  292. }
  293. // Should be impossible, but make sure it's not null.
  294. ERR_FAIL_NULL_MSG(physics_server_3d, "Failed to initialize PhysicsServer3D.");
  295. physics_server_3d->init();
  296. #endif // _3D_DISABLED
  297. // 2D Physics server
  298. physics_server_2d = PhysicsServer2DManager::get_singleton()->new_server(
  299. GLOBAL_GET(PhysicsServer2DManager::get_singleton()->setting_property_name));
  300. if (!physics_server_2d) {
  301. // Physics server not found, Use the default physics
  302. physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
  303. }
  304. // Fall back to dummy if no default server has been registered.
  305. if (!physics_server_2d) {
  306. WARN_PRINT(vformat("Falling back to dummy PhysicsServer2D; 2D physics functionality will be disabled. If this is intended, set the %s project setting to Dummy.", PhysicsServer2DManager::setting_property_name));
  307. physics_server_2d = memnew(PhysicsServer2DDummy);
  308. }
  309. // Should be impossible, but make sure it's not null.
  310. ERR_FAIL_NULL_MSG(physics_server_2d, "Failed to initialize PhysicsServer2D.");
  311. physics_server_2d->init();
  312. }
  313. void finalize_physics() {
  314. #ifndef _3D_DISABLED
  315. physics_server_3d->finish();
  316. memdelete(physics_server_3d);
  317. #endif // _3D_DISABLED
  318. physics_server_2d->finish();
  319. memdelete(physics_server_2d);
  320. }
  321. void finalize_display() {
  322. rendering_server->finish();
  323. memdelete(rendering_server);
  324. memdelete(display_server);
  325. }
  326. void initialize_navigation_server() {
  327. ERR_FAIL_COND(navigation_server_3d != nullptr);
  328. ERR_FAIL_COND(navigation_server_2d != nullptr);
  329. // Init 3D Navigation Server
  330. navigation_server_3d = NavigationServer3DManager::new_default_server();
  331. // Fall back to dummy if no default server has been registered.
  332. if (!navigation_server_3d) {
  333. navigation_server_3d = memnew(NavigationServer3DDummy);
  334. }
  335. // Should be impossible, but make sure it's not null.
  336. ERR_FAIL_NULL_MSG(navigation_server_3d, "Failed to initialize NavigationServer3D.");
  337. navigation_server_3d->init();
  338. // Init 2D Navigation Server
  339. navigation_server_2d = NavigationServer2DManager::new_default_server();
  340. if (!navigation_server_2d) {
  341. navigation_server_2d = memnew(NavigationServer2DDummy);
  342. }
  343. ERR_FAIL_NULL_MSG(navigation_server_2d, "Failed to initialize NavigationServer2D.");
  344. navigation_server_2d->init();
  345. }
  346. void finalize_navigation_server() {
  347. ERR_FAIL_NULL(navigation_server_3d);
  348. navigation_server_3d->finish();
  349. memdelete(navigation_server_3d);
  350. navigation_server_3d = nullptr;
  351. ERR_FAIL_NULL(navigation_server_2d);
  352. navigation_server_2d->finish();
  353. memdelete(navigation_server_2d);
  354. navigation_server_2d = nullptr;
  355. }
  356. void initialize_theme_db() {
  357. theme_db = memnew(ThemeDB);
  358. }
  359. void finalize_theme_db() {
  360. memdelete(theme_db);
  361. theme_db = nullptr;
  362. }
  363. //#define DEBUG_INIT
  364. #ifdef DEBUG_INIT
  365. #define MAIN_PRINT(m_txt) print_line(m_txt)
  366. #else
  367. #define MAIN_PRINT(m_txt)
  368. #endif
  369. void Main::print_header(bool p_rich) {
  370. if (VERSION_TIMESTAMP > 0) {
  371. // Version timestamp available.
  372. if (p_rich) {
  373. Engine::get_singleton()->print_header_rich("\u001b[38;5;39m" + String(VERSION_NAME) + "\u001b[0m v" + get_full_version_string() + " (" + Time::get_singleton()->get_datetime_string_from_unix_time(VERSION_TIMESTAMP, true) + " UTC) - \u001b[4m" + String(VERSION_WEBSITE));
  374. } else {
  375. Engine::get_singleton()->print_header(String(VERSION_NAME) + " v" + get_full_version_string() + " (" + Time::get_singleton()->get_datetime_string_from_unix_time(VERSION_TIMESTAMP, true) + " UTC) - " + String(VERSION_WEBSITE));
  376. }
  377. } else {
  378. if (p_rich) {
  379. Engine::get_singleton()->print_header_rich("\u001b[38;5;39m" + String(VERSION_NAME) + "\u001b[0m v" + get_full_version_string() + " - \u001b[4m" + String(VERSION_WEBSITE));
  380. } else {
  381. Engine::get_singleton()->print_header(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
  382. }
  383. }
  384. }
  385. /**
  386. * Prints a copyright notice in the command-line help with colored text. A newline is
  387. * automatically added at the end.
  388. */
  389. void Main::print_help_copyright(const char *p_notice) {
  390. OS::get_singleton()->print("\u001b[90m%s\u001b[0m\n", p_notice);
  391. }
  392. /**
  393. * Prints a title in the command-line help with colored text. A newline is
  394. * automatically added at beginning and at the end.
  395. */
  396. void Main::print_help_title(const char *p_title) {
  397. OS::get_singleton()->print("\n\u001b[1;93m%s:\u001b[0m\n", p_title);
  398. }
  399. /**
  400. * Returns the option string with required and optional arguments colored separately from the rest of the option.
  401. * This color replacement must be done *after* calling `rpad()` for the length padding to be done correctly.
  402. */
  403. String Main::format_help_option(const char *p_option) {
  404. return (String(p_option)
  405. .rpad(OPTION_COLUMN_LENGTH)
  406. .replace("[", "\u001b[96m[")
  407. .replace("]", "]\u001b[0m")
  408. .replace("<", "\u001b[95m<")
  409. .replace(">", ">\u001b[0m"));
  410. }
  411. /**
  412. * Prints an option in the command-line help with colored text. No newline is
  413. * added at the end. `p_availability` denotes which build types the argument is
  414. * available in. Support in release export templates implies support in debug
  415. * export templates and editor. Support in debug export templates implies
  416. * support in editor.
  417. */
  418. void Main::print_help_option(const char *p_option, const char *p_description, CLIOptionAvailability p_availability) {
  419. const bool option_empty = (p_option && !p_option[0]);
  420. if (!option_empty) {
  421. const char *availability_badge = "";
  422. switch (p_availability) {
  423. case CLI_OPTION_AVAILABILITY_EDITOR:
  424. availability_badge = "\u001b[1;91mE";
  425. break;
  426. case CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG:
  427. availability_badge = "\u001b[1;94mD";
  428. break;
  429. case CLI_OPTION_AVAILABILITY_TEMPLATE_RELEASE:
  430. availability_badge = "\u001b[1;92mR";
  431. break;
  432. case CLI_OPTION_AVAILABILITY_HIDDEN:
  433. // Use for multiline option names (but not when the option name is empty).
  434. availability_badge = " ";
  435. break;
  436. }
  437. OS::get_singleton()->print(
  438. " \u001b[92m%s %s\u001b[0m %s",
  439. format_help_option(p_option).utf8().ptr(),
  440. availability_badge,
  441. p_description);
  442. } else {
  443. // Make continuation lines for descriptions faint if the option name is empty.
  444. OS::get_singleton()->print(
  445. " \u001b[92m%s \u001b[0m \u001b[90m%s",
  446. format_help_option(p_option).utf8().ptr(),
  447. p_description);
  448. }
  449. }
  450. void Main::print_help(const char *p_binary) {
  451. print_header(true);
  452. print_help_copyright("Free and open source software under the terms of the MIT license.");
  453. print_help_copyright("(c) 2014-present Godot Engine contributors. (c) 2007-present Juan Linietsky, Ariel Manzur.");
  454. print_help_title("Usage");
  455. OS::get_singleton()->print(" %s \u001b[96m[options] [path to scene or \"project.godot\" file]\u001b[0m\n", p_binary);
  456. #if defined(TOOLS_ENABLED)
  457. print_help_title("Option legend (this build = editor)");
  458. #elif defined(DEBUG_ENABLED)
  459. print_help_title("Option legend (this build = debug export template)");
  460. #else
  461. print_help_title("Option legend (this build = release export template)");
  462. #endif
  463. OS::get_singleton()->print(" \u001b[1;92mR\u001b[0m Available in editor builds, debug export templates and release export templates.\n");
  464. #ifdef DEBUG_ENABLED
  465. OS::get_singleton()->print(" \u001b[1;94mD\u001b[0m Available in editor builds and debug export templates only.\n");
  466. #endif
  467. #ifdef TOOLS_ENABLED
  468. OS::get_singleton()->print(" \u001b[1;91mE\u001b[0m Only available in editor builds.\n");
  469. #endif
  470. print_help_title("General options");
  471. print_help_option("-h, --help", "Display this help message.\n");
  472. print_help_option("--version", "Display the version string.\n");
  473. print_help_option("-v, --verbose", "Use verbose stdout mode.\n");
  474. print_help_option("--quiet", "Quiet mode, silences stdout messages. Errors are still displayed.\n");
  475. print_help_option("--no-header", "Do not print engine version and rendering method header on startup.\n");
  476. print_help_title("Run options");
  477. print_help_option("--, ++", "Separator for user-provided arguments. Following arguments are not used by the engine, but can be read from `OS.get_cmdline_user_args()`.\n");
  478. #ifdef TOOLS_ENABLED
  479. print_help_option("-e, --editor", "Start the editor instead of running the scene.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  480. print_help_option("-p, --project-manager", "Start the project manager, even if a project is auto-detected.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  481. print_help_option("--debug-server <uri>", "Start the editor debug server (<protocol>://<host/IP>[:port], e.g. tcp://127.0.0.1:6007)\n", CLI_OPTION_AVAILABILITY_EDITOR);
  482. print_help_option("--dap-port <port>", "Use the specified port for the GDScript Debugger Adaptor protocol. Recommended port range [1024, 49151].\n", CLI_OPTION_AVAILABILITY_EDITOR);
  483. #if defined(MODULE_GDSCRIPT_ENABLED) && !defined(GDSCRIPT_NO_LSP)
  484. print_help_option("--lsp-port <port>", "Use the specified port for the GDScript language server protocol. Recommended port range [1024, 49151].\n", CLI_OPTION_AVAILABILITY_EDITOR);
  485. #endif // MODULE_GDSCRIPT_ENABLED && !GDSCRIPT_NO_LSP
  486. #endif
  487. print_help_option("--quit", "Quit after the first iteration.\n");
  488. print_help_option("--quit-after <int>", "Quit after the given number of iterations. Set to 0 to disable.\n");
  489. print_help_option("-l, --language <locale>", "Use a specific locale (<locale> being a two-letter code).\n");
  490. print_help_option("--path <directory>", "Path to a project (<directory> must contain a \"project.godot\" file).\n");
  491. print_help_option("-u, --upwards", "Scan folders upwards for project.godot file.\n");
  492. print_help_option("--main-pack <file>", "Path to a pack (.pck) file to load.\n");
  493. print_help_option("--render-thread <mode>", "Render thread mode (\"unsafe\", \"safe\", \"separate\").\n");
  494. print_help_option("--remote-fs <address>", "Remote filesystem (<host/IP>[:<port>] address).\n");
  495. print_help_option("--remote-fs-password <password>", "Password for remote filesystem.\n");
  496. print_help_option("--audio-driver <driver>", "Audio driver [");
  497. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  498. if (i > 0) {
  499. OS::get_singleton()->print(", ");
  500. }
  501. OS::get_singleton()->print("\"%s\"", AudioDriverManager::get_driver(i)->get_name());
  502. }
  503. OS::get_singleton()->print("].\n");
  504. print_help_option("--display-driver <driver>", "Display driver (and rendering driver) [");
  505. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  506. if (i > 0) {
  507. OS::get_singleton()->print(", ");
  508. }
  509. OS::get_singleton()->print("\"%s\" (", DisplayServer::get_create_function_name(i));
  510. Vector<String> rd = DisplayServer::get_create_function_rendering_drivers(i);
  511. for (int j = 0; j < rd.size(); j++) {
  512. if (j > 0) {
  513. OS::get_singleton()->print(", ");
  514. }
  515. OS::get_singleton()->print("\"%s\"", rd[j].utf8().get_data());
  516. }
  517. OS::get_singleton()->print(")");
  518. }
  519. OS::get_singleton()->print("].\n");
  520. print_help_option("--audio-output-latency <ms>", "Override audio output latency in milliseconds (default is 15 ms).\n");
  521. print_help_option("", "Lower values make sound playback more reactive but increase CPU usage, and may result in audio cracking if the CPU can't keep up.\n");
  522. print_help_option("--rendering-method <renderer>", "Renderer name. Requires driver support.\n");
  523. print_help_option("--rendering-driver <driver>", "Rendering driver (depends on display driver).\n");
  524. print_help_option("--gpu-index <device_index>", "Use a specific GPU (run with --verbose to get a list of available devices).\n");
  525. print_help_option("--text-driver <driver>", "Text driver (used for font rendering, bidirectional support and shaping).\n");
  526. print_help_option("--tablet-driver <driver>", "Pen tablet input driver.\n");
  527. print_help_option("--headless", "Enable headless mode (--display-driver headless --audio-driver Dummy). Useful for servers and with --script.\n");
  528. print_help_option("--log-file <file>", "Write output/error log to the specified path instead of the default location defined by the project.\n");
  529. print_help_option("", "<file> path should be absolute or relative to the project directory.\n");
  530. print_help_option("--write-movie <file>", "Write a video to the specified path (usually with .avi or .png extension).\n");
  531. print_help_option("", "--fixed-fps is forced when enabled, but it can be used to change movie FPS.\n");
  532. print_help_option("", "--disable-vsync can speed up movie writing but makes interaction more difficult.\n");
  533. print_help_option("", "--quit-after can be used to specify the number of frames to write.\n");
  534. print_help_title("Display options");
  535. print_help_option("-f, --fullscreen", "Request fullscreen mode.\n");
  536. print_help_option("-m, --maximized", "Request a maximized window.\n");
  537. print_help_option("-w, --windowed", "Request windowed mode.\n");
  538. print_help_option("-t, --always-on-top", "Request an always-on-top window.\n");
  539. print_help_option("--resolution <W>x<H>", "Request window resolution.\n");
  540. print_help_option("--position <X>,<Y>", "Request window position.\n");
  541. print_help_option("--screen <N>", "Request window screen.\n");
  542. print_help_option("--single-window", "Use a single window (no separate subwindows).\n");
  543. #ifndef _3D_DISABLED
  544. print_help_option("--xr-mode <mode>", "Select XR (Extended Reality) mode [\"default\", \"off\", \"on\"].\n");
  545. #endif
  546. print_help_title("Debug options");
  547. print_help_option("-d, --debug", "Debug (local stdout debugger).\n");
  548. print_help_option("-b, --breakpoints", "Breakpoint list as source::line comma-separated pairs, no spaces (use %%20 instead).\n");
  549. print_help_option("--profiling", "Enable profiling in the script debugger.\n");
  550. print_help_option("--gpu-profile", "Show a GPU profile of the tasks that took the most time during frame rendering.\n");
  551. print_help_option("--gpu-validation", "Enable graphics API validation layers for debugging.\n");
  552. #ifdef DEBUG_ENABLED
  553. print_help_option("--gpu-abort", "Abort on graphics API usage errors (usually validation layer errors). May help see the problem if your system freezes.\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  554. #endif
  555. print_help_option("--generate-spirv-debug-info", "Generate SPIR-V debug information. This allows source-level shader debugging with RenderDoc.\n");
  556. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  557. print_help_option("--extra-gpu-memory-tracking", "Enables additional memory tracking (see class reference for `RenderingDevice.get_driver_and_device_memory_report()` and linked methods). Currently only implemented for Vulkan. Enabling this feature may cause crashes on some systems due to buggy drivers or bugs in the Vulkan Loader. See https://github.com/godotengine/godot/issues/95967\n");
  558. print_help_option("--accurate-breadcrumbs", "Force barriers between breadcrumbs. Useful for narrowing down a command causing GPU resets. Currently only implemented for Vulkan.\n");
  559. #endif
  560. print_help_option("--remote-debug <uri>", "Remote debug (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007).\n");
  561. print_help_option("--single-threaded-scene", "Force scene tree to run in single-threaded mode. Sub-thread groups are disabled and run on the main thread.\n");
  562. #if defined(DEBUG_ENABLED)
  563. print_help_option("--debug-collisions", "Show collision shapes when running the scene.\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  564. print_help_option("--debug-paths", "Show path lines when running the scene.\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  565. print_help_option("--debug-navigation", "Show navigation polygons when running the scene.\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  566. print_help_option("--debug-avoidance", "Show navigation avoidance debug visuals when running the scene.\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  567. print_help_option("--debug-stringnames", "Print all StringName allocations to stdout when the engine quits.\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  568. print_help_option("--debug-canvas-item-redraw", "Display a rectangle each time a canvas item requests a redraw (useful to troubleshoot low processor mode).\n", CLI_OPTION_AVAILABILITY_TEMPLATE_DEBUG);
  569. #endif
  570. print_help_option("--max-fps <fps>", "Set a maximum number of frames per second rendered (can be used to limit power usage). A value of 0 results in unlimited framerate.\n");
  571. print_help_option("--frame-delay <ms>", "Simulate high CPU load (delay each frame by <ms> milliseconds). Do not use as a FPS limiter; use --max-fps instead.\n");
  572. print_help_option("--time-scale <scale>", "Force time scale (higher values are faster, 1.0 is normal speed).\n");
  573. print_help_option("--disable-vsync", "Forces disabling of vertical synchronization, even if enabled in the project settings. Does not override driver-level V-Sync enforcement.\n");
  574. print_help_option("--disable-render-loop", "Disable render loop so rendering only occurs when called explicitly from script.\n");
  575. print_help_option("--disable-crash-handler", "Disable crash handler when supported by the platform code.\n");
  576. print_help_option("--fixed-fps <fps>", "Force a fixed number of frames per second. This setting disables real-time synchronization.\n");
  577. print_help_option("--delta-smoothing <enable>", "Enable or disable frame delta smoothing [\"enable\", \"disable\"].\n");
  578. print_help_option("--print-fps", "Print the frames per second to the stdout.\n");
  579. #ifdef TOOLS_ENABLED
  580. print_help_option("--editor-pseudolocalization", "Enable pseudolocalization for the editor and the project manager.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  581. #endif
  582. print_help_title("Standalone tools");
  583. print_help_option("-s, --script <script>", "Run a script.\n");
  584. print_help_option("--main-loop <main_loop_name>", "Run a MainLoop specified by its global class name.\n");
  585. print_help_option("--check-only", "Only parse for errors and quit (use with --script).\n");
  586. #ifdef TOOLS_ENABLED
  587. print_help_option("--import", "Starts the editor, waits for any resources to be imported, and then quits.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  588. print_help_option("--export-release <preset> <path>", "Export the project in release mode using the given preset and output path. The preset name should match one defined in \"export_presets.cfg\".\n", CLI_OPTION_AVAILABILITY_EDITOR);
  589. print_help_option("", "<path> should be absolute or relative to the project directory, and include the filename for the binary (e.g. \"builds/game.exe\").\n");
  590. print_help_option("", "The target directory must exist.\n");
  591. print_help_option("--export-debug <preset> <path>", "Export the project in debug mode using the given preset and output path. See --export-release description for other considerations.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  592. print_help_option("--export-pack <preset> <path>", "Export the project data only using the given preset and output path. The <path> extension determines whether it will be in PCK or ZIP format.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  593. print_help_option("--export-patch <preset> <path>", "Export pack with changed files only. See --export-pack description for other considerations.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  594. print_help_option("--patches <paths>", "List of patches to use with --export-patch. The list is comma-separated.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  595. print_help_option("--install-android-build-template", "Install the Android build template. Used in conjunction with --export-release or --export-debug.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  596. #ifndef DISABLE_DEPRECATED
  597. // Commands are long; split the description to a second line.
  598. print_help_option("--convert-3to4 ", "\n", CLI_OPTION_AVAILABILITY_HIDDEN);
  599. print_help_option(" [max_file_kb] [max_line_size]", "Converts project from Godot 3.x to Godot 4.x.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  600. print_help_option("--validate-conversion-3to4 ", "\n", CLI_OPTION_AVAILABILITY_HIDDEN);
  601. print_help_option(" [max_file_kb] [max_line_size]", "Shows what elements will be renamed when converting project from Godot 3.x to Godot 4.x.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  602. #endif // DISABLE_DEPRECATED
  603. print_help_option("--doctool [path]", "Dump the engine API reference to the given <path> (defaults to current directory) in XML format, merging if existing files are found.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  604. print_help_option("--no-docbase", "Disallow dumping the base types (used with --doctool).\n", CLI_OPTION_AVAILABILITY_EDITOR);
  605. print_help_option("--gdextension-docs", "Rather than dumping the engine API, generate API reference from all the GDExtensions loaded in the current project (used with --doctool).\n", CLI_OPTION_AVAILABILITY_EDITOR);
  606. #ifdef MODULE_GDSCRIPT_ENABLED
  607. print_help_option("--gdscript-docs <path>", "Rather than dumping the engine API, generate API reference from the inline documentation in the GDScript files found in <path> (used with --doctool).\n", CLI_OPTION_AVAILABILITY_EDITOR);
  608. #endif
  609. print_help_option("--build-solutions", "Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  610. print_help_option("--dump-gdextension-interface", "Generate a GDExtension header file \"gdextension_interface.h\" in the current folder. This file is the base file required to implement a GDExtension.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  611. print_help_option("--dump-extension-api", "Generate a JSON dump of the Godot API for GDExtension bindings named \"extension_api.json\" in the current folder.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  612. print_help_option("--dump-extension-api-with-docs", "Generate JSON dump of the Godot API like the previous option, but including documentation.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  613. print_help_option("--validate-extension-api <path>", "Validate an extension API file dumped (with one of the two previous options) from a previous version of the engine to ensure API compatibility.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  614. print_help_option("", "If incompatibilities or errors are detected, the exit code will be non-zero.\n");
  615. print_help_option("--benchmark", "Benchmark the run time and print it to console.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  616. print_help_option("--benchmark-file <path>", "Benchmark the run time and save it to a given file in JSON format. The path should be absolute.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  617. #ifdef TESTS_ENABLED
  618. print_help_option("--test [--help]", "Run unit tests. Use --test --help for more information.\n", CLI_OPTION_AVAILABILITY_EDITOR);
  619. #endif
  620. #endif
  621. OS::get_singleton()->print("\n");
  622. }
  623. #ifdef TESTS_ENABLED
  624. // The order is the same as in `Main::setup()`, only core and some editor types
  625. // are initialized here. This also combines `Main::setup2()` initialization.
  626. Error Main::test_setup() {
  627. Thread::make_main_thread();
  628. set_current_thread_safe_for_nodes(true);
  629. OS::get_singleton()->initialize();
  630. engine = memnew(Engine);
  631. register_core_types();
  632. register_core_driver_types();
  633. packed_data = memnew(PackedData);
  634. globals = memnew(ProjectSettings);
  635. register_core_settings(); // Here globals are present.
  636. translation_server = memnew(TranslationServer);
  637. tsman = memnew(TextServerManager);
  638. if (tsman) {
  639. Ref<TextServerDummy> ts;
  640. ts.instantiate();
  641. tsman->add_interface(ts);
  642. }
  643. #ifndef _3D_DISABLED
  644. physics_server_3d_manager = memnew(PhysicsServer3DManager);
  645. #endif // _3D_DISABLED
  646. physics_server_2d_manager = memnew(PhysicsServer2DManager);
  647. // From `Main::setup2()`.
  648. initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  649. register_core_extensions();
  650. register_core_singletons();
  651. /** INITIALIZE SERVERS **/
  652. register_server_types();
  653. #ifndef _3D_DISABLED
  654. XRServer::set_xr_mode(XRServer::XRMODE_OFF); // Skip in tests.
  655. #endif // _3D_DISABLED
  656. initialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  657. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  658. translation_server->setup(); //register translations, load them, etc.
  659. if (!locale.is_empty()) {
  660. translation_server->set_locale(locale);
  661. }
  662. translation_server->load_translations();
  663. ResourceLoader::load_translation_remaps(); //load remaps for resources
  664. ResourceLoader::load_path_remaps();
  665. // Initialize ThemeDB early so that scene types can register their theme items.
  666. // Default theme will be initialized later, after modules and ScriptServer are ready.
  667. initialize_theme_db();
  668. register_scene_types();
  669. register_driver_types();
  670. register_scene_singletons();
  671. initialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  672. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  673. #ifdef TOOLS_ENABLED
  674. ClassDB::set_current_api(ClassDB::API_EDITOR);
  675. register_editor_types();
  676. initialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  677. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  678. ClassDB::set_current_api(ClassDB::API_CORE);
  679. #endif
  680. register_platform_apis();
  681. // Theme needs modules to be initialized so that sub-resources can be loaded.
  682. theme_db->initialize_theme_noproject();
  683. initialize_navigation_server();
  684. ERR_FAIL_COND_V(TextServerManager::get_singleton()->get_interface_count() == 0, ERR_CANT_CREATE);
  685. /* Use one with the most features available. */
  686. int max_features = 0;
  687. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  688. uint32_t features = TextServerManager::get_singleton()->get_interface(i)->get_features();
  689. int feature_number = 0;
  690. while (features) {
  691. feature_number += features & 1;
  692. features >>= 1;
  693. }
  694. if (feature_number >= max_features) {
  695. max_features = feature_number;
  696. text_driver_idx = i;
  697. }
  698. }
  699. if (text_driver_idx >= 0) {
  700. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(text_driver_idx);
  701. TextServerManager::get_singleton()->set_primary_interface(ts);
  702. if (ts->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  703. ts->load_support_data("res://" + ts->get_support_data_filename());
  704. }
  705. } else {
  706. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "TextServer: Unable to create TextServer interface.");
  707. }
  708. ClassDB::set_current_api(ClassDB::API_NONE);
  709. _start_success = true;
  710. return OK;
  711. }
  712. // The order is the same as in `Main::cleanup()`.
  713. void Main::test_cleanup() {
  714. ERR_FAIL_COND(!_start_success);
  715. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  716. TextServerManager::get_singleton()->get_interface(i)->cleanup();
  717. }
  718. ResourceLoader::remove_custom_loaders();
  719. ResourceSaver::remove_custom_savers();
  720. PropertyListHelper::clear_base_helpers();
  721. #ifdef TOOLS_ENABLED
  722. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  723. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  724. unregister_editor_types();
  725. #endif
  726. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  727. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  728. unregister_platform_apis();
  729. unregister_driver_types();
  730. unregister_scene_types();
  731. finalize_theme_db();
  732. finalize_navigation_server();
  733. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  734. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  735. unregister_server_types();
  736. EngineDebugger::deinitialize();
  737. OS::get_singleton()->finalize();
  738. if (packed_data) {
  739. memdelete(packed_data);
  740. }
  741. if (translation_server) {
  742. memdelete(translation_server);
  743. }
  744. if (tsman) {
  745. memdelete(tsman);
  746. }
  747. #ifndef _3D_DISABLED
  748. if (physics_server_3d_manager) {
  749. memdelete(physics_server_3d_manager);
  750. }
  751. #endif // _3D_DISABLED
  752. if (physics_server_2d_manager) {
  753. memdelete(physics_server_2d_manager);
  754. }
  755. if (globals) {
  756. memdelete(globals);
  757. }
  758. unregister_core_driver_types();
  759. unregister_core_extensions();
  760. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  761. if (engine) {
  762. memdelete(engine);
  763. }
  764. unregister_core_types();
  765. OS::get_singleton()->finalize_core();
  766. }
  767. #endif
  768. int Main::test_entrypoint(int argc, char *argv[], bool &tests_need_run) {
  769. for (int x = 0; x < argc; x++) {
  770. if ((strncmp(argv[x], "--test", 6) == 0) && (strlen(argv[x]) == 6)) {
  771. tests_need_run = true;
  772. #ifdef TESTS_ENABLED
  773. // TODO: need to come up with different test contexts.
  774. // Not every test requires high-level functionality like `ClassDB`.
  775. test_setup();
  776. int status = test_main(argc, argv);
  777. test_cleanup();
  778. return status;
  779. #else
  780. ERR_PRINT(
  781. "`--test` was specified on the command line, but this Godot binary was compiled without support for unit tests. Aborting.\n"
  782. "To be able to run unit tests, use the `tests=yes` SCons option when compiling Godot.\n");
  783. return EXIT_FAILURE;
  784. #endif
  785. }
  786. }
  787. tests_need_run = false;
  788. return EXIT_SUCCESS;
  789. }
  790. /* Engine initialization
  791. *
  792. * Consists of several methods that are called by each platform's specific main(argc, argv).
  793. * To fully understand engine init, one should therefore start from the platform's main and
  794. * see how it calls into the Main class' methods.
  795. *
  796. * The initialization is typically done in 3 steps (with the setup2 step triggered either
  797. * automatically by setup, or manually in the platform's main).
  798. *
  799. * - setup(execpath, argc, argv, p_second_phase) is the main entry point for all platforms,
  800. * responsible for the initialization of all low level singletons and core types, and parsing
  801. * command line arguments to configure things accordingly.
  802. * If p_second_phase is true, it will chain into setup2() (default behavior). This is
  803. * disabled on some platforms (Android, iOS) which trigger the second step in their own time.
  804. *
  805. * - setup2(p_main_tid_override) registers high level servers and singletons, displays the
  806. * boot splash, then registers higher level types (scene, editor, etc.).
  807. *
  808. * - start() is the last step and that's where command line tools can run, or the main loop
  809. * can be created eventually and the project settings put into action. That's also where
  810. * the editor node is created, if relevant.
  811. * start() does it own argument parsing for a subset of the command line arguments described
  812. * in help, it's a bit messy and should be globalized with the setup() parsing somehow.
  813. */
  814. Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_phase) {
  815. Thread::make_main_thread();
  816. set_current_thread_safe_for_nodes(true);
  817. OS::get_singleton()->initialize();
  818. // Benchmark tracking must be done after `OS::get_singleton()->initialize()` as on some
  819. // platforms, it's used to set up the time utilities.
  820. OS::get_singleton()->benchmark_begin_measure("Startup", "Main::Setup");
  821. engine = memnew(Engine);
  822. MAIN_PRINT("Main: Initialize CORE");
  823. register_core_types();
  824. register_core_driver_types();
  825. MAIN_PRINT("Main: Initialize Globals");
  826. input_map = memnew(InputMap);
  827. globals = memnew(ProjectSettings);
  828. register_core_settings(); //here globals are present
  829. translation_server = memnew(TranslationServer);
  830. performance = memnew(Performance);
  831. GDREGISTER_CLASS(Performance);
  832. engine->add_singleton(Engine::Singleton("Performance", performance));
  833. // Only flush stdout in debug builds by default, as spamming `print()` will
  834. // decrease performance if this is enabled.
  835. GLOBAL_DEF_RST("application/run/flush_stdout_on_print", false);
  836. GLOBAL_DEF_RST("application/run/flush_stdout_on_print.debug", true);
  837. MAIN_PRINT("Main: Parse CMDLine");
  838. /* argument parsing and main creation */
  839. List<String> args;
  840. List<String> main_args;
  841. List<String> user_args;
  842. bool adding_user_args = false;
  843. List<String> platform_args = OS::get_singleton()->get_cmdline_platform_args();
  844. // Add command line arguments.
  845. for (int i = 0; i < argc; i++) {
  846. args.push_back(String::utf8(argv[i]));
  847. }
  848. // Add arguments received from macOS LaunchService (URL schemas, file associations).
  849. for (const String &arg : platform_args) {
  850. args.push_back(arg);
  851. }
  852. List<String>::Element *I = args.front();
  853. while (I) {
  854. I->get() = unescape_cmdline(I->get().strip_edges());
  855. I = I->next();
  856. }
  857. String audio_driver = "";
  858. String project_path = ".";
  859. bool upwards = false;
  860. String debug_uri = "";
  861. bool skip_breakpoints = false;
  862. String main_pack;
  863. bool quiet_stdout = false;
  864. int rtm = -1;
  865. String remotefs;
  866. String remotefs_pass;
  867. Vector<String> breakpoints;
  868. bool use_custom_res = true;
  869. bool force_res = false;
  870. bool delta_smoothing_override = false;
  871. String default_renderer = "";
  872. String default_renderer_mobile = "";
  873. String renderer_hints = "";
  874. packed_data = PackedData::get_singleton();
  875. if (!packed_data) {
  876. packed_data = memnew(PackedData);
  877. }
  878. #ifdef MINIZIP_ENABLED
  879. //XXX: always get_singleton() == 0x0
  880. zip_packed_data = ZipArchive::get_singleton();
  881. //TODO: remove this temporary fix
  882. if (!zip_packed_data) {
  883. zip_packed_data = memnew(ZipArchive);
  884. }
  885. packed_data->add_pack_source(zip_packed_data);
  886. #endif
  887. // Exit error code used in the `goto error` conditions.
  888. // It's returned as the program exit code. ERR_HELP is special cased and handled as success (0).
  889. Error exit_err = ERR_INVALID_PARAMETER;
  890. I = args.front();
  891. while (I) {
  892. List<String>::Element *N = I->next();
  893. const String &arg = I->get();
  894. #ifdef MACOS_ENABLED
  895. // Ignore the process serial number argument passed by macOS Gatekeeper.
  896. // Otherwise, Godot would try to open a non-existent project on the first start and abort.
  897. if (arg.begins_with("-psn_")) {
  898. I = N;
  899. continue;
  900. }
  901. #endif
  902. #ifdef TOOLS_ENABLED
  903. if (arg == "--debug" ||
  904. arg == "--verbose" ||
  905. arg == "--disable-crash-handler") {
  906. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(arg);
  907. forwardable_cli_arguments[CLI_SCOPE_PROJECT].push_back(arg);
  908. }
  909. if (arg == "--single-window") {
  910. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(arg);
  911. }
  912. if (arg == "--audio-driver" ||
  913. arg == "--display-driver" ||
  914. arg == "--rendering-method" ||
  915. arg == "--rendering-driver" ||
  916. arg == "--xr-mode") {
  917. if (N) {
  918. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(arg);
  919. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(N->get());
  920. }
  921. }
  922. // If gpu is specified, both editor and debug instances started from editor will inherit.
  923. if (arg == "--gpu-index") {
  924. if (N) {
  925. const String &next_arg = N->get();
  926. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(arg);
  927. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(next_arg);
  928. forwardable_cli_arguments[CLI_SCOPE_PROJECT].push_back(arg);
  929. forwardable_cli_arguments[CLI_SCOPE_PROJECT].push_back(next_arg);
  930. }
  931. }
  932. #endif
  933. if (adding_user_args) {
  934. user_args.push_back(arg);
  935. } else if (arg == "-h" || arg == "--help" || arg == "/?") { // display help
  936. show_help = true;
  937. exit_err = ERR_HELP; // Hack to force an early exit in `main()` with a success code.
  938. goto error;
  939. } else if (arg == "--version") {
  940. print_line(get_full_version_string());
  941. exit_err = ERR_HELP; // Hack to force an early exit in `main()` with a success code.
  942. goto error;
  943. } else if (arg == "-v" || arg == "--verbose") { // verbose output
  944. OS::get_singleton()->_verbose_stdout = true;
  945. } else if (arg == "-q" || arg == "--quiet") { // quieter output
  946. quiet_stdout = true;
  947. } else if (arg == "--no-header") {
  948. Engine::get_singleton()->_print_header = false;
  949. } else if (arg == "--audio-driver") { // audio driver
  950. if (N) {
  951. audio_driver = N->get();
  952. bool found = false;
  953. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  954. if (audio_driver == AudioDriverManager::get_driver(i)->get_name()) {
  955. found = true;
  956. }
  957. }
  958. if (!found) {
  959. OS::get_singleton()->print("Unknown audio driver '%s', aborting.\nValid options are ",
  960. audio_driver.utf8().get_data());
  961. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  962. if (i == AudioDriverManager::get_driver_count() - 1) {
  963. OS::get_singleton()->print(" and ");
  964. } else if (i != 0) {
  965. OS::get_singleton()->print(", ");
  966. }
  967. OS::get_singleton()->print("'%s'", AudioDriverManager::get_driver(i)->get_name());
  968. }
  969. OS::get_singleton()->print(".\n");
  970. goto error;
  971. }
  972. N = N->next();
  973. } else {
  974. OS::get_singleton()->print("Missing audio driver argument, aborting.\n");
  975. goto error;
  976. }
  977. } else if (arg == "--audio-output-latency") {
  978. if (N) {
  979. audio_output_latency = N->get().to_int();
  980. N = N->next();
  981. } else {
  982. OS::get_singleton()->print("Missing audio output latency argument, aborting.\n");
  983. goto error;
  984. }
  985. } else if (arg == "--text-driver") {
  986. if (N) {
  987. text_driver = N->get();
  988. N = N->next();
  989. } else {
  990. OS::get_singleton()->print("Missing text driver argument, aborting.\n");
  991. goto error;
  992. }
  993. } else if (arg == "--display-driver") { // force video driver
  994. if (N) {
  995. display_driver = N->get();
  996. bool found = false;
  997. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  998. if (display_driver == DisplayServer::get_create_function_name(i)) {
  999. found = true;
  1000. }
  1001. }
  1002. if (!found) {
  1003. OS::get_singleton()->print("Unknown display driver '%s', aborting.\nValid options are ",
  1004. display_driver.utf8().get_data());
  1005. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1006. if (i == DisplayServer::get_create_function_count() - 1) {
  1007. OS::get_singleton()->print(" and ");
  1008. } else if (i != 0) {
  1009. OS::get_singleton()->print(", ");
  1010. }
  1011. OS::get_singleton()->print("'%s'", DisplayServer::get_create_function_name(i));
  1012. }
  1013. OS::get_singleton()->print(".\n");
  1014. goto error;
  1015. }
  1016. N = N->next();
  1017. } else {
  1018. OS::get_singleton()->print("Missing display driver argument, aborting.\n");
  1019. goto error;
  1020. }
  1021. } else if (arg == "--rendering-method") {
  1022. if (N) {
  1023. rendering_method = N->get();
  1024. N = N->next();
  1025. } else {
  1026. OS::get_singleton()->print("Missing renderer name argument, aborting.\n");
  1027. goto error;
  1028. }
  1029. } else if (arg == "--rendering-driver") {
  1030. if (N) {
  1031. rendering_driver = N->get();
  1032. N = N->next();
  1033. } else {
  1034. OS::get_singleton()->print("Missing rendering driver argument, aborting.\n");
  1035. goto error;
  1036. }
  1037. } else if (arg == "-f" || arg == "--fullscreen") { // force fullscreen
  1038. init_fullscreen = true;
  1039. window_mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  1040. } else if (arg == "-m" || arg == "--maximized") { // force maximized window
  1041. init_maximized = true;
  1042. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  1043. } else if (arg == "-w" || arg == "--windowed") { // force windowed window
  1044. init_windowed = true;
  1045. } else if (arg == "--gpu-index") {
  1046. if (N) {
  1047. Engine::singleton->gpu_idx = N->get().to_int();
  1048. N = N->next();
  1049. } else {
  1050. OS::get_singleton()->print("Missing GPU index argument, aborting.\n");
  1051. goto error;
  1052. }
  1053. } else if (arg == "--gpu-validation") {
  1054. Engine::singleton->use_validation_layers = true;
  1055. #ifdef DEBUG_ENABLED
  1056. } else if (arg == "--gpu-abort") {
  1057. Engine::singleton->abort_on_gpu_errors = true;
  1058. #endif
  1059. } else if (arg == "--generate-spirv-debug-info") {
  1060. Engine::singleton->generate_spirv_debug_info = true;
  1061. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  1062. } else if (arg == "--extra-gpu-memory-tracking") {
  1063. Engine::singleton->extra_gpu_memory_tracking = true;
  1064. } else if (arg == "--accurate-breadcrumbs") {
  1065. Engine::singleton->accurate_breadcrumbs = true;
  1066. #endif
  1067. } else if (arg == "--tablet-driver") {
  1068. if (N) {
  1069. tablet_driver = N->get();
  1070. N = N->next();
  1071. } else {
  1072. OS::get_singleton()->print("Missing tablet driver argument, aborting.\n");
  1073. goto error;
  1074. }
  1075. } else if (arg == "--delta-smoothing") {
  1076. if (N) {
  1077. String string = N->get();
  1078. bool recognized = false;
  1079. if (string == "enable") {
  1080. OS::get_singleton()->set_delta_smoothing(true);
  1081. delta_smoothing_override = true;
  1082. recognized = true;
  1083. }
  1084. if (string == "disable") {
  1085. OS::get_singleton()->set_delta_smoothing(false);
  1086. delta_smoothing_override = false;
  1087. recognized = true;
  1088. }
  1089. if (!recognized) {
  1090. OS::get_singleton()->print("Delta-smoothing argument not recognized, aborting.\n");
  1091. goto error;
  1092. }
  1093. N = N->next();
  1094. } else {
  1095. OS::get_singleton()->print("Missing delta-smoothing argument, aborting.\n");
  1096. goto error;
  1097. }
  1098. } else if (arg == "--single-window") { // force single window
  1099. single_window = true;
  1100. } else if (arg == "-t" || arg == "--always-on-top") { // force always-on-top window
  1101. init_always_on_top = true;
  1102. } else if (arg == "--resolution") { // force resolution
  1103. if (N) {
  1104. String vm = N->get();
  1105. if (!vm.contains("x")) { // invalid parameter format
  1106. OS::get_singleton()->print("Invalid resolution '%s', it should be e.g. '1280x720'.\n",
  1107. vm.utf8().get_data());
  1108. goto error;
  1109. }
  1110. int w = vm.get_slice("x", 0).to_int();
  1111. int h = vm.get_slice("x", 1).to_int();
  1112. if (w <= 0 || h <= 0) {
  1113. OS::get_singleton()->print("Invalid resolution '%s', width and height must be above 0.\n",
  1114. vm.utf8().get_data());
  1115. goto error;
  1116. }
  1117. window_size.width = w;
  1118. window_size.height = h;
  1119. force_res = true;
  1120. N = N->next();
  1121. } else {
  1122. OS::get_singleton()->print("Missing resolution argument, aborting.\n");
  1123. goto error;
  1124. }
  1125. } else if (arg == "--screen") { // set window screen
  1126. if (N) {
  1127. init_screen = N->get().to_int();
  1128. init_use_custom_screen = true;
  1129. N = N->next();
  1130. } else {
  1131. OS::get_singleton()->print("Missing screen argument, aborting.\n");
  1132. goto error;
  1133. }
  1134. } else if (arg == "--position") { // set window position
  1135. if (N) {
  1136. String vm = N->get();
  1137. if (!vm.contains(",")) { // invalid parameter format
  1138. OS::get_singleton()->print("Invalid position '%s', it should be e.g. '80,128'.\n",
  1139. vm.utf8().get_data());
  1140. goto error;
  1141. }
  1142. int x = vm.get_slice(",", 0).to_int();
  1143. int y = vm.get_slice(",", 1).to_int();
  1144. init_custom_pos = Point2(x, y);
  1145. init_use_custom_pos = true;
  1146. N = N->next();
  1147. } else {
  1148. OS::get_singleton()->print("Missing position argument, aborting.\n");
  1149. goto error;
  1150. }
  1151. } else if (arg == "--headless") { // enable headless mode (no audio, no rendering).
  1152. audio_driver = NULL_AUDIO_DRIVER;
  1153. display_driver = NULL_DISPLAY_DRIVER;
  1154. } else if (arg == "--log-file") { // write to log file
  1155. if (N) {
  1156. log_file = N->get();
  1157. N = N->next();
  1158. } else {
  1159. OS::get_singleton()->print("Missing log file path argument, aborting.\n");
  1160. goto error;
  1161. }
  1162. } else if (arg == "--profiling") { // enable profiling
  1163. use_debug_profiler = true;
  1164. } else if (arg == "-l" || arg == "--language") { // language
  1165. if (N) {
  1166. locale = N->get();
  1167. N = N->next();
  1168. } else {
  1169. OS::get_singleton()->print("Missing language argument, aborting.\n");
  1170. goto error;
  1171. }
  1172. } else if (arg == "--remote-fs") { // remote filesystem
  1173. if (N) {
  1174. remotefs = N->get();
  1175. N = N->next();
  1176. } else {
  1177. OS::get_singleton()->print("Missing remote filesystem address, aborting.\n");
  1178. goto error;
  1179. }
  1180. } else if (arg == "--remote-fs-password") { // remote filesystem password
  1181. if (N) {
  1182. remotefs_pass = N->get();
  1183. N = N->next();
  1184. } else {
  1185. OS::get_singleton()->print("Missing remote filesystem password, aborting.\n");
  1186. goto error;
  1187. }
  1188. } else if (arg == "--render-thread") { // render thread mode
  1189. if (N) {
  1190. if (N->get() == "safe") {
  1191. rtm = OS::RENDER_THREAD_SAFE;
  1192. } else if (N->get() == "unsafe") {
  1193. rtm = OS::RENDER_THREAD_UNSAFE;
  1194. } else if (N->get() == "separate") {
  1195. rtm = OS::RENDER_SEPARATE_THREAD;
  1196. } else {
  1197. OS::get_singleton()->print("Unknown render thread mode, aborting.\nValid options are 'unsafe', 'safe' and 'separate'.\n");
  1198. goto error;
  1199. }
  1200. N = N->next();
  1201. } else {
  1202. OS::get_singleton()->print("Missing render thread mode argument, aborting.\n");
  1203. goto error;
  1204. }
  1205. #ifdef TOOLS_ENABLED
  1206. } else if (arg == "-e" || arg == "--editor") { // starts editor
  1207. editor = true;
  1208. } else if (arg == "-p" || arg == "--project-manager") { // starts project manager
  1209. project_manager = true;
  1210. } else if (arg == "--debug-server") {
  1211. if (N) {
  1212. debug_server_uri = N->get();
  1213. if (!debug_server_uri.contains("://")) { // wrong address
  1214. OS::get_singleton()->print("Invalid debug server uri. It should be of the form <protocol>://<bind_address>:<port>.\n");
  1215. goto error;
  1216. }
  1217. N = N->next();
  1218. } else {
  1219. OS::get_singleton()->print("Missing remote debug server uri, aborting.\n");
  1220. goto error;
  1221. }
  1222. } else if (arg == "--single-threaded-scene") {
  1223. single_threaded_scene = true;
  1224. } else if (arg == "--build-solutions") { // Build the scripting solution such C#
  1225. auto_build_solutions = true;
  1226. editor = true;
  1227. cmdline_tool = true;
  1228. } else if (arg == "--dump-gdextension-interface") {
  1229. // Register as an editor instance to use low-end fallback if relevant.
  1230. editor = true;
  1231. cmdline_tool = true;
  1232. dump_gdextension_interface = true;
  1233. print_line("Dumping GDExtension interface header file");
  1234. // Hack. Not needed but otherwise we end up detecting that this should
  1235. // run the project instead of a cmdline tool.
  1236. // Needs full refactoring to fix properly.
  1237. main_args.push_back(arg);
  1238. } else if (arg == "--dump-extension-api") {
  1239. // Register as an editor instance to use low-end fallback if relevant.
  1240. editor = true;
  1241. cmdline_tool = true;
  1242. dump_extension_api = true;
  1243. print_line("Dumping Extension API");
  1244. // Hack. Not needed but otherwise we end up detecting that this should
  1245. // run the project instead of a cmdline tool.
  1246. // Needs full refactoring to fix properly.
  1247. main_args.push_back(arg);
  1248. } else if (arg == "--dump-extension-api-with-docs") {
  1249. // Register as an editor instance to use low-end fallback if relevant.
  1250. editor = true;
  1251. cmdline_tool = true;
  1252. dump_extension_api = true;
  1253. include_docs_in_extension_api_dump = true;
  1254. print_line("Dumping Extension API including documentation");
  1255. // Hack. Not needed but otherwise we end up detecting that this should
  1256. // run the project instead of a cmdline tool.
  1257. // Needs full refactoring to fix properly.
  1258. main_args.push_back(arg);
  1259. } else if (arg == "--validate-extension-api") {
  1260. // Register as an editor instance to use low-end fallback if relevant.
  1261. editor = true;
  1262. cmdline_tool = true;
  1263. validate_extension_api = true;
  1264. // Hack. Not needed but otherwise we end up detecting that this should
  1265. // run the project instead of a cmdline tool.
  1266. // Needs full refactoring to fix properly.
  1267. main_args.push_back(arg);
  1268. if (N) {
  1269. validate_extension_api_file = N->get();
  1270. N = N->next();
  1271. } else {
  1272. OS::get_singleton()->print("Missing file to load argument after --validate-extension-api, aborting.");
  1273. goto error;
  1274. }
  1275. } else if (arg == "--import") {
  1276. editor = true;
  1277. cmdline_tool = true;
  1278. wait_for_import = true;
  1279. quit_after = 1;
  1280. } else if (arg == "--export-release" || arg == "--export-debug" ||
  1281. arg == "--export-pack" || arg == "--export-patch") { // Export project
  1282. // Actually handling is done in start().
  1283. editor = true;
  1284. cmdline_tool = true;
  1285. wait_for_import = true;
  1286. main_args.push_back(arg);
  1287. } else if (arg == "--patches") {
  1288. if (N) {
  1289. // Actually handling is done in start().
  1290. main_args.push_back(arg);
  1291. main_args.push_back(N->get());
  1292. N = N->next();
  1293. } else {
  1294. OS::get_singleton()->print("Missing comma-separated list of patches after --patches, aborting.\n");
  1295. goto error;
  1296. }
  1297. #ifndef DISABLE_DEPRECATED
  1298. } else if (arg == "--export") { // For users used to 3.x syntax.
  1299. OS::get_singleton()->print("The Godot 3 --export option was changed to more explicit --export-release / --export-debug / --export-pack options.\nSee the --help output for details.\n");
  1300. goto error;
  1301. } else if (arg == "--convert-3to4") {
  1302. // Actually handling is done in start().
  1303. cmdline_tool = true;
  1304. main_args.push_back(arg);
  1305. if (N && !N->get().begins_with("-")) {
  1306. if (itos(N->get().to_int()) == N->get()) {
  1307. converter_max_kb_file = N->get().to_int();
  1308. }
  1309. if (N->next() && !N->next()->get().begins_with("-")) {
  1310. if (itos(N->next()->get().to_int()) == N->next()->get()) {
  1311. converter_max_line_length = N->next()->get().to_int();
  1312. }
  1313. }
  1314. }
  1315. } else if (arg == "--validate-conversion-3to4") {
  1316. // Actually handling is done in start().
  1317. cmdline_tool = true;
  1318. main_args.push_back(arg);
  1319. if (N && !N->get().begins_with("-")) {
  1320. if (itos(N->get().to_int()) == N->get()) {
  1321. converter_max_kb_file = N->get().to_int();
  1322. }
  1323. if (N->next() && !N->next()->get().begins_with("-")) {
  1324. if (itos(N->next()->get().to_int()) == N->next()->get()) {
  1325. converter_max_line_length = N->next()->get().to_int();
  1326. }
  1327. }
  1328. }
  1329. #endif // DISABLE_DEPRECATED
  1330. } else if (arg == "--doctool") {
  1331. // Actually handling is done in start().
  1332. cmdline_tool = true;
  1333. // `--doctool` implies `--headless` to avoid spawning an unnecessary window
  1334. // and speed up class reference generation.
  1335. audio_driver = NULL_AUDIO_DRIVER;
  1336. display_driver = NULL_DISPLAY_DRIVER;
  1337. main_args.push_back(arg);
  1338. #ifdef MODULE_GDSCRIPT_ENABLED
  1339. } else if (arg == "--gdscript-docs") {
  1340. if (N) {
  1341. project_path = N->get();
  1342. // Will be handled in start()
  1343. main_args.push_back(arg);
  1344. main_args.push_back(N->get());
  1345. N = N->next();
  1346. // GDScript docgen requires Autoloads, but loading those also creates a main loop.
  1347. // This forces main loop to quit without adding more GDScript-specific exceptions to setup.
  1348. quit_after = 1;
  1349. } else {
  1350. OS::get_singleton()->print("Missing relative or absolute path to project for --gdscript-docs, aborting.\n");
  1351. goto error;
  1352. }
  1353. #endif // MODULE_GDSCRIPT_ENABLED
  1354. #endif // TOOLS_ENABLED
  1355. } else if (arg == "--path") { // set path of project to start or edit
  1356. if (N) {
  1357. String p = N->get();
  1358. if (OS::get_singleton()->set_cwd(p) != OK) {
  1359. OS::get_singleton()->print("Invalid project path specified: \"%s\", aborting.\n", p.utf8().get_data());
  1360. goto error;
  1361. }
  1362. N = N->next();
  1363. } else {
  1364. OS::get_singleton()->print("Missing relative or absolute path, aborting.\n");
  1365. goto error;
  1366. }
  1367. } else if (arg == "-u" || arg == "--upwards") { // scan folders upwards
  1368. upwards = true;
  1369. } else if (arg == "--quit") { // Auto quit at the end of the first main loop iteration
  1370. quit_after = 1;
  1371. } else if (arg == "--quit-after") { // Quit after the given number of iterations
  1372. if (N) {
  1373. quit_after = N->get().to_int();
  1374. N = N->next();
  1375. } else {
  1376. OS::get_singleton()->print("Missing number of iterations, aborting.\n");
  1377. goto error;
  1378. }
  1379. } else if (arg.ends_with("project.godot")) {
  1380. String path;
  1381. String file = arg;
  1382. int sep = MAX(file.rfind_char('/'), file.rfind_char('\\'));
  1383. if (sep == -1) {
  1384. path = ".";
  1385. } else {
  1386. path = file.substr(0, sep);
  1387. }
  1388. if (OS::get_singleton()->set_cwd(path) == OK) {
  1389. // path already specified, don't override
  1390. } else {
  1391. project_path = path;
  1392. }
  1393. #ifdef TOOLS_ENABLED
  1394. editor = true;
  1395. #endif
  1396. } else if (arg == "-b" || arg == "--breakpoints") { // add breakpoints
  1397. if (N) {
  1398. String bplist = N->get();
  1399. breakpoints = bplist.split(",");
  1400. N = N->next();
  1401. } else {
  1402. OS::get_singleton()->print("Missing list of breakpoints, aborting.\n");
  1403. goto error;
  1404. }
  1405. } else if (arg == "--max-fps") { // set maximum rendered FPS
  1406. if (N) {
  1407. max_fps = N->get().to_int();
  1408. N = N->next();
  1409. } else {
  1410. OS::get_singleton()->print("Missing maximum FPS argument, aborting.\n");
  1411. goto error;
  1412. }
  1413. } else if (arg == "--frame-delay") { // force frame delay
  1414. if (N) {
  1415. frame_delay = N->get().to_int();
  1416. N = N->next();
  1417. } else {
  1418. OS::get_singleton()->print("Missing frame delay argument, aborting.\n");
  1419. goto error;
  1420. }
  1421. } else if (arg == "--time-scale") { // force time scale
  1422. if (N) {
  1423. Engine::get_singleton()->set_time_scale(N->get().to_float());
  1424. N = N->next();
  1425. } else {
  1426. OS::get_singleton()->print("Missing time scale argument, aborting.\n");
  1427. goto error;
  1428. }
  1429. } else if (arg == "--main-pack") {
  1430. if (N) {
  1431. main_pack = N->get();
  1432. N = N->next();
  1433. } else {
  1434. OS::get_singleton()->print("Missing path to main pack file, aborting.\n");
  1435. goto error;
  1436. }
  1437. } else if (arg == "-d" || arg == "--debug") {
  1438. debug_uri = "local://";
  1439. OS::get_singleton()->_debug_stdout = true;
  1440. #if defined(DEBUG_ENABLED)
  1441. } else if (arg == "--debug-collisions") {
  1442. debug_collisions = true;
  1443. } else if (arg == "--debug-paths") {
  1444. debug_paths = true;
  1445. } else if (arg == "--debug-navigation") {
  1446. debug_navigation = true;
  1447. } else if (arg == "--debug-avoidance") {
  1448. debug_avoidance = true;
  1449. } else if (arg == "--debug-canvas-item-redraw") {
  1450. debug_canvas_item_redraw = true;
  1451. } else if (arg == "--debug-stringnames") {
  1452. StringName::set_debug_stringnames(true);
  1453. #endif
  1454. } else if (arg == "--remote-debug") {
  1455. if (N) {
  1456. debug_uri = N->get();
  1457. if (!debug_uri.contains("://")) { // wrong address
  1458. OS::get_singleton()->print(
  1459. "Invalid debug host address, it should be of the form <protocol>://<host/IP>:<port>.\n");
  1460. goto error;
  1461. }
  1462. N = N->next();
  1463. } else {
  1464. OS::get_singleton()->print("Missing remote debug host address, aborting.\n");
  1465. goto error;
  1466. }
  1467. } else if (arg == "--editor-pid") { // not exposed to user
  1468. if (N) {
  1469. editor_pid = N->get().to_int();
  1470. N = N->next();
  1471. } else {
  1472. OS::get_singleton()->print("Missing editor PID argument, aborting.\n");
  1473. goto error;
  1474. }
  1475. } else if (arg == "--disable-render-loop") {
  1476. disable_render_loop = true;
  1477. } else if (arg == "--fixed-fps") {
  1478. if (N) {
  1479. fixed_fps = N->get().to_int();
  1480. N = N->next();
  1481. } else {
  1482. OS::get_singleton()->print("Missing fixed-fps argument, aborting.\n");
  1483. goto error;
  1484. }
  1485. } else if (arg == "--write-movie") {
  1486. if (N) {
  1487. Engine::get_singleton()->set_write_movie_path(N->get());
  1488. N = N->next();
  1489. if (fixed_fps == -1) {
  1490. fixed_fps = 60;
  1491. }
  1492. OS::get_singleton()->_writing_movie = true;
  1493. } else {
  1494. OS::get_singleton()->print("Missing write-movie argument, aborting.\n");
  1495. goto error;
  1496. }
  1497. } else if (arg == "--disable-vsync") {
  1498. disable_vsync = true;
  1499. } else if (arg == "--print-fps") {
  1500. print_fps = true;
  1501. #ifdef TOOLS_ENABLED
  1502. } else if (arg == "--editor-pseudolocalization") {
  1503. editor_pseudolocalization = true;
  1504. #endif // TOOLS_ENABLED
  1505. } else if (arg == "--profile-gpu") {
  1506. profile_gpu = true;
  1507. } else if (arg == "--disable-crash-handler") {
  1508. OS::get_singleton()->disable_crash_handler();
  1509. } else if (arg == "--skip-breakpoints") {
  1510. skip_breakpoints = true;
  1511. #ifndef _3D_DISABLED
  1512. } else if (arg == "--xr-mode") {
  1513. if (N) {
  1514. String xr_mode = N->get().to_lower();
  1515. N = N->next();
  1516. if (xr_mode == "default") {
  1517. XRServer::set_xr_mode(XRServer::XRMODE_DEFAULT);
  1518. } else if (xr_mode == "off") {
  1519. XRServer::set_xr_mode(XRServer::XRMODE_OFF);
  1520. } else if (xr_mode == "on") {
  1521. XRServer::set_xr_mode(XRServer::XRMODE_ON);
  1522. } else {
  1523. OS::get_singleton()->print("Unknown --xr-mode argument \"%s\", aborting.\n", xr_mode.ascii().get_data());
  1524. goto error;
  1525. }
  1526. } else {
  1527. OS::get_singleton()->print("Missing --xr-mode argument, aborting.\n");
  1528. goto error;
  1529. }
  1530. #endif // _3D_DISABLED
  1531. } else if (arg == "--benchmark") {
  1532. OS::get_singleton()->set_use_benchmark(true);
  1533. } else if (arg == "--benchmark-file") {
  1534. if (N) {
  1535. OS::get_singleton()->set_use_benchmark(true);
  1536. String benchmark_file = N->get();
  1537. OS::get_singleton()->set_benchmark_file(benchmark_file);
  1538. N = N->next();
  1539. } else {
  1540. OS::get_singleton()->print("Missing <path> argument for --benchmark-file <path>.\n");
  1541. goto error;
  1542. }
  1543. #if defined(TOOLS_ENABLED) && defined(MODULE_GDSCRIPT_ENABLED) && !defined(GDSCRIPT_NO_LSP)
  1544. } else if (arg == "--lsp-port") {
  1545. if (N) {
  1546. int port_override = N->get().to_int();
  1547. if (port_override < 0 || port_override > 65535) {
  1548. OS::get_singleton()->print("<port> argument for --lsp-port <port> must be between 0 and 65535.\n");
  1549. goto error;
  1550. }
  1551. GDScriptLanguageServer::port_override = port_override;
  1552. N = N->next();
  1553. } else {
  1554. OS::get_singleton()->print("Missing <port> argument for --lsp-port <port>.\n");
  1555. goto error;
  1556. }
  1557. #endif // TOOLS_ENABLED && MODULE_GDSCRIPT_ENABLED && !GDSCRIPT_NO_LSP
  1558. #if defined(TOOLS_ENABLED)
  1559. } else if (arg == "--dap-port") {
  1560. if (N) {
  1561. int port_override = N->get().to_int();
  1562. if (port_override < 0 || port_override > 65535) {
  1563. OS::get_singleton()->print("<port> argument for --dap-port <port> must be between 0 and 65535.\n");
  1564. goto error;
  1565. }
  1566. DebugAdapterServer::port_override = port_override;
  1567. N = N->next();
  1568. } else {
  1569. OS::get_singleton()->print("Missing <port> argument for --dap-port <port>.\n");
  1570. goto error;
  1571. }
  1572. #endif // TOOLS_ENABLED
  1573. } else if (arg == "--" || arg == "++") {
  1574. adding_user_args = true;
  1575. } else {
  1576. main_args.push_back(arg);
  1577. }
  1578. I = N;
  1579. }
  1580. #ifdef TOOLS_ENABLED
  1581. if (editor && project_manager) {
  1582. OS::get_singleton()->print(
  1583. "Error: Command line arguments implied opening both editor and project manager, which is not possible. Aborting.\n");
  1584. goto error;
  1585. }
  1586. #endif
  1587. // Network file system needs to be configured before globals, since globals are based on the
  1588. // 'project.godot' file which will only be available through the network if this is enabled
  1589. if (!remotefs.is_empty()) {
  1590. int port;
  1591. if (remotefs.contains(":")) {
  1592. port = remotefs.get_slicec(':', 1).to_int();
  1593. remotefs = remotefs.get_slicec(':', 0);
  1594. } else {
  1595. port = 6010;
  1596. }
  1597. Error err = OS::get_singleton()->setup_remote_filesystem(remotefs, port, remotefs_pass, project_path);
  1598. if (err) {
  1599. OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i.\n", remotefs.utf8().get_data(), port);
  1600. goto error;
  1601. }
  1602. }
  1603. OS::get_singleton()->_in_editor = editor;
  1604. if (globals->setup(project_path, main_pack, upwards, editor) == OK) {
  1605. #ifdef TOOLS_ENABLED
  1606. found_project = true;
  1607. #endif
  1608. } else {
  1609. #ifdef TOOLS_ENABLED
  1610. editor = false;
  1611. #else
  1612. const String error_msg = "Error: Couldn't load project data at path \"" + project_path + "\". Is the .pck file missing?\nIf you've renamed the executable, the associated .pck file should also be renamed to match the executable's name (without the extension).\n";
  1613. OS::get_singleton()->print("%s", error_msg.utf8().get_data());
  1614. OS::get_singleton()->alert(error_msg);
  1615. goto error;
  1616. #endif
  1617. }
  1618. // Initialize WorkerThreadPool.
  1619. {
  1620. #ifdef THREADS_ENABLED
  1621. if (editor || project_manager) {
  1622. WorkerThreadPool::get_singleton()->init(-1, 0.75);
  1623. } else {
  1624. int worker_threads = GLOBAL_GET("threading/worker_pool/max_threads");
  1625. float low_priority_ratio = GLOBAL_GET("threading/worker_pool/low_priority_thread_ratio");
  1626. WorkerThreadPool::get_singleton()->init(worker_threads, low_priority_ratio);
  1627. }
  1628. #else
  1629. WorkerThreadPool::get_singleton()->init(0, 0);
  1630. #endif
  1631. }
  1632. #ifdef TOOLS_ENABLED
  1633. if (editor) {
  1634. Engine::get_singleton()->set_editor_hint(true);
  1635. Engine::get_singleton()->set_extension_reloading_enabled(true);
  1636. }
  1637. #endif
  1638. // Initialize user data dir.
  1639. OS::get_singleton()->ensure_user_data_dir();
  1640. initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  1641. register_core_extensions(); // core extensions must be registered after globals setup and before display
  1642. ResourceUID::get_singleton()->load_from_cache(true); // load UUIDs from cache.
  1643. if (ProjectSettings::get_singleton()->has_custom_feature("dedicated_server")) {
  1644. audio_driver = NULL_AUDIO_DRIVER;
  1645. display_driver = NULL_DISPLAY_DRIVER;
  1646. }
  1647. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_chars_per_second", PROPERTY_HINT_RANGE, "0, 4096, 1, or_greater"), 32768);
  1648. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_queued_messages", PROPERTY_HINT_RANGE, "0, 8192, 1, or_greater"), 2048);
  1649. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_errors_per_second", PROPERTY_HINT_RANGE, "0, 200, 1, or_greater"), 400);
  1650. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_warnings_per_second", PROPERTY_HINT_RANGE, "0, 200, 1, or_greater"), 400);
  1651. EngineDebugger::initialize(debug_uri, skip_breakpoints, breakpoints, []() {
  1652. if (editor_pid) {
  1653. DisplayServer::get_singleton()->enable_for_stealing_focus(editor_pid);
  1654. }
  1655. });
  1656. #ifdef TOOLS_ENABLED
  1657. if (editor) {
  1658. packed_data->set_disabled(true);
  1659. main_args.push_back("--editor");
  1660. if (!init_windowed && !init_fullscreen) {
  1661. init_maximized = true;
  1662. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  1663. }
  1664. }
  1665. if (!project_manager && !editor) {
  1666. // If we didn't find a project, we fall back to the project manager.
  1667. project_manager = !found_project && !cmdline_tool;
  1668. }
  1669. if (project_manager) {
  1670. Engine::get_singleton()->set_project_manager_hint(true);
  1671. }
  1672. #endif
  1673. GLOBAL_DEF("debug/file_logging/enable_file_logging", false);
  1674. // Only file logging by default on desktop platforms as logs can't be
  1675. // accessed easily on mobile/Web platforms (if at all).
  1676. // This also prevents logs from being created for the editor instance, as feature tags
  1677. // are disabled while in the editor (even if they should logically apply).
  1678. GLOBAL_DEF("debug/file_logging/enable_file_logging.pc", true);
  1679. GLOBAL_DEF("debug/file_logging/log_path", "user://logs/godot.log");
  1680. GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/file_logging/max_log_files", PROPERTY_HINT_RANGE, "0,20,1,or_greater"), 5);
  1681. // If `--log-file` is used to override the log path, allow creating logs for the project manager or editor
  1682. // and even if file logging is disabled in the Project Settings.
  1683. // `--log-file` can be used with any path (including absolute paths outside the project folder),
  1684. // so check for filesystem access if it's used.
  1685. if (FileAccess::get_create_func(!log_file.is_empty() ? FileAccess::ACCESS_FILESYSTEM : FileAccess::ACCESS_USERDATA) &&
  1686. (!log_file.is_empty() || (!project_manager && !editor && GLOBAL_GET("debug/file_logging/enable_file_logging")))) {
  1687. // Don't create logs for the project manager as they would be written to
  1688. // the current working directory, which is inconvenient.
  1689. String base_path;
  1690. int max_files;
  1691. if (!log_file.is_empty()) {
  1692. base_path = log_file;
  1693. // Ensure log file name respects the specified override by disabling log rotation.
  1694. max_files = 1;
  1695. } else {
  1696. base_path = GLOBAL_GET("debug/file_logging/log_path");
  1697. max_files = GLOBAL_GET("debug/file_logging/max_log_files");
  1698. }
  1699. OS::get_singleton()->add_logger(memnew(RotatedFileLogger(base_path, max_files)));
  1700. }
  1701. if (main_args.size() == 0 && String(GLOBAL_GET("application/run/main_scene")) == "") {
  1702. #ifdef TOOLS_ENABLED
  1703. if (!editor && !project_manager) {
  1704. #endif
  1705. const String error_msg = "Error: Can't run project: no main scene defined in the project.\n";
  1706. OS::get_singleton()->print("%s", error_msg.utf8().get_data());
  1707. OS::get_singleton()->alert(error_msg);
  1708. goto error;
  1709. #ifdef TOOLS_ENABLED
  1710. }
  1711. #endif
  1712. }
  1713. if (editor || project_manager) {
  1714. Engine::get_singleton()->set_editor_hint(true);
  1715. use_custom_res = false;
  1716. input_map->load_default(); //keys for editor
  1717. } else {
  1718. input_map->load_from_project_settings(); //keys for game
  1719. }
  1720. if (bool(GLOBAL_GET("application/run/disable_stdout"))) {
  1721. quiet_stdout = true;
  1722. }
  1723. if (bool(GLOBAL_GET("application/run/disable_stderr"))) {
  1724. CoreGlobals::print_error_enabled = false;
  1725. }
  1726. if (!bool(GLOBAL_GET("application/run/print_header"))) {
  1727. // --no-header option for project settings.
  1728. Engine::get_singleton()->_print_header = false;
  1729. }
  1730. if (quiet_stdout) {
  1731. CoreGlobals::print_line_enabled = false;
  1732. }
  1733. Logger::set_flush_stdout_on_print(GLOBAL_GET("application/run/flush_stdout_on_print"));
  1734. OS::get_singleton()->set_cmdline(execpath, main_args, user_args);
  1735. {
  1736. String driver_hints = "";
  1737. String driver_hints_with_d3d12 = "";
  1738. String driver_hints_with_metal = "";
  1739. {
  1740. Vector<String> driver_hints_arr;
  1741. #ifdef VULKAN_ENABLED
  1742. driver_hints_arr.push_back("vulkan");
  1743. #endif
  1744. driver_hints = String(",").join(driver_hints_arr);
  1745. #ifdef D3D12_ENABLED
  1746. driver_hints_arr.push_back("d3d12");
  1747. #endif
  1748. driver_hints_with_d3d12 = String(",").join(driver_hints_arr);
  1749. #ifdef METAL_ENABLED
  1750. // Make metal the preferred and default driver.
  1751. driver_hints_arr.insert(0, "metal");
  1752. #endif
  1753. driver_hints_with_metal = String(",").join(driver_hints_arr);
  1754. }
  1755. String default_driver = driver_hints.get_slice(",", 0);
  1756. String default_driver_with_d3d12 = driver_hints_with_d3d12.get_slice(",", 0);
  1757. String default_driver_with_metal = driver_hints_with_metal.get_slice(",", 0);
  1758. // For now everything defaults to vulkan when available. This can change in future updates.
  1759. GLOBAL_DEF_RST_NOVAL("rendering/rendering_device/driver", default_driver);
  1760. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.windows", PROPERTY_HINT_ENUM, driver_hints_with_d3d12), default_driver_with_d3d12);
  1761. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.linuxbsd", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1762. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.android", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1763. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.ios", PROPERTY_HINT_ENUM, driver_hints_with_metal), default_driver_with_metal);
  1764. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.macos", PROPERTY_HINT_ENUM, driver_hints_with_metal), default_driver_with_metal);
  1765. GLOBAL_DEF_RST("rendering/rendering_device/fallback_to_vulkan", true);
  1766. GLOBAL_DEF_RST("rendering/rendering_device/fallback_to_d3d12", true);
  1767. GLOBAL_DEF_RST("rendering/rendering_device/fallback_to_opengl3", true);
  1768. }
  1769. {
  1770. String driver_hints = "";
  1771. String driver_hints_angle = "";
  1772. String driver_hints_egl = "";
  1773. #ifdef GLES3_ENABLED
  1774. driver_hints = "opengl3";
  1775. driver_hints_angle = "opengl3,opengl3_angle"; // macOS, Windows.
  1776. driver_hints_egl = "opengl3,opengl3_es"; // Linux.
  1777. #endif
  1778. String default_driver = driver_hints.get_slice(",", 0);
  1779. GLOBAL_DEF_RST_NOVAL("rendering/gl_compatibility/driver", default_driver);
  1780. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.windows", PROPERTY_HINT_ENUM, driver_hints_angle), default_driver);
  1781. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.linuxbsd", PROPERTY_HINT_ENUM, driver_hints_egl), default_driver);
  1782. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.web", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1783. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.android", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1784. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.ios", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1785. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.macos", PROPERTY_HINT_ENUM, driver_hints_angle), default_driver);
  1786. GLOBAL_DEF_RST("rendering/gl_compatibility/nvidia_disable_threaded_optimization", true);
  1787. GLOBAL_DEF_RST("rendering/gl_compatibility/fallback_to_angle", true);
  1788. GLOBAL_DEF_RST("rendering/gl_compatibility/fallback_to_native", true);
  1789. GLOBAL_DEF_RST("rendering/gl_compatibility/fallback_to_gles", true);
  1790. Array device_blocklist;
  1791. #define BLOCK_DEVICE(m_vendor, m_name) \
  1792. { \
  1793. Dictionary device; \
  1794. device["vendor"] = m_vendor; \
  1795. device["name"] = m_name; \
  1796. device_blocklist.push_back(device); \
  1797. }
  1798. // AMD GPUs.
  1799. BLOCK_DEVICE("ATI", "Radeon 9"); // ATI Radeon 9000 Series
  1800. BLOCK_DEVICE("ATI", "Radeon X"); // ATI Radeon X500-X2000 Series
  1801. BLOCK_DEVICE("ATI", "Radeon HD 2"); // AMD/ATI (Mobility) Radeon HD 2xxx Series
  1802. BLOCK_DEVICE("ATI", "Radeon HD 3"); // AMD/ATI (Mobility) Radeon HD 3xxx Series
  1803. BLOCK_DEVICE("ATI", "Radeon HD 4"); // AMD/ATI (Mobility) Radeon HD 4xxx Series
  1804. BLOCK_DEVICE("ATI", "Radeon HD 5"); // AMD/ATI (Mobility) Radeon HD 5xxx Series
  1805. BLOCK_DEVICE("ATI", "Radeon HD 6"); // AMD/ATI (Mobility) Radeon HD 6xxx Series
  1806. BLOCK_DEVICE("ATI", "Radeon HD 7"); // AMD/ATI (Mobility) Radeon HD 7xxx Series
  1807. BLOCK_DEVICE("ATI", "Radeon HD 8"); // AMD/ATI (Mobility) Radeon HD 8xxx Series
  1808. BLOCK_DEVICE("ATI", "Radeon(TM) R2 Graphics"); // APUs
  1809. BLOCK_DEVICE("ATI", "Radeon(TM) R3 Graphics");
  1810. BLOCK_DEVICE("ATI", "Radeon(TM) R4 Graphics");
  1811. BLOCK_DEVICE("ATI", "Radeon(TM) R5 Graphics");
  1812. BLOCK_DEVICE("ATI", "Radeon(TM) R6 Graphics");
  1813. BLOCK_DEVICE("ATI", "Radeon(TM) R7 Graphics");
  1814. BLOCK_DEVICE("AMD", "Radeon(TM) R7 Graphics");
  1815. BLOCK_DEVICE("AMD", "Radeon(TM) R8 Graphics");
  1816. BLOCK_DEVICE("ATI", "Radeon R5 Graphics");
  1817. BLOCK_DEVICE("ATI", "Radeon R6 Graphics");
  1818. BLOCK_DEVICE("ATI", "Radeon R7 Graphics");
  1819. BLOCK_DEVICE("AMD", "Radeon R7 Graphics");
  1820. BLOCK_DEVICE("AMD", "Radeon R8 Graphics");
  1821. BLOCK_DEVICE("ATI", "Radeon R5 2"); // Rx 2xx Series
  1822. BLOCK_DEVICE("ATI", "Radeon R7 2");
  1823. BLOCK_DEVICE("ATI", "Radeon R9 2");
  1824. BLOCK_DEVICE("ATI", "Radeon R5 M2"); // Rx M2xx Series
  1825. BLOCK_DEVICE("ATI", "Radeon R7 M2");
  1826. BLOCK_DEVICE("ATI", "Radeon R9 M2");
  1827. BLOCK_DEVICE("ATI", "Radeon (TM) R9 Fury");
  1828. BLOCK_DEVICE("ATI", "Radeon (TM) R5 3"); // Rx 3xx Series
  1829. BLOCK_DEVICE("AMD", "Radeon (TM) R5 3");
  1830. BLOCK_DEVICE("ATI", "Radeon (TM) R7 3");
  1831. BLOCK_DEVICE("AMD", "Radeon (TM) R7 3");
  1832. BLOCK_DEVICE("ATI", "Radeon (TM) R9 3");
  1833. BLOCK_DEVICE("AMD", "Radeon (TM) R9 3");
  1834. BLOCK_DEVICE("ATI", "Radeon (TM) R5 M3"); // Rx M3xx Series
  1835. BLOCK_DEVICE("AMD", "Radeon (TM) R5 M3");
  1836. BLOCK_DEVICE("ATI", "Radeon (TM) R7 M3");
  1837. BLOCK_DEVICE("AMD", "Radeon (TM) R7 M3");
  1838. BLOCK_DEVICE("ATI", "Radeon (TM) R9 M3");
  1839. BLOCK_DEVICE("AMD", "Radeon (TM) R9 M3");
  1840. // Intel GPUs.
  1841. BLOCK_DEVICE("0x8086", "0x0042"); // HD Graphics, Gen5, Clarkdale
  1842. BLOCK_DEVICE("0x8086", "0x0046"); // HD Graphics, Gen5, Arrandale
  1843. BLOCK_DEVICE("0x8086", "0x010A"); // HD Graphics, Gen6, Sandy Bridge
  1844. BLOCK_DEVICE("Intel", "Intel HD Graphics 2000");
  1845. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 2000");
  1846. BLOCK_DEVICE("0x8086", "0x0102"); // HD Graphics 2000, Gen6, Sandy Bridge
  1847. BLOCK_DEVICE("0x8086", "0x0116"); // HD Graphics 3000, Gen6, Sandy Bridge
  1848. BLOCK_DEVICE("Intel", "Intel HD Graphics 3000");
  1849. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 3000");
  1850. BLOCK_DEVICE("0x8086", "0x0126"); // HD Graphics 3000, Gen6, Sandy Bridge
  1851. BLOCK_DEVICE("Intel", "Intel HD Graphics P3000");
  1852. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics P3000");
  1853. BLOCK_DEVICE("0x8086", "0x0112"); // HD Graphics P3000, Gen6, Sandy Bridge
  1854. BLOCK_DEVICE("0x8086", "0x0122");
  1855. BLOCK_DEVICE("0x8086", "0x015A"); // HD Graphics, Gen7, Ivy Bridge
  1856. BLOCK_DEVICE("Intel", "Intel HD Graphics 2500");
  1857. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 2500");
  1858. BLOCK_DEVICE("0x8086", "0x0152"); // HD Graphics 2500, Gen7, Ivy Bridge
  1859. BLOCK_DEVICE("Intel", "Intel HD Graphics 4000");
  1860. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 4000");
  1861. BLOCK_DEVICE("0x8086", "0x0162"); // HD Graphics 4000, Gen7, Ivy Bridge
  1862. BLOCK_DEVICE("0x8086", "0x0166");
  1863. BLOCK_DEVICE("Intel", "Intel HD Graphics P4000");
  1864. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics P4000");
  1865. BLOCK_DEVICE("0x8086", "0x016A"); // HD Graphics P4000, Gen7, Ivy Bridge
  1866. BLOCK_DEVICE("Intel", "Intel(R) Vallyview Graphics");
  1867. BLOCK_DEVICE("0x8086", "0x0F30"); // Intel(R) Vallyview Graphics, Gen7, Vallyview
  1868. BLOCK_DEVICE("0x8086", "0x0F31");
  1869. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 4200");
  1870. BLOCK_DEVICE("0x8086", "0x0A1E"); // Intel(R) HD Graphics 4200, Gen7.5, Haswell
  1871. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 4400");
  1872. BLOCK_DEVICE("0x8086", "0x0A16"); // Intel(R) HD Graphics 4400, Gen7.5, Haswell
  1873. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 4600");
  1874. BLOCK_DEVICE("0x8086", "0x0412"); // Intel(R) HD Graphics 4600, Gen7.5, Haswell
  1875. BLOCK_DEVICE("0x8086", "0x0416");
  1876. BLOCK_DEVICE("0x8086", "0x0426");
  1877. BLOCK_DEVICE("0x8086", "0x0D12");
  1878. BLOCK_DEVICE("0x8086", "0x0D16");
  1879. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics P4600/P4700");
  1880. BLOCK_DEVICE("0x8086", "0x041A"); // Intel(R) HD Graphics P4600/P4700, Gen7.5, Haswell
  1881. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 5000");
  1882. BLOCK_DEVICE("0x8086", "0x0422"); // Intel(R) HD Graphics 5000, Gen7.5, Haswell
  1883. BLOCK_DEVICE("0x8086", "0x042A");
  1884. BLOCK_DEVICE("0x8086", "0x0A26");
  1885. BLOCK_DEVICE("Intel", "Intel(R) Iris(TM) Graphics 5100");
  1886. BLOCK_DEVICE("0x8086", "0x0A22"); // Intel(R) Iris(TM) Graphics 5100, Gen7.5, Haswell
  1887. BLOCK_DEVICE("0x8086", "0x0A2A");
  1888. BLOCK_DEVICE("0x8086", "0x0A2B");
  1889. BLOCK_DEVICE("0x8086", "0x0A2E");
  1890. BLOCK_DEVICE("Intel", "Intel(R) Iris(TM) Pro Graphics 5200");
  1891. BLOCK_DEVICE("0x8086", "0x0D22"); // Intel(R) Iris(TM) Pro Graphics 5200, Gen7.5, Haswell
  1892. BLOCK_DEVICE("0x8086", "0x0D26");
  1893. BLOCK_DEVICE("0x8086", "0x0D2A");
  1894. BLOCK_DEVICE("0x8086", "0x0D2B");
  1895. BLOCK_DEVICE("0x8086", "0x0D2E");
  1896. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 400");
  1897. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 405");
  1898. BLOCK_DEVICE("0x8086", "0x22B0"); // Intel(R) HD Graphics, Gen8, Cherryview Braswell
  1899. BLOCK_DEVICE("0x8086", "0x22B1");
  1900. BLOCK_DEVICE("0x8086", "0x22B2");
  1901. BLOCK_DEVICE("0x8086", "0x22B3");
  1902. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 5300");
  1903. BLOCK_DEVICE("0x8086", "0x161E"); // Intel(R) HD Graphics 5300, Gen8, Broadwell
  1904. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 5500");
  1905. BLOCK_DEVICE("0x8086", "0x1616"); // Intel(R) HD Graphics 5500, Gen8, Broadwell
  1906. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 5600");
  1907. BLOCK_DEVICE("0x8086", "0x1612"); // Intel(R) HD Graphics 5600, Gen8, Broadwell
  1908. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 6000");
  1909. BLOCK_DEVICE("0x8086", "0x1626"); // Intel(R) HD Graphics 6000, Gen8, Broadwell
  1910. BLOCK_DEVICE("Intel", "Intel(R) Iris(TM) Graphics 6100");
  1911. BLOCK_DEVICE("0x8086", "0x162B"); // Intel(R) Iris(TM) Graphics 6100, Gen8, Broadwell
  1912. BLOCK_DEVICE("Intel", "Intel(R) Iris(TM) Pro Graphics 6200");
  1913. BLOCK_DEVICE("0x8086", "0x1622"); // Intel(R) Iris(TM) Pro Graphics 6200, Gen8, Broadwell
  1914. BLOCK_DEVICE("Intel", "Intel(R) Iris(TM) Pro Graphics P6300");
  1915. BLOCK_DEVICE("0x8086", "0x162A"); // Intel(R) Iris(TM) Pro Graphics P6300, Gen8, Broadwell
  1916. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 500");
  1917. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 505");
  1918. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 510");
  1919. BLOCK_DEVICE("0x8086", "0x1902"); // Intel(R) HD Graphics 510, Gen9, Skylake
  1920. BLOCK_DEVICE("0x8086", "0x1906");
  1921. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 520");
  1922. BLOCK_DEVICE("0x8086", "0x1916"); // Intel(R) HD Graphics 520, Gen9, Skylake
  1923. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 530");
  1924. BLOCK_DEVICE("0x8086", "0x1912"); // Intel(R) HD Graphics 530, Gen9, Skylake
  1925. BLOCK_DEVICE("0x8086", "0x191B");
  1926. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics P530");
  1927. BLOCK_DEVICE("0x8086", "0x191D"); // Intel(R) HD Graphics P530, Gen9, Skylake
  1928. BLOCK_DEVICE("Intel", "Intel(R) HD Graphics 515");
  1929. BLOCK_DEVICE("0x8086", "0x191E"); // Intel(R) HD Graphics 515, Gen9, Skylake
  1930. BLOCK_DEVICE("Intel", "Intel(R) Iris Graphics 540");
  1931. BLOCK_DEVICE("0x8086", "0x1926"); // Intel(R) Iris Graphics 540, Gen9, Skylake
  1932. BLOCK_DEVICE("0x8086", "0x1927");
  1933. BLOCK_DEVICE("Intel", "Intel(R) Iris Pro Graphics 580");
  1934. BLOCK_DEVICE("0x8086", "0x193B"); // Intel(R) Iris Pro Graphics 580, Gen9, Skylake
  1935. BLOCK_DEVICE("Intel", "Intel(R) Iris Pro Graphics P580");
  1936. BLOCK_DEVICE("0x8086", "0x193D"); // Intel(R) Iris Pro Graphics P580, Gen9, Skylake
  1937. #undef BLOCK_DEVICE
  1938. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::ARRAY, "rendering/gl_compatibility/force_angle_on_devices", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::DICTIONARY, PROPERTY_HINT_NONE, String())), device_blocklist);
  1939. }
  1940. // Start with RenderingDevice-based backends.
  1941. #ifdef RD_ENABLED
  1942. renderer_hints = "forward_plus,mobile";
  1943. default_renderer_mobile = "mobile";
  1944. #endif
  1945. // And Compatibility next, or first if Vulkan is disabled.
  1946. #ifdef GLES3_ENABLED
  1947. if (!renderer_hints.is_empty()) {
  1948. renderer_hints += ",";
  1949. }
  1950. renderer_hints += "gl_compatibility";
  1951. if (default_renderer_mobile.is_empty()) {
  1952. default_renderer_mobile = "gl_compatibility";
  1953. }
  1954. // Default to Compatibility when using the project manager.
  1955. if (rendering_driver.is_empty() && rendering_method.is_empty() && project_manager) {
  1956. rendering_driver = "opengl3";
  1957. rendering_method = "gl_compatibility";
  1958. default_renderer_mobile = "gl_compatibility";
  1959. }
  1960. #endif
  1961. if (renderer_hints.is_empty()) {
  1962. ERR_PRINT("No renderers available.");
  1963. }
  1964. if (!rendering_method.is_empty()) {
  1965. if (rendering_method != "forward_plus" &&
  1966. rendering_method != "mobile" &&
  1967. rendering_method != "gl_compatibility") {
  1968. OS::get_singleton()->print("Unknown rendering method '%s', aborting.\nValid options are ",
  1969. rendering_method.utf8().get_data());
  1970. const Vector<String> rendering_method_hints = renderer_hints.split(",");
  1971. for (int i = 0; i < rendering_method_hints.size(); i++) {
  1972. if (i == rendering_method_hints.size() - 1) {
  1973. OS::get_singleton()->print(" and ");
  1974. } else if (i != 0) {
  1975. OS::get_singleton()->print(", ");
  1976. }
  1977. OS::get_singleton()->print("'%s'", rendering_method_hints[i].utf8().get_data());
  1978. }
  1979. OS::get_singleton()->print(".\n");
  1980. goto error;
  1981. }
  1982. }
  1983. if (!rendering_driver.is_empty()) {
  1984. // As the rendering drivers available may depend on the display driver and renderer
  1985. // selected, we can't do an exhaustive check here, but we can look through all
  1986. // the options in all the display drivers for a match.
  1987. bool found = false;
  1988. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1989. Vector<String> r_drivers = DisplayServer::get_create_function_rendering_drivers(i);
  1990. for (int d = 0; d < r_drivers.size(); d++) {
  1991. if (rendering_driver == r_drivers[d]) {
  1992. found = true;
  1993. break;
  1994. }
  1995. }
  1996. }
  1997. if (!found) {
  1998. OS::get_singleton()->print("Unknown rendering driver '%s', aborting.\nValid options are ",
  1999. rendering_driver.utf8().get_data());
  2000. // Deduplicate driver entries, as a rendering driver may be supported by several display servers.
  2001. Vector<String> unique_rendering_drivers;
  2002. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  2003. Vector<String> r_drivers = DisplayServer::get_create_function_rendering_drivers(i);
  2004. for (int d = 0; d < r_drivers.size(); d++) {
  2005. if (!unique_rendering_drivers.has(r_drivers[d])) {
  2006. unique_rendering_drivers.append(r_drivers[d]);
  2007. }
  2008. }
  2009. }
  2010. for (int i = 0; i < unique_rendering_drivers.size(); i++) {
  2011. if (i == unique_rendering_drivers.size() - 1) {
  2012. OS::get_singleton()->print(" and ");
  2013. } else if (i != 0) {
  2014. OS::get_singleton()->print(", ");
  2015. }
  2016. OS::get_singleton()->print("'%s'", unique_rendering_drivers[i].utf8().get_data());
  2017. }
  2018. OS::get_singleton()->print(".\n");
  2019. goto error;
  2020. }
  2021. // Set a default renderer if none selected. Try to choose one that matches the driver.
  2022. if (rendering_method.is_empty()) {
  2023. if (rendering_driver == "opengl3" || rendering_driver == "opengl3_angle" || rendering_driver == "opengl3_es") {
  2024. rendering_method = "gl_compatibility";
  2025. } else {
  2026. rendering_method = "forward_plus";
  2027. }
  2028. }
  2029. // Now validate whether the selected driver matches with the renderer.
  2030. bool valid_combination = false;
  2031. Vector<String> available_drivers;
  2032. if (rendering_method == "forward_plus" || rendering_method == "mobile") {
  2033. #ifdef VULKAN_ENABLED
  2034. available_drivers.push_back("vulkan");
  2035. #endif
  2036. #ifdef D3D12_ENABLED
  2037. available_drivers.push_back("d3d12");
  2038. #endif
  2039. #ifdef METAL_ENABLED
  2040. available_drivers.push_back("metal");
  2041. #endif
  2042. }
  2043. #ifdef GLES3_ENABLED
  2044. if (rendering_method == "gl_compatibility") {
  2045. available_drivers.push_back("opengl3");
  2046. available_drivers.push_back("opengl3_angle");
  2047. available_drivers.push_back("opengl3_es");
  2048. }
  2049. #endif
  2050. if (available_drivers.is_empty()) {
  2051. OS::get_singleton()->print("Unknown renderer name '%s', aborting.\n", rendering_method.utf8().get_data());
  2052. goto error;
  2053. }
  2054. for (int i = 0; i < available_drivers.size(); i++) {
  2055. if (rendering_driver == available_drivers[i]) {
  2056. valid_combination = true;
  2057. break;
  2058. }
  2059. }
  2060. if (!valid_combination) {
  2061. OS::get_singleton()->print("Invalid renderer/driver combination '%s' and '%s', aborting. %s only supports the following drivers ", rendering_method.utf8().get_data(), rendering_driver.utf8().get_data(), rendering_method.utf8().get_data());
  2062. for (int d = 0; d < available_drivers.size(); d++) {
  2063. OS::get_singleton()->print("'%s', ", available_drivers[d].utf8().get_data());
  2064. }
  2065. OS::get_singleton()->print(".\n");
  2066. goto error;
  2067. }
  2068. }
  2069. default_renderer = renderer_hints.get_slice(",", 0);
  2070. GLOBAL_DEF_RST_BASIC(PropertyInfo(Variant::STRING, "rendering/renderer/rendering_method", PROPERTY_HINT_ENUM, renderer_hints), default_renderer);
  2071. GLOBAL_DEF_RST_BASIC("rendering/renderer/rendering_method.mobile", default_renderer_mobile);
  2072. GLOBAL_DEF_RST_BASIC("rendering/renderer/rendering_method.web", "gl_compatibility"); // This is a bit of a hack until we have WebGPU support.
  2073. // Default to ProjectSettings default if nothing set on the command line.
  2074. if (rendering_method.is_empty()) {
  2075. rendering_method = GLOBAL_GET("rendering/renderer/rendering_method");
  2076. }
  2077. if (rendering_driver.is_empty()) {
  2078. if (rendering_method == "gl_compatibility") {
  2079. rendering_driver = GLOBAL_GET("rendering/gl_compatibility/driver");
  2080. } else {
  2081. rendering_driver = GLOBAL_GET("rendering/rendering_device/driver");
  2082. }
  2083. }
  2084. // always convert to lower case for consistency in the code
  2085. rendering_driver = rendering_driver.to_lower();
  2086. OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
  2087. OS::get_singleton()->set_current_rendering_method(rendering_method);
  2088. if (use_custom_res) {
  2089. if (!force_res) {
  2090. window_size.width = GLOBAL_GET("display/window/size/viewport_width");
  2091. window_size.height = GLOBAL_GET("display/window/size/viewport_height");
  2092. if (globals->has_setting("display/window/size/window_width_override") &&
  2093. globals->has_setting("display/window/size/window_height_override")) {
  2094. int desired_width = GLOBAL_GET("display/window/size/window_width_override");
  2095. if (desired_width > 0) {
  2096. window_size.width = desired_width;
  2097. }
  2098. int desired_height = GLOBAL_GET("display/window/size/window_height_override");
  2099. if (desired_height > 0) {
  2100. window_size.height = desired_height;
  2101. }
  2102. }
  2103. }
  2104. if (!bool(GLOBAL_GET("display/window/size/resizable"))) {
  2105. window_flags |= DisplayServer::WINDOW_FLAG_RESIZE_DISABLED_BIT;
  2106. }
  2107. if (bool(GLOBAL_GET("display/window/size/borderless"))) {
  2108. window_flags |= DisplayServer::WINDOW_FLAG_BORDERLESS_BIT;
  2109. }
  2110. if (bool(GLOBAL_GET("display/window/size/always_on_top"))) {
  2111. window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP_BIT;
  2112. }
  2113. if (bool(GLOBAL_GET("display/window/size/transparent"))) {
  2114. window_flags |= DisplayServer::WINDOW_FLAG_TRANSPARENT_BIT;
  2115. }
  2116. if (bool(GLOBAL_GET("display/window/size/extend_to_title"))) {
  2117. window_flags |= DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE_BIT;
  2118. }
  2119. if (bool(GLOBAL_GET("display/window/size/no_focus"))) {
  2120. window_flags |= DisplayServer::WINDOW_FLAG_NO_FOCUS_BIT;
  2121. }
  2122. if (bool(GLOBAL_GET("display/window/size/sharp_corners"))) {
  2123. window_flags |= DisplayServer::WINDOW_FLAG_SHARP_CORNERS_BIT;
  2124. }
  2125. window_mode = (DisplayServer::WindowMode)(GLOBAL_GET("display/window/size/mode").operator int());
  2126. int initial_position_type = GLOBAL_GET("display/window/size/initial_position_type").operator int();
  2127. if (initial_position_type == 0) { // Absolute.
  2128. if (!init_use_custom_pos) {
  2129. init_custom_pos = GLOBAL_GET("display/window/size/initial_position").operator Vector2i();
  2130. init_use_custom_pos = true;
  2131. }
  2132. } else if (initial_position_type == 1) { // Center of Primary Screen.
  2133. if (!init_use_custom_screen) {
  2134. init_screen = DisplayServer::SCREEN_PRIMARY;
  2135. init_use_custom_screen = true;
  2136. }
  2137. } else if (initial_position_type == 2) { // Center of Other Screen.
  2138. if (!init_use_custom_screen) {
  2139. init_screen = GLOBAL_GET("display/window/size/initial_screen").operator int();
  2140. init_use_custom_screen = true;
  2141. }
  2142. } else if (initial_position_type == 3) { // Center of Screen With Mouse Pointer.
  2143. if (!init_use_custom_screen) {
  2144. init_screen = DisplayServer::SCREEN_WITH_MOUSE_FOCUS;
  2145. init_use_custom_screen = true;
  2146. }
  2147. } else if (initial_position_type == 4) { // Center of Screen With Keyboard Focus.
  2148. if (!init_use_custom_screen) {
  2149. init_screen = DisplayServer::SCREEN_WITH_KEYBOARD_FOCUS;
  2150. init_use_custom_screen = true;
  2151. }
  2152. }
  2153. }
  2154. GLOBAL_DEF("internationalization/locale/include_text_server_data", false);
  2155. OS::get_singleton()->_allow_hidpi = GLOBAL_DEF("display/window/dpi/allow_hidpi", true);
  2156. OS::get_singleton()->_allow_layered = GLOBAL_DEF("display/window/per_pixel_transparency/allowed", false);
  2157. #ifdef TOOLS_ENABLED
  2158. if (editor || project_manager) {
  2159. // The editor and project manager always detect and use hiDPI if needed.
  2160. OS::get_singleton()->_allow_hidpi = true;
  2161. // Disable Vulkan overlays in editor, they cause various issues.
  2162. OS::get_singleton()->set_environment("DISABLE_MANGOHUD", "1"); // GH-57403.
  2163. OS::get_singleton()->set_environment("DISABLE_RTSS_LAYER", "1"); // GH-57937.
  2164. OS::get_singleton()->set_environment("DISABLE_VKBASALT", "1");
  2165. OS::get_singleton()->set_environment("DISABLE_VK_LAYER_reshade_1", "1"); // GH-70849.
  2166. } else {
  2167. // Re-allow using Vulkan overlays, disabled while using the editor.
  2168. OS::get_singleton()->unset_environment("DISABLE_MANGOHUD");
  2169. OS::get_singleton()->unset_environment("DISABLE_RTSS_LAYER");
  2170. OS::get_singleton()->unset_environment("DISABLE_VKBASALT");
  2171. OS::get_singleton()->unset_environment("DISABLE_VK_LAYER_reshade_1");
  2172. }
  2173. #endif
  2174. if (rtm == -1) {
  2175. rtm = GLOBAL_DEF("rendering/driver/threads/thread_model", OS::RENDER_THREAD_SAFE);
  2176. }
  2177. if (rtm >= 0 && rtm < 3) {
  2178. if (editor || project_manager) {
  2179. // Editor and project manager cannot run with rendering in a separate thread (they will crash on startup).
  2180. rtm = OS::RENDER_THREAD_SAFE;
  2181. }
  2182. #if !defined(THREADS_ENABLED)
  2183. rtm = OS::RENDER_THREAD_SAFE;
  2184. #endif
  2185. OS::get_singleton()->_render_thread_mode = OS::RenderThreadMode(rtm);
  2186. }
  2187. /* Determine audio and video drivers */
  2188. // Display driver, e.g. X11, Wayland.
  2189. // Make sure that headless is the last one, which it is assumed to be by design.
  2190. DEV_ASSERT(NULL_DISPLAY_DRIVER == DisplayServer::get_create_function_name(DisplayServer::get_create_function_count() - 1));
  2191. GLOBAL_DEF_NOVAL("display/display_server/driver", "default");
  2192. GLOBAL_DEF_NOVAL(PropertyInfo(Variant::STRING, "display/display_server/driver.windows", PROPERTY_HINT_ENUM_SUGGESTION, "default,windows,headless"), "default");
  2193. GLOBAL_DEF_NOVAL(PropertyInfo(Variant::STRING, "display/display_server/driver.linuxbsd", PROPERTY_HINT_ENUM_SUGGESTION, "default,x11,wayland,headless"), "default");
  2194. GLOBAL_DEF_NOVAL(PropertyInfo(Variant::STRING, "display/display_server/driver.android", PROPERTY_HINT_ENUM_SUGGESTION, "default,android,headless"), "default");
  2195. GLOBAL_DEF_NOVAL(PropertyInfo(Variant::STRING, "display/display_server/driver.ios", PROPERTY_HINT_ENUM_SUGGESTION, "default,iOS,headless"), "default");
  2196. GLOBAL_DEF_NOVAL(PropertyInfo(Variant::STRING, "display/display_server/driver.macos", PROPERTY_HINT_ENUM_SUGGESTION, "default,macos,headless"), "default");
  2197. GLOBAL_DEF_RST_NOVAL("audio/driver/driver", AudioDriverManager::get_driver(0)->get_name());
  2198. if (audio_driver.is_empty()) { // Specified in project.godot.
  2199. if (project_manager) {
  2200. // The project manager doesn't need to play sound (TTS audio output is not emitted by Godot, but by the system itself).
  2201. // Disable audio output so it doesn't appear in the list of applications outputting sound in the OS.
  2202. // On macOS, this also prevents the project manager from inhibiting suspend.
  2203. audio_driver = "Dummy";
  2204. } else {
  2205. audio_driver = GLOBAL_GET("audio/driver/driver");
  2206. }
  2207. }
  2208. // Make sure that dummy is the last one, which it is assumed to be by design.
  2209. DEV_ASSERT(NULL_AUDIO_DRIVER == AudioDriverManager::get_driver(AudioDriverManager::get_driver_count() - 1)->get_name());
  2210. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  2211. if (audio_driver == AudioDriverManager::get_driver(i)->get_name()) {
  2212. audio_driver_idx = i;
  2213. break;
  2214. }
  2215. }
  2216. if (audio_driver_idx < 0) {
  2217. // If the requested driver wasn't found, pick the first entry.
  2218. // If all else failed it would be the dummy driver (no sound).
  2219. audio_driver_idx = 0;
  2220. }
  2221. if (Engine::get_singleton()->get_write_movie_path() != String()) {
  2222. // Always use dummy driver for audio driver (which is last), also in no threaded mode.
  2223. audio_driver_idx = AudioDriverManager::get_driver_count() - 1;
  2224. AudioDriverDummy::get_dummy_singleton()->set_use_threads(false);
  2225. }
  2226. {
  2227. window_orientation = DisplayServer::ScreenOrientation(int(GLOBAL_DEF_BASIC("display/window/handheld/orientation", DisplayServer::ScreenOrientation::SCREEN_LANDSCAPE)));
  2228. }
  2229. {
  2230. window_vsync_mode = DisplayServer::VSyncMode(int(GLOBAL_DEF_BASIC("display/window/vsync/vsync_mode", DisplayServer::VSyncMode::VSYNC_ENABLED)));
  2231. if (disable_vsync) {
  2232. window_vsync_mode = DisplayServer::VSyncMode::VSYNC_DISABLED;
  2233. }
  2234. }
  2235. Engine::get_singleton()->set_physics_ticks_per_second(GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "physics/common/physics_ticks_per_second", PROPERTY_HINT_RANGE, "1,1000,1"), 60));
  2236. Engine::get_singleton()->set_max_physics_steps_per_frame(GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "physics/common/max_physics_steps_per_frame", PROPERTY_HINT_RANGE, "1,100,1"), 8));
  2237. Engine::get_singleton()->set_physics_jitter_fix(GLOBAL_DEF("physics/common/physics_jitter_fix", 0.5));
  2238. GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/driver/output_latency", PROPERTY_HINT_RANGE, "1,100,1"), 15);
  2239. // Use a safer default output_latency for web to avoid audio cracking on low-end devices, especially mobile.
  2240. GLOBAL_DEF_RST("audio/driver/output_latency.web", 50);
  2241. Engine::get_singleton()->set_audio_output_latency(GLOBAL_GET("audio/driver/output_latency"));
  2242. GLOBAL_DEF("debug/settings/stdout/print_fps", false);
  2243. GLOBAL_DEF("debug/settings/stdout/print_gpu_profile", false);
  2244. GLOBAL_DEF("debug/settings/stdout/verbose_stdout", false);
  2245. GLOBAL_DEF("debug/settings/physics_interpolation/enable_warnings", true);
  2246. if (!OS::get_singleton()->_verbose_stdout) { // Not manually overridden.
  2247. OS::get_singleton()->_verbose_stdout = GLOBAL_GET("debug/settings/stdout/verbose_stdout");
  2248. }
  2249. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  2250. OS::get_singleton()->set_environment("MVK_CONFIG_LOG_LEVEL", OS::get_singleton()->_verbose_stdout ? "3" : "1"); // 1 = Errors only, 3 = Info
  2251. #endif
  2252. if (frame_delay == 0) {
  2253. frame_delay = GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/frame_delay_msec", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), 0);
  2254. if (Engine::get_singleton()->is_editor_hint()) {
  2255. frame_delay = 0;
  2256. }
  2257. }
  2258. if (audio_output_latency >= 1) {
  2259. Engine::get_singleton()->set_audio_output_latency(audio_output_latency);
  2260. }
  2261. OS::get_singleton()->set_low_processor_usage_mode(GLOBAL_DEF("application/run/low_processor_mode", false));
  2262. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(
  2263. GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/low_processor_mode_sleep_usec", PROPERTY_HINT_RANGE, "0,33200,1,or_greater"), 6900)); // Roughly 144 FPS
  2264. GLOBAL_DEF("application/run/delta_smoothing", true);
  2265. if (!delta_smoothing_override) {
  2266. OS::get_singleton()->set_delta_smoothing(GLOBAL_GET("application/run/delta_smoothing"));
  2267. }
  2268. GLOBAL_DEF("display/window/ios/allow_high_refresh_rate", true);
  2269. GLOBAL_DEF("display/window/ios/hide_home_indicator", true);
  2270. GLOBAL_DEF("display/window/ios/hide_status_bar", true);
  2271. GLOBAL_DEF("display/window/ios/suppress_ui_gesture", true);
  2272. #ifndef _3D_DISABLED
  2273. // XR project settings.
  2274. GLOBAL_DEF_RST_BASIC("xr/openxr/enabled", false);
  2275. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "xr/openxr/default_action_map", PROPERTY_HINT_FILE, "*.tres"), "res://openxr_action_map.tres");
  2276. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/form_factor", PROPERTY_HINT_ENUM, "Head Mounted,Handheld"), "0");
  2277. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/view_configuration", PROPERTY_HINT_ENUM, "Mono,Stereo"), "1"); // "Mono,Stereo,Quad,Observer"
  2278. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/reference_space", PROPERTY_HINT_ENUM, "Local,Stage,Local Floor"), "1");
  2279. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/environment_blend_mode", PROPERTY_HINT_ENUM, "Opaque,Additive,Alpha"), "0");
  2280. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/foveation_level", PROPERTY_HINT_ENUM, "Off,Low,Medium,High"), "0");
  2281. GLOBAL_DEF_BASIC("xr/openxr/foveation_dynamic", false);
  2282. GLOBAL_DEF_BASIC("xr/openxr/submit_depth_buffer", false);
  2283. GLOBAL_DEF_BASIC("xr/openxr/startup_alert", true);
  2284. // OpenXR project extensions settings.
  2285. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/extensions/debug_utils", PROPERTY_HINT_ENUM, "Disabled,Error,Warning,Info,Verbose"), "0");
  2286. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/extensions/debug_message_types", PROPERTY_HINT_FLAGS, "General,Validation,Performance,Conformance"), "15");
  2287. GLOBAL_DEF_BASIC("xr/openxr/extensions/hand_tracking", false);
  2288. GLOBAL_DEF_BASIC("xr/openxr/extensions/hand_tracking_unobstructed_data_source", false); // XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT
  2289. GLOBAL_DEF_BASIC("xr/openxr/extensions/hand_tracking_controller_data_source", false); // XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT
  2290. GLOBAL_DEF_RST_BASIC("xr/openxr/extensions/hand_interaction_profile", false);
  2291. GLOBAL_DEF_BASIC("xr/openxr/extensions/eye_gaze_interaction", false);
  2292. #ifdef TOOLS_ENABLED
  2293. // Disabled for now, using XR inside of the editor we'll be working on during the coming months.
  2294. // editor settings (it seems we're too early in the process when setting up rendering, to access editor settings...)
  2295. // EDITOR_DEF_RST("xr/openxr/in_editor", false);
  2296. // GLOBAL_DEF("xr/openxr/in_editor", false);
  2297. #endif // TOOLS_ENABLED
  2298. #endif // _3D_DISABLED
  2299. Engine::get_singleton()->set_frame_delay(frame_delay);
  2300. message_queue = memnew(MessageQueue);
  2301. Thread::release_main_thread(); // If setup2() is called from another thread, that one will become main thread, so preventively release this one.
  2302. set_current_thread_safe_for_nodes(false);
  2303. #if defined(STEAMAPI_ENABLED)
  2304. if (editor || project_manager) {
  2305. steam_tracker = memnew(SteamTracker);
  2306. }
  2307. #endif
  2308. OS::get_singleton()->benchmark_end_measure("Startup", "Main::Setup");
  2309. if (p_second_phase) {
  2310. exit_err = setup2();
  2311. if (exit_err != OK) {
  2312. goto error;
  2313. }
  2314. }
  2315. return OK;
  2316. error:
  2317. text_driver = "";
  2318. display_driver = "";
  2319. audio_driver = "";
  2320. tablet_driver = "";
  2321. Engine::get_singleton()->set_write_movie_path(String());
  2322. project_path = "";
  2323. args.clear();
  2324. main_args.clear();
  2325. if (show_help) {
  2326. print_help(execpath);
  2327. }
  2328. EngineDebugger::deinitialize();
  2329. if (performance) {
  2330. memdelete(performance);
  2331. }
  2332. if (input_map) {
  2333. memdelete(input_map);
  2334. }
  2335. if (translation_server) {
  2336. memdelete(translation_server);
  2337. }
  2338. if (globals) {
  2339. memdelete(globals);
  2340. }
  2341. if (packed_data) {
  2342. memdelete(packed_data);
  2343. }
  2344. unregister_core_driver_types();
  2345. unregister_core_extensions();
  2346. if (engine) {
  2347. memdelete(engine);
  2348. }
  2349. unregister_core_types();
  2350. OS::get_singleton()->_cmdline.clear();
  2351. OS::get_singleton()->_user_args.clear();
  2352. if (message_queue) {
  2353. memdelete(message_queue);
  2354. }
  2355. OS::get_singleton()->benchmark_end_measure("Startup", "Main::Setup");
  2356. #if defined(STEAMAPI_ENABLED)
  2357. if (steam_tracker) {
  2358. memdelete(steam_tracker);
  2359. }
  2360. #endif
  2361. OS::get_singleton()->finalize_core();
  2362. locale = String();
  2363. return exit_err;
  2364. }
  2365. Error _parse_resource_dummy(void *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) {
  2366. VariantParser::Token token;
  2367. VariantParser::get_token(p_stream, token, line, r_err_str);
  2368. if (token.type != VariantParser::TK_NUMBER && token.type != VariantParser::TK_STRING) {
  2369. r_err_str = "Expected number (old style sub-resource index) or String (ext-resource ID)";
  2370. return ERR_PARSE_ERROR;
  2371. }
  2372. r_res.unref();
  2373. VariantParser::get_token(p_stream, token, line, r_err_str);
  2374. if (token.type != VariantParser::TK_PARENTHESIS_CLOSE) {
  2375. r_err_str = "Expected ')'";
  2376. return ERR_PARSE_ERROR;
  2377. }
  2378. return OK;
  2379. }
  2380. Error Main::setup2(bool p_show_boot_logo) {
  2381. OS::get_singleton()->benchmark_begin_measure("Startup", "Main::Setup2");
  2382. Thread::make_main_thread(); // Make whatever thread call this the main thread.
  2383. set_current_thread_safe_for_nodes(true);
  2384. // Don't use rich formatting to prevent ANSI escape codes from being written to log files.
  2385. print_header(false);
  2386. #ifdef TOOLS_ENABLED
  2387. if (editor || project_manager || cmdline_tool) {
  2388. OS::get_singleton()->benchmark_begin_measure("Startup", "Initialize Early Settings");
  2389. EditorPaths::create();
  2390. // Editor setting class is not available, load config directly.
  2391. if (!init_use_custom_screen && (editor || project_manager) && EditorPaths::get_singleton()->are_paths_valid()) {
  2392. ERR_FAIL_COND_V(!DirAccess::dir_exists_absolute(EditorPaths::get_singleton()->get_config_dir()), FAILED);
  2393. String config_file_path = EditorSettings::get_existing_settings_path();
  2394. if (FileAccess::exists(config_file_path)) {
  2395. Error err;
  2396. Ref<FileAccess> f = FileAccess::open(config_file_path, FileAccess::READ, &err);
  2397. if (f.is_valid()) {
  2398. VariantParser::StreamFile stream;
  2399. stream.f = f;
  2400. String assign;
  2401. Variant value;
  2402. VariantParser::Tag next_tag;
  2403. int lines = 0;
  2404. String error_text;
  2405. VariantParser::ResourceParser rp_new;
  2406. rp_new.ext_func = _parse_resource_dummy;
  2407. rp_new.sub_func = _parse_resource_dummy;
  2408. bool screen_found = false;
  2409. String screen_property;
  2410. bool prefer_wayland_found = false;
  2411. bool prefer_wayland = false;
  2412. if (editor) {
  2413. screen_property = "interface/editor/editor_screen";
  2414. } else if (project_manager) {
  2415. screen_property = "interface/editor/project_manager_screen";
  2416. } else {
  2417. // Skip.
  2418. screen_found = true;
  2419. }
  2420. if (!display_driver.is_empty()) {
  2421. // Skip.
  2422. prefer_wayland_found = true;
  2423. }
  2424. while (!screen_found || !prefer_wayland_found) {
  2425. assign = Variant();
  2426. next_tag.fields.clear();
  2427. next_tag.name = String();
  2428. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, &rp_new, true);
  2429. if (err == ERR_FILE_EOF) {
  2430. break;
  2431. }
  2432. if (err == OK && !assign.is_empty()) {
  2433. if (!screen_found && assign == screen_property) {
  2434. init_screen = value;
  2435. screen_found = true;
  2436. if (editor) {
  2437. restore_editor_window_layout = value.operator int() == EditorSettings::InitialScreen::INITIAL_SCREEN_AUTO;
  2438. }
  2439. }
  2440. if (!prefer_wayland_found && assign == "run/platforms/linuxbsd/prefer_wayland") {
  2441. prefer_wayland = value;
  2442. prefer_wayland_found = true;
  2443. }
  2444. }
  2445. }
  2446. if (display_driver.is_empty()) {
  2447. if (prefer_wayland) {
  2448. display_driver = "wayland";
  2449. } else {
  2450. display_driver = "default";
  2451. }
  2452. }
  2453. }
  2454. }
  2455. }
  2456. if (found_project && EditorPaths::get_singleton()->is_self_contained()) {
  2457. if (ProjectSettings::get_singleton()->get_resource_path() == OS::get_singleton()->get_executable_path().get_base_dir()) {
  2458. ERR_PRINT("You are trying to run a self-contained editor at the same location as a project. This is not allowed, since editor files will mix with project files.");
  2459. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  2460. return FAILED;
  2461. }
  2462. }
  2463. bool has_command_line_window_override = init_use_custom_pos || init_use_custom_screen || init_windowed;
  2464. if (editor && !has_command_line_window_override && restore_editor_window_layout) {
  2465. Ref<ConfigFile> config;
  2466. config.instantiate();
  2467. // Load and amend existing config if it exists.
  2468. Error err = config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  2469. if (err == OK) {
  2470. init_screen = config->get_value("EditorWindow", "screen", init_screen);
  2471. String mode = config->get_value("EditorWindow", "mode", "maximized");
  2472. window_size = config->get_value("EditorWindow", "size", window_size);
  2473. if (mode == "windowed") {
  2474. window_mode = DisplayServer::WINDOW_MODE_WINDOWED;
  2475. init_windowed = true;
  2476. } else if (mode == "fullscreen") {
  2477. window_mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  2478. init_fullscreen = true;
  2479. } else {
  2480. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  2481. init_maximized = true;
  2482. }
  2483. if (init_windowed) {
  2484. init_use_custom_pos = true;
  2485. init_custom_pos = config->get_value("EditorWindow", "position", Vector2i(0, 0));
  2486. }
  2487. }
  2488. }
  2489. OS::get_singleton()->benchmark_end_measure("Startup", "Initialize Early Settings");
  2490. }
  2491. #endif
  2492. OS::get_singleton()->benchmark_begin_measure("Startup", "Servers");
  2493. tsman = memnew(TextServerManager);
  2494. if (tsman) {
  2495. Ref<TextServerDummy> ts;
  2496. ts.instantiate();
  2497. tsman->add_interface(ts);
  2498. }
  2499. #ifndef _3D_DISABLED
  2500. physics_server_3d_manager = memnew(PhysicsServer3DManager);
  2501. #endif // _3D_DISABLED
  2502. physics_server_2d_manager = memnew(PhysicsServer2DManager);
  2503. register_server_types();
  2504. {
  2505. OS::get_singleton()->benchmark_begin_measure("Servers", "Modules and Extensions");
  2506. initialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  2507. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  2508. OS::get_singleton()->benchmark_end_measure("Servers", "Modules and Extensions");
  2509. }
  2510. /* Initialize Input */
  2511. {
  2512. OS::get_singleton()->benchmark_begin_measure("Servers", "Input");
  2513. input = memnew(Input);
  2514. OS::get_singleton()->initialize_joypads();
  2515. OS::get_singleton()->benchmark_end_measure("Servers", "Input");
  2516. }
  2517. /* Initialize Display Server */
  2518. {
  2519. OS::get_singleton()->benchmark_begin_measure("Servers", "Display");
  2520. if (display_driver.is_empty()) {
  2521. display_driver = GLOBAL_GET("display/display_server/driver");
  2522. }
  2523. int display_driver_idx = -1;
  2524. if (display_driver.is_empty() || display_driver == "default") {
  2525. display_driver_idx = 0;
  2526. } else {
  2527. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  2528. String name = DisplayServer::get_create_function_name(i);
  2529. if (display_driver == name) {
  2530. display_driver_idx = i;
  2531. break;
  2532. }
  2533. }
  2534. if (display_driver_idx < 0) {
  2535. // If the requested driver wasn't found, pick the first entry.
  2536. // If all else failed it would be the headless server.
  2537. display_driver_idx = 0;
  2538. }
  2539. }
  2540. // Store this in a globally accessible place, so we can retrieve the rendering drivers
  2541. // list from the display driver for the editor UI.
  2542. OS::get_singleton()->set_display_driver_id(display_driver_idx);
  2543. Vector2i *window_position = nullptr;
  2544. Vector2i position = init_custom_pos;
  2545. if (init_use_custom_pos) {
  2546. window_position = &position;
  2547. }
  2548. Color boot_bg_color = GLOBAL_DEF_BASIC("application/boot_splash/bg_color", boot_splash_bg_color);
  2549. DisplayServer::set_early_window_clear_color_override(true, boot_bg_color);
  2550. DisplayServer::Context context;
  2551. if (editor) {
  2552. context = DisplayServer::CONTEXT_EDITOR;
  2553. } else if (project_manager) {
  2554. context = DisplayServer::CONTEXT_PROJECTMAN;
  2555. } else {
  2556. context = DisplayServer::CONTEXT_ENGINE;
  2557. }
  2558. // rendering_driver now held in static global String in main and initialized in setup()
  2559. Error err;
  2560. display_server = DisplayServer::create(display_driver_idx, rendering_driver, window_mode, window_vsync_mode, window_flags, window_position, window_size, init_screen, context, err);
  2561. if (err != OK || display_server == nullptr) {
  2562. String last_name = DisplayServer::get_create_function_name(display_driver_idx);
  2563. // We can't use this display server, try other ones as fallback.
  2564. // Skip headless (always last registered) because that's not what users
  2565. // would expect if they didn't request it explicitly.
  2566. for (int i = 0; i < DisplayServer::get_create_function_count() - 1; i++) {
  2567. if (i == display_driver_idx) {
  2568. continue; // Don't try the same twice.
  2569. }
  2570. String name = DisplayServer::get_create_function_name(i);
  2571. WARN_PRINT(vformat("Display driver %s failed, falling back to %s.", last_name, name));
  2572. display_server = DisplayServer::create(i, rendering_driver, window_mode, window_vsync_mode, window_flags, window_position, window_size, init_screen, context, err);
  2573. if (err == OK && display_server != nullptr) {
  2574. break;
  2575. }
  2576. }
  2577. }
  2578. if (err != OK || display_server == nullptr) {
  2579. ERR_PRINT("Unable to create DisplayServer, all display drivers failed.\nUse \"--headless\" command line argument to run the engine in headless mode if this is desired (e.g. for continuous integration).");
  2580. if (display_server) {
  2581. memdelete(display_server);
  2582. }
  2583. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  2584. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  2585. unregister_server_types();
  2586. if (input) {
  2587. memdelete(input);
  2588. }
  2589. if (tsman) {
  2590. memdelete(tsman);
  2591. }
  2592. #ifndef _3D_DISABLED
  2593. if (physics_server_3d_manager) {
  2594. memdelete(physics_server_3d_manager);
  2595. }
  2596. #endif // _3D_DISABLED
  2597. if (physics_server_2d_manager) {
  2598. memdelete(physics_server_2d_manager);
  2599. }
  2600. return err;
  2601. }
  2602. if (display_server->has_feature(DisplayServer::FEATURE_ORIENTATION)) {
  2603. display_server->screen_set_orientation(window_orientation);
  2604. }
  2605. OS::get_singleton()->benchmark_end_measure("Servers", "Display");
  2606. }
  2607. // Max FPS needs to be set after the DisplayServer is created.
  2608. Engine::get_singleton()->set_max_fps(GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/max_fps", PROPERTY_HINT_RANGE, "0,1000,1"), 0));
  2609. if (max_fps >= 0) {
  2610. Engine::get_singleton()->set_max_fps(max_fps);
  2611. }
  2612. #ifdef TOOLS_ENABLED
  2613. // If the editor is running in windowed mode, ensure the window rect fits
  2614. // the screen in case screen count or position has changed.
  2615. if (editor && init_windowed) {
  2616. // We still need to check we are actually in windowed mode, because
  2617. // certain platform might only support one fullscreen window.
  2618. if (DisplayServer::get_singleton()->window_get_mode() == DisplayServer::WINDOW_MODE_WINDOWED) {
  2619. Vector2i current_size = DisplayServer::get_singleton()->window_get_size();
  2620. Vector2i current_pos = DisplayServer::get_singleton()->window_get_position();
  2621. int screen = DisplayServer::get_singleton()->window_get_current_screen();
  2622. Rect2i screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  2623. Vector2i adjusted_end = screen_rect.get_end().min(current_pos + current_size);
  2624. Vector2i adjusted_pos = screen_rect.get_position().max(adjusted_end - current_size);
  2625. Vector2i adjusted_size = DisplayServer::get_singleton()->window_get_min_size().max(adjusted_end - adjusted_pos);
  2626. if (current_pos != adjusted_end || current_size != adjusted_size) {
  2627. DisplayServer::get_singleton()->window_set_position(adjusted_pos);
  2628. DisplayServer::get_singleton()->window_set_size(adjusted_size);
  2629. }
  2630. }
  2631. }
  2632. #endif
  2633. if (GLOBAL_GET("debug/settings/stdout/print_fps") || print_fps) {
  2634. // Print requested V-Sync mode at startup to diagnose the printed FPS not going above the monitor refresh rate.
  2635. switch (window_vsync_mode) {
  2636. case DisplayServer::VSyncMode::VSYNC_DISABLED:
  2637. print_line("Requested V-Sync mode: Disabled");
  2638. break;
  2639. case DisplayServer::VSyncMode::VSYNC_ENABLED:
  2640. print_line("Requested V-Sync mode: Enabled - FPS will likely be capped to the monitor refresh rate.");
  2641. break;
  2642. case DisplayServer::VSyncMode::VSYNC_ADAPTIVE:
  2643. print_line("Requested V-Sync mode: Adaptive");
  2644. break;
  2645. case DisplayServer::VSyncMode::VSYNC_MAILBOX:
  2646. print_line("Requested V-Sync mode: Mailbox");
  2647. break;
  2648. }
  2649. }
  2650. if (OS::get_singleton()->_render_thread_mode == OS::RENDER_SEPARATE_THREAD) {
  2651. WARN_PRINT("The Multi-Threaded rendering thread model is experimental. Feel free to try it since it will eventually become a stable feature.\n"
  2652. "However, bear in mind that at the moment it can lead to project crashes or instability.\n"
  2653. "So, unless you want to test the engine, use the Single-Safe option in the project settings instead.");
  2654. }
  2655. /* Initialize Pen Tablet Driver */
  2656. {
  2657. OS::get_singleton()->benchmark_begin_measure("Servers", "Tablet Driver");
  2658. GLOBAL_DEF_RST_NOVAL("input_devices/pen_tablet/driver", "");
  2659. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "input_devices/pen_tablet/driver.windows", PROPERTY_HINT_ENUM, "winink,wintab,dummy"), "");
  2660. if (tablet_driver.is_empty()) { // specified in project.godot
  2661. tablet_driver = GLOBAL_GET("input_devices/pen_tablet/driver");
  2662. if (tablet_driver.is_empty()) {
  2663. tablet_driver = DisplayServer::get_singleton()->tablet_get_driver_name(0);
  2664. }
  2665. }
  2666. for (int i = 0; i < DisplayServer::get_singleton()->tablet_get_driver_count(); i++) {
  2667. if (tablet_driver == DisplayServer::get_singleton()->tablet_get_driver_name(i)) {
  2668. DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(i));
  2669. break;
  2670. }
  2671. }
  2672. if (DisplayServer::get_singleton()->tablet_get_current_driver().is_empty()) {
  2673. DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(0));
  2674. }
  2675. print_verbose("Using \"" + tablet_driver + "\" pen tablet driver...");
  2676. OS::get_singleton()->benchmark_end_measure("Servers", "Tablet Driver");
  2677. }
  2678. /* Initialize Rendering Server */
  2679. {
  2680. OS::get_singleton()->benchmark_begin_measure("Servers", "Rendering");
  2681. rendering_server = memnew(RenderingServerDefault(OS::get_singleton()->get_render_thread_mode() == OS::RENDER_SEPARATE_THREAD));
  2682. rendering_server->init();
  2683. //rendering_server->call_set_use_vsync(OS::get_singleton()->_use_vsync);
  2684. rendering_server->set_render_loop_enabled(!disable_render_loop);
  2685. if (profile_gpu || (!editor && bool(GLOBAL_GET("debug/settings/stdout/print_gpu_profile")))) {
  2686. rendering_server->set_print_gpu_profile(true);
  2687. }
  2688. if (Engine::get_singleton()->get_write_movie_path() != String()) {
  2689. movie_writer = MovieWriter::find_writer_for_file(Engine::get_singleton()->get_write_movie_path());
  2690. if (movie_writer == nullptr) {
  2691. ERR_PRINT("Can't find movie writer for file type, aborting: " + Engine::get_singleton()->get_write_movie_path());
  2692. Engine::get_singleton()->set_write_movie_path(String());
  2693. }
  2694. }
  2695. OS::get_singleton()->benchmark_end_measure("Servers", "Rendering");
  2696. }
  2697. #ifdef UNIX_ENABLED
  2698. // Print warning after initializing the renderer but before initializing audio.
  2699. if (OS::get_singleton()->get_environment("USER") == "root" && !OS::get_singleton()->has_environment("GODOT_SILENCE_ROOT_WARNING")) {
  2700. WARN_PRINT("Started the engine as `root`/superuser. This is a security risk, and subsystems like audio may not work correctly.\nSet the environment variable `GODOT_SILENCE_ROOT_WARNING` to 1 to silence this warning.");
  2701. }
  2702. #endif
  2703. /* Initialize Audio Driver */
  2704. {
  2705. OS::get_singleton()->benchmark_begin_measure("Servers", "Audio");
  2706. AudioDriverManager::initialize(audio_driver_idx);
  2707. // Right moment to create and initialize the audio server.
  2708. audio_server = memnew(AudioServer);
  2709. audio_server->init();
  2710. OS::get_singleton()->benchmark_end_measure("Servers", "Audio");
  2711. }
  2712. #ifndef _3D_DISABLED
  2713. /* Initialize XR Server */
  2714. {
  2715. OS::get_singleton()->benchmark_begin_measure("Servers", "XR");
  2716. xr_server = memnew(XRServer);
  2717. OS::get_singleton()->benchmark_end_measure("Servers", "XR");
  2718. }
  2719. #endif // _3D_DISABLED
  2720. OS::get_singleton()->benchmark_end_measure("Startup", "Servers");
  2721. #ifndef WEB_ENABLED
  2722. // Add a blank line for readability.
  2723. Engine::get_singleton()->print_header("");
  2724. #endif // WEB_ENABLED
  2725. register_core_singletons();
  2726. /* Initialize the main window and boot screen */
  2727. {
  2728. OS::get_singleton()->benchmark_begin_measure("Startup", "Setup Window and Boot");
  2729. MAIN_PRINT("Main: Setup Logo");
  2730. if (init_windowed) {
  2731. //do none..
  2732. } else if (init_maximized) {
  2733. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_MAXIMIZED);
  2734. } else if (init_fullscreen) {
  2735. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_FULLSCREEN);
  2736. }
  2737. if (init_always_on_top) {
  2738. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP, true);
  2739. }
  2740. Color clear = GLOBAL_DEF_BASIC("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3));
  2741. RenderingServer::get_singleton()->set_default_clear_color(clear);
  2742. if (p_show_boot_logo) {
  2743. setup_boot_logo();
  2744. }
  2745. MAIN_PRINT("Main: Clear Color");
  2746. DisplayServer::set_early_window_clear_color_override(false);
  2747. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/config/icon", PROPERTY_HINT_FILE, "*.png,*.bmp,*.hdr,*.jpg,*.jpeg,*.svg,*.tga,*.exr,*.webp"), String());
  2748. GLOBAL_DEF(PropertyInfo(Variant::STRING, "application/config/macos_native_icon", PROPERTY_HINT_FILE, "*.icns"), String());
  2749. GLOBAL_DEF(PropertyInfo(Variant::STRING, "application/config/windows_native_icon", PROPERTY_HINT_FILE, "*.ico"), String());
  2750. MAIN_PRINT("Main: Touch Input");
  2751. Input *id = Input::get_singleton();
  2752. if (id) {
  2753. bool agile_input_event_flushing = GLOBAL_DEF("input_devices/buffering/agile_event_flushing", false);
  2754. id->set_agile_input_event_flushing(agile_input_event_flushing);
  2755. if (bool(GLOBAL_DEF_BASIC("input_devices/pointing/emulate_touch_from_mouse", false)) &&
  2756. !(editor || project_manager)) {
  2757. if (!DisplayServer::get_singleton()->is_touchscreen_available()) {
  2758. //only if no touchscreen ui hint, set emulation
  2759. id->set_emulate_touch_from_mouse(true);
  2760. }
  2761. }
  2762. id->set_emulate_mouse_from_touch(bool(GLOBAL_DEF_BASIC("input_devices/pointing/emulate_mouse_from_touch", true)));
  2763. if (editor) {
  2764. id->set_emulate_mouse_from_touch(true);
  2765. }
  2766. }
  2767. OS::get_singleton()->benchmark_end_measure("Startup", "Setup Window and Boot");
  2768. }
  2769. MAIN_PRINT("Main: Load Translations and Remaps");
  2770. /* Setup translations and remaps */
  2771. {
  2772. OS::get_singleton()->benchmark_begin_measure("Startup", "Translations and Remaps");
  2773. translation_server->setup(); //register translations, load them, etc.
  2774. if (!locale.is_empty()) {
  2775. translation_server->set_locale(locale);
  2776. }
  2777. translation_server->load_translations();
  2778. ResourceLoader::load_translation_remaps(); //load remaps for resources
  2779. ResourceLoader::load_path_remaps();
  2780. OS::get_singleton()->benchmark_end_measure("Startup", "Translations and Remaps");
  2781. }
  2782. MAIN_PRINT("Main: Load TextServer");
  2783. /* Setup Text Server */
  2784. {
  2785. OS::get_singleton()->benchmark_begin_measure("Startup", "Text Server");
  2786. /* Enum text drivers */
  2787. GLOBAL_DEF_RST("internationalization/rendering/text_driver", "");
  2788. String text_driver_options;
  2789. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  2790. const String driver_name = TextServerManager::get_singleton()->get_interface(i)->get_name();
  2791. if (driver_name == "Dummy") {
  2792. // Dummy text driver cannot draw any text, making the editor unusable if selected.
  2793. continue;
  2794. }
  2795. if (!text_driver_options.is_empty() && !text_driver_options.contains(",")) {
  2796. // Not the first option; add a comma before it as a separator for the property hint.
  2797. text_driver_options += ",";
  2798. }
  2799. text_driver_options += driver_name;
  2800. }
  2801. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/rendering/text_driver", PROPERTY_HINT_ENUM, text_driver_options));
  2802. /* Determine text driver */
  2803. if (text_driver.is_empty()) {
  2804. text_driver = GLOBAL_GET("internationalization/rendering/text_driver");
  2805. }
  2806. if (!text_driver.is_empty()) {
  2807. /* Load user selected text server. */
  2808. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  2809. if (TextServerManager::get_singleton()->get_interface(i)->get_name() == text_driver) {
  2810. text_driver_idx = i;
  2811. break;
  2812. }
  2813. }
  2814. }
  2815. if (text_driver_idx < 0) {
  2816. /* If not selected, use one with the most features available. */
  2817. int max_features = 0;
  2818. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  2819. uint32_t features = TextServerManager::get_singleton()->get_interface(i)->get_features();
  2820. int feature_number = 0;
  2821. while (features) {
  2822. feature_number += features & 1;
  2823. features >>= 1;
  2824. }
  2825. if (feature_number >= max_features) {
  2826. max_features = feature_number;
  2827. text_driver_idx = i;
  2828. }
  2829. }
  2830. }
  2831. if (text_driver_idx >= 0) {
  2832. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(text_driver_idx);
  2833. TextServerManager::get_singleton()->set_primary_interface(ts);
  2834. if (ts->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  2835. ts->load_support_data("res://" + ts->get_support_data_filename());
  2836. }
  2837. } else {
  2838. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "TextServer: Unable to create TextServer interface.");
  2839. }
  2840. OS::get_singleton()->benchmark_end_measure("Startup", "Text Server");
  2841. }
  2842. MAIN_PRINT("Main: Load Scene Types");
  2843. OS::get_singleton()->benchmark_begin_measure("Startup", "Scene");
  2844. // Initialize ThemeDB early so that scene types can register their theme items.
  2845. // Default theme will be initialized later, after modules and ScriptServer are ready.
  2846. initialize_theme_db();
  2847. register_scene_types();
  2848. register_driver_types();
  2849. register_scene_singletons();
  2850. {
  2851. OS::get_singleton()->benchmark_begin_measure("Scene", "Modules and Extensions");
  2852. initialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  2853. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  2854. OS::get_singleton()->benchmark_end_measure("Scene", "Modules and Extensions");
  2855. }
  2856. PackedStringArray extensions;
  2857. extensions.push_back("gd");
  2858. if (ClassDB::class_exists("CSharpScript")) {
  2859. extensions.push_back("cs");
  2860. }
  2861. extensions.push_back("gdshader");
  2862. GLOBAL_DEF_NOVAL(PropertyInfo(Variant::PACKED_STRING_ARRAY, "editor/script/search_in_file_extensions"), extensions); // Note: should be defined after Scene level modules init to see .NET.
  2863. OS::get_singleton()->benchmark_end_measure("Startup", "Scene");
  2864. #ifdef TOOLS_ENABLED
  2865. ClassDB::set_current_api(ClassDB::API_EDITOR);
  2866. register_editor_types();
  2867. {
  2868. OS::get_singleton()->benchmark_begin_measure("Editor", "Modules and Extensions");
  2869. initialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  2870. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  2871. OS::get_singleton()->benchmark_end_measure("Editor", "Modules and Extensions");
  2872. }
  2873. ClassDB::set_current_api(ClassDB::API_CORE);
  2874. #endif
  2875. MAIN_PRINT("Main: Load Platforms");
  2876. OS::get_singleton()->benchmark_begin_measure("Startup", "Platforms");
  2877. register_platform_apis();
  2878. OS::get_singleton()->benchmark_end_measure("Startup", "Platforms");
  2879. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/mouse_cursor/custom_image", PROPERTY_HINT_FILE, "*.png,*.bmp,*.hdr,*.jpg,*.jpeg,*.svg,*.tga,*.exr,*.webp"), String());
  2880. GLOBAL_DEF_BASIC("display/mouse_cursor/custom_image_hotspot", Vector2());
  2881. GLOBAL_DEF_BASIC("display/mouse_cursor/tooltip_position_offset", Point2(10, 10));
  2882. if (String(GLOBAL_GET("display/mouse_cursor/custom_image")) != String()) {
  2883. Ref<Texture2D> cursor = ResourceLoader::load(
  2884. GLOBAL_GET("display/mouse_cursor/custom_image"));
  2885. if (cursor.is_valid()) {
  2886. Vector2 hotspot = GLOBAL_GET("display/mouse_cursor/custom_image_hotspot");
  2887. Input::get_singleton()->set_custom_mouse_cursor(cursor, Input::CURSOR_ARROW, hotspot);
  2888. }
  2889. }
  2890. OS::get_singleton()->benchmark_begin_measure("Startup", "Finalize Setup");
  2891. camera_server = CameraServer::create();
  2892. MAIN_PRINT("Main: Load Physics");
  2893. initialize_physics();
  2894. MAIN_PRINT("Main: Load Navigation");
  2895. initialize_navigation_server();
  2896. register_server_singletons();
  2897. // This loads global classes, so it must happen before custom loaders and savers are registered
  2898. ScriptServer::init_languages();
  2899. theme_db->initialize_theme();
  2900. audio_server->load_default_bus_layout();
  2901. #if defined(MODULE_MONO_ENABLED) && defined(TOOLS_ENABLED)
  2902. // Hacky to have it here, but we don't have good facility yet to let modules
  2903. // register command line options to call at the right time. This needs to happen
  2904. // after init'ing the ScriptServer, but also after init'ing the ThemeDB,
  2905. // for the C# docs generation in the bindings.
  2906. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  2907. BindingsGenerator::handle_cmdline_args(cmdline_args);
  2908. #endif
  2909. if (use_debug_profiler && EngineDebugger::is_active()) {
  2910. // Start the "scripts" profiler, used in local debugging.
  2911. // We could add more, and make the CLI arg require a comma-separated list of profilers.
  2912. EngineDebugger::get_singleton()->profiler_enable("scripts", true);
  2913. }
  2914. if (!project_manager) {
  2915. // If not running the project manager, and now that the engine is
  2916. // able to load resources, load the global shader variables.
  2917. // If running on editor, don't load the textures because the editor
  2918. // may want to import them first. Editor will reload those later.
  2919. rendering_server->global_shader_parameters_load_settings(!editor);
  2920. }
  2921. OS::get_singleton()->benchmark_end_measure("Startup", "Finalize Setup");
  2922. _start_success = true;
  2923. ClassDB::set_current_api(ClassDB::API_NONE); //no more APIs are registered at this point
  2924. print_verbose("CORE API HASH: " + uitos(ClassDB::get_api_hash(ClassDB::API_CORE)));
  2925. print_verbose("EDITOR API HASH: " + uitos(ClassDB::get_api_hash(ClassDB::API_EDITOR)));
  2926. MAIN_PRINT("Main: Done");
  2927. OS::get_singleton()->benchmark_end_measure("Startup", "Main::Setup2");
  2928. return OK;
  2929. }
  2930. void Main::setup_boot_logo() {
  2931. MAIN_PRINT("Main: Load Boot Image");
  2932. #if !defined(TOOLS_ENABLED) && defined(WEB_ENABLED)
  2933. bool show_logo = false;
  2934. #else
  2935. bool show_logo = true;
  2936. #endif
  2937. if (show_logo) { //boot logo!
  2938. const bool boot_logo_image = GLOBAL_DEF_BASIC("application/boot_splash/show_image", true);
  2939. const String boot_logo_path = ResourceUID::ensure_path(GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/boot_splash/image", PROPERTY_HINT_FILE, "*.png"), String())).strip_edges();
  2940. const bool boot_logo_scale = GLOBAL_DEF_BASIC("application/boot_splash/fullsize", true);
  2941. const bool boot_logo_filter = GLOBAL_DEF_BASIC("application/boot_splash/use_filter", true);
  2942. Ref<Image> boot_logo;
  2943. if (boot_logo_image) {
  2944. if (!boot_logo_path.is_empty()) {
  2945. boot_logo.instantiate();
  2946. Error load_err = ImageLoader::load_image(boot_logo_path, boot_logo);
  2947. if (load_err) {
  2948. String msg = (boot_logo_path.ends_with(".png") ? "" : "The only supported format is PNG.");
  2949. ERR_PRINT("Non-existing or invalid boot splash at '" + boot_logo_path + +"'. " + msg + " Loading default splash.");
  2950. }
  2951. }
  2952. } else {
  2953. // Create a 1×1 transparent image. This will effectively hide the splash image.
  2954. boot_logo.instantiate();
  2955. boot_logo->initialize_data(1, 1, false, Image::FORMAT_RGBA8);
  2956. boot_logo->set_pixel(0, 0, Color(0, 0, 0, 0));
  2957. }
  2958. Color boot_bg_color = GLOBAL_GET("application/boot_splash/bg_color");
  2959. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  2960. boot_bg_color = GLOBAL_DEF_BASIC("application/boot_splash/bg_color", (editor || project_manager) ? boot_splash_editor_bg_color : boot_splash_bg_color);
  2961. #endif
  2962. if (boot_logo.is_valid()) {
  2963. RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, boot_logo_scale, boot_logo_filter);
  2964. } else {
  2965. #ifndef NO_DEFAULT_BOOT_LOGO
  2966. MAIN_PRINT("Main: Create bootsplash");
  2967. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  2968. Ref<Image> splash = (editor || project_manager) ? memnew(Image(boot_splash_editor_png)) : memnew(Image(boot_splash_png));
  2969. #else
  2970. Ref<Image> splash = memnew(Image(boot_splash_png));
  2971. #endif
  2972. MAIN_PRINT("Main: ClearColor");
  2973. RenderingServer::get_singleton()->set_default_clear_color(boot_bg_color);
  2974. MAIN_PRINT("Main: Image");
  2975. RenderingServer::get_singleton()->set_boot_image(splash, boot_bg_color, false);
  2976. #endif
  2977. }
  2978. #if defined(TOOLS_ENABLED) && defined(MACOS_ENABLED)
  2979. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_ICON) && OS::get_singleton()->get_bundle_icon_path().is_empty()) {
  2980. Ref<Image> icon = memnew(Image(app_icon_png));
  2981. DisplayServer::get_singleton()->set_icon(icon);
  2982. }
  2983. #endif
  2984. }
  2985. RenderingServer::get_singleton()->set_default_clear_color(
  2986. GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
  2987. }
  2988. String Main::get_rendering_driver_name() {
  2989. return rendering_driver;
  2990. }
  2991. // everything the main loop needs to know about frame timings
  2992. static MainTimerSync main_timer_sync;
  2993. // Return value should be EXIT_SUCCESS if we start successfully
  2994. // and should move on to `OS::run`, and EXIT_FAILURE otherwise for
  2995. // an early exit with that error code.
  2996. int Main::start() {
  2997. OS::get_singleton()->benchmark_begin_measure("Startup", "Main::Start");
  2998. ERR_FAIL_COND_V(!_start_success, false);
  2999. bool has_icon = false;
  3000. String positional_arg;
  3001. String game_path;
  3002. String script;
  3003. String main_loop_type;
  3004. bool check_only = false;
  3005. #ifdef TOOLS_ENABLED
  3006. String doc_tool_path;
  3007. bool doc_tool_implicit_cwd = false;
  3008. BitField<DocTools::GenerateFlags> gen_flags;
  3009. String _export_preset;
  3010. Vector<String> patches;
  3011. bool export_debug = false;
  3012. bool export_pack_only = false;
  3013. bool install_android_build_template = false;
  3014. bool export_patch = false;
  3015. #ifdef MODULE_GDSCRIPT_ENABLED
  3016. String gdscript_docs_path;
  3017. #endif
  3018. #ifndef DISABLE_DEPRECATED
  3019. bool converting_project = false;
  3020. bool validating_converting_project = false;
  3021. #endif // DISABLE_DEPRECATED
  3022. #endif // TOOLS_ENABLED
  3023. main_timer_sync.init(OS::get_singleton()->get_ticks_usec());
  3024. List<String> args = OS::get_singleton()->get_cmdline_args();
  3025. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  3026. // First check parameters that do not have an argument to the right.
  3027. // Doctest Unit Testing Handler
  3028. // Designed to override and pass arguments to the unit test handler.
  3029. if (E->get() == "--check-only") {
  3030. check_only = true;
  3031. #ifdef TOOLS_ENABLED
  3032. } else if (E->get() == "--no-docbase") {
  3033. gen_flags.set_flag(DocTools::GENERATE_FLAG_SKIP_BASIC_TYPES);
  3034. } else if (E->get() == "--gdextension-docs") {
  3035. gen_flags.set_flag(DocTools::GENERATE_FLAG_SKIP_BASIC_TYPES);
  3036. gen_flags.set_flag(DocTools::GENERATE_FLAG_EXTENSION_CLASSES_ONLY);
  3037. #ifndef DISABLE_DEPRECATED
  3038. } else if (E->get() == "--convert-3to4") {
  3039. converting_project = true;
  3040. } else if (E->get() == "--validate-conversion-3to4") {
  3041. validating_converting_project = true;
  3042. #endif // DISABLE_DEPRECATED
  3043. } else if (E->get() == "-e" || E->get() == "--editor") {
  3044. editor = true;
  3045. } else if (E->get() == "-p" || E->get() == "--project-manager") {
  3046. project_manager = true;
  3047. } else if (E->get() == "--install-android-build-template") {
  3048. install_android_build_template = true;
  3049. #endif // TOOLS_ENABLED
  3050. } else if (E->get().length() && E->get()[0] != '-' && positional_arg.is_empty()) {
  3051. positional_arg = E->get();
  3052. if (E->get().ends_with(".scn") ||
  3053. E->get().ends_with(".tscn") ||
  3054. E->get().ends_with(".escn") ||
  3055. E->get().ends_with(".res") ||
  3056. E->get().ends_with(".tres")) {
  3057. // Only consider the positional argument to be a scene path if it ends with
  3058. // a file extension associated with Godot scenes. This makes it possible
  3059. // for projects to parse command-line arguments for custom CLI arguments
  3060. // or other file extensions without trouble. This can be used to implement
  3061. // "drag-and-drop onto executable" logic, which can prove helpful
  3062. // for non-game applications.
  3063. game_path = E->get();
  3064. }
  3065. }
  3066. // Then parameters that have an argument to the right.
  3067. else if (E->next()) {
  3068. bool parsed_pair = true;
  3069. if (E->get() == "-s" || E->get() == "--script") {
  3070. script = E->next()->get();
  3071. } else if (E->get() == "--main-loop") {
  3072. main_loop_type = E->next()->get();
  3073. #ifdef TOOLS_ENABLED
  3074. } else if (E->get() == "--doctool") {
  3075. doc_tool_path = E->next()->get();
  3076. if (doc_tool_path.begins_with("-")) {
  3077. // Assuming other command line arg, so default to cwd.
  3078. doc_tool_path = ".";
  3079. doc_tool_implicit_cwd = true;
  3080. parsed_pair = false;
  3081. }
  3082. #ifdef MODULE_GDSCRIPT_ENABLED
  3083. } else if (E->get() == "--gdscript-docs") {
  3084. gdscript_docs_path = E->next()->get();
  3085. #endif
  3086. } else if (E->get() == "--export-release") {
  3087. ERR_FAIL_COND_V_MSG(!editor && !found_project, EXIT_FAILURE, "Please provide a valid project path when exporting, aborting.");
  3088. editor = true; //needs editor
  3089. _export_preset = E->next()->get();
  3090. } else if (E->get() == "--export-debug") {
  3091. ERR_FAIL_COND_V_MSG(!editor && !found_project, EXIT_FAILURE, "Please provide a valid project path when exporting, aborting.");
  3092. editor = true; //needs editor
  3093. _export_preset = E->next()->get();
  3094. export_debug = true;
  3095. } else if (E->get() == "--export-pack") {
  3096. ERR_FAIL_COND_V_MSG(!editor && !found_project, EXIT_FAILURE, "Please provide a valid project path when exporting, aborting.");
  3097. editor = true;
  3098. _export_preset = E->next()->get();
  3099. export_pack_only = true;
  3100. } else if (E->get() == "--export-patch") {
  3101. ERR_FAIL_COND_V_MSG(!editor && !found_project, EXIT_FAILURE, "Please provide a valid project path when exporting, aborting.");
  3102. editor = true;
  3103. _export_preset = E->next()->get();
  3104. export_pack_only = true;
  3105. export_patch = true;
  3106. } else if (E->get() == "--patches") {
  3107. patches = E->next()->get().split(",", false);
  3108. #endif
  3109. } else {
  3110. // The parameter does not match anything known, don't skip the next argument
  3111. parsed_pair = false;
  3112. }
  3113. if (parsed_pair) {
  3114. E = E->next();
  3115. }
  3116. } else if (E->get().begins_with("--export-")) {
  3117. ERR_FAIL_V_MSG(EXIT_FAILURE, "Missing export preset name, aborting.");
  3118. }
  3119. #ifdef TOOLS_ENABLED
  3120. // Handle case where no path is given to --doctool.
  3121. else if (E->get() == "--doctool") {
  3122. doc_tool_path = ".";
  3123. doc_tool_implicit_cwd = true;
  3124. }
  3125. #endif
  3126. }
  3127. uint64_t minimum_time_msec = GLOBAL_DEF(PropertyInfo(Variant::INT, "application/boot_splash/minimum_display_time", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:ms"), 0);
  3128. if (Engine::get_singleton()->is_editor_hint()) {
  3129. minimum_time_msec = 0;
  3130. }
  3131. #ifdef TOOLS_ENABLED
  3132. #ifdef MODULE_GDSCRIPT_ENABLED
  3133. if (!doc_tool_path.is_empty() && gdscript_docs_path.is_empty()) {
  3134. #else
  3135. if (!doc_tool_path.is_empty()) {
  3136. #endif
  3137. // Needed to instance editor-only classes for their default values
  3138. Engine::get_singleton()->set_editor_hint(true);
  3139. // Translate the class reference only when `-l LOCALE` parameter is given.
  3140. if (!locale.is_empty() && locale != "en") {
  3141. load_doc_translations(locale);
  3142. }
  3143. {
  3144. Ref<DirAccess> da = DirAccess::open(doc_tool_path);
  3145. ERR_FAIL_COND_V_MSG(da.is_null(), EXIT_FAILURE, "Argument supplied to --doctool must be a valid directory path.");
  3146. // Ensure that doctool is running in the root dir, but only if
  3147. // user did not manually specify a path as argument.
  3148. if (doc_tool_implicit_cwd) {
  3149. ERR_FAIL_COND_V_MSG(!da->dir_exists("doc"), EXIT_FAILURE, "--doctool must be run from the Godot repository's root folder, or specify a path that points there.");
  3150. }
  3151. }
  3152. #ifndef MODULE_MONO_ENABLED
  3153. // Hack to define .NET-specific project settings even on non-.NET builds,
  3154. // so that we don't lose their descriptions and default values in DocTools.
  3155. // Default values should be synced with mono_gd/gd_mono.cpp.
  3156. GLOBAL_DEF("dotnet/project/assembly_name", "");
  3157. GLOBAL_DEF("dotnet/project/solution_directory", "");
  3158. GLOBAL_DEF(PropertyInfo(Variant::INT, "dotnet/project/assembly_reload_attempts", PROPERTY_HINT_RANGE, "1,16,1,or_greater"), 3);
  3159. #endif
  3160. Error err;
  3161. DocTools doc;
  3162. doc.generate(gen_flags);
  3163. DocTools docsrc;
  3164. HashMap<String, String> doc_data_classes;
  3165. HashSet<String> checked_paths;
  3166. print_line("Loading docs...");
  3167. const bool gdextension_docs = gen_flags.has_flag(DocTools::GENERATE_FLAG_EXTENSION_CLASSES_ONLY);
  3168. if (!gdextension_docs) {
  3169. for (int i = 0; i < _doc_data_class_path_count; i++) {
  3170. // Custom modules are always located by absolute path.
  3171. String path = _doc_data_class_paths[i].path;
  3172. if (path.is_relative_path()) {
  3173. path = doc_tool_path.path_join(path);
  3174. }
  3175. String name = _doc_data_class_paths[i].name;
  3176. doc_data_classes[name] = path;
  3177. if (!checked_paths.has(path)) {
  3178. checked_paths.insert(path);
  3179. // Create the module documentation directory if it doesn't exist
  3180. Ref<DirAccess> da = DirAccess::create_for_path(path);
  3181. err = da->make_dir_recursive(path);
  3182. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error: Can't create directory: " + path + ": " + itos(err));
  3183. print_line("Loading docs from: " + path);
  3184. err = docsrc.load_classes(path);
  3185. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error loading docs from: " + path + ": " + itos(err));
  3186. }
  3187. }
  3188. }
  3189. // For GDExtension docs, use a path that is compatible with Godot modules.
  3190. String index_path = gdextension_docs ? doc_tool_path.path_join("doc_classes") : doc_tool_path.path_join("doc/classes");
  3191. // Create the main documentation directory if it doesn't exist
  3192. Ref<DirAccess> da = DirAccess::create_for_path(index_path);
  3193. err = da->make_dir_recursive(index_path);
  3194. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error: Can't create index directory: " + index_path + ": " + itos(err));
  3195. print_line("Loading classes from: " + index_path);
  3196. err = docsrc.load_classes(index_path);
  3197. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error loading classes from: " + index_path + ": " + itos(err));
  3198. checked_paths.insert(index_path);
  3199. print_line("Merging docs...");
  3200. doc.merge_from(docsrc);
  3201. for (const String &E : checked_paths) {
  3202. print_line("Erasing old docs at: " + E);
  3203. err = DocTools::erase_classes(E);
  3204. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error erasing old docs at: " + E + ": " + itos(err));
  3205. }
  3206. print_line("Generating new docs...");
  3207. err = doc.save_classes(index_path, doc_data_classes, !gdextension_docs);
  3208. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error saving new docs:" + itos(err));
  3209. print_line("Deleting docs cache...");
  3210. if (FileAccess::exists(EditorHelp::get_cache_full_path())) {
  3211. DirAccess::remove_file_or_error(EditorHelp::get_cache_full_path());
  3212. }
  3213. return EXIT_SUCCESS;
  3214. }
  3215. // GDExtension API and interface.
  3216. {
  3217. if (dump_gdextension_interface) {
  3218. GDExtensionInterfaceDump::generate_gdextension_interface_file("gdextension_interface.h");
  3219. }
  3220. if (dump_extension_api) {
  3221. Engine::get_singleton()->set_editor_hint(true); // "extension_api.json" should always contains editor singletons.
  3222. GDExtensionAPIDump::generate_extension_json_file("extension_api.json", include_docs_in_extension_api_dump);
  3223. }
  3224. if (dump_gdextension_interface || dump_extension_api) {
  3225. return EXIT_SUCCESS;
  3226. }
  3227. if (validate_extension_api) {
  3228. Engine::get_singleton()->set_editor_hint(true); // "extension_api.json" should always contains editor singletons.
  3229. bool valid = GDExtensionAPIDump::validate_extension_json_file(validate_extension_api_file) == OK;
  3230. return valid ? EXIT_SUCCESS : EXIT_FAILURE;
  3231. }
  3232. }
  3233. #ifndef DISABLE_DEPRECATED
  3234. if (converting_project) {
  3235. int ret = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).convert();
  3236. return ret ? EXIT_SUCCESS : EXIT_FAILURE;
  3237. }
  3238. if (validating_converting_project) {
  3239. bool ret = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).validate_conversion();
  3240. return ret ? EXIT_SUCCESS : EXIT_FAILURE;
  3241. }
  3242. #endif // DISABLE_DEPRECATED
  3243. #endif // TOOLS_ENABLED
  3244. if (script.is_empty() && game_path.is_empty() && String(GLOBAL_GET("application/run/main_scene")) != "") {
  3245. game_path = GLOBAL_GET("application/run/main_scene");
  3246. }
  3247. #ifdef TOOLS_ENABLED
  3248. if (!editor && !project_manager && !cmdline_tool && script.is_empty() && game_path.is_empty()) {
  3249. // If we end up here, it means we didn't manage to detect what we want to run.
  3250. // Let's throw an error gently. The code leading to this is pretty brittle so
  3251. // this might end up triggered by valid usage, in which case we'll have to
  3252. // fine-tune further.
  3253. OS::get_singleton()->alert("Couldn't detect whether to run the editor, the project manager or a specific project. Aborting.");
  3254. ERR_FAIL_V_MSG(EXIT_FAILURE, "Couldn't detect whether to run the editor, the project manager or a specific project. Aborting.");
  3255. }
  3256. #endif
  3257. MainLoop *main_loop = nullptr;
  3258. if (editor) {
  3259. main_loop = memnew(SceneTree);
  3260. }
  3261. if (main_loop_type.is_empty()) {
  3262. main_loop_type = GLOBAL_GET("application/run/main_loop_type");
  3263. }
  3264. if (!script.is_empty()) {
  3265. Ref<Script> script_res = ResourceLoader::load(script);
  3266. ERR_FAIL_COND_V_MSG(script_res.is_null(), EXIT_FAILURE, "Can't load script: " + script);
  3267. if (check_only) {
  3268. return script_res->is_valid() ? EXIT_SUCCESS : EXIT_FAILURE;
  3269. }
  3270. if (script_res->can_instantiate()) {
  3271. StringName instance_type = script_res->get_instance_base_type();
  3272. Object *obj = ClassDB::instantiate(instance_type);
  3273. MainLoop *script_loop = Object::cast_to<MainLoop>(obj);
  3274. if (!script_loop) {
  3275. if (obj) {
  3276. memdelete(obj);
  3277. }
  3278. OS::get_singleton()->alert(vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.", script));
  3279. ERR_FAIL_V_MSG(EXIT_FAILURE, vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.", script));
  3280. }
  3281. script_loop->set_script(script_res);
  3282. main_loop = script_loop;
  3283. } else {
  3284. return EXIT_FAILURE;
  3285. }
  3286. } else { // Not based on script path.
  3287. if (!editor && !ClassDB::class_exists(main_loop_type) && ScriptServer::is_global_class(main_loop_type)) {
  3288. String script_path = ScriptServer::get_global_class_path(main_loop_type);
  3289. Ref<Script> script_res = ResourceLoader::load(script_path);
  3290. if (script_res.is_null()) {
  3291. OS::get_singleton()->alert("Error: Could not load MainLoop script type: " + main_loop_type);
  3292. ERR_FAIL_V_MSG(EXIT_FAILURE, vformat("Could not load global class %s.", main_loop_type));
  3293. }
  3294. StringName script_base = script_res->get_instance_base_type();
  3295. Object *obj = ClassDB::instantiate(script_base);
  3296. MainLoop *script_loop = Object::cast_to<MainLoop>(obj);
  3297. if (!script_loop) {
  3298. if (obj) {
  3299. memdelete(obj);
  3300. }
  3301. OS::get_singleton()->alert("Error: Invalid MainLoop script base type: " + script_base);
  3302. ERR_FAIL_V_MSG(EXIT_FAILURE, vformat("The global class %s does not inherit from SceneTree or MainLoop.", main_loop_type));
  3303. }
  3304. script_loop->set_script(script_res);
  3305. main_loop = script_loop;
  3306. }
  3307. }
  3308. if (!main_loop && main_loop_type.is_empty()) {
  3309. main_loop_type = "SceneTree";
  3310. }
  3311. if (!main_loop) {
  3312. if (!ClassDB::class_exists(main_loop_type)) {
  3313. OS::get_singleton()->alert("Error: MainLoop type doesn't exist: " + main_loop_type);
  3314. return EXIT_FAILURE;
  3315. } else {
  3316. Object *ml = ClassDB::instantiate(main_loop_type);
  3317. ERR_FAIL_NULL_V_MSG(ml, EXIT_FAILURE, "Can't instance MainLoop type.");
  3318. main_loop = Object::cast_to<MainLoop>(ml);
  3319. if (!main_loop) {
  3320. memdelete(ml);
  3321. ERR_FAIL_V_MSG(EXIT_FAILURE, "Invalid MainLoop type.");
  3322. }
  3323. }
  3324. }
  3325. OS::get_singleton()->set_main_loop(main_loop);
  3326. SceneTree *sml = Object::cast_to<SceneTree>(main_loop);
  3327. if (sml) {
  3328. #ifdef DEBUG_ENABLED
  3329. if (debug_collisions) {
  3330. sml->set_debug_collisions_hint(true);
  3331. }
  3332. if (debug_paths) {
  3333. sml->set_debug_paths_hint(true);
  3334. }
  3335. if (debug_navigation) {
  3336. sml->set_debug_navigation_hint(true);
  3337. NavigationServer3D::get_singleton()->set_debug_navigation_enabled(true);
  3338. }
  3339. if (debug_avoidance) {
  3340. NavigationServer3D::get_singleton()->set_debug_avoidance_enabled(true);
  3341. }
  3342. if (debug_navigation || debug_avoidance) {
  3343. NavigationServer3D::get_singleton()->set_active(true);
  3344. NavigationServer3D::get_singleton()->set_debug_enabled(true);
  3345. }
  3346. if (debug_canvas_item_redraw) {
  3347. RenderingServer::get_singleton()->canvas_item_set_debug_redraw(true);
  3348. }
  3349. #endif
  3350. if (single_threaded_scene) {
  3351. sml->set_disable_node_threading(true);
  3352. }
  3353. bool embed_subwindows = GLOBAL_GET("display/window/subwindows/embed_subwindows");
  3354. if (single_window || (!project_manager && !editor && embed_subwindows) || !DisplayServer::get_singleton()->has_feature(DisplayServer::Feature::FEATURE_SUBWINDOWS)) {
  3355. sml->get_root()->set_embedding_subwindows(true);
  3356. }
  3357. ResourceLoader::add_custom_loaders();
  3358. ResourceSaver::add_custom_savers();
  3359. if (!project_manager && !editor) { // game
  3360. if (!game_path.is_empty() || !script.is_empty()) {
  3361. //autoload
  3362. OS::get_singleton()->benchmark_begin_measure("Startup", "Load Autoloads");
  3363. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  3364. //first pass, add the constants so they exist before any script is loaded
  3365. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  3366. const ProjectSettings::AutoloadInfo &info = E.value;
  3367. if (info.is_singleton) {
  3368. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  3369. ScriptServer::get_language(i)->add_global_constant(info.name, Variant());
  3370. }
  3371. }
  3372. }
  3373. //second pass, load into global constants
  3374. List<Node *> to_add;
  3375. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  3376. const ProjectSettings::AutoloadInfo &info = E.value;
  3377. Node *n = nullptr;
  3378. if (ResourceLoader::get_resource_type(info.path) == "PackedScene") {
  3379. // Cache the scene reference before loading it (for cyclic references)
  3380. Ref<PackedScene> scn;
  3381. scn.instantiate();
  3382. scn->set_path(info.path);
  3383. scn->reload_from_file();
  3384. ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
  3385. if (scn.is_valid()) {
  3386. n = scn->instantiate();
  3387. }
  3388. } else {
  3389. Ref<Resource> res = ResourceLoader::load(info.path);
  3390. ERR_CONTINUE_MSG(res.is_null(), vformat("Failed to instantiate an autoload, can't load from path: %s.", info.path));
  3391. Ref<Script> script_res = res;
  3392. if (script_res.is_valid()) {
  3393. StringName ibt = script_res->get_instance_base_type();
  3394. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  3395. ERR_CONTINUE_MSG(!valid_type, vformat("Failed to instantiate an autoload, script '%s' does not inherit from 'Node'.", info.path));
  3396. Object *obj = ClassDB::instantiate(ibt);
  3397. ERR_CONTINUE_MSG(!obj, vformat("Failed to instantiate an autoload, cannot instantiate '%s'.", ibt));
  3398. n = Object::cast_to<Node>(obj);
  3399. n->set_script(script_res);
  3400. }
  3401. }
  3402. ERR_CONTINUE_MSG(!n, vformat("Failed to instantiate an autoload, path is not pointing to a scene or a script: %s.", info.path));
  3403. n->set_name(info.name);
  3404. //defer so references are all valid on _ready()
  3405. to_add.push_back(n);
  3406. if (info.is_singleton) {
  3407. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  3408. ScriptServer::get_language(i)->add_global_constant(info.name, n);
  3409. }
  3410. }
  3411. }
  3412. for (Node *E : to_add) {
  3413. sml->get_root()->add_child(E);
  3414. }
  3415. OS::get_singleton()->benchmark_end_measure("Startup", "Load Autoloads");
  3416. }
  3417. }
  3418. #ifdef TOOLS_ENABLED
  3419. #ifdef MODULE_GDSCRIPT_ENABLED
  3420. if (!doc_tool_path.is_empty() && !gdscript_docs_path.is_empty()) {
  3421. DocTools docs;
  3422. Error err;
  3423. Vector<String> paths = get_files_with_extension(gdscript_docs_path, "gd");
  3424. ERR_FAIL_COND_V_MSG(paths.is_empty(), EXIT_FAILURE, "Couldn't find any GDScript files under the given directory: " + gdscript_docs_path);
  3425. for (const String &path : paths) {
  3426. Ref<GDScript> gdscript = ResourceLoader::load(path);
  3427. for (const DocData::ClassDoc &class_doc : gdscript->get_documentation()) {
  3428. docs.add_doc(class_doc);
  3429. }
  3430. }
  3431. if (doc_tool_implicit_cwd) {
  3432. doc_tool_path = "./docs";
  3433. }
  3434. Ref<DirAccess> da = DirAccess::create_for_path(doc_tool_path);
  3435. err = da->make_dir_recursive(doc_tool_path);
  3436. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error: Can't create GDScript docs directory: " + doc_tool_path + ": " + itos(err));
  3437. HashMap<String, String> doc_data_classes;
  3438. err = docs.save_classes(doc_tool_path, doc_data_classes, false);
  3439. ERR_FAIL_COND_V_MSG(err != OK, EXIT_FAILURE, "Error saving GDScript docs:" + itos(err));
  3440. return EXIT_SUCCESS;
  3441. }
  3442. #endif // MODULE_GDSCRIPT_ENABLED
  3443. EditorNode *editor_node = nullptr;
  3444. if (editor) {
  3445. OS::get_singleton()->benchmark_begin_measure("Startup", "Editor");
  3446. if (editor_pseudolocalization) {
  3447. translation_server->get_editor_domain()->set_pseudolocalization_enabled(true);
  3448. }
  3449. editor_node = memnew(EditorNode);
  3450. sml->get_root()->add_child(editor_node);
  3451. if (!_export_preset.is_empty()) {
  3452. editor_node->export_preset(_export_preset, positional_arg, export_debug, export_pack_only, install_android_build_template, export_patch, patches);
  3453. game_path = ""; // Do not load anything.
  3454. }
  3455. OS::get_singleton()->benchmark_end_measure("Startup", "Editor");
  3456. }
  3457. #endif
  3458. sml->set_auto_accept_quit(GLOBAL_GET("application/config/auto_accept_quit"));
  3459. sml->set_quit_on_go_back(GLOBAL_GET("application/config/quit_on_go_back"));
  3460. if (!editor && !project_manager) {
  3461. //standard helpers that can be changed from main config
  3462. String stretch_mode = GLOBAL_GET("display/window/stretch/mode");
  3463. String stretch_aspect = GLOBAL_GET("display/window/stretch/aspect");
  3464. Size2i stretch_size = Size2i(GLOBAL_GET("display/window/size/viewport_width"),
  3465. GLOBAL_GET("display/window/size/viewport_height"));
  3466. real_t stretch_scale = GLOBAL_GET("display/window/stretch/scale");
  3467. String stretch_scale_mode = GLOBAL_GET("display/window/stretch/scale_mode");
  3468. Window::ContentScaleMode cs_sm = Window::CONTENT_SCALE_MODE_DISABLED;
  3469. if (stretch_mode == "canvas_items") {
  3470. cs_sm = Window::CONTENT_SCALE_MODE_CANVAS_ITEMS;
  3471. } else if (stretch_mode == "viewport") {
  3472. cs_sm = Window::CONTENT_SCALE_MODE_VIEWPORT;
  3473. }
  3474. Window::ContentScaleAspect cs_aspect = Window::CONTENT_SCALE_ASPECT_IGNORE;
  3475. if (stretch_aspect == "keep") {
  3476. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP;
  3477. } else if (stretch_aspect == "keep_width") {
  3478. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_WIDTH;
  3479. } else if (stretch_aspect == "keep_height") {
  3480. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_HEIGHT;
  3481. } else if (stretch_aspect == "expand") {
  3482. cs_aspect = Window::CONTENT_SCALE_ASPECT_EXPAND;
  3483. }
  3484. Window::ContentScaleStretch cs_stretch = Window::CONTENT_SCALE_STRETCH_FRACTIONAL;
  3485. if (stretch_scale_mode == "integer") {
  3486. cs_stretch = Window::CONTENT_SCALE_STRETCH_INTEGER;
  3487. }
  3488. sml->get_root()->set_content_scale_mode(cs_sm);
  3489. sml->get_root()->set_content_scale_aspect(cs_aspect);
  3490. sml->get_root()->set_content_scale_stretch(cs_stretch);
  3491. sml->get_root()->set_content_scale_size(stretch_size);
  3492. sml->get_root()->set_content_scale_factor(stretch_scale);
  3493. sml->set_auto_accept_quit(GLOBAL_GET("application/config/auto_accept_quit"));
  3494. sml->set_quit_on_go_back(GLOBAL_GET("application/config/quit_on_go_back"));
  3495. String appname = GLOBAL_GET("application/config/name");
  3496. appname = TranslationServer::get_singleton()->translate(appname);
  3497. #ifdef DEBUG_ENABLED
  3498. // Append a suffix to the window title to denote that the project is running
  3499. // from a debug build (including the editor). Since this results in lower performance,
  3500. // this should be clearly presented to the user.
  3501. DisplayServer::get_singleton()->window_set_title(vformat("%s (DEBUG)", appname));
  3502. #else
  3503. DisplayServer::get_singleton()->window_set_title(appname);
  3504. #endif
  3505. bool snap_controls = GLOBAL_GET("gui/common/snap_controls_to_pixels");
  3506. sml->get_root()->set_snap_controls_to_pixels(snap_controls);
  3507. bool font_oversampling = GLOBAL_GET("gui/fonts/dynamic_fonts/use_oversampling");
  3508. sml->get_root()->set_use_font_oversampling(font_oversampling);
  3509. int texture_filter = GLOBAL_GET("rendering/textures/canvas_textures/default_texture_filter");
  3510. int texture_repeat = GLOBAL_GET("rendering/textures/canvas_textures/default_texture_repeat");
  3511. sml->get_root()->set_default_canvas_item_texture_filter(
  3512. Viewport::DefaultCanvasItemTextureFilter(texture_filter));
  3513. sml->get_root()->set_default_canvas_item_texture_repeat(
  3514. Viewport::DefaultCanvasItemTextureRepeat(texture_repeat));
  3515. }
  3516. #ifdef TOOLS_ENABLED
  3517. if (editor) {
  3518. bool editor_embed_subwindows = EditorSettings::get_singleton()->get_setting(
  3519. "interface/editor/single_window_mode");
  3520. if (editor_embed_subwindows) {
  3521. sml->get_root()->set_embedding_subwindows(true);
  3522. }
  3523. restore_editor_window_layout = EditorSettings::get_singleton()->get_setting("interface/editor/editor_screen").operator int() == EditorSettings::InitialScreen::INITIAL_SCREEN_AUTO;
  3524. }
  3525. #endif
  3526. String local_game_path;
  3527. if (!game_path.is_empty() && !project_manager) {
  3528. local_game_path = game_path.replace("\\", "/");
  3529. if (!local_game_path.begins_with("res://")) {
  3530. bool absolute =
  3531. (local_game_path.size() > 1) && (local_game_path[0] == '/' || local_game_path[1] == ':');
  3532. if (!absolute) {
  3533. if (ProjectSettings::get_singleton()->is_using_datapack()) {
  3534. local_game_path = "res://" + local_game_path;
  3535. } else {
  3536. int sep = local_game_path.rfind_char('/');
  3537. if (sep == -1) {
  3538. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  3539. ERR_FAIL_COND_V(da.is_null(), EXIT_FAILURE);
  3540. local_game_path = da->get_current_dir().path_join(local_game_path);
  3541. } else {
  3542. Ref<DirAccess> da = DirAccess::open(local_game_path.substr(0, sep));
  3543. if (da.is_valid()) {
  3544. local_game_path = da->get_current_dir().path_join(
  3545. local_game_path.substr(sep + 1, local_game_path.length()));
  3546. }
  3547. }
  3548. }
  3549. }
  3550. }
  3551. local_game_path = ProjectSettings::get_singleton()->localize_path(local_game_path);
  3552. #ifdef TOOLS_ENABLED
  3553. if (editor) {
  3554. if (game_path != String(GLOBAL_GET("application/run/main_scene")) || !editor_node->has_scenes_in_session()) {
  3555. Error serr = editor_node->load_scene(local_game_path);
  3556. if (serr != OK) {
  3557. ERR_PRINT("Failed to load scene");
  3558. }
  3559. }
  3560. if (!debug_server_uri.is_empty()) {
  3561. EditorDebuggerNode::get_singleton()->start(debug_server_uri);
  3562. EditorDebuggerNode::get_singleton()->set_keep_open(true);
  3563. }
  3564. }
  3565. #endif
  3566. }
  3567. if (!project_manager && !editor) { // game
  3568. OS::get_singleton()->benchmark_begin_measure("Startup", "Load Game");
  3569. // Load SSL Certificates from Project Settings (or builtin).
  3570. Crypto::load_default_certificates(GLOBAL_GET("network/tls/certificate_bundle_override"));
  3571. if (!game_path.is_empty()) {
  3572. Node *scene = nullptr;
  3573. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  3574. if (scenedata.is_valid()) {
  3575. scene = scenedata->instantiate();
  3576. }
  3577. ERR_FAIL_NULL_V_MSG(scene, EXIT_FAILURE, "Failed loading scene: " + local_game_path + ".");
  3578. sml->add_current_scene(scene);
  3579. #ifdef MACOS_ENABLED
  3580. String mac_icon_path = GLOBAL_GET("application/config/macos_native_icon");
  3581. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_ICON) && !mac_icon_path.is_empty()) {
  3582. DisplayServer::get_singleton()->set_native_icon(mac_icon_path);
  3583. has_icon = true;
  3584. }
  3585. #endif
  3586. #ifdef WINDOWS_ENABLED
  3587. String win_icon_path = GLOBAL_GET("application/config/windows_native_icon");
  3588. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_ICON) && !win_icon_path.is_empty()) {
  3589. DisplayServer::get_singleton()->set_native_icon(win_icon_path);
  3590. has_icon = true;
  3591. }
  3592. #endif
  3593. String icon_path = GLOBAL_GET("application/config/icon");
  3594. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_ICON) && !icon_path.is_empty() && !has_icon) {
  3595. Ref<Image> icon;
  3596. icon.instantiate();
  3597. if (ImageLoader::load_image(icon_path, icon) == OK) {
  3598. DisplayServer::get_singleton()->set_icon(icon);
  3599. has_icon = true;
  3600. }
  3601. }
  3602. }
  3603. OS::get_singleton()->benchmark_end_measure("Startup", "Load Game");
  3604. }
  3605. #ifdef TOOLS_ENABLED
  3606. if (project_manager) {
  3607. OS::get_singleton()->benchmark_begin_measure("Startup", "Project Manager");
  3608. Engine::get_singleton()->set_editor_hint(true);
  3609. if (editor_pseudolocalization) {
  3610. translation_server->get_editor_domain()->set_pseudolocalization_enabled(true);
  3611. }
  3612. ProjectManager *pmanager = memnew(ProjectManager);
  3613. ProgressDialog *progress_dialog = memnew(ProgressDialog);
  3614. pmanager->add_child(progress_dialog);
  3615. sml->get_root()->add_child(pmanager);
  3616. OS::get_singleton()->benchmark_end_measure("Startup", "Project Manager");
  3617. }
  3618. if (project_manager || editor) {
  3619. // Load SSL Certificates from Editor Settings (or builtin)
  3620. Crypto::load_default_certificates(
  3621. EditorSettings::get_singleton()->get_setting("network/tls/editor_tls_certificates").operator String());
  3622. }
  3623. #endif
  3624. }
  3625. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_ICON) && !has_icon && OS::get_singleton()->get_bundle_icon_path().is_empty()) {
  3626. Ref<Image> icon = memnew(Image(app_icon_png));
  3627. DisplayServer::get_singleton()->set_icon(icon);
  3628. }
  3629. if (movie_writer) {
  3630. movie_writer->begin(DisplayServer::get_singleton()->window_get_size(), fixed_fps, Engine::get_singleton()->get_write_movie_path());
  3631. }
  3632. if (minimum_time_msec) {
  3633. uint64_t minimum_time = 1000 * minimum_time_msec;
  3634. uint64_t elapsed_time = OS::get_singleton()->get_ticks_usec();
  3635. if (elapsed_time < minimum_time) {
  3636. OS::get_singleton()->delay_usec(minimum_time - elapsed_time);
  3637. }
  3638. }
  3639. OS::get_singleton()->benchmark_end_measure("Startup", "Main::Start");
  3640. OS::get_singleton()->benchmark_dump();
  3641. return EXIT_SUCCESS;
  3642. }
  3643. /* Main iteration
  3644. *
  3645. * This is the iteration of the engine's game loop, advancing the state of physics,
  3646. * rendering and audio.
  3647. * It's called directly by the platform's OS::run method, where the loop is created
  3648. * and monitored.
  3649. *
  3650. * The OS implementation can impact its draw step with the Main::force_redraw() method.
  3651. */
  3652. uint64_t Main::last_ticks = 0;
  3653. uint32_t Main::frames = 0;
  3654. uint32_t Main::hide_print_fps_attempts = 3;
  3655. uint32_t Main::frame = 0;
  3656. bool Main::force_redraw_requested = false;
  3657. int Main::iterating = 0;
  3658. bool Main::is_iterating() {
  3659. return iterating > 0;
  3660. }
  3661. // For performance metrics.
  3662. static uint64_t physics_process_max = 0;
  3663. static uint64_t process_max = 0;
  3664. static uint64_t navigation_process_max = 0;
  3665. // Return false means iterating further, returning true means `OS::run`
  3666. // will terminate the program. In case of failure, the OS exit code needs
  3667. // to be set explicitly here (defaults to EXIT_SUCCESS).
  3668. bool Main::iteration() {
  3669. iterating++;
  3670. const uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  3671. Engine::get_singleton()->_frame_ticks = ticks;
  3672. main_timer_sync.set_cpu_ticks_usec(ticks);
  3673. main_timer_sync.set_fixed_fps(fixed_fps);
  3674. const uint64_t ticks_elapsed = ticks - last_ticks;
  3675. const int physics_ticks_per_second = Engine::get_singleton()->get_physics_ticks_per_second();
  3676. const double physics_step = 1.0 / physics_ticks_per_second;
  3677. const double time_scale = Engine::get_singleton()->get_time_scale();
  3678. MainFrameTime advance = main_timer_sync.advance(physics_step, physics_ticks_per_second);
  3679. double process_step = advance.process_step;
  3680. double scaled_step = process_step * time_scale;
  3681. Engine::get_singleton()->_process_step = process_step;
  3682. Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction;
  3683. uint64_t physics_process_ticks = 0;
  3684. uint64_t process_ticks = 0;
  3685. uint64_t navigation_process_ticks = 0;
  3686. frame += ticks_elapsed;
  3687. last_ticks = ticks;
  3688. const int max_physics_steps = Engine::get_singleton()->get_max_physics_steps_per_frame();
  3689. if (fixed_fps == -1 && advance.physics_steps > max_physics_steps) {
  3690. process_step -= (advance.physics_steps - max_physics_steps) * physics_step;
  3691. advance.physics_steps = max_physics_steps;
  3692. }
  3693. bool exit = false;
  3694. // process all our active interfaces
  3695. #ifndef _3D_DISABLED
  3696. XRServer::get_singleton()->_process();
  3697. #endif // _3D_DISABLED
  3698. NavigationServer2D::get_singleton()->sync();
  3699. NavigationServer3D::get_singleton()->sync();
  3700. for (int iters = 0; iters < advance.physics_steps; ++iters) {
  3701. if (Input::get_singleton()->is_agile_input_event_flushing()) {
  3702. Input::get_singleton()->flush_buffered_events();
  3703. }
  3704. Engine::get_singleton()->_in_physics = true;
  3705. Engine::get_singleton()->_physics_frames++;
  3706. uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();
  3707. // Prepare the fixed timestep interpolated nodes BEFORE they are updated
  3708. // by the physics server, otherwise the current and previous transforms
  3709. // may be the same, and no interpolation takes place.
  3710. OS::get_singleton()->get_main_loop()->iteration_prepare();
  3711. #ifndef _3D_DISABLED
  3712. PhysicsServer3D::get_singleton()->sync();
  3713. PhysicsServer3D::get_singleton()->flush_queries();
  3714. #endif // _3D_DISABLED
  3715. PhysicsServer2D::get_singleton()->sync();
  3716. PhysicsServer2D::get_singleton()->flush_queries();
  3717. if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) {
  3718. #ifndef _3D_DISABLED
  3719. PhysicsServer3D::get_singleton()->end_sync();
  3720. #endif // _3D_DISABLED
  3721. PhysicsServer2D::get_singleton()->end_sync();
  3722. Engine::get_singleton()->_in_physics = false;
  3723. exit = true;
  3724. break;
  3725. }
  3726. uint64_t navigation_begin = OS::get_singleton()->get_ticks_usec();
  3727. NavigationServer3D::get_singleton()->process(physics_step * time_scale);
  3728. navigation_process_ticks = MAX(navigation_process_ticks, OS::get_singleton()->get_ticks_usec() - navigation_begin); // keep the largest one for reference
  3729. navigation_process_max = MAX(OS::get_singleton()->get_ticks_usec() - navigation_begin, navigation_process_max);
  3730. message_queue->flush();
  3731. #ifndef _3D_DISABLED
  3732. PhysicsServer3D::get_singleton()->end_sync();
  3733. PhysicsServer3D::get_singleton()->step(physics_step * time_scale);
  3734. #endif // _3D_DISABLED
  3735. PhysicsServer2D::get_singleton()->end_sync();
  3736. PhysicsServer2D::get_singleton()->step(physics_step * time_scale);
  3737. message_queue->flush();
  3738. OS::get_singleton()->get_main_loop()->iteration_end();
  3739. physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
  3740. physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
  3741. Engine::get_singleton()->_in_physics = false;
  3742. }
  3743. if (Input::get_singleton()->is_agile_input_event_flushing()) {
  3744. Input::get_singleton()->flush_buffered_events();
  3745. }
  3746. uint64_t process_begin = OS::get_singleton()->get_ticks_usec();
  3747. if (OS::get_singleton()->get_main_loop()->process(process_step * time_scale)) {
  3748. exit = true;
  3749. }
  3750. message_queue->flush();
  3751. RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames.
  3752. if ((DisplayServer::get_singleton()->can_any_window_draw() || DisplayServer::get_singleton()->has_additional_outputs()) &&
  3753. RenderingServer::get_singleton()->is_render_loop_enabled()) {
  3754. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  3755. if (RenderingServer::get_singleton()->has_changed()) {
  3756. RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
  3757. Engine::get_singleton()->increment_frames_drawn();
  3758. }
  3759. } else {
  3760. RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
  3761. Engine::get_singleton()->increment_frames_drawn();
  3762. force_redraw_requested = false;
  3763. }
  3764. }
  3765. process_ticks = OS::get_singleton()->get_ticks_usec() - process_begin;
  3766. process_max = MAX(process_ticks, process_max);
  3767. uint64_t frame_time = OS::get_singleton()->get_ticks_usec() - ticks;
  3768. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  3769. ScriptServer::get_language(i)->frame();
  3770. }
  3771. AudioServer::get_singleton()->update();
  3772. if (EngineDebugger::is_active()) {
  3773. EngineDebugger::get_singleton()->iteration(frame_time, process_ticks, physics_process_ticks, physics_step);
  3774. }
  3775. frames++;
  3776. Engine::get_singleton()->_process_frames++;
  3777. if (frame > 1000000) {
  3778. // Wait a few seconds before printing FPS, as FPS reporting just after the engine has started is inaccurate.
  3779. if (hide_print_fps_attempts == 0) {
  3780. if (editor || project_manager) {
  3781. if (print_fps) {
  3782. print_line(vformat("Editor FPS: %d (%s mspf)", frames, rtos(1000.0 / frames).pad_decimals(2)));
  3783. }
  3784. } else if (print_fps || GLOBAL_GET("debug/settings/stdout/print_fps")) {
  3785. print_line(vformat("Project FPS: %d (%s mspf)", frames, rtos(1000.0 / frames).pad_decimals(2)));
  3786. }
  3787. } else {
  3788. hide_print_fps_attempts--;
  3789. }
  3790. Engine::get_singleton()->_fps = frames;
  3791. performance->set_process_time(USEC_TO_SEC(process_max));
  3792. performance->set_physics_process_time(USEC_TO_SEC(physics_process_max));
  3793. performance->set_navigation_process_time(USEC_TO_SEC(navigation_process_max));
  3794. process_max = 0;
  3795. physics_process_max = 0;
  3796. navigation_process_max = 0;
  3797. frame %= 1000000;
  3798. frames = 0;
  3799. }
  3800. iterating--;
  3801. if (movie_writer) {
  3802. movie_writer->add_frame();
  3803. }
  3804. #ifdef TOOLS_ENABLED
  3805. bool quit_after_timeout = false;
  3806. #endif
  3807. if ((quit_after > 0) && (Engine::get_singleton()->_process_frames >= quit_after)) {
  3808. #ifdef TOOLS_ENABLED
  3809. quit_after_timeout = true;
  3810. #endif
  3811. exit = true;
  3812. }
  3813. #ifdef TOOLS_ENABLED
  3814. if (wait_for_import && EditorFileSystem::get_singleton() && EditorFileSystem::get_singleton()->doing_first_scan()) {
  3815. exit = false;
  3816. }
  3817. #endif
  3818. if (fixed_fps != -1) {
  3819. return exit;
  3820. }
  3821. OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw());
  3822. #ifdef TOOLS_ENABLED
  3823. if (auto_build_solutions) {
  3824. auto_build_solutions = false;
  3825. // Only relevant when running the editor.
  3826. if (!editor) {
  3827. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  3828. ERR_FAIL_V_MSG(true,
  3829. "Command line option --build-solutions was passed, but no project is being edited. Aborting.");
  3830. }
  3831. if (!EditorNode::get_singleton()->call_build()) {
  3832. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  3833. ERR_FAIL_V_MSG(true,
  3834. "Command line option --build-solutions was passed, but the build callback failed. Aborting.");
  3835. }
  3836. }
  3837. #endif
  3838. #ifdef TOOLS_ENABLED
  3839. if (exit && quit_after_timeout && EditorNode::get_singleton()) {
  3840. EditorNode::get_singleton()->unload_editor_addons();
  3841. }
  3842. #endif
  3843. return exit;
  3844. }
  3845. void Main::force_redraw() {
  3846. force_redraw_requested = true;
  3847. }
  3848. /* Engine deinitialization
  3849. *
  3850. * Responsible for freeing all the memory allocated by previous setup steps,
  3851. * so that the engine closes cleanly without leaking memory or crashing.
  3852. * The order matters as some of those steps are linked with each other.
  3853. */
  3854. void Main::cleanup(bool p_force) {
  3855. OS::get_singleton()->benchmark_begin_measure("Shutdown", "Main::Cleanup");
  3856. if (!p_force) {
  3857. ERR_FAIL_COND(!_start_success);
  3858. }
  3859. #ifdef DEBUG_ENABLED
  3860. if (input) {
  3861. input->flush_frame_parsed_events();
  3862. }
  3863. #endif
  3864. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  3865. TextServerManager::get_singleton()->get_interface(i)->cleanup();
  3866. }
  3867. if (movie_writer) {
  3868. movie_writer->end();
  3869. }
  3870. ResourceLoader::clear_thread_load_tasks();
  3871. ResourceLoader::remove_custom_loaders();
  3872. ResourceSaver::remove_custom_savers();
  3873. PropertyListHelper::clear_base_helpers();
  3874. // Flush before uninitializing the scene, but delete the MessageQueue as late as possible.
  3875. message_queue->flush();
  3876. OS::get_singleton()->delete_main_loop();
  3877. OS::get_singleton()->_cmdline.clear();
  3878. OS::get_singleton()->_user_args.clear();
  3879. OS::get_singleton()->_execpath = "";
  3880. OS::get_singleton()->_local_clipboard = "";
  3881. ResourceLoader::clear_translation_remaps();
  3882. ResourceLoader::clear_path_remaps();
  3883. WorkerThreadPool::get_singleton()->exit_languages_threads();
  3884. ScriptServer::finish_languages();
  3885. // Sync pending commands that may have been queued from a different thread during ScriptServer finalization
  3886. RenderingServer::get_singleton()->sync();
  3887. //clear global shader variables before scene and other graphics stuff are deinitialized.
  3888. rendering_server->global_shader_parameters_clear();
  3889. #ifndef _3D_DISABLED
  3890. if (xr_server) {
  3891. // Now that we're unregistering properly in plugins we need to keep access to xr_server for a little longer
  3892. // We do however unset our primary interface
  3893. xr_server->set_primary_interface(Ref<XRInterface>());
  3894. }
  3895. #endif // _3D_DISABLED
  3896. #ifdef TOOLS_ENABLED
  3897. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  3898. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  3899. unregister_editor_types();
  3900. #endif
  3901. ImageLoader::cleanup();
  3902. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  3903. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  3904. unregister_platform_apis();
  3905. unregister_driver_types();
  3906. unregister_scene_types();
  3907. finalize_theme_db();
  3908. // Before deinitializing server extensions, finalize servers which may be loaded as extensions.
  3909. finalize_navigation_server();
  3910. finalize_physics();
  3911. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  3912. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  3913. unregister_server_types();
  3914. EngineDebugger::deinitialize();
  3915. #ifndef _3D_DISABLED
  3916. if (xr_server) {
  3917. memdelete(xr_server);
  3918. }
  3919. #endif // _3D_DISABLED
  3920. if (audio_server) {
  3921. audio_server->finish();
  3922. memdelete(audio_server);
  3923. }
  3924. if (camera_server) {
  3925. memdelete(camera_server);
  3926. }
  3927. OS::get_singleton()->finalize();
  3928. finalize_display();
  3929. if (input) {
  3930. memdelete(input);
  3931. }
  3932. if (packed_data) {
  3933. memdelete(packed_data);
  3934. }
  3935. if (performance) {
  3936. memdelete(performance);
  3937. }
  3938. if (input_map) {
  3939. memdelete(input_map);
  3940. }
  3941. if (translation_server) {
  3942. memdelete(translation_server);
  3943. }
  3944. if (tsman) {
  3945. memdelete(tsman);
  3946. }
  3947. #ifndef _3D_DISABLED
  3948. if (physics_server_3d_manager) {
  3949. memdelete(physics_server_3d_manager);
  3950. }
  3951. #endif // _3D_DISABLED
  3952. if (physics_server_2d_manager) {
  3953. memdelete(physics_server_2d_manager);
  3954. }
  3955. if (globals) {
  3956. memdelete(globals);
  3957. }
  3958. if (OS::get_singleton()->is_restart_on_exit_set()) {
  3959. //attempt to restart with arguments
  3960. List<String> args = OS::get_singleton()->get_restart_on_exit_arguments();
  3961. OS::get_singleton()->create_instance(args);
  3962. OS::get_singleton()->set_restart_on_exit(false, List<String>()); //clear list (uses memory)
  3963. }
  3964. // Now should be safe to delete MessageQueue (famous last words).
  3965. message_queue->flush();
  3966. memdelete(message_queue);
  3967. #if defined(STEAMAPI_ENABLED)
  3968. if (steam_tracker) {
  3969. memdelete(steam_tracker);
  3970. }
  3971. #endif
  3972. unregister_core_driver_types();
  3973. unregister_core_extensions();
  3974. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  3975. if (engine) {
  3976. memdelete(engine);
  3977. }
  3978. unregister_core_types();
  3979. OS::get_singleton()->benchmark_end_measure("Shutdown", "Main::Cleanup");
  3980. OS::get_singleton()->benchmark_dump();
  3981. OS::get_singleton()->finalize_core();
  3982. }