main.cpp 62 KB

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