main.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. /*************************************************************************/
  2. /* main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "servers/register_server_types.h"
  37. #include "modules/register_module_types.h"
  38. #include "script_debugger_local.h"
  39. #include "script_debugger_remote.h"
  40. #include "message_queue.h"
  41. #include "path_remap.h"
  42. #include "input_map.h"
  43. #include "io/resource_loader.h"
  44. #include "scene/main/scene_main_loop.h"
  45. #include "script_language.h"
  46. #include "io/resource_loader.h"
  47. #include "bin/tests/test_main.h"
  48. #include "os/dir_access.h"
  49. #include "core/io/ip.h"
  50. #include "scene/resources/packed_scene.h"
  51. #include "scene/main/viewport.h"
  52. #ifdef TOOLS_ENABLED
  53. #include "tools/editor/editor_node.h"
  54. #include "tools/editor/project_manager.h"
  55. #include "tools/pck/pck_packer.h"
  56. #endif
  57. #include "io/file_access_network.h"
  58. #include "tools/doc/doc_data.h"
  59. #include "servers/spatial_sound_server.h"
  60. #include "servers/spatial_sound_2d_server.h"
  61. #include "servers/physics_2d_server.h"
  62. #include "core/io/stream_peer_tcp.h"
  63. #include "core/os/thread.h"
  64. #include "core/io/file_access_pack.h"
  65. #include "core/io/file_access_zip.h"
  66. #include "translation.h"
  67. #include "version.h"
  68. #include "main/input_default.h"
  69. #include "performance.h"
  70. static Globals *globals=NULL;
  71. static InputMap *input_map=NULL;
  72. static bool _start_success=false;
  73. static ScriptDebugger *script_debugger=NULL;
  74. static MessageQueue *message_queue=NULL;
  75. static Performance *performance = NULL;
  76. static PathRemap *path_remap;
  77. static PackedData *packed_data=NULL;
  78. #ifdef MINIZIP_ENABLED
  79. static ZipArchive *zip_packed_data=NULL;
  80. #endif
  81. static FileAccessNetworkClient *file_access_network_client=NULL;
  82. static TranslationServer *translation_server = NULL;
  83. static OS::VideoMode video_mode;
  84. static bool init_maximized=false;
  85. static bool init_windowed=false;
  86. static bool init_fullscreen=false;
  87. static bool init_use_custom_pos=false;
  88. static bool debug_collisions=false;
  89. static bool debug_navigation=false;
  90. static Vector2 init_custom_pos;
  91. static int video_driver_idx=-1;
  92. static int audio_driver_idx=-1;
  93. static String locale;
  94. static int init_screen=-1;
  95. static String unescape_cmdline(const String& p_str) {
  96. return p_str.replace("%20"," ");
  97. }
  98. //#define DEBUG_INIT
  99. #ifdef DEBUG_INIT
  100. #define MAIN_PRINT(m_txt) print_line(m_txt)
  101. #else
  102. #define MAIN_PRINT(m_txt)
  103. #endif
  104. void Main::print_help(const char* p_binary) {
  105. OS::get_singleton()->print(VERSION_FULL_NAME" (c) 2008-2016 Juan Linietsky, Ariel Manzur.\n");
  106. OS::get_singleton()->print("Usage: %s [options] [scene]\n",p_binary);
  107. OS::get_singleton()->print("Options:\n");
  108. OS::get_singleton()->print("\t-path [dir] : Path to a game, containing engine.cfg\n");
  109. #ifdef TOOLS_ENABLED
  110. OS::get_singleton()->print("\t-e,-editor : Bring up the editor instead of running the scene.\n");
  111. #endif
  112. OS::get_singleton()->print("\t-test [test] : Run a test.\n");
  113. OS::get_singleton()->print("\t\t(");
  114. const char **test_names=tests_get_names();
  115. const char* coma = "";
  116. while(*test_names) {
  117. OS::get_singleton()->print("%s%s", coma, *test_names);
  118. test_names++;
  119. coma = ", ";
  120. }
  121. OS::get_singleton()->print(")\n");
  122. OS::get_singleton()->print("\t-r WIDTHxHEIGHT\t : Request Window Resolution\n");
  123. OS::get_singleton()->print("\t-p XxY\t : Request Window Position\n");
  124. OS::get_singleton()->print("\t-f\t\t : Request Fullscreen\n");
  125. OS::get_singleton()->print("\t-mx\t\t Request Maximized\n");
  126. OS::get_singleton()->print("\t-w\t\t Request Windowed\n");
  127. OS::get_singleton()->print("\t-vd DRIVER\t : Video Driver (");
  128. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  129. if (i!=0)
  130. OS::get_singleton()->print(", ");
  131. OS::get_singleton()->print("%s",OS::get_singleton()->get_video_driver_name(i));
  132. }
  133. OS::get_singleton()->print(")\n");
  134. OS::get_singleton()->print("\t-ad DRIVER\t : Audio Driver (");
  135. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  136. if (i!=0)
  137. OS::get_singleton()->print(", ");
  138. OS::get_singleton()->print("%s",OS::get_singleton()->get_audio_driver_name(i));
  139. }
  140. OS::get_singleton()->print(")\n");
  141. OS::get_singleton()->print("\t-rthread <mode>\t : Render Thread Mode ('unsafe', 'safe', 'separate).");
  142. OS::get_singleton()->print(")\n");
  143. OS::get_singleton()->print("\t-s,-script [script] : Run a script.\n");
  144. OS::get_singleton()->print("\t-d,-debug : Debug (local stdout debugger).\n");
  145. OS::get_singleton()->print("\t-rdebug ADDRESS : Remote debug (<ip>:<port> host address).\n");
  146. OS::get_singleton()->print("\t-fdelay [msec]: Simulate high CPU load (delay each frame by [msec]).\n");
  147. OS::get_singleton()->print("\t-timescale [msec]: Simulate high CPU load (delay each frame by [msec]).\n");
  148. OS::get_singleton()->print("\t-bp : breakpoint list as source::line comma separated pairs, no spaces (%%20,%%2C,etc instead).\n");
  149. OS::get_singleton()->print("\t-v : Verbose stdout mode\n");
  150. OS::get_singleton()->print("\t-lang [locale]: Use a specific locale\n");
  151. OS::get_singleton()->print("\t-rfs <host/ip>[:<port>] : Remote FileSystem.\n");
  152. OS::get_singleton()->print("\t-rfs_pass <password> : Password for Remote FileSystem.\n");
  153. #ifdef TOOLS_ENABLED
  154. 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");
  155. OS::get_singleton()->print("\t-nodocbase: Disallow dump the base types (used with -doctool).\n");
  156. OS::get_singleton()->print("\t-optimize FILE Save an optimized copy of scene to FILE.\n");
  157. OS::get_singleton()->print("\t-optimize_preset [preset] Use a given preset for optimization.\n");
  158. OS::get_singleton()->print("\t-export [target] Export the project using given export target.\n");
  159. #endif
  160. }
  161. Error Main::setup(const char *execpath,int argc, char *argv[],bool p_second_phase) {
  162. RID_OwnerBase::init_rid();
  163. OS::get_singleton()->initialize_core();
  164. ObjectTypeDB::init();
  165. MAIN_PRINT("Main: Initialize CORE");
  166. register_core_types();
  167. register_core_driver_types();
  168. MAIN_PRINT("Main: Initialize Globals");
  169. Thread::_main_thread_id = Thread::get_caller_ID();
  170. globals = memnew( Globals );
  171. input_map = memnew( InputMap );
  172. path_remap = memnew( PathRemap );
  173. translation_server = memnew( TranslationServer );
  174. performance = memnew( Performance );
  175. globals->add_singleton(Globals::Singleton("Performance",performance));
  176. MAIN_PRINT("Main: Parse CMDLine");
  177. /* argument parsing and main creation */
  178. List<String> args;
  179. List<String> main_args;
  180. for(int i=0;i<argc;i++) {
  181. args.push_back(String::utf8(argv[i]));
  182. }
  183. List<String>::Element *I=args.front();
  184. I=args.front();
  185. while (I) {
  186. I->get()=unescape_cmdline(I->get().strip_escapes());
  187. // print_line("CMD: "+I->get());
  188. I=I->next();
  189. }
  190. I=args.front();
  191. video_mode = OS::get_singleton()->get_default_video_mode();
  192. String video_driver="";
  193. String audio_driver="";
  194. String game_path=".";
  195. String debug_mode;
  196. String debug_host;
  197. String main_pack;
  198. bool quiet_stdout=false;
  199. int rtm=-1;
  200. String remotefs;
  201. String remotefs_pass;
  202. String screen = "";
  203. List<String> pack_list;
  204. Vector<String> breakpoints;
  205. bool use_custom_res=true;
  206. bool force_res=false;
  207. I=args.front();
  208. packed_data = PackedData::get_singleton();
  209. if (!packed_data)
  210. packed_data = memnew(PackedData);
  211. #ifdef MINIZIP_ENABLED
  212. //XXX: always get_singleton() == 0x0
  213. zip_packed_data = ZipArchive::get_singleton();
  214. //TODO: remove this temporary fix
  215. if (!zip_packed_data) {
  216. zip_packed_data = memnew(ZipArchive);
  217. }
  218. packed_data->add_pack_source(zip_packed_data);
  219. #endif
  220. bool editor=false;
  221. while(I) {
  222. List<String>::Element *N=I->next();
  223. if (I->get() == "-noop") {
  224. // no op
  225. } else if (I->get()=="-h" || I->get()=="--help" || I->get()=="/?") { // resolution
  226. goto error;
  227. } else if (I->get()=="-r") { // resolution
  228. if (I->next()) {
  229. String vm=I->next()->get();
  230. if (vm.find("x")==-1) { // invalid parameter format
  231. OS::get_singleton()->print("Invalid -r argument: %s\n",vm.utf8().get_data());
  232. goto error;
  233. }
  234. int w=vm.get_slice("x",0).to_int();
  235. int h=vm.get_slice("x",1).to_int();
  236. if (w==0 || h==0) {
  237. OS::get_singleton()->print("Invalid -r resolution, x and y must be >0\n");
  238. goto error;
  239. }
  240. video_mode.width=w;
  241. video_mode.height=h;
  242. force_res=true;
  243. N=I->next()->next();
  244. } else {
  245. OS::get_singleton()->print("Invalid -p argument, needs resolution\n");
  246. goto error;
  247. }
  248. } else if (I->get()=="-p") { // position
  249. if (I->next()) {
  250. String vm=I->next()->get();
  251. if (vm.find("x")==-1) { // invalid parameter format
  252. OS::get_singleton()->print("Invalid -p argument: %s\n",vm.utf8().get_data());
  253. goto error;
  254. }
  255. int x=vm.get_slice("x",0).to_int();
  256. int y=vm.get_slice("x",1).to_int();
  257. init_custom_pos=Point2(x,y);
  258. init_use_custom_pos=true;
  259. N=I->next()->next();
  260. } else {
  261. OS::get_singleton()->print("Invalid -r argument, needs position\n");
  262. goto error;
  263. }
  264. } else if (I->get()=="-mx") { // video driver
  265. init_maximized=true;
  266. } else if (I->get()=="-w") { // video driver
  267. init_windowed=true;
  268. } else if (I->get()=="-vd") { // video driver
  269. if (I->next()) {
  270. video_driver=I->next()->get();
  271. N=I->next()->next();
  272. } else {
  273. OS::get_singleton()->print("Invalid -cd argument, needs driver name\n");
  274. goto error;
  275. }
  276. } else if (I->get()=="-lang") { // language
  277. if (I->next()) {
  278. locale=I->next()->get();
  279. N=I->next()->next();
  280. } else {
  281. OS::get_singleton()->print("Invalid -lang argument, needs language code\n");
  282. goto error;
  283. }
  284. } else if (I->get()=="-rfs") { // language
  285. if (I->next()) {
  286. remotefs=I->next()->get();
  287. N=I->next()->next();
  288. } else {
  289. goto error;
  290. }
  291. } else if (I->get()=="-rfs_pass") { // language
  292. if (I->next()) {
  293. remotefs_pass=I->next()->get();
  294. N=I->next()->next();
  295. } else {
  296. goto error;
  297. }
  298. } else if (I->get()=="-rthread") { // language
  299. if (I->next()) {
  300. if (I->next()->get()=="safe")
  301. rtm=OS::RENDER_THREAD_SAFE;
  302. else if (I->next()->get()=="unsafe")
  303. rtm=OS::RENDER_THREAD_UNSAFE;
  304. else if (I->next()->get()=="separate")
  305. rtm=OS::RENDER_SEPARATE_THREAD;
  306. N=I->next()->next();
  307. } else {
  308. goto error;
  309. }
  310. } else if (I->get()=="-ad") { // video driver
  311. if (I->next()) {
  312. audio_driver=I->next()->get();
  313. N=I->next()->next();
  314. } else {
  315. goto error;
  316. }
  317. } else if (I->get()=="-f") { // fullscreen
  318. //video_mode.fullscreen=false;
  319. init_fullscreen=true;
  320. } else if (I->get()=="-e" || I->get()=="-editor") { // fonud editor
  321. editor=true;
  322. } else if (I->get()=="-nowindow") { // fullscreen
  323. OS::get_singleton()->set_no_window_mode(true);
  324. } else if (I->get()=="-quiet") { // fullscreen
  325. quiet_stdout=true;
  326. } else if (I->get()=="-v") { // fullscreen
  327. OS::get_singleton()->_verbose_stdout=true;
  328. } else if (I->get()=="-path") { // resolution
  329. if (I->next()) {
  330. String p = I->next()->get();
  331. if (OS::get_singleton()->set_cwd(p)==OK) {
  332. //nothing
  333. } else {
  334. game_path=I->next()->get(); //use game_path instead
  335. }
  336. N=I->next()->next();
  337. } else {
  338. goto error;
  339. }
  340. } else if (I->get()=="-bp") { // /breakpoints
  341. if (I->next()) {
  342. String bplist = I->next()->get();
  343. breakpoints= bplist.split(",");
  344. N=I->next()->next();
  345. } else {
  346. goto error;
  347. }
  348. } else if (I->get()=="-fdelay") { // resolution
  349. if (I->next()) {
  350. OS::get_singleton()->set_frame_delay(I->next()->get().to_int());
  351. N=I->next()->next();
  352. } else {
  353. goto error;
  354. }
  355. } else if (I->get()=="-timescale") { // resolution
  356. if (I->next()) {
  357. OS::get_singleton()->set_time_scale(I->next()->get().to_double());
  358. N=I->next()->next();
  359. } else {
  360. goto error;
  361. }
  362. } else if (I->get() == "-pack") {
  363. if (I->next()) {
  364. pack_list.push_back(I->next()->get());
  365. N = I->next()->next();
  366. } else {
  367. goto error;
  368. };
  369. } else if (I->get() == "-main_pack") {
  370. if (I->next()) {
  371. main_pack=I->next()->get();
  372. N = I->next()->next();
  373. } else {
  374. goto error;
  375. };
  376. } else if (I->get()=="-debug" || I->get()=="-d") {
  377. debug_mode="local";
  378. } else if (I->get()=="-debugcol" || I->get()=="-dc") {
  379. debug_collisions=true;
  380. } else if (I->get()=="-debugnav" || I->get()=="-dn") {
  381. debug_navigation=true;
  382. } else if (I->get()=="-editor_scene") {
  383. if (I->next()) {
  384. Globals::get_singleton()->set("editor_scene",game_path=I->next()->get());
  385. } else {
  386. goto error;
  387. }
  388. } else if (I->get()=="-rdebug") {
  389. if (I->next()) {
  390. debug_mode="remote";
  391. debug_host=I->next()->get();
  392. if (debug_host.find(":")==-1) { //wrong host
  393. OS::get_singleton()->print("Invalid debug host string\n");
  394. goto error;
  395. }
  396. N=I->next()->next();
  397. } else {
  398. goto error;
  399. }
  400. } else {
  401. //test for game path
  402. bool gpfound=false;
  403. if (!I->get().begins_with("-") && game_path=="") {
  404. DirAccess* da = DirAccess::open(I->get());
  405. if (da!=NULL) {
  406. game_path=I->get();
  407. gpfound=true;
  408. memdelete(da);
  409. }
  410. }
  411. if (!gpfound) {
  412. main_args.push_back(I->get());
  413. }
  414. }
  415. I=N;
  416. }
  417. GLOBAL_DEF("debug/max_remote_stdout_chars_per_second",2048);
  418. if (debug_mode == "remote") {
  419. ScriptDebuggerRemote *sdr = memnew( ScriptDebuggerRemote );
  420. uint16_t debug_port = GLOBAL_DEF("debug/remote_port",6007);
  421. if (debug_host.find(":")!=-1) {
  422. debug_port=debug_host.get_slicec(':',1).to_int();
  423. debug_host=debug_host.get_slicec(':',0);
  424. }
  425. Error derr = sdr->connect_to_host(debug_host,debug_port);
  426. if (derr!=OK) {
  427. memdelete(sdr);
  428. } else {
  429. script_debugger=sdr;
  430. }
  431. } else if (debug_mode=="local") {
  432. script_debugger = memnew( ScriptDebuggerLocal );
  433. }
  434. if (remotefs!="") {
  435. file_access_network_client=memnew(FileAccessNetworkClient);
  436. int port;
  437. if (remotefs.find(":")!=-1) {
  438. port=remotefs.get_slicec(':',1).to_int();
  439. remotefs=remotefs.get_slicec(':',0);
  440. } else {
  441. port=6010;
  442. }
  443. Error err = file_access_network_client->connect(remotefs,port,remotefs_pass);
  444. if (err) {
  445. OS::get_singleton()->printerr("Could not connect to remotefs: %s:%i\n",remotefs.utf8().get_data(),port);
  446. goto error;
  447. }
  448. FileAccess::make_default<FileAccessNetwork>(FileAccess::ACCESS_RESOURCES);
  449. }
  450. if (script_debugger) {
  451. //there is a debugger, parse breakpoints
  452. for(int i=0;i<breakpoints.size();i++) {
  453. String bp = breakpoints[i];
  454. int sp=bp.find_last(":");
  455. if (sp==-1) {
  456. ERR_EXPLAIN("Invalid breakpoint: '"+bp+"', expected file:line format.");
  457. ERR_CONTINUE(sp==-1);
  458. }
  459. script_debugger->insert_breakpoint(bp.substr(sp+1,bp.length()).to_int(),bp.substr(0,sp));
  460. }
  461. }
  462. #ifdef TOOLS_ENABLED
  463. if (editor) {
  464. packed_data->set_disabled(true);
  465. globals->set_disable_platform_override(true);
  466. }
  467. #endif
  468. if (globals->setup(game_path,main_pack)!=OK) {
  469. #ifdef TOOLS_ENABLED
  470. editor=false;
  471. #else
  472. OS::get_singleton()->print("error: Couldn't load game path '%s'\n",game_path.ascii().get_data());
  473. goto error;
  474. #endif
  475. }
  476. if (editor) {
  477. main_args.push_back("-editor");
  478. init_maximized=true;
  479. use_custom_res=false;
  480. }
  481. if (bool(Globals::get_singleton()->get("application/disable_stdout"))) {
  482. quiet_stdout=true;
  483. }
  484. if (bool(Globals::get_singleton()->get("application/disable_stderr"))) {
  485. _print_error_enabled = false;
  486. };
  487. if (quiet_stdout)
  488. _print_line_enabled=false;
  489. OS::get_singleton()->set_cmdline(execpath, main_args);
  490. #ifdef TOOLS_ENABLED
  491. 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"))==""))
  492. use_custom_res=false; //project manager (run without arguments)
  493. #endif
  494. input_map->load_from_globals();
  495. if (video_driver=="") // specified in engine.cfg
  496. video_driver=_GLOBAL_DEF("display/driver",Variant((const char*)OS::get_singleton()->get_video_driver_name(0)));
  497. if (!force_res && use_custom_res && globals->has("display/width"))
  498. video_mode.width=globals->get("display/width");
  499. if (!force_res &&use_custom_res && globals->has("display/height"))
  500. video_mode.height=globals->get("display/height");
  501. if (use_custom_res && globals->has("display/fullscreen"))
  502. video_mode.fullscreen=globals->get("display/fullscreen");
  503. if (use_custom_res && globals->has("display/resizable"))
  504. video_mode.resizable=globals->get("display/resizable");
  505. if (!force_res && use_custom_res && globals->has("display/test_width") && globals->has("display/test_height")) {
  506. int tw = globals->get("display/test_width");
  507. int th = globals->get("display/test_height");
  508. if (tw>0 && th>0) {
  509. video_mode.width=tw;
  510. video_mode.height=th;
  511. }
  512. }
  513. GLOBAL_DEF("display/width",video_mode.width);
  514. GLOBAL_DEF("display/height",video_mode.height);
  515. GLOBAL_DEF("display/fullscreen",video_mode.fullscreen);
  516. GLOBAL_DEF("display/resizable",video_mode.resizable);
  517. GLOBAL_DEF("display/test_width",0);
  518. GLOBAL_DEF("display/test_height",0);
  519. OS::get_singleton()->_pixel_snap=GLOBAL_DEF("display/use_2d_pixel_snap",false);
  520. OS::get_singleton()->_keep_screen_on=GLOBAL_DEF("display/keep_screen_on",true);
  521. if (rtm==-1) {
  522. rtm=GLOBAL_DEF("render/thread_model",OS::RENDER_THREAD_SAFE);
  523. if (rtm>=1) //hack for now
  524. rtm=1;
  525. }
  526. if (rtm>=0 && rtm<3) {
  527. if (editor) {
  528. rtm=OS::RENDER_THREAD_SAFE;
  529. }
  530. OS::get_singleton()->_render_thread_mode=OS::RenderThreadMode(rtm);
  531. }
  532. /* Determine Video Driver */
  533. if (audio_driver=="") { // specified in engine.cfg
  534. audio_driver=GLOBAL_DEF("audio/driver",OS::get_singleton()->get_audio_driver_name(0));
  535. }
  536. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  537. if (video_driver==OS::get_singleton()->get_video_driver_name(i)) {
  538. video_driver_idx=i;
  539. break;
  540. }
  541. }
  542. if (video_driver_idx<0) {
  543. OS::get_singleton()->alert( "Invalid Video Driver: "+video_driver );
  544. video_driver_idx = 0;
  545. //goto error;
  546. }
  547. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  548. if (audio_driver==OS::get_singleton()->get_audio_driver_name(i)) {
  549. audio_driver_idx=i;
  550. break;
  551. }
  552. }
  553. if (audio_driver_idx<0) {
  554. OS::get_singleton()->alert( "Invalid Audio Driver: "+audio_driver );
  555. audio_driver_idx = 0;
  556. //goto error;
  557. }
  558. {
  559. String orientation = GLOBAL_DEF("display/orientation","landscape");
  560. if (orientation=="portrait")
  561. OS::get_singleton()->set_screen_orientation(OS::SCREEN_PORTRAIT);
  562. else if (orientation=="reverse_landscape")
  563. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_LANDSCAPE);
  564. else if (orientation=="reverse_portrait")
  565. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_PORTRAIT);
  566. else if (orientation=="sensor_landscape")
  567. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_LANDSCAPE);
  568. else if (orientation=="sensor_portrait")
  569. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_PORTRAIT);
  570. else if (orientation=="sensor")
  571. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR);
  572. else
  573. OS::get_singleton()->set_screen_orientation(OS::SCREEN_LANDSCAPE);
  574. }
  575. OS::get_singleton()->set_iterations_per_second(GLOBAL_DEF("physics/fixed_fps",60));
  576. OS::get_singleton()->set_target_fps(GLOBAL_DEF("application/target_fps",0));
  577. if (!OS::get_singleton()->_verbose_stdout) //overrided
  578. OS::get_singleton()->_verbose_stdout=GLOBAL_DEF("debug/verbose_stdout",false);
  579. message_queue = memnew( MessageQueue );
  580. Globals::get_singleton()->register_global_defaults();
  581. if (p_second_phase)
  582. return setup2();
  583. return OK;
  584. error:
  585. video_driver="";
  586. audio_driver="";
  587. game_path="";
  588. args.clear();
  589. main_args.clear();
  590. print_help(execpath);
  591. if (performance)
  592. memdelete(performance);
  593. if (input_map)
  594. memdelete(input_map);
  595. if (translation_server)
  596. memdelete( translation_server );
  597. if (globals)
  598. memdelete(globals);
  599. if (script_debugger)
  600. memdelete(script_debugger);
  601. if (packed_data)
  602. memdelete(packed_data);
  603. if (file_access_network_client)
  604. memdelete(file_access_network_client);
  605. if(path_remap)
  606. memdelete(path_remap);
  607. // Note 1: *zip_packed_data live into *packed_data
  608. // Note 2: PackedData::~PackedData destroy this.
  609. //#ifdef MINIZIP_ENABLED
  610. // if (zip_packed_data)
  611. // memdelete( zip_packed_data );
  612. //#endif
  613. unregister_core_driver_types();
  614. unregister_core_types();
  615. OS::get_singleton()->_cmdline.clear();
  616. if (message_queue)
  617. memdelete( message_queue);
  618. OS::get_singleton()->finalize_core();
  619. locale=String();
  620. return ERR_INVALID_PARAMETER;
  621. }
  622. Error Main::setup2() {
  623. OS::get_singleton()->initialize(video_mode,video_driver_idx,audio_driver_idx);
  624. if (init_use_custom_pos) {
  625. OS::get_singleton()->set_window_position(init_custom_pos);
  626. }
  627. register_core_singletons();
  628. MAIN_PRINT("Main: Setup Logo");
  629. bool show_logo=true;
  630. #ifdef JAVASCRIPT_ENABLED
  631. show_logo=false;
  632. #endif
  633. if (init_screen!=-1) {
  634. OS::get_singleton()->set_current_screen(init_screen);
  635. }
  636. if (init_windowed) {
  637. //do none..
  638. } else if (init_maximized) {
  639. OS::get_singleton()->set_window_maximized(true);
  640. } else if (init_fullscreen) {
  641. OS::get_singleton()->set_window_fullscreen(true);
  642. }
  643. MAIN_PRINT("Main: Load Remaps");
  644. path_remap->load_remaps();
  645. if (show_logo) { //boot logo!
  646. String boot_logo_path=GLOBAL_DEF("application/boot_splash",String());
  647. bool boot_logo_scale=GLOBAL_DEF("application/boot_splash_fullsize",true);
  648. Globals::get_singleton()->set_custom_property_info("application/boot_splash",PropertyInfo(Variant::STRING,"application/boot_splash",PROPERTY_HINT_FILE,"*.png"));
  649. Image boot_logo;
  650. boot_logo_path = boot_logo_path.strip_edges();
  651. if (boot_logo_path!=String() /*&& FileAccess::exists(boot_logo_path)*/) {
  652. print_line("Boot splash path: "+boot_logo_path);
  653. Error err = boot_logo.load(boot_logo_path);
  654. }
  655. if (!boot_logo.empty()) {
  656. OS::get_singleton()->_msec_splash=OS::get_singleton()->get_ticks_msec();
  657. Color clear = GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3));
  658. VisualServer::get_singleton()->set_default_clear_color(clear);
  659. Color boot_bg = GLOBAL_DEF("application/boot_bg_color", clear);
  660. VisualServer::get_singleton()->set_boot_image(boot_logo, boot_bg,boot_logo_scale);
  661. #ifndef TOOLS_ENABLED
  662. //no tools, so free the boot logo (no longer needed)
  663. // Globals::get_singleton()->set("application/boot_logo",Image());
  664. #endif
  665. } else {
  666. #ifndef NO_DEFAULT_BOOT_LOGO
  667. MAIN_PRINT("Main: Create bootsplash");
  668. Image splash(boot_splash_png);
  669. MAIN_PRINT("Main: ClearColor");
  670. VisualServer::get_singleton()->set_default_clear_color(boot_splash_bg_color);
  671. MAIN_PRINT("Main: Image");
  672. VisualServer::get_singleton()->set_boot_image(splash, boot_splash_bg_color,false);
  673. #endif
  674. MAIN_PRINT("Main: DCC");
  675. VisualServer::get_singleton()->set_default_clear_color(GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3)));
  676. MAIN_PRINT("Main: END");
  677. }
  678. Image icon(app_icon_png);
  679. OS::get_singleton()->set_icon(icon);
  680. }
  681. GLOBAL_DEF("application/icon",String());
  682. Globals::get_singleton()->set_custom_property_info("application/icon",PropertyInfo(Variant::STRING,"application/icon",PROPERTY_HINT_FILE,"*.png,*.webp"));
  683. if (bool(GLOBAL_DEF("display/emulate_touchscreen",false))) {
  684. if (!OS::get_singleton()->has_touchscreen_ui_hint() && Input::get_singleton()) {
  685. //only if no touchscreen ui hint, set emulation
  686. InputDefault *id = Input::get_singleton()->cast_to<InputDefault>();
  687. if (id)
  688. id->set_emulate_touch(true);
  689. }
  690. }
  691. MAIN_PRINT("Main: Load Remaps");
  692. MAIN_PRINT("Main: Load Scene Types");
  693. register_scene_types();
  694. register_server_types();
  695. GLOBAL_DEF("display/custom_mouse_cursor",String());
  696. GLOBAL_DEF("display/custom_mouse_cursor_hotspot",Vector2());
  697. Globals::get_singleton()->set_custom_property_info("display/custom_mouse_cursor",PropertyInfo(Variant::STRING,"display/custom_mouse_cursor",PROPERTY_HINT_FILE,"*.png,*.webp"));
  698. if (String(Globals::get_singleton()->get("display/custom_mouse_cursor"))!=String()) {
  699. //print_line("use custom cursor");
  700. Ref<Texture> cursor=ResourceLoader::load(Globals::get_singleton()->get("display/custom_mouse_cursor"));
  701. if (cursor.is_valid()) {
  702. // print_line("loaded ok");
  703. Vector2 hotspot = Globals::get_singleton()->get("display/custom_mouse_cursor_hotspot");
  704. Input::get_singleton()->set_custom_mouse_cursor(cursor,hotspot);
  705. }
  706. }
  707. #ifdef TOOLS_ENABLED
  708. EditorNode::register_editor_types();
  709. ObjectTypeDB::register_type<PCKPacker>(); // todo: move somewhere else
  710. #endif
  711. MAIN_PRINT("Main: Load Scripts, Modules, Drivers");
  712. register_module_types();
  713. register_driver_types();
  714. ScriptServer::init_languages();
  715. MAIN_PRINT("Main: Load Translations");
  716. translation_server->setup(); //register translations, load them, etc.
  717. if (locale!="") {
  718. translation_server->set_locale(locale);
  719. }
  720. translation_server->load_translations();
  721. _start_success=true;
  722. locale=String();
  723. MAIN_PRINT("Main: Done");
  724. return OK;
  725. }
  726. bool Main::start() {
  727. ERR_FAIL_COND_V(!_start_success,false);
  728. bool editor=false;
  729. String doc_tool;
  730. bool doc_base=true;
  731. String game_path;
  732. String script;
  733. String test;
  734. String screen;
  735. String optimize;
  736. String optimize_preset;
  737. String _export_platform;
  738. String _import;
  739. String _import_script;
  740. String dumpstrings;
  741. bool noquit=false;
  742. bool convert_old=false;
  743. bool export_debug=false;
  744. bool project_manager_request = false;
  745. List<String> args = OS::get_singleton()->get_cmdline_args();
  746. for (int i=0;i<args.size();i++) {
  747. //parameters that do not have an argument to the right
  748. if (args[i]=="-nodocbase") {
  749. doc_base=false;
  750. } else if (args[i]=="-noquit") {
  751. noquit=true;
  752. } else if (args[i]=="-convert_old") {
  753. convert_old=true;
  754. } else if (args[i]=="-editor" || args[i]=="-e") {
  755. editor=true;
  756. } else if (args[i] == "-pm" || args[i] == "-project_manager") {
  757. project_manager_request = true;
  758. } else if (args[i].length() && args[i][0] != '-' && game_path == "") {
  759. game_path=args[i];
  760. }
  761. //parameters that have an argument to the right
  762. else if (i < (args.size()-1)) {
  763. bool parsed_pair=true;
  764. if (args[i]=="-doctool") {
  765. doc_tool=args[i+1];
  766. } else if (args[i]=="-script" || args[i]=="-s") {
  767. script=args[i+1];
  768. } else if (args[i]=="-level" || args[i]=="-l") {
  769. OS::get_singleton()->_custom_level=args[i+1];
  770. } else if (args[i]=="-test") {
  771. test=args[i+1];
  772. } else if (args[i]=="-optimize") {
  773. optimize=args[i+1];
  774. } else if (args[i]=="-optimize_preset") {
  775. optimize_preset=args[i+1];
  776. } else if (args[i]=="-export") {
  777. editor=true; //needs editor
  778. _export_platform=args[i+1];
  779. } else if (args[i]=="-export_debug") {
  780. editor=true; //needs editor
  781. _export_platform=args[i+1];
  782. export_debug=true;
  783. } else if (args[i]=="-import") {
  784. editor=true; //needs editor
  785. _import=args[i+1];
  786. } else if (args[i]=="-import_script") {
  787. editor=true; //needs editor
  788. _import_script=args[i+1];
  789. } else if (args[i]=="-dumpstrings") {
  790. editor=true; //needs editor
  791. dumpstrings=args[i+1];
  792. } else {
  793. // The parameter does not match anything known, don't skip the next argument
  794. parsed_pair=false;
  795. }
  796. if (parsed_pair) {
  797. i++;
  798. }
  799. }
  800. }
  801. if (editor)
  802. Globals::get_singleton()->set("editor_active",true);
  803. String main_loop_type;
  804. #ifdef TOOLS_ENABLED
  805. if(doc_tool!="") {
  806. DocData doc;
  807. doc.generate(doc_base);
  808. DocData docsrc;
  809. if (docsrc.load(doc_tool)==OK) {
  810. print_line("Doc exists. Merging..");
  811. doc.merge_from(docsrc);
  812. } else {
  813. print_line("No Doc exists. Generating empty.");
  814. }
  815. doc.save(doc_tool);
  816. return false;
  817. }
  818. if (optimize!="")
  819. editor=true; //need editor
  820. #endif
  821. if (_export_platform!="") {
  822. if (game_path=="") {
  823. String err="Command line param ";
  824. err+=export_debug?"-export_debug":"-export";
  825. err+=" passed but no destination path given.\n";
  826. err+="Please specify the binary's file path to export to. Aborting export.";
  827. ERR_PRINT(err.utf8().get_data());
  828. return false;
  829. }
  830. }
  831. if(script=="" && game_path=="" && String(GLOBAL_DEF("application/main_scene",""))!="") {
  832. game_path=GLOBAL_DEF("application/main_scene","");
  833. }
  834. MainLoop *main_loop=NULL;
  835. if (editor) {
  836. main_loop = memnew(SceneTree);
  837. };
  838. if (test!="") {
  839. #ifdef DEBUG_ENABLED
  840. main_loop = test_main(test,args);
  841. if (!main_loop)
  842. return false;
  843. #endif
  844. } else if (script!="") {
  845. Ref<Script> script_res = ResourceLoader::load(script);
  846. ERR_EXPLAIN("Can't load script: "+script);
  847. ERR_FAIL_COND_V(script_res.is_null(),false);
  848. if( script_res->can_instance() /*&& script_res->inherits_from("SceneTreeScripted")*/) {
  849. StringName instance_type=script_res->get_instance_base_type();
  850. Object *obj = ObjectTypeDB::instance(instance_type);
  851. MainLoop *script_loop = obj?obj->cast_to<MainLoop>():NULL;
  852. if (!script_loop) {
  853. if (obj)
  854. memdelete(obj);
  855. ERR_EXPLAIN("Can't load script '"+script+"', it does not inherit from a MainLoop type");
  856. ERR_FAIL_COND_V(!script_loop,false);
  857. }
  858. script_loop->set_init_script(script_res);
  859. main_loop=script_loop;
  860. } else {
  861. return false;
  862. }
  863. } else {
  864. main_loop_type=GLOBAL_DEF("application/main_loop_type","");
  865. }
  866. if (!main_loop && main_loop_type=="")
  867. main_loop_type="SceneTree";
  868. if (!main_loop) {
  869. if (!ObjectTypeDB::type_exists(main_loop_type)) {
  870. OS::get_singleton()->alert("godot: error: MainLoop type doesn't exist: "+main_loop_type);
  871. return false;
  872. } else {
  873. Object *ml = ObjectTypeDB::instance(main_loop_type);
  874. if (!ml) {
  875. ERR_EXPLAIN("Can't instance MainLoop type");
  876. ERR_FAIL_V(false);
  877. }
  878. main_loop=ml->cast_to<MainLoop>();
  879. if (!main_loop) {
  880. memdelete(ml);
  881. ERR_EXPLAIN("Invalid MainLoop type");
  882. ERR_FAIL_V(false);
  883. }
  884. }
  885. }
  886. if (main_loop->is_type("SceneTree")) {
  887. SceneTree *sml = main_loop->cast_to<SceneTree>();
  888. if (debug_collisions) {
  889. sml->set_debug_collisions_hint(true);
  890. }
  891. if (debug_navigation) {
  892. sml->set_debug_navigation_hint(true);
  893. }
  894. #ifdef TOOLS_ENABLED
  895. EditorNode *editor_node=NULL;
  896. if (editor) {
  897. editor_node = memnew( EditorNode );
  898. sml->get_root()->add_child(editor_node);
  899. //root_node->set_editor(editor);
  900. //startup editor
  901. if (_export_platform!="") {
  902. editor_node->export_platform(_export_platform,game_path,export_debug,"",true);
  903. game_path=""; //no load anything
  904. }
  905. }
  906. #endif
  907. if (!editor) {
  908. //standard helpers that can be changed from main config
  909. String stretch_mode = GLOBAL_DEF("display/stretch_mode","disabled");
  910. String stretch_aspect = GLOBAL_DEF("display/stretch_aspect","ignore");
  911. Size2i stretch_size = Size2(GLOBAL_DEF("display/width",0),GLOBAL_DEF("display/height",0));
  912. SceneTree::StretchMode sml_sm=SceneTree::STRETCH_MODE_DISABLED;
  913. if (stretch_mode=="2d")
  914. sml_sm=SceneTree::STRETCH_MODE_2D;
  915. else if (stretch_mode=="viewport")
  916. sml_sm=SceneTree::STRETCH_MODE_VIEWPORT;
  917. SceneTree::StretchAspect sml_aspect=SceneTree::STRETCH_ASPECT_IGNORE;
  918. if (stretch_aspect=="keep")
  919. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP;
  920. else if (stretch_aspect=="keep_width")
  921. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_WIDTH;
  922. else if (stretch_aspect=="keep_height")
  923. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_HEIGHT;
  924. sml->set_screen_stretch(sml_sm,sml_aspect,stretch_size);
  925. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  926. String appname = Globals::get_singleton()->get("application/name");
  927. appname = TranslationServer::get_singleton()->translate(appname);
  928. OS::get_singleton()->set_window_title(appname);
  929. } else {
  930. GLOBAL_DEF("display/stretch_mode","disabled");
  931. Globals::get_singleton()->set_custom_property_info("display/stretch_mode",PropertyInfo(Variant::STRING,"display/stretch_mode",PROPERTY_HINT_ENUM,"disabled,2d,viewport"));
  932. GLOBAL_DEF("display/stretch_aspect","ignore");
  933. Globals::get_singleton()->set_custom_property_info("display/stretch_aspect",PropertyInfo(Variant::STRING,"display/stretch_aspect",PROPERTY_HINT_ENUM,"ignore,keep,keep_width,keep_height"));
  934. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  935. }
  936. if (game_path!="" && !project_manager_request) {
  937. String local_game_path=game_path.replace("\\","/");
  938. if (!local_game_path.begins_with("res://")) {
  939. bool absolute=(local_game_path.size()>1) && (local_game_path[0]=='/' || local_game_path[1]==':');
  940. if (!absolute) {
  941. if (Globals::get_singleton()->is_using_datapack()) {
  942. local_game_path="res://"+local_game_path;
  943. } else {
  944. int sep=local_game_path.find_last("/");
  945. if (sep==-1) {
  946. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  947. local_game_path=da->get_current_dir()+"/"+local_game_path;
  948. memdelete(da) ;
  949. } else {
  950. DirAccess *da = DirAccess::open(local_game_path.substr(0,sep));
  951. if (da) {
  952. local_game_path=da->get_current_dir()+"/"+local_game_path.substr(sep+1,local_game_path.length());;
  953. memdelete(da);
  954. }
  955. }
  956. }
  957. }
  958. }
  959. local_game_path=Globals::get_singleton()->localize_path(local_game_path);
  960. #ifdef TOOLS_ENABLED
  961. if (editor) {
  962. if (_import!="") {
  963. //editor_node->import_scene(_import,local_game_path,_import_script);
  964. if (!noquit)
  965. sml->quit();
  966. game_path=""; //no load anything
  967. } else {
  968. Error serr = editor_node->load_scene(local_game_path);
  969. if (serr==OK) {
  970. if (optimize!="") {
  971. editor_node->save_optimized_copy(optimize,optimize_preset);
  972. if (!noquit)
  973. sml->quit();
  974. }
  975. if (dumpstrings!="") {
  976. editor_node->save_translatable_strings(dumpstrings);
  977. if (!noquit)
  978. sml->quit();
  979. }
  980. }
  981. }
  982. OS::get_singleton()->set_context(OS::CONTEXT_EDITOR);
  983. //editor_node->set_edited_scene(game);
  984. } else {
  985. #endif
  986. {
  987. //autoload
  988. List<PropertyInfo> props;
  989. Globals::get_singleton()->get_property_list(&props);
  990. //first pass, add the constants so they exist before any script is loaded
  991. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  992. String s = E->get().name;
  993. if (!s.begins_with("autoload/"))
  994. continue;
  995. String name = s.get_slicec('/',1);
  996. String path = Globals::get_singleton()->get(s);
  997. bool global_var=false;
  998. if (path.begins_with("*")) {
  999. global_var=true;
  1000. }
  1001. if (global_var) {
  1002. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1003. ScriptServer::get_language(i)->add_global_constant(name,Variant());
  1004. }
  1005. }
  1006. }
  1007. //second pass, load into global constants
  1008. List<Node*> to_add;
  1009. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  1010. String s = E->get().name;
  1011. if (!s.begins_with("autoload/"))
  1012. continue;
  1013. String name = s.get_slicec('/',1);
  1014. String path = Globals::get_singleton()->get(s);
  1015. bool global_var=false;
  1016. if (path.begins_with("*")) {
  1017. global_var=true;
  1018. path=path.substr(1,path.length()-1);
  1019. }
  1020. RES res = ResourceLoader::load(path);
  1021. ERR_EXPLAIN("Can't autoload: "+path);
  1022. ERR_CONTINUE(res.is_null());
  1023. Node *n=NULL;
  1024. if (res->is_type("PackedScene")) {
  1025. Ref<PackedScene> ps = res;
  1026. n=ps->instance();
  1027. } else if (res->is_type("Script")) {
  1028. Ref<Script> s = res;
  1029. StringName ibt = s->get_instance_base_type();
  1030. bool valid_type = ObjectTypeDB::is_type(ibt,"Node");
  1031. ERR_EXPLAIN("Script does not inherit a Node: "+path);
  1032. ERR_CONTINUE( !valid_type );
  1033. Object *obj = ObjectTypeDB::instance(ibt);
  1034. ERR_EXPLAIN("Cannot instance script for autoload, expected 'Node' inheritance, got: "+String(ibt));
  1035. ERR_CONTINUE( obj==NULL );
  1036. n = obj->cast_to<Node>();
  1037. n->set_script(s.get_ref_ptr());
  1038. }
  1039. ERR_EXPLAIN("Path in autoload not a node or script: "+path);
  1040. ERR_CONTINUE(!n);
  1041. n->set_name(name);
  1042. //defer so references are all valid on _ready()
  1043. //sml->get_root()->add_child(n);
  1044. to_add.push_back(n);
  1045. if (global_var) {
  1046. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1047. ScriptServer::get_language(i)->add_global_constant(name,n);
  1048. }
  1049. }
  1050. }
  1051. for(List<Node*>::Element *E=to_add.front();E;E=E->next()) {
  1052. sml->get_root()->add_child(E->get());
  1053. }
  1054. }
  1055. Node *scene=NULL;
  1056. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  1057. if (scenedata.is_valid())
  1058. scene=scenedata->instance();
  1059. ERR_EXPLAIN("Failed loading scene: "+local_game_path);
  1060. ERR_FAIL_COND_V(!scene,false)
  1061. //sml->get_root()->add_child(scene);
  1062. sml->add_current_scene(scene);
  1063. String iconpath = GLOBAL_DEF("application/icon","Variant()""");
  1064. if (iconpath!="") {
  1065. Image icon;
  1066. if (icon.load(iconpath)==OK)
  1067. OS::get_singleton()->set_icon(icon);
  1068. }
  1069. //singletons
  1070. #ifdef TOOLS_ENABLED
  1071. }
  1072. #endif
  1073. }
  1074. #ifdef TOOLS_ENABLED
  1075. /*if (_export_platform!="") {
  1076. sml->quit();
  1077. }*/
  1078. /*
  1079. if (sml->get_root_node()) {
  1080. Console *console = memnew( Console );
  1081. sml->get_root_node()->cast_to<RootNode>()->set_console(console);
  1082. if (GLOBAL_DEF("console/visible_default",false).operator bool()) {
  1083. console->show();
  1084. } else {P
  1085. console->hide();
  1086. };
  1087. }
  1088. */
  1089. if (project_manager_request || (script=="" && test=="" && game_path=="" && !editor)) {
  1090. ProjectManager *pmanager = memnew( ProjectManager );
  1091. sml->get_root()->add_child(pmanager);
  1092. OS::get_singleton()->set_context(OS::CONTEXT_PROJECTMAN);
  1093. }
  1094. #endif
  1095. }
  1096. OS::get_singleton()->set_main_loop( main_loop );
  1097. return true;
  1098. }
  1099. uint64_t Main::last_ticks=0;
  1100. uint64_t Main::target_ticks=0;
  1101. float Main::time_accum=0;
  1102. uint32_t Main::frames=0;
  1103. uint32_t Main::frame=0;
  1104. bool Main::force_redraw_requested = false;
  1105. static uint64_t fixed_process_max=0;
  1106. static uint64_t idle_process_max=0;
  1107. bool Main::iteration() {
  1108. uint64_t ticks=OS::get_singleton()->get_ticks_usec();
  1109. uint64_t ticks_elapsed=ticks-last_ticks;
  1110. double step=(double)ticks_elapsed / 1000000.0;
  1111. float frame_slice=1.0/OS::get_singleton()->get_iterations_per_second();
  1112. // if (time_accum+step < frame_slice)
  1113. // return false;
  1114. frame+=ticks_elapsed;
  1115. last_ticks=ticks;
  1116. if (step>frame_slice*8)
  1117. step=frame_slice*8;
  1118. time_accum+=step;
  1119. float time_scale = OS::get_singleton()->get_time_scale();
  1120. bool exit=false;
  1121. int iters = 0;
  1122. while(time_accum>frame_slice) {
  1123. uint64_t fixed_begin = OS::get_singleton()->get_ticks_usec();
  1124. PhysicsServer::get_singleton()->sync();
  1125. PhysicsServer::get_singleton()->flush_queries();
  1126. Physics2DServer::get_singleton()->sync();
  1127. Physics2DServer::get_singleton()->flush_queries();
  1128. if (OS::get_singleton()->get_main_loop()->iteration( frame_slice*time_scale )) {
  1129. exit=true;
  1130. break;
  1131. }
  1132. message_queue->flush();
  1133. PhysicsServer::get_singleton()->step(frame_slice*time_scale);
  1134. Physics2DServer::get_singleton()->end_sync();
  1135. Physics2DServer::get_singleton()->step(frame_slice*time_scale);
  1136. time_accum-=frame_slice;
  1137. message_queue->flush();
  1138. //if (AudioServer::get_singleton())
  1139. // AudioServer::get_singleton()->update();
  1140. fixed_process_max=MAX(OS::get_singleton()->get_ticks_usec()-fixed_begin,fixed_process_max);
  1141. iters++;
  1142. }
  1143. uint64_t idle_begin = OS::get_singleton()->get_ticks_usec();
  1144. OS::get_singleton()->get_main_loop()->idle( step*time_scale );
  1145. message_queue->flush();
  1146. if (SpatialSoundServer::get_singleton())
  1147. SpatialSoundServer::get_singleton()->update( step*time_scale );
  1148. if (SpatialSound2DServer::get_singleton())
  1149. SpatialSound2DServer::get_singleton()->update( step*time_scale );
  1150. VisualServer::get_singleton()->sync(); //sync if still drawing from previous frames.
  1151. if (OS::get_singleton()->can_draw()) {
  1152. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  1153. if (VisualServer::get_singleton()->has_changed()) {
  1154. VisualServer::get_singleton()->draw(); // flush visual commands
  1155. OS::get_singleton()->frames_drawn++;
  1156. }
  1157. } else {
  1158. VisualServer::get_singleton()->draw(); // flush visual commands
  1159. OS::get_singleton()->frames_drawn++;
  1160. force_redraw_requested = false;
  1161. }
  1162. }
  1163. if (AudioServer::get_singleton())
  1164. AudioServer::get_singleton()->update();
  1165. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1166. ScriptServer::get_language(i)->frame();
  1167. }
  1168. idle_process_max=MAX(OS::get_singleton()->get_ticks_usec()-idle_begin,idle_process_max);
  1169. if (script_debugger)
  1170. script_debugger->idle_poll();
  1171. // x11_delay_usec(10000);
  1172. frames++;
  1173. if (frame>1000000) {
  1174. if (GLOBAL_DEF("debug/print_fps", OS::get_singleton()->is_stdout_verbose())) {
  1175. print_line("FPS: "+itos(frames));
  1176. };
  1177. OS::get_singleton()->_fps=frames;
  1178. performance->set_process_time(idle_process_max/1000000.0);
  1179. performance->set_fixed_process_time(fixed_process_max/1000000.0);
  1180. idle_process_max=0;
  1181. fixed_process_max=0;
  1182. if (GLOBAL_DEF("debug/print_metrics", false)) {
  1183. //PerformanceMetrics::print();
  1184. };
  1185. frame%=1000000;
  1186. frames=0;
  1187. }
  1188. if (OS::get_singleton()->is_in_low_processor_usage_mode() || !OS::get_singleton()->can_draw())
  1189. OS::get_singleton()->delay_usec(25000); //apply some delay to force idle time
  1190. else {
  1191. uint32_t frame_delay = OS::get_singleton()->get_frame_delay();
  1192. if (frame_delay)
  1193. OS::get_singleton()->delay_usec( OS::get_singleton()->get_frame_delay()*1000 );
  1194. }
  1195. int target_fps = OS::get_singleton()->get_target_fps();
  1196. if (target_fps>0) {
  1197. uint64_t time_step = 1000000L/target_fps;
  1198. target_ticks += time_step;
  1199. uint64_t current_ticks = OS::get_singleton()->get_ticks_usec();
  1200. if (current_ticks<target_ticks) OS::get_singleton()->delay_usec(target_ticks-current_ticks);
  1201. current_ticks = OS::get_singleton()->get_ticks_usec();
  1202. target_ticks = MIN(MAX(target_ticks,current_ticks-time_step),current_ticks+time_step);
  1203. }
  1204. return exit;
  1205. }
  1206. void Main::force_redraw() {
  1207. force_redraw_requested = true;
  1208. };
  1209. void Main::cleanup() {
  1210. ERR_FAIL_COND(!_start_success);
  1211. if (script_debugger)
  1212. memdelete(script_debugger);
  1213. OS::get_singleton()->delete_main_loop();
  1214. OS::get_singleton()->_cmdline.clear();
  1215. OS::get_singleton()->_execpath="";
  1216. OS::get_singleton()->_local_clipboard="";
  1217. #ifdef TOOLS_ENABLED
  1218. EditorNode::unregister_editor_types();
  1219. #endif
  1220. unregister_driver_types();
  1221. unregister_module_types();
  1222. unregister_scene_types();
  1223. unregister_server_types();
  1224. OS::get_singleton()->finalize();
  1225. if (packed_data)
  1226. memdelete(packed_data);
  1227. if (file_access_network_client)
  1228. memdelete(file_access_network_client);
  1229. if (performance)
  1230. memdelete(performance);
  1231. if (input_map)
  1232. memdelete(input_map);
  1233. if (translation_server)
  1234. memdelete( translation_server );
  1235. if (path_remap)
  1236. memdelete(path_remap);
  1237. if (globals)
  1238. memdelete(globals);
  1239. memdelete( message_queue );
  1240. unregister_core_driver_types();
  1241. unregister_core_types();
  1242. //PerformanceMetrics::finish();
  1243. OS::get_singleton()->clear_last_error();
  1244. OS::get_singleton()->finalize_core();
  1245. }