main.cpp 70 KB

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