2
0

main.cpp 70 KB

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