main.cpp 93 KB

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