main.cpp 42 KB

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