main.cpp 81 KB

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