main.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*************************************************************************/
  2. /* main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "main.h"
  30. #include "os/os.h"
  31. #include "globals.h"
  32. #include "splash.h"
  33. #include "core/register_core_types.h"
  34. #include "scene/register_scene_types.h"
  35. #include "drivers/register_driver_types.h"
  36. #include "script/register_script_types.h"
  37. #include "servers/register_server_types.h"
  38. #include "modules/register_module_types.h"
  39. #include "script_debugger_local.h"
  40. #include "script_debugger_remote.h"
  41. #include "message_queue.h"
  42. #include "path_remap.h"
  43. #include "input_map.h"
  44. #include "io/resource_loader.h"
  45. #include "scene/main/scene_main_loop.h"
  46. #include "scene/io/scene_loader.h"
  47. #include "script_language.h"
  48. #include "io/resource_loader.h"
  49. #include "bin/tests/test_main.h"
  50. #include "os/dir_access.h"
  51. #include "core/io/ip.h"
  52. #include "scene/resources/packed_scene.h"
  53. #include "scene/main/viewport.h"
  54. #ifdef TOOLS_ENABLED
  55. #include "tools/editor/editor_node.h"
  56. #include "tools/editor/project_manager.h"
  57. #include "tools/editor/console.h"
  58. #endif
  59. #include "io/file_access_network.h"
  60. #include "tools/doc/doc_data.h"
  61. #include "servers/spatial_sound_server.h"
  62. #include "servers/spatial_sound_2d_server.h"
  63. #include "servers/physics_2d_server.h"
  64. #include "core/io/stream_peer_tcp.h"
  65. #include "core/os/thread.h"
  66. #include "core/io/file_access_pack.h"
  67. #include "core/io/file_access_zip.h"
  68. #include "translation.h"
  69. #include "version.h"
  70. #include "performance.h"
  71. static Globals *globals=NULL;
  72. static InputMap *input_map=NULL;
  73. static bool _start_success=false;
  74. static ScriptDebugger *script_debugger=NULL;
  75. static MessageQueue *message_queue=NULL;
  76. static Performance *performance = NULL;
  77. static PathRemap *path_remap;
  78. static PackedData *packed_data=NULL;
  79. static FileAccessNetworkClient *file_access_network_client=NULL;
  80. static TranslationServer *translation_server = NULL;
  81. static OS::VideoMode video_mode;
  82. static int video_driver_idx=-1;
  83. static int audio_driver_idx=-1;
  84. static String locale;
  85. static String unescape_cmdline(const String& p_str) {
  86. return p_str.replace("%20"," ");
  87. }
  88. //#define DEBUG_INIT
  89. #ifdef DEBUG_INIT
  90. #define MAIN_PRINT(m_txt) print_line(m_txt)
  91. #else
  92. #define MAIN_PRINT(m_txt)
  93. #endif
  94. void Main::print_help(const char* p_binary) {
  95. OS::get_singleton()->print(VERSION_FULL_NAME" (c) 2008-2010 Juan Linietsky, Ariel Manzur.\n");
  96. OS::get_singleton()->print("Usage: %s [options] [scene]\n",p_binary);
  97. OS::get_singleton()->print("Options:\n");
  98. OS::get_singleton()->print("\t-path [dir] : Path to a game, containing engine.cfg\n");
  99. #ifdef TOOLS_ENABLED
  100. OS::get_singleton()->print("\t-e,-editor : Bring up the editor instead of running the scene.\n");
  101. #endif
  102. OS::get_singleton()->print("\t-test [test] : Run a test.\n");
  103. OS::get_singleton()->print("\t\t(");
  104. const char **test_names=tests_get_names();
  105. const char* coma = "";
  106. while(*test_names) {
  107. OS::get_singleton()->print("%s%s", coma, *test_names);
  108. test_names++;
  109. coma = ", ";
  110. }
  111. OS::get_singleton()->print(")\n");
  112. OS::get_singleton()->print("\t-r WIDTHxHEIGHT\t : Request Screen Resolution\n");
  113. OS::get_singleton()->print("\t-f\t\t : Request Fullscreen\n");
  114. OS::get_singleton()->print("\t-vd DRIVER\t : Video Driver (");
  115. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  116. if (i!=0)
  117. OS::get_singleton()->print(", ");
  118. OS::get_singleton()->print("%s",OS::get_singleton()->get_video_driver_name(i));
  119. }
  120. OS::get_singleton()->print(")\n");
  121. OS::get_singleton()->print("\t-ad DRIVER\t : Audio Driver (");
  122. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  123. if (i!=0)
  124. OS::get_singleton()->print(", ");
  125. OS::get_singleton()->print("%s",OS::get_singleton()->get_audio_driver_name(i));
  126. }
  127. OS::get_singleton()->print("\t-rthread <mode>\t : Render Thread Mode ('unsafe', 'safe', 'separate).");
  128. OS::get_singleton()->print(")\n");
  129. OS::get_singleton()->print("\t-s,-script [script] : Run a script.\n");
  130. OS::get_singleton()->print("\t-d,-debug : Debug (local stdout debugger).\n");
  131. OS::get_singleton()->print("\t-rdebug ADDRESS : Remote debug (<ip>:<port> host address).\n");
  132. OS::get_singleton()->print("\t-fdelay [msec]: Simulate high CPU load (delay each frame by [msec]).\n");
  133. OS::get_singleton()->print("\t-bp : breakpoint list as source::line comma separated pairs, no spaces (%%20,%%2C,etc instead).\n");
  134. OS::get_singleton()->print("\t-v : Verbose stdout mode\n");
  135. OS::get_singleton()->print("\t-lang [locale]: Use a specific locale\n");
  136. OS::get_singleton()->print("\t-rfs <host/ip>[:<port>] : Remote FileSystem.\n");
  137. OS::get_singleton()->print("\t-rfs_pass <password> : Password for Remote FileSystem.\n");
  138. #ifdef TOOLS_ENABLED
  139. OS::get_singleton()->print("\t-doctool FILE: Dump the whole engine api to FILE in XML format. If FILE exists, it will be merged.\n");
  140. OS::get_singleton()->print("\t-nodocbase: Disallow dump the base types (used with -doctool).\n");
  141. OS::get_singleton()->print("\t-optimize FILE Save an optimized copy of scene to FILE.\n");
  142. OS::get_singleton()->print("\t-optimize_preset [preset] Use a given preset for optimization.\n");
  143. OS::get_singleton()->print("\t-export [target] Export the project using given export target.\n");
  144. #endif
  145. }
  146. Error Main::setup(const char *execpath,int argc, char *argv[],bool p_second_phase) {
  147. RID_OwnerBase::init_rid();
  148. OS::get_singleton()->initialize_core();
  149. ObjectTypeDB::init();
  150. MAIN_PRINT("Main: Initialize CORE");
  151. register_core_types();
  152. register_core_driver_types();
  153. MAIN_PRINT("Main: Initialize Globals");
  154. Thread::_main_thread_id = Thread::get_caller_ID();
  155. globals = memnew( Globals );
  156. input_map = memnew( InputMap );
  157. path_remap = memnew( PathRemap );
  158. translation_server = memnew( TranslationServer );
  159. performance = memnew( Performance );
  160. globals->add_singleton(Globals::Singleton("Performance",performance));
  161. MAIN_PRINT("Main: Parse CMDLine");
  162. /* argument parsing and main creation */
  163. List<String> args;
  164. List<String> main_args;
  165. for(int i=0;i<argc;i++) {
  166. args.push_back(argv[i]);
  167. }
  168. List<String>::Element *I=args.front();
  169. I=args.front();
  170. while (I) {
  171. I->get()=unescape_cmdline(I->get().strip_escapes());
  172. I=I->next();
  173. }
  174. I=args.front();
  175. video_mode = OS::get_singleton()->get_default_video_mode();
  176. String video_driver="";
  177. String audio_driver="";
  178. String game_path=".";
  179. String debug_mode;
  180. String debug_host;
  181. int rtm=-1;
  182. String remotefs;
  183. String remotefs_pass;
  184. String screen = "";
  185. List<String> pack_list;
  186. Vector<String> breakpoints;
  187. bool use_custom_res=true;
  188. bool force_res=false;
  189. I=args.front();
  190. packed_data = PackedData::get_singleton();
  191. if (!packed_data)
  192. packed_data = memnew(PackedData);
  193. #ifdef MINIZIP_ENABLED
  194. packed_data->add_pack_source(ZipArchive::get_singleton());
  195. #endif
  196. bool editor=false;
  197. while(I) {
  198. List<String>::Element *N=I->next();
  199. if (I->get() == "-noop") {
  200. // no op
  201. } else if (I->get()=="-h" || I->get()=="--help" || I->get()=="/?") { // resolution
  202. goto error;
  203. } else if (I->get()=="-r") { // resolution
  204. if (I->next()) {
  205. String vm=I->next()->get();
  206. if (vm.find("x")==-1) { // invalid parameter format
  207. goto error;
  208. }
  209. int w=vm.get_slice("x",0).to_int();
  210. int h=vm.get_slice("x",1).to_int();
  211. if (w==0 || h==0) {
  212. goto error;
  213. }
  214. video_mode.width=w;
  215. video_mode.height=h;
  216. force_res=true;
  217. N=I->next()->next();
  218. } else {
  219. goto error;
  220. }
  221. } else if (I->get()=="-vd") { // video driver
  222. if (I->next()) {
  223. video_driver=I->next()->get();
  224. N=I->next()->next();
  225. } else {
  226. goto error;
  227. }
  228. } else if (I->get()=="-lang") { // language
  229. if (I->next()) {
  230. locale=I->next()->get();
  231. N=I->next()->next();
  232. } else {
  233. goto error;
  234. }
  235. } else if (I->get()=="-rfs") { // language
  236. if (I->next()) {
  237. remotefs=I->next()->get();
  238. N=I->next()->next();
  239. } else {
  240. goto error;
  241. }
  242. } else if (I->get()=="-rfs_pass") { // language
  243. if (I->next()) {
  244. remotefs_pass=I->next()->get();
  245. N=I->next()->next();
  246. } else {
  247. goto error;
  248. }
  249. } else if (I->get()=="-rthread") { // language
  250. if (I->next()) {
  251. if (I->next()->get()=="safe")
  252. rtm=OS::RENDER_THREAD_SAFE;
  253. else if (I->next()->get()=="unsafe")
  254. rtm=OS::RENDER_THREAD_UNSAFE;
  255. else if (I->next()->get()=="separate")
  256. rtm=OS::RENDER_SEPARATE_THREAD;
  257. N=I->next()->next();
  258. } else {
  259. goto error;
  260. }
  261. } else if (I->get()=="-ad") { // video driver
  262. if (I->next()) {
  263. audio_driver=I->next()->get();
  264. N=I->next()->next();
  265. } else {
  266. goto error;
  267. }
  268. } else if (I->get()=="-f") { // fullscreen
  269. video_mode.fullscreen=true;
  270. } else if (I->get()=="-e" || I->get()=="-editor") { // fonud editor
  271. editor=true;
  272. } else if (I->get()=="-nowindow") { // fullscreen
  273. OS::get_singleton()->set_no_window_mode(true);
  274. } else if (I->get()=="-v") { // fullscreen
  275. OS::get_singleton()->_verbose_stdout=true;
  276. } else if (I->get()=="-path") { // resolution
  277. if (I->next()) {
  278. String p = I->next()->get();
  279. if (OS::get_singleton()->set_cwd(p)==OK) {
  280. //nothing
  281. } else {
  282. game_path=I->next()->get(); //use game_path instead
  283. }
  284. N=I->next()->next();
  285. } else {
  286. goto error;
  287. }
  288. } else if (I->get()=="-bp") { // /breakpoints
  289. if (I->next()) {
  290. String bplist = I->next()->get();
  291. breakpoints= bplist.split(",");
  292. N=I->next()->next();
  293. } else {
  294. goto error;
  295. }
  296. } else if (I->get()=="-fdelay") { // resolution
  297. if (I->next()) {
  298. OS::get_singleton()->set_frame_delay(I->next()->get().to_int());
  299. N=I->next()->next();
  300. } else {
  301. goto error;
  302. }
  303. } else if (I->get() == "-pack") {
  304. if (I->next()) {
  305. pack_list.push_back(I->next()->get());
  306. N = I->next()->next();
  307. } else {
  308. goto error;
  309. };
  310. } else if (I->get()=="-debug" || I->get()=="-d") {
  311. debug_mode="local";
  312. } else if (I->get()=="-editor_scene") {
  313. if (I->next()) {
  314. Globals::get_singleton()->set("editor_scene",game_path=I->next()->get());
  315. } else {
  316. goto error;
  317. }
  318. } else if (I->get()=="-rdebug") {
  319. if (I->next()) {
  320. debug_mode="remote";
  321. debug_host=I->next()->get();
  322. if (debug_host.find(":")==-1) //wrong host
  323. goto error;
  324. N=I->next()->next();
  325. } else {
  326. goto error;
  327. }
  328. } else {
  329. //test for game path
  330. bool gpfound=false;
  331. if (!I->get().begins_with("-") && game_path=="") {
  332. DirAccess* da = DirAccess::open(I->get());
  333. if (da!=NULL) {
  334. game_path=I->get();
  335. gpfound=true;
  336. memdelete(da);
  337. }
  338. }
  339. if (!gpfound) {
  340. main_args.push_back(I->get());
  341. }
  342. }
  343. I=N;
  344. }
  345. if (debug_mode == "remote") {
  346. ScriptDebuggerRemote *sdr = memnew( ScriptDebuggerRemote );
  347. uint16_t debug_port = GLOBAL_DEF("debug/remote_port",6007);
  348. if (debug_host.find(":")!=-1) {
  349. debug_port=debug_host.get_slice(":",1).to_int();
  350. debug_host=debug_host.get_slice(":",0);
  351. }
  352. Error derr = sdr->connect_to_host(debug_host,debug_port);
  353. if (derr!=OK) {
  354. memdelete(sdr);
  355. } else {
  356. script_debugger=sdr;
  357. }
  358. } else if (debug_mode=="local") {
  359. script_debugger = memnew( ScriptDebuggerLocal );
  360. }
  361. if (remotefs!="") {
  362. file_access_network_client=memnew(FileAccessNetworkClient);
  363. int port;
  364. if (remotefs.find(":")!=-1) {
  365. port=remotefs.get_slice(":",1).to_int();
  366. remotefs=remotefs.get_slice(":",0);
  367. } else {
  368. port=6010;
  369. }
  370. Error err = file_access_network_client->connect(remotefs,port,remotefs_pass);
  371. if (err) {
  372. OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i\n",remotefs.utf8().get_data(),port);
  373. goto error;
  374. }
  375. FileAccess::make_default<FileAccessNetwork>(FileAccess::ACCESS_RESOURCES);
  376. }
  377. if (script_debugger) {
  378. //there is a debugger, parse breakpoints
  379. for(int i=0;i<breakpoints.size();i++) {
  380. String bp = breakpoints[i];
  381. int sp=bp.find_last(":");
  382. if (sp==-1) {
  383. ERR_EXPLAIN("Invalid breakpoint: '"+bp+"', expected file:line format.");
  384. ERR_CONTINUE(sp==-1);
  385. }
  386. script_debugger->insert_breakpoint(bp.substr(sp+1,bp.length()).to_int(),bp.substr(0,sp));
  387. }
  388. }
  389. #ifdef TOOLS_ENABLED
  390. if (editor) {
  391. packed_data->set_disabled(true);
  392. globals->set_disable_platform_override(true);
  393. }
  394. #endif
  395. if (globals->setup(game_path)!=OK) {
  396. #ifdef TOOLS_ENABLED
  397. editor=false;
  398. #else
  399. OS::get_singleton()->print("error: Couldn't load game path '%s'\n",game_path.ascii().get_data());
  400. goto error;
  401. #endif
  402. }
  403. if (editor) {
  404. main_args.push_back("-editor");
  405. use_custom_res=false;
  406. }
  407. OS::get_singleton()->set_cmdline(execpath, main_args);
  408. #ifdef TOOLS_ENABLED
  409. if (main_args.size()==0 && (!Globals::get_singleton()->has("application/main_loop_type")) && (!Globals::get_singleton()->has("application/main_scene") || String(Globals::get_singleton()->get("application/main_scene"))==""))
  410. use_custom_res=false; //project manager (run without arguments)
  411. #endif
  412. input_map->load_from_globals();
  413. if (video_driver=="") // specified in engine.cfg
  414. video_driver=_GLOBAL_DEF("display/driver",Variant((const char*)OS::get_singleton()->get_video_driver_name(0)));
  415. if (!force_res && use_custom_res && globals->has("display/width"))
  416. video_mode.width=globals->get("display/width");
  417. if (!force_res &&use_custom_res && globals->has("display/height"))
  418. video_mode.height=globals->get("display/height");
  419. if (use_custom_res && globals->has("display/fullscreen"))
  420. video_mode.fullscreen=globals->get("display/fullscreen");
  421. GLOBAL_DEF("display/width",video_mode.width);
  422. GLOBAL_DEF("display/height",video_mode.height);
  423. GLOBAL_DEF("display/fullscreen",video_mode.fullscreen);
  424. if (rtm==-1) {
  425. rtm=GLOBAL_DEF("render/thread_model",OS::RENDER_THREAD_SAFE);
  426. }
  427. if (rtm>=0 && rtm<3)
  428. OS::get_singleton()->_render_thread_mode=OS::RenderThreadMode(rtm);
  429. /* Determine Video Driver */
  430. if (audio_driver=="") // specified in engine.cfg
  431. audio_driver=GLOBAL_DEF("audio/driver",OS::get_singleton()->get_audio_driver_name(0));
  432. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  433. if (video_driver==OS::get_singleton()->get_video_driver_name(i)) {
  434. video_driver_idx=i;
  435. break;
  436. }
  437. }
  438. if (video_driver_idx<0) {
  439. OS::get_singleton()->alert( "Invalid Video Driver: "+video_driver );
  440. video_driver_idx = 0;
  441. //goto error;
  442. }
  443. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  444. if (audio_driver==OS::get_singleton()->get_audio_driver_name(i)) {
  445. audio_driver_idx=i;
  446. break;
  447. }
  448. }
  449. if (audio_driver_idx<0) {
  450. OS::get_singleton()->alert( "Invalid Audio Driver: "+audio_driver );
  451. goto error;
  452. }
  453. {
  454. String orientation = GLOBAL_DEF("display/orientation","landscape");
  455. if (orientation=="portrait")
  456. OS::get_singleton()->set_screen_orientation(OS::SCREEN_PORTRAIT);
  457. else if (orientation=="reverse_landscape")
  458. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_LANDSCAPE);
  459. else if (orientation=="reverse_portrait")
  460. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_PORTRAIT);
  461. else if (orientation=="sensor_landscape")
  462. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_LANDSCAPE);
  463. else if (orientation=="sensor_portrait")
  464. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_PORTRAIT);
  465. else if (orientation=="sensor")
  466. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR);
  467. else
  468. OS::get_singleton()->set_screen_orientation(OS::SCREEN_LANDSCAPE);
  469. }
  470. OS::get_singleton()->set_iterations_per_second(GLOBAL_DEF("display/target_fps",60));
  471. if (!OS::get_singleton()->_verbose_stdout) //overrided
  472. OS::get_singleton()->_verbose_stdout=GLOBAL_DEF("debug/verbose_stdout",false);
  473. message_queue = memnew( MessageQueue );
  474. Globals::get_singleton()->register_global_defaults();
  475. if (p_second_phase)
  476. return setup2();
  477. return OK;
  478. error:
  479. video_driver="";
  480. audio_driver="";
  481. game_path="";
  482. args.clear();
  483. main_args.clear();
  484. print_help(execpath);
  485. if (performance)
  486. memdelete(performance);
  487. if (input_map)
  488. memdelete(input_map);
  489. if (translation_server)
  490. memdelete( translation_server );
  491. if (globals)
  492. memdelete(globals);
  493. if (script_debugger)
  494. memdelete(script_debugger);
  495. if (packed_data)
  496. memdelete(packed_data);
  497. if (file_access_network_client)
  498. memdelete(file_access_network_client);
  499. unregister_core_types();
  500. OS::get_singleton()->_cmdline.clear();
  501. if (message_queue)
  502. memdelete( message_queue);
  503. OS::get_singleton()->finalize_core();
  504. locale=String();
  505. return ERR_INVALID_PARAMETER;
  506. }
  507. Error Main::setup2() {
  508. OS::get_singleton()->initialize(video_mode,video_driver_idx,audio_driver_idx);
  509. register_core_singletons();
  510. MAIN_PRINT("Main: Setup Logo");
  511. bool show_logo=true;
  512. #ifdef JAVASCRIPT_ENABLED
  513. show_logo=false;
  514. #endif
  515. if (show_logo) { //boot logo!
  516. Image boot_logo=GLOBAL_DEF("application/boot_logo",Image());
  517. if (!boot_logo.empty()) {
  518. Color clear = GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3));
  519. VisualServer::get_singleton()->set_default_clear_color(clear);
  520. Color boot_bg = GLOBAL_DEF("application/boot_bg_color", clear);
  521. VisualServer::get_singleton()->set_boot_image(boot_logo, boot_bg);
  522. #ifndef TOOLS_ENABLED
  523. //no tools, so free the boot logo (no longer needed)
  524. Globals::get_singleton()->set("application/boot_logo",Image());
  525. #endif
  526. } else {
  527. #ifndef NO_DEFAULT_BOOT_LOGO
  528. MAIN_PRINT("Main: Create botsplash");
  529. Image splash(boot_splash_png);
  530. MAIN_PRINT("Main: ClearColor");
  531. VisualServer::get_singleton()->set_default_clear_color(boot_splash_bg_color);
  532. MAIN_PRINT("Main: Image");
  533. VisualServer::get_singleton()->set_boot_image(splash, boot_splash_bg_color);
  534. #endif
  535. MAIN_PRINT("Main: DCC");
  536. VisualServer::get_singleton()->set_default_clear_color(GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3)));
  537. MAIN_PRINT("Main: END");
  538. }
  539. Image icon(app_icon_png);
  540. OS::get_singleton()->set_icon(icon);
  541. }
  542. GLOBAL_DEF("application/icon",String());
  543. Globals::get_singleton()->set_custom_property_info("application/icon",PropertyInfo(Variant::STRING,"application/icon",PROPERTY_HINT_FILE,"*.png,*.webp"));
  544. MAIN_PRINT("Main: Load Remaps");
  545. path_remap->load_remaps();
  546. MAIN_PRINT("Main: Load Scene Types");
  547. register_scene_types();
  548. register_server_types();
  549. #ifdef TOOLS_ENABLED
  550. EditorNode::register_editor_types();
  551. #endif
  552. MAIN_PRINT("Main: Load Scripts, Modules, Drivers");
  553. register_module_types();
  554. register_script_types();
  555. register_driver_types();
  556. MAIN_PRINT("Main: Load Translations");
  557. translation_server->setup(); //register translations, load them, etc.
  558. if (locale!="") {
  559. translation_server->set_locale(locale);
  560. }
  561. translation_server->load_translations();
  562. _start_success=true;
  563. locale=String();
  564. MAIN_PRINT("Main: Done");
  565. return OK;
  566. }
  567. bool Main::start() {
  568. ERR_FAIL_COND_V(!_start_success,false);
  569. bool editor=false;
  570. String doc_tool;
  571. bool doc_base=true;
  572. String game_path;
  573. String script;
  574. String test;
  575. String screen;
  576. String optimize;
  577. String optimize_preset;
  578. String _export_platform;
  579. String _import;
  580. String _import_script;
  581. String dumpstrings;
  582. bool noquit=false;
  583. bool convert_old=false;
  584. bool export_debug=false;
  585. List<String> args = OS::get_singleton()->get_cmdline_args();
  586. for (int i=0;i<args.size();i++) {
  587. if (args[i]=="-doctool" && i <(args.size()-1)) {
  588. doc_tool=args[i+1];
  589. i++;
  590. }else if (args[i]=="-nodocbase") {
  591. doc_base=false;
  592. } else if ((args[i]=="-script" || args[i]=="-s") && i <(args.size()-1)) {
  593. script=args[i+1];
  594. i++;
  595. } else if ((args[i]=="-level" || args[i]=="-l") && i <(args.size()-1)) {
  596. OS::get_singleton()->_custom_level=args[i+1];
  597. i++;
  598. } else if (args[i]=="-test" && i <(args.size()-1)) {
  599. test=args[i+1];
  600. i++;
  601. } else if (args[i]=="-optimize" && i <(args.size()-1)) {
  602. optimize=args[i+1];
  603. i++;
  604. } else if (args[i]=="-optimize_preset" && i <(args.size()-1)) {
  605. optimize_preset=args[i+1];
  606. i++;
  607. } else if (args[i]=="-export" && i <(args.size()-1)) {
  608. editor=true; //needs editor
  609. _export_platform=args[i+1];
  610. i++;
  611. } else if (args[i]=="-export_debug" && i <(args.size()-1)) {
  612. editor=true; //needs editor
  613. _export_platform=args[i+1];
  614. export_debug=true;
  615. i++;
  616. } else if (args[i]=="-import" && i <(args.size()-1)) {
  617. editor=true; //needs editor
  618. _import=args[i+1];
  619. i++;
  620. } else if (args[i]=="-import_script" && i <(args.size()-1)) {
  621. editor=true; //needs editor
  622. _import_script=args[i+1];
  623. i++;
  624. } else if (args[i]=="-noquit" ) {
  625. noquit=true;
  626. } else if (args[i]=="-dumpstrings" && i <(args.size()-1)) {
  627. editor=true; //needs editor
  628. dumpstrings=args[i+1];
  629. i++;
  630. } else if (args[i]=="-editor" || args[i]=="-e") {
  631. editor=true;
  632. } else if (args[i]=="-convert_old") {
  633. convert_old=true;
  634. } else if (args[i].length() && args[i][0] != '-' && game_path == "") {
  635. game_path=args[i];
  636. }
  637. }
  638. print_line("editor: "+itos(editor));
  639. if (editor)
  640. Globals::get_singleton()->set("editor_active",true);
  641. String main_loop_type;
  642. #ifdef TOOLS_ENABLED
  643. if(doc_tool!="") {
  644. DocData doc;
  645. doc.generate(doc_base);
  646. DocData docsrc;
  647. if (docsrc.load(doc_tool)==OK) {
  648. print_line("Doc exists. Merging..");
  649. doc.merge_from(docsrc);
  650. } else {
  651. print_line("No Doc exists. Generating empty.");
  652. }
  653. doc.save(doc_tool);
  654. return false;
  655. }
  656. if (optimize!="")
  657. editor=true; //need editor
  658. #endif
  659. if(script=="" && game_path=="" && !editor && String(GLOBAL_DEF("application/main_scene",""))!="") {
  660. game_path=GLOBAL_DEF("application/main_scene","");
  661. }
  662. MainLoop *main_loop=NULL;
  663. if (editor) {
  664. main_loop = memnew(SceneMainLoop);
  665. };
  666. if (test!="") {
  667. #ifdef DEBUG_ENABLED
  668. main_loop = test_main(test,args);
  669. if (!main_loop)
  670. return false;
  671. #endif
  672. } else if (script!="") {
  673. Ref<Script> script_res = ResourceLoader::load(script);
  674. ERR_EXPLAIN("Can't load script: "+script);
  675. ERR_FAIL_COND_V(script_res.is_null(),false);
  676. if( script_res->can_instance() /*&& script_res->inherits_from("SceneMainLoopScripted")*/) {
  677. StringName instance_type=script_res->get_instance_base_type();
  678. Object *obj = ObjectTypeDB::instance(instance_type);
  679. MainLoop *script_loop = obj?obj->cast_to<MainLoop>():NULL;
  680. if (!script_loop) {
  681. if (obj)
  682. memdelete(obj);
  683. ERR_EXPLAIN("Can't load script '"+script+"', it does not inherit from a MainLoop type");
  684. ERR_FAIL_COND_V(!script_loop,false);
  685. }
  686. script_loop->set_init_script(script_res);
  687. main_loop=script_loop;
  688. } else {
  689. return false;
  690. }
  691. } else {
  692. main_loop_type=GLOBAL_DEF("application/main_loop_type","");
  693. }
  694. if (!main_loop && main_loop_type=="")
  695. main_loop_type="SceneMainLoop";
  696. if (!main_loop) {
  697. if (!ObjectTypeDB::type_exists(main_loop_type)) {
  698. OS::get_singleton()->alert("godot: error: MainLoop type doesn't exist: "+main_loop_type);
  699. return false;
  700. } else {
  701. Object *ml = ObjectTypeDB::instance(main_loop_type);
  702. if (!ml) {
  703. ERR_EXPLAIN("Can't instance MainLoop type");
  704. ERR_FAIL_V(false);
  705. }
  706. main_loop=ml->cast_to<MainLoop>();
  707. if (!main_loop) {
  708. memdelete(ml);
  709. ERR_EXPLAIN("Invalid MainLoop type");
  710. ERR_FAIL_V(false);
  711. }
  712. }
  713. }
  714. if (main_loop->is_type("SceneMainLoop")) {
  715. SceneMainLoop *sml = main_loop->cast_to<SceneMainLoop>();
  716. #ifdef TOOLS_ENABLED
  717. EditorNode *editor_node=NULL;
  718. if (editor) {
  719. editor_node = memnew( EditorNode );
  720. sml->get_root()->add_child(editor_node);
  721. //root_node->set_editor(editor);
  722. //startup editor
  723. if (_export_platform!="") {
  724. editor_node->export_platform(_export_platform,game_path,export_debug,"",true);
  725. game_path=""; //no load anything
  726. }
  727. }
  728. #endif
  729. if (!editor) {
  730. //standard helpers that can be changed from main config
  731. if (GLOBAL_DEF("display/stretch_2d",false).operator bool()) {
  732. sml->get_root()->set_size_override(true,Size2(Globals::get_singleton()->get("display/width"),Globals::get_singleton()->get("display/height")));
  733. sml->get_root()->set_size_override_stretch(true);
  734. }
  735. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  736. String appname = Globals::get_singleton()->get("application/name");
  737. appname = TranslationServer::get_singleton()->translate(appname);
  738. OS::get_singleton()->set_window_title(appname);
  739. } else {
  740. GLOBAL_DEF("display/stretch_2d",false);
  741. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  742. }
  743. if (game_path!="") {
  744. String local_game_path=game_path.replace("\\","/");
  745. if (!local_game_path.begins_with("res://")) {
  746. bool absolute=(local_game_path.size()>1) && (local_game_path[0]=='/' || local_game_path[1]==':');
  747. if (!absolute) {
  748. if (Globals::get_singleton()->is_using_datapack()) {
  749. local_game_path="res://"+local_game_path;
  750. } else {
  751. int sep=local_game_path.find_last("/");
  752. if (sep==-1) {
  753. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  754. local_game_path=da->get_current_dir()+"/"+local_game_path;
  755. memdelete(da) ;
  756. } else {
  757. DirAccess *da = DirAccess::open(local_game_path.substr(0,sep));
  758. if (da) {
  759. local_game_path=da->get_current_dir()+"/"+local_game_path.substr(sep+1,local_game_path.length());;
  760. memdelete(da);
  761. }
  762. }
  763. }
  764. }
  765. }
  766. local_game_path=Globals::get_singleton()->localize_path(local_game_path);
  767. #ifdef TOOLS_ENABLED
  768. if (editor) {
  769. #ifdef OLD_SCENE_FORMAT_ENABLED
  770. if (convert_old)
  771. editor_node->set_convert_old_scene(true);
  772. #endif
  773. if (_import!="") {
  774. //editor_node->import_scene(_import,local_game_path,_import_script);
  775. if (!noquit)
  776. sml->quit();
  777. game_path=""; //no load anything
  778. } else {
  779. Error serr = editor_node->load_scene(local_game_path);
  780. if (serr==OK) {
  781. if (optimize!="") {
  782. editor_node->save_optimized_copy(optimize,optimize_preset);
  783. if (!noquit)
  784. sml->quit();
  785. }
  786. if (dumpstrings!="") {
  787. editor_node->save_translatable_strings(dumpstrings);
  788. if (!noquit)
  789. sml->quit();
  790. }
  791. }
  792. }
  793. //editor_node->set_edited_scene(game);
  794. } else {
  795. #endif
  796. {
  797. //autoload
  798. List<PropertyInfo> props;
  799. Globals::get_singleton()->get_property_list(&props);
  800. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  801. String s = E->get().name;
  802. if (!s.begins_with("autoload/"))
  803. continue;
  804. String name = s.get_slice("/",1);
  805. String path = Globals::get_singleton()->get(s);
  806. RES res = ResourceLoader::load(path);
  807. ERR_EXPLAIN("Can't autoload: "+path);
  808. ERR_CONTINUE(res.is_null());
  809. Node *n=NULL;
  810. if (res->is_type("PackedScene")) {
  811. Ref<PackedScene> ps = res;
  812. n=ps->instance();
  813. } else if (res->is_type("Script")) {
  814. Ref<Script> s = res;
  815. StringName ibt = s->get_instance_base_type();
  816. ERR_EXPLAIN("Script does not inherit a Node: "+path);
  817. ERR_CONTINUE( !ObjectTypeDB::is_type(ibt,"Node") );
  818. Object *obj = ObjectTypeDB::instance(ibt);
  819. ERR_EXPLAIN("Cannot instance node for autoload type: "+String(ibt));
  820. ERR_CONTINUE( obj==NULL );
  821. n = obj->cast_to<Node>();
  822. n->set_script(s.get_ref_ptr());
  823. }
  824. ERR_EXPLAIN("Path in autoload not a node or script: "+path);
  825. ERR_CONTINUE(!n);
  826. n->set_name(name);
  827. sml->get_root()->add_child(n);
  828. }
  829. }
  830. Node *scene=NULL;
  831. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  832. if (scenedata.is_valid())
  833. scene=scenedata->instance();
  834. #ifdef OLD_SCENE_FORMAT_ENABLED
  835. if (!scene) {
  836. scene = SceneLoader::load(local_game_path,true);
  837. }
  838. #endif
  839. ERR_EXPLAIN("Failed loading scene: "+local_game_path);
  840. ERR_FAIL_COND_V(!scene,false)
  841. sml->get_root()->add_child(scene);
  842. String iconpath = GLOBAL_DEF("application/icon","Variant()""");
  843. if (iconpath!="") {
  844. Image icon;
  845. if (icon.load(iconpath)==OK)
  846. OS::get_singleton()->set_icon(icon);
  847. }
  848. //singletons
  849. #ifdef TOOLS_ENABLED
  850. }
  851. #endif
  852. }
  853. #ifdef TOOLS_ENABLED
  854. /*if (_export_platform!="") {
  855. sml->quit();
  856. }*/
  857. /*
  858. if (sml->get_root_node()) {
  859. Console *console = memnew( Console );
  860. sml->get_root_node()->cast_to<RootNode>()->set_console(console);
  861. if (GLOBAL_DEF("console/visible_default",false).operator bool()) {
  862. console->show();
  863. } else {P
  864. console->hide();
  865. };
  866. }
  867. */
  868. if (script=="" && test=="" && game_path=="" && !editor) {
  869. ProjectManager *pmanager = memnew( ProjectManager );
  870. sml->get_root()->add_child(pmanager);
  871. }
  872. #endif
  873. }
  874. OS::get_singleton()->set_main_loop( main_loop );
  875. return true;
  876. }
  877. uint64_t Main::last_ticks=0;
  878. float Main::time_accum=0;
  879. uint32_t Main::frames=0;
  880. uint32_t Main::frame=0;
  881. bool Main::force_redraw_requested = false;
  882. static uint64_t fixed_process_max=0;
  883. static uint64_t idle_process_max=0;
  884. bool Main::iteration() {
  885. uint64_t ticks=OS::get_singleton()->get_ticks_usec();
  886. uint64_t ticks_elapsed=ticks-last_ticks;
  887. frame+=ticks_elapsed;
  888. last_ticks=ticks;
  889. double step=(double)ticks_elapsed / 1000000.0;
  890. float frame_slice=1.0/OS::get_singleton()->get_iterations_per_second();
  891. if (step>frame_slice*8)
  892. step=frame_slice*8;
  893. time_accum+=step;
  894. bool exit=false;
  895. int iters = 0;
  896. while(time_accum>frame_slice) {
  897. uint64_t fixed_begin = OS::get_singleton()->get_ticks_usec();
  898. PhysicsServer::get_singleton()->sync();
  899. PhysicsServer::get_singleton()->flush_queries();
  900. Physics2DServer::get_singleton()->sync();
  901. Physics2DServer::get_singleton()->flush_queries();
  902. if (OS::get_singleton()->get_main_loop()->iteration( frame_slice )) {
  903. exit=true;
  904. break;
  905. }
  906. message_queue->flush();
  907. PhysicsServer::get_singleton()->step(frame_slice);
  908. Physics2DServer::get_singleton()->step(frame_slice);
  909. time_accum-=frame_slice;
  910. message_queue->flush();
  911. //if (AudioServer::get_singleton())
  912. // AudioServer::get_singleton()->update();
  913. fixed_process_max=MAX(OS::get_singleton()->get_ticks_usec()-fixed_begin,fixed_process_max);
  914. iters++;
  915. }
  916. uint64_t idle_begin = OS::get_singleton()->get_ticks_usec();
  917. OS::get_singleton()->get_main_loop()->idle( step );
  918. message_queue->flush();
  919. if (SpatialSoundServer::get_singleton())
  920. SpatialSoundServer::get_singleton()->update( step );
  921. if (SpatialSound2DServer::get_singleton())
  922. SpatialSound2DServer::get_singleton()->update( step );
  923. if (OS::get_singleton()->can_draw()) {
  924. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  925. if (VisualServer::get_singleton()->has_changed()) {
  926. VisualServer::get_singleton()->draw(); // flush visual commands
  927. OS::get_singleton()->frames_drawn++;
  928. }
  929. } else {
  930. VisualServer::get_singleton()->draw(); // flush visual commands
  931. OS::get_singleton()->frames_drawn++;
  932. force_redraw_requested = false;
  933. }
  934. } else {
  935. VisualServer::get_singleton()->flush(); // flush visual commands
  936. }
  937. if (AudioServer::get_singleton())
  938. AudioServer::get_singleton()->update();
  939. for(int i=0;i<ScriptServer::get_language_count();i++) {
  940. ScriptServer::get_language(i)->frame();
  941. }
  942. idle_process_max=MAX(OS::get_singleton()->get_ticks_usec()-idle_begin,idle_process_max);
  943. if (script_debugger)
  944. script_debugger->idle_poll();
  945. // x11_delay_usec(10000);
  946. frames++;
  947. if (frame>1000000) {
  948. if (GLOBAL_DEF("debug/print_fps", OS::get_singleton()->is_stdout_verbose())) {
  949. print_line("FPS: "+itos(frames));
  950. };
  951. OS::get_singleton()->_fps=frames;
  952. performance->set_process_time(idle_process_max/1000000.0);
  953. performance->set_fixed_process_time(fixed_process_max/1000000.0);
  954. idle_process_max=0;
  955. fixed_process_max=0;
  956. if (GLOBAL_DEF("debug/print_metrics", false)) {
  957. //PerformanceMetrics::print();
  958. };
  959. frame%=1000000;
  960. frames=0;
  961. }
  962. if (OS::get_singleton()->is_in_low_processor_usage_mode() || !OS::get_singleton()->can_draw())
  963. OS::get_singleton()->delay_usec(25000); //apply some delay to force idle time
  964. else {
  965. uint32_t frame_delay = OS::get_singleton()->get_frame_delay();
  966. if (frame_delay)
  967. OS::get_singleton()->delay_usec( OS::get_singleton()->get_frame_delay()*1000 );
  968. }
  969. return exit;
  970. }
  971. void Main::force_redraw() {
  972. force_redraw_requested = true;
  973. };
  974. void Main::cleanup() {
  975. ERR_FAIL_COND(!_start_success);
  976. if (script_debugger)
  977. memdelete(script_debugger);
  978. OS::get_singleton()->delete_main_loop();
  979. OS::get_singleton()->_cmdline.clear();
  980. OS::get_singleton()->_execpath="";
  981. OS::get_singleton()->_local_clipboard="";
  982. unregister_driver_types();
  983. unregister_module_types();
  984. unregister_scene_types();
  985. unregister_server_types();
  986. unregister_script_types();
  987. OS::get_singleton()->finalize();
  988. if (packed_data)
  989. memdelete(packed_data);
  990. if (file_access_network_client)
  991. memdelete(file_access_network_client);
  992. if (performance)
  993. memdelete(performance);
  994. if (input_map)
  995. memdelete(input_map);
  996. if (translation_server)
  997. memdelete( translation_server );
  998. if (path_remap)
  999. memdelete(path_remap);
  1000. if (globals)
  1001. memdelete(globals);
  1002. memdelete( message_queue );
  1003. unregister_core_driver_types();
  1004. unregister_core_types();
  1005. //PerformanceMetrics::finish();
  1006. OS::get_singleton()->clear_last_error();
  1007. OS::get_singleton()->finalize_core();
  1008. }