main.cpp 82 KB

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