-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
main.cpp
2645 lines (2202 loc) · 95.6 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* main.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "main.h"
#include "core/crypto/crypto.h"
#include "core/input_map.h"
#include "core/io/file_access_network.h"
#include "core/io/file_access_pack.h"
#include "core/io/file_access_zip.h"
#include "core/io/image_loader.h"
#include "core/io/ip.h"
#include "core/io/resource_loader.h"
#include "core/message_queue.h"
#include "core/os/dir_access.h"
#include "core/os/os.h"
#include "core/os/time.h"
#include "core/project_settings.h"
#include "core/register_core_types.h"
#include "core/script_debugger_local.h"
#include "core/script_language.h"
#include "core/translation.h"
#include "core/version.h"
#include "drivers/register_driver_types.h"
#include "main/app_icon.gen.h"
#include "main/input_default.h"
#include "main/main_timer_sync.h"
#include "main/performance.h"
#include "main/splash.gen.h"
#include "main/tests/test_main.h"
#include "modules/register_module_types.h"
#include "platform/register_platform_apis.h"
#include "scene/debugger/script_debugger_remote.h"
#include "scene/main/scene_tree.h"
#include "scene/main/viewport.h"
#include "scene/register_scene_types.h"
#include "scene/resources/packed_scene.h"
#include "servers/arvr_server.h"
#include "servers/audio_server.h"
#include "servers/camera_server.h"
#include "servers/navigation_2d_server.h"
#include "servers/navigation_server.h"
#include "servers/physics_2d_server.h"
#include "servers/physics_server.h"
#include "servers/register_server_types.h"
#include "servers/visual_server_callbacks.h"
#ifdef TOOLS_ENABLED
#include "editor/doc/doc_data.h"
#include "editor/doc/doc_data_class_path.gen.h"
#include "editor/editor_node.h"
#include "editor/editor_settings.h"
#include "editor/editor_translation.h"
#include "editor/progress_dialog.h"
#include "editor/project_manager.h"
#include "editor/script_editor_debugger.h"
#ifndef NO_EDITOR_SPLASH
#include "main/splash_editor.gen.h"
#endif
#endif
#ifdef MODULE_GDSCRIPT_ENABLED
#if defined(TOOLS_ENABLED) && !defined(GDSCRIPT_NO_LSP)
#include "modules/gdscript/language_server/gdscript_language_server.h"
#endif // TOOLS_ENABLED && !GDSCRIPT_NO_LSP
#endif // MODULE_GDSCRIPT_ENABLED
/* Static members */
// Singletons
// Initialized in setup()
static Engine *engine = nullptr;
static ProjectSettings *globals = nullptr;
static InputMap *input_map = nullptr;
static TranslationServer *translation_server = nullptr;
static Performance *performance = nullptr;
static PackedData *packed_data = nullptr;
static Time *time_singleton = nullptr;
#ifdef MINIZIP_ENABLED
static ZipArchive *zip_packed_data = nullptr;
#endif
static FileAccessNetworkClient *file_access_network_client = nullptr;
static ScriptDebugger *script_debugger = nullptr;
static MessageQueue *message_queue = nullptr;
// Initialized in setup2()
static AudioServer *audio_server = nullptr;
static CameraServer *camera_server = nullptr;
static ARVRServer *arvr_server = nullptr;
static PhysicsServer *physics_server = nullptr;
static Physics2DServer *physics_2d_server = nullptr;
static VisualServerCallbacks *visual_server_callbacks = nullptr;
static NavigationServer *navigation_server = nullptr;
static Navigation2DServer *navigation_2d_server = nullptr;
// We error out if setup2() doesn't turn this true
static bool _start_success = false;
// Drivers
static int video_driver_idx = -1;
static int audio_driver_idx = -1;
// Engine config/tools
static bool editor = false;
static bool project_manager = false;
static String locale;
static bool show_help = false;
static bool auto_quit = false;
static OS::ProcessID allow_focus_steal_pid = 0;
static bool delta_sync_after_draw = false;
#ifdef TOOLS_ENABLED
static bool auto_build_solutions = false;
static String debug_server_uri;
HashMap<Main::CLIScope, Vector<String>> forwardable_cli_arguments;
#endif
// Display
static OS::VideoMode video_mode;
static int init_screen = -1;
static bool init_fullscreen = false;
static bool init_maximized = false;
static bool init_windowed = false;
static bool init_always_on_top = false;
static bool init_use_custom_pos = false;
static Vector2 init_custom_pos;
static bool force_lowdpi = false;
// Debug
static bool use_debug_profiler = false;
#ifdef DEBUG_ENABLED
static bool debug_collisions = false;
static bool debug_navigation = false;
static bool debug_shader_fallbacks = false;
#endif
static int frame_delay = 0;
static bool disable_render_loop = false;
static int fixed_fps = -1;
static bool print_fps = false;
/* Helper methods */
// Used by Mono module, should likely be registered in Engine singleton instead
// FIXME: This is also not 100% accurate, `project_manager` is only true when it was requested,
// but not if e.g. we fail to load and project and fallback to the manager.
bool Main::is_project_manager() {
return project_manager;
}
#ifdef TOOLS_ENABLED
const Vector<String> &Main::get_forwardable_cli_arguments(Main::CLIScope p_scope) {
return forwardable_cli_arguments[p_scope];
}
#endif
static String unescape_cmdline(const String &p_str) {
return p_str.replace("%20", " ");
}
static String get_full_version_string() {
String hash = String(VERSION_HASH);
if (!hash.empty()) {
hash = "." + hash.left(9);
}
return String(VERSION_FULL_BUILD) + hash;
}
// FIXME: Could maybe be moved to PhysicsServerManager and Physics2DServerManager directly
// to have less code in main.cpp.
void initialize_physics() {
// This must be defined BEFORE the 3d physics server is created,
// otherwise it won't always show up in the project settings page.
GLOBAL_DEF("physics/3d/godot_physics/use_bvh", true);
GLOBAL_DEF("physics/3d/godot_physics/bvh_collision_margin", 0.1);
ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/godot_physics/bvh_collision_margin", PropertyInfo(Variant::REAL, "physics/3d/godot_physics/bvh_collision_margin", PROPERTY_HINT_RANGE, "0.0,2.0,0.01"));
/// 3D Physics Server
physics_server = PhysicsServerManager::new_server(ProjectSettings::get_singleton()->get(PhysicsServerManager::setting_property_name));
if (!physics_server) {
// Physics server not found, Use the default physics
physics_server = PhysicsServerManager::new_default_server();
}
ERR_FAIL_COND(!physics_server);
physics_server->init();
/// 2D Physics server
physics_2d_server = Physics2DServerManager::new_server(ProjectSettings::get_singleton()->get(Physics2DServerManager::setting_property_name));
if (!physics_2d_server) {
// Physics server not found, Use the default physics
physics_2d_server = Physics2DServerManager::new_default_server();
}
ERR_FAIL_COND(!physics_2d_server);
physics_2d_server->init();
}
void finalize_physics() {
physics_server->finish();
memdelete(physics_server);
physics_2d_server->finish();
memdelete(physics_2d_server);
}
void initialize_navigation_server() {
ERR_FAIL_COND(navigation_server != nullptr);
navigation_server = NavigationServerManager::new_default_server();
navigation_2d_server = memnew(Navigation2DServer);
}
void finalize_navigation_server() {
memdelete(navigation_server);
navigation_server = nullptr;
memdelete(navigation_2d_server);
navigation_2d_server = nullptr;
}
//#define DEBUG_INIT
#ifdef DEBUG_INIT
#define MAIN_PRINT(m_txt) print_line(m_txt)
#else
#define MAIN_PRINT(m_txt)
#endif
void Main::print_help(const char *p_binary) {
print_line(String(VERSION_NAME) + " v" + get_full_version_string() + " - " + String(VERSION_WEBSITE));
OS::get_singleton()->print("Free and open source software under the terms of the MIT license.\n");
OS::get_singleton()->print("(c) 2014-present Godot Engine contributors.\n");
OS::get_singleton()->print("(c) 2007-2014 Juan Linietsky, Ariel Manzur.\n");
OS::get_singleton()->print("\n");
OS::get_singleton()->print("Usage: %s [options] [path to scene or 'project.godot' file]\n", p_binary);
OS::get_singleton()->print("\n");
OS::get_singleton()->print("General options:\n");
OS::get_singleton()->print(" -h, --help Display this help message.\n");
OS::get_singleton()->print(" --version Display the version string.\n");
OS::get_singleton()->print(" -v, --verbose Use verbose stdout mode.\n");
OS::get_singleton()->print(" --quiet Quiet mode, silences stdout messages. Errors are still displayed.\n");
OS::get_singleton()->print("\n");
OS::get_singleton()->print("Run options:\n");
#ifdef TOOLS_ENABLED
OS::get_singleton()->print(" -e, --editor Start the editor instead of running the scene.\n");
OS::get_singleton()->print(" -p, --project-manager Start the project manager, even if a project is auto-detected.\n");
OS::get_singleton()->print(" --debug-server <address> Start the editor debug server (<IP>:<port>, e.g. 127.0.0.1:6007)\n");
#if defined(MODULE_GDSCRIPT_ENABLED) && !defined(GDSCRIPT_NO_LSP)
OS::get_singleton()->print(" --lsp-port <port> Use the specified port for the language server protocol. The port must be between 0 to 65535.\n");
#endif // MODULE_GDSCRIPT_ENABLED && !GDSCRIPT_NO_LSP
#endif
OS::get_singleton()->print(" -q, --quit Quit after the first iteration.\n");
OS::get_singleton()->print(" -l, --language <locale> Use a specific locale (<locale> being a two-letter code).\n");
OS::get_singleton()->print(" --path <directory> Path to a project (<directory> must contain a 'project.godot' file).\n");
OS::get_singleton()->print(" -u, --upwards Scan folders upwards for project.godot file.\n");
OS::get_singleton()->print(" --main-pack <file> Path to a pack (.pck) file to load.\n");
OS::get_singleton()->print(" --render-thread <mode> Render thread mode ('unsafe', 'safe', 'separate').\n");
OS::get_singleton()->print(" --remote-fs <address> Remote filesystem (<host/IP>[:<port>] address).\n");
OS::get_singleton()->print(" --remote-fs-password <password> Password for remote filesystem.\n");
OS::get_singleton()->print(" --audio-driver <driver> Audio driver (");
for (int i = 0; i < OS::get_singleton()->get_audio_driver_count(); i++) {
if (i != 0) {
OS::get_singleton()->print(", ");
}
OS::get_singleton()->print("'%s'", OS::get_singleton()->get_audio_driver_name(i));
}
OS::get_singleton()->print(").\n");
OS::get_singleton()->print(" --video-driver <driver> Video driver (");
for (int i = 0; i < OS::get_singleton()->get_video_driver_count(); i++) {
if (i != 0) {
OS::get_singleton()->print(", ");
}
OS::get_singleton()->print("'%s'", OS::get_singleton()->get_video_driver_name(i));
}
OS::get_singleton()->print(").\n");
OS::get_singleton()->print("\n");
#ifndef SERVER_ENABLED
OS::get_singleton()->print("Display options:\n");
OS::get_singleton()->print(" -f, --fullscreen Request fullscreen mode.\n");
OS::get_singleton()->print(" -m, --maximized Request a maximized window.\n");
OS::get_singleton()->print(" -w, --windowed Request windowed mode.\n");
OS::get_singleton()->print(" -t, --always-on-top Request an always-on-top window.\n");
OS::get_singleton()->print(" --resolution <W>x<H> Request window resolution.\n");
OS::get_singleton()->print(" --position <X>,<Y> Request window position.\n");
OS::get_singleton()->print(" --low-dpi Force low-DPI mode (macOS and Windows only).\n");
OS::get_singleton()->print(" --no-window Run with invisible window. Useful together with --script.\n");
OS::get_singleton()->print(" --enable-vsync-via-compositor When vsync is enabled, vsync via the OS' window compositor (Windows only).\n");
OS::get_singleton()->print(" --disable-vsync-via-compositor Disable vsync via the OS' window compositor (Windows only).\n");
OS::get_singleton()->print(" --enable-delta-smoothing When vsync is enabled, enabled frame delta smoothing.\n");
OS::get_singleton()->print(" --disable-delta-smoothing Disable frame delta smoothing.\n");
OS::get_singleton()->print(" --tablet-driver Tablet input driver (");
for (int i = 0; i < OS::get_singleton()->get_tablet_driver_count(); i++) {
if (i != 0) {
OS::get_singleton()->print(", ");
}
OS::get_singleton()->print("'%s'", OS::get_singleton()->get_tablet_driver_name(i).utf8().get_data());
}
OS::get_singleton()->print(") (Windows only).\n");
OS::get_singleton()->print("\n");
#endif
OS::get_singleton()->print("Debug options:\n");
OS::get_singleton()->print(" -d, --debug Debug (local stdout debugger).\n");
OS::get_singleton()->print(" -b, --breakpoints Breakpoint list as source::line comma-separated pairs, no spaces (use %%20 instead).\n");
OS::get_singleton()->print(" --profiling Enable profiling in the script debugger.\n");
OS::get_singleton()->print(" --remote-debug <address> Remote debug (<host/IP>:<port> address).\n");
#if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED)
OS::get_singleton()->print(" --debug-collisions Show collision shapes when running the scene.\n");
OS::get_singleton()->print(" --debug-navigation Show navigation polygons when running the scene.\n");
OS::get_singleton()->print(" --debug-shader-fallbacks Use the fallbacks of the shaders which have one when running the scene (GL ES 3 only).\n");
#endif
OS::get_singleton()->print(" --frame-delay <ms> Simulate high CPU load (delay each frame by <ms> milliseconds).\n");
OS::get_singleton()->print(" --time-scale <scale> Force time scale (higher values are faster, 1.0 is normal speed).\n");
OS::get_singleton()->print(" --disable-render-loop Disable render loop so rendering only occurs when called explicitly from script.\n");
OS::get_singleton()->print(" --disable-crash-handler Disable crash handler when supported by the platform code.\n");
OS::get_singleton()->print(" --fixed-fps <fps> Force a fixed number of frames per second. This setting disables real-time synchronization.\n");
OS::get_singleton()->print(" --print-fps Print the frames per second to the stdout.\n");
OS::get_singleton()->print("\n");
OS::get_singleton()->print("Standalone tools:\n");
OS::get_singleton()->print(" -s, --script <script> Run a script.\n");
OS::get_singleton()->print(" --check-only Only parse for errors and quit (use with --script).\n");
#ifdef TOOLS_ENABLED
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");
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");
OS::get_singleton()->print(" --export-debug <preset> <path> Same as --export, but using the debug template.\n");
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");
OS::get_singleton()->print(" --doctool [<path>] Dump the engine API reference to the given <path> (defaults to current dir) in XML format, merging if existing files are found.\n");
OS::get_singleton()->print(" --no-docbase Disallow dumping the base types (used with --doctool).\n");
OS::get_singleton()->print(" --build-solutions Build the scripting solutions (e.g. for C# projects). Implies --editor and requires a valid project to edit.\n");
OS::get_singleton()->print(" --benchmark Benchmark the run time and print it to console.\n");
OS::get_singleton()->print(" --benchmark-file <path> Benchmark the run time and save it to a given file in JSON format. The path should be absolute.\n");
#ifdef DEBUG_METHODS_ENABLED
OS::get_singleton()->print(" --gdnative-generate-json-api Generate JSON dump of the Godot API for GDNative bindings.\n");
#endif
OS::get_singleton()->print(" --test <test> Run a unit test (");
const char **test_names = tests_get_names();
const char *comma = "";
while (*test_names) {
OS::get_singleton()->print("%s'%s'", comma, *test_names);
test_names++;
comma = ", ";
}
OS::get_singleton()->print(").\n");
#endif
}
/* Engine initialization
*
* Consists of several methods that are called by each platform's specific main(argc, argv).
* To fully understand engine init, one should therefore start from the platform's main and
* see how it calls into the Main class' methods.
*
* The initialization is typically done in 3 steps (with the setup2 step triggered either
* automatically by setup, or manually in the platform's main).
*
* - setup(execpath, argc, argv, p_second_phase) is the main entry point for all platforms,
* responsible for the initialization of all low level singletons and core types, and parsing
* command line arguments to configure things accordingly.
* If p_second_phase is true, it will chain into setup2() (default behaviour). This is
* disabled on some platforms (Android, iOS, UWP) which trigger the second step in their
* own time.
*
* - setup2(p_main_tid_override) registers high level servers and singletons, displays the
* boot splash, then registers higher level types (scene, editor, etc.).
*
* - start() is the last step and that's where command line tools can run, or the main loop
* can be created eventually and the project settings put into action. That's also where
* the editor node is created, if relevant.
* start() does it own argument parsing for a subset of the command line arguments described
* in help, it's a bit messy and should be globalized with the setup() parsing somehow.
*/
Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_phase) {
#if defined(DEBUG_ENABLED) && !defined(NO_THREADS)
check_lockless_atomics();
#endif
RID_OwnerBase::init_rid();
OS::get_singleton()->initialize_core();
// Benchmark tracking must be done after `OS::get_singleton()->initialize_core()` as on some
// platforms, it's used to set up the time utilities.
OS::get_singleton()->benchmark_begin_measure("startup_begin");
engine = memnew(Engine);
MAIN_PRINT("Main: Initialize CORE");
OS::get_singleton()->benchmark_begin_measure("core");
register_core_types();
register_core_driver_types();
MAIN_PRINT("Main: Initialize Globals");
globals = memnew(ProjectSettings);
input_map = memnew(InputMap);
time_singleton = memnew(Time);
register_core_settings(); //here globals is present
translation_server = memnew(TranslationServer);
performance = memnew(Performance);
ClassDB::register_class<Performance>();
engine->add_singleton(Engine::Singleton("Performance", performance));
GLOBAL_DEF("debug/settings/crash_handler/message",
String("Please include this when reporting the bug to the project developer."));
GLOBAL_DEF("debug/settings/crash_handler/message.editor",
String("Please include this when reporting the bug on: https://github.com/godotengine/godot/issues"));
MAIN_PRINT("Main: Parse CMDLine");
/* argument parsing and main creation */
List<String> args;
List<String> main_args;
for (int i = 0; i < argc; i++) {
args.push_back(String::utf8(argv[i]));
}
List<String>::Element *I = args.front();
I = args.front();
while (I) {
I->get() = unescape_cmdline(I->get().strip_edges());
I = I->next();
}
I = args.front();
String video_driver = "";
String audio_driver = "";
String tablet_driver = "";
String project_path = ".";
bool upwards = false;
String debug_mode;
String debug_host;
bool skip_breakpoints = false;
String main_pack;
bool quiet_stdout = false;
int rtm = -1;
String remotefs;
String remotefs_pass;
Vector<String> breakpoints;
bool use_custom_res = true;
bool force_res = false;
bool saw_vsync_via_compositor_override = false;
bool delta_smoothing_override = false;
#ifdef TOOLS_ENABLED
bool found_project = false;
#endif
packed_data = PackedData::get_singleton();
if (!packed_data) {
packed_data = memnew(PackedData);
}
#ifdef MINIZIP_ENABLED
//XXX: always get_singleton() == 0x0
zip_packed_data = ZipArchive::get_singleton();
//TODO: remove this temporary fix
if (!zip_packed_data) {
zip_packed_data = memnew(ZipArchive);
}
packed_data->add_pack_source(zip_packed_data);
#endif
// Default exit code, can be modified for certain errors.
Error exit_code = ERR_INVALID_PARAMETER;
I = args.front();
while (I) {
#ifdef OSX_ENABLED
// Ignore the process serial number argument passed by macOS Gatekeeper.
// Otherwise, Godot would try to open a non-existent project on the first start and abort.
if (I->get().begins_with("-psn_")) {
I = I->next();
continue;
}
#endif
List<String>::Element *N = I->next();
#ifdef TOOLS_ENABLED
if (I->get() == "--debug" ||
I->get() == "--verbose" ||
I->get() == "--disable-crash-handler") {
forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->get());
forwardable_cli_arguments[CLI_SCOPE_PROJECT].push_back(I->get());
}
if (I->get() == "--audio-driver" ||
I->get() == "--video-driver") {
if (I->next()) {
forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->get());
forwardable_cli_arguments[CLI_SCOPE_TOOL].push_back(I->next()->get());
}
}
#endif
if (I->get() == "-h" || I->get() == "--help" || I->get() == "/?") { // display help
show_help = true;
exit_code = ERR_HELP; // Hack to force an early exit in `main()` with a success code.
goto error;
} else if (I->get() == "--version") {
print_line(get_full_version_string());
exit_code = ERR_HELP; // Hack to force an early exit in `main()` with a success code.
goto error;
} else if (I->get() == "-v" || I->get() == "--verbose") { // verbose output
OS::get_singleton()->_verbose_stdout = true;
} else if (I->get() == "--quiet") { // quieter output
quiet_stdout = true;
} else if (I->get() == "--audio-driver") { // audio driver
if (I->next()) {
audio_driver = I->next()->get();
bool found = false;
for (int i = 0; i < OS::get_singleton()->get_audio_driver_count(); i++) {
if (audio_driver == OS::get_singleton()->get_audio_driver_name(i)) {
found = true;
}
}
if (!found) {
OS::get_singleton()->print("Unknown audio driver '%s', aborting.\nValid options are ", audio_driver.utf8().get_data());
for (int i = 0; i < OS::get_singleton()->get_audio_driver_count(); i++) {
if (i == OS::get_singleton()->get_audio_driver_count() - 1) {
OS::get_singleton()->print(" and ");
} else if (i != 0) {
OS::get_singleton()->print(", ");
}
OS::get_singleton()->print("'%s'", OS::get_singleton()->get_audio_driver_name(i));
}
OS::get_singleton()->print(".\n");
goto error;
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing audio driver argument, aborting.\n");
goto error;
}
} else if (I->get() == "--video-driver") { // force video driver
if (I->next()) {
video_driver = I->next()->get();
bool found = false;
for (int i = 0; i < OS::get_singleton()->get_video_driver_count(); i++) {
if (video_driver == OS::get_singleton()->get_video_driver_name(i)) {
found = true;
}
}
if (!found) {
OS::get_singleton()->print("Unknown video driver '%s', aborting.\nValid options are ", video_driver.utf8().get_data());
for (int i = 0; i < OS::get_singleton()->get_video_driver_count(); i++) {
if (i == OS::get_singleton()->get_video_driver_count() - 1) {
OS::get_singleton()->print(" and ");
} else if (i != 0) {
OS::get_singleton()->print(", ");
}
OS::get_singleton()->print("'%s'", OS::get_singleton()->get_video_driver_name(i));
}
OS::get_singleton()->print(".\n");
goto error;
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing video driver argument, aborting.\n");
goto error;
}
#ifndef SERVER_ENABLED
} else if (I->get() == "-f" || I->get() == "--fullscreen") { // force fullscreen
init_fullscreen = true;
} else if (I->get() == "-m" || I->get() == "--maximized") { // force maximized window
init_maximized = true;
video_mode.maximized = true;
} else if (I->get() == "-w" || I->get() == "--windowed") { // force windowed window
init_windowed = true;
} else if (I->get() == "-t" || I->get() == "--always-on-top") { // force always-on-top window
init_always_on_top = true;
} else if (I->get() == "--resolution") { // force resolution
if (I->next()) {
String vm = I->next()->get();
if (vm.find("x") == -1) { // invalid parameter format
OS::get_singleton()->print("Invalid resolution '%s', it should be e.g. '1280x720'.\n", vm.utf8().get_data());
goto error;
}
int w = vm.get_slice("x", 0).to_int();
int h = vm.get_slice("x", 1).to_int();
if (w <= 0 || h <= 0) {
OS::get_singleton()->print("Invalid resolution '%s', width and height must be above 0.\n", vm.utf8().get_data());
goto error;
}
video_mode.width = w;
video_mode.height = h;
force_res = true;
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing resolution argument, aborting.\n");
goto error;
}
} else if (I->get() == "--position") { // set window position
if (I->next()) {
String vm = I->next()->get();
if (vm.find(",") == -1) { // invalid parameter format
OS::get_singleton()->print("Invalid position '%s', it should be e.g. '80,128'.\n", vm.utf8().get_data());
goto error;
}
int x = vm.get_slice(",", 0).to_int();
int y = vm.get_slice(",", 1).to_int();
init_custom_pos = Point2(x, y);
init_use_custom_pos = true;
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing position argument, aborting.\n");
goto error;
}
} else if (I->get() == "--low-dpi") { // force low DPI (macOS only)
force_lowdpi = true;
} else if (I->get() == "--no-window") { // run with an invisible window
OS::get_singleton()->set_no_window_mode(true);
} else if (I->get() == "--tablet-driver") {
if (I->next()) {
tablet_driver = I->next()->get();
bool found = false;
for (int i = 0; i < OS::get_singleton()->get_tablet_driver_count(); i++) {
if (tablet_driver == OS::get_singleton()->get_tablet_driver_name(i)) {
found = true;
}
}
if (!found) {
OS::get_singleton()->print("Unknown tablet driver '%s', aborting.\n", tablet_driver.utf8().get_data());
goto error;
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing tablet driver argument, aborting.\n");
goto error;
}
} else if (I->get() == "--enable-vsync-via-compositor") {
video_mode.vsync_via_compositor = true;
saw_vsync_via_compositor_override = true;
} else if (I->get() == "--disable-vsync-via-compositor") {
video_mode.vsync_via_compositor = false;
saw_vsync_via_compositor_override = true;
} else if (I->get() == "--enable-delta-smoothing") {
OS::get_singleton()->set_delta_smoothing(true);
delta_smoothing_override = true;
} else if (I->get() == "--disable-delta-smoothing") {
OS::get_singleton()->set_delta_smoothing(false);
delta_smoothing_override = true;
#endif
} else if (I->get() == "--profiling") { // enable profiling
use_debug_profiler = true;
} else if (I->get() == "-l" || I->get() == "--language") { // language
if (I->next()) {
locale = I->next()->get();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing language argument, aborting.\n");
goto error;
}
} else if (I->get() == "--remote-fs") { // remote filesystem
if (I->next()) {
remotefs = I->next()->get();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing remote filesystem address, aborting.\n");
goto error;
}
} else if (I->get() == "--remote-fs-password") { // remote filesystem password
if (I->next()) {
remotefs_pass = I->next()->get();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing remote filesystem password, aborting.\n");
goto error;
}
} else if (I->get() == "--render-thread") { // render thread mode
if (I->next()) {
if (I->next()->get() == "safe") {
rtm = OS::RENDER_THREAD_SAFE;
} else if (I->next()->get() == "unsafe") {
rtm = OS::RENDER_THREAD_UNSAFE;
} else if (I->next()->get() == "separate") {
rtm = OS::RENDER_SEPARATE_THREAD;
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing render thread mode argument, aborting.\n");
goto error;
}
#ifdef TOOLS_ENABLED
} else if (I->get() == "-e" || I->get() == "--editor") { // starts editor
editor = true;
} else if (I->get() == "-p" || I->get() == "--project-manager") { // starts project manager
project_manager = true;
} else if (I->get() == "--debug-server") {
if (I->next()) {
debug_server_uri = I->next()->get();
if (debug_server_uri.find(":") == -1) { // wrong address
OS::get_singleton()->print("Invalid debug server address. It should be of the form <bind_address>:<port>.\n");
goto error;
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing remote debug server server, aborting.\n");
goto error;
}
} else if (I->get() == "--build-solutions") { // Build the scripting solution such C#
auto_build_solutions = true;
editor = true;
#ifdef DEBUG_METHODS_ENABLED
} else if (I->get() == "--gdnative-generate-json-api") {
// Register as an editor instance to use the GLES2 fallback automatically on hardware that doesn't support the GLES3 backend
editor = true;
// We still pass it to the main arguments since the argument handling itself is not done in this function
main_args.push_back(I->get());
#endif
} else if (I->get() == "--export" || I->get() == "--export-debug" || I->get() == "--export-pack") { // Export project
editor = true;
main_args.push_back(I->get());
#endif
} else if (I->get() == "--path") { // set path of project to start or edit
if (I->next()) {
String p = I->next()->get();
if (OS::get_singleton()->set_cwd(p) == OK) {
//nothing
} else {
project_path = I->next()->get(); //use project_path instead
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing relative or absolute path, aborting.\n");
goto error;
}
} else if (I->get() == "-u" || I->get() == "--upwards") { // scan folders upwards
upwards = true;
} else if (I->get() == "-q" || I->get() == "--quit") { // Auto quit at the end of the first main loop iteration
auto_quit = true;
} else if (I->get().ends_with("project.godot")) {
String path;
String file = I->get();
int sep = MAX(file.rfind("/"), file.rfind("\\"));
if (sep == -1) {
path = ".";
} else {
path = file.substr(0, sep);
}
if (OS::get_singleton()->set_cwd(path) == OK) {
// path already specified, don't override
} else {
project_path = path;
}
#ifdef TOOLS_ENABLED
editor = true;
#endif
} else if (I->get() == "-b" || I->get() == "--breakpoints") { // add breakpoints
if (I->next()) {
String bplist = I->next()->get();
breakpoints = bplist.split(",");
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing list of breakpoints, aborting.\n");
goto error;
}
} else if (I->get() == "--frame-delay") { // force frame delay
if (I->next()) {
frame_delay = I->next()->get().to_int();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing frame delay argument, aborting.\n");
goto error;
}
} else if (I->get() == "--time-scale") { // force time scale
if (I->next()) {
Engine::get_singleton()->set_time_scale(I->next()->get().to_double());
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing time scale argument, aborting.\n");
goto error;
}
} else if (I->get() == "--main-pack") {
if (I->next()) {
main_pack = I->next()->get();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing path to main pack file, aborting.\n");
goto error;
};
} else if (I->get() == "-d" || I->get() == "--debug") {
debug_mode = "local";
OS::get_singleton()->_debug_stdout = true;
#if defined(DEBUG_ENABLED) && !defined(SERVER_ENABLED)
} else if (I->get() == "--debug-collisions") {
debug_collisions = true;
} else if (I->get() == "--debug-navigation") {
debug_navigation = true;
} else if (I->get() == "--debug-shader-fallbacks") {
debug_shader_fallbacks = true;
#endif
} else if (I->get() == "--remote-debug") {
if (I->next()) {
debug_mode = "remote";
debug_host = I->next()->get();
if (debug_host.find(":") == -1) { // wrong address
OS::get_singleton()->print("Invalid debug host address, it should be of the form <host/IP>:<port>.\n");
goto error;
}
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing remote debug host address, aborting.\n");
goto error;
}
} else if (I->get() == "--allow_focus_steal_pid") { // not exposed to user
if (I->next()) {
allow_focus_steal_pid = I->next()->get().to_int64();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing editor PID argument, aborting.\n");
goto error;
}
} else if (I->get() == "--disable-render-loop") {
disable_render_loop = true;
} else if (I->get() == "--fixed-fps") {
if (I->next()) {
fixed_fps = I->next()->get().to_int();
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing fixed-fps argument, aborting.\n");
goto error;
}
} else if (I->get() == "--print-fps") {
print_fps = true;
} else if (I->get() == "--disable-crash-handler") {
OS::get_singleton()->disable_crash_handler();
} else if (I->get() == "--skip-breakpoints") {
skip_breakpoints = true;
} else if (I->get() == "--benchmark") {
OS::get_singleton()->set_use_benchmark(true);
} else if (I->get() == "--benchmark-file") {
if (I->next()) {
OS::get_singleton()->set_use_benchmark(true);
String benchmark_file = I->next()->get();
OS::get_singleton()->set_benchmark_file(benchmark_file);
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing <path> argument for --startup-benchmark-file <path>.\n");
OS::get_singleton()->print("Missing <path> argument for --benchmark-file <path>.\n");
goto error;
}
#if defined(TOOLS_ENABLED) && !defined(GDSCRIPT_NO_LSP)
} else if (I->get() == "--lsp-port") {
if (I->next()) {
int port_override = I->next()->get().to_int();
if (port_override < 0 || port_override > 65535) {
OS::get_singleton()->print("<port> argument for --lsp-port <port> must be between 0 and 65535.\n");
goto error;
}
GDScriptLanguageServer::port_override = port_override;
N = I->next()->next();
} else {
OS::get_singleton()->print("Missing <port> argument for --lsp-port <port>.\n");
goto error;
}
#endif // TOOLS_ENABLED && !GDSCRIPT_NO_LSP
} else {
main_args.push_back(I->get());
}
I = N;
}
#ifdef TOOLS_ENABLED
if (editor && project_manager) {
OS::get_singleton()->print("Error: Command line arguments implied opening both editor and project manager, which is not possible. Aborting.\n");
goto error;
}
#endif
// Network file system needs to be configured before globals, since globals are based on the
// 'project.godot' file which will only be available through the network if this is enabled
FileAccessNetwork::configure();
if (remotefs != "") {
file_access_network_client = memnew(FileAccessNetworkClient);
int port;
if (remotefs.find(":") != -1) {
port = remotefs.get_slicec(':', 1).to_int();
remotefs = remotefs.get_slicec(':', 0);
} else {
port = 6010;
}
Error err = file_access_network_client->connect(remotefs, port, remotefs_pass);
if (err) {
OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i.\n", remotefs.utf8().get_data(), port);
goto error;
}