main.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /*************************************************************************/
  2. /* main.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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-2015 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. if (rtm==-1) {
  521. rtm=GLOBAL_DEF("render/thread_model",OS::RENDER_THREAD_SAFE);
  522. if (rtm>=1) //hack for now
  523. rtm=1;
  524. }
  525. if (rtm>=0 && rtm<3)
  526. OS::get_singleton()->_render_thread_mode=OS::RenderThreadMode(rtm);
  527. /* Determine Video Driver */
  528. if (audio_driver=="") // specified in engine.cfg
  529. audio_driver=GLOBAL_DEF("audio/driver",OS::get_singleton()->get_audio_driver_name(0));
  530. for (int i=0;i<OS::get_singleton()->get_video_driver_count();i++) {
  531. if (video_driver==OS::get_singleton()->get_video_driver_name(i)) {
  532. video_driver_idx=i;
  533. break;
  534. }
  535. }
  536. if (video_driver_idx<0) {
  537. OS::get_singleton()->alert( "Invalid Video Driver: "+video_driver );
  538. video_driver_idx = 0;
  539. //goto error;
  540. }
  541. for (int i=0;i<OS::get_singleton()->get_audio_driver_count();i++) {
  542. if (audio_driver==OS::get_singleton()->get_audio_driver_name(i)) {
  543. audio_driver_idx=i;
  544. break;
  545. }
  546. }
  547. if (audio_driver_idx<0) {
  548. OS::get_singleton()->alert( "Invalid Audio Driver: "+audio_driver );
  549. goto error;
  550. }
  551. {
  552. String orientation = GLOBAL_DEF("display/orientation","landscape");
  553. if (orientation=="portrait")
  554. OS::get_singleton()->set_screen_orientation(OS::SCREEN_PORTRAIT);
  555. else if (orientation=="reverse_landscape")
  556. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_LANDSCAPE);
  557. else if (orientation=="reverse_portrait")
  558. OS::get_singleton()->set_screen_orientation(OS::SCREEN_REVERSE_PORTRAIT);
  559. else if (orientation=="sensor_landscape")
  560. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_LANDSCAPE);
  561. else if (orientation=="sensor_portrait")
  562. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR_PORTRAIT);
  563. else if (orientation=="sensor")
  564. OS::get_singleton()->set_screen_orientation(OS::SCREEN_SENSOR);
  565. else
  566. OS::get_singleton()->set_screen_orientation(OS::SCREEN_LANDSCAPE);
  567. }
  568. OS::get_singleton()->set_iterations_per_second(GLOBAL_DEF("physics/fixed_fps",60));
  569. OS::get_singleton()->set_target_fps(GLOBAL_DEF("application/target_fps",0));
  570. if (!OS::get_singleton()->_verbose_stdout) //overrided
  571. OS::get_singleton()->_verbose_stdout=GLOBAL_DEF("debug/verbose_stdout",false);
  572. message_queue = memnew( MessageQueue );
  573. Globals::get_singleton()->register_global_defaults();
  574. if (p_second_phase)
  575. return setup2();
  576. return OK;
  577. error:
  578. video_driver="";
  579. audio_driver="";
  580. game_path="";
  581. args.clear();
  582. main_args.clear();
  583. print_help(execpath);
  584. if (performance)
  585. memdelete(performance);
  586. if (input_map)
  587. memdelete(input_map);
  588. if (translation_server)
  589. memdelete( translation_server );
  590. if (globals)
  591. memdelete(globals);
  592. if (script_debugger)
  593. memdelete(script_debugger);
  594. if (packed_data)
  595. memdelete(packed_data);
  596. if (file_access_network_client)
  597. memdelete(file_access_network_client);
  598. if(path_remap)
  599. memdelete(path_remap);
  600. // Note 1: *zip_packed_data live into *packed_data
  601. // Note 2: PackedData::~PackedData destroy this.
  602. //#ifdef MINIZIP_ENABLED
  603. // if (zip_packed_data)
  604. // memdelete( zip_packed_data );
  605. //#endif
  606. unregister_core_driver_types();
  607. unregister_core_types();
  608. OS::get_singleton()->_cmdline.clear();
  609. if (message_queue)
  610. memdelete( message_queue);
  611. OS::get_singleton()->finalize_core();
  612. locale=String();
  613. return ERR_INVALID_PARAMETER;
  614. }
  615. Error Main::setup2() {
  616. OS::get_singleton()->initialize(video_mode,video_driver_idx,audio_driver_idx);
  617. if (init_use_custom_pos) {
  618. OS::get_singleton()->set_window_position(init_custom_pos);
  619. }
  620. register_core_singletons();
  621. MAIN_PRINT("Main: Setup Logo");
  622. bool show_logo=true;
  623. #ifdef JAVASCRIPT_ENABLED
  624. show_logo=false;
  625. #endif
  626. if (init_screen!=-1) {
  627. OS::get_singleton()->set_current_screen(init_screen);
  628. }
  629. if (init_windowed) {
  630. //do none..
  631. } else if (init_maximized) {
  632. OS::get_singleton()->set_window_maximized(true);
  633. } else if (init_fullscreen) {
  634. OS::get_singleton()->set_window_fullscreen(true);
  635. }
  636. MAIN_PRINT("Main: Load Remaps");
  637. path_remap->load_remaps();
  638. if (show_logo) { //boot logo!
  639. String boot_logo_path=GLOBAL_DEF("application/boot_splash",String());
  640. bool boot_logo_scale=GLOBAL_DEF("application/boot_splash_fullsize",true);
  641. Globals::get_singleton()->set_custom_property_info("application/boot_splash",PropertyInfo(Variant::STRING,"application/boot_splash",PROPERTY_HINT_FILE,"*.png"));
  642. Image boot_logo;
  643. boot_logo_path = boot_logo_path.strip_edges();
  644. if (boot_logo_path!=String() /*&& FileAccess::exists(boot_logo_path)*/) {
  645. print_line("Boot splash path: "+boot_logo_path);
  646. Error err = boot_logo.load(boot_logo_path);
  647. }
  648. if (!boot_logo.empty()) {
  649. OS::get_singleton()->_msec_splash=OS::get_singleton()->get_ticks_msec();
  650. Color clear = GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3));
  651. VisualServer::get_singleton()->set_default_clear_color(clear);
  652. Color boot_bg = GLOBAL_DEF("application/boot_bg_color", clear);
  653. VisualServer::get_singleton()->set_boot_image(boot_logo, boot_bg,boot_logo_scale);
  654. #ifndef TOOLS_ENABLED
  655. //no tools, so free the boot logo (no longer needed)
  656. // Globals::get_singleton()->set("application/boot_logo",Image());
  657. #endif
  658. } else {
  659. #ifndef NO_DEFAULT_BOOT_LOGO
  660. MAIN_PRINT("Main: Create bootsplash");
  661. Image splash(boot_splash_png);
  662. MAIN_PRINT("Main: ClearColor");
  663. VisualServer::get_singleton()->set_default_clear_color(boot_splash_bg_color);
  664. MAIN_PRINT("Main: Image");
  665. VisualServer::get_singleton()->set_boot_image(splash, boot_splash_bg_color,false);
  666. #endif
  667. MAIN_PRINT("Main: DCC");
  668. VisualServer::get_singleton()->set_default_clear_color(GLOBAL_DEF("render/default_clear_color",Color(0.3,0.3,0.3)));
  669. MAIN_PRINT("Main: END");
  670. }
  671. Image icon(app_icon_png);
  672. OS::get_singleton()->set_icon(icon);
  673. }
  674. GLOBAL_DEF("application/icon",String());
  675. Globals::get_singleton()->set_custom_property_info("application/icon",PropertyInfo(Variant::STRING,"application/icon",PROPERTY_HINT_FILE,"*.png,*.webp"));
  676. if (bool(GLOBAL_DEF("display/emulate_touchscreen",false))) {
  677. if (!OS::get_singleton()->has_touchscreen_ui_hint() && Input::get_singleton()) {
  678. //only if no touchscreen ui hint, set emulation
  679. InputDefault *id = Input::get_singleton()->cast_to<InputDefault>();
  680. if (id)
  681. id->set_emulate_touch(true);
  682. }
  683. }
  684. MAIN_PRINT("Main: Load Remaps");
  685. MAIN_PRINT("Main: Load Scene Types");
  686. register_scene_types();
  687. register_server_types();
  688. GLOBAL_DEF("display/custom_mouse_cursor",String());
  689. GLOBAL_DEF("display/custom_mouse_cursor_hotspot",Vector2());
  690. Globals::get_singleton()->set_custom_property_info("display/custom_mouse_cursor",PropertyInfo(Variant::STRING,"display/custom_mouse_cursor",PROPERTY_HINT_FILE,"*.png,*.webp"));
  691. if (String(Globals::get_singleton()->get("display/custom_mouse_cursor"))!=String()) {
  692. //print_line("use custom cursor");
  693. Ref<Texture> cursor=ResourceLoader::load(Globals::get_singleton()->get("display/custom_mouse_cursor"));
  694. if (cursor.is_valid()) {
  695. // print_line("loaded ok");
  696. Vector2 hotspot = Globals::get_singleton()->get("display/custom_mouse_cursor_hotspot");
  697. Input::get_singleton()->set_custom_mouse_cursor(cursor,hotspot);
  698. }
  699. }
  700. #ifdef TOOLS_ENABLED
  701. EditorNode::register_editor_types();
  702. ObjectTypeDB::register_type<PCKPacker>(); // todo: move somewhere else
  703. #endif
  704. MAIN_PRINT("Main: Load Scripts, Modules, Drivers");
  705. register_module_types();
  706. register_driver_types();
  707. ScriptServer::init_languages();
  708. MAIN_PRINT("Main: Load Translations");
  709. translation_server->setup(); //register translations, load them, etc.
  710. if (locale!="") {
  711. translation_server->set_locale(locale);
  712. }
  713. translation_server->load_translations();
  714. _start_success=true;
  715. locale=String();
  716. MAIN_PRINT("Main: Done");
  717. return OK;
  718. }
  719. bool Main::start() {
  720. ERR_FAIL_COND_V(!_start_success,false);
  721. bool editor=false;
  722. String doc_tool;
  723. bool doc_base=true;
  724. String game_path;
  725. String script;
  726. String test;
  727. String screen;
  728. String optimize;
  729. String optimize_preset;
  730. String _export_platform;
  731. String _import;
  732. String _import_script;
  733. String dumpstrings;
  734. bool noquit=false;
  735. bool convert_old=false;
  736. bool export_debug=false;
  737. List<String> args = OS::get_singleton()->get_cmdline_args();
  738. for (int i=0;i<args.size();i++) {
  739. //parameters that do not have an argument to the right
  740. if (args[i]=="-nodocbase") {
  741. doc_base=false;
  742. } else if (args[i]=="-noquit") {
  743. noquit=true;
  744. } else if (args[i]=="-convert_old") {
  745. convert_old=true;
  746. } else if (args[i]=="-editor" || args[i]=="-e") {
  747. editor=true;
  748. } else if (args[i].length() && args[i][0] != '-' && game_path == "") {
  749. game_path=args[i];
  750. }
  751. //parameters that have an argument to the right
  752. else if (i < (args.size()-1)) {
  753. bool parsed_pair=true;
  754. if (args[i]=="-doctool") {
  755. doc_tool=args[i+1];
  756. } else if (args[i]=="-script" || args[i]=="-s") {
  757. script=args[i+1];
  758. } else if (args[i]=="-level" || args[i]=="-l") {
  759. OS::get_singleton()->_custom_level=args[i+1];
  760. } else if (args[i]=="-test") {
  761. test=args[i+1];
  762. } else if (args[i]=="-optimize") {
  763. optimize=args[i+1];
  764. } else if (args[i]=="-optimize_preset") {
  765. optimize_preset=args[i+1];
  766. } else if (args[i]=="-export") {
  767. editor=true; //needs editor
  768. _export_platform=args[i+1];
  769. } else if (args[i]=="-export_debug") {
  770. editor=true; //needs editor
  771. _export_platform=args[i+1];
  772. export_debug=true;
  773. } else if (args[i]=="-import") {
  774. editor=true; //needs editor
  775. _import=args[i+1];
  776. } else if (args[i]=="-import_script") {
  777. editor=true; //needs editor
  778. _import_script=args[i+1];
  779. } else if (args[i]=="-dumpstrings") {
  780. editor=true; //needs editor
  781. dumpstrings=args[i+1];
  782. } else {
  783. // The parameter does not match anything known, don't skip the next argument
  784. parsed_pair=false;
  785. }
  786. if (parsed_pair) {
  787. i++;
  788. }
  789. }
  790. }
  791. if (editor)
  792. Globals::get_singleton()->set("editor_active",true);
  793. String main_loop_type;
  794. #ifdef TOOLS_ENABLED
  795. if(doc_tool!="") {
  796. DocData doc;
  797. doc.generate(doc_base);
  798. DocData docsrc;
  799. if (docsrc.load(doc_tool)==OK) {
  800. print_line("Doc exists. Merging..");
  801. doc.merge_from(docsrc);
  802. } else {
  803. print_line("No Doc exists. Generating empty.");
  804. }
  805. doc.save(doc_tool);
  806. return false;
  807. }
  808. if (optimize!="")
  809. editor=true; //need editor
  810. #endif
  811. if (_export_platform!="") {
  812. if (game_path=="") {
  813. String err="Command line param ";
  814. err+=export_debug?"-export_debug":"-export";
  815. err+=" passed but no destination path given.\n";
  816. err+="Please specify the binary's file path to export to. Aborting export.";
  817. ERR_PRINT(err.utf8().get_data());
  818. return false;
  819. }
  820. }
  821. if(script=="" && game_path=="" && String(GLOBAL_DEF("application/main_scene",""))!="") {
  822. game_path=GLOBAL_DEF("application/main_scene","");
  823. }
  824. MainLoop *main_loop=NULL;
  825. if (editor) {
  826. main_loop = memnew(SceneTree);
  827. };
  828. if (test!="") {
  829. #ifdef DEBUG_ENABLED
  830. main_loop = test_main(test,args);
  831. if (!main_loop)
  832. return false;
  833. #endif
  834. } else if (script!="") {
  835. Ref<Script> script_res = ResourceLoader::load(script);
  836. ERR_EXPLAIN("Can't load script: "+script);
  837. ERR_FAIL_COND_V(script_res.is_null(),false);
  838. if( script_res->can_instance() /*&& script_res->inherits_from("SceneTreeScripted")*/) {
  839. StringName instance_type=script_res->get_instance_base_type();
  840. Object *obj = ObjectTypeDB::instance(instance_type);
  841. MainLoop *script_loop = obj?obj->cast_to<MainLoop>():NULL;
  842. if (!script_loop) {
  843. if (obj)
  844. memdelete(obj);
  845. ERR_EXPLAIN("Can't load script '"+script+"', it does not inherit from a MainLoop type");
  846. ERR_FAIL_COND_V(!script_loop,false);
  847. }
  848. script_loop->set_init_script(script_res);
  849. main_loop=script_loop;
  850. } else {
  851. return false;
  852. }
  853. } else {
  854. main_loop_type=GLOBAL_DEF("application/main_loop_type","");
  855. }
  856. if (!main_loop && main_loop_type=="")
  857. main_loop_type="SceneTree";
  858. if (!main_loop) {
  859. if (!ObjectTypeDB::type_exists(main_loop_type)) {
  860. OS::get_singleton()->alert("godot: error: MainLoop type doesn't exist: "+main_loop_type);
  861. return false;
  862. } else {
  863. Object *ml = ObjectTypeDB::instance(main_loop_type);
  864. if (!ml) {
  865. ERR_EXPLAIN("Can't instance MainLoop type");
  866. ERR_FAIL_V(false);
  867. }
  868. main_loop=ml->cast_to<MainLoop>();
  869. if (!main_loop) {
  870. memdelete(ml);
  871. ERR_EXPLAIN("Invalid MainLoop type");
  872. ERR_FAIL_V(false);
  873. }
  874. }
  875. }
  876. if (main_loop->is_type("SceneTree")) {
  877. SceneTree *sml = main_loop->cast_to<SceneTree>();
  878. if (debug_collisions) {
  879. sml->set_debug_collisions_hint(true);
  880. }
  881. if (debug_navigation) {
  882. sml->set_debug_navigation_hint(true);
  883. }
  884. #ifdef TOOLS_ENABLED
  885. EditorNode *editor_node=NULL;
  886. if (editor) {
  887. editor_node = memnew( EditorNode );
  888. sml->get_root()->add_child(editor_node);
  889. //root_node->set_editor(editor);
  890. //startup editor
  891. if (_export_platform!="") {
  892. editor_node->export_platform(_export_platform,game_path,export_debug,"",true);
  893. game_path=""; //no load anything
  894. }
  895. }
  896. #endif
  897. if (!editor) {
  898. //standard helpers that can be changed from main config
  899. String stretch_mode = GLOBAL_DEF("display/stretch_mode","disabled");
  900. String stretch_aspect = GLOBAL_DEF("display/stretch_aspect","ignore");
  901. Size2i stretch_size = Size2(GLOBAL_DEF("display/width",0),GLOBAL_DEF("display/height",0));
  902. SceneTree::StretchMode sml_sm=SceneTree::STRETCH_MODE_DISABLED;
  903. if (stretch_mode=="2d")
  904. sml_sm=SceneTree::STRETCH_MODE_2D;
  905. else if (stretch_mode=="viewport")
  906. sml_sm=SceneTree::STRETCH_MODE_VIEWPORT;
  907. SceneTree::StretchAspect sml_aspect=SceneTree::STRETCH_ASPECT_IGNORE;
  908. if (stretch_aspect=="keep")
  909. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP;
  910. else if (stretch_aspect=="keep_width")
  911. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_WIDTH;
  912. else if (stretch_aspect=="keep_height")
  913. sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_HEIGHT;
  914. sml->set_screen_stretch(sml_sm,sml_aspect,stretch_size);
  915. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  916. String appname = Globals::get_singleton()->get("application/name");
  917. appname = TranslationServer::get_singleton()->translate(appname);
  918. OS::get_singleton()->set_window_title(appname);
  919. } else {
  920. GLOBAL_DEF("display/stretch_mode","disabled");
  921. Globals::get_singleton()->set_custom_property_info("display/stretch_mode",PropertyInfo(Variant::STRING,"display/stretch_mode",PROPERTY_HINT_ENUM,"disabled,2d,viewport"));
  922. GLOBAL_DEF("display/stretch_aspect","ignore");
  923. 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"));
  924. sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true));
  925. }
  926. if (game_path!="") {
  927. String local_game_path=game_path.replace("\\","/");
  928. if (!local_game_path.begins_with("res://")) {
  929. bool absolute=(local_game_path.size()>1) && (local_game_path[0]=='/' || local_game_path[1]==':');
  930. if (!absolute) {
  931. if (Globals::get_singleton()->is_using_datapack()) {
  932. local_game_path="res://"+local_game_path;
  933. } else {
  934. int sep=local_game_path.find_last("/");
  935. if (sep==-1) {
  936. DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  937. local_game_path=da->get_current_dir()+"/"+local_game_path;
  938. memdelete(da) ;
  939. } else {
  940. DirAccess *da = DirAccess::open(local_game_path.substr(0,sep));
  941. if (da) {
  942. local_game_path=da->get_current_dir()+"/"+local_game_path.substr(sep+1,local_game_path.length());;
  943. memdelete(da);
  944. }
  945. }
  946. }
  947. }
  948. }
  949. local_game_path=Globals::get_singleton()->localize_path(local_game_path);
  950. #ifdef TOOLS_ENABLED
  951. if (editor) {
  952. if (_import!="") {
  953. //editor_node->import_scene(_import,local_game_path,_import_script);
  954. if (!noquit)
  955. sml->quit();
  956. game_path=""; //no load anything
  957. } else {
  958. Error serr = editor_node->load_scene(local_game_path);
  959. if (serr==OK) {
  960. if (optimize!="") {
  961. editor_node->save_optimized_copy(optimize,optimize_preset);
  962. if (!noquit)
  963. sml->quit();
  964. }
  965. if (dumpstrings!="") {
  966. editor_node->save_translatable_strings(dumpstrings);
  967. if (!noquit)
  968. sml->quit();
  969. }
  970. }
  971. }
  972. //editor_node->set_edited_scene(game);
  973. } else {
  974. #endif
  975. {
  976. //autoload
  977. List<PropertyInfo> props;
  978. Globals::get_singleton()->get_property_list(&props);
  979. for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) {
  980. String s = E->get().name;
  981. if (!s.begins_with("autoload/"))
  982. continue;
  983. String name = s.get_slicec('/',1);
  984. String path = Globals::get_singleton()->get(s);
  985. bool global_var=false;
  986. if (path.begins_with("*")) {
  987. global_var=true;
  988. path=path.substr(1,path.length()-1);
  989. }
  990. RES res = ResourceLoader::load(path);
  991. ERR_EXPLAIN("Can't autoload: "+path);
  992. ERR_CONTINUE(res.is_null());
  993. Node *n=NULL;
  994. if (res->is_type("PackedScene")) {
  995. Ref<PackedScene> ps = res;
  996. n=ps->instance();
  997. } else if (res->is_type("Script")) {
  998. Ref<Script> s = res;
  999. StringName ibt = s->get_instance_base_type();
  1000. ERR_EXPLAIN("Script does not inherit a Node: "+path);
  1001. ERR_CONTINUE( !ObjectTypeDB::is_type(ibt,"Node") );
  1002. Object *obj = ObjectTypeDB::instance(ibt);
  1003. ERR_EXPLAIN("Cannot instance node for autoload type: "+String(ibt));
  1004. ERR_CONTINUE( obj==NULL );
  1005. n = obj->cast_to<Node>();
  1006. n->set_script(s.get_ref_ptr());
  1007. }
  1008. ERR_EXPLAIN("Path in autoload not a node or script: "+path);
  1009. ERR_CONTINUE(!n);
  1010. n->set_name(name);
  1011. sml->get_root()->add_child(n);
  1012. if (global_var) {
  1013. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1014. ScriptServer::get_language(i)->add_global_constant(name,n);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. Node *scene=NULL;
  1020. Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path);
  1021. if (scenedata.is_valid())
  1022. scene=scenedata->instance();
  1023. ERR_EXPLAIN("Failed loading scene: "+local_game_path);
  1024. ERR_FAIL_COND_V(!scene,false)
  1025. //sml->get_root()->add_child(scene);
  1026. sml->add_current_scene(scene);
  1027. String iconpath = GLOBAL_DEF("application/icon","Variant()""");
  1028. if (iconpath!="") {
  1029. Image icon;
  1030. if (icon.load(iconpath)==OK)
  1031. OS::get_singleton()->set_icon(icon);
  1032. }
  1033. //singletons
  1034. #ifdef TOOLS_ENABLED
  1035. }
  1036. #endif
  1037. }
  1038. #ifdef TOOLS_ENABLED
  1039. /*if (_export_platform!="") {
  1040. sml->quit();
  1041. }*/
  1042. /*
  1043. if (sml->get_root_node()) {
  1044. Console *console = memnew( Console );
  1045. sml->get_root_node()->cast_to<RootNode>()->set_console(console);
  1046. if (GLOBAL_DEF("console/visible_default",false).operator bool()) {
  1047. console->show();
  1048. } else {P
  1049. console->hide();
  1050. };
  1051. }
  1052. */
  1053. if (script=="" && test=="" && game_path=="" && !editor) {
  1054. ProjectManager *pmanager = memnew( ProjectManager );
  1055. sml->get_root()->add_child(pmanager);
  1056. }
  1057. #endif
  1058. }
  1059. OS::get_singleton()->set_main_loop( main_loop );
  1060. return true;
  1061. }
  1062. uint64_t Main::last_ticks=0;
  1063. uint64_t Main::target_ticks=0;
  1064. float Main::time_accum=0;
  1065. uint32_t Main::frames=0;
  1066. uint32_t Main::frame=0;
  1067. bool Main::force_redraw_requested = false;
  1068. static uint64_t fixed_process_max=0;
  1069. static uint64_t idle_process_max=0;
  1070. bool Main::iteration() {
  1071. uint64_t ticks=OS::get_singleton()->get_ticks_usec();
  1072. uint64_t ticks_elapsed=ticks-last_ticks;
  1073. double step=(double)ticks_elapsed / 1000000.0;
  1074. float frame_slice=1.0/OS::get_singleton()->get_iterations_per_second();
  1075. // if (time_accum+step < frame_slice)
  1076. // return false;
  1077. frame+=ticks_elapsed;
  1078. last_ticks=ticks;
  1079. if (step>frame_slice*8)
  1080. step=frame_slice*8;
  1081. time_accum+=step;
  1082. float time_scale = OS::get_singleton()->get_time_scale();
  1083. bool exit=false;
  1084. int iters = 0;
  1085. while(time_accum>frame_slice) {
  1086. uint64_t fixed_begin = OS::get_singleton()->get_ticks_usec();
  1087. PhysicsServer::get_singleton()->sync();
  1088. PhysicsServer::get_singleton()->flush_queries();
  1089. Physics2DServer::get_singleton()->sync();
  1090. Physics2DServer::get_singleton()->flush_queries();
  1091. if (OS::get_singleton()->get_main_loop()->iteration( frame_slice*time_scale )) {
  1092. exit=true;
  1093. break;
  1094. }
  1095. message_queue->flush();
  1096. PhysicsServer::get_singleton()->step(frame_slice*time_scale);
  1097. Physics2DServer::get_singleton()->end_sync();
  1098. Physics2DServer::get_singleton()->step(frame_slice*time_scale);
  1099. time_accum-=frame_slice;
  1100. message_queue->flush();
  1101. //if (AudioServer::get_singleton())
  1102. // AudioServer::get_singleton()->update();
  1103. fixed_process_max=MAX(OS::get_singleton()->get_ticks_usec()-fixed_begin,fixed_process_max);
  1104. iters++;
  1105. }
  1106. uint64_t idle_begin = OS::get_singleton()->get_ticks_usec();
  1107. OS::get_singleton()->get_main_loop()->idle( step*time_scale );
  1108. message_queue->flush();
  1109. if (SpatialSoundServer::get_singleton())
  1110. SpatialSoundServer::get_singleton()->update( step*time_scale );
  1111. if (SpatialSound2DServer::get_singleton())
  1112. SpatialSound2DServer::get_singleton()->update( step*time_scale );
  1113. VisualServer::get_singleton()->sync(); //sync if still drawing from previous frames.
  1114. if (OS::get_singleton()->can_draw()) {
  1115. if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
  1116. if (VisualServer::get_singleton()->has_changed()) {
  1117. VisualServer::get_singleton()->draw(); // flush visual commands
  1118. OS::get_singleton()->frames_drawn++;
  1119. }
  1120. } else {
  1121. VisualServer::get_singleton()->draw(); // flush visual commands
  1122. OS::get_singleton()->frames_drawn++;
  1123. force_redraw_requested = false;
  1124. }
  1125. }
  1126. if (AudioServer::get_singleton())
  1127. AudioServer::get_singleton()->update();
  1128. for(int i=0;i<ScriptServer::get_language_count();i++) {
  1129. ScriptServer::get_language(i)->frame();
  1130. }
  1131. idle_process_max=MAX(OS::get_singleton()->get_ticks_usec()-idle_begin,idle_process_max);
  1132. if (script_debugger)
  1133. script_debugger->idle_poll();
  1134. // x11_delay_usec(10000);
  1135. frames++;
  1136. if (frame>1000000) {
  1137. if (GLOBAL_DEF("debug/print_fps", OS::get_singleton()->is_stdout_verbose())) {
  1138. print_line("FPS: "+itos(frames));
  1139. };
  1140. OS::get_singleton()->_fps=frames;
  1141. performance->set_process_time(idle_process_max/1000000.0);
  1142. performance->set_fixed_process_time(fixed_process_max/1000000.0);
  1143. idle_process_max=0;
  1144. fixed_process_max=0;
  1145. if (GLOBAL_DEF("debug/print_metrics", false)) {
  1146. //PerformanceMetrics::print();
  1147. };
  1148. frame%=1000000;
  1149. frames=0;
  1150. }
  1151. if (OS::get_singleton()->is_in_low_processor_usage_mode() || !OS::get_singleton()->can_draw())
  1152. OS::get_singleton()->delay_usec(25000); //apply some delay to force idle time
  1153. else {
  1154. uint32_t frame_delay = OS::get_singleton()->get_frame_delay();
  1155. if (frame_delay)
  1156. OS::get_singleton()->delay_usec( OS::get_singleton()->get_frame_delay()*1000 );
  1157. }
  1158. int target_fps = OS::get_singleton()->get_target_fps();
  1159. if (target_fps>0) {
  1160. uint64_t time_step = 1000000L/target_fps;
  1161. target_ticks += time_step;
  1162. uint64_t current_ticks = OS::get_singleton()->get_ticks_usec();
  1163. if (current_ticks<target_ticks) OS::get_singleton()->delay_usec(target_ticks-current_ticks);
  1164. current_ticks = OS::get_singleton()->get_ticks_usec();
  1165. target_ticks = MIN(MAX(target_ticks,current_ticks-time_step),current_ticks+time_step);
  1166. }
  1167. return exit;
  1168. }
  1169. void Main::force_redraw() {
  1170. force_redraw_requested = true;
  1171. };
  1172. void Main::cleanup() {
  1173. ERR_FAIL_COND(!_start_success);
  1174. if (script_debugger)
  1175. memdelete(script_debugger);
  1176. OS::get_singleton()->delete_main_loop();
  1177. OS::get_singleton()->_cmdline.clear();
  1178. OS::get_singleton()->_execpath="";
  1179. OS::get_singleton()->_local_clipboard="";
  1180. #ifdef TOOLS_ENABLED
  1181. EditorNode::unregister_editor_types();
  1182. #endif
  1183. unregister_driver_types();
  1184. unregister_module_types();
  1185. unregister_scene_types();
  1186. unregister_server_types();
  1187. OS::get_singleton()->finalize();
  1188. if (packed_data)
  1189. memdelete(packed_data);
  1190. if (file_access_network_client)
  1191. memdelete(file_access_network_client);
  1192. if (performance)
  1193. memdelete(performance);
  1194. if (input_map)
  1195. memdelete(input_map);
  1196. if (translation_server)
  1197. memdelete( translation_server );
  1198. if (path_remap)
  1199. memdelete(path_remap);
  1200. if (globals)
  1201. memdelete(globals);
  1202. memdelete( message_queue );
  1203. unregister_core_driver_types();
  1204. unregister_core_types();
  1205. //PerformanceMetrics::finish();
  1206. OS::get_singleton()->clear_last_error();
  1207. OS::get_singleton()->finalize_core();
  1208. }