main.cpp 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428
  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/core_string_names.h"
  34. #include "core/crypto/crypto.h"
  35. #include "core/debugger/engine_debugger.h"
  36. #include "core/extension/extension_api_dump.h"
  37. #include "core/extension/gdextension_interface_dump.gen.h"
  38. #include "core/extension/gdextension_manager.h"
  39. #include "core/input/input.h"
  40. #include "core/input/input_map.h"
  41. #include "core/io/dir_access.h"
  42. #include "core/io/file_access_network.h"
  43. #include "core/io/file_access_pack.h"
  44. #include "core/io/file_access_zip.h"
  45. #include "core/io/image_loader.h"
  46. #include "core/io/ip.h"
  47. #include "core/io/resource_loader.h"
  48. #include "core/object/message_queue.h"
  49. #include "core/os/os.h"
  50. #include "core/os/time.h"
  51. #include "core/register_core_types.h"
  52. #include "core/string/translation.h"
  53. #include "core/version.h"
  54. #include "drivers/register_driver_types.h"
  55. #include "main/app_icon.gen.h"
  56. #include "main/main_timer_sync.h"
  57. #include "main/performance.h"
  58. #include "main/splash.gen.h"
  59. #include "modules/register_module_types.h"
  60. #include "platform/register_platform_apis.h"
  61. #include "scene/main/scene_tree.h"
  62. #include "scene/main/window.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_2d.h"
  72. #include "servers/navigation_server_3d.h"
  73. #include "servers/navigation_server_3d_dummy.h"
  74. #include "servers/physics_server_2d.h"
  75. #include "servers/physics_server_3d.h"
  76. #include "servers/register_server_types.h"
  77. #include "servers/rendering/rendering_server_default.h"
  78. #include "servers/text/text_server_dummy.h"
  79. #include "servers/text_server.h"
  80. #include "servers/xr_server.h"
  81. #ifdef TESTS_ENABLED
  82. #include "tests/test_main.h"
  83. #endif
  84. #ifdef TOOLS_ENABLED
  85. #include "editor/debugger/editor_debugger_node.h"
  86. #include "editor/doc_data_class_path.gen.h"
  87. #include "editor/doc_tools.h"
  88. #include "editor/editor_node.h"
  89. #include "editor/editor_paths.h"
  90. #include "editor/editor_settings.h"
  91. #include "editor/editor_translation.h"
  92. #include "editor/progress_dialog.h"
  93. #include "editor/project_manager.h"
  94. #include "editor/register_editor_types.h"
  95. #ifndef NO_EDITOR_SPLASH
  96. #include "main/splash_editor.gen.h"
  97. #endif
  98. #ifndef DISABLE_DEPRECATED
  99. #include "editor/project_converter_3_to_4.h"
  100. #endif // DISABLE_DEPRECATED
  101. #endif // TOOLS_ENABLED
  102. #include "modules/modules_enabled.gen.h" // For mono.
  103. #if defined(MODULE_MONO_ENABLED) && defined(TOOLS_ENABLED)
  104. #include "modules/mono/editor/bindings_generator.h"
  105. #endif
  106. /* Static members */
  107. // Singletons
  108. // Initialized in setup()
  109. static Engine *engine = nullptr;
  110. static ProjectSettings *globals = nullptr;
  111. static Input *input = nullptr;
  112. static InputMap *input_map = nullptr;
  113. static TranslationServer *translation_server = nullptr;
  114. static Performance *performance = nullptr;
  115. static PackedData *packed_data = nullptr;
  116. static Time *time_singleton = nullptr;
  117. #ifdef MINIZIP_ENABLED
  118. static ZipArchive *zip_packed_data = nullptr;
  119. #endif
  120. static FileAccessNetworkClient *file_access_network_client = nullptr;
  121. static MessageQueue *message_queue = nullptr;
  122. // Initialized in setup2()
  123. static AudioServer *audio_server = nullptr;
  124. static DisplayServer *display_server = nullptr;
  125. static RenderingServer *rendering_server = nullptr;
  126. static CameraServer *camera_server = nullptr;
  127. static XRServer *xr_server = nullptr;
  128. static TextServerManager *tsman = nullptr;
  129. static PhysicsServer3DManager *physics_server_3d_manager = nullptr;
  130. static PhysicsServer3D *physics_server_3d = nullptr;
  131. static PhysicsServer2DManager *physics_server_2d_manager = nullptr;
  132. static PhysicsServer2D *physics_server_2d = nullptr;
  133. static NavigationServer3D *navigation_server_3d = nullptr;
  134. static NavigationServer2D *navigation_server_2d = nullptr;
  135. static ThemeDB *theme_db = nullptr;
  136. // We error out if setup2() doesn't turn this true
  137. static bool _start_success = false;
  138. // Drivers
  139. String tablet_driver = "";
  140. String text_driver = "";
  141. String rendering_driver = "";
  142. String rendering_method = "";
  143. static int text_driver_idx = -1;
  144. static int display_driver_idx = -1;
  145. static int audio_driver_idx = -1;
  146. // Engine config/tools
  147. static bool single_window = false;
  148. static bool editor = false;
  149. static bool project_manager = false;
  150. static bool cmdline_tool = false;
  151. static String locale;
  152. static bool show_help = false;
  153. static bool auto_quit = false;
  154. static OS::ProcessID editor_pid = 0;
  155. #ifdef TOOLS_ENABLED
  156. static bool found_project = false;
  157. static bool auto_build_solutions = false;
  158. static String debug_server_uri;
  159. #ifndef DISABLE_DEPRECATED
  160. static int converter_max_kb_file = 4 * 1024; // 4MB
  161. static int converter_max_line_length = 100000;
  162. #endif // DISABLE_DEPRECATED
  163. HashMap<Main::CLIScope, Vector<String>> forwardable_cli_arguments;
  164. #endif
  165. bool use_startup_benchmark = false;
  166. String startup_benchmark_file;
  167. // Display
  168. static DisplayServer::WindowMode window_mode = DisplayServer::WINDOW_MODE_WINDOWED;
  169. static DisplayServer::ScreenOrientation window_orientation = DisplayServer::SCREEN_LANDSCAPE;
  170. static DisplayServer::VSyncMode window_vsync_mode = DisplayServer::VSYNC_ENABLED;
  171. static uint32_t window_flags = 0;
  172. static Size2i window_size = Size2i(1152, 648);
  173. static int init_screen = DisplayServer::SCREEN_PRIMARY;
  174. static bool init_fullscreen = false;
  175. static bool init_maximized = false;
  176. static bool init_windowed = false;
  177. static bool init_always_on_top = false;
  178. static bool init_use_custom_pos = false;
  179. static bool init_use_custom_screen = false;
  180. static Vector2 init_custom_pos;
  181. // Debug
  182. static bool use_debug_profiler = false;
  183. #ifdef DEBUG_ENABLED
  184. static bool debug_collisions = false;
  185. static bool debug_paths = false;
  186. static bool debug_navigation = false;
  187. #endif
  188. static int frame_delay = 0;
  189. static bool disable_render_loop = false;
  190. static int fixed_fps = -1;
  191. static MovieWriter *movie_writer = nullptr;
  192. static bool disable_vsync = false;
  193. static bool print_fps = false;
  194. #ifdef TOOLS_ENABLED
  195. static bool dump_gdextension_interface = false;
  196. static bool dump_extension_api = false;
  197. #endif
  198. bool profile_gpu = false;
  199. // Constants.
  200. static const String NULL_DISPLAY_DRIVER("headless");
  201. static const String NULL_AUDIO_DRIVER("Dummy");
  202. /* Helper methods */
  203. bool Main::is_cmdline_tool() {
  204. return cmdline_tool;
  205. }
  206. #ifdef TOOLS_ENABLED
  207. const Vector<String> &Main::get_forwardable_cli_arguments(Main::CLIScope p_scope) {
  208. return forwardable_cli_arguments[p_scope];
  209. }
  210. #endif
  211. static String unescape_cmdline(const String &p_str) {
  212. return p_str.replace("%20", " ");
  213. }
  214. static String get_full_version_string() {
  215. String hash = String(VERSION_HASH);
  216. if (!hash.is_empty()) {
  217. hash = "." + hash.left(9);
  218. }
  219. return String(VERSION_FULL_BUILD) + hash;
  220. }
  221. // FIXME: Could maybe be moved to have less code in main.cpp.
  222. void initialize_physics() {
  223. /// 3D Physics Server
  224. physics_server_3d = PhysicsServer3DManager::get_singleton()->new_server(
  225. GLOBAL_GET(PhysicsServer3DManager::setting_property_name));
  226. if (!physics_server_3d) {
  227. // Physics server not found, Use the default physics
  228. physics_server_3d = PhysicsServer3DManager::get_singleton()->new_default_server();
  229. }
  230. ERR_FAIL_COND(!physics_server_3d);
  231. physics_server_3d->init();
  232. // 2D Physics server
  233. physics_server_2d = PhysicsServer2DManager::get_singleton()->new_server(
  234. GLOBAL_GET(PhysicsServer2DManager::get_singleton()->setting_property_name));
  235. if (!physics_server_2d) {
  236. // Physics server not found, Use the default physics
  237. physics_server_2d = PhysicsServer2DManager::get_singleton()->new_default_server();
  238. }
  239. ERR_FAIL_COND(!physics_server_2d);
  240. physics_server_2d->init();
  241. }
  242. void finalize_physics() {
  243. physics_server_3d->finish();
  244. memdelete(physics_server_3d);
  245. physics_server_2d->finish();
  246. memdelete(physics_server_2d);
  247. }
  248. void finalize_display() {
  249. rendering_server->finish();
  250. memdelete(rendering_server);
  251. memdelete(display_server);
  252. }
  253. void initialize_navigation_server() {
  254. ERR_FAIL_COND(navigation_server_3d != nullptr);
  255. // Init 3D Navigation Server
  256. navigation_server_3d = NavigationServer3DManager::new_default_server();
  257. // Fall back to dummy if no default server has been registered.
  258. if (!navigation_server_3d) {
  259. WARN_PRINT_ONCE("No NavigationServer3D implementation has been registered! Falling back to a dummy implementation: navigation features will be unavailable.");
  260. navigation_server_3d = memnew(NavigationServer3DDummy);
  261. }
  262. // Should be impossible, but make sure it's not null.
  263. ERR_FAIL_NULL_MSG(navigation_server_3d, "Failed to initialize NavigationServer3D.");
  264. // Init 2D Navigation Server
  265. navigation_server_2d = memnew(NavigationServer2D);
  266. ERR_FAIL_NULL_MSG(navigation_server_2d, "Failed to initialize NavigationServer2D.");
  267. }
  268. void finalize_navigation_server() {
  269. memdelete(navigation_server_3d);
  270. navigation_server_3d = nullptr;
  271. memdelete(navigation_server_2d);
  272. navigation_server_2d = nullptr;
  273. }
  274. void initialize_theme_db() {
  275. theme_db = memnew(ThemeDB);
  276. }
  277. void finalize_theme_db() {
  278. memdelete(theme_db);
  279. theme_db = nullptr;
  280. }
  281. //#define DEBUG_INIT
  282. #ifdef DEBUG_INIT
  283. #define MAIN_PRINT(m_txt) print_line(m_txt)
  284. #else
  285. #define MAIN_PRINT(m_txt)
  286. #endif
  287. void Main::print_help(const char *p_binary) {
  288. print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
  289. OS::get_singleton()->print("Free and open source software under the terms of the MIT license.\n");
  290. OS::get_singleton()->print("(c) 2014-present Godot Engine contributors.\n");
  291. OS::get_singleton()->print("(c) 2007-2014 Juan Linietsky, Ariel Manzur.\n");
  292. OS::get_singleton()->print("\n");
  293. OS::get_singleton()->print("Usage: %s [options] [path to scene or 'project.godot' file]\n", p_binary);
  294. OS::get_singleton()->print("\n");
  295. OS::get_singleton()->print("General options:\n");
  296. OS::get_singleton()->print(" -h, --help Display this help message.\n");
  297. OS::get_singleton()->print(" --version Display the version string.\n");
  298. OS::get_singleton()->print(" -v, --verbose Use verbose stdout mode.\n");
  299. OS::get_singleton()->print(" -q, --quiet Quiet mode, silences stdout messages. Errors are still displayed.\n");
  300. OS::get_singleton()->print("\n");
  301. OS::get_singleton()->print("Run options:\n");
  302. OS::get_singleton()->print(" --, ++ Separator for user-provided arguments. Following arguments are not used by the engine, but can be read from `OS.get_cmdline_user_args()`.\n");
  303. #ifdef TOOLS_ENABLED
  304. OS::get_singleton()->print(" -e, --editor Start the editor instead of running the scene.\n");
  305. OS::get_singleton()->print(" -p, --project-manager Start the project manager, even if a project is auto-detected.\n");
  306. OS::get_singleton()->print(" --debug-server <uri> Start the editor debug server (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007)\n");
  307. #endif
  308. OS::get_singleton()->print(" --quit Quit after the first iteration.\n");
  309. OS::get_singleton()->print(" -l, --language <locale> Use a specific locale (<locale> being a two-letter code).\n");
  310. OS::get_singleton()->print(" --path <directory> Path to a project (<directory> must contain a 'project.godot' file).\n");
  311. OS::get_singleton()->print(" -u, --upwards Scan folders upwards for project.godot file.\n");
  312. OS::get_singleton()->print(" --main-pack <file> Path to a pack (.pck) file to load.\n");
  313. OS::get_singleton()->print(" --render-thread <mode> Render thread mode ['unsafe', 'safe', 'separate'].\n");
  314. OS::get_singleton()->print(" --remote-fs <address> Remote filesystem (<host/IP>[:<port>] address).\n");
  315. OS::get_singleton()->print(" --remote-fs-password <password> Password for remote filesystem.\n");
  316. OS::get_singleton()->print(" --audio-driver <driver> Audio driver [");
  317. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  318. if (i > 0) {
  319. OS::get_singleton()->print(", ");
  320. }
  321. OS::get_singleton()->print("'%s'", AudioDriverManager::get_driver(i)->get_name());
  322. }
  323. OS::get_singleton()->print("].\n");
  324. OS::get_singleton()->print(" --display-driver <driver> Display driver (and rendering driver) [");
  325. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  326. if (i > 0) {
  327. OS::get_singleton()->print(", ");
  328. }
  329. OS::get_singleton()->print("'%s' (", DisplayServer::get_create_function_name(i));
  330. Vector<String> rd = DisplayServer::get_create_function_rendering_drivers(i);
  331. for (int j = 0; j < rd.size(); j++) {
  332. if (j > 0) {
  333. OS::get_singleton()->print(", ");
  334. }
  335. OS::get_singleton()->print("'%s'", rd[j].utf8().get_data());
  336. }
  337. OS::get_singleton()->print(")");
  338. }
  339. OS::get_singleton()->print("].\n");
  340. OS::get_singleton()->print(" --rendering-method <renderer> Renderer name. Requires driver support.\n");
  341. OS::get_singleton()->print(" --rendering-driver <driver> Rendering driver (depends on display driver).\n");
  342. OS::get_singleton()->print(" --gpu-index <device_index> Use a specific GPU (run with --verbose to get available device list).\n");
  343. OS::get_singleton()->print(" --text-driver <driver> Text driver (Fonts, BiDi, shaping).\n");
  344. OS::get_singleton()->print(" --tablet-driver <driver> Pen tablet input driver.\n");
  345. OS::get_singleton()->print(" --headless Enable headless mode (--display-driver headless --audio-driver Dummy). Useful for servers and with --script.\n");
  346. OS::get_singleton()->print(" --write-movie <file> Writes a video to the specified path (usually with .avi or .png extension).\n");
  347. OS::get_singleton()->print(" --fixed-fps is forced when enabled, but it can be used to change movie FPS.\n");
  348. OS::get_singleton()->print(" --disable-vsync can speed up movie writing but makes interaction more difficult.\n");
  349. OS::get_singleton()->print("\n");
  350. OS::get_singleton()->print("Display options:\n");
  351. OS::get_singleton()->print(" -f, --fullscreen Request fullscreen mode.\n");
  352. OS::get_singleton()->print(" -m, --maximized Request a maximized window.\n");
  353. OS::get_singleton()->print(" -w, --windowed Request windowed mode.\n");
  354. OS::get_singleton()->print(" -t, --always-on-top Request an always-on-top window.\n");
  355. OS::get_singleton()->print(" --resolution <W>x<H> Request window resolution.\n");
  356. OS::get_singleton()->print(" --position <X>,<Y> Request window position (if set, screen argument is ignored).\n");
  357. OS::get_singleton()->print(" --screen <N> Request window screen.\n");
  358. OS::get_singleton()->print(" --single-window Use a single window (no separate subwindows).\n");
  359. OS::get_singleton()->print(" --xr-mode <mode> Select XR (Extended Reality) mode ['default', 'off', 'on'].\n");
  360. OS::get_singleton()->print("\n");
  361. OS::get_singleton()->print("Debug options:\n");
  362. OS::get_singleton()->print(" -d, --debug Debug (local stdout debugger).\n");
  363. OS::get_singleton()->print(" -b, --breakpoints Breakpoint list as source::line comma-separated pairs, no spaces (use %%20 instead).\n");
  364. OS::get_singleton()->print(" --profiling Enable profiling in the script debugger.\n");
  365. OS::get_singleton()->print(" --gpu-profile Show a GPU profile of the tasks that took the most time during frame rendering.\n");
  366. OS::get_singleton()->print(" --gpu-validation Enable graphics API validation layers for debugging.\n");
  367. #if DEBUG_ENABLED
  368. OS::get_singleton()->print(" --gpu-abort Abort on graphics API usage errors (usually validation layer errors). May help see the problem if your system freezes.\n");
  369. #endif
  370. OS::get_singleton()->print(" --remote-debug <uri> Remote debug (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007).\n");
  371. #if defined(DEBUG_ENABLED)
  372. OS::get_singleton()->print(" --debug-collisions Show collision shapes when running the scene.\n");
  373. OS::get_singleton()->print(" --debug-paths Show path lines when running the scene.\n");
  374. OS::get_singleton()->print(" --debug-navigation Show navigation polygons when running the scene.\n");
  375. OS::get_singleton()->print(" --debug-stringnames Print all StringName allocations to stdout when the engine quits.\n");
  376. #endif
  377. OS::get_singleton()->print(" --frame-delay <ms> Simulate high CPU load (delay each frame by <ms> milliseconds).\n");
  378. OS::get_singleton()->print(" --time-scale <scale> Force time scale (higher values are faster, 1.0 is normal speed).\n");
  379. OS::get_singleton()->print(" --disable-vsync Forces disabling of vertical synchronization, even if enabled in the project settings. Does not override driver-level V-Sync enforcement.\n");
  380. OS::get_singleton()->print(" --disable-render-loop Disable render loop so rendering only occurs when called explicitly from script.\n");
  381. OS::get_singleton()->print(" --disable-crash-handler Disable crash handler when supported by the platform code.\n");
  382. OS::get_singleton()->print(" --fixed-fps <fps> Force a fixed number of frames per second. This setting disables real-time synchronization.\n");
  383. OS::get_singleton()->print(" --print-fps Print the frames per second to the stdout.\n");
  384. OS::get_singleton()->print("\n");
  385. OS::get_singleton()->print("Standalone tools:\n");
  386. OS::get_singleton()->print(" -s, --script <script> Run a script.\n");
  387. OS::get_singleton()->print(" --check-only Only parse for errors and quit (use with --script).\n");
  388. #ifdef TOOLS_ENABLED
  389. OS::get_singleton()->print(" --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");
  390. OS::get_singleton()->print(" <path> should be absolute or relative to the project directory, and include the filename for the binary (e.g. 'builds/game.exe').\n");
  391. OS::get_singleton()->print(" The target directory must exist.\n");
  392. OS::get_singleton()->print(" --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");
  393. OS::get_singleton()->print(" --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");
  394. #ifndef DISABLE_DEPRECATED
  395. OS::get_singleton()->print(" --convert-3to4 [<max_file_kb>] [<max_line_size>]\n");
  396. OS::get_singleton()->print(" Converts project from Godot 3.x to Godot 4.x.\n");
  397. OS::get_singleton()->print(" --validate-conversion-3to4 [<max_file_kb>] [<max_line_size>]\n");
  398. OS::get_singleton()->print(" Shows what elements will be renamed when converting project from Godot 3.x to Godot 4.x.\n");
  399. #endif // DISABLE_DEPRECATED
  400. OS::get_singleton()->print(" --doctool [<path>] Dump the engine API reference to the given <path> (defaults to current dir) in XML format, merging if existing files are found.\n");
  401. OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n");
  402. OS::get_singleton()->print(" --build-solutions Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n");
  403. OS::get_singleton()->print(" --dump-gdextension-interface Generate GDExtension header file 'gdextension_interface.h' in the current folder. This file is the base file required to implement a GDExtension.\n");
  404. OS::get_singleton()->print(" --dump-extension-api Generate JSON dump of the Godot API for GDExtension bindings named 'extension_api.json' in the current folder.\n");
  405. OS::get_singleton()->print(" --startup-benchmark Benchmark the startup time and print it to console.\n");
  406. OS::get_singleton()->print(" --startup-benchmark-file <path> Benchmark the startup time and save it to a given file in JSON format.\n");
  407. #ifdef TESTS_ENABLED
  408. OS::get_singleton()->print(" --test [--help] Run unit tests. Use --test --help for more information.\n");
  409. #endif
  410. #endif
  411. OS::get_singleton()->print("\n");
  412. }
  413. #ifdef TESTS_ENABLED
  414. // The order is the same as in `Main::setup()`, only core and some editor types
  415. // are initialized here. This also combines `Main::setup2()` initialization.
  416. Error Main::test_setup() {
  417. OS::get_singleton()->initialize();
  418. engine = memnew(Engine);
  419. register_core_types();
  420. register_core_driver_types();
  421. packed_data = memnew(PackedData);
  422. globals = memnew(ProjectSettings);
  423. register_core_settings(); // Here globals are present.
  424. translation_server = memnew(TranslationServer);
  425. tsman = memnew(TextServerManager);
  426. if (tsman) {
  427. Ref<TextServerDummy> ts;
  428. ts.instantiate();
  429. tsman->add_interface(ts);
  430. }
  431. physics_server_3d_manager = memnew(PhysicsServer3DManager);
  432. physics_server_2d_manager = memnew(PhysicsServer2DManager);
  433. // From `Main::setup2()`.
  434. initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  435. register_core_extensions();
  436. register_core_singletons();
  437. /** INITIALIZE SERVERS **/
  438. register_server_types();
  439. XRServer::set_xr_mode(XRServer::XRMODE_OFF); // Skip in tests.
  440. initialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  441. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  442. translation_server->setup(); //register translations, load them, etc.
  443. if (!locale.is_empty()) {
  444. translation_server->set_locale(locale);
  445. }
  446. translation_server->load_translations();
  447. ResourceLoader::load_translation_remaps(); //load remaps for resources
  448. ResourceLoader::load_path_remaps();
  449. register_scene_types();
  450. register_driver_types();
  451. initialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  452. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  453. #ifdef TOOLS_ENABLED
  454. ClassDB::set_current_api(ClassDB::API_EDITOR);
  455. register_editor_types();
  456. initialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  457. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  458. ClassDB::set_current_api(ClassDB::API_CORE);
  459. #endif
  460. register_platform_apis();
  461. // Theme needs modules to be initialized so that sub-resources can be loaded.
  462. initialize_theme_db();
  463. theme_db->initialize_theme();
  464. register_scene_singletons();
  465. ERR_FAIL_COND_V(TextServerManager::get_singleton()->get_interface_count() == 0, ERR_CANT_CREATE);
  466. /* Use one with the most features available. */
  467. int max_features = 0;
  468. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  469. uint32_t features = TextServerManager::get_singleton()->get_interface(i)->get_features();
  470. int feature_number = 0;
  471. while (features) {
  472. feature_number += features & 1;
  473. features >>= 1;
  474. }
  475. if (feature_number >= max_features) {
  476. max_features = feature_number;
  477. text_driver_idx = i;
  478. }
  479. }
  480. if (text_driver_idx >= 0) {
  481. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(text_driver_idx);
  482. TextServerManager::get_singleton()->set_primary_interface(ts);
  483. if (ts->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  484. ts->load_support_data("res://" + ts->get_support_data_filename());
  485. }
  486. } else {
  487. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "TextServer: Unable to create TextServer interface.");
  488. }
  489. ClassDB::set_current_api(ClassDB::API_NONE);
  490. _start_success = true;
  491. return OK;
  492. }
  493. // The order is the same as in `Main::cleanup()`.
  494. void Main::test_cleanup() {
  495. ERR_FAIL_COND(!_start_success);
  496. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  497. TextServerManager::get_singleton()->get_interface(i)->cleanup();
  498. }
  499. EngineDebugger::deinitialize();
  500. ResourceLoader::remove_custom_loaders();
  501. ResourceSaver::remove_custom_savers();
  502. #ifdef TOOLS_ENABLED
  503. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  504. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  505. unregister_editor_types();
  506. #endif
  507. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  508. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  509. unregister_platform_apis();
  510. unregister_driver_types();
  511. unregister_scene_types();
  512. finalize_theme_db();
  513. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  514. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  515. unregister_server_types();
  516. OS::get_singleton()->finalize();
  517. if (translation_server) {
  518. memdelete(translation_server);
  519. }
  520. if (tsman) {
  521. memdelete(tsman);
  522. }
  523. if (physics_server_3d_manager) {
  524. memdelete(physics_server_3d_manager);
  525. }
  526. if (physics_server_2d_manager) {
  527. memdelete(physics_server_2d_manager);
  528. }
  529. if (globals) {
  530. memdelete(globals);
  531. }
  532. if (packed_data) {
  533. memdelete(packed_data);
  534. }
  535. if (engine) {
  536. memdelete(engine);
  537. }
  538. unregister_core_driver_types();
  539. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  540. unregister_core_extensions();
  541. unregister_core_types();
  542. OS::get_singleton()->finalize_core();
  543. }
  544. #endif
  545. int Main::test_entrypoint(int argc, char *argv[], bool &tests_need_run) {
  546. #ifdef TESTS_ENABLED
  547. for (int x = 0; x < argc; x++) {
  548. if ((strncmp(argv[x], "--test", 6) == 0) && (strlen(argv[x]) == 6)) {
  549. tests_need_run = true;
  550. // TODO: need to come up with different test contexts.
  551. // Not every test requires high-level functionality like `ClassDB`.
  552. test_setup();
  553. int status = test_main(argc, argv);
  554. test_cleanup();
  555. return status;
  556. }
  557. }
  558. #endif
  559. tests_need_run = false;
  560. return 0;
  561. }
  562. /* Engine initialization
  563. *
  564. * Consists of several methods that are called by each platform's specific main(argc, argv).
  565. * To fully understand engine init, one should therefore start from the platform's main and
  566. * see how it calls into the Main class' methods.
  567. *
  568. * The initialization is typically done in 3 steps (with the setup2 step triggered either
  569. * automatically by setup, or manually in the platform's main).
  570. *
  571. * - setup(execpath, argc, argv, p_second_phase) is the main entry point for all platforms,
  572. * responsible for the initialization of all low level singletons and core types, and parsing
  573. * command line arguments to configure things accordingly.
  574. * If p_second_phase is true, it will chain into setup2() (default behavior). This is
  575. * disabled on some platforms (Android, iOS, UWP) which trigger the second step in their
  576. * own time.
  577. *
  578. * - setup2(p_main_tid_override) registers high level servers and singletons, displays the
  579. * boot splash, then registers higher level types (scene, editor, etc.).
  580. *
  581. * - start() is the last step and that's where command line tools can run, or the main loop
  582. * can be created eventually and the project settings put into action. That's also where
  583. * the editor node is created, if relevant.
  584. * start() does it own argument parsing for a subset of the command line arguments described
  585. * in help, it's a bit messy and should be globalized with the setup() parsing somehow.
  586. */
  587. Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_phase) {
  588. OS::get_singleton()->initialize();
  589. engine = memnew(Engine);
  590. MAIN_PRINT("Main: Initialize CORE");
  591. engine->startup_begin();
  592. engine->startup_benchmark_begin_measure("core");
  593. register_core_types();
  594. register_core_driver_types();
  595. MAIN_PRINT("Main: Initialize Globals");
  596. input_map = memnew(InputMap);
  597. time_singleton = memnew(Time);
  598. globals = memnew(ProjectSettings);
  599. register_core_settings(); //here globals are present
  600. translation_server = memnew(TranslationServer);
  601. performance = memnew(Performance);
  602. GDREGISTER_CLASS(Performance);
  603. engine->add_singleton(Engine::Singleton("Performance", performance));
  604. // Only flush stdout in debug builds by default, as spamming `print()` will
  605. // decrease performance if this is enabled.
  606. GLOBAL_DEF_RST("application/run/flush_stdout_on_print", false);
  607. GLOBAL_DEF_RST("application/run/flush_stdout_on_print.debug", true);
  608. MAIN_PRINT("Main: Parse CMDLine");
  609. /* argument parsing and main creation */
  610. List<String> args;
  611. List<String> main_args;
  612. List<String> user_args;
  613. bool adding_user_args = false;
  614. List<String> platform_args = OS::get_singleton()->get_cmdline_platform_args();
  615. // Add command line arguments.
  616. for (int i = 0; i < argc; i++) {
  617. args.push_back(String::utf8(argv[i]));
  618. }
  619. // Add arguments received from macOS LaunchService (URL schemas, file associations).
  620. for (const String &arg : platform_args) {
  621. args.push_back(arg);
  622. }
  623. List<String>::Element *I = args.front();
  624. while (I) {
  625. I->get() = unescape_cmdline(I->get().strip_edges());
  626. I = I->next();
  627. }
  628. String display_driver = "";
  629. String audio_driver = "";
  630. String project_path = ".";
  631. bool upwards = false;
  632. String debug_uri = "";
  633. bool skip_breakpoints = false;
  634. String main_pack;
  635. bool quiet_stdout = false;
  636. int rtm = -1;
  637. String remotefs;
  638. String remotefs_pass;
  639. Vector<String> breakpoints;
  640. bool use_custom_res = true;
  641. bool force_res = false;
  642. String default_renderer = "";
  643. String default_renderer_mobile = "";
  644. String renderer_hints = "";
  645. packed_data = PackedData::get_singleton();
  646. if (!packed_data) {
  647. packed_data = memnew(PackedData);
  648. }
  649. #ifdef MINIZIP_ENABLED
  650. //XXX: always get_singleton() == 0x0
  651. zip_packed_data = ZipArchive::get_singleton();
  652. //TODO: remove this temporary fix
  653. if (!zip_packed_data) {
  654. zip_packed_data = memnew(ZipArchive);
  655. }
  656. packed_data->add_pack_source(zip_packed_data);
  657. #endif
  658. // Default exit code, can be modified for certain errors.
  659. Error exit_code = ERR_INVALID_PARAMETER;
  660. I = args.front();
  661. while (I) {
  662. #ifdef MACOS_ENABLED
  663. // Ignore the process serial number argument passed by macOS Gatekeeper.
  664. // Otherwise, Godot would try to open a non-existent project on the first start and abort.
  665. if (I->get().begins_with("-psn_")) {
  666. I = I->next();
  667. continue;
  668. }
  669. #endif
  670. List<String>::Element *N = I->next();
  671. #ifdef TOOLS_ENABLED
  672. if (I->get() == "--debug" ||
  673. I->get() == "--verbose" ||
  674. I->get() == "--disable-crash-handler") {
  675. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->get());
  676. forwardable_cli_arguments[CLI_SCOPE_PROJECT].push_back(I->get());
  677. }
  678. if (I->get() == "--single-window") {
  679. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->get());
  680. }
  681. if (I->get() == "--audio-driver" ||
  682. I->get() == "--display-driver" ||
  683. I->get() == "--rendering-method" ||
  684. I->get() == "--rendering-driver") {
  685. if (I->next()) {
  686. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->get());
  687. forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->next()->get());
  688. }
  689. }
  690. #endif
  691. if (adding_user_args) {
  692. user_args.push_back(I->get());
  693. } else if (I->get() == "-h" || I->get() == "--help" || I->get() == "/?") { // display help
  694. show_help = true;
  695. exit_code = ERR_HELP; // Hack to force an early exit in `main()` with a success code.
  696. goto error;
  697. } else if (I->get() == "--version") {
  698. print_line(get_full_version_string());
  699. exit_code = ERR_HELP; // Hack to force an early exit in `main()` with a success code.
  700. goto error;
  701. } else if (I->get() == "-v" || I->get() == "--verbose") { // verbose output
  702. OS::get_singleton()->_verbose_stdout = true;
  703. } else if (I->get() == "-q" || I->get() == "--quiet") { // quieter output
  704. quiet_stdout = true;
  705. } else if (I->get() == "--audio-driver") { // audio driver
  706. if (I->next()) {
  707. audio_driver = I->next()->get();
  708. bool found = false;
  709. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  710. if (audio_driver == AudioDriverManager::get_driver(i)->get_name()) {
  711. found = true;
  712. }
  713. }
  714. if (!found) {
  715. OS::get_singleton()->print("Unknown audio driver '%s', aborting.\nValid options are ",
  716. audio_driver.utf8().get_data());
  717. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  718. if (i == AudioDriverManager::get_driver_count() - 1) {
  719. OS::get_singleton()->print(" and ");
  720. } else if (i != 0) {
  721. OS::get_singleton()->print(", ");
  722. }
  723. OS::get_singleton()->print("'%s'", AudioDriverManager::get_driver(i)->get_name());
  724. }
  725. OS::get_singleton()->print(".\n");
  726. goto error;
  727. }
  728. N = I->next()->next();
  729. } else {
  730. OS::get_singleton()->print("Missing audio driver argument, aborting.\n");
  731. goto error;
  732. }
  733. } else if (I->get() == "--text-driver") {
  734. if (I->next()) {
  735. text_driver = I->next()->get();
  736. N = I->next()->next();
  737. } else {
  738. OS::get_singleton()->print("Missing text driver argument, aborting.\n");
  739. goto error;
  740. }
  741. } else if (I->get() == "--display-driver") { // force video driver
  742. if (I->next()) {
  743. display_driver = I->next()->get();
  744. bool found = false;
  745. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  746. if (display_driver == DisplayServer::get_create_function_name(i)) {
  747. found = true;
  748. }
  749. }
  750. if (!found) {
  751. OS::get_singleton()->print("Unknown display driver '%s', aborting.\nValid options are ",
  752. display_driver.utf8().get_data());
  753. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  754. if (i == DisplayServer::get_create_function_count() - 1) {
  755. OS::get_singleton()->print(" and ");
  756. } else if (i != 0) {
  757. OS::get_singleton()->print(", ");
  758. }
  759. OS::get_singleton()->print("'%s'", DisplayServer::get_create_function_name(i));
  760. }
  761. OS::get_singleton()->print(".\n");
  762. goto error;
  763. }
  764. N = I->next()->next();
  765. } else {
  766. OS::get_singleton()->print("Missing display driver argument, aborting.\n");
  767. goto error;
  768. }
  769. } else if (I->get() == "--rendering-method") {
  770. if (I->next()) {
  771. rendering_method = I->next()->get();
  772. N = I->next()->next();
  773. } else {
  774. OS::get_singleton()->print("Missing renderer name argument, aborting.\n");
  775. goto error;
  776. }
  777. } else if (I->get() == "--rendering-driver") {
  778. if (I->next()) {
  779. rendering_driver = I->next()->get();
  780. N = I->next()->next();
  781. } else {
  782. OS::get_singleton()->print("Missing rendering driver argument, aborting.\n");
  783. goto error;
  784. }
  785. } else if (I->get() == "-f" || I->get() == "--fullscreen") { // force fullscreen
  786. init_fullscreen = true;
  787. } else if (I->get() == "-m" || I->get() == "--maximized") { // force maximized window
  788. init_maximized = true;
  789. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  790. } else if (I->get() == "-w" || I->get() == "--windowed") { // force windowed window
  791. init_windowed = true;
  792. } else if (I->get() == "--gpu-index") {
  793. if (I->next()) {
  794. Engine::singleton->gpu_idx = I->next()->get().to_int();
  795. N = I->next()->next();
  796. } else {
  797. OS::get_singleton()->print("Missing GPU index argument, aborting.\n");
  798. goto error;
  799. }
  800. } else if (I->get() == "--gpu-validation") {
  801. Engine::singleton->use_validation_layers = true;
  802. #ifdef DEBUG_ENABLED
  803. } else if (I->get() == "--gpu-abort") {
  804. Engine::singleton->abort_on_gpu_errors = true;
  805. #endif
  806. } else if (I->get() == "--tablet-driver") {
  807. if (I->next()) {
  808. tablet_driver = I->next()->get();
  809. N = I->next()->next();
  810. } else {
  811. OS::get_singleton()->print("Missing tablet driver argument, aborting.\n");
  812. goto error;
  813. }
  814. } else if (I->get() == "--single-window") { // force single window
  815. single_window = true;
  816. } else if (I->get() == "-t" || I->get() == "--always-on-top") { // force always-on-top window
  817. init_always_on_top = true;
  818. } else if (I->get() == "--resolution") { // force resolution
  819. if (I->next()) {
  820. String vm = I->next()->get();
  821. if (!vm.contains("x")) { // invalid parameter format
  822. OS::get_singleton()->print("Invalid resolution '%s', it should be e.g. '1280x720'.\n",
  823. vm.utf8().get_data());
  824. goto error;
  825. }
  826. int w = vm.get_slice("x", 0).to_int();
  827. int h = vm.get_slice("x", 1).to_int();
  828. if (w <= 0 || h <= 0) {
  829. OS::get_singleton()->print("Invalid resolution '%s', width and height must be above 0.\n",
  830. vm.utf8().get_data());
  831. goto error;
  832. }
  833. window_size.width = w;
  834. window_size.height = h;
  835. force_res = true;
  836. N = I->next()->next();
  837. } else {
  838. OS::get_singleton()->print("Missing resolution argument, aborting.\n");
  839. goto error;
  840. }
  841. } else if (I->get() == "--screen") { // set window screen
  842. if (I->next()) {
  843. init_screen = I->next()->get().to_int();
  844. init_use_custom_screen = true;
  845. N = I->next()->next();
  846. } else {
  847. OS::get_singleton()->print("Missing screen argument, aborting.\n");
  848. goto error;
  849. }
  850. } else if (I->get() == "--position") { // set window position
  851. if (I->next()) {
  852. String vm = I->next()->get();
  853. if (!vm.contains(",")) { // invalid parameter format
  854. OS::get_singleton()->print("Invalid position '%s', it should be e.g. '80,128'.\n",
  855. vm.utf8().get_data());
  856. goto error;
  857. }
  858. int x = vm.get_slice(",", 0).to_int();
  859. int y = vm.get_slice(",", 1).to_int();
  860. init_custom_pos = Point2(x, y);
  861. init_use_custom_pos = true;
  862. N = I->next()->next();
  863. } else {
  864. OS::get_singleton()->print("Missing position argument, aborting.\n");
  865. goto error;
  866. }
  867. } else if (I->get() == "--headless") { // enable headless mode (no audio, no rendering).
  868. audio_driver = NULL_AUDIO_DRIVER;
  869. display_driver = NULL_DISPLAY_DRIVER;
  870. } else if (I->get() == "--profiling") { // enable profiling
  871. use_debug_profiler = true;
  872. } else if (I->get() == "-l" || I->get() == "--language") { // language
  873. if (I->next()) {
  874. locale = I->next()->get();
  875. N = I->next()->next();
  876. } else {
  877. OS::get_singleton()->print("Missing language argument, aborting.\n");
  878. goto error;
  879. }
  880. } else if (I->get() == "--remote-fs") { // remote filesystem
  881. if (I->next()) {
  882. remotefs = I->next()->get();
  883. N = I->next()->next();
  884. } else {
  885. OS::get_singleton()->print("Missing remote filesystem address, aborting.\n");
  886. goto error;
  887. }
  888. } else if (I->get() == "--remote-fs-password") { // remote filesystem password
  889. if (I->next()) {
  890. remotefs_pass = I->next()->get();
  891. N = I->next()->next();
  892. } else {
  893. OS::get_singleton()->print("Missing remote filesystem password, aborting.\n");
  894. goto error;
  895. }
  896. } else if (I->get() == "--render-thread") { // render thread mode
  897. if (I->next()) {
  898. if (I->next()->get() == "safe") {
  899. rtm = OS::RENDER_THREAD_SAFE;
  900. } else if (I->next()->get() == "unsafe") {
  901. rtm = OS::RENDER_THREAD_UNSAFE;
  902. } else if (I->next()->get() == "separate") {
  903. rtm = OS::RENDER_SEPARATE_THREAD;
  904. }
  905. N = I->next()->next();
  906. } else {
  907. OS::get_singleton()->print("Missing render thread mode argument, aborting.\n");
  908. goto error;
  909. }
  910. #ifdef TOOLS_ENABLED
  911. } else if (I->get() == "-e" || I->get() == "--editor") { // starts editor
  912. editor = true;
  913. } else if (I->get() == "-p" || I->get() == "--project-manager") { // starts project manager
  914. project_manager = true;
  915. } else if (I->get() == "--debug-server") {
  916. if (I->next()) {
  917. debug_server_uri = I->next()->get();
  918. if (!debug_server_uri.contains("://")) { // wrong address
  919. OS::get_singleton()->print("Invalid debug server uri. It should be of the form <protocol>://<bind_address>:<port>.\n");
  920. goto error;
  921. }
  922. N = I->next()->next();
  923. } else {
  924. OS::get_singleton()->print("Missing remote debug server uri, aborting.\n");
  925. goto error;
  926. }
  927. } else if (I->get() == "--build-solutions") { // Build the scripting solution such C#
  928. auto_build_solutions = true;
  929. editor = true;
  930. cmdline_tool = true;
  931. } else if (I->get() == "--dump-gdextension-interface") {
  932. // Register as an editor instance to use low-end fallback if relevant.
  933. editor = true;
  934. cmdline_tool = true;
  935. dump_gdextension_interface = true;
  936. print_line("Dumping GDExtension interface header file");
  937. // Hack. Not needed but otherwise we end up detecting that this should
  938. // run the project instead of a cmdline tool.
  939. // Needs full refactoring to fix properly.
  940. main_args.push_back(I->get());
  941. } else if (I->get() == "--dump-extension-api") {
  942. // Register as an editor instance to use low-end fallback if relevant.
  943. editor = true;
  944. cmdline_tool = true;
  945. dump_extension_api = true;
  946. print_line("Dumping Extension API");
  947. // Hack. Not needed but otherwise we end up detecting that this should
  948. // run the project instead of a cmdline tool.
  949. // Needs full refactoring to fix properly.
  950. main_args.push_back(I->get());
  951. } else if (I->get() == "--export-release" || I->get() == "--export-debug" ||
  952. I->get() == "--export-pack") { // Export project
  953. // Actually handling is done in start().
  954. editor = true;
  955. cmdline_tool = true;
  956. main_args.push_back(I->get());
  957. #ifndef DISABLE_DEPRECATED
  958. } else if (I->get() == "--export") { // For users used to 3.x syntax.
  959. 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");
  960. goto error;
  961. } else if (I->get() == "--convert-3to4") {
  962. // Actually handling is done in start().
  963. cmdline_tool = true;
  964. main_args.push_back(I->get());
  965. if (I->next() && !I->next()->get().begins_with("-")) {
  966. if (itos(I->next()->get().to_int()) == I->next()->get()) {
  967. converter_max_kb_file = I->next()->get().to_int();
  968. }
  969. if (I->next()->next() && !I->next()->next()->get().begins_with("-")) {
  970. if (itos(I->next()->next()->get().to_int()) == I->next()->next()->get()) {
  971. converter_max_line_length = I->next()->next()->get().to_int();
  972. }
  973. }
  974. }
  975. } else if (I->get() == "--validate-conversion-3to4") {
  976. // Actually handling is done in start().
  977. cmdline_tool = true;
  978. main_args.push_back(I->get());
  979. if (I->next() && !I->next()->get().begins_with("-")) {
  980. if (itos(I->next()->get().to_int()) == I->next()->get()) {
  981. converter_max_kb_file = I->next()->get().to_int();
  982. }
  983. if (I->next()->next() && !I->next()->next()->get().begins_with("-")) {
  984. if (itos(I->next()->next()->get().to_int()) == I->next()->next()->get()) {
  985. converter_max_line_length = I->next()->next()->get().to_int();
  986. }
  987. }
  988. }
  989. #endif // DISABLE_DEPRECATED
  990. } else if (I->get() == "--doctool") {
  991. // Actually handling is done in start().
  992. cmdline_tool = true;
  993. // `--doctool` implies `--headless` to avoid spawning an unnecessary window
  994. // and speed up class reference generation.
  995. audio_driver = NULL_AUDIO_DRIVER;
  996. display_driver = NULL_DISPLAY_DRIVER;
  997. main_args.push_back(I->get());
  998. #endif // TOOLS_ENABLED
  999. } else if (I->get() == "--path") { // set path of project to start or edit
  1000. if (I->next()) {
  1001. String p = I->next()->get();
  1002. if (OS::get_singleton()->set_cwd(p) != OK) {
  1003. OS::get_singleton()->print("Invalid project path specified: \"%s\", aborting.\n", p.utf8().get_data());
  1004. goto error;
  1005. }
  1006. N = I->next()->next();
  1007. } else {
  1008. OS::get_singleton()->print("Missing relative or absolute path, aborting.\n");
  1009. goto error;
  1010. }
  1011. } else if (I->get() == "-u" || I->get() == "--upwards") { // scan folders upwards
  1012. upwards = true;
  1013. } else if (I->get() == "--quit") { // Auto quit at the end of the first main loop iteration
  1014. auto_quit = true;
  1015. } else if (I->get().ends_with("project.godot")) {
  1016. String path;
  1017. String file = I->get();
  1018. int sep = MAX(file.rfind("/"), file.rfind("\\"));
  1019. if (sep == -1) {
  1020. path = ".";
  1021. } else {
  1022. path = file.substr(0, sep);
  1023. }
  1024. if (OS::get_singleton()->set_cwd(path) == OK) {
  1025. // path already specified, don't override
  1026. } else {
  1027. project_path = path;
  1028. }
  1029. #ifdef TOOLS_ENABLED
  1030. editor = true;
  1031. #endif
  1032. } else if (I->get() == "-b" || I->get() == "--breakpoints") { // add breakpoints
  1033. if (I->next()) {
  1034. String bplist = I->next()->get();
  1035. breakpoints = bplist.split(",");
  1036. N = I->next()->next();
  1037. } else {
  1038. OS::get_singleton()->print("Missing list of breakpoints, aborting.\n");
  1039. goto error;
  1040. }
  1041. } else if (I->get() == "--frame-delay") { // force frame delay
  1042. if (I->next()) {
  1043. frame_delay = I->next()->get().to_int();
  1044. N = I->next()->next();
  1045. } else {
  1046. OS::get_singleton()->print("Missing frame delay argument, aborting.\n");
  1047. goto error;
  1048. }
  1049. } else if (I->get() == "--time-scale") { // force time scale
  1050. if (I->next()) {
  1051. Engine::get_singleton()->set_time_scale(I->next()->get().to_float());
  1052. N = I->next()->next();
  1053. } else {
  1054. OS::get_singleton()->print("Missing time scale argument, aborting.\n");
  1055. goto error;
  1056. }
  1057. } else if (I->get() == "--main-pack") {
  1058. if (I->next()) {
  1059. main_pack = I->next()->get();
  1060. N = I->next()->next();
  1061. } else {
  1062. OS::get_singleton()->print("Missing path to main pack file, aborting.\n");
  1063. goto error;
  1064. };
  1065. } else if (I->get() == "-d" || I->get() == "--debug") {
  1066. debug_uri = "local://";
  1067. OS::get_singleton()->_debug_stdout = true;
  1068. #if defined(DEBUG_ENABLED)
  1069. } else if (I->get() == "--debug-collisions") {
  1070. debug_collisions = true;
  1071. } else if (I->get() == "--debug-paths") {
  1072. debug_paths = true;
  1073. } else if (I->get() == "--debug-navigation") {
  1074. debug_navigation = true;
  1075. } else if (I->get() == "--debug-stringnames") {
  1076. StringName::set_debug_stringnames(true);
  1077. #endif
  1078. } else if (I->get() == "--remote-debug") {
  1079. if (I->next()) {
  1080. debug_uri = I->next()->get();
  1081. if (!debug_uri.contains("://")) { // wrong address
  1082. OS::get_singleton()->print(
  1083. "Invalid debug host address, it should be of the form <protocol>://<host/IP>:<port>.\n");
  1084. goto error;
  1085. }
  1086. N = I->next()->next();
  1087. } else {
  1088. OS::get_singleton()->print("Missing remote debug host address, aborting.\n");
  1089. goto error;
  1090. }
  1091. } else if (I->get() == "--editor-pid") { // not exposed to user
  1092. if (I->next()) {
  1093. editor_pid = I->next()->get().to_int();
  1094. N = I->next()->next();
  1095. } else {
  1096. OS::get_singleton()->print("Missing editor PID argument, aborting.\n");
  1097. goto error;
  1098. }
  1099. } else if (I->get() == "--disable-render-loop") {
  1100. disable_render_loop = true;
  1101. } else if (I->get() == "--fixed-fps") {
  1102. if (I->next()) {
  1103. fixed_fps = I->next()->get().to_int();
  1104. N = I->next()->next();
  1105. } else {
  1106. OS::get_singleton()->print("Missing fixed-fps argument, aborting.\n");
  1107. goto error;
  1108. }
  1109. } else if (I->get() == "--write-movie") {
  1110. if (I->next()) {
  1111. Engine::get_singleton()->set_write_movie_path(I->next()->get());
  1112. N = I->next()->next();
  1113. if (fixed_fps == -1) {
  1114. fixed_fps = 60;
  1115. }
  1116. OS::get_singleton()->_writing_movie = true;
  1117. } else {
  1118. OS::get_singleton()->print("Missing write-movie argument, aborting.\n");
  1119. goto error;
  1120. }
  1121. } else if (I->get() == "--disable-vsync") {
  1122. disable_vsync = true;
  1123. } else if (I->get() == "--print-fps") {
  1124. print_fps = true;
  1125. } else if (I->get() == "--profile-gpu") {
  1126. profile_gpu = true;
  1127. } else if (I->get() == "--disable-crash-handler") {
  1128. OS::get_singleton()->disable_crash_handler();
  1129. } else if (I->get() == "--skip-breakpoints") {
  1130. skip_breakpoints = true;
  1131. } else if (I->get() == "--xr-mode") {
  1132. if (I->next()) {
  1133. String xr_mode = I->next()->get().to_lower();
  1134. N = I->next()->next();
  1135. if (xr_mode == "default") {
  1136. XRServer::set_xr_mode(XRServer::XRMODE_DEFAULT);
  1137. } else if (xr_mode == "off") {
  1138. XRServer::set_xr_mode(XRServer::XRMODE_OFF);
  1139. } else if (xr_mode == "on") {
  1140. XRServer::set_xr_mode(XRServer::XRMODE_ON);
  1141. } else {
  1142. OS::get_singleton()->print("Unknown --xr-mode argument \"%s\", aborting.\n", xr_mode.ascii().get_data());
  1143. goto error;
  1144. }
  1145. } else {
  1146. OS::get_singleton()->print("Missing --xr-mode argument, aborting.\n");
  1147. goto error;
  1148. }
  1149. } else if (I->get() == "--startup-benchmark") {
  1150. use_startup_benchmark = true;
  1151. } else if (I->get() == "--startup-benchmark-file") {
  1152. if (I->next()) {
  1153. use_startup_benchmark = true;
  1154. startup_benchmark_file = I->next()->get();
  1155. N = I->next()->next();
  1156. } else {
  1157. OS::get_singleton()->print("Missing <path> argument for --startup-benchmark-file <path>.\n");
  1158. goto error;
  1159. }
  1160. } else if (I->get() == "--" || I->get() == "++") {
  1161. adding_user_args = true;
  1162. } else {
  1163. main_args.push_back(I->get());
  1164. }
  1165. I = N;
  1166. }
  1167. #ifdef TOOLS_ENABLED
  1168. if (editor && project_manager) {
  1169. OS::get_singleton()->print(
  1170. "Error: Command line arguments implied opening both editor and project manager, which is not possible. Aborting.\n");
  1171. goto error;
  1172. }
  1173. #endif
  1174. // Network file system needs to be configured before globals, since globals are based on the
  1175. // 'project.godot' file which will only be available through the network if this is enabled
  1176. FileAccessNetwork::configure();
  1177. if (!remotefs.is_empty()) {
  1178. file_access_network_client = memnew(FileAccessNetworkClient);
  1179. int port;
  1180. if (remotefs.contains(":")) {
  1181. port = remotefs.get_slicec(':', 1).to_int();
  1182. remotefs = remotefs.get_slicec(':', 0);
  1183. } else {
  1184. port = 6010;
  1185. }
  1186. Error err = file_access_network_client->connect(remotefs, port, remotefs_pass);
  1187. if (err) {
  1188. OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i.\n", remotefs.utf8().get_data(), port);
  1189. goto error;
  1190. }
  1191. FileAccess::make_default<FileAccessNetwork>(FileAccess::ACCESS_RESOURCES);
  1192. }
  1193. if (globals->setup(project_path, main_pack, upwards, editor) == OK) {
  1194. #ifdef TOOLS_ENABLED
  1195. found_project = true;
  1196. #endif
  1197. } else {
  1198. #ifdef TOOLS_ENABLED
  1199. editor = false;
  1200. #else
  1201. 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";
  1202. OS::get_singleton()->print("%s", error_msg.utf8().get_data());
  1203. OS::get_singleton()->alert(error_msg);
  1204. goto error;
  1205. #endif
  1206. }
  1207. // Initialize user data dir.
  1208. OS::get_singleton()->ensure_user_data_dir();
  1209. initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  1210. register_core_extensions(); // core extensions must be registered after globals setup and before display
  1211. ResourceUID::get_singleton()->load_from_cache(); // load UUIDs from cache.
  1212. if (ProjectSettings::get_singleton()->has_custom_feature("dedicated_server")) {
  1213. audio_driver = NULL_AUDIO_DRIVER;
  1214. display_driver = NULL_DISPLAY_DRIVER;
  1215. }
  1216. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_chars_per_second", PROPERTY_HINT_RANGE, "0, 4096, 1, or_greater"), 32768);
  1217. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_queued_messages", PROPERTY_HINT_RANGE, "0, 8192, 1, or_greater"), 2048);
  1218. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_errors_per_second", PROPERTY_HINT_RANGE, "0, 200, 1, or_greater"), 400);
  1219. GLOBAL_DEF(PropertyInfo(Variant::INT, "network/limits/debugger/max_warnings_per_second", PROPERTY_HINT_RANGE, "0, 200, 1, or_greater"), 400);
  1220. EngineDebugger::initialize(debug_uri, skip_breakpoints, breakpoints, []() {
  1221. if (editor_pid) {
  1222. DisplayServer::get_singleton()->enable_for_stealing_focus(editor_pid);
  1223. }
  1224. });
  1225. #ifdef TOOLS_ENABLED
  1226. if (editor) {
  1227. packed_data->set_disabled(true);
  1228. Engine::get_singleton()->set_editor_hint(true);
  1229. main_args.push_back("--editor");
  1230. if (!init_windowed) {
  1231. init_maximized = true;
  1232. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  1233. }
  1234. }
  1235. if (!project_manager && !editor) {
  1236. // If we didn't find a project, we fall back to the project manager.
  1237. project_manager = !found_project && !cmdline_tool;
  1238. }
  1239. if (project_manager) {
  1240. Engine::get_singleton()->set_project_manager_hint(true);
  1241. }
  1242. #endif
  1243. GLOBAL_DEF("debug/file_logging/enable_file_logging", false);
  1244. // Only file logging by default on desktop platforms as logs can't be
  1245. // accessed easily on mobile/Web platforms (if at all).
  1246. // This also prevents logs from being created for the editor instance, as feature tags
  1247. // are disabled while in the editor (even if they should logically apply).
  1248. GLOBAL_DEF("debug/file_logging/enable_file_logging.pc", true);
  1249. GLOBAL_DEF("debug/file_logging/log_path", "user://logs/godot.log");
  1250. GLOBAL_DEF(PropertyInfo(Variant::INT, "debug/file_logging/max_log_files", PROPERTY_HINT_RANGE, "0,20,1,or_greater"), 5);
  1251. if (!project_manager && !editor && FileAccess::get_create_func(FileAccess::ACCESS_USERDATA) &&
  1252. GLOBAL_GET("debug/file_logging/enable_file_logging")) {
  1253. // Don't create logs for the project manager as they would be written to
  1254. // the current working directory, which is inconvenient.
  1255. String base_path = GLOBAL_GET("debug/file_logging/log_path");
  1256. int max_files = GLOBAL_GET("debug/file_logging/max_log_files");
  1257. OS::get_singleton()->add_logger(memnew(RotatedFileLogger(base_path, max_files)));
  1258. }
  1259. if (main_args.size() == 0 && String(GLOBAL_GET("application/run/main_scene")) == "") {
  1260. #ifdef TOOLS_ENABLED
  1261. if (!editor && !project_manager) {
  1262. #endif
  1263. const String error_msg = "Error: Can't run project: no main scene defined in the project.\n";
  1264. OS::get_singleton()->print("%s", error_msg.utf8().get_data());
  1265. OS::get_singleton()->alert(error_msg);
  1266. goto error;
  1267. #ifdef TOOLS_ENABLED
  1268. }
  1269. #endif
  1270. }
  1271. if (editor || project_manager) {
  1272. Engine::get_singleton()->set_editor_hint(true);
  1273. use_custom_res = false;
  1274. input_map->load_default(); //keys for editor
  1275. } else {
  1276. input_map->load_from_project_settings(); //keys for game
  1277. }
  1278. if (bool(GLOBAL_GET("application/run/disable_stdout"))) {
  1279. quiet_stdout = true;
  1280. }
  1281. if (bool(GLOBAL_GET("application/run/disable_stderr"))) {
  1282. CoreGlobals::print_error_enabled = false;
  1283. };
  1284. if (quiet_stdout) {
  1285. CoreGlobals::print_line_enabled = false;
  1286. }
  1287. Logger::set_flush_stdout_on_print(GLOBAL_GET("application/run/flush_stdout_on_print"));
  1288. OS::get_singleton()->set_cmdline(execpath, main_args, user_args);
  1289. {
  1290. String driver_hints = "";
  1291. #ifdef VULKAN_ENABLED
  1292. driver_hints = "vulkan";
  1293. #endif
  1294. String default_driver = driver_hints.get_slice(",", 0);
  1295. // For now everything defaults to vulkan when available. This can change in future updates.
  1296. GLOBAL_DEF("rendering/rendering_device/driver", default_driver);
  1297. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.windows", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1298. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.linuxbsd", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1299. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.android", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1300. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.ios", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1301. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/rendering_device/driver.macos", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1302. driver_hints = "";
  1303. #ifdef GLES3_ENABLED
  1304. driver_hints += "opengl3";
  1305. #endif
  1306. default_driver = driver_hints.get_slice(",", 0);
  1307. GLOBAL_DEF("rendering/gl_compatibility/driver", default_driver);
  1308. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.windows", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1309. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.linuxbsd", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1310. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.web", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1311. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.android", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1312. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.ios", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1313. GLOBAL_DEF(PropertyInfo(Variant::STRING, "rendering/gl_compatibility/driver.macos", PROPERTY_HINT_ENUM, driver_hints), default_driver);
  1314. }
  1315. // Start with RenderingDevice-based backends. Should be included if any RD driver present.
  1316. #ifdef VULKAN_ENABLED
  1317. renderer_hints = "forward_plus,mobile";
  1318. default_renderer_mobile = "mobile";
  1319. #endif
  1320. // And Compatibility next, or first if Vulkan is disabled.
  1321. #ifdef GLES3_ENABLED
  1322. if (!renderer_hints.is_empty()) {
  1323. renderer_hints += ",";
  1324. }
  1325. renderer_hints += "gl_compatibility";
  1326. if (default_renderer_mobile.is_empty()) {
  1327. default_renderer_mobile = "gl_compatibility";
  1328. }
  1329. // Default to Compatibility when using the project manager.
  1330. if (rendering_driver.is_empty() && rendering_method.is_empty() && project_manager) {
  1331. rendering_driver = "opengl3";
  1332. rendering_method = "gl_compatibility";
  1333. default_renderer_mobile = "gl_compatibility";
  1334. }
  1335. #endif
  1336. if (renderer_hints.is_empty()) {
  1337. ERR_PRINT("No renderers available.");
  1338. }
  1339. if (!rendering_method.is_empty()) {
  1340. if (rendering_method != "forward_plus" &&
  1341. rendering_method != "mobile" &&
  1342. rendering_method != "gl_compatibility") {
  1343. OS::get_singleton()->print("Unknown renderer name '%s', aborting. Valid options are: %s\n", rendering_method.utf8().get_data(), renderer_hints.utf8().get_data());
  1344. goto error;
  1345. }
  1346. }
  1347. if (!rendering_driver.is_empty()) {
  1348. // As the rendering drivers available may depend on the display driver and renderer
  1349. // selected, we can't do an exhaustive check here, but we can look through all
  1350. // the options in all the display drivers for a match.
  1351. bool found = false;
  1352. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1353. Vector<String> r_drivers = DisplayServer::get_create_function_rendering_drivers(i);
  1354. for (int d = 0; d < r_drivers.size(); d++) {
  1355. if (rendering_driver == r_drivers[d]) {
  1356. found = true;
  1357. break;
  1358. }
  1359. }
  1360. }
  1361. if (!found) {
  1362. OS::get_singleton()->print("Unknown rendering driver '%s', aborting.\nValid options are ",
  1363. rendering_driver.utf8().get_data());
  1364. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1365. Vector<String> r_drivers = DisplayServer::get_create_function_rendering_drivers(i);
  1366. for (int d = 0; d < r_drivers.size(); d++) {
  1367. OS::get_singleton()->print("'%s', ", r_drivers[d].utf8().get_data());
  1368. }
  1369. }
  1370. OS::get_singleton()->print(".\n");
  1371. goto error;
  1372. }
  1373. // Set a default renderer if none selected. Try to choose one that matches the driver.
  1374. if (rendering_method.is_empty()) {
  1375. if (rendering_driver == "opengl3") {
  1376. rendering_method = "gl_compatibility";
  1377. } else {
  1378. rendering_method = "forward_plus";
  1379. }
  1380. }
  1381. // Now validate whether the selected driver matches with the renderer.
  1382. bool valid_combination = false;
  1383. Vector<String> available_drivers;
  1384. #ifdef VULKAN_ENABLED
  1385. if (rendering_method == "forward_plus" || rendering_method == "mobile") {
  1386. available_drivers.push_back("vulkan");
  1387. }
  1388. #endif
  1389. #ifdef GLES3_ENABLED
  1390. if (rendering_method == "gl_compatibility") {
  1391. available_drivers.push_back("opengl3");
  1392. }
  1393. #endif
  1394. if (available_drivers.is_empty()) {
  1395. OS::get_singleton()->print("Unknown renderer name '%s', aborting.\n", rendering_method.utf8().get_data());
  1396. goto error;
  1397. }
  1398. for (int i = 0; i < available_drivers.size(); i++) {
  1399. if (rendering_driver == available_drivers[i]) {
  1400. valid_combination = true;
  1401. break;
  1402. }
  1403. }
  1404. if (!valid_combination) {
  1405. 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());
  1406. for (int d = 0; d < available_drivers.size(); d++) {
  1407. OS::get_singleton()->print("'%s', ", available_drivers[d].utf8().get_data());
  1408. }
  1409. OS::get_singleton()->print(".\n");
  1410. goto error;
  1411. }
  1412. }
  1413. default_renderer = renderer_hints.get_slice(",", 0);
  1414. GLOBAL_DEF_RST_BASIC(PropertyInfo(Variant::STRING, "rendering/renderer/rendering_method", PROPERTY_HINT_ENUM, renderer_hints), default_renderer);
  1415. GLOBAL_DEF_RST_BASIC("rendering/renderer/rendering_method.mobile", default_renderer_mobile);
  1416. GLOBAL_DEF_RST_BASIC("rendering/renderer/rendering_method.web", "gl_compatibility"); // This is a bit of a hack until we have WebGPU support.
  1417. // Default to ProjectSettings default if nothing set on the command line.
  1418. if (rendering_method.is_empty()) {
  1419. rendering_method = GLOBAL_GET("rendering/renderer/rendering_method");
  1420. }
  1421. if (rendering_driver.is_empty()) {
  1422. if (rendering_method == "gl_compatibility") {
  1423. rendering_driver = GLOBAL_GET("rendering/gl_compatibility/driver");
  1424. } else {
  1425. rendering_driver = GLOBAL_GET("rendering/rendering_device/driver");
  1426. }
  1427. }
  1428. // note this is the desired rendering driver, it doesn't mean we will get it.
  1429. // TODO - make sure this is updated in the case of fallbacks, so that the user interface
  1430. // shows the correct driver string.
  1431. OS::get_singleton()->set_current_rendering_driver_name(rendering_driver);
  1432. OS::get_singleton()->set_current_rendering_method(rendering_method);
  1433. // always convert to lower case for consistency in the code
  1434. rendering_driver = rendering_driver.to_lower();
  1435. if (use_custom_res) {
  1436. if (!force_res) {
  1437. window_size.width = GLOBAL_GET("display/window/size/viewport_width");
  1438. window_size.height = GLOBAL_GET("display/window/size/viewport_height");
  1439. if (globals->has_setting("display/window/size/window_width_override") &&
  1440. globals->has_setting("display/window/size/window_height_override")) {
  1441. int desired_width = globals->get("display/window/size/window_width_override");
  1442. if (desired_width > 0) {
  1443. window_size.width = desired_width;
  1444. }
  1445. int desired_height = globals->get("display/window/size/window_height_override");
  1446. if (desired_height > 0) {
  1447. window_size.height = desired_height;
  1448. }
  1449. }
  1450. }
  1451. if (!bool(GLOBAL_GET("display/window/size/resizable"))) {
  1452. window_flags |= DisplayServer::WINDOW_FLAG_RESIZE_DISABLED_BIT;
  1453. }
  1454. if (bool(GLOBAL_GET("display/window/size/borderless"))) {
  1455. window_flags |= DisplayServer::WINDOW_FLAG_BORDERLESS_BIT;
  1456. }
  1457. if (bool(GLOBAL_GET("display/window/size/always_on_top"))) {
  1458. window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP_BIT;
  1459. }
  1460. if (bool(GLOBAL_GET("display/window/size/transparent"))) {
  1461. window_flags |= DisplayServer::WINDOW_FLAG_TRANSPARENT_BIT;
  1462. }
  1463. if (bool(GLOBAL_GET("display/window/size/extend_to_title"))) {
  1464. window_flags |= DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE_BIT;
  1465. }
  1466. if (bool(GLOBAL_GET("display/window/size/no_focus"))) {
  1467. window_flags |= DisplayServer::WINDOW_FLAG_NO_FOCUS_BIT;
  1468. }
  1469. window_mode = (DisplayServer::WindowMode)(GLOBAL_GET("display/window/size/mode").operator int());
  1470. int initial_position_type = GLOBAL_GET("display/window/size/initial_position_type").operator int();
  1471. if (initial_position_type == 0) {
  1472. if (!init_use_custom_pos) {
  1473. init_custom_pos = GLOBAL_GET("display/window/size/initial_position").operator Vector2i();
  1474. init_use_custom_pos = true;
  1475. }
  1476. } else if (initial_position_type == 1) {
  1477. if (!init_use_custom_screen) {
  1478. init_screen = DisplayServer::SCREEN_PRIMARY;
  1479. init_use_custom_screen = true;
  1480. }
  1481. } else if (initial_position_type == 2) {
  1482. if (!init_use_custom_screen) {
  1483. init_screen = GLOBAL_GET("display/window/size/initial_screen").operator int();
  1484. init_use_custom_screen = true;
  1485. }
  1486. }
  1487. }
  1488. GLOBAL_DEF("internationalization/locale/include_text_server_data", false);
  1489. OS::get_singleton()->_allow_hidpi = GLOBAL_DEF("display/window/dpi/allow_hidpi", true);
  1490. OS::get_singleton()->_allow_layered = GLOBAL_DEF("display/window/per_pixel_transparency/allowed", false);
  1491. #ifdef TOOLS_ENABLED
  1492. if (editor || project_manager) {
  1493. // The editor and project manager always detect and use hiDPI if needed.
  1494. OS::get_singleton()->_allow_hidpi = true;
  1495. // Disable Vulkan overlays in editor, they cause various issues.
  1496. OS::get_singleton()->set_environment("DISABLE_MANGOHUD", "1"); // GH-57403.
  1497. OS::get_singleton()->set_environment("DISABLE_RTSS_LAYER", "1"); // GH-57937.
  1498. OS::get_singleton()->set_environment("DISABLE_VKBASALT", "1");
  1499. } else {
  1500. // Re-allow using Vulkan overlays, disabled while using the editor.
  1501. OS::get_singleton()->unset_environment("DISABLE_MANGOHUD");
  1502. OS::get_singleton()->unset_environment("DISABLE_RTSS_LAYER");
  1503. OS::get_singleton()->unset_environment("DISABLE_VKBASALT");
  1504. }
  1505. #endif
  1506. if (rtm == -1) {
  1507. rtm = GLOBAL_DEF("rendering/driver/threads/thread_model", OS::RENDER_THREAD_SAFE);
  1508. }
  1509. if (rtm >= 0 && rtm < 3) {
  1510. if (editor) {
  1511. rtm = OS::RENDER_THREAD_SAFE;
  1512. }
  1513. OS::get_singleton()->_render_thread_mode = OS::RenderThreadMode(rtm);
  1514. }
  1515. /* Determine audio and video drivers */
  1516. // Display driver, e.g. X11, Wayland.
  1517. // Make sure that headless is the last one, which it is assumed to be by design.
  1518. DEV_ASSERT(NULL_DISPLAY_DRIVER == DisplayServer::get_create_function_name(DisplayServer::get_create_function_count() - 1));
  1519. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1520. String name = DisplayServer::get_create_function_name(i);
  1521. if (display_driver == name) {
  1522. display_driver_idx = i;
  1523. break;
  1524. }
  1525. }
  1526. if (display_driver_idx < 0) {
  1527. // If the requested driver wasn't found, pick the first entry.
  1528. // If all else failed it would be the headless server.
  1529. display_driver_idx = 0;
  1530. }
  1531. // Store this in a globally accessible place, so we can retrieve the rendering drivers
  1532. // list from the display driver for the editor UI.
  1533. OS::get_singleton()->set_display_driver_id(display_driver_idx);
  1534. GLOBAL_DEF_RST_NOVAL("audio/driver/driver", AudioDriverManager::get_driver(0)->get_name());
  1535. if (audio_driver.is_empty()) { // Specified in project.godot.
  1536. audio_driver = GLOBAL_GET("audio/driver/driver");
  1537. }
  1538. // Make sure that dummy is the last one, which it is assumed to be by design.
  1539. DEV_ASSERT(NULL_AUDIO_DRIVER == AudioDriverManager::get_driver(AudioDriverManager::get_driver_count() - 1)->get_name());
  1540. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  1541. if (audio_driver == AudioDriverManager::get_driver(i)->get_name()) {
  1542. audio_driver_idx = i;
  1543. break;
  1544. }
  1545. }
  1546. if (audio_driver_idx < 0) {
  1547. // If the requested driver wasn't found, pick the first entry.
  1548. // If all else failed it would be the dummy driver (no sound).
  1549. audio_driver_idx = 0;
  1550. }
  1551. if (Engine::get_singleton()->get_write_movie_path() != String()) {
  1552. // Always use dummy driver for audio driver (which is last), also in no threaded mode.
  1553. audio_driver_idx = AudioDriverManager::get_driver_count() - 1;
  1554. AudioDriverDummy::get_dummy_singleton()->set_use_threads(false);
  1555. }
  1556. {
  1557. window_orientation = DisplayServer::ScreenOrientation(int(GLOBAL_DEF_BASIC("display/window/handheld/orientation", DisplayServer::ScreenOrientation::SCREEN_LANDSCAPE)));
  1558. }
  1559. {
  1560. window_vsync_mode = DisplayServer::VSyncMode(int(GLOBAL_DEF_BASIC("display/window/vsync/vsync_mode", DisplayServer::VSyncMode::VSYNC_ENABLED)));
  1561. if (disable_vsync) {
  1562. window_vsync_mode = DisplayServer::VSyncMode::VSYNC_DISABLED;
  1563. }
  1564. }
  1565. 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));
  1566. 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));
  1567. Engine::get_singleton()->set_physics_jitter_fix(GLOBAL_DEF("physics/common/physics_jitter_fix", 0.5));
  1568. Engine::get_singleton()->set_max_fps(GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/max_fps", PROPERTY_HINT_RANGE, "0,1000,1"), 0));
  1569. GLOBAL_DEF("debug/settings/stdout/print_fps", false);
  1570. GLOBAL_DEF("debug/settings/stdout/print_gpu_profile", false);
  1571. GLOBAL_DEF("debug/settings/stdout/verbose_stdout", false);
  1572. if (!OS::get_singleton()->_verbose_stdout) { // Not manually overridden.
  1573. OS::get_singleton()->_verbose_stdout = GLOBAL_GET("debug/settings/stdout/verbose_stdout");
  1574. }
  1575. if (frame_delay == 0) {
  1576. frame_delay = GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/frame_delay_msec", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), 0);
  1577. }
  1578. OS::get_singleton()->set_low_processor_usage_mode(GLOBAL_DEF("application/run/low_processor_mode", false));
  1579. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(
  1580. GLOBAL_DEF(PropertyInfo(Variant::INT, "application/run/low_processor_mode_sleep_usec", PROPERTY_HINT_RANGE, "0,33200,1,or_greater"), 6900)); // Roughly 144 FPS
  1581. GLOBAL_DEF("display/window/ios/allow_high_refresh_rate", true);
  1582. GLOBAL_DEF("display/window/ios/hide_home_indicator", true);
  1583. GLOBAL_DEF("display/window/ios/hide_status_bar", true);
  1584. GLOBAL_DEF("display/window/ios/suppress_ui_gesture", true);
  1585. // XR project settings.
  1586. GLOBAL_DEF_RST_BASIC("xr/openxr/enabled", false);
  1587. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "xr/openxr/default_action_map", PROPERTY_HINT_FILE, "*.tres"), "res://openxr_action_map.tres");
  1588. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/form_factor", PROPERTY_HINT_ENUM, "Head Mounted,Handheld"), "0");
  1589. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/view_configuration", PROPERTY_HINT_ENUM, "Mono,Stereo"), "1"); // "Mono,Stereo,Quad,Observer"
  1590. GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "xr/openxr/reference_space", PROPERTY_HINT_ENUM, "Local,Stage"), "1");
  1591. GLOBAL_DEF_BASIC("xr/openxr/submit_depth_buffer", false);
  1592. GLOBAL_DEF_BASIC("xr/openxr/startup_alert", true);
  1593. #ifdef TOOLS_ENABLED
  1594. // Disabled for now, using XR inside of the editor we'll be working on during the coming months.
  1595. // editor settings (it seems we're too early in the process when setting up rendering, to access editor settings...)
  1596. // EDITOR_DEF_RST("xr/openxr/in_editor", false);
  1597. // GLOBAL_DEF("xr/openxr/in_editor", false);
  1598. #endif
  1599. Engine::get_singleton()->set_frame_delay(frame_delay);
  1600. message_queue = memnew(MessageQueue);
  1601. engine->startup_benchmark_end_measure(); // core
  1602. if (p_second_phase) {
  1603. return setup2();
  1604. }
  1605. return OK;
  1606. error:
  1607. text_driver = "";
  1608. display_driver = "";
  1609. audio_driver = "";
  1610. tablet_driver = "";
  1611. Engine::get_singleton()->set_write_movie_path(String());
  1612. project_path = "";
  1613. args.clear();
  1614. main_args.clear();
  1615. if (show_help) {
  1616. print_help(execpath);
  1617. }
  1618. EngineDebugger::deinitialize();
  1619. if (performance) {
  1620. memdelete(performance);
  1621. }
  1622. if (input_map) {
  1623. memdelete(input_map);
  1624. }
  1625. if (time_singleton) {
  1626. memdelete(time_singleton);
  1627. }
  1628. if (translation_server) {
  1629. memdelete(translation_server);
  1630. }
  1631. if (globals) {
  1632. memdelete(globals);
  1633. }
  1634. if (engine) {
  1635. memdelete(engine);
  1636. }
  1637. if (packed_data) {
  1638. memdelete(packed_data);
  1639. }
  1640. if (file_access_network_client) {
  1641. memdelete(file_access_network_client);
  1642. }
  1643. unregister_core_driver_types();
  1644. unregister_core_extensions();
  1645. unregister_core_types();
  1646. OS::get_singleton()->_cmdline.clear();
  1647. OS::get_singleton()->_user_args.clear();
  1648. if (message_queue) {
  1649. memdelete(message_queue);
  1650. }
  1651. OS::get_singleton()->finalize_core();
  1652. locale = String();
  1653. return exit_code;
  1654. }
  1655. Error Main::setup2(Thread::ID p_main_tid_override) {
  1656. // Print engine name and version
  1657. print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
  1658. engine->startup_benchmark_begin_measure("servers");
  1659. tsman = memnew(TextServerManager);
  1660. if (tsman) {
  1661. Ref<TextServerDummy> ts;
  1662. ts.instantiate();
  1663. tsman->add_interface(ts);
  1664. }
  1665. physics_server_3d_manager = memnew(PhysicsServer3DManager);
  1666. physics_server_2d_manager = memnew(PhysicsServer2DManager);
  1667. register_server_types();
  1668. initialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  1669. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  1670. if (p_main_tid_override) {
  1671. Thread::main_thread_id = p_main_tid_override;
  1672. }
  1673. #ifdef TOOLS_ENABLED
  1674. if (editor || project_manager || cmdline_tool) {
  1675. EditorPaths::create();
  1676. if (found_project && EditorPaths::get_singleton()->is_self_contained()) {
  1677. if (ProjectSettings::get_singleton()->get_resource_path() == OS::get_singleton()->get_executable_path().get_base_dir()) {
  1678. 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.");
  1679. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  1680. return FAILED;
  1681. }
  1682. }
  1683. }
  1684. #endif
  1685. /* Initialize Input */
  1686. input = memnew(Input);
  1687. /* Initialize Display Server */
  1688. {
  1689. String display_driver = DisplayServer::get_create_function_name(display_driver_idx);
  1690. Vector2i *window_position = nullptr;
  1691. Vector2i position = init_custom_pos;
  1692. if (init_use_custom_pos) {
  1693. window_position = &position;
  1694. }
  1695. Color boot_bg_color = GLOBAL_DEF_BASIC("application/boot_splash/bg_color", boot_splash_bg_color);
  1696. DisplayServer::set_early_window_clear_color_override(true, boot_bg_color);
  1697. // rendering_driver now held in static global String in main and initialized in setup()
  1698. Error err;
  1699. display_server = DisplayServer::create(display_driver_idx, rendering_driver, window_mode, window_vsync_mode, window_flags, window_position, window_size, init_screen, err);
  1700. if (err != OK || display_server == nullptr) {
  1701. // We can't use this display server, try other ones as fallback.
  1702. // Skip headless (always last registered) because that's not what users
  1703. // would expect if they didn't request it explicitly.
  1704. for (int i = 0; i < DisplayServer::get_create_function_count() - 1; i++) {
  1705. if (i == display_driver_idx) {
  1706. continue; // Don't try the same twice.
  1707. }
  1708. display_server = DisplayServer::create(i, rendering_driver, window_mode, window_vsync_mode, window_flags, window_position, window_size, init_screen, err);
  1709. if (err == OK && display_server != nullptr) {
  1710. break;
  1711. }
  1712. }
  1713. }
  1714. if (err != OK || display_server == nullptr) {
  1715. ERR_PRINT("Unable to create DisplayServer, all display drivers failed.");
  1716. return err;
  1717. }
  1718. }
  1719. if (display_server->has_feature(DisplayServer::FEATURE_ORIENTATION)) {
  1720. display_server->screen_set_orientation(window_orientation);
  1721. }
  1722. if (GLOBAL_GET("debug/settings/stdout/print_fps") || print_fps) {
  1723. // Print requested V-Sync mode at startup to diagnose the printed FPS not going above the monitor refresh rate.
  1724. switch (window_vsync_mode) {
  1725. case DisplayServer::VSyncMode::VSYNC_DISABLED:
  1726. print_line("Requested V-Sync mode: Disabled");
  1727. break;
  1728. case DisplayServer::VSyncMode::VSYNC_ENABLED:
  1729. print_line("Requested V-Sync mode: Enabled - FPS will likely be capped to the monitor refresh rate.");
  1730. break;
  1731. case DisplayServer::VSyncMode::VSYNC_ADAPTIVE:
  1732. print_line("Requested V-Sync mode: Adaptive");
  1733. break;
  1734. case DisplayServer::VSyncMode::VSYNC_MAILBOX:
  1735. print_line("Requested V-Sync mode: Mailbox");
  1736. break;
  1737. }
  1738. }
  1739. /* Initialize Pen Tablet Driver */
  1740. {
  1741. GLOBAL_DEF_RST_NOVAL("input_devices/pen_tablet/driver", "");
  1742. GLOBAL_DEF_RST_NOVAL(PropertyInfo(Variant::STRING, "input_devices/pen_tablet/driver.windows", PROPERTY_HINT_ENUM, "wintab,winink"), "");
  1743. }
  1744. if (tablet_driver.is_empty()) { // specified in project.godot
  1745. tablet_driver = GLOBAL_GET("input_devices/pen_tablet/driver");
  1746. if (tablet_driver.is_empty()) {
  1747. tablet_driver = DisplayServer::get_singleton()->tablet_get_driver_name(0);
  1748. }
  1749. }
  1750. for (int i = 0; i < DisplayServer::get_singleton()->tablet_get_driver_count(); i++) {
  1751. if (tablet_driver == DisplayServer::get_singleton()->tablet_get_driver_name(i)) {
  1752. DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(i));
  1753. break;
  1754. }
  1755. }
  1756. if (DisplayServer::get_singleton()->tablet_get_current_driver().is_empty()) {
  1757. DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(0));
  1758. }
  1759. print_verbose("Using \"" + tablet_driver + "\" pen tablet driver...");
  1760. /* Initialize Rendering Server */
  1761. rendering_server = memnew(RenderingServerDefault(OS::get_singleton()->get_render_thread_mode() == OS::RENDER_SEPARATE_THREAD));
  1762. rendering_server->init();
  1763. //rendering_server->call_set_use_vsync(OS::get_singleton()->_use_vsync);
  1764. rendering_server->set_render_loop_enabled(!disable_render_loop);
  1765. if (profile_gpu || (!editor && bool(GLOBAL_GET("debug/settings/stdout/print_gpu_profile")))) {
  1766. rendering_server->set_print_gpu_profile(true);
  1767. }
  1768. if (Engine::get_singleton()->get_write_movie_path() != String()) {
  1769. movie_writer = MovieWriter::find_writer_for_file(Engine::get_singleton()->get_write_movie_path());
  1770. if (movie_writer == nullptr) {
  1771. ERR_PRINT("Can't find movie writer for file type, aborting: " + Engine::get_singleton()->get_write_movie_path());
  1772. Engine::get_singleton()->set_write_movie_path(String());
  1773. }
  1774. }
  1775. #ifdef UNIX_ENABLED
  1776. // Print warning after initializing the renderer but before initializing audio.
  1777. if (OS::get_singleton()->get_environment("USER") == "root" && !OS::get_singleton()->has_environment("GODOT_SILENCE_ROOT_WARNING")) {
  1778. 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.");
  1779. }
  1780. #endif
  1781. OS::get_singleton()->initialize_joypads();
  1782. /* Initialize Audio Driver */
  1783. AudioDriverManager::initialize(audio_driver_idx);
  1784. print_line(" "); //add a blank line for readability
  1785. // right moment to create and initialize the audio server
  1786. audio_server = memnew(AudioServer);
  1787. audio_server->init();
  1788. // also init our xr_server from here
  1789. xr_server = memnew(XRServer);
  1790. register_core_singletons();
  1791. MAIN_PRINT("Main: Setup Logo");
  1792. #if !defined(TOOLS_ENABLED) && (defined(WEB_ENABLED) || defined(ANDROID_ENABLED))
  1793. bool show_logo = false;
  1794. #else
  1795. bool show_logo = true;
  1796. #endif
  1797. if (init_windowed) {
  1798. //do none..
  1799. } else if (init_maximized) {
  1800. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_MAXIMIZED);
  1801. } else if (init_fullscreen) {
  1802. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_FULLSCREEN);
  1803. }
  1804. if (init_always_on_top) {
  1805. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP, true);
  1806. }
  1807. MAIN_PRINT("Main: Load Boot Image");
  1808. Color clear = GLOBAL_DEF_BASIC("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3));
  1809. RenderingServer::get_singleton()->set_default_clear_color(clear);
  1810. if (show_logo) { //boot logo!
  1811. const bool boot_logo_image = GLOBAL_DEF_BASIC("application/boot_splash/show_image", true);
  1812. const String boot_logo_path = String(GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/boot_splash/image", PROPERTY_HINT_FILE, "*.png"), String())).strip_edges();
  1813. const bool boot_logo_scale = GLOBAL_DEF_BASIC("application/boot_splash/fullsize", true);
  1814. const bool boot_logo_filter = GLOBAL_DEF_BASIC("application/boot_splash/use_filter", true);
  1815. Ref<Image> boot_logo;
  1816. if (boot_logo_image) {
  1817. if (!boot_logo_path.is_empty()) {
  1818. boot_logo.instantiate();
  1819. Error load_err = ImageLoader::load_image(boot_logo_path, boot_logo);
  1820. if (load_err) {
  1821. ERR_PRINT("Non-existing or invalid boot splash at '" + boot_logo_path + "'. Loading default splash.");
  1822. }
  1823. }
  1824. } else {
  1825. // Create a 1×1 transparent image. This will effectively hide the splash image.
  1826. boot_logo.instantiate();
  1827. boot_logo->initialize_data(1, 1, false, Image::FORMAT_RGBA8);
  1828. boot_logo->set_pixel(0, 0, Color(0, 0, 0, 0));
  1829. }
  1830. Color boot_bg_color = GLOBAL_GET("application/boot_splash/bg_color");
  1831. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  1832. boot_bg_color =
  1833. GLOBAL_DEF_BASIC("application/boot_splash/bg_color",
  1834. (editor || project_manager) ? boot_splash_editor_bg_color : boot_splash_bg_color);
  1835. #endif
  1836. if (boot_logo.is_valid()) {
  1837. RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, boot_logo_scale,
  1838. boot_logo_filter);
  1839. } else {
  1840. #ifndef NO_DEFAULT_BOOT_LOGO
  1841. MAIN_PRINT("Main: Create bootsplash");
  1842. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  1843. Ref<Image> splash = (editor || project_manager) ? memnew(Image(boot_splash_editor_png)) : memnew(Image(boot_splash_png));
  1844. #else
  1845. Ref<Image> splash = memnew(Image(boot_splash_png));
  1846. #endif
  1847. MAIN_PRINT("Main: ClearColor");
  1848. RenderingServer::get_singleton()->set_default_clear_color(boot_bg_color);
  1849. MAIN_PRINT("Main: Image");
  1850. RenderingServer::get_singleton()->set_boot_image(splash, boot_bg_color, false);
  1851. #endif
  1852. }
  1853. #if defined(TOOLS_ENABLED) && defined(MACOS_ENABLED)
  1854. if (OS::get_singleton()->get_bundle_icon_path().is_empty()) {
  1855. Ref<Image> icon = memnew(Image(app_icon_png));
  1856. DisplayServer::get_singleton()->set_icon(icon);
  1857. }
  1858. #endif
  1859. }
  1860. DisplayServer::set_early_window_clear_color_override(false);
  1861. MAIN_PRINT("Main: DCC");
  1862. RenderingServer::get_singleton()->set_default_clear_color(
  1863. GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
  1864. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "application/config/icon", PROPERTY_HINT_FILE, "*.png,*.webp,*.svg"), String());
  1865. GLOBAL_DEF(PropertyInfo(Variant::STRING, "application/config/macos_native_icon", PROPERTY_HINT_FILE, "*.icns"), String());
  1866. GLOBAL_DEF(PropertyInfo(Variant::STRING, "application/config/windows_native_icon", PROPERTY_HINT_FILE, "*.ico"), String());
  1867. Input *id = Input::get_singleton();
  1868. if (id) {
  1869. agile_input_event_flushing = GLOBAL_DEF("input_devices/buffering/agile_event_flushing", false);
  1870. if (bool(GLOBAL_DEF_BASIC("input_devices/pointing/emulate_touch_from_mouse", false)) &&
  1871. !(editor || project_manager)) {
  1872. if (!DisplayServer::get_singleton()->is_touchscreen_available()) {
  1873. //only if no touchscreen ui hint, set emulation
  1874. id->set_emulate_touch_from_mouse(true);
  1875. }
  1876. }
  1877. id->set_emulate_mouse_from_touch(bool(GLOBAL_DEF_BASIC("input_devices/pointing/emulate_mouse_from_touch", true)));
  1878. }
  1879. MAIN_PRINT("Main: Load Translations and Remaps");
  1880. translation_server->setup(); //register translations, load them, etc.
  1881. if (!locale.is_empty()) {
  1882. translation_server->set_locale(locale);
  1883. }
  1884. translation_server->load_translations();
  1885. ResourceLoader::load_translation_remaps(); //load remaps for resources
  1886. ResourceLoader::load_path_remaps();
  1887. MAIN_PRINT("Main: Load TextServer");
  1888. /* Enum text drivers */
  1889. GLOBAL_DEF_RST("internationalization/rendering/text_driver", "");
  1890. String text_driver_options;
  1891. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  1892. const String driver_name = TextServerManager::get_singleton()->get_interface(i)->get_name();
  1893. if (driver_name == "Dummy") {
  1894. // Dummy text driver cannot draw any text, making the editor unusable if selected.
  1895. continue;
  1896. }
  1897. if (!text_driver_options.is_empty() && text_driver_options.find(",") == -1) {
  1898. // Not the first option; add a comma before it as a separator for the property hint.
  1899. text_driver_options += ",";
  1900. }
  1901. text_driver_options += driver_name;
  1902. }
  1903. ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, "internationalization/rendering/text_driver", PROPERTY_HINT_ENUM, text_driver_options));
  1904. /* Determine text driver */
  1905. if (text_driver.is_empty()) {
  1906. text_driver = GLOBAL_GET("internationalization/rendering/text_driver");
  1907. }
  1908. if (!text_driver.is_empty()) {
  1909. /* Load user selected text server. */
  1910. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  1911. if (TextServerManager::get_singleton()->get_interface(i)->get_name() == text_driver) {
  1912. text_driver_idx = i;
  1913. break;
  1914. }
  1915. }
  1916. }
  1917. if (text_driver_idx < 0) {
  1918. /* If not selected, use one with the most features available. */
  1919. int max_features = 0;
  1920. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  1921. uint32_t features = TextServerManager::get_singleton()->get_interface(i)->get_features();
  1922. int feature_number = 0;
  1923. while (features) {
  1924. feature_number += features & 1;
  1925. features >>= 1;
  1926. }
  1927. if (feature_number >= max_features) {
  1928. max_features = feature_number;
  1929. text_driver_idx = i;
  1930. }
  1931. }
  1932. }
  1933. if (text_driver_idx >= 0) {
  1934. Ref<TextServer> ts = TextServerManager::get_singleton()->get_interface(text_driver_idx);
  1935. TextServerManager::get_singleton()->set_primary_interface(ts);
  1936. if (ts->has_feature(TextServer::FEATURE_USE_SUPPORT_DATA)) {
  1937. ts->load_support_data("res://" + ts->get_support_data_filename());
  1938. }
  1939. } else {
  1940. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "TextServer: Unable to create TextServer interface.");
  1941. }
  1942. engine->startup_benchmark_end_measure(); // servers
  1943. MAIN_PRINT("Main: Load Scene Types");
  1944. engine->startup_benchmark_begin_measure("scene");
  1945. register_scene_types();
  1946. register_driver_types();
  1947. initialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  1948. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  1949. #ifdef TOOLS_ENABLED
  1950. ClassDB::set_current_api(ClassDB::API_EDITOR);
  1951. register_editor_types();
  1952. initialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  1953. GDExtensionManager::get_singleton()->initialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  1954. ClassDB::set_current_api(ClassDB::API_CORE);
  1955. #endif
  1956. MAIN_PRINT("Main: Load Modules");
  1957. register_platform_apis();
  1958. // Theme needs modules to be initialized so that sub-resources can be loaded.
  1959. // Default theme is initialized later, after ScriptServer is ready.
  1960. initialize_theme_db();
  1961. register_scene_singletons();
  1962. GLOBAL_DEF_BASIC(PropertyInfo(Variant::STRING, "display/mouse_cursor/custom_image", PROPERTY_HINT_FILE, "*.png,*.webp"), String());
  1963. GLOBAL_DEF_BASIC("display/mouse_cursor/custom_image_hotspot", Vector2());
  1964. GLOBAL_DEF_BASIC("display/mouse_cursor/tooltip_position_offset", Point2(10, 10));
  1965. if (String(GLOBAL_GET("display/mouse_cursor/custom_image")) != String()) {
  1966. Ref<Texture2D> cursor = ResourceLoader::load(
  1967. GLOBAL_GET("display/mouse_cursor/custom_image"));
  1968. if (cursor.is_valid()) {
  1969. Vector2 hotspot = GLOBAL_GET("display/mouse_cursor/custom_image_hotspot");
  1970. Input::get_singleton()->set_custom_mouse_cursor(cursor, Input::CURSOR_ARROW, hotspot);
  1971. }
  1972. }
  1973. camera_server = CameraServer::create();
  1974. MAIN_PRINT("Main: Load Physics");
  1975. initialize_physics();
  1976. initialize_navigation_server();
  1977. register_server_singletons();
  1978. // This loads global classes, so it must happen before custom loaders and savers are registered
  1979. ScriptServer::init_languages();
  1980. theme_db->initialize_theme();
  1981. audio_server->load_default_bus_layout();
  1982. #if defined(MODULE_MONO_ENABLED) && defined(TOOLS_ENABLED)
  1983. // Hacky to have it here, but we don't have good facility yet to let modules
  1984. // register command line options to call at the right time. This needs to happen
  1985. // after init'ing the ScriptServer, but also after init'ing the ThemeDB,
  1986. // for the C# docs generation in the bindings.
  1987. List<String> cmdline_args = OS::get_singleton()->get_cmdline_args();
  1988. BindingsGenerator::handle_cmdline_args(cmdline_args);
  1989. #endif
  1990. if (use_debug_profiler && EngineDebugger::is_active()) {
  1991. // Start the "scripts" profiler, used in local debugging.
  1992. // We could add more, and make the CLI arg require a comma-separated list of profilers.
  1993. EngineDebugger::get_singleton()->profiler_enable("scripts", true);
  1994. }
  1995. if (!project_manager) {
  1996. // If not running the project manager, and now that the engine is
  1997. // able to load resources, load the global shader variables.
  1998. // If running on editor, don't load the textures because the editor
  1999. // may want to import them first. Editor will reload those later.
  2000. rendering_server->global_shader_parameters_load_settings(!editor);
  2001. }
  2002. _start_success = true;
  2003. ClassDB::set_current_api(ClassDB::API_NONE); //no more APIs are registered at this point
  2004. print_verbose("CORE API HASH: " + uitos(ClassDB::get_api_hash(ClassDB::API_CORE)));
  2005. print_verbose("EDITOR API HASH: " + uitos(ClassDB::get_api_hash(ClassDB::API_EDITOR)));
  2006. MAIN_PRINT("Main: Done");
  2007. engine->startup_benchmark_end_measure(); // scene
  2008. return OK;
  2009. }
  2010. String Main::get_rendering_driver_name() {
  2011. return rendering_driver;
  2012. }
  2013. // everything the main loop needs to know about frame timings
  2014. static MainTimerSync main_timer_sync;
  2015. bool Main::start() {
  2016. ERR_FAIL_COND_V(!_start_success, false);
  2017. bool hasicon = false;
  2018. String positional_arg;
  2019. String game_path;
  2020. String script;
  2021. bool check_only = false;
  2022. #ifdef TOOLS_ENABLED
  2023. String doc_tool_path;
  2024. bool doc_base = true;
  2025. String _export_preset;
  2026. bool export_debug = false;
  2027. bool export_pack_only = false;
  2028. #ifndef DISABLE_DEPRECATED
  2029. bool converting_project = false;
  2030. bool validating_converting_project = false;
  2031. #endif // DISABLE_DEPRECATED
  2032. #endif // TOOLS_ENABLED
  2033. main_timer_sync.init(OS::get_singleton()->get_ticks_usec());
  2034. List<String> args = OS::get_singleton()->get_cmdline_args();
  2035. for (int i = 0; i < args.size(); i++) {
  2036. // First check parameters that do not have an argument to the right.
  2037. // Doctest Unit Testing Handler
  2038. // Designed to override and pass arguments to the unit test handler.
  2039. if (args[i] == "--check-only") {
  2040. check_only = true;
  2041. #ifdef TOOLS_ENABLED
  2042. } else if (args[i] == "--no-docbase") {
  2043. doc_base = false;
  2044. #ifndef DISABLE_DEPRECATED
  2045. } else if (args[i] == "--convert-3to4") {
  2046. converting_project = true;
  2047. } else if (args[i] == "--validate-conversion-3to4") {
  2048. validating_converting_project = true;
  2049. #endif // DISABLE_DEPRECATED
  2050. } else if (args[i] == "-e" || args[i] == "--editor") {
  2051. editor = true;
  2052. } else if (args[i] == "-p" || args[i] == "--project-manager") {
  2053. project_manager = true;
  2054. #endif // TOOLS_ENABLED
  2055. } else if (args[i].length() && args[i][0] != '-' && positional_arg.is_empty()) {
  2056. positional_arg = args[i];
  2057. if (args[i].ends_with(".scn") ||
  2058. args[i].ends_with(".tscn") ||
  2059. args[i].ends_with(".escn") ||
  2060. args[i].ends_with(".res") ||
  2061. args[i].ends_with(".tres")) {
  2062. // Only consider the positional argument to be a scene path if it ends with
  2063. // a file extension associated with Godot scenes. This makes it possible
  2064. // for projects to parse command-line arguments for custom CLI arguments
  2065. // or other file extensions without trouble. This can be used to implement
  2066. // "drag-and-drop onto executable" logic, which can prove helpful
  2067. // for non-game applications.
  2068. game_path = args[i];
  2069. }
  2070. }
  2071. // Then parameters that have an argument to the right.
  2072. else if (i < (args.size() - 1)) {
  2073. bool parsed_pair = true;
  2074. if (args[i] == "-s" || args[i] == "--script") {
  2075. script = args[i + 1];
  2076. #ifdef TOOLS_ENABLED
  2077. } else if (args[i] == "--doctool") {
  2078. doc_tool_path = args[i + 1];
  2079. if (doc_tool_path.begins_with("-")) {
  2080. // Assuming other command line arg, so default to cwd.
  2081. doc_tool_path = ".";
  2082. parsed_pair = false;
  2083. }
  2084. } else if (args[i] == "--export-release") {
  2085. editor = true; //needs editor
  2086. _export_preset = args[i + 1];
  2087. } else if (args[i] == "--export-debug") {
  2088. editor = true; //needs editor
  2089. _export_preset = args[i + 1];
  2090. export_debug = true;
  2091. } else if (args[i] == "--export-pack") {
  2092. editor = true;
  2093. _export_preset = args[i + 1];
  2094. export_pack_only = true;
  2095. #endif
  2096. } else {
  2097. // The parameter does not match anything known, don't skip the next argument
  2098. parsed_pair = false;
  2099. }
  2100. if (parsed_pair) {
  2101. i++;
  2102. }
  2103. }
  2104. #ifdef TOOLS_ENABLED
  2105. // Handle case where no path is given to --doctool.
  2106. else if (args[i] == "--doctool") {
  2107. doc_tool_path = ".";
  2108. }
  2109. #endif
  2110. }
  2111. 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);
  2112. #ifdef TOOLS_ENABLED
  2113. if (!doc_tool_path.is_empty()) {
  2114. // Needed to instance editor-only classes for their default values
  2115. Engine::get_singleton()->set_editor_hint(true);
  2116. // Translate the class reference only when `-l LOCALE` parameter is given.
  2117. if (!locale.is_empty() && locale != "en") {
  2118. load_doc_translations(locale);
  2119. }
  2120. {
  2121. Ref<DirAccess> da = DirAccess::open(doc_tool_path);
  2122. ERR_FAIL_COND_V_MSG(da.is_null(), false, "Argument supplied to --doctool must be a valid directory path.");
  2123. }
  2124. #ifndef MODULE_MONO_ENABLED
  2125. // Hack to define .NET-specific project settings even on non-.NET builds,
  2126. // so that we don't lose their descriptions and default values in DocTools.
  2127. // Default values should be synced with mono_gd/gd_mono.cpp.
  2128. GLOBAL_DEF("dotnet/project/assembly_name", "");
  2129. GLOBAL_DEF("dotnet/project/solution_directory", "");
  2130. #endif
  2131. Error err;
  2132. DocTools doc;
  2133. doc.generate(doc_base);
  2134. DocTools docsrc;
  2135. HashMap<String, String> doc_data_classes;
  2136. HashSet<String> checked_paths;
  2137. print_line("Loading docs...");
  2138. for (int i = 0; i < _doc_data_class_path_count; i++) {
  2139. // Custom modules are always located by absolute path.
  2140. String path = _doc_data_class_paths[i].path;
  2141. if (path.is_relative_path()) {
  2142. path = doc_tool_path.path_join(path);
  2143. }
  2144. String name = _doc_data_class_paths[i].name;
  2145. doc_data_classes[name] = path;
  2146. if (!checked_paths.has(path)) {
  2147. checked_paths.insert(path);
  2148. // Create the module documentation directory if it doesn't exist
  2149. Ref<DirAccess> da = DirAccess::create_for_path(path);
  2150. err = da->make_dir_recursive(path);
  2151. ERR_FAIL_COND_V_MSG(err != OK, false, "Error: Can't create directory: " + path + ": " + itos(err));
  2152. print_line("Loading docs from: " + path);
  2153. err = docsrc.load_classes(path);
  2154. ERR_FAIL_COND_V_MSG(err != OK, false, "Error loading docs from: " + path + ": " + itos(err));
  2155. }
  2156. }
  2157. String index_path = doc_tool_path.path_join("doc/classes");
  2158. // Create the main documentation directory if it doesn't exist
  2159. Ref<DirAccess> da = DirAccess::create_for_path(index_path);
  2160. err = da->make_dir_recursive(index_path);
  2161. ERR_FAIL_COND_V_MSG(err != OK, false, "Error: Can't create index directory: " + index_path + ": " + itos(err));
  2162. print_line("Loading classes from: " + index_path);
  2163. err = docsrc.load_classes(index_path);
  2164. ERR_FAIL_COND_V_MSG(err != OK, false, "Error loading classes from: " + index_path + ": " + itos(err));
  2165. checked_paths.insert(index_path);
  2166. print_line("Merging docs...");
  2167. doc.merge_from(docsrc);
  2168. for (const String &E : checked_paths) {
  2169. print_line("Erasing old docs at: " + E);
  2170. err = DocTools::erase_classes(E);
  2171. ERR_FAIL_COND_V_MSG(err != OK, false, "Error erasing old docs at: " + E + ": " + itos(err));
  2172. }
  2173. print_line("Generating new docs...");
  2174. err = doc.save_classes(index_path, doc_data_classes);
  2175. ERR_FAIL_COND_V_MSG(err != OK, false, "Error saving new docs:" + itos(err));
  2176. OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
  2177. return false;
  2178. }
  2179. if (dump_gdextension_interface) {
  2180. GDExtensionInterfaceDump::generate_gdextension_interface_file("gdextension_interface.h");
  2181. }
  2182. if (dump_extension_api) {
  2183. GDExtensionAPIDump::generate_extension_json_file("extension_api.json");
  2184. }
  2185. if (dump_gdextension_interface || dump_extension_api) {
  2186. OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
  2187. return false;
  2188. }
  2189. #ifndef DISABLE_DEPRECATED
  2190. if (converting_project) {
  2191. int ret = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).convert();
  2192. if (ret) {
  2193. OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
  2194. }
  2195. return false;
  2196. }
  2197. if (validating_converting_project) {
  2198. bool ret = ProjectConverter3To4(converter_max_kb_file, converter_max_line_length).validate_conversion();
  2199. if (ret) {
  2200. OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
  2201. }
  2202. return false;
  2203. }
  2204. #endif // DISABLE_DEPRECATED
  2205. #endif // TOOLS_ENABLED
  2206. if (script.is_empty() && game_path.is_empty() && String(GLOBAL_GET("application/run/main_scene")) != "") {
  2207. game_path = GLOBAL_GET("application/run/main_scene");
  2208. }
  2209. #ifdef TOOLS_ENABLED
  2210. if (!editor && !project_manager && !cmdline_tool && script.is_empty() && game_path.is_empty()) {
  2211. // If we end up here, it means we didn't manage to detect what we want to run.
  2212. // Let's throw an error gently. The code leading to this is pretty brittle so
  2213. // this might end up triggered by valid usage, in which case we'll have to
  2214. // fine-tune further.
  2215. OS::get_singleton()->alert("Couldn't detect whether to run the editor, the project manager or a specific project. Aborting.");
  2216. ERR_FAIL_V_MSG(false, "Couldn't detect whether to run the editor, the project manager or a specific project. Aborting.");
  2217. }
  2218. #endif
  2219. MainLoop *main_loop = nullptr;
  2220. if (editor) {
  2221. main_loop = memnew(SceneTree);
  2222. }
  2223. String main_loop_type = GLOBAL_GET("application/run/main_loop_type");
  2224. if (!script.is_empty()) {
  2225. Ref<Script> script_res = ResourceLoader::load(script);
  2226. ERR_FAIL_COND_V_MSG(script_res.is_null(), false, "Can't load script: " + script);
  2227. if (check_only) {
  2228. if (!script_res->is_valid()) {
  2229. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  2230. } else {
  2231. OS::get_singleton()->set_exit_code(EXIT_SUCCESS);
  2232. }
  2233. return false;
  2234. }
  2235. if (script_res->can_instantiate()) {
  2236. StringName instance_type = script_res->get_instance_base_type();
  2237. Object *obj = ClassDB::instantiate(instance_type);
  2238. MainLoop *script_loop = Object::cast_to<MainLoop>(obj);
  2239. if (!script_loop) {
  2240. if (obj) {
  2241. memdelete(obj);
  2242. }
  2243. OS::get_singleton()->alert(vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.", script));
  2244. ERR_FAIL_V_MSG(false, vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.", script));
  2245. }
  2246. script_loop->set_initialize_script(script_res);
  2247. main_loop = script_loop;
  2248. } else {
  2249. return false;
  2250. }
  2251. } else { // Not based on script path.
  2252. if (!editor && !ClassDB::class_exists(main_loop_type) && ScriptServer::is_global_class(main_loop_type)) {
  2253. String script_path = ScriptServer::get_global_class_path(main_loop_type);
  2254. Ref<Script> script_res = ResourceLoader::load(script_path);
  2255. if (script_res.is_null()) {
  2256. OS::get_singleton()->alert("Error: Could not load MainLoop script type: " + main_loop_type);
  2257. ERR_FAIL_V_MSG(false, vformat("Could not load global class %s.", main_loop_type));
  2258. }
  2259. StringName script_base = script_res->get_instance_base_type();
  2260. Object *obj = ClassDB::instantiate(script_base);
  2261. MainLoop *script_loop = Object::cast_to<MainLoop>(obj);
  2262. if (!script_loop) {
  2263. if (obj) {
  2264. memdelete(obj);
  2265. }
  2266. OS::get_singleton()->alert("Error: Invalid MainLoop script base type: " + script_base);
  2267. ERR_FAIL_V_MSG(false, vformat("The global class %s does not inherit from SceneTree or MainLoop.", main_loop_type));
  2268. }
  2269. script_loop->set_initialize_script(script_res);
  2270. main_loop = script_loop;
  2271. }
  2272. }
  2273. if (!main_loop && main_loop_type.is_empty()) {
  2274. main_loop_type = "SceneTree";
  2275. }
  2276. if (!main_loop) {
  2277. if (!ClassDB::class_exists(main_loop_type)) {
  2278. OS::get_singleton()->alert("Error: MainLoop type doesn't exist: " + main_loop_type);
  2279. return false;
  2280. } else {
  2281. Object *ml = ClassDB::instantiate(main_loop_type);
  2282. ERR_FAIL_COND_V_MSG(!ml, false, "Can't instance MainLoop type.");
  2283. main_loop = Object::cast_to<MainLoop>(ml);
  2284. if (!main_loop) {
  2285. memdelete(ml);
  2286. ERR_FAIL_V_MSG(false, "Invalid MainLoop type.");
  2287. }
  2288. }
  2289. }
  2290. SceneTree *sml = Object::cast_to<SceneTree>(main_loop);
  2291. if (sml) {
  2292. #ifdef DEBUG_ENABLED
  2293. if (debug_collisions) {
  2294. sml->set_debug_collisions_hint(true);
  2295. }
  2296. if (debug_paths) {
  2297. sml->set_debug_paths_hint(true);
  2298. }
  2299. if (debug_navigation) {
  2300. sml->set_debug_navigation_hint(true);
  2301. NavigationServer3D::get_singleton()->set_active(true);
  2302. NavigationServer3D::get_singleton()->set_debug_enabled(true);
  2303. }
  2304. #endif
  2305. bool embed_subwindows = GLOBAL_GET("display/window/subwindows/embed_subwindows");
  2306. if (single_window || (!project_manager && !editor && embed_subwindows) || !DisplayServer::get_singleton()->has_feature(DisplayServer::Feature::FEATURE_SUBWINDOWS)) {
  2307. sml->get_root()->set_embedding_subwindows(true);
  2308. }
  2309. ResourceLoader::add_custom_loaders();
  2310. ResourceSaver::add_custom_savers();
  2311. if (!project_manager && !editor) { // game
  2312. if (!game_path.is_empty() || !script.is_empty()) {
  2313. //autoload
  2314. Engine::get_singleton()->startup_benchmark_begin_measure("load_autoloads");
  2315. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  2316. //first pass, add the constants so they exist before any script is loaded
  2317. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  2318. const ProjectSettings::AutoloadInfo &info = E.value;
  2319. if (info.is_singleton) {
  2320. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2321. ScriptServer::get_language(i)->add_global_constant(info.name, Variant());
  2322. }
  2323. }
  2324. }
  2325. //second pass, load into global constants
  2326. List<Node *> to_add;
  2327. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  2328. const ProjectSettings::AutoloadInfo &info = E.value;
  2329. Node *n = nullptr;
  2330. if (ResourceLoader::get_resource_type(info.path) == "PackedScene") {
  2331. // Cache the scene reference before loading it (for cyclic references)
  2332. Ref<PackedScene> scn;
  2333. scn.instantiate();
  2334. scn->set_path(info.path);
  2335. scn->reload_from_file();
  2336. ERR_CONTINUE_MSG(!scn.is_valid(), vformat("Can't autoload: %s.", info.path));
  2337. if (scn.is_valid()) {
  2338. n = scn->instantiate();
  2339. }
  2340. } else {
  2341. Ref<Resource> res = ResourceLoader::load(info.path);
  2342. ERR_CONTINUE_MSG(res.is_null(), vformat("Can't autoload: %s.", info.path));
  2343. Ref<Script> script_res = res;
  2344. if (script_res.is_valid()) {
  2345. StringName ibt = script_res->get_instance_base_type();
  2346. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  2347. ERR_CONTINUE_MSG(!valid_type, vformat("Script does not inherit from Node: %s.", info.path));
  2348. Object *obj = ClassDB::instantiate(ibt);
  2349. ERR_CONTINUE_MSG(!obj, vformat("Cannot instance script for autoload, expected 'Node' inheritance, got: %s."));
  2350. n = Object::cast_to<Node>(obj);
  2351. n->set_script(script_res);
  2352. }
  2353. }
  2354. ERR_CONTINUE_MSG(!n, vformat("Path in autoload not a node or script: %s.", info.path));
  2355. n->set_name(info.name);
  2356. //defer so references are all valid on _ready()
  2357. to_add.push_back(n);
  2358. if (info.is_singleton) {
  2359. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2360. ScriptServer::get_language(i)->add_global_constant(info.name, n);
  2361. }
  2362. }
  2363. }
  2364. for (Node *E : to_add) {
  2365. sml->get_root()->add_child(E);
  2366. }
  2367. Engine::get_singleton()->startup_benchmark_end_measure(); // load autoloads
  2368. }
  2369. }
  2370. #ifdef TOOLS_ENABLED
  2371. EditorNode *editor_node = nullptr;
  2372. if (editor) {
  2373. Engine::get_singleton()->startup_benchmark_begin_measure("editor");
  2374. editor_node = memnew(EditorNode);
  2375. sml->get_root()->add_child(editor_node);
  2376. if (!_export_preset.is_empty()) {
  2377. editor_node->export_preset(_export_preset, positional_arg, export_debug, export_pack_only);
  2378. game_path = ""; // Do not load anything.
  2379. }
  2380. Engine::get_singleton()->startup_benchmark_end_measure();
  2381. editor_node->set_use_startup_benchmark(use_startup_benchmark, startup_benchmark_file);
  2382. // Editor takes over
  2383. use_startup_benchmark = false;
  2384. startup_benchmark_file = String();
  2385. }
  2386. #endif
  2387. sml->set_auto_accept_quit(GLOBAL_GET("application/config/auto_accept_quit"));
  2388. sml->set_quit_on_go_back(GLOBAL_GET("application/config/quit_on_go_back"));
  2389. if (!editor && !project_manager) {
  2390. //standard helpers that can be changed from main config
  2391. String stretch_mode = GLOBAL_GET("display/window/stretch/mode");
  2392. String stretch_aspect = GLOBAL_GET("display/window/stretch/aspect");
  2393. Size2i stretch_size = Size2i(GLOBAL_GET("display/window/size/viewport_width"),
  2394. GLOBAL_GET("display/window/size/viewport_height"));
  2395. real_t stretch_scale = GLOBAL_GET("display/window/stretch/scale");
  2396. Window::ContentScaleMode cs_sm = Window::CONTENT_SCALE_MODE_DISABLED;
  2397. if (stretch_mode == "canvas_items") {
  2398. cs_sm = Window::CONTENT_SCALE_MODE_CANVAS_ITEMS;
  2399. } else if (stretch_mode == "viewport") {
  2400. cs_sm = Window::CONTENT_SCALE_MODE_VIEWPORT;
  2401. }
  2402. Window::ContentScaleAspect cs_aspect = Window::CONTENT_SCALE_ASPECT_IGNORE;
  2403. if (stretch_aspect == "keep") {
  2404. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP;
  2405. } else if (stretch_aspect == "keep_width") {
  2406. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_WIDTH;
  2407. } else if (stretch_aspect == "keep_height") {
  2408. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_HEIGHT;
  2409. } else if (stretch_aspect == "expand") {
  2410. cs_aspect = Window::CONTENT_SCALE_ASPECT_EXPAND;
  2411. }
  2412. sml->get_root()->set_content_scale_mode(cs_sm);
  2413. sml->get_root()->set_content_scale_aspect(cs_aspect);
  2414. sml->get_root()->set_content_scale_size(stretch_size);
  2415. sml->get_root()->set_content_scale_factor(stretch_scale);
  2416. sml->set_auto_accept_quit(GLOBAL_GET("application/config/auto_accept_quit"));
  2417. sml->set_quit_on_go_back(GLOBAL_GET("application/config/quit_on_go_back"));
  2418. String appname = GLOBAL_GET("application/config/name");
  2419. appname = TranslationServer::get_singleton()->translate(appname);
  2420. #ifdef DEBUG_ENABLED
  2421. // Append a suffix to the window title to denote that the project is running
  2422. // from a debug build (including the editor). Since this results in lower performance,
  2423. // this should be clearly presented to the user.
  2424. DisplayServer::get_singleton()->window_set_title(vformat("%s (DEBUG)", appname));
  2425. #else
  2426. DisplayServer::get_singleton()->window_set_title(appname);
  2427. #endif
  2428. bool snap_controls = GLOBAL_GET("gui/common/snap_controls_to_pixels");
  2429. sml->get_root()->set_snap_controls_to_pixels(snap_controls);
  2430. bool font_oversampling = GLOBAL_GET("gui/fonts/dynamic_fonts/use_oversampling");
  2431. sml->get_root()->set_use_font_oversampling(font_oversampling);
  2432. int texture_filter = GLOBAL_GET("rendering/textures/canvas_textures/default_texture_filter");
  2433. int texture_repeat = GLOBAL_GET("rendering/textures/canvas_textures/default_texture_repeat");
  2434. sml->get_root()->set_default_canvas_item_texture_filter(
  2435. Viewport::DefaultCanvasItemTextureFilter(texture_filter));
  2436. sml->get_root()->set_default_canvas_item_texture_repeat(
  2437. Viewport::DefaultCanvasItemTextureRepeat(texture_repeat));
  2438. }
  2439. #ifdef TOOLS_ENABLED
  2440. if (editor) {
  2441. bool editor_embed_subwindows = EditorSettings::get_singleton()->get_setting(
  2442. "interface/editor/single_window_mode");
  2443. if (editor_embed_subwindows) {
  2444. sml->get_root()->set_embedding_subwindows(true);
  2445. }
  2446. }
  2447. #endif
  2448. String local_game_path;
  2449. if (!game_path.is_empty() && !project_manager) {
  2450. local_game_path = game_path.replace("\\", "/");
  2451. if (!local_game_path.begins_with("res://")) {
  2452. bool absolute =
  2453. (local_game_path.size() > 1) && (local_game_path[0] == '/' || local_game_path[1] == ':');
  2454. if (!absolute) {
  2455. if (ProjectSettings::get_singleton()->is_using_datapack()) {
  2456. local_game_path = "res://" + local_game_path;
  2457. } else {
  2458. int sep = local_game_path.rfind("/");
  2459. if (sep == -1) {
  2460. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  2461. local_game_path = da->get_current_dir().path_join(local_game_path);
  2462. } else {
  2463. Ref<DirAccess> da = DirAccess::open(local_game_path.substr(0, sep));
  2464. if (da.is_valid()) {
  2465. local_game_path = da->get_current_dir().path_join(
  2466. local_game_path.substr(sep + 1, local_game_path.length()));
  2467. }
  2468. }
  2469. }
  2470. }
  2471. }
  2472. local_game_path = ProjectSettings::get_singleton()->localize_path(local_game_path);
  2473. #ifdef TOOLS_ENABLED
  2474. if (editor) {
  2475. if (game_path != String(GLOBAL_GET("application/run/main_scene")) || !editor_node->has_scenes_in_session()) {
  2476. Error serr = editor_node->load_scene(local_game_path);
  2477. if (serr != OK) {
  2478. ERR_PRINT("Failed to load scene");
  2479. }
  2480. }
  2481. DisplayServer::get_singleton()->set_context(DisplayServer::CONTEXT_EDITOR);
  2482. if (!debug_server_uri.is_empty()) {
  2483. EditorDebuggerNode::get_singleton()->start(debug_server_uri);
  2484. EditorDebuggerNode::get_singleton()->set_keep_open(true);
  2485. }
  2486. }
  2487. #endif
  2488. if (!editor) {
  2489. DisplayServer::get_singleton()->set_context(DisplayServer::CONTEXT_ENGINE);
  2490. }
  2491. }
  2492. if (!project_manager && !editor) { // game
  2493. Engine::get_singleton()->startup_benchmark_begin_measure("game_load");
  2494. // Load SSL Certificates from Project Settings (or builtin).
  2495. Crypto::load_default_certificates(GLOBAL_GET("network/tls/certificate_bundle_override"));
  2496. if (!game_path.is_empty()) {
  2497. Node *scene = nullptr;
  2498. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  2499. if (scenedata.is_valid()) {
  2500. scene = scenedata->instantiate();
  2501. }
  2502. ERR_FAIL_COND_V_MSG(!scene, false, "Failed loading scene: " + local_game_path);
  2503. sml->add_current_scene(scene);
  2504. #ifdef MACOS_ENABLED
  2505. String mac_iconpath = GLOBAL_GET("application/config/macos_native_icon");
  2506. if (!mac_iconpath.is_empty()) {
  2507. DisplayServer::get_singleton()->set_native_icon(mac_iconpath);
  2508. hasicon = true;
  2509. }
  2510. #endif
  2511. #ifdef WINDOWS_ENABLED
  2512. String win_iconpath = GLOBAL_GET("application/config/windows_native_icon");
  2513. if (!win_iconpath.is_empty()) {
  2514. DisplayServer::get_singleton()->set_native_icon(win_iconpath);
  2515. hasicon = true;
  2516. }
  2517. #endif
  2518. String iconpath = GLOBAL_GET("application/config/icon");
  2519. if ((!iconpath.is_empty()) && (!hasicon)) {
  2520. Ref<Image> icon;
  2521. icon.instantiate();
  2522. if (ImageLoader::load_image(iconpath, icon) == OK) {
  2523. DisplayServer::get_singleton()->set_icon(icon);
  2524. hasicon = true;
  2525. }
  2526. }
  2527. }
  2528. Engine::get_singleton()->startup_benchmark_end_measure(); // game_load
  2529. }
  2530. #ifdef TOOLS_ENABLED
  2531. if (project_manager) {
  2532. Engine::get_singleton()->startup_benchmark_begin_measure("project_manager");
  2533. Engine::get_singleton()->set_editor_hint(true);
  2534. ProjectManager *pmanager = memnew(ProjectManager);
  2535. ProgressDialog *progress_dialog = memnew(ProgressDialog);
  2536. pmanager->add_child(progress_dialog);
  2537. sml->get_root()->add_child(pmanager);
  2538. DisplayServer::get_singleton()->set_context(DisplayServer::CONTEXT_PROJECTMAN);
  2539. Engine::get_singleton()->startup_benchmark_end_measure();
  2540. }
  2541. if (project_manager || editor) {
  2542. // Load SSL Certificates from Editor Settings (or builtin)
  2543. Crypto::load_default_certificates(
  2544. EditorSettings::get_singleton()->get_setting("network/tls/editor_tls_certificates").operator String());
  2545. }
  2546. #endif
  2547. }
  2548. if (!hasicon && OS::get_singleton()->get_bundle_icon_path().is_empty()) {
  2549. Ref<Image> icon = memnew(Image(app_icon_png));
  2550. DisplayServer::get_singleton()->set_icon(icon);
  2551. }
  2552. OS::get_singleton()->set_main_loop(main_loop);
  2553. if (movie_writer) {
  2554. movie_writer->begin(DisplayServer::get_singleton()->window_get_size(), fixed_fps, Engine::get_singleton()->get_write_movie_path());
  2555. }
  2556. if (minimum_time_msec) {
  2557. uint64_t minimum_time = 1000 * minimum_time_msec;
  2558. uint64_t elapsed_time = OS::get_singleton()->get_ticks_usec();
  2559. if (elapsed_time < minimum_time) {
  2560. OS::get_singleton()->delay_usec(minimum_time - elapsed_time);
  2561. }
  2562. }
  2563. if (use_startup_benchmark) {
  2564. Engine::get_singleton()->startup_dump(startup_benchmark_file);
  2565. startup_benchmark_file = String();
  2566. }
  2567. return true;
  2568. }
  2569. /* Main iteration
  2570. *
  2571. * This is the iteration of the engine's game loop, advancing the state of physics,
  2572. * rendering and audio.
  2573. * It's called directly by the platform's OS::run method, where the loop is created
  2574. * and monitored.
  2575. *
  2576. * The OS implementation can impact its draw step with the Main::force_redraw() method.
  2577. */
  2578. uint64_t Main::last_ticks = 0;
  2579. uint32_t Main::frames = 0;
  2580. uint32_t Main::hide_print_fps_attempts = 3;
  2581. uint32_t Main::frame = 0;
  2582. bool Main::force_redraw_requested = false;
  2583. int Main::iterating = 0;
  2584. bool Main::agile_input_event_flushing = false;
  2585. bool Main::is_iterating() {
  2586. return iterating > 0;
  2587. }
  2588. // For performance metrics.
  2589. static uint64_t physics_process_max = 0;
  2590. static uint64_t process_max = 0;
  2591. static uint64_t navigation_process_max = 0;
  2592. bool Main::iteration() {
  2593. //for now do not error on this
  2594. //ERR_FAIL_COND_V(iterating, false);
  2595. iterating++;
  2596. const uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  2597. Engine::get_singleton()->_frame_ticks = ticks;
  2598. main_timer_sync.set_cpu_ticks_usec(ticks);
  2599. main_timer_sync.set_fixed_fps(fixed_fps);
  2600. const uint64_t ticks_elapsed = ticks - last_ticks;
  2601. const int physics_ticks_per_second = Engine::get_singleton()->get_physics_ticks_per_second();
  2602. const double physics_step = 1.0 / physics_ticks_per_second;
  2603. const double time_scale = Engine::get_singleton()->get_time_scale();
  2604. MainFrameTime advance = main_timer_sync.advance(physics_step, physics_ticks_per_second);
  2605. double process_step = advance.process_step;
  2606. double scaled_step = process_step * time_scale;
  2607. Engine::get_singleton()->_process_step = process_step;
  2608. Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction;
  2609. uint64_t physics_process_ticks = 0;
  2610. uint64_t process_ticks = 0;
  2611. uint64_t navigation_process_ticks = 0;
  2612. frame += ticks_elapsed;
  2613. last_ticks = ticks;
  2614. const int max_physics_steps = Engine::get_singleton()->get_max_physics_steps_per_frame();
  2615. if (fixed_fps == -1 && advance.physics_steps > max_physics_steps) {
  2616. process_step -= (advance.physics_steps - max_physics_steps) * physics_step;
  2617. advance.physics_steps = max_physics_steps;
  2618. }
  2619. bool exit = false;
  2620. // process all our active interfaces
  2621. XRServer::get_singleton()->_process();
  2622. for (int iters = 0; iters < advance.physics_steps; ++iters) {
  2623. if (Input::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
  2624. Input::get_singleton()->flush_buffered_events();
  2625. }
  2626. Engine::get_singleton()->_in_physics = true;
  2627. uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();
  2628. PhysicsServer3D::get_singleton()->sync();
  2629. PhysicsServer3D::get_singleton()->flush_queries();
  2630. PhysicsServer2D::get_singleton()->sync();
  2631. PhysicsServer2D::get_singleton()->flush_queries();
  2632. if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) {
  2633. PhysicsServer3D::get_singleton()->end_sync();
  2634. PhysicsServer2D::get_singleton()->end_sync();
  2635. exit = true;
  2636. break;
  2637. }
  2638. uint64_t navigation_begin = OS::get_singleton()->get_ticks_usec();
  2639. NavigationServer3D::get_singleton()->process(physics_step * time_scale);
  2640. navigation_process_ticks = MAX(navigation_process_ticks, OS::get_singleton()->get_ticks_usec() - navigation_begin); // keep the largest one for reference
  2641. navigation_process_max = MAX(OS::get_singleton()->get_ticks_usec() - navigation_begin, navigation_process_max);
  2642. message_queue->flush();
  2643. PhysicsServer3D::get_singleton()->end_sync();
  2644. PhysicsServer3D::get_singleton()->step(physics_step * time_scale);
  2645. PhysicsServer2D::get_singleton()->end_sync();
  2646. PhysicsServer2D::get_singleton()->step(physics_step * time_scale);
  2647. message_queue->flush();
  2648. physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
  2649. physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
  2650. Engine::get_singleton()->_physics_frames++;
  2651. Engine::get_singleton()->_in_physics = false;
  2652. }
  2653. if (Input::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
  2654. Input::get_singleton()->flush_buffered_events();
  2655. }
  2656. uint64_t process_begin = OS::get_singleton()->get_ticks_usec();
  2657. if (OS::get_singleton()->get_main_loop()->process(process_step * time_scale)) {
  2658. exit = true;
  2659. }
  2660. message_queue->flush();
  2661. RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames.
  2662. if (DisplayServer::get_singleton()->can_any_window_draw() &&
  2663. RenderingServer::get_singleton()->is_render_loop_enabled()) {
  2664. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  2665. if (RenderingServer::get_singleton()->has_changed()) {
  2666. RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
  2667. Engine::get_singleton()->frames_drawn++;
  2668. }
  2669. } else {
  2670. RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
  2671. Engine::get_singleton()->frames_drawn++;
  2672. force_redraw_requested = false;
  2673. }
  2674. }
  2675. process_ticks = OS::get_singleton()->get_ticks_usec() - process_begin;
  2676. process_max = MAX(process_ticks, process_max);
  2677. uint64_t frame_time = OS::get_singleton()->get_ticks_usec() - ticks;
  2678. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2679. ScriptServer::get_language(i)->frame();
  2680. }
  2681. AudioServer::get_singleton()->update();
  2682. if (EngineDebugger::is_active()) {
  2683. EngineDebugger::get_singleton()->iteration(frame_time, process_ticks, physics_process_ticks, physics_step);
  2684. }
  2685. frames++;
  2686. Engine::get_singleton()->_process_frames++;
  2687. if (frame > 1000000) {
  2688. // Wait a few seconds before printing FPS, as FPS reporting just after the engine has started is inaccurate.
  2689. if (hide_print_fps_attempts == 0) {
  2690. if (editor || project_manager) {
  2691. if (print_fps) {
  2692. print_line(vformat("Editor FPS: %d (%s mspf)", frames, rtos(1000.0 / frames).pad_decimals(2)));
  2693. }
  2694. } else if (print_fps || GLOBAL_GET("debug/settings/stdout/print_fps")) {
  2695. print_line(vformat("Project FPS: %d (%s mspf)", frames, rtos(1000.0 / frames).pad_decimals(2)));
  2696. }
  2697. } else {
  2698. hide_print_fps_attempts--;
  2699. }
  2700. Engine::get_singleton()->_fps = frames;
  2701. performance->set_process_time(USEC_TO_SEC(process_max));
  2702. performance->set_physics_process_time(USEC_TO_SEC(physics_process_max));
  2703. performance->set_navigation_process_time(USEC_TO_SEC(navigation_process_max));
  2704. process_max = 0;
  2705. physics_process_max = 0;
  2706. navigation_process_max = 0;
  2707. frame %= 1000000;
  2708. frames = 0;
  2709. }
  2710. iterating--;
  2711. // Needed for OSs using input buffering regardless accumulation (like Android)
  2712. if (Input::get_singleton()->is_using_input_buffering() && !agile_input_event_flushing) {
  2713. Input::get_singleton()->flush_buffered_events();
  2714. }
  2715. if (movie_writer) {
  2716. RID main_vp_rid = RenderingServer::get_singleton()->viewport_find_from_screen_attachment(DisplayServer::MAIN_WINDOW_ID);
  2717. RID main_vp_texture = RenderingServer::get_singleton()->viewport_get_texture(main_vp_rid);
  2718. Ref<Image> vp_tex = RenderingServer::get_singleton()->texture_2d_get(main_vp_texture);
  2719. movie_writer->add_frame(vp_tex);
  2720. }
  2721. if (fixed_fps != -1) {
  2722. return exit;
  2723. }
  2724. OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw());
  2725. #ifdef TOOLS_ENABLED
  2726. if (auto_build_solutions) {
  2727. auto_build_solutions = false;
  2728. // Only relevant when running the editor.
  2729. if (!editor) {
  2730. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  2731. ERR_FAIL_V_MSG(true,
  2732. "Command line option --build-solutions was passed, but no project is being edited. Aborting.");
  2733. }
  2734. if (!EditorNode::get_singleton()->call_build()) {
  2735. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  2736. ERR_FAIL_V_MSG(true,
  2737. "Command line option --build-solutions was passed, but the build callback failed. Aborting.");
  2738. }
  2739. }
  2740. #endif
  2741. return exit || auto_quit;
  2742. }
  2743. void Main::force_redraw() {
  2744. force_redraw_requested = true;
  2745. }
  2746. /* Engine deinitialization
  2747. *
  2748. * Responsible for freeing all the memory allocated by previous setup steps,
  2749. * so that the engine closes cleanly without leaking memory or crashing.
  2750. * The order matters as some of those steps are linked with each other.
  2751. */
  2752. void Main::cleanup(bool p_force) {
  2753. if (!p_force) {
  2754. ERR_FAIL_COND(!_start_success);
  2755. }
  2756. for (int i = 0; i < TextServerManager::get_singleton()->get_interface_count(); i++) {
  2757. TextServerManager::get_singleton()->get_interface(i)->cleanup();
  2758. }
  2759. if (movie_writer) {
  2760. movie_writer->end();
  2761. }
  2762. ResourceLoader::remove_custom_loaders();
  2763. ResourceSaver::remove_custom_savers();
  2764. // Flush before uninitializing the scene, but delete the MessageQueue as late as possible.
  2765. message_queue->flush();
  2766. OS::get_singleton()->delete_main_loop();
  2767. OS::get_singleton()->_cmdline.clear();
  2768. OS::get_singleton()->_user_args.clear();
  2769. OS::get_singleton()->_execpath = "";
  2770. OS::get_singleton()->_local_clipboard = "";
  2771. ResourceLoader::clear_translation_remaps();
  2772. ResourceLoader::clear_path_remaps();
  2773. ResourceLoader::clear_thread_load_tasks();
  2774. ScriptServer::finish_languages();
  2775. // Sync pending commands that may have been queued from a different thread during ScriptServer finalization
  2776. RenderingServer::get_singleton()->sync();
  2777. //clear global shader variables before scene and other graphics stuff are deinitialized.
  2778. rendering_server->global_shader_parameters_clear();
  2779. if (xr_server) {
  2780. // Now that we're unregistering properly in plugins we need to keep access to xr_server for a little longer
  2781. // We do however unset our primary interface
  2782. xr_server->set_primary_interface(Ref<XRInterface>());
  2783. }
  2784. #ifdef TOOLS_ENABLED
  2785. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
  2786. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
  2787. unregister_editor_types();
  2788. #endif
  2789. ImageLoader::cleanup();
  2790. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SCENE);
  2791. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SCENE);
  2792. unregister_platform_apis();
  2793. unregister_driver_types();
  2794. unregister_scene_types();
  2795. finalize_theme_db();
  2796. // Before deinitializing server extensions, finalize servers which may be loaded as extensions.
  2797. finalize_navigation_server();
  2798. finalize_physics();
  2799. GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_SERVERS);
  2800. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_SERVERS);
  2801. unregister_server_types();
  2802. EngineDebugger::deinitialize();
  2803. if (xr_server) {
  2804. memdelete(xr_server);
  2805. }
  2806. if (audio_server) {
  2807. audio_server->finish();
  2808. memdelete(audio_server);
  2809. }
  2810. if (camera_server) {
  2811. memdelete(camera_server);
  2812. }
  2813. OS::get_singleton()->finalize();
  2814. finalize_display();
  2815. if (input) {
  2816. memdelete(input);
  2817. }
  2818. if (packed_data) {
  2819. memdelete(packed_data);
  2820. }
  2821. if (file_access_network_client) {
  2822. memdelete(file_access_network_client);
  2823. }
  2824. if (performance) {
  2825. memdelete(performance);
  2826. }
  2827. if (input_map) {
  2828. memdelete(input_map);
  2829. }
  2830. if (time_singleton) {
  2831. memdelete(time_singleton);
  2832. }
  2833. if (translation_server) {
  2834. memdelete(translation_server);
  2835. }
  2836. if (tsman) {
  2837. memdelete(tsman);
  2838. }
  2839. if (physics_server_3d_manager) {
  2840. memdelete(physics_server_3d_manager);
  2841. }
  2842. if (physics_server_2d_manager) {
  2843. memdelete(physics_server_2d_manager);
  2844. }
  2845. if (globals) {
  2846. memdelete(globals);
  2847. }
  2848. if (engine) {
  2849. memdelete(engine);
  2850. }
  2851. if (OS::get_singleton()->is_restart_on_exit_set()) {
  2852. //attempt to restart with arguments
  2853. List<String> args = OS::get_singleton()->get_restart_on_exit_arguments();
  2854. OS::get_singleton()->create_instance(args);
  2855. OS::get_singleton()->set_restart_on_exit(false, List<String>()); //clear list (uses memory)
  2856. }
  2857. // Now should be safe to delete MessageQueue (famous last words).
  2858. message_queue->flush();
  2859. memdelete(message_queue);
  2860. unregister_core_driver_types();
  2861. unregister_core_extensions();
  2862. uninitialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
  2863. unregister_core_types();
  2864. OS::get_singleton()->finalize_core();
  2865. }