main.cpp 91 KB

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