main.cpp 77 KB

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