main.cpp 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726
  1. /*************************************************************************/
  2. /* main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "main.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_string_names.h"
  33. #include "core/crypto/crypto.h"
  34. #include "core/debugger/engine_debugger.h"
  35. #include "core/extension/extension_api_dump.h"
  36. #include "core/input/input.h"
  37. #include "core/input/input_map.h"
  38. #include "core/io/dir_access.h"
  39. #include "core/io/file_access_network.h"
  40. #include "core/io/file_access_pack.h"
  41. #include "core/io/file_access_zip.h"
  42. #include "core/io/image_loader.h"
  43. #include "core/io/ip.h"
  44. #include "core/io/resource_loader.h"
  45. #include "core/object/message_queue.h"
  46. #include "core/os/os.h"
  47. #include "core/os/time.h"
  48. #include "core/register_core_types.h"
  49. #include "core/string/translation.h"
  50. #include "core/version.h"
  51. #include "core/version_hash.gen.h"
  52. #include "drivers/register_driver_types.h"
  53. #include "main/app_icon.gen.h"
  54. #include "main/main_timer_sync.h"
  55. #include "main/performance.h"
  56. #include "main/splash.gen.h"
  57. #include "main/splash_editor.gen.h"
  58. #include "modules/modules_enabled.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 "servers/audio_server.h"
  66. #include "servers/camera_server.h"
  67. #include "servers/display_server.h"
  68. #include "servers/navigation_server_2d.h"
  69. #include "servers/navigation_server_3d.h"
  70. #include "servers/physics_server_2d.h"
  71. #include "servers/physics_server_3d.h"
  72. #include "servers/register_server_types.h"
  73. #include "servers/rendering/rendering_server_default.h"
  74. #include "servers/text_server.h"
  75. #include "servers/xr_server.h"
  76. #ifdef TESTS_ENABLED
  77. #include "tests/test_main.h"
  78. #endif
  79. #ifdef TOOLS_ENABLED
  80. #include "editor/doc_data_class_path.gen.h"
  81. #include "editor/doc_tools.h"
  82. #include "editor/editor_node.h"
  83. #include "editor/editor_settings.h"
  84. #include "editor/progress_dialog.h"
  85. #include "editor/project_manager.h"
  86. #endif
  87. /* Static members */
  88. // Singletons
  89. // Initialized in setup()
  90. static Engine *engine = nullptr;
  91. static ProjectSettings *globals = nullptr;
  92. static Input *input = nullptr;
  93. static InputMap *input_map = nullptr;
  94. static TranslationServer *translation_server = nullptr;
  95. static Performance *performance = nullptr;
  96. static PackedData *packed_data = nullptr;
  97. static Time *time_singleton = nullptr;
  98. #ifdef MINIZIP_ENABLED
  99. static ZipArchive *zip_packed_data = nullptr;
  100. #endif
  101. static FileAccessNetworkClient *file_access_network_client = nullptr;
  102. static MessageQueue *message_queue = nullptr;
  103. // Initialized in setup2()
  104. static AudioServer *audio_server = nullptr;
  105. static DisplayServer *display_server = nullptr;
  106. static RenderingServer *rendering_server = nullptr;
  107. static CameraServer *camera_server = nullptr;
  108. static XRServer *xr_server = nullptr;
  109. static TextServerManager *tsman = nullptr;
  110. static PhysicsServer3D *physics_server = nullptr;
  111. static PhysicsServer2D *physics_2d_server = nullptr;
  112. static NavigationServer3D *navigation_server = nullptr;
  113. static NavigationServer2D *navigation_2d_server = nullptr;
  114. // We error out if setup2() doesn't turn this true
  115. static bool _start_success = false;
  116. // Drivers
  117. String tablet_driver = "";
  118. String text_driver = "";
  119. static int text_driver_idx = -1;
  120. static int display_driver_idx = -1;
  121. static int audio_driver_idx = -1;
  122. // Engine config/tools
  123. static bool single_window = false;
  124. static bool editor = false;
  125. static bool project_manager = false;
  126. static bool cmdline_tool = false;
  127. static String locale;
  128. static bool show_help = false;
  129. static bool auto_quit = false;
  130. static OS::ProcessID allow_focus_steal_pid = 0;
  131. #ifdef TOOLS_ENABLED
  132. static bool auto_build_solutions = false;
  133. #endif
  134. // Display
  135. static DisplayServer::WindowMode window_mode = DisplayServer::WINDOW_MODE_WINDOWED;
  136. static DisplayServer::ScreenOrientation window_orientation = DisplayServer::SCREEN_LANDSCAPE;
  137. static DisplayServer::VSyncMode window_vsync_mode = DisplayServer::VSYNC_ENABLED;
  138. static uint32_t window_flags = 0;
  139. static Size2i window_size = Size2i(1024, 600);
  140. static int init_screen = -1;
  141. static bool init_fullscreen = false;
  142. static bool init_maximized = false;
  143. static bool init_windowed = false;
  144. static bool init_always_on_top = false;
  145. static bool init_use_custom_pos = false;
  146. static Vector2 init_custom_pos;
  147. static bool force_lowdpi = false;
  148. // Debug
  149. static bool use_debug_profiler = false;
  150. #ifdef DEBUG_ENABLED
  151. static bool debug_collisions = false;
  152. static bool debug_navigation = false;
  153. #endif
  154. static int frame_delay = 0;
  155. static bool disable_render_loop = false;
  156. static int fixed_fps = -1;
  157. static bool print_fps = false;
  158. #ifdef TOOLS_ENABLED
  159. static bool dump_extension_api = false;
  160. #endif
  161. bool profile_gpu = false;
  162. /* Helper methods */
  163. // Used by Mono module, should likely be registered in Engine singleton instead
  164. // FIXME: This is also not 100% accurate, `project_manager` is only true when it was requested,
  165. // but not if e.g. we fail to load and project and fallback to the manager.
  166. bool Main::is_project_manager() {
  167. return project_manager;
  168. }
  169. bool Main::is_cmdline_tool() {
  170. return cmdline_tool;
  171. }
  172. static String unescape_cmdline(const String &p_str) {
  173. return p_str.replace("%20", " ");
  174. }
  175. static String get_full_version_string() {
  176. String hash = String(VERSION_HASH);
  177. if (hash.length() != 0) {
  178. hash = "." + hash.left(9);
  179. }
  180. return String(VERSION_FULL_BUILD) + hash;
  181. }
  182. // FIXME: Could maybe be moved to PhysicsServer3DManager and PhysicsServer2DManager directly
  183. // to have less code in main.cpp.
  184. void initialize_physics() {
  185. /// 3D Physics Server
  186. physics_server = PhysicsServer3DManager::new_server(
  187. ProjectSettings::get_singleton()->get(PhysicsServer3DManager::setting_property_name));
  188. if (!physics_server) {
  189. // Physics server not found, Use the default physics
  190. physics_server = PhysicsServer3DManager::new_default_server();
  191. }
  192. ERR_FAIL_COND(!physics_server);
  193. physics_server->init();
  194. /// 2D Physics server
  195. physics_2d_server = PhysicsServer2DManager::new_server(
  196. ProjectSettings::get_singleton()->get(PhysicsServer2DManager::setting_property_name));
  197. if (!physics_2d_server) {
  198. // Physics server not found, Use the default physics
  199. physics_2d_server = PhysicsServer2DManager::new_default_server();
  200. }
  201. ERR_FAIL_COND(!physics_2d_server);
  202. physics_2d_server->init();
  203. }
  204. void finalize_physics() {
  205. physics_server->finish();
  206. memdelete(physics_server);
  207. physics_2d_server->finish();
  208. memdelete(physics_2d_server);
  209. }
  210. void finalize_display() {
  211. rendering_server->finish();
  212. memdelete(rendering_server);
  213. memdelete(display_server);
  214. }
  215. void initialize_navigation_server() {
  216. ERR_FAIL_COND(navigation_server != nullptr);
  217. navigation_server = NavigationServer3DManager::new_default_server();
  218. navigation_2d_server = memnew(NavigationServer2D);
  219. }
  220. void finalize_navigation_server() {
  221. memdelete(navigation_server);
  222. navigation_server = nullptr;
  223. memdelete(navigation_2d_server);
  224. navigation_2d_server = nullptr;
  225. }
  226. //#define DEBUG_INIT
  227. #ifdef DEBUG_INIT
  228. #define MAIN_PRINT(m_txt) print_line(m_txt)
  229. #else
  230. #define MAIN_PRINT(m_txt)
  231. #endif
  232. void Main::print_help(const char *p_binary) {
  233. print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
  234. OS::get_singleton()->print("Free and open source software under the terms of the MIT license.\n");
  235. OS::get_singleton()->print("(c) 2007-2021 Juan Linietsky, Ariel Manzur.\n");
  236. OS::get_singleton()->print("(c) 2014-2021 Godot Engine contributors.\n");
  237. OS::get_singleton()->print("\n");
  238. OS::get_singleton()->print("Usage: %s [options] [path to scene or 'project.godot' file]\n", p_binary);
  239. OS::get_singleton()->print("\n");
  240. OS::get_singleton()->print("General options:\n");
  241. OS::get_singleton()->print(" -h, --help Display this help message.\n");
  242. OS::get_singleton()->print(" --version Display the version string.\n");
  243. OS::get_singleton()->print(" -v, --verbose Use verbose stdout mode.\n");
  244. OS::get_singleton()->print(" --quiet Quiet mode, silences stdout messages. Errors are still displayed.\n");
  245. OS::get_singleton()->print("\n");
  246. OS::get_singleton()->print("Run options:\n");
  247. #ifdef TOOLS_ENABLED
  248. OS::get_singleton()->print(" -e, --editor Start the editor instead of running the scene.\n");
  249. OS::get_singleton()->print(" -p, --project-manager Start the project manager, even if a project is auto-detected.\n");
  250. #endif
  251. OS::get_singleton()->print(" -q, --quit Quit after the first iteration.\n");
  252. OS::get_singleton()->print(" -l, --language <locale> Use a specific locale (<locale> being a two-letter code).\n");
  253. OS::get_singleton()->print(" --path <directory> Path to a project (<directory> must contain a 'project.godot' file).\n");
  254. OS::get_singleton()->print(" -u, --upwards Scan folders upwards for project.godot file.\n");
  255. OS::get_singleton()->print(" --main-pack <file> Path to a pack (.pck) file to load.\n");
  256. OS::get_singleton()->print(" --render-thread <mode> Render thread mode ('unsafe', 'safe', 'separate').\n");
  257. OS::get_singleton()->print(" --remote-fs <address> Remote filesystem (<host/IP>[:<port>] address).\n");
  258. OS::get_singleton()->print(" --remote-fs-password <password> Password for remote filesystem.\n");
  259. OS::get_singleton()->print(" --audio-driver <driver> Audio driver [");
  260. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  261. if (i > 0) {
  262. OS::get_singleton()->print(", ");
  263. }
  264. OS::get_singleton()->print("'%s'", AudioDriverManager::get_driver(i)->get_name());
  265. }
  266. OS::get_singleton()->print("].\n");
  267. OS::get_singleton()->print(" --display-driver <driver> Display driver (and rendering driver) [");
  268. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  269. if (i > 0) {
  270. OS::get_singleton()->print(", ");
  271. }
  272. OS::get_singleton()->print("'%s' (", DisplayServer::get_create_function_name(i));
  273. Vector<String> rd = DisplayServer::get_create_function_rendering_drivers(i);
  274. for (int j = 0; j < rd.size(); j++) {
  275. if (j > 0) {
  276. OS::get_singleton()->print(", ");
  277. }
  278. OS::get_singleton()->print("'%s'", rd[j].utf8().get_data());
  279. }
  280. OS::get_singleton()->print(")");
  281. }
  282. OS::get_singleton()->print("].\n");
  283. OS::get_singleton()->print(" --rendering-driver <driver> Rendering driver (depends on display driver).\n");
  284. OS::get_singleton()->print(" --text-driver <driver> Text driver (Fonts, BiDi, shaping)\n");
  285. OS::get_singleton()->print("\n");
  286. #ifndef SERVER_ENABLED
  287. OS::get_singleton()->print("Display options:\n");
  288. OS::get_singleton()->print(" -f, --fullscreen Request fullscreen mode.\n");
  289. OS::get_singleton()->print(" -m, --maximized Request a maximized window.\n");
  290. OS::get_singleton()->print(" -w, --windowed Request windowed mode.\n");
  291. OS::get_singleton()->print(" -t, --always-on-top Request an always-on-top window.\n");
  292. OS::get_singleton()->print(" --resolution <W>x<H> Request window resolution.\n");
  293. OS::get_singleton()->print(" --position <X>,<Y> Request window position.\n");
  294. OS::get_singleton()->print(" --low-dpi Force low-DPI mode (macOS and Windows only).\n");
  295. OS::get_singleton()->print(" --no-window Disable window creation (Windows only). Useful together with --script.\n");
  296. OS::get_singleton()->print(" --single-window Use a single window (no separate subwindows).\n");
  297. OS::get_singleton()->print(" --tablet-driver Pen tablet input driver.\n");
  298. OS::get_singleton()->print("\n");
  299. #endif
  300. OS::get_singleton()->print("Debug options:\n");
  301. OS::get_singleton()->print(" -d, --debug Debug (local stdout debugger).\n");
  302. OS::get_singleton()->print(" -b, --breakpoints Breakpoint list as source::line comma-separated pairs, no spaces (use %%20 instead).\n");
  303. OS::get_singleton()->print(" --profiling Enable profiling in the script debugger.\n");
  304. OS::get_singleton()->print(" --vk-layers Enable Vulkan Validation layers for debugging.\n");
  305. #if DEBUG_ENABLED
  306. OS::get_singleton()->print(" --gpu-abort Abort on GPU errors (usually validation layer errors), may help see the problem if your system freezes.\n");
  307. #endif
  308. OS::get_singleton()->print(" --remote-debug <uri> Remote debug (<protocol>://<host/IP>[:<port>], e.g. tcp://127.0.0.1:6007).\n");
  309. #if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED)
  310. OS::get_singleton()->print(" --debug-collisions Show collision shapes when running the scene.\n");
  311. OS::get_singleton()->print(" --debug-navigation Show navigation polygons when running the scene.\n");
  312. #endif
  313. OS::get_singleton()->print(" --frame-delay <ms> Simulate high CPU load (delay each frame by <ms> milliseconds).\n");
  314. OS::get_singleton()->print(" --time-scale <scale> Force time scale (higher values are faster, 1.0 is normal speed).\n");
  315. OS::get_singleton()->print(" --disable-render-loop Disable render loop so rendering only occurs when called explicitly from script.\n");
  316. OS::get_singleton()->print(" --disable-crash-handler Disable crash handler when supported by the platform code.\n");
  317. OS::get_singleton()->print(" --fixed-fps <fps> Force a fixed number of frames per second. This setting disables real-time synchronization.\n");
  318. OS::get_singleton()->print(" --print-fps Print the frames per second to the stdout.\n");
  319. OS::get_singleton()->print(" --profile-gpu Show a simple profile of the tasks that took more time during frame rendering.\n");
  320. OS::get_singleton()->print("\n");
  321. OS::get_singleton()->print("Standalone tools:\n");
  322. OS::get_singleton()->print(" -s, --script <script> Run a script.\n");
  323. OS::get_singleton()->print(" --check-only Only parse for errors and quit (use with --script).\n");
  324. #ifdef TOOLS_ENABLED
  325. OS::get_singleton()->print(" --export <preset> <path> Export the project using the given preset and matching release template. The preset name should match one defined in export_presets.cfg.\n");
  326. 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'). The target directory should exist.\n");
  327. OS::get_singleton()->print(" --export-debug <preset> <path> Same as --export, but using the debug template.\n");
  328. OS::get_singleton()->print(" --export-pack <preset> <path> Same as --export, but only export the game pack for the given preset. The <path> extension determines whether it will be in PCK or ZIP format.\n");
  329. 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");
  330. OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n");
  331. 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");
  332. #ifdef DEBUG_METHODS_ENABLED
  333. OS::get_singleton()->print(" --gdnative-generate-json-api <path> Generate JSON dump of the Godot API for GDNative bindings and save it on the file specified in <path>.\n");
  334. OS::get_singleton()->print(" --gdnative-generate-json-builtin-api <path> Generate JSON dump of the Godot API of the builtin Variant types and utility functions for GDNative bindings and save it on the file specified in <path>.\n");
  335. #endif
  336. #ifdef TESTS_ENABLED
  337. OS::get_singleton()->print(" --test [--help] Run unit tests. Use --test --help for more information.\n");
  338. #endif
  339. #endif
  340. OS::get_singleton()->print("\n");
  341. }
  342. #ifdef TESTS_ENABLED
  343. // The order is the same as in `Main::setup()`, only core and some editor types
  344. // are initialized here. This also combines `Main::setup2()` initialization.
  345. Error Main::test_setup() {
  346. OS::get_singleton()->initialize();
  347. engine = memnew(Engine);
  348. register_core_types();
  349. register_core_driver_types();
  350. packed_data = memnew(PackedData);
  351. globals = memnew(ProjectSettings);
  352. GLOBAL_DEF("debug/settings/crash_handler/message",
  353. String("Please include this when reporting the bug on https://github.com/godotengine/godot/issues"));
  354. translation_server = memnew(TranslationServer);
  355. register_core_extensions();
  356. // From `Main::setup2()`.
  357. preregister_module_types();
  358. preregister_server_types();
  359. register_core_singletons();
  360. register_server_types();
  361. translation_server->setup(); //register translations, load them, etc.
  362. if (locale != "") {
  363. translation_server->set_locale(locale);
  364. }
  365. translation_server->load_translations();
  366. ResourceLoader::load_translation_remaps(); //load remaps for resources
  367. ResourceLoader::load_path_remaps();
  368. register_scene_types();
  369. #ifdef TOOLS_ENABLED
  370. ClassDB::set_current_api(ClassDB::API_EDITOR);
  371. EditorNode::register_editor_types();
  372. ClassDB::set_current_api(ClassDB::API_CORE);
  373. #endif
  374. register_platform_apis();
  375. register_module_types();
  376. register_driver_types();
  377. ClassDB::set_current_api(ClassDB::API_NONE);
  378. _start_success = true;
  379. return OK;
  380. }
  381. // The order is the same as in `Main::cleanup()`.
  382. void Main::test_cleanup() {
  383. ERR_FAIL_COND(!_start_success);
  384. EngineDebugger::deinitialize();
  385. ResourceLoader::remove_custom_loaders();
  386. ResourceSaver::remove_custom_savers();
  387. #ifdef TOOLS_ENABLED
  388. EditorNode::unregister_editor_types();
  389. #endif
  390. unregister_driver_types();
  391. unregister_module_types();
  392. unregister_platform_apis();
  393. unregister_scene_types();
  394. unregister_server_types();
  395. OS::get_singleton()->finalize();
  396. if (translation_server) {
  397. memdelete(translation_server);
  398. }
  399. if (globals) {
  400. memdelete(globals);
  401. }
  402. if (packed_data) {
  403. memdelete(packed_data);
  404. }
  405. if (engine) {
  406. memdelete(engine);
  407. }
  408. unregister_core_driver_types();
  409. unregister_core_types();
  410. OS::get_singleton()->finalize_core();
  411. }
  412. #endif
  413. int Main::test_entrypoint(int argc, char *argv[], bool &tests_need_run) {
  414. #ifdef TESTS_ENABLED
  415. for (int x = 0; x < argc; x++) {
  416. if ((strncmp(argv[x], "--test", 6) == 0) && (strlen(argv[x]) == 6)) {
  417. tests_need_run = true;
  418. // TODO: need to come up with different test contexts.
  419. // Not every test requires high-level functionality like `ClassDB`.
  420. test_setup();
  421. int status = test_main(argc, argv);
  422. test_cleanup();
  423. return status;
  424. }
  425. }
  426. #endif
  427. tests_need_run = false;
  428. return 0;
  429. }
  430. /* Engine initialization
  431. *
  432. * Consists of several methods that are called by each platform's specific main(argc, argv).
  433. * To fully understand engine init, one should therefore start from the platform's main and
  434. * see how it calls into the Main class' methods.
  435. *
  436. * The initialization is typically done in 3 steps (with the setup2 step triggered either
  437. * automatically by setup, or manually in the platform's main).
  438. *
  439. * - setup(execpath, argc, argv, p_second_phase) is the main entry point for all platforms,
  440. * responsible for the initialization of all low level singletons and core types, and parsing
  441. * command line arguments to configure things accordingly.
  442. * If p_second_phase is true, it will chain into setup2() (default behaviour). This is
  443. * disabled on some platforms (Android, iOS, UWP) which trigger the second step in their
  444. * own time.
  445. *
  446. * - setup2(p_main_tid_override) registers high level servers and singletons, displays the
  447. * boot splash, then registers higher level types (scene, editor, etc.).
  448. *
  449. * - start() is the last step and that's where command line tools can run, or the main loop
  450. * can be created eventually and the project settings put into action. That's also where
  451. * the editor node is created, if relevant.
  452. * start() does it own argument parsing for a subset of the command line arguments described
  453. * in help, it's a bit messy and should be globalized with the setup() parsing somehow.
  454. */
  455. Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_phase) {
  456. OS::get_singleton()->initialize();
  457. engine = memnew(Engine);
  458. MAIN_PRINT("Main: Initialize CORE");
  459. register_core_types();
  460. register_core_driver_types();
  461. MAIN_PRINT("Main: Initialize Globals");
  462. input_map = memnew(InputMap);
  463. time_singleton = memnew(Time);
  464. globals = memnew(ProjectSettings);
  465. register_core_settings(); //here globals are present
  466. translation_server = memnew(TranslationServer);
  467. performance = memnew(Performance);
  468. GDREGISTER_CLASS(Performance);
  469. engine->add_singleton(Engine::Singleton("Performance", performance));
  470. // Only flush stdout in debug builds by default, as spamming `print()` will
  471. // decrease performance if this is enabled.
  472. GLOBAL_DEF_RST("application/run/flush_stdout_on_print", false);
  473. GLOBAL_DEF_RST("application/run/flush_stdout_on_print.debug", true);
  474. GLOBAL_DEF("debug/settings/crash_handler/message",
  475. String("Please include this when reporting the bug on https://github.com/godotengine/godot/issues"));
  476. MAIN_PRINT("Main: Parse CMDLine");
  477. /* argument parsing and main creation */
  478. List<String> args;
  479. List<String> main_args;
  480. for (int i = 0; i < argc; i++) {
  481. args.push_back(String::utf8(argv[i]));
  482. }
  483. List<String>::Element *I = args.front();
  484. I = args.front();
  485. while (I) {
  486. I->get() = unescape_cmdline(I->get().strip_edges());
  487. I = I->next();
  488. }
  489. I = args.front();
  490. String display_driver = "";
  491. String audio_driver = "";
  492. String project_path = ".";
  493. bool upwards = false;
  494. String debug_uri = "";
  495. bool skip_breakpoints = false;
  496. String main_pack;
  497. bool quiet_stdout = false;
  498. int rtm = -1;
  499. String remotefs;
  500. String remotefs_pass;
  501. Vector<String> breakpoints;
  502. bool use_custom_res = true;
  503. bool force_res = false;
  504. #ifdef TOOLS_ENABLED
  505. bool found_project = false;
  506. #endif
  507. packed_data = PackedData::get_singleton();
  508. if (!packed_data) {
  509. packed_data = memnew(PackedData);
  510. }
  511. #ifdef MINIZIP_ENABLED
  512. //XXX: always get_singleton() == 0x0
  513. zip_packed_data = ZipArchive::get_singleton();
  514. //TODO: remove this temporary fix
  515. if (!zip_packed_data) {
  516. zip_packed_data = memnew(ZipArchive);
  517. }
  518. packed_data->add_pack_source(zip_packed_data);
  519. #endif
  520. I = args.front();
  521. while (I) {
  522. #ifdef OSX_ENABLED
  523. // Ignore the process serial number argument passed by macOS Gatekeeper.
  524. // Otherwise, Godot would try to open a non-existent project on the first start and abort.
  525. if (I->get().begins_with("-psn_")) {
  526. I = I->next();
  527. continue;
  528. }
  529. #endif
  530. List<String>::Element *N = I->next();
  531. if (I->get() == "-h" || I->get() == "--help" || I->get() == "/?") { // display help
  532. show_help = true;
  533. goto error;
  534. } else if (I->get() == "--version") {
  535. print_line(get_full_version_string());
  536. goto error;
  537. } else if (I->get() == "-v" || I->get() == "--verbose") { // verbose output
  538. OS::get_singleton()->_verbose_stdout = true;
  539. } else if (I->get() == "--quiet") { // quieter output
  540. quiet_stdout = true;
  541. } else if (I->get() == "--audio-driver") { // audio driver
  542. if (I->next()) {
  543. audio_driver = I->next()->get();
  544. bool found = false;
  545. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  546. if (audio_driver == AudioDriverManager::get_driver(i)->get_name()) {
  547. found = true;
  548. }
  549. }
  550. if (!found) {
  551. OS::get_singleton()->print("Unknown audio driver '%s', aborting.\nValid options are ",
  552. audio_driver.utf8().get_data());
  553. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  554. if (i == AudioDriverManager::get_driver_count() - 1) {
  555. OS::get_singleton()->print(" and ");
  556. } else if (i != 0) {
  557. OS::get_singleton()->print(", ");
  558. }
  559. OS::get_singleton()->print("'%s'", AudioDriverManager::get_driver(i)->get_name());
  560. }
  561. OS::get_singleton()->print(".\n");
  562. goto error;
  563. }
  564. N = I->next()->next();
  565. } else {
  566. OS::get_singleton()->print("Missing audio driver argument, aborting.\n");
  567. goto error;
  568. }
  569. } else if (I->get() == "--text-driver") {
  570. if (I->next()) {
  571. text_driver = I->next()->get();
  572. N = I->next()->next();
  573. } else {
  574. OS::get_singleton()->print("Missing text driver argument, aborting.\n");
  575. goto error;
  576. }
  577. } else if (I->get() == "--display-driver") { // force video driver
  578. if (I->next()) {
  579. display_driver = I->next()->get();
  580. bool found = false;
  581. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  582. if (display_driver == DisplayServer::get_create_function_name(i)) {
  583. found = true;
  584. }
  585. }
  586. if (!found) {
  587. OS::get_singleton()->print("Unknown display driver '%s', aborting.\nValid options are ",
  588. display_driver.utf8().get_data());
  589. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  590. if (i == DisplayServer::get_create_function_count() - 1) {
  591. OS::get_singleton()->print(" and ");
  592. } else if (i != 0) {
  593. OS::get_singleton()->print(", ");
  594. }
  595. OS::get_singleton()->print("'%s'", DisplayServer::get_create_function_name(i));
  596. }
  597. OS::get_singleton()->print(".\n");
  598. goto error;
  599. }
  600. N = I->next()->next();
  601. } else {
  602. OS::get_singleton()->print("Missing video driver argument, aborting.\n");
  603. goto error;
  604. }
  605. #ifndef SERVER_ENABLED
  606. } else if (I->get() == "-f" || I->get() == "--fullscreen") { // force fullscreen
  607. init_fullscreen = true;
  608. } else if (I->get() == "-m" || I->get() == "--maximized") { // force maximized window
  609. init_maximized = true;
  610. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  611. } else if (I->get() == "-w" || I->get() == "--windowed") { // force windowed window
  612. init_windowed = true;
  613. } else if (I->get() == "--vk-layers") {
  614. Engine::singleton->use_validation_layers = true;
  615. #ifdef DEBUG_ENABLED
  616. } else if (I->get() == "--gpu-abort") {
  617. Engine::singleton->abort_on_gpu_errors = true;
  618. #endif
  619. } else if (I->get() == "--tablet-driver") {
  620. if (I->next()) {
  621. tablet_driver = I->next()->get();
  622. N = I->next()->next();
  623. } else {
  624. OS::get_singleton()->print("Missing tablet driver argument, aborting.\n");
  625. goto error;
  626. }
  627. } else if (I->get() == "--single-window") { // force single window
  628. single_window = true;
  629. } else if (I->get() == "-t" || I->get() == "--always-on-top") { // force always-on-top window
  630. init_always_on_top = true;
  631. } else if (I->get() == "--resolution") { // force resolution
  632. if (I->next()) {
  633. String vm = I->next()->get();
  634. if (vm.find("x") == -1) { // invalid parameter format
  635. OS::get_singleton()->print("Invalid resolution '%s', it should be e.g. '1280x720'.\n",
  636. vm.utf8().get_data());
  637. goto error;
  638. }
  639. int w = vm.get_slice("x", 0).to_int();
  640. int h = vm.get_slice("x", 1).to_int();
  641. if (w <= 0 || h <= 0) {
  642. OS::get_singleton()->print("Invalid resolution '%s', width and height must be above 0.\n",
  643. vm.utf8().get_data());
  644. goto error;
  645. }
  646. window_size.width = w;
  647. window_size.height = h;
  648. force_res = true;
  649. N = I->next()->next();
  650. } else {
  651. OS::get_singleton()->print("Missing resolution argument, aborting.\n");
  652. goto error;
  653. }
  654. } else if (I->get() == "--position") { // set window position
  655. if (I->next()) {
  656. String vm = I->next()->get();
  657. if (vm.find(",") == -1) { // invalid parameter format
  658. OS::get_singleton()->print("Invalid position '%s', it should be e.g. '80,128'.\n",
  659. vm.utf8().get_data());
  660. goto error;
  661. }
  662. int x = vm.get_slice(",", 0).to_int();
  663. int y = vm.get_slice(",", 1).to_int();
  664. init_custom_pos = Point2(x, y);
  665. init_use_custom_pos = true;
  666. N = I->next()->next();
  667. } else {
  668. OS::get_singleton()->print("Missing position argument, aborting.\n");
  669. goto error;
  670. }
  671. } else if (I->get() == "--low-dpi") { // force low DPI (macOS only)
  672. force_lowdpi = true;
  673. } else if (I->get() == "--no-window") { // disable window creation (Windows only)
  674. OS::get_singleton()->set_no_window_mode(true);
  675. #endif
  676. } else if (I->get() == "--profiling") { // enable profiling
  677. use_debug_profiler = true;
  678. } else if (I->get() == "-l" || I->get() == "--language") { // language
  679. if (I->next()) {
  680. locale = I->next()->get();
  681. N = I->next()->next();
  682. } else {
  683. OS::get_singleton()->print("Missing language argument, aborting.\n");
  684. goto error;
  685. }
  686. } else if (I->get() == "--remote-fs") { // remote filesystem
  687. if (I->next()) {
  688. remotefs = I->next()->get();
  689. N = I->next()->next();
  690. } else {
  691. OS::get_singleton()->print("Missing remote filesystem address, aborting.\n");
  692. goto error;
  693. }
  694. } else if (I->get() == "--remote-fs-password") { // remote filesystem password
  695. if (I->next()) {
  696. remotefs_pass = I->next()->get();
  697. N = I->next()->next();
  698. } else {
  699. OS::get_singleton()->print("Missing remote filesystem password, aborting.\n");
  700. goto error;
  701. }
  702. } else if (I->get() == "--render-thread") { // render thread mode
  703. if (I->next()) {
  704. if (I->next()->get() == "safe") {
  705. rtm = OS::RENDER_THREAD_SAFE;
  706. } else if (I->next()->get() == "unsafe") {
  707. rtm = OS::RENDER_THREAD_UNSAFE;
  708. } else if (I->next()->get() == "separate") {
  709. rtm = OS::RENDER_SEPARATE_THREAD;
  710. }
  711. N = I->next()->next();
  712. } else {
  713. OS::get_singleton()->print("Missing render thread mode argument, aborting.\n");
  714. goto error;
  715. }
  716. #ifdef TOOLS_ENABLED
  717. } else if (I->get() == "-e" || I->get() == "--editor") { // starts editor
  718. editor = true;
  719. } else if (I->get() == "-p" || I->get() == "--project-manager") { // starts project manager
  720. project_manager = true;
  721. } else if (I->get() == "--build-solutions") { // Build the scripting solution such C#
  722. auto_build_solutions = true;
  723. editor = true;
  724. cmdline_tool = true;
  725. } else if (I->get() == "--gdnative-generate-json-api" || I->get() == "--gdnative-generate-json-builtin-api") {
  726. // Register as an editor instance to use low-end fallback if relevant.
  727. editor = true;
  728. cmdline_tool = true;
  729. // We still pass it to the main arguments since the argument handling itself is not done in this function
  730. main_args.push_back(I->get());
  731. } else if (I->get() == "--dump-extension-api") {
  732. // Register as an editor instance to use low-end fallback if relevant.
  733. editor = true;
  734. cmdline_tool = true;
  735. dump_extension_api = true;
  736. print_line("dump extension?");
  737. main_args.push_back(I->get());
  738. } else if (I->get() == "--export" || I->get() == "--export-debug" ||
  739. I->get() == "--export-pack") { // Export project
  740. // Actually handling is done in start().
  741. editor = true;
  742. cmdline_tool = true;
  743. main_args.push_back(I->get());
  744. } else if (I->get() == "--doctool") {
  745. // Actually handling is done in start().
  746. cmdline_tool = true;
  747. main_args.push_back(I->get());
  748. #endif
  749. } else if (I->get() == "--path") { // set path of project to start or edit
  750. if (I->next()) {
  751. String p = I->next()->get();
  752. if (OS::get_singleton()->set_cwd(p) == OK) {
  753. //nothing
  754. } else {
  755. project_path = I->next()->get(); //use project_path instead
  756. }
  757. N = I->next()->next();
  758. } else {
  759. OS::get_singleton()->print("Missing relative or absolute path, aborting.\n");
  760. goto error;
  761. }
  762. } else if (I->get() == "-u" || I->get() == "--upwards") { // scan folders upwards
  763. upwards = true;
  764. } else if (I->get() == "-q" || I->get() == "--quit") { // Auto quit at the end of the first main loop iteration
  765. auto_quit = true;
  766. } else if (I->get().ends_with("project.godot")) {
  767. String path;
  768. String file = I->get();
  769. int sep = MAX(file.rfind("/"), file.rfind("\\"));
  770. if (sep == -1) {
  771. path = ".";
  772. } else {
  773. path = file.substr(0, sep);
  774. }
  775. if (OS::get_singleton()->set_cwd(path) == OK) {
  776. // path already specified, don't override
  777. } else {
  778. project_path = path;
  779. }
  780. #ifdef TOOLS_ENABLED
  781. editor = true;
  782. #endif
  783. } else if (I->get() == "-b" || I->get() == "--breakpoints") { // add breakpoints
  784. if (I->next()) {
  785. String bplist = I->next()->get();
  786. breakpoints = bplist.split(",");
  787. N = I->next()->next();
  788. } else {
  789. OS::get_singleton()->print("Missing list of breakpoints, aborting.\n");
  790. goto error;
  791. }
  792. } else if (I->get() == "--frame-delay") { // force frame delay
  793. if (I->next()) {
  794. frame_delay = I->next()->get().to_int();
  795. N = I->next()->next();
  796. } else {
  797. OS::get_singleton()->print("Missing frame delay argument, aborting.\n");
  798. goto error;
  799. }
  800. } else if (I->get() == "--time-scale") { // force time scale
  801. if (I->next()) {
  802. Engine::get_singleton()->set_time_scale(I->next()->get().to_float());
  803. N = I->next()->next();
  804. } else {
  805. OS::get_singleton()->print("Missing time scale argument, aborting.\n");
  806. goto error;
  807. }
  808. } else if (I->get() == "--main-pack") {
  809. if (I->next()) {
  810. main_pack = I->next()->get();
  811. N = I->next()->next();
  812. } else {
  813. OS::get_singleton()->print("Missing path to main pack file, aborting.\n");
  814. goto error;
  815. };
  816. } else if (I->get() == "-d" || I->get() == "--debug") {
  817. debug_uri = "local://";
  818. OS::get_singleton()->_debug_stdout = true;
  819. #if defined(DEBUG_ENABLED)
  820. } else if (I->get() == "--debug-collisions") {
  821. debug_collisions = true;
  822. } else if (I->get() == "--debug-navigation") {
  823. debug_navigation = true;
  824. } else if (I->get() == "--debug-stringnames") {
  825. StringName::set_debug_stringnames(true);
  826. #endif
  827. } else if (I->get() == "--remote-debug") {
  828. if (I->next()) {
  829. debug_uri = I->next()->get();
  830. if (debug_uri.find("://") == -1) { // wrong address
  831. OS::get_singleton()->print(
  832. "Invalid debug host address, it should be of the form <protocol>://<host/IP>:<port>.\n");
  833. goto error;
  834. }
  835. N = I->next()->next();
  836. } else {
  837. OS::get_singleton()->print("Missing remote debug host address, aborting.\n");
  838. goto error;
  839. }
  840. } else if (I->get() == "--allow_focus_steal_pid") { // not exposed to user
  841. if (I->next()) {
  842. allow_focus_steal_pid = I->next()->get().to_int();
  843. N = I->next()->next();
  844. } else {
  845. OS::get_singleton()->print("Missing editor PID argument, aborting.\n");
  846. goto error;
  847. }
  848. } else if (I->get() == "--disable-render-loop") {
  849. disable_render_loop = true;
  850. } else if (I->get() == "--fixed-fps") {
  851. if (I->next()) {
  852. fixed_fps = I->next()->get().to_int();
  853. N = I->next()->next();
  854. } else {
  855. OS::get_singleton()->print("Missing fixed-fps argument, aborting.\n");
  856. goto error;
  857. }
  858. } else if (I->get() == "--print-fps") {
  859. print_fps = true;
  860. } else if (I->get() == "--profile-gpu") {
  861. profile_gpu = true;
  862. } else if (I->get() == "--disable-crash-handler") {
  863. OS::get_singleton()->disable_crash_handler();
  864. } else if (I->get() == "--skip-breakpoints") {
  865. skip_breakpoints = true;
  866. } else {
  867. main_args.push_back(I->get());
  868. }
  869. I = N;
  870. }
  871. #ifdef TOOLS_ENABLED
  872. if (editor && project_manager) {
  873. OS::get_singleton()->print(
  874. "Error: Command line arguments implied opening both editor and project manager, which is not possible. Aborting.\n");
  875. goto error;
  876. }
  877. #endif
  878. // Network file system needs to be configured before globals, since globals are based on the
  879. // 'project.godot' file which will only be available through the network if this is enabled
  880. FileAccessNetwork::configure();
  881. if (remotefs != "") {
  882. file_access_network_client = memnew(FileAccessNetworkClient);
  883. int port;
  884. if (remotefs.find(":") != -1) {
  885. port = remotefs.get_slicec(':', 1).to_int();
  886. remotefs = remotefs.get_slicec(':', 0);
  887. } else {
  888. port = 6010;
  889. }
  890. Error err = file_access_network_client->connect(remotefs, port, remotefs_pass);
  891. if (err) {
  892. OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i.\n", remotefs.utf8().get_data(), port);
  893. goto error;
  894. }
  895. FileAccess::make_default<FileAccessNetwork>(FileAccess::ACCESS_RESOURCES);
  896. }
  897. if (globals->setup(project_path, main_pack, upwards) == OK) {
  898. #ifdef TOOLS_ENABLED
  899. found_project = true;
  900. #endif
  901. } else {
  902. #ifdef TOOLS_ENABLED
  903. editor = false;
  904. #else
  905. 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";
  906. OS::get_singleton()->print("%s", error_msg.ascii().get_data());
  907. DisplayServer::get_singleton()->alert(error_msg);
  908. goto error;
  909. #endif
  910. }
  911. // Initialize user data dir.
  912. OS::get_singleton()->ensure_user_data_dir();
  913. GLOBAL_DEF("memory/limits/multithreaded_server/rid_pool_prealloc", 60);
  914. ProjectSettings::get_singleton()->set_custom_property_info("memory/limits/multithreaded_server/rid_pool_prealloc",
  915. PropertyInfo(Variant::INT,
  916. "memory/limits/multithreaded_server/rid_pool_prealloc",
  917. PROPERTY_HINT_RANGE,
  918. "0,500,1")); // No negative and limit to 500 due to crashes
  919. GLOBAL_DEF("network/limits/debugger/max_chars_per_second", 32768);
  920. ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_chars_per_second",
  921. PropertyInfo(Variant::INT,
  922. "network/limits/debugger/max_chars_per_second",
  923. PROPERTY_HINT_RANGE,
  924. "0, 4096, 1, or_greater"));
  925. GLOBAL_DEF("network/limits/debugger/max_queued_messages", 2048);
  926. ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_queued_messages",
  927. PropertyInfo(Variant::INT,
  928. "network/limits/debugger/max_queued_messages",
  929. PROPERTY_HINT_RANGE,
  930. "0, 8192, 1, or_greater"));
  931. GLOBAL_DEF("network/limits/debugger/max_errors_per_second", 400);
  932. ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_errors_per_second",
  933. PropertyInfo(Variant::INT,
  934. "network/limits/debugger/max_errors_per_second",
  935. PROPERTY_HINT_RANGE,
  936. "0, 200, 1, or_greater"));
  937. GLOBAL_DEF("network/limits/debugger/max_warnings_per_second", 400);
  938. ProjectSettings::get_singleton()->set_custom_property_info("network/limits/debugger/max_warnings_per_second",
  939. PropertyInfo(Variant::INT,
  940. "network/limits/debugger/max_warnings_per_second",
  941. PROPERTY_HINT_RANGE,
  942. "0, 200, 1, or_greater"));
  943. EngineDebugger::initialize(debug_uri, skip_breakpoints, breakpoints);
  944. #ifdef TOOLS_ENABLED
  945. if (editor) {
  946. packed_data->set_disabled(true);
  947. globals->set_disable_feature_overrides(true);
  948. }
  949. #endif
  950. #ifdef TOOLS_ENABLED
  951. if (editor) {
  952. Engine::get_singleton()->set_editor_hint(true);
  953. main_args.push_back("--editor");
  954. if (!init_windowed) {
  955. init_maximized = true;
  956. window_mode = DisplayServer::WINDOW_MODE_MAXIMIZED;
  957. }
  958. }
  959. if (!project_manager && !editor) {
  960. // If we didn't find a project, we fall back to the project manager.
  961. project_manager = !found_project && !cmdline_tool;
  962. }
  963. #endif
  964. GLOBAL_DEF("debug/file_logging/enable_file_logging", false);
  965. // Only file logging by default on desktop platforms as logs can't be
  966. // accessed easily on mobile/Web platforms (if at all).
  967. // This also prevents logs from being created for the editor instance, as feature tags
  968. // are disabled while in the editor (even if they should logically apply).
  969. GLOBAL_DEF("debug/file_logging/enable_file_logging.pc", true);
  970. GLOBAL_DEF("debug/file_logging/log_path", "user://logs/godot.log");
  971. GLOBAL_DEF("debug/file_logging/max_log_files", 5);
  972. ProjectSettings::get_singleton()->set_custom_property_info("debug/file_logging/max_log_files",
  973. PropertyInfo(Variant::INT,
  974. "debug/file_logging/max_log_files",
  975. PROPERTY_HINT_RANGE,
  976. "0,20,1,or_greater")); //no negative numbers
  977. if (!project_manager && !editor && FileAccess::get_create_func(FileAccess::ACCESS_USERDATA) &&
  978. GLOBAL_GET("debug/file_logging/enable_file_logging")) {
  979. // Don't create logs for the project manager as they would be written to
  980. // the current working directory, which is inconvenient.
  981. String base_path = GLOBAL_GET("debug/file_logging/log_path");
  982. int max_files = GLOBAL_GET("debug/file_logging/max_log_files");
  983. OS::get_singleton()->add_logger(memnew(RotatedFileLogger(base_path, max_files)));
  984. }
  985. if (main_args.size() == 0 && String(GLOBAL_GET("application/run/main_scene")) == "") {
  986. #ifdef TOOLS_ENABLED
  987. if (!editor && !project_manager) {
  988. #endif
  989. OS::get_singleton()->print("Error: Can't run project: no main scene defined.\n");
  990. goto error;
  991. #ifdef TOOLS_ENABLED
  992. }
  993. #endif
  994. }
  995. if (editor || project_manager) {
  996. Engine::get_singleton()->set_editor_hint(true);
  997. use_custom_res = false;
  998. input_map->load_default(); //keys for editor
  999. } else {
  1000. input_map->load_from_project_settings(); //keys for game
  1001. }
  1002. if (bool(ProjectSettings::get_singleton()->get("application/run/disable_stdout"))) {
  1003. quiet_stdout = true;
  1004. }
  1005. if (bool(ProjectSettings::get_singleton()->get("application/run/disable_stderr"))) {
  1006. _print_error_enabled = false;
  1007. };
  1008. if (quiet_stdout) {
  1009. _print_line_enabled = false;
  1010. }
  1011. Logger::set_flush_stdout_on_print(ProjectSettings::get_singleton()->get("application/run/flush_stdout_on_print"));
  1012. OS::get_singleton()->set_cmdline(execpath, main_args);
  1013. register_core_extensions(); //before display
  1014. GLOBAL_DEF("rendering/driver/driver_name", "Vulkan");
  1015. ProjectSettings::get_singleton()->set_custom_property_info("rendering/driver/driver_name",
  1016. PropertyInfo(Variant::STRING,
  1017. "rendering/driver/driver_name",
  1018. PROPERTY_HINT_ENUM, "Vulkan"));
  1019. if (display_driver == "") {
  1020. display_driver = GLOBAL_GET("rendering/driver/driver_name");
  1021. }
  1022. GLOBAL_DEF_BASIC("display/window/size/width", 1024);
  1023. ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/width",
  1024. PropertyInfo(Variant::INT, "display/window/size/width",
  1025. PROPERTY_HINT_RANGE,
  1026. "0,7680,or_greater")); // 8K resolution
  1027. GLOBAL_DEF_BASIC("display/window/size/height", 600);
  1028. ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/height",
  1029. PropertyInfo(Variant::INT, "display/window/size/height",
  1030. PROPERTY_HINT_RANGE,
  1031. "0,4320,or_greater")); // 8K resolution
  1032. GLOBAL_DEF_BASIC("display/window/size/resizable", true);
  1033. GLOBAL_DEF_BASIC("display/window/size/borderless", false);
  1034. GLOBAL_DEF_BASIC("display/window/size/fullscreen", false);
  1035. GLOBAL_DEF("display/window/size/always_on_top", false);
  1036. GLOBAL_DEF("display/window/size/test_width", 0);
  1037. ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/test_width",
  1038. PropertyInfo(Variant::INT,
  1039. "display/window/size/test_width",
  1040. PROPERTY_HINT_RANGE,
  1041. "0,7680,or_greater")); // 8K resolution
  1042. GLOBAL_DEF("display/window/size/test_height", 0);
  1043. ProjectSettings::get_singleton()->set_custom_property_info("display/window/size/test_height",
  1044. PropertyInfo(Variant::INT,
  1045. "display/window/size/test_height",
  1046. PROPERTY_HINT_RANGE,
  1047. "0,4320,or_greater")); // 8K resolution
  1048. if (use_custom_res) {
  1049. if (!force_res) {
  1050. window_size.width = GLOBAL_GET("display/window/size/width");
  1051. window_size.height = GLOBAL_GET("display/window/size/height");
  1052. if (globals->has_setting("display/window/size/test_width") &&
  1053. globals->has_setting("display/window/size/test_height")) {
  1054. int tw = globals->get("display/window/size/test_width");
  1055. if (tw > 0) {
  1056. window_size.width = tw;
  1057. }
  1058. int th = globals->get("display/window/size/test_height");
  1059. if (th > 0) {
  1060. window_size.height = th;
  1061. }
  1062. }
  1063. }
  1064. if (!bool(GLOBAL_GET("display/window/size/resizable"))) {
  1065. window_flags |= DisplayServer::WINDOW_FLAG_RESIZE_DISABLED_BIT;
  1066. }
  1067. if (bool(GLOBAL_GET("display/window/size/borderless"))) {
  1068. window_flags |= DisplayServer::WINDOW_FLAG_BORDERLESS_BIT;
  1069. }
  1070. if (bool(GLOBAL_GET("display/window/size/fullscreen"))) {
  1071. window_mode = DisplayServer::WINDOW_MODE_FULLSCREEN;
  1072. }
  1073. if (bool(GLOBAL_GET("display/window/size/always_on_top"))) {
  1074. window_flags |= DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP_BIT;
  1075. }
  1076. }
  1077. GLOBAL_DEF("internationalization/rendering/force_right_to_left_layout_direction", false);
  1078. GLOBAL_DEF("internationalization/locale/include_text_server_data", false);
  1079. if (!force_lowdpi) {
  1080. OS::get_singleton()->_allow_hidpi = GLOBAL_DEF("display/window/dpi/allow_hidpi", false);
  1081. }
  1082. /* todo restore
  1083. OS::get_singleton()->_allow_layered = GLOBAL_DEF("display/window/per_pixel_transparency/allowed", false);
  1084. video_mode.layered = GLOBAL_DEF("display/window/per_pixel_transparency/enabled", false);
  1085. */
  1086. if (editor || project_manager) {
  1087. // The editor and project manager always detect and use hiDPI if needed
  1088. OS::get_singleton()->_allow_hidpi = true;
  1089. OS::get_singleton()->_allow_layered = false;
  1090. }
  1091. OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
  1092. if (rtm == -1) {
  1093. rtm = GLOBAL_DEF("rendering/driver/threads/thread_model", OS::RENDER_THREAD_SAFE);
  1094. }
  1095. if (rtm >= 0 && rtm < 3) {
  1096. #ifdef NO_THREADS
  1097. rtm = OS::RENDER_THREAD_UNSAFE; // No threads available on this platform.
  1098. #else
  1099. if (editor) {
  1100. rtm = OS::RENDER_THREAD_SAFE;
  1101. }
  1102. #endif
  1103. OS::get_singleton()->_render_thread_mode = OS::RenderThreadMode(rtm);
  1104. }
  1105. /* Determine audio and video drivers */
  1106. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1107. if (display_driver == DisplayServer::get_create_function_name(i)) {
  1108. display_driver_idx = i;
  1109. break;
  1110. }
  1111. }
  1112. if (display_driver_idx < 0) {
  1113. display_driver_idx = 0;
  1114. }
  1115. if (audio_driver == "") { // specified in project.godot
  1116. audio_driver = GLOBAL_DEF_RST_NOVAL("audio/driver/driver", AudioDriverManager::get_driver(0)->get_name());
  1117. }
  1118. for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
  1119. if (audio_driver == AudioDriverManager::get_driver(i)->get_name()) {
  1120. audio_driver_idx = i;
  1121. break;
  1122. }
  1123. }
  1124. if (audio_driver_idx < 0) {
  1125. audio_driver_idx = 0;
  1126. }
  1127. {
  1128. window_orientation = DisplayServer::ScreenOrientation(int(GLOBAL_DEF_BASIC("display/window/handheld/orientation", DisplayServer::ScreenOrientation::SCREEN_LANDSCAPE)));
  1129. }
  1130. {
  1131. window_vsync_mode = DisplayServer::VSyncMode(int(GLOBAL_DEF("display/window/vsync/vsync_mode", DisplayServer::VSyncMode::VSYNC_ENABLED)));
  1132. }
  1133. Engine::get_singleton()->set_iterations_per_second(GLOBAL_DEF_BASIC("physics/common/physics_fps", 60));
  1134. ProjectSettings::get_singleton()->set_custom_property_info("physics/common/physics_fps",
  1135. PropertyInfo(Variant::INT, "physics/common/physics_fps",
  1136. PROPERTY_HINT_RANGE, "1,1000,1"));
  1137. Engine::get_singleton()->set_physics_jitter_fix(GLOBAL_DEF("physics/common/physics_jitter_fix", 0.5));
  1138. Engine::get_singleton()->set_target_fps(GLOBAL_DEF("debug/settings/fps/force_fps", 0));
  1139. ProjectSettings::get_singleton()->set_custom_property_info("debug/settings/fps/force_fps",
  1140. PropertyInfo(Variant::INT,
  1141. "debug/settings/fps/force_fps",
  1142. PROPERTY_HINT_RANGE, "0,1000,1"));
  1143. GLOBAL_DEF("debug/settings/stdout/print_fps", false);
  1144. GLOBAL_DEF("debug/settings/stdout/verbose_stdout", false);
  1145. if (!OS::get_singleton()->_verbose_stdout) { // Not manually overridden.
  1146. OS::get_singleton()->_verbose_stdout = GLOBAL_GET("debug/settings/stdout/verbose_stdout");
  1147. }
  1148. if (frame_delay == 0) {
  1149. frame_delay = GLOBAL_DEF("application/run/frame_delay_msec", 0);
  1150. ProjectSettings::get_singleton()->set_custom_property_info("application/run/frame_delay_msec",
  1151. PropertyInfo(Variant::INT,
  1152. "application/run/frame_delay_msec",
  1153. PROPERTY_HINT_RANGE,
  1154. "0,100,1,or_greater")); // No negative numbers
  1155. }
  1156. OS::get_singleton()->set_low_processor_usage_mode(GLOBAL_DEF("application/run/low_processor_mode", false));
  1157. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(
  1158. GLOBAL_DEF("application/run/low_processor_mode_sleep_usec", 6900)); // Roughly 144 FPS
  1159. ProjectSettings::get_singleton()->set_custom_property_info("application/run/low_processor_mode_sleep_usec",
  1160. PropertyInfo(Variant::INT,
  1161. "application/run/low_processor_mode_sleep_usec",
  1162. PROPERTY_HINT_RANGE,
  1163. "0,33200,1,or_greater")); // No negative numbers
  1164. GLOBAL_DEF("display/window/ios/hide_home_indicator", true);
  1165. GLOBAL_DEF("input_devices/pointing/ios/touch_delay", 0.150);
  1166. Engine::get_singleton()->set_frame_delay(frame_delay);
  1167. message_queue = memnew(MessageQueue);
  1168. if (p_second_phase) {
  1169. return setup2();
  1170. }
  1171. return OK;
  1172. error:
  1173. text_driver = "";
  1174. display_driver = "";
  1175. audio_driver = "";
  1176. tablet_driver = "";
  1177. project_path = "";
  1178. args.clear();
  1179. main_args.clear();
  1180. if (show_help) {
  1181. print_help(execpath);
  1182. }
  1183. EngineDebugger::deinitialize();
  1184. if (performance) {
  1185. memdelete(performance);
  1186. }
  1187. if (input_map) {
  1188. memdelete(input_map);
  1189. }
  1190. if (time_singleton) {
  1191. memdelete(time_singleton);
  1192. }
  1193. if (translation_server) {
  1194. memdelete(translation_server);
  1195. }
  1196. if (globals) {
  1197. memdelete(globals);
  1198. }
  1199. if (engine) {
  1200. memdelete(engine);
  1201. }
  1202. if (packed_data) {
  1203. memdelete(packed_data);
  1204. }
  1205. if (file_access_network_client) {
  1206. memdelete(file_access_network_client);
  1207. }
  1208. unregister_core_driver_types();
  1209. unregister_core_types();
  1210. OS::get_singleton()->_cmdline.clear();
  1211. if (message_queue) {
  1212. memdelete(message_queue);
  1213. }
  1214. OS::get_singleton()->finalize_core();
  1215. locale = String();
  1216. return ERR_INVALID_PARAMETER;
  1217. }
  1218. Error Main::setup2(Thread::ID p_main_tid_override) {
  1219. preregister_module_types();
  1220. preregister_server_types();
  1221. // Print engine name and version
  1222. print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
  1223. #if !defined(NO_THREADS)
  1224. if (p_main_tid_override) {
  1225. Thread::main_thread_id = p_main_tid_override;
  1226. }
  1227. #endif
  1228. #ifdef TOOLS_ENABLED
  1229. if (editor || project_manager || cmdline_tool) {
  1230. EditorPaths::create();
  1231. }
  1232. #endif
  1233. /* Determine text driver */
  1234. if (text_driver == "") {
  1235. text_driver = GLOBAL_GET("internationalization/rendering/text_driver");
  1236. }
  1237. if (text_driver != "") {
  1238. /* Load user selected text server. */
  1239. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  1240. if (text_driver == TextServerManager::get_interface_name(i)) {
  1241. text_driver_idx = i;
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. if (text_driver_idx < 0) {
  1247. /* If not selected, use one with the most features available. */
  1248. int max_features = 0;
  1249. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  1250. uint32_t ftrs = TextServerManager::get_interface_features(i);
  1251. int features = 0;
  1252. while (ftrs) {
  1253. features += ftrs & 1;
  1254. ftrs >>= 1;
  1255. }
  1256. if (features >= max_features) {
  1257. max_features = features;
  1258. text_driver_idx = i;
  1259. }
  1260. }
  1261. }
  1262. print_verbose("Using \"" + TextServerManager::get_interface_name(text_driver_idx) + "\" text server...");
  1263. /* Initialize Text Server */
  1264. {
  1265. tsman = memnew(TextServerManager);
  1266. Error err;
  1267. TextServer *text_server = TextServerManager::initialize(text_driver_idx, err);
  1268. if (err != OK || text_server == nullptr) {
  1269. for (int i = 0; i < TextServerManager::get_interface_count(); i++) {
  1270. if (i == text_driver_idx) {
  1271. continue; //don't try the same twice
  1272. }
  1273. text_server = TextServerManager::initialize(i, err);
  1274. if (err == OK && text_server != nullptr) {
  1275. break;
  1276. }
  1277. }
  1278. }
  1279. if (err != OK || text_server == nullptr) {
  1280. ERR_PRINT("Unable to create TextServer, all text drivers failed.");
  1281. return err;
  1282. }
  1283. }
  1284. /* Initialize Input */
  1285. input = memnew(Input);
  1286. /* Initialize Display Server */
  1287. {
  1288. String rendering_driver; // temp broken
  1289. Error err;
  1290. display_server = DisplayServer::create(display_driver_idx, rendering_driver, window_mode, window_vsync_mode, window_flags, window_size, err);
  1291. if (err != OK || display_server == nullptr) {
  1292. //ok i guess we can't use this display server, try other ones
  1293. for (int i = 0; i < DisplayServer::get_create_function_count(); i++) {
  1294. if (i == display_driver_idx) {
  1295. continue; //don't try the same twice
  1296. }
  1297. display_server = DisplayServer::create(i, rendering_driver, window_mode, window_vsync_mode, window_flags, window_size, err);
  1298. if (err == OK && display_server != nullptr) {
  1299. break;
  1300. }
  1301. }
  1302. }
  1303. if (err != OK || display_server == nullptr) {
  1304. ERR_PRINT("Unable to create DisplayServer, all display drivers failed.");
  1305. return err;
  1306. }
  1307. }
  1308. if (display_server->has_feature(DisplayServer::FEATURE_ORIENTATION)) {
  1309. display_server->screen_set_orientation(window_orientation);
  1310. }
  1311. /* Initialize Pen Tablet Driver */
  1312. {
  1313. GLOBAL_DEF_RST_NOVAL("input_devices/pen_tablet/driver", "");
  1314. GLOBAL_DEF_RST_NOVAL("input_devices/pen_tablet/driver.Windows", "");
  1315. ProjectSettings::get_singleton()->set_custom_property_info("input_devices/pen_tablet/driver.Windows", PropertyInfo(Variant::STRING, "input_devices/pen_tablet/driver.Windows", PROPERTY_HINT_ENUM, "wintab,winink"));
  1316. }
  1317. if (tablet_driver == "") { // specified in project.godot
  1318. tablet_driver = GLOBAL_GET("input_devices/pen_tablet/driver");
  1319. if (tablet_driver == "") {
  1320. tablet_driver = DisplayServer::get_singleton()->tablet_get_driver_name(0);
  1321. }
  1322. }
  1323. for (int i = 0; i < DisplayServer::get_singleton()->tablet_get_driver_count(); i++) {
  1324. if (tablet_driver == DisplayServer::get_singleton()->tablet_get_driver_name(i)) {
  1325. DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(i));
  1326. break;
  1327. }
  1328. }
  1329. if (DisplayServer::get_singleton()->tablet_get_current_driver() == "") {
  1330. DisplayServer::get_singleton()->tablet_set_current_driver(DisplayServer::get_singleton()->tablet_get_driver_name(0));
  1331. }
  1332. print_verbose("Using \"" + tablet_driver + "\" pen tablet driver...");
  1333. /* Initialize Rendering Server */
  1334. rendering_server = memnew(RenderingServerDefault(OS::get_singleton()->get_render_thread_mode() == OS::RENDER_SEPARATE_THREAD));
  1335. rendering_server->init();
  1336. rendering_server->set_render_loop_enabled(!disable_render_loop);
  1337. if (profile_gpu) {
  1338. rendering_server->set_print_gpu_profile(true);
  1339. }
  1340. OS::get_singleton()->initialize_joypads();
  1341. /* Initialize Audio Driver */
  1342. AudioDriverManager::initialize(audio_driver_idx);
  1343. print_line(" "); //add a blank line for readability
  1344. if (init_use_custom_pos) {
  1345. display_server->window_set_position(init_custom_pos);
  1346. }
  1347. // right moment to create and initialize the audio server
  1348. audio_server = memnew(AudioServer);
  1349. audio_server->init();
  1350. // also init our xr_server from here
  1351. xr_server = memnew(XRServer);
  1352. register_core_singletons();
  1353. MAIN_PRINT("Main: Setup Logo");
  1354. #if defined(JAVASCRIPT_ENABLED) || defined(ANDROID_ENABLED)
  1355. bool show_logo = false;
  1356. #else
  1357. bool show_logo = true;
  1358. #endif
  1359. if (init_screen != -1) {
  1360. DisplayServer::get_singleton()->window_set_current_screen(init_screen);
  1361. }
  1362. if (init_windowed) {
  1363. //do none..
  1364. } else if (init_maximized) {
  1365. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_MAXIMIZED);
  1366. } else if (init_fullscreen) {
  1367. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_FULLSCREEN);
  1368. }
  1369. if (init_always_on_top) {
  1370. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WINDOW_FLAG_ALWAYS_ON_TOP, true);
  1371. }
  1372. if (allow_focus_steal_pid) {
  1373. DisplayServer::get_singleton()->enable_for_stealing_focus(allow_focus_steal_pid);
  1374. }
  1375. register_server_types();
  1376. MAIN_PRINT("Main: Load Boot Image");
  1377. Color clear = GLOBAL_DEF("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3));
  1378. RenderingServer::get_singleton()->set_default_clear_color(clear);
  1379. if (show_logo) { //boot logo!
  1380. String boot_logo_path = GLOBAL_DEF("application/boot_splash/image", String());
  1381. bool boot_logo_scale = GLOBAL_DEF("application/boot_splash/fullsize", true);
  1382. bool boot_logo_filter = GLOBAL_DEF("application/boot_splash/use_filter", true);
  1383. ProjectSettings::get_singleton()->set_custom_property_info("application/boot_splash/image",
  1384. PropertyInfo(Variant::STRING,
  1385. "application/boot_splash/image",
  1386. PROPERTY_HINT_FILE, "*.png"));
  1387. Ref<Image> boot_logo;
  1388. boot_logo_path = boot_logo_path.strip_edges();
  1389. if (boot_logo_path != String()) {
  1390. boot_logo.instantiate();
  1391. Error load_err = ImageLoader::load_image(boot_logo_path, boot_logo);
  1392. if (load_err) {
  1393. ERR_PRINT("Non-existing or invalid boot splash at '" + boot_logo_path + "'. Loading default splash.");
  1394. }
  1395. }
  1396. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  1397. const Color boot_bg_color =
  1398. GLOBAL_DEF("application/boot_splash/bg_color",
  1399. (editor || project_manager) ? boot_splash_editor_bg_color : boot_splash_bg_color);
  1400. #else
  1401. const Color boot_bg_color = GLOBAL_DEF("application/boot_splash/bg_color", boot_splash_bg_color);
  1402. #endif
  1403. if (boot_logo.is_valid()) {
  1404. RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, boot_logo_scale,
  1405. boot_logo_filter);
  1406. } else {
  1407. #ifndef NO_DEFAULT_BOOT_LOGO
  1408. MAIN_PRINT("Main: Create bootsplash");
  1409. #if defined(TOOLS_ENABLED) && !defined(NO_EDITOR_SPLASH)
  1410. Ref<Image> splash = (editor || project_manager) ? memnew(Image(boot_splash_editor_png)) : memnew(Image(boot_splash_png));
  1411. #else
  1412. Ref<Image> splash = memnew(Image(boot_splash_png));
  1413. #endif
  1414. MAIN_PRINT("Main: ClearColor");
  1415. RenderingServer::get_singleton()->set_default_clear_color(boot_bg_color);
  1416. MAIN_PRINT("Main: Image");
  1417. RenderingServer::get_singleton()->set_boot_image(splash, boot_bg_color, false);
  1418. #endif
  1419. }
  1420. #ifdef TOOLS_ENABLED
  1421. Ref<Image> icon = memnew(Image(app_icon_png));
  1422. DisplayServer::get_singleton()->set_icon(icon);
  1423. #endif
  1424. }
  1425. MAIN_PRINT("Main: DCC");
  1426. RenderingServer::get_singleton()->set_default_clear_color(
  1427. GLOBAL_DEF("rendering/environment/defaults/default_clear_color", Color(0.3, 0.3, 0.3)));
  1428. GLOBAL_DEF("application/config/icon", String());
  1429. ProjectSettings::get_singleton()->set_custom_property_info("application/config/icon",
  1430. PropertyInfo(Variant::STRING, "application/config/icon",
  1431. PROPERTY_HINT_FILE, "*.png,*.webp,*.svg,*.svgz"));
  1432. GLOBAL_DEF("application/config/macos_native_icon", String());
  1433. ProjectSettings::get_singleton()->set_custom_property_info("application/config/macos_native_icon",
  1434. PropertyInfo(Variant::STRING,
  1435. "application/config/macos_native_icon",
  1436. PROPERTY_HINT_FILE, "*.icns"));
  1437. GLOBAL_DEF("application/config/windows_native_icon", String());
  1438. ProjectSettings::get_singleton()->set_custom_property_info("application/config/windows_native_icon",
  1439. PropertyInfo(Variant::STRING,
  1440. "application/config/windows_native_icon",
  1441. PROPERTY_HINT_FILE, "*.ico"));
  1442. Input *id = Input::get_singleton();
  1443. if (id) {
  1444. if (bool(GLOBAL_DEF("input_devices/pointing/emulate_touch_from_mouse", false)) &&
  1445. !(editor || project_manager)) {
  1446. bool found_touchscreen = false;
  1447. for (int i = 0; i < DisplayServer::get_singleton()->get_screen_count(); i++) {
  1448. if (DisplayServer::get_singleton()->screen_is_touchscreen(i)) {
  1449. found_touchscreen = true;
  1450. }
  1451. }
  1452. if (!found_touchscreen) {
  1453. //only if no touchscreen ui hint, set emulation
  1454. id->set_emulate_touch_from_mouse(true);
  1455. }
  1456. }
  1457. id->set_emulate_mouse_from_touch(bool(GLOBAL_DEF("input_devices/pointing/emulate_mouse_from_touch", true)));
  1458. }
  1459. MAIN_PRINT("Main: Load Translations and Remaps");
  1460. translation_server->setup(); //register translations, load them, etc.
  1461. if (locale != "") {
  1462. translation_server->set_locale(locale);
  1463. }
  1464. translation_server->load_translations();
  1465. ResourceLoader::load_translation_remaps(); //load remaps for resources
  1466. ResourceLoader::load_path_remaps();
  1467. MAIN_PRINT("Main: Load Scene Types");
  1468. register_scene_types();
  1469. #ifdef TOOLS_ENABLED
  1470. ClassDB::set_current_api(ClassDB::API_EDITOR);
  1471. EditorNode::register_editor_types();
  1472. ClassDB::set_current_api(ClassDB::API_CORE);
  1473. #endif
  1474. MAIN_PRINT("Main: Load Modules, Physics, Drivers, Scripts");
  1475. register_platform_apis();
  1476. register_module_types();
  1477. GLOBAL_DEF("display/mouse_cursor/custom_image", String());
  1478. GLOBAL_DEF("display/mouse_cursor/custom_image_hotspot", Vector2());
  1479. GLOBAL_DEF("display/mouse_cursor/tooltip_position_offset", Point2(10, 10));
  1480. ProjectSettings::get_singleton()->set_custom_property_info("display/mouse_cursor/custom_image",
  1481. PropertyInfo(Variant::STRING,
  1482. "display/mouse_cursor/custom_image",
  1483. PROPERTY_HINT_FILE, "*.png,*.webp"));
  1484. if (String(ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image")) != String()) {
  1485. Ref<Texture2D> cursor = ResourceLoader::load(
  1486. ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image"));
  1487. if (cursor.is_valid()) {
  1488. Vector2 hotspot = ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image_hotspot");
  1489. Input::get_singleton()->set_custom_mouse_cursor(cursor, Input::CURSOR_ARROW, hotspot);
  1490. }
  1491. }
  1492. camera_server = CameraServer::create();
  1493. initialize_physics();
  1494. initialize_navigation_server();
  1495. register_server_singletons();
  1496. register_driver_types();
  1497. // This loads global classes, so it must happen before custom loaders and savers are registered
  1498. ScriptServer::init_languages();
  1499. audio_server->load_default_bus_layout();
  1500. if (use_debug_profiler && EngineDebugger::is_active()) {
  1501. // Start the "scripts" profiler, used in local debugging.
  1502. // We could add more, and make the CLI arg require a comma-separated list of profilers.
  1503. EngineDebugger::get_singleton()->profiler_enable("scripts", true);
  1504. }
  1505. if (!project_manager) {
  1506. // If not running the project manager, and now that the engine is
  1507. // able to load resources, load the global shader variables.
  1508. // If running on editor, don't load the textures because the editor
  1509. // may want to import them first. Editor will reload those later.
  1510. rendering_server->global_variables_load_settings(!editor);
  1511. }
  1512. _start_success = true;
  1513. locale = String();
  1514. ClassDB::set_current_api(ClassDB::API_NONE); //no more APIs are registered at this point
  1515. print_verbose("CORE API HASH: " + uitos(ClassDB::get_api_hash(ClassDB::API_CORE)));
  1516. print_verbose("EDITOR API HASH: " + uitos(ClassDB::get_api_hash(ClassDB::API_EDITOR)));
  1517. MAIN_PRINT("Main: Done");
  1518. return OK;
  1519. }
  1520. // everything the main loop needs to know about frame timings
  1521. static MainTimerSync main_timer_sync;
  1522. bool Main::start() {
  1523. ERR_FAIL_COND_V(!_start_success, false);
  1524. bool hasicon = false;
  1525. String positional_arg;
  1526. String game_path;
  1527. String script;
  1528. bool check_only = false;
  1529. #ifdef TOOLS_ENABLED
  1530. String doc_tool_path;
  1531. bool doc_base = true;
  1532. String _export_preset;
  1533. bool export_debug = false;
  1534. bool export_pack_only = false;
  1535. #endif
  1536. main_timer_sync.init(OS::get_singleton()->get_ticks_usec());
  1537. List<String> args = OS::get_singleton()->get_cmdline_args();
  1538. for (int i = 0; i < args.size(); i++) {
  1539. // First check parameters that do not have an argument to the right.
  1540. // Doctest Unit Testing Handler
  1541. // Designed to override and pass arguments to the unit test handler.
  1542. if (args[i] == "--check-only") {
  1543. check_only = true;
  1544. #ifdef TOOLS_ENABLED
  1545. } else if (args[i] == "--no-docbase") {
  1546. doc_base = false;
  1547. } else if (args[i] == "-e" || args[i] == "--editor") {
  1548. editor = true;
  1549. } else if (args[i] == "-p" || args[i] == "--project-manager") {
  1550. project_manager = true;
  1551. #endif
  1552. } else if (args[i].length() && args[i][0] != '-' && positional_arg == "") {
  1553. positional_arg = args[i];
  1554. if (args[i].ends_with(".scn") ||
  1555. args[i].ends_with(".tscn") ||
  1556. args[i].ends_with(".escn") ||
  1557. args[i].ends_with(".res") ||
  1558. args[i].ends_with(".tres")) {
  1559. // Only consider the positional argument to be a scene path if it ends with
  1560. // a file extension associated with Godot scenes. This makes it possible
  1561. // for projects to parse command-line arguments for custom CLI arguments
  1562. // or other file extensions without trouble. This can be used to implement
  1563. // "drag-and-drop onto executable" logic, which can prove helpful
  1564. // for non-game applications.
  1565. game_path = args[i];
  1566. }
  1567. }
  1568. // Then parameters that have an argument to the right.
  1569. else if (i < (args.size() - 1)) {
  1570. bool parsed_pair = true;
  1571. if (args[i] == "-s" || args[i] == "--script") {
  1572. script = args[i + 1];
  1573. #ifdef TOOLS_ENABLED
  1574. } else if (args[i] == "--doctool") {
  1575. doc_tool_path = args[i + 1];
  1576. if (doc_tool_path.begins_with("-")) {
  1577. // Assuming other command line arg, so default to cwd.
  1578. doc_tool_path = ".";
  1579. parsed_pair = false;
  1580. }
  1581. } else if (args[i] == "--export") {
  1582. editor = true; //needs editor
  1583. _export_preset = args[i + 1];
  1584. } else if (args[i] == "--export-debug") {
  1585. editor = true; //needs editor
  1586. _export_preset = args[i + 1];
  1587. export_debug = true;
  1588. } else if (args[i] == "--export-pack") {
  1589. editor = true;
  1590. _export_preset = args[i + 1];
  1591. export_pack_only = true;
  1592. #endif
  1593. } else {
  1594. // The parameter does not match anything known, don't skip the next argument
  1595. parsed_pair = false;
  1596. }
  1597. if (parsed_pair) {
  1598. i++;
  1599. }
  1600. }
  1601. #ifdef TOOLS_ENABLED
  1602. // Handle case where no path is given to --doctool.
  1603. else if (args[i] == "--doctool") {
  1604. doc_tool_path = ".";
  1605. }
  1606. #endif
  1607. }
  1608. #ifdef TOOLS_ENABLED
  1609. if (doc_tool_path != "") {
  1610. // Needed to instance editor-only classes for their default values
  1611. Engine::get_singleton()->set_editor_hint(true);
  1612. {
  1613. DirAccessRef da = DirAccess::open(doc_tool_path);
  1614. ERR_FAIL_COND_V_MSG(!da, false, "Argument supplied to --doctool must be a valid directory path.");
  1615. }
  1616. #ifndef MODULE_MONO_ENABLED
  1617. // Hack to define Mono-specific project settings even on non-Mono builds,
  1618. // so that we don't lose their descriptions and default values in DocData.
  1619. // Default values should be synced with mono_gd/gd_mono.cpp.
  1620. GLOBAL_DEF("mono/debugger_agent/port", 23685);
  1621. GLOBAL_DEF("mono/debugger_agent/wait_for_debugger", false);
  1622. GLOBAL_DEF("mono/debugger_agent/wait_timeout", 3000);
  1623. GLOBAL_DEF("mono/profiler/args", "log:calls,alloc,sample,output=output.mlpd");
  1624. GLOBAL_DEF("mono/profiler/enabled", false);
  1625. GLOBAL_DEF("mono/unhandled_exception_policy", 0);
  1626. // From editor/csharp_project.cpp.
  1627. GLOBAL_DEF("mono/project/auto_update_project", true);
  1628. #endif
  1629. DocTools doc;
  1630. doc.generate(doc_base);
  1631. DocTools docsrc;
  1632. Map<String, String> doc_data_classes;
  1633. Set<String> checked_paths;
  1634. print_line("Loading docs...");
  1635. for (int i = 0; i < _doc_data_class_path_count; i++) {
  1636. // Custom modules are always located by absolute path.
  1637. String path = _doc_data_class_paths[i].path;
  1638. if (path.is_rel_path()) {
  1639. path = doc_tool_path.plus_file(path);
  1640. }
  1641. String name = _doc_data_class_paths[i].name;
  1642. doc_data_classes[name] = path;
  1643. if (!checked_paths.has(path)) {
  1644. checked_paths.insert(path);
  1645. // Create the module documentation directory if it doesn't exist
  1646. DirAccess *da = DirAccess::create_for_path(path);
  1647. da->make_dir_recursive(path);
  1648. memdelete(da);
  1649. docsrc.load_classes(path);
  1650. print_line("Loading docs from: " + path);
  1651. }
  1652. }
  1653. String index_path = doc_tool_path.plus_file("doc/classes");
  1654. // Create the main documentation directory if it doesn't exist
  1655. DirAccess *da = DirAccess::create_for_path(index_path);
  1656. da->make_dir_recursive(index_path);
  1657. memdelete(da);
  1658. docsrc.load_classes(index_path);
  1659. checked_paths.insert(index_path);
  1660. print_line("Loading docs from: " + index_path);
  1661. print_line("Merging docs...");
  1662. doc.merge_from(docsrc);
  1663. for (Set<String>::Element *E = checked_paths.front(); E; E = E->next()) {
  1664. print_line("Erasing old docs at: " + E->get());
  1665. DocTools::erase_classes(E->get());
  1666. }
  1667. print_line("Generating new docs...");
  1668. doc.save_classes(index_path, doc_data_classes);
  1669. return false;
  1670. }
  1671. if (dump_extension_api) {
  1672. NativeExtensionAPIDump::generate_extension_json_file("extension_api.json");
  1673. return false;
  1674. }
  1675. #endif
  1676. if (script == "" && game_path == "" && String(GLOBAL_GET("application/run/main_scene")) != "") {
  1677. game_path = GLOBAL_GET("application/run/main_scene");
  1678. }
  1679. #ifdef TOOLS_ENABLED
  1680. if (!editor && !project_manager && !cmdline_tool && script == "" && game_path == "") {
  1681. // If we end up here, it means we didn't manage to detect what we want to run.
  1682. // Let's throw an error gently. The code leading to this is pretty brittle so
  1683. // this might end up triggered by valid usage, in which case we'll have to
  1684. // fine-tune further.
  1685. ERR_FAIL_V_MSG(false, "Couldn't detect whether to run the editor, the project manager or a specific project. Aborting.");
  1686. }
  1687. #endif
  1688. MainLoop *main_loop = nullptr;
  1689. if (editor) {
  1690. main_loop = memnew(SceneTree);
  1691. }
  1692. String main_loop_type = GLOBAL_DEF("application/run/main_loop_type", "SceneTree");
  1693. if (script != "") {
  1694. Ref<Script> script_res = ResourceLoader::load(script);
  1695. ERR_FAIL_COND_V_MSG(script_res.is_null(), false, "Can't load script: " + script);
  1696. if (check_only) {
  1697. if (!script_res->is_valid()) {
  1698. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  1699. }
  1700. return false;
  1701. }
  1702. if (script_res->can_instantiate()) {
  1703. StringName instance_type = script_res->get_instance_base_type();
  1704. Object *obj = ClassDB::instantiate(instance_type);
  1705. MainLoop *script_loop = Object::cast_to<MainLoop>(obj);
  1706. if (!script_loop) {
  1707. if (obj) {
  1708. memdelete(obj);
  1709. }
  1710. ERR_FAIL_V_MSG(false,
  1711. vformat("Can't load the script \"%s\" as it doesn't inherit from SceneTree or MainLoop.",
  1712. script));
  1713. }
  1714. script_loop->set_initialize_script(script_res);
  1715. main_loop = script_loop;
  1716. } else {
  1717. return false;
  1718. }
  1719. } else { // Not based on script path.
  1720. if (!editor && !ClassDB::class_exists(main_loop_type) && ScriptServer::is_global_class(main_loop_type)) {
  1721. String script_path = ScriptServer::get_global_class_path(main_loop_type);
  1722. Ref<Script> script_res = ResourceLoader::load(script_path);
  1723. StringName script_base = ScriptServer::get_global_class_native_base(main_loop_type);
  1724. Object *obj = ClassDB::instantiate(script_base);
  1725. MainLoop *script_loop = Object::cast_to<MainLoop>(obj);
  1726. if (!script_loop) {
  1727. if (obj) {
  1728. memdelete(obj);
  1729. }
  1730. DisplayServer::get_singleton()->alert("Error: Invalid MainLoop script base type: " + script_base);
  1731. ERR_FAIL_V_MSG(false, vformat("The global class %s does not inherit from SceneTree or MainLoop.", main_loop_type));
  1732. }
  1733. script_loop->set_initialize_script(script_res);
  1734. main_loop = script_loop;
  1735. }
  1736. }
  1737. if (!main_loop && main_loop_type == "") {
  1738. main_loop_type = "SceneTree";
  1739. }
  1740. if (!main_loop) {
  1741. if (!ClassDB::class_exists(main_loop_type)) {
  1742. DisplayServer::get_singleton()->alert("Error: MainLoop type doesn't exist: " + main_loop_type);
  1743. return false;
  1744. } else {
  1745. Object *ml = ClassDB::instantiate(main_loop_type);
  1746. ERR_FAIL_COND_V_MSG(!ml, false, "Can't instance MainLoop type.");
  1747. main_loop = Object::cast_to<MainLoop>(ml);
  1748. if (!main_loop) {
  1749. memdelete(ml);
  1750. ERR_FAIL_V_MSG(false, "Invalid MainLoop type.");
  1751. }
  1752. }
  1753. }
  1754. if (main_loop->is_class("SceneTree")) {
  1755. SceneTree *sml = Object::cast_to<SceneTree>(main_loop);
  1756. #ifdef DEBUG_ENABLED
  1757. if (debug_collisions) {
  1758. sml->set_debug_collisions_hint(true);
  1759. }
  1760. if (debug_navigation) {
  1761. sml->set_debug_navigation_hint(true);
  1762. }
  1763. #endif
  1764. bool embed_subwindows = GLOBAL_DEF("display/window/subwindows/embed_subwindows", false);
  1765. if (single_window || (!project_manager && !editor && embed_subwindows)) {
  1766. sml->get_root()->set_embed_subwindows_hint(true);
  1767. }
  1768. ResourceLoader::add_custom_loaders();
  1769. ResourceSaver::add_custom_savers();
  1770. if (!project_manager && !editor) { // game
  1771. if (game_path != "" || script != "") {
  1772. //autoload
  1773. Map<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  1774. //first pass, add the constants so they exist before any script is loaded
  1775. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  1776. const ProjectSettings::AutoloadInfo &info = E->get();
  1777. if (info.is_singleton) {
  1778. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1779. ScriptServer::get_language(i)->add_global_constant(info.name, Variant());
  1780. }
  1781. }
  1782. }
  1783. //second pass, load into global constants
  1784. List<Node *> to_add;
  1785. for (Map<StringName, ProjectSettings::AutoloadInfo>::Element *E = autoloads.front(); E; E = E->next()) {
  1786. const ProjectSettings::AutoloadInfo &info = E->get();
  1787. RES res = ResourceLoader::load(info.path);
  1788. ERR_CONTINUE_MSG(res.is_null(), "Can't autoload: " + info.path);
  1789. Node *n = nullptr;
  1790. if (res->is_class("PackedScene")) {
  1791. Ref<PackedScene> ps = res;
  1792. n = ps->instantiate();
  1793. } else if (res->is_class("Script")) {
  1794. Ref<Script> script_res = res;
  1795. StringName ibt = script_res->get_instance_base_type();
  1796. bool valid_type = ClassDB::is_parent_class(ibt, "Node");
  1797. ERR_CONTINUE_MSG(!valid_type, "Script does not inherit a Node: " + info.path);
  1798. Object *obj = ClassDB::instantiate(ibt);
  1799. ERR_CONTINUE_MSG(obj == nullptr,
  1800. "Cannot instance script for autoload, expected 'Node' inheritance, got: " +
  1801. String(ibt));
  1802. n = Object::cast_to<Node>(obj);
  1803. n->set_script(script_res);
  1804. }
  1805. ERR_CONTINUE_MSG(!n, "Path in autoload not a node or script: " + info.path);
  1806. n->set_name(info.name);
  1807. //defer so references are all valid on _ready()
  1808. to_add.push_back(n);
  1809. if (info.is_singleton) {
  1810. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  1811. ScriptServer::get_language(i)->add_global_constant(info.name, n);
  1812. }
  1813. }
  1814. }
  1815. for (List<Node *>::Element *E = to_add.front(); E; E = E->next()) {
  1816. sml->get_root()->add_child(E->get());
  1817. }
  1818. }
  1819. }
  1820. #ifdef TOOLS_ENABLED
  1821. EditorNode *editor_node = nullptr;
  1822. if (editor) {
  1823. editor_node = memnew(EditorNode);
  1824. sml->get_root()->add_child(editor_node);
  1825. if (_export_preset != "") {
  1826. editor_node->export_preset(_export_preset, positional_arg, export_debug, export_pack_only);
  1827. game_path = ""; // Do not load anything.
  1828. }
  1829. }
  1830. #endif
  1831. if (!editor && !project_manager) {
  1832. //standard helpers that can be changed from main config
  1833. String stretch_mode = GLOBAL_DEF_BASIC("display/window/stretch/mode", "disabled");
  1834. String stretch_aspect = GLOBAL_DEF_BASIC("display/window/stretch/aspect", "ignore");
  1835. Size2i stretch_size = Size2i(GLOBAL_DEF_BASIC("display/window/size/width", 0),
  1836. GLOBAL_DEF_BASIC("display/window/size/height", 0));
  1837. Window::ContentScaleMode cs_sm = Window::CONTENT_SCALE_MODE_DISABLED;
  1838. if (stretch_mode == "canvas_items") {
  1839. cs_sm = Window::CONTENT_SCALE_MODE_CANVAS_ITEMS;
  1840. } else if (stretch_mode == "viewport") {
  1841. cs_sm = Window::CONTENT_SCALE_MODE_VIEWPORT;
  1842. }
  1843. Window::ContentScaleAspect cs_aspect = Window::CONTENT_SCALE_ASPECT_IGNORE;
  1844. if (stretch_aspect == "keep") {
  1845. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP;
  1846. } else if (stretch_aspect == "keep_width") {
  1847. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_WIDTH;
  1848. } else if (stretch_aspect == "keep_height") {
  1849. cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_HEIGHT;
  1850. } else if (stretch_aspect == "expand") {
  1851. cs_aspect = Window::CONTENT_SCALE_ASPECT_EXPAND;
  1852. }
  1853. sml->get_root()->set_content_scale_mode(cs_sm);
  1854. sml->get_root()->set_content_scale_aspect(cs_aspect);
  1855. sml->get_root()->set_content_scale_size(stretch_size);
  1856. sml->set_auto_accept_quit(GLOBAL_DEF("application/config/auto_accept_quit", true));
  1857. sml->set_quit_on_go_back(GLOBAL_DEF("application/config/quit_on_go_back", true));
  1858. String appname = ProjectSettings::get_singleton()->get("application/config/name");
  1859. appname = TranslationServer::get_singleton()->translate(appname);
  1860. #ifdef DEBUG_ENABLED
  1861. // Append a suffix to the window title to denote that the project is running
  1862. // from a debug build (including the editor). Since this results in lower performance,
  1863. // this should be clearly presented to the user.
  1864. DisplayServer::get_singleton()->window_set_title(vformat("%s (DEBUG)", appname));
  1865. #else
  1866. DisplayServer::get_singleton()->window_set_title(appname);
  1867. #endif
  1868. bool snap_controls = GLOBAL_DEF("gui/common/snap_controls_to_pixels", true);
  1869. sml->get_root()->set_snap_controls_to_pixels(snap_controls);
  1870. bool font_oversampling = GLOBAL_DEF("gui/fonts/dynamic_fonts/use_oversampling", true);
  1871. sml->get_root()->set_use_font_oversampling(font_oversampling);
  1872. int texture_filter = GLOBAL_DEF("rendering/textures/canvas_textures/default_texture_filter", 1);
  1873. int texture_repeat = GLOBAL_DEF("rendering/textures/canvas_textures/default_texture_repeat", 0);
  1874. sml->get_root()->set_default_canvas_item_texture_filter(
  1875. Viewport::DefaultCanvasItemTextureFilter(texture_filter));
  1876. sml->get_root()->set_default_canvas_item_texture_repeat(
  1877. Viewport::DefaultCanvasItemTextureRepeat(texture_repeat));
  1878. } else {
  1879. GLOBAL_DEF_BASIC("display/window/stretch/mode", "disabled");
  1880. ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/mode",
  1881. PropertyInfo(Variant::STRING,
  1882. "display/window/stretch/mode",
  1883. PROPERTY_HINT_ENUM,
  1884. "disabled,canvas_items,viewport"));
  1885. GLOBAL_DEF_BASIC("display/window/stretch/aspect", "ignore");
  1886. ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/aspect",
  1887. PropertyInfo(Variant::STRING,
  1888. "display/window/stretch/aspect",
  1889. PROPERTY_HINT_ENUM,
  1890. "ignore,keep,keep_width,keep_height,expand"));
  1891. GLOBAL_DEF_BASIC("display/window/stretch/shrink", 1.0);
  1892. ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/shrink",
  1893. PropertyInfo(Variant::FLOAT,
  1894. "display/window/stretch/shrink",
  1895. PROPERTY_HINT_RANGE,
  1896. "1.0,8.0,0.1"));
  1897. sml->set_auto_accept_quit(GLOBAL_DEF("application/config/auto_accept_quit", true));
  1898. sml->set_quit_on_go_back(GLOBAL_DEF("application/config/quit_on_go_back", true));
  1899. GLOBAL_DEF_BASIC("gui/common/snap_controls_to_pixels", true);
  1900. GLOBAL_DEF_BASIC("gui/fonts/dynamic_fonts/use_oversampling", true);
  1901. GLOBAL_DEF_BASIC("rendering/textures/canvas_textures/default_texture_filter", 1);
  1902. ProjectSettings::get_singleton()->set_custom_property_info(
  1903. "rendering/textures/canvas_textures/default_texture_filter",
  1904. PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_filter", PROPERTY_HINT_ENUM,
  1905. "Nearest,Linear,Linear Mipmap,Nearest Mipmap"));
  1906. GLOBAL_DEF_BASIC("rendering/textures/canvas_textures/default_texture_repeat", 0);
  1907. ProjectSettings::get_singleton()->set_custom_property_info(
  1908. "rendering/textures/canvas_textures/default_texture_repeat",
  1909. PropertyInfo(Variant::INT, "rendering/textures/canvas_textures/default_texture_repeat", PROPERTY_HINT_ENUM,
  1910. "Disable,Enable,Mirror"));
  1911. }
  1912. #ifdef TOOLS_ENABLED
  1913. if (editor) {
  1914. bool editor_embed_subwindows = EditorSettings::get_singleton()->get_setting(
  1915. "interface/editor/single_window_mode");
  1916. if (editor_embed_subwindows) {
  1917. sml->get_root()->set_embed_subwindows_hint(true);
  1918. }
  1919. }
  1920. #endif
  1921. String local_game_path;
  1922. if (game_path != "" && !project_manager) {
  1923. local_game_path = game_path.replace("\\", "/");
  1924. if (!local_game_path.begins_with("res://")) {
  1925. bool absolute =
  1926. (local_game_path.size() > 1) && (local_game_path[0] == '/' || local_game_path[1] == ':');
  1927. if (!absolute) {
  1928. if (ProjectSettings::get_singleton()->is_using_datapack()) {
  1929. local_game_path = "res://" + local_game_path;
  1930. } else {
  1931. int sep = local_game_path.rfind("/");
  1932. if (sep == -1) {
  1933. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1934. local_game_path = da->get_current_dir().plus_file(local_game_path);
  1935. memdelete(da);
  1936. } else {
  1937. DirAccess *da = DirAccess::open(local_game_path.substr(0, sep));
  1938. if (da) {
  1939. local_game_path = da->get_current_dir().plus_file(
  1940. local_game_path.substr(sep + 1, local_game_path.length()));
  1941. memdelete(da);
  1942. }
  1943. }
  1944. }
  1945. }
  1946. }
  1947. local_game_path = ProjectSettings::get_singleton()->localize_path(local_game_path);
  1948. #ifdef TOOLS_ENABLED
  1949. if (editor) {
  1950. if (game_path != String(GLOBAL_GET("application/run/main_scene")) || !editor_node->has_scenes_in_session()) {
  1951. Error serr = editor_node->load_scene(local_game_path);
  1952. if (serr != OK) {
  1953. ERR_PRINT("Failed to load scene");
  1954. }
  1955. }
  1956. DisplayServer::get_singleton()->set_context(DisplayServer::CONTEXT_EDITOR);
  1957. }
  1958. #endif
  1959. if (!editor) {
  1960. DisplayServer::get_singleton()->set_context(DisplayServer::CONTEXT_ENGINE);
  1961. }
  1962. }
  1963. if (!project_manager && !editor) { // game
  1964. // Load SSL Certificates from Project Settings (or builtin).
  1965. Crypto::load_default_certificates(GLOBAL_DEF("network/ssl/certificate_bundle_override", ""));
  1966. if (game_path != "") {
  1967. Node *scene = nullptr;
  1968. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  1969. if (scenedata.is_valid()) {
  1970. scene = scenedata->instantiate();
  1971. }
  1972. ERR_FAIL_COND_V_MSG(!scene, false, "Failed loading scene: " + local_game_path);
  1973. sml->add_current_scene(scene);
  1974. #ifdef OSX_ENABLED
  1975. String mac_iconpath = GLOBAL_DEF("application/config/macos_native_icon", "Variant()");
  1976. if (mac_iconpath != "") {
  1977. DisplayServer::get_singleton()->set_native_icon(mac_iconpath);
  1978. hasicon = true;
  1979. }
  1980. #endif
  1981. #ifdef WINDOWS_ENABLED
  1982. String win_iconpath = GLOBAL_DEF("application/config/windows_native_icon", "Variant()");
  1983. if (win_iconpath != "") {
  1984. DisplayServer::get_singleton()->set_native_icon(win_iconpath);
  1985. hasicon = true;
  1986. }
  1987. #endif
  1988. String iconpath = GLOBAL_DEF("application/config/icon", "Variant()");
  1989. if ((iconpath != "") && (!hasicon)) {
  1990. Ref<Image> icon;
  1991. icon.instantiate();
  1992. if (ImageLoader::load_image(iconpath, icon) == OK) {
  1993. DisplayServer::get_singleton()->set_icon(icon);
  1994. hasicon = true;
  1995. }
  1996. }
  1997. }
  1998. }
  1999. #ifdef TOOLS_ENABLED
  2000. if (project_manager) {
  2001. Engine::get_singleton()->set_editor_hint(true);
  2002. ProjectManager *pmanager = memnew(ProjectManager);
  2003. ProgressDialog *progress_dialog = memnew(ProgressDialog);
  2004. pmanager->add_child(progress_dialog);
  2005. sml->get_root()->add_child(pmanager);
  2006. DisplayServer::get_singleton()->set_context(DisplayServer::CONTEXT_PROJECTMAN);
  2007. }
  2008. if (project_manager || editor) {
  2009. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CONSOLE_WINDOW)) {
  2010. // Hide console window if requested (Windows-only).
  2011. bool hide_console = EditorSettings::get_singleton()->get_setting(
  2012. "interface/editor/hide_console_window");
  2013. DisplayServer::get_singleton()->console_set_visible(!hide_console);
  2014. }
  2015. // Load SSL Certificates from Editor Settings (or builtin)
  2016. Crypto::load_default_certificates(
  2017. EditorSettings::get_singleton()->get_setting("network/ssl/editor_ssl_certificates").operator String());
  2018. }
  2019. #endif
  2020. }
  2021. if (!hasicon) {
  2022. Ref<Image> icon = memnew(Image(app_icon_png));
  2023. DisplayServer::get_singleton()->set_icon(icon);
  2024. }
  2025. OS::get_singleton()->set_main_loop(main_loop);
  2026. return true;
  2027. }
  2028. /* Main iteration
  2029. *
  2030. * This is the iteration of the engine's game loop, advancing the state of physics,
  2031. * rendering and audio.
  2032. * It's called directly by the platform's OS::run method, where the loop is created
  2033. * and monitored.
  2034. *
  2035. * The OS implementation can impact its draw step with the Main::force_redraw() method.
  2036. */
  2037. uint64_t Main::last_ticks = 0;
  2038. uint32_t Main::frames = 0;
  2039. uint32_t Main::frame = 0;
  2040. bool Main::force_redraw_requested = false;
  2041. int Main::iterating = 0;
  2042. bool Main::is_iterating() {
  2043. return iterating > 0;
  2044. }
  2045. // For performance metrics.
  2046. static uint64_t physics_process_max = 0;
  2047. static uint64_t process_max = 0;
  2048. bool Main::iteration() {
  2049. //for now do not error on this
  2050. //ERR_FAIL_COND_V(iterating, false);
  2051. iterating++;
  2052. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  2053. Engine::get_singleton()->_frame_ticks = ticks;
  2054. main_timer_sync.set_cpu_ticks_usec(ticks);
  2055. main_timer_sync.set_fixed_fps(fixed_fps);
  2056. uint64_t ticks_elapsed = ticks - last_ticks;
  2057. int physics_fps = Engine::get_singleton()->get_iterations_per_second();
  2058. float physics_step = 1.0 / physics_fps;
  2059. float time_scale = Engine::get_singleton()->get_time_scale();
  2060. MainFrameTime advance = main_timer_sync.advance(physics_step, physics_fps);
  2061. double process_step = advance.process_step;
  2062. double scaled_step = process_step * time_scale;
  2063. Engine::get_singleton()->_process_step = process_step;
  2064. Engine::get_singleton()->_physics_interpolation_fraction = advance.interpolation_fraction;
  2065. uint64_t physics_process_ticks = 0;
  2066. uint64_t process_ticks = 0;
  2067. frame += ticks_elapsed;
  2068. last_ticks = ticks;
  2069. static const int max_physics_steps = 8;
  2070. if (fixed_fps == -1 && advance.physics_steps > max_physics_steps) {
  2071. process_step -= (advance.physics_steps - max_physics_steps) * physics_step;
  2072. advance.physics_steps = max_physics_steps;
  2073. }
  2074. bool exit = false;
  2075. Engine::get_singleton()->_in_physics = true;
  2076. for (int iters = 0; iters < advance.physics_steps; ++iters) {
  2077. uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();
  2078. PhysicsServer3D::get_singleton()->sync();
  2079. PhysicsServer3D::get_singleton()->flush_queries();
  2080. PhysicsServer2D::get_singleton()->sync();
  2081. PhysicsServer2D::get_singleton()->flush_queries();
  2082. if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) {
  2083. exit = true;
  2084. break;
  2085. }
  2086. NavigationServer3D::get_singleton_mut()->process(physics_step * time_scale);
  2087. message_queue->flush();
  2088. PhysicsServer3D::get_singleton()->end_sync();
  2089. PhysicsServer3D::get_singleton()->step(physics_step * time_scale);
  2090. PhysicsServer2D::get_singleton()->end_sync();
  2091. PhysicsServer2D::get_singleton()->step(physics_step * time_scale);
  2092. message_queue->flush();
  2093. physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
  2094. physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
  2095. Engine::get_singleton()->_physics_frames++;
  2096. }
  2097. Engine::get_singleton()->_in_physics = false;
  2098. uint64_t process_begin = OS::get_singleton()->get_ticks_usec();
  2099. if (OS::get_singleton()->get_main_loop()->process(process_step * time_scale)) {
  2100. exit = true;
  2101. }
  2102. message_queue->flush();
  2103. RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames.
  2104. if (DisplayServer::get_singleton()->can_any_window_draw() &&
  2105. RenderingServer::get_singleton()->is_render_loop_enabled()) {
  2106. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  2107. if (RenderingServer::get_singleton()->has_changed()) {
  2108. RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
  2109. Engine::get_singleton()->frames_drawn++;
  2110. }
  2111. } else {
  2112. RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
  2113. Engine::get_singleton()->frames_drawn++;
  2114. force_redraw_requested = false;
  2115. }
  2116. }
  2117. process_ticks = OS::get_singleton()->get_ticks_usec() - process_begin;
  2118. process_max = MAX(process_ticks, process_max);
  2119. uint64_t frame_time = OS::get_singleton()->get_ticks_usec() - ticks;
  2120. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  2121. ScriptServer::get_language(i)->frame();
  2122. }
  2123. AudioServer::get_singleton()->update();
  2124. if (EngineDebugger::is_active()) {
  2125. EngineDebugger::get_singleton()->iteration(frame_time, process_ticks, physics_process_ticks, physics_step);
  2126. }
  2127. frames++;
  2128. Engine::get_singleton()->_process_frames++;
  2129. if (frame > 1000000) {
  2130. if (editor || project_manager) {
  2131. if (print_fps) {
  2132. print_line(vformat("Editor FPS: %d (%s mspf)", frames, rtos(1000.0 / frames).pad_decimals(1)));
  2133. }
  2134. } else if (GLOBAL_GET("debug/settings/stdout/print_fps") || print_fps) {
  2135. print_line(vformat("Project FPS: %d (%s mspf)", frames, rtos(1000.0 / frames).pad_decimals(1)));
  2136. }
  2137. Engine::get_singleton()->_fps = frames;
  2138. performance->set_process_time(USEC_TO_SEC(process_max));
  2139. performance->set_physics_process_time(USEC_TO_SEC(physics_process_max));
  2140. process_max = 0;
  2141. physics_process_max = 0;
  2142. frame %= 1000000;
  2143. frames = 0;
  2144. }
  2145. iterating--;
  2146. if (fixed_fps != -1) {
  2147. return exit;
  2148. }
  2149. OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw());
  2150. #ifdef TOOLS_ENABLED
  2151. if (auto_build_solutions) {
  2152. auto_build_solutions = false;
  2153. // Only relevant when running the editor.
  2154. if (!editor) {
  2155. ERR_FAIL_V_MSG(true,
  2156. "Command line option --build-solutions was passed, but no project is being edited. Aborting.");
  2157. }
  2158. if (!EditorNode::get_singleton()->call_build()) {
  2159. ERR_FAIL_V_MSG(true,
  2160. "Command line option --build-solutions was passed, but the build callback failed. Aborting.");
  2161. }
  2162. }
  2163. #endif
  2164. return exit || auto_quit;
  2165. }
  2166. void Main::force_redraw() {
  2167. force_redraw_requested = true;
  2168. }
  2169. /* Engine deinitialization
  2170. *
  2171. * Responsible for freeing all the memory allocated by previous setup steps,
  2172. * so that the engine closes cleanly without leaking memory or crashing.
  2173. * The order matters as some of those steps are linked with each other.
  2174. */
  2175. void Main::cleanup(bool p_force) {
  2176. if (!p_force) {
  2177. ERR_FAIL_COND(!_start_success);
  2178. }
  2179. EngineDebugger::deinitialize();
  2180. ResourceLoader::remove_custom_loaders();
  2181. ResourceSaver::remove_custom_savers();
  2182. // Flush before uninitializing the scene, but delete the MessageQueue as late as possible.
  2183. message_queue->flush();
  2184. OS::get_singleton()->delete_main_loop();
  2185. OS::get_singleton()->_cmdline.clear();
  2186. OS::get_singleton()->_execpath = "";
  2187. OS::get_singleton()->_local_clipboard = "";
  2188. ResourceLoader::clear_translation_remaps();
  2189. ResourceLoader::clear_path_remaps();
  2190. ScriptServer::finish_languages();
  2191. // Sync pending commands that may have been queued from a different thread during ScriptServer finalization
  2192. RenderingServer::get_singleton()->sync();
  2193. //clear global shader variables before scene and other graphics stuff are deinitialized.
  2194. rendering_server->global_variables_clear();
  2195. #ifdef TOOLS_ENABLED
  2196. EditorNode::unregister_editor_types();
  2197. #endif
  2198. if (xr_server) {
  2199. // cleanup now before we pull the rug from underneath...
  2200. memdelete(xr_server);
  2201. }
  2202. ImageLoader::cleanup();
  2203. unregister_driver_types();
  2204. unregister_module_types();
  2205. unregister_platform_apis();
  2206. unregister_scene_types();
  2207. unregister_server_types();
  2208. if (audio_server) {
  2209. audio_server->finish();
  2210. memdelete(audio_server);
  2211. }
  2212. if (camera_server) {
  2213. memdelete(camera_server);
  2214. }
  2215. OS::get_singleton()->finalize();
  2216. finalize_physics();
  2217. finalize_navigation_server();
  2218. finalize_display();
  2219. if (tsman) {
  2220. memdelete(tsman);
  2221. }
  2222. if (input) {
  2223. memdelete(input);
  2224. }
  2225. if (packed_data) {
  2226. memdelete(packed_data);
  2227. }
  2228. if (file_access_network_client) {
  2229. memdelete(file_access_network_client);
  2230. }
  2231. if (performance) {
  2232. memdelete(performance);
  2233. }
  2234. if (input_map) {
  2235. memdelete(input_map);
  2236. }
  2237. if (time_singleton) {
  2238. memdelete(time_singleton);
  2239. }
  2240. if (translation_server) {
  2241. memdelete(translation_server);
  2242. }
  2243. if (globals) {
  2244. memdelete(globals);
  2245. }
  2246. if (engine) {
  2247. memdelete(engine);
  2248. }
  2249. if (OS::get_singleton()->is_restart_on_exit_set()) {
  2250. //attempt to restart with arguments
  2251. String exec = OS::get_singleton()->get_executable_path();
  2252. List<String> args = OS::get_singleton()->get_restart_on_exit_arguments();
  2253. OS::get_singleton()->create_process(exec, args);
  2254. OS::get_singleton()->set_restart_on_exit(false, List<String>()); //clear list (uses memory)
  2255. }
  2256. // Now should be safe to delete MessageQueue (famous last words).
  2257. message_queue->flush();
  2258. memdelete(message_queue);
  2259. unregister_core_driver_types();
  2260. unregister_core_types();
  2261. OS::get_singleton()->finalize_core();
  2262. }