main.cpp 74 KB

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