From a7d49da21770d62ac85feeaaaf54e55232cb07aa Mon Sep 17 00:00:00 2001 From: BlockMen Date: Sun, 1 Jun 2014 00:55:23 +0200 Subject: [PATCH] Codestyle cleanup (main.cpp) --- src/main.cpp | 616 ++++++++++++++++++++++++--------------------------- 1 file changed, 284 insertions(+), 332 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 0531a8540..52000dd1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -176,7 +176,7 @@ public: u32 getTime(TimePrecision prec) { if (prec == PRECISION_MILLI) { - if(m_device == NULL) + if (m_device == NULL) return 0; return m_device->getTimer()->getRealTime(); } else { @@ -202,7 +202,7 @@ TimeGetter *g_timegetter = NULL; u32 getTimeMs() { - if(g_timegetter == NULL) + if (g_timegetter == NULL) return 0; return g_timegetter->getTime(PRECISION_MILLI); } @@ -220,7 +220,7 @@ public: /* line: Full line with timestamp, level and thread */ void printLog(const std::string &line) { - std::cerr< map1; tempf = -324; - const s16 ii=300; - for(s16 y=0; y=0; y--){ - for(s16 x=0; x= 0; y--) { + for(s16 x = 0; x < ii; x++) { + tempf = map1[v2s16(x, y)]; } } } { - infostream<<"Around 5000/ms should do well here."< &worldspecs, - std::ostream &os) +static void print_worldspecs(const std::vector &worldspecs, std::ostream &os) { - for(u32 i=0; i::iterator i = allowed_options.begin(); - i != allowed_options.end(); ++i) - { + i != allowed_options.end(); ++i) { std::ostringstream os1(std::ios::binary); - os1<<" --"<first; - if(i->second.type == VALUETYPE_FLAG) - {} - else - os1<<_(" "); - dstream<first; + if (i->second.type == VALUETYPE_FLAG) { + } else + os1 << _(" "); + dstream << padStringRight(os1.str(), 24); - if(i->second.help != NULL) - dstream<second.help; - dstream<second.help != NULL) + dstream << i->second.help; + dstream << std::endl; } return cmd_args.getFlag("help") ? 0 : 1; } - if(cmd_args.getFlag("version")) - { + if (cmd_args.getFlag("version")) { #ifdef SERVER - dstream<<"minetestserver "< gameids = getAvailableGameIds(); for(std::set::const_iterator i = gameids.begin(); i != gameids.end(); i++) @@ -905,18 +878,16 @@ int main(int argc, char *argv[]) } // List worlds if requested - if(cmd_args.exists("world") && cmd_args.get("world") == "list"){ - dstream<<_("Available worlds:")< worldspecs = getAvailableWorlds(); print_worldspecs(worldspecs, dstream); return 0; } // Print startup message - infostream<readConfigFile(cmd_args.get("config").c_str()); - if(r == false) - { - errorstream<<"Could not read configuration from \"" - < filenames; filenames.push_back(porting::path_user + DIR_DELIM + "minetest.conf"); @@ -962,18 +929,16 @@ int main(int argc, char *argv[]) DIR_DELIM + ".." + DIR_DELIM + ".." + DIR_DELIM + "minetest.conf"); #endif - for(u32 i=0; ireadConfigFile(filenames[i].c_str()); - if(r) - { + if (r) { g_settings_path = filenames[i]; break; } } // If no path found, use the first one (menu creates the file) - if(g_settings_path == "") + if (g_settings_path == "") g_settings_path = filenames[0]; } @@ -984,7 +949,7 @@ int main(int argc, char *argv[]) #else std::string logfile = porting::path_user+DIR_DELIM+DEBUGFILE; #endif - if(cmd_args.exists("logfile")) + if (cmd_args.exists("logfile")) logfile = cmd_args.get("logfile"); log_remove_output(&main_dstream_no_stderr_log_out); @@ -993,14 +958,15 @@ int main(int argc, char *argv[]) if (loglevel == 0) //no logging logfile = ""; else if (loglevel > 0 && loglevel <= LMT_NUM_VALUES) - log_add_output_maxlev(&main_dstream_no_stderr_log_out, (LogMessageLevel)(loglevel - 1)); + log_add_output_maxlev(&main_dstream_no_stderr_log_out, + (LogMessageLevel)(loglevel - 1)); - if(logfile != "") + if (logfile != "") debugstreams_init(false, logfile.c_str()); else debugstreams_init(false, NULL); - infostream<<"logfile = "<get("language"),argc,argv); + init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(), + g_settings->get("language"), argc, argv); #else - init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(),g_settings->get("language")); + init_gettext((porting::path_share + DIR_DELIM + "locale").c_str(), + g_settings->get("language")); #endif /* @@ -1030,61 +997,61 @@ int main(int argc, char *argv[]) // Port u16 port = 30000; - if(cmd_args.exists("port")) + if (cmd_args.exists("port")) port = cmd_args.getU16("port"); - else if(g_settings->exists("port")) + else if (g_settings->exists("port")) port = g_settings->getU16("port"); - if(port == 0) + if (port == 0) port = 30000; // World directory std::string commanded_world = ""; - if(cmd_args.exists("world")) + if (cmd_args.exists("world")) commanded_world = cmd_args.get("world"); - else if(cmd_args.exists("map-dir")) + else if (cmd_args.exists("map-dir")) commanded_world = cmd_args.get("map-dir"); - else if(cmd_args.exists("nonopt0")) // First nameless argument + else if (cmd_args.exists("nonopt0")) // First nameless argument commanded_world = cmd_args.get("nonopt0"); - else if(g_settings->exists("map-dir")) + else if (g_settings->exists("map-dir")) commanded_world = g_settings->get("map-dir"); // World name std::string commanded_worldname = ""; - if(cmd_args.exists("worldname")) + if (cmd_args.exists("worldname")) commanded_worldname = cmd_args.get("worldname"); // Strip world.mt from commanded_world { std::string worldmt = "world.mt"; - if(commanded_world.size() > worldmt.size() && - commanded_world.substr(commanded_world.size()-worldmt.size()) - == worldmt){ - dstream<<_("Supplied world.mt file - stripping it off.")< worldmt.size() && + commanded_world.substr(commanded_world.size() - worldmt.size()) + == worldmt) { + dstream << _("Supplied world.mt file - stripping it off.") << std::endl; + commanded_world = commanded_world.substr(0, + commanded_world.size() - worldmt.size()); } } // If a world name was specified, convert it to a path - if(commanded_worldname != ""){ + if (commanded_worldname != "") { // Get information about available worlds std::vector worldspecs = getAvailableWorlds(); bool found = false; - for(u32 i=0; iset("server_dedicated", run_dedicated_server ? "true" : "false"); - if(run_dedicated_server) + if (run_dedicated_server) { DSTACK("Dedicated server branch"); // Create time getter if built with Irrlicht @@ -1121,46 +1088,43 @@ int main(int argc, char *argv[]) // World directory std::string world_path; - verbosestream<<_("Determining world path")< worldspecs = getAvailableWorlds(); // If a world name was specified, select it - if(commanded_worldname != ""){ + if (commanded_worldname != "") { world_path = ""; - for(u32 i=0; i 1){ - dstream<<_("Multiple worlds are available.")<" - " or --world ")< 1) { + dstream << _("Multiple worlds are available.") << std::endl; + dstream << _("Please select one using --worldname " + " or --world ") << std::endl; print_worldspecs(worldspecs, dstream); return 1; // If there are no worlds, automatically create a new one @@ -1168,63 +1132,57 @@ int main(int argc, char *argv[]) // This is the ultimate default world path world_path = porting::path_user + DIR_DELIM + "worlds" + DIR_DELIM + "world"; - infostream<<"Creating default world at [" - <get("default_game")); - infostream<<"Using default gameid ["<get("bind_address"); - Address bind_addr(0,0,0,0, port); + Address bind_addr(0, 0, 0, 0, port); if (g_settings->getBool("ipv6_server")) { bind_addr.setAddress((IPv6AddressBytes*) NULL); @@ -1236,7 +1194,7 @@ int main(int argc, char *argv[]) << "\" failed: " << e.what() << " -- Listening on all addresses." << std::endl; } - if(bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) { + if (bind_addr.isIPv6() && !g_settings->getBool("enable_ipv6")) { errorstream << "Unable to listen on " << bind_addr.serializeString() << L" because IPv6 is disabled" << std::endl; @@ -1250,7 +1208,8 @@ int main(int argc, char *argv[]) if (cmd_args.exists("migrate")) { std::string migrate_to = cmd_args.get("migrate"); Settings world_mt; - bool success = world_mt.readConfigFile((world_path + DIR_DELIM + "world.mt").c_str()); + bool success = world_mt.readConfigFile((world_path + DIR_DELIM + + "world.mt").c_str()); if (!success) { errorstream << "Cannot read world.mt" << std::endl; return 1; @@ -1263,7 +1222,8 @@ int main(int argc, char *argv[]) std::string backend = world_mt.get("backend"); Database *new_db; if (backend == migrate_to) { - errorstream << "Cannot migrate: new backend is same as the old one" << std::endl; + errorstream << "Cannot migrate: new backend is same" + <<" as the old one" << std::endl; return 1; } if (migrate_to == "sqlite3") @@ -1277,7 +1237,8 @@ int main(int argc, char *argv[]) new_db = new Database_Redis(&(ServerMap&)server.getMap(), world_path); #endif else { - errorstream << "Migration to " << migrate_to << " is not supported" << std::endl; + errorstream << "Migration to " << migrate_to + << " is not supported" << std::endl; return 1; } @@ -1286,7 +1247,7 @@ int main(int argc, char *argv[]) old_map.listAllLoadableBlocks(blocks); int count = 0; new_db->beginSave(); - for (std::list::iterator i = blocks.begin(); i != blocks.end(); ++i) { + for (std::list::iterator i = blocks.begin(); i != blocks.end(); i++) { MapBlock *block = old_map.loadBlock(*i); new_db->saveBlock(block); MapSector *sector = old_map.getSectorNoGenerate(v2s16(i->X, i->Z)); @@ -1301,10 +1262,10 @@ int main(int argc, char *argv[]) actionstream << "Successfully migrated " << count << " blocks" << std::endl; world_mt.set("backend", migrate_to); - if(!world_mt.updateConfigFile((world_path + DIR_DELIM + "world.mt").c_str())) - errorstream<<"Failed to update world.mt!"<get("address"); - if(commanded_world != "") + if (commanded_world != "") address = ""; - else if(cmd_args.exists("address")) + else if (cmd_args.exists("address")) address = cmd_args.get("address"); std::string playername = g_settings->get("name"); - if(cmd_args.exists("name")) + if (cmd_args.exists("name")) playername = cmd_args.get("name"); bool skip_main_menu = cmd_args.getFlag("go"); @@ -1357,30 +1318,29 @@ int main(int argc, char *argv[]) std::string driverstring = g_settings->get("video_driver"); - if(driverstring == "null") + if (driverstring == "null") driverType = video::EDT_NULL; - else if(driverstring == "software") + else if (driverstring == "software") driverType = video::EDT_SOFTWARE; - else if(driverstring == "burningsvideo") + else if (driverstring == "burningsvideo") driverType = video::EDT_BURNINGSVIDEO; - else if(driverstring == "direct3d8") + else if (driverstring == "direct3d8") driverType = video::EDT_DIRECT3D8; - else if(driverstring == "direct3d9") + else if (driverstring == "direct3d9") driverType = video::EDT_DIRECT3D9; - else if(driverstring == "opengl") + else if (driverstring == "opengl") driverType = video::EDT_OPENGL; #ifdef _IRR_COMPILE_WITH_OGLES1_ - else if(driverstring == "ogles1") + else if (driverstring == "ogles1") driverType = video::EDT_OGLES1; #endif #ifdef _IRR_COMPILE_WITH_OGLES2_ - else if(driverstring == "ogles2") + else if (driverstring == "ogles2") driverType = video::EDT_OGLES2; #endif - else - { - errorstream<<"WARNING: Invalid video_driver specified; defaulting " - "to opengl"<getVideoModeList(); - if(videomode_list == 0){ + if (videomode_list == 0) { nulldevice->drop(); return 1; } @@ -1422,18 +1382,18 @@ int main(int argc, char *argv[]) s32 videomode_count = videomode_list->getVideoModeCount(); core::dimension2d videomode_res; s32 videomode_depth; - for (s32 i = 0; i < videomode_count; ++i){ + for (s32 i = 0; i < videomode_count; ++i) { videomode_res = videomode_list->getVideoModeResolution(i); videomode_depth = videomode_list->getVideoModeDepth(i); - dstream<getDesktopResolution(); videomode_depth = videomode_list->getDesktopDepth(); - dstream<drop(); @@ -1502,9 +1462,9 @@ int main(int argc, char *argv[]) /* Speed tests (done after irrlicht is loaded to get timer) */ - if(cmd_args.getFlag("speedtests")) + if (cmd_args.getFlag("speedtests")) { - dstream<<"Running speed tests"<drop(); return 0; @@ -1515,7 +1475,7 @@ int main(int argc, char *argv[]) bool random_input = g_settings->getBool("random_input") || cmd_args.getFlag("random-input"); InputHandler *input = NULL; - if(random_input) { + if (random_input) { input = new RandomInputHandler(); } else { input = new RealInputHandler(device, &receiver); @@ -1537,38 +1497,36 @@ int main(int argc, char *argv[]) font_path = g_settings->get(fallback + "font_path"); u32 font_shadow = g_settings->getU16(fallback + "font_shadow"); u32 font_shadow_alpha = g_settings->getU16(fallback + "font_shadow_alpha"); - font = gui::CGUITTFont::createTTFont(guienv, font_path.c_str(), font_size, true, true, font_shadow, font_shadow_alpha); + font = gui::CGUITTFont::createTTFont(guienv, font_path.c_str(), font_size, + true, true, font_shadow, font_shadow_alpha); } else { font = guienv->getFont(font_path.c_str()); } #else font = guienv->getFont(font_path.c_str()); #endif - if(font) + if (font) skin->setFont(font); else - errorstream<<"WARNING: Font file was not found." - " Using default font."<getFont(); assert(font); u32 text_height = font->getDimension(L"Hello, world!").Height; - infostream<<"text_height="<setWindowCaption((std::wstring(L"Minetest [")+text+L"]").c_str()); + device->setWindowCaption((std::wstring(L"Minetest [") + text + L"]").c_str()); delete[] text; // This is used for catching disconnects @@ -1627,7 +1585,7 @@ int main(int argc, char *argv[]) Otherwise they won't be automatically drawn. */ guiroot = guienv->addStaticText(L"", - core::rect(0, 0, 10000, 10000)); + core::rect(0, 0, 10000, 10000)); SubgameSpec gamespec; WorldSpec worldspec; @@ -1644,10 +1602,9 @@ int main(int argc, char *argv[]) Loop quits when menu returns proper parameters. */ - while(kill == false) - { + while (kill == false) { // If skip_main_menu, only go through here once - if(skip_main_menu && !first_loop){ + if (skip_main_menu && !first_loop) { kill = true; break; } @@ -1666,7 +1623,7 @@ int main(int argc, char *argv[]) menudata.port = itos(port); menudata.errormessage = wide_to_narrow(error_message); error_message = L""; - if(cmd_args.exists("password")) + if (cmd_args.exists("password")) menudata.password = cmd_args.get("password"); driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, g_settings->getBool("mip_map")); @@ -1676,44 +1633,43 @@ int main(int argc, char *argv[]) std::vector worldspecs = getAvailableWorlds(); // If a world was commanded, append and select it - if(commanded_world != ""){ + if (commanded_world != "") { std::string gameid = getWorldGameId(commanded_world, true); std::string name = _("[--world parameter]"); - if(gameid == ""){ + if (gameid == "") { gameid = g_settings->get("default_game"); name += " [new]"; } //TODO find within worldspecs and set config } - if(skip_main_menu == false) - { + if (skip_main_menu == false) { video::IVideoDriver* driver = device->getVideoDriver(); - infostream<<"Waiting for other menus"<run() && kill == false) - { - if(noMenuActive()) + infostream << "Waiting for other menus" << std::endl; + while (device->run() && kill == false) { + if (noMenuActive()) break; driver->beginScene(true, true, - video::SColor(255,128,128,128)); + video::SColor(255, 128, 128, 128)); guienv->drawAll(); driver->endScene(); // On some computers framerate doesn't seem to be // automatically limited sleep_ms(25); } - infostream<<"Waited for other menus"<clear(); } - if(menudata.errormessage != ""){ + if (menudata.errormessage != "") { error_message = narrow_to_wide(menudata.errormessage); continue; } @@ -1731,7 +1687,7 @@ int main(int argc, char *argv[]) address = menudata.address; int newport = stoi(menudata.port); - if(newport != 0) + if (newport != 0) port = newport; simple_singleplayer_mode = menudata.simple_singleplayer_mode; @@ -1739,13 +1695,13 @@ int main(int argc, char *argv[]) // Save settings g_settings->set("name", playername); - if((menudata.selected_world >= 0) && + if ((menudata.selected_world >= 0) && (menudata.selected_world < (int)worldspecs.size())) g_settings->set("selected_world_path", worldspecs[menudata.selected_world].path); // Break out of menu-game loop to shut down cleanly - if(device->run() == false || kill == true) + if (device->run() == false || kill == true) break; current_playername = playername; @@ -1754,14 +1710,12 @@ int main(int argc, char *argv[]) current_port = port; // If using simple singleplayer mode, override - if(simple_singleplayer_mode){ + if (simple_singleplayer_mode) { current_playername = "singleplayer"; current_password = ""; current_address = ""; current_port = myrand_range(49152, 65535); - } - else if (address != "") - { + } else if (address != "") { ServerListSpec server; server["name"] = menudata.servername; server["address"] = menudata.address; @@ -1779,35 +1733,34 @@ int main(int argc, char *argv[]) } // If local game - if(current_address == "") - { - if(menudata.selected_world == -1){ + if (current_address == "") { + if (menudata.selected_world == -1) { error_message = wgettext("No world selected and no address " "provided. Nothing to do."); - errorstream<run() == false || kill == true) { - if(g_settings_path != "") { - g_settings->updateConfigFile( - g_settings_path.c_str()); + if (device->run() == false || kill == true) { + if (g_settings_path != "") { + g_settings->updateConfigFile(g_settings_path.c_str()); } break; } @@ -1850,7 +1802,7 @@ int main(int argc, char *argv[]) catch(con::PeerNotFoundException &e) { error_message = wgettext("Connection error (timed out?)"); - errorstream<updateConfigFile(g_settings_path.c_str()); // Print modified quicktune values { bool header_printed = false; std::vector names = getQuicktuneNames(); - for(u32 i=0; i