main.cpp 70 KB

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