main.cpp 80 KB

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