void BattleMapTab::OnStartTypeSelect(wxCommandEvent& /*unused*/) { if (!m_battle) return; const std::string pos = stdprintf("%d", m_start_radios->GetSelection()); m_battle->CustomBattleOptions().setSingleOption("startpostype", pos, LSL::Enum::EngineOption); m_battle->SendHostInfo(stdprintf("%d_startpostype", LSL::Enum::EngineOption)); }
void ServerEvents::OnRedirect(const std::string& address, unsigned int port, const std::string& CurrentNick, const std::string& CurrentPassword) { const std::string name = stdprintf("%s:%d", address.c_str(), port); sett().SetServer(name, TowxString(address), port, ""); //TODO: Rework this. Server calls it`s own method through another object! ServerManager::Instance()->DoConnectToServer(name, CurrentNick, CurrentPassword); }
//FIXME: merge with basicly duplicate in slpaths.cpp std::string GetSpringlobbyInfo() { static const std::string nl = std::string("\n"); std::string res; res = getSpringlobbyAgent() + nl; const bool configwriteable = wxFileName::IsFileWritable(TowxString(SlPaths::GetConfigPath())); res += stdprintf("SpringLobby config file: %s (%swritable)\n", SlPaths::GetConfigPath().c_str(), BtS(configwriteable, "", "not ").c_str()); Paths paths; getWritePaths(paths); for (size_t i = 0; i < paths.size(); ++i) { std::string path = paths[i].m_path; #if defined(__WIN32__) || defined(_MSC_VER) path = Utf8ToLocalEncoding(path.c_str()); #endif res += stdprintf("%s (%s)\n", paths[i].m_desc.c_str(), path.c_str()); const bool wx = wxFileName::IsDirWritable(path); const bool posix = access(path.c_str(), WRITABLE) == 0; bool tried = false; try { std::ofstream of; wxString dummy_fn = paths[i].m_path; if (!wxEndsWithPathSeparator(dummy_fn)) { dummy_fn += wxFileName::GetPathSeparator(); } dummy_fn += _T("dummy.txt"); std::string dummyFileString = dummy_fn.ToStdString(); #if defined(__WIN32__) || defined(_MSC_VER) dummyFileString = Utf8ToLocalEncoding(dummyFileString.c_str()); #endif of.open(dummyFileString); if (of.is_open()) { of << "fhreuohgeiuhguie"; of.flush(); of.close(); tried = wxRemoveFile(dummyFileString); } } catch (...) { } if (paths[i].m_requireswrite && (!wx || !posix || !tried)) { wxLogError("%s is not writeable!", path.c_str()); } res += stdprintf(("\tWX: %s POSIX: %s TRY: %s\n"), BtS(wx).c_str(), BtS(posix).c_str(), BtS(tried).c_str()); } res += stdprintf("Current unitsync: %s\n", SlPaths::GetUnitSync().c_str()); res += stdprintf("Current spring executable: %s\n", SlPaths::GetSpringBinary().c_str()); res += stdprintf("Portable mode: %s\n", BtS(SlPaths::IsPortableMode()).c_str()); res += stdprintf(("Compiled with wxWidgets %d.%d.%d.%d"), wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER, wxSUBRELEASE_NUMBER) + nl; res += "Started with: \n"; for (int i = 0; i < wxTheApp->argc; ++i) res += STD_STRING(wxTheApp->argv[i]) + std::string(" "); return res; }
void ContentDownloadDialog::Search(const wxString& str) { //FIXME: make async! const std::string param = ConvToIRI(STD_STRING(str)); const std::string query = stdprintf("http://api.springfiles.com/json.php?nosensitive=on&logical=or&springname=%s&tag=%s", param.c_str(), param.c_str()); std::string res; CHttpDownloader::DownloadUrl(query, res); CHttpDownloader::ParseResult(STD_STRING(str), res, dls); wxCommandEvent e; OnSearchCompleted(e); }
void BattleroomMMOptionsTab::OnSpinCtrlDoubleChange(wxSpinDoubleEvent& event) { if (!m_battle) return; wxSpinCtrlDouble* box = (wxSpinCtrlDouble*)event.GetEventObject(); const auto key = STD_STRING((box->GetName()).AfterFirst(sep)); long gameoption; box->GetName().BeforeFirst(sep).ToLong(&gameoption); if (m_battle->CustomBattleOptions().setSingleOption(key, stdprintf("%f", box->GetValue()), (LSL::Enum::GameOption)gameoption)) { if (m_battle->IsFounderMe()) { m_battle->SendHostInfo(STD_STRING((wxString() << gameoption) + wxsep + TowxString(key))); } } }
//! (koshi) don't delete commented things please, they might be need in the future and i'm lazy void IBattle::GetBattleFromScript( bool loadmapmod ) { BattleOptions opts; std::stringstream ss (GetScript()); LSL::TDF::PDataList script( LSL::TDF::ParseTDF(ss) ); LSL::TDF::PDataList replayNode ( script->Find("GAME") ); if ( replayNode.ok() ) { std::string modname = replayNode->GetString("GameType"); std::string modhash = replayNode->GetString("ModHash"); if ( !modhash.empty() ) modhash = STD_STRING(MakeHashUnsigned(TowxString(modhash))); SetHostMod( modname, modhash ); //don't have the maphash, what to do? //ui download function works with mapname if hash is empty, so works for now std::string mapname = replayNode->GetString("MapName"); std::string maphash = replayNode->GetString("MapHash"); if ( !maphash.empty() ) maphash = STD_STRING(MakeHashUnsigned(TowxString(maphash))); SetHostMap( mapname, maphash ); // opts.ip = replayNode->GetString( _T("HostIP") ); // opts.port = replayNode->GetInt ( _T("HostPort"), DEFAULT_EXTERNAL_UDP_SOURCE_PORT ); opts.spectators = 0; int playernum = replayNode->GetInt("NumPlayers", 0); int usersnum = replayNode->GetInt("NumUsers", 0); if ( usersnum > 0 ) playernum = usersnum; // int allynum = replayNode->GetInt ( _T("NumAllyTeams"), 1); // int teamnum = replayNode->GetInt ( _T("NumTeams"), 1); LSL::StringVector sides; if ( loadmapmod ) { sides = LSL::usync().GetSides(modname); } IBattle::TeamVec parsed_teams = GetParsedTeamsVec(); IBattle::AllyVec parsed_allies = GetParsedAlliesVec(); //[PLAYERX] sections for ( int i = 0; i < playernum ; ++i ) { LSL::TDF::PDataList player ( replayNode->Find(stdprintf("PLAYER%d", i))); LSL::TDF::PDataList bot ( replayNode->Find(stdprintf("AI%d", i ))); if ( player.ok() || bot.ok() ) { if ( bot.ok() ) player = bot; User user(player->GetString("Name"), boost::to_upper_copy(player->GetString("CountryCode")), 0); UserBattleStatus& status = user.BattleStatus(); status.isfromdemo = true; status.spectator = player->GetInt("Spectator", 0 ); opts.spectators += user.BattleStatus().spectator; status.team = player->GetInt("Team"); if ( !status.spectator ) { PlayerJoinedTeam( status.team ); } status.sync = true; status.ready = true; if ( status.spectator ) m_opts.spectators++; else { if ( !bot.ok() ) { if ( status.ready) m_players_ready++; if ( status.sync ) m_players_sync++; if ( status.sync && status.ready ) m_players_ok++; } } //! (koshi) changed this from ServerRankContainer to RankContainer user.Status().rank = (UserStatus::RankContainer)player->GetInt("Rank", -1 ); if ( bot.ok() ) { status.aishortname = bot->GetString("ShortName"); status.aiversion = bot->GetString("Version"); int ownerindex = bot->GetInt("Host"); LSL::TDF::PDataList aiowner (replayNode->Find(stdprintf("PLAYER%d", ownerindex))); if ( aiowner.ok() ) { status.owner = aiowner->GetString("Name"); } } IBattle::TeamInfoContainer teaminfos = parsed_teams[user.BattleStatus().team]; if ( !teaminfos.exist ) { LSL::TDF::PDataList team( replayNode->Find( stdprintf("TEAM%d", user.BattleStatus().team) )); if ( team.ok() ) { teaminfos.exist = true; teaminfos.TeamLeader = team->GetInt("TeamLeader", 0 ); teaminfos.StartPosX = team->GetInt("StartPosX", -1 ); teaminfos.StartPosY = team->GetInt("StartPosY", -1 ); teaminfos.AllyTeam = team->GetInt("AllyTeam", 0 ); teaminfos.RGBColor = GetColorFromFloatStrng(TowxString(team->GetString("RGBColor"))); teaminfos.SideName = team->GetString("Side", ""); teaminfos.Handicap = team->GetInt("Handicap", 0 ); const int sidepos = LSL::Util::IndexInSequence(sides, teaminfos.SideName); teaminfos.SideNum = sidepos; parsed_teams[ user.BattleStatus().team ] = teaminfos; } } if ( teaminfos.exist ) { status.ally = teaminfos.AllyTeam; status.pos.x = teaminfos.StartPosX; status.pos.y = teaminfos.StartPosY; status.colour = teaminfos.RGBColor; status.handicap = teaminfos.Handicap; if ( !status.spectator ) { PlayerJoinedAlly( status.ally ); } if ( teaminfos.SideNum >= 0 ) status.side = teaminfos.SideNum; IBattle::AllyInfoContainer allyinfos = parsed_allies[user.BattleStatus().ally]; if ( !allyinfos.exist ) { LSL::TDF::PDataList ally( replayNode->Find(stdprintf("ALLYTEAM%d", user.BattleStatus().ally) ) ); if ( ally.ok() ) { allyinfos.exist = true; allyinfos.NumAllies = ally->GetInt("NumAllies", 0 ); allyinfos.StartRectLeft = ally->GetInt("StartRectLeft", 0 ); allyinfos.StartRectTop = ally->GetInt("StartRectTop", 0 ); allyinfos.StartRectRight = ally->GetInt("StartRectRight", 0 ); allyinfos.StartRectBottom = ally->GetInt("StartRectBottom", 0 ); parsed_allies[ user.BattleStatus().ally ] = allyinfos; AddStartRect( user.BattleStatus().ally, allyinfos.StartRectTop, allyinfos.StartRectTop, allyinfos.StartRectRight, allyinfos.StartRectBottom ); } } } AddUserFromDemo( user ); } } SetParsedTeamsVec( parsed_teams ); SetParsedAlliesVec( parsed_allies ); //MMoptions, this'll fail unless loading map/mod into wrapper first if ( loadmapmod ) { LoadScriptMMOpts("mapoptions", replayNode ); LoadScriptMMOpts("modoptions", replayNode ); } opts.maxplayers = playernum ; } SetBattleOptions( opts ); }
Channel& ChannelList::GetChannel(const std::string& name) { channel_iter_t u = m_chans.find(name); ASSERT_LOGIC(u != m_chans.end(), stdprintf("ChannelList::GetChannel(\"%s\"): no such channel", name.c_str())); return *u->second; }
wxString Spring::WriteScriptTxt( IBattle& battle ) const { wxLogMessage(_T("0 WriteScriptTxt called ")); std::stringstream ret; LSL::TDF::TDFWriter tdf(ret); // Start generating the script. tdf.EnterSection("GAME"); if ( battle.IsFounderMe() ) { tdf.Append("HostIP", ""); //Listen on all addresses for connections when hosting if ( battle.GetNatType() == NAT_Hole_punching ) tdf.Append("HostPort", battle.GetMyInternalUdpSourcePort() ); else tdf.Append("HostPort", battle.GetHostPort() ); } else { tdf.Append("HostIP", battle.GetHostIp() ); tdf.Append("HostPort", battle.GetHostPort() ); if ( battle.GetNatType() == NAT_Hole_punching ) { tdf.Append("SourcePort", battle.GetMyInternalUdpSourcePort() ); } else if ( sett().GetClientPort() != 0) { tdf.Append("SourcePort", sett().GetClientPort() ); /// this allows to play with broken router by setting SourcePort to some forwarded port. } } tdf.Append("IsHost", battle.IsFounderMe() ); User& me = battle.GetMe(); tdf.Append("MyPlayerName", me.GetNick()); if ( !me.BattleStatus().scriptPassword.empty() ) { tdf.Append("MyPasswd", me.BattleStatus().scriptPassword); } if ( !battle.IsFounderMe() ) { tdf.LeaveSection(); return TowxString(ret.str()); } /********************************************************************************** Host-only section **********************************************************************************/ tdf.AppendLineBreak(); tdf.Append("ModHash", battle.LoadMod().hash); tdf.Append("MapHash", battle.LoadMap().hash); tdf.Append("Mapname", battle.GetHostMapName()); tdf.Append("GameType", battle.GetHostModName()); tdf.AppendLineBreak(); switch ( battle.GetBattleType() ) { case BT_Played: break; case BT_Replay: { wxString path = TowxString(battle.GetPlayBackFilePath()); if ( path.Find(_T("/")) != wxNOT_FOUND ) path.BeforeLast(_T('/')); tdf.Append("DemoFile", STD_STRING(path)); tdf.AppendLineBreak(); break; } case BT_Savegame: { wxString path = TowxString(battle.GetPlayBackFilePath()); if ( path.Find(_T("/")) != wxNOT_FOUND ) path.BeforeLast(_T('/')); tdf.Append("Savefile", STD_STRING(path)); tdf.AppendLineBreak(); break; } default: slLogDebugFunc(""); break; } const long startpostype = LSL::Util::FromString<long>( battle.CustomBattleOptions().getSingleValue("startpostype", LSL::Enum::EngineOption )); std::vector<LSL::StartPos> remap_positions; if ( battle.IsProxy() && ( startpostype != IBattle::ST_Pick ) && ( startpostype != IBattle::ST_Choose ) ) { std::set<int> parsedteams; unsigned int NumUsers = battle.GetNumUsers(); unsigned int NumTeams = 0; for ( unsigned int i = 0; i < NumUsers; i++ ) { User& usr = battle.GetUser( i ); UserBattleStatus& status = usr.BattleStatus(); if ( status.spectator ) continue; if ( parsedteams.find( status.team ) != parsedteams.end() ) continue; // skip duplicates parsedteams.insert( status.team ); NumTeams++; } LSL::MapInfo infos = battle.LoadMap().info; unsigned int nummapstartpositions = infos.positions.size(); unsigned int copysize = std::min( nummapstartpositions, NumTeams ); remap_positions = std::vector<LSL::StartPos> ( infos.positions.begin(), infos.positions.begin() + copysize ); // only add the first x positions if ( startpostype == IBattle::ST_Random ) { std::random_shuffle( remap_positions.begin(), remap_positions.end() ); // shuffle the positions } } if ( battle.IsProxy() ) { if ( ( startpostype == IBattle::ST_Random ) || ( startpostype == IBattle::ST_Fixed ) ) { tdf.Append("startpostype", IBattle::ST_Pick ); } else tdf.Append("startpostype", startpostype ); } else tdf.Append("startpostype", startpostype ); tdf.EnterSection("mapoptions"); for (const auto& it : battle.CustomBattleOptions().getOptions( LSL::Enum::MapOption )) { tdf.Append(it.first, it.second.second); } tdf.LeaveSection(); tdf.EnterSection("modoptions"); tdf.Append("relayhoststartpostype", startpostype ); // also save the original wanted setting for (const auto& it : battle.CustomBattleOptions().getOptions( LSL::Enum::ModOption )) { tdf.Append(it.first, it.second.second); } tdf.LeaveSection(); std::map<std::string,int> units = battle.RestrictedUnits(); tdf.Append("NumRestrictions", units.size()); tdf.EnterSection("RESTRICT"); int restrictcount = 0; for ( std::map<std::string, int>::const_iterator itor = units.begin(); itor != units.end(); ++itor ) { tdf.Append(stdprintf("Unit%d", restrictcount), itor->first ); tdf.Append(stdprintf("Limit%d", restrictcount), itor->second ); restrictcount++; } tdf.LeaveSection(); tdf.AppendLineBreak(); if ( battle.IsProxy() ) { tdf.Append("NumPlayers", battle.GetNumPlayers() -1 ); tdf.Append("NumUsers", battle.GetNumUsers() -1 ); } else { tdf.Append("NumPlayers", battle.GetNumPlayers() ); tdf.Append("NumUsers", battle.GetNumUsers() ); } tdf.AppendLineBreak(); unsigned int NumUsers = battle.GetNumUsers(); typedef std::map<int, int> ProgressiveTeamsVec; typedef ProgressiveTeamsVec::iterator ProgressiveTeamsVecIter; ProgressiveTeamsVec teams_to_sorted_teams; // original team -> progressive team int free_team = 0; std::map<User*, int> player_to_number; // player -> ordernumber srand ( time(NULL) ); for ( unsigned int i = 0; i < NumUsers; i++ ) { User& user = battle.GetUser( i ); UserBattleStatus& status = user.BattleStatus(); if ( !status.spectator ) { ProgressiveTeamsVecIter itor = teams_to_sorted_teams.find ( status.team ); if ( itor == teams_to_sorted_teams.end() ) { teams_to_sorted_teams[status.team] = free_team; free_team++; } } if ( battle.IsProxy() && ( user.GetNick() == battle.GetFounder().GetNick() ) ) continue; if ( status.IsBot() ) continue; tdf.EnterSection(stdprintf("PLAYER%d", i)); tdf.Append("Name", user.GetNick() ); tdf.Append("CountryCode", STD_STRING(TowxString(user.GetCountry()).Lower())); tdf.Append("Spectator", status.spectator ); tdf.Append("Rank", (int)user.GetRank() ); tdf.Append("IsFromDemo", int(status.isfromdemo) ); if ( !status.scriptPassword.empty() ) { tdf.Append("Password", status.scriptPassword ); } if ( !status.spectator ) { tdf.Append("Team", teams_to_sorted_teams[status.team] ); } else { int speccteam = 0; if ( !teams_to_sorted_teams.empty() ) speccteam = rand() % teams_to_sorted_teams.size(); tdf.Append("Team", speccteam ); } tdf.LeaveSection(); player_to_number[&user] = i; } for ( unsigned int i = 0; i < NumUsers; i++ ) { User& user = battle.GetUser( i ); UserBattleStatus& status = user.BattleStatus(); if ( !status.IsBot() ) continue; tdf.EnterSection(stdprintf("AI%d", i)); tdf.Append("Name", user.GetNick()); // AI's nick; tdf.Append("ShortName", status.aishortname ); // AI libtype tdf.Append("Version", status.aiversion ); // AI libtype version tdf.Append("Team", teams_to_sorted_teams[status.team] ); tdf.Append("IsFromDemo", int(status.isfromdemo) ); tdf.Append("Host", player_to_number[&battle.GetUser( status.owner )] ); tdf.EnterSection("Options"); int optionmapindex = battle.CustomBattleOptions().GetAIOptionIndex(user.GetNick()); if ( optionmapindex > 0 ) { for (const auto& it : battle.CustomBattleOptions().getOptions((LSL::Enum::GameOption)optionmapindex )) { tdf.Append(it.first, it.second.second); } } tdf.LeaveSection(); tdf.LeaveSection(); player_to_number[&user] = i; } tdf.AppendLineBreak(); std::set<int> parsedteams; const auto sides = LSL::usync().GetSides(battle.GetHostModName()); for ( unsigned int i = 0; i < NumUsers; i++ ) { User& usr = battle.GetUser( i ); UserBattleStatus& status = usr.BattleStatus(); if ( status.spectator ) continue; if ( parsedteams.find( status.team ) != parsedteams.end() ) continue; // skip duplicates parsedteams.insert( status.team ); tdf.EnterSection(stdprintf("TEAM%d", teams_to_sorted_teams[status.team])); if ( status.IsBot() ) { tdf.Append("TeamLeader", player_to_number[&battle.GetUser( status.owner )] ); } else { tdf.Append("TeamLeader", player_to_number[&usr] ); } if ( battle.IsProxy() ) { if ( startpostype == IBattle::ST_Pick ) { tdf.Append("StartPosX", status.pos.x ); tdf.Append("StartPosZ", status.pos.y ); } else if ( ( startpostype == IBattle::ST_Fixed ) || ( startpostype == IBattle::ST_Random ) ) { int teamnumber = teams_to_sorted_teams[status.team]; if ( teamnumber < int(remap_positions.size()) ) { // don't overflow LSL::StartPos position = remap_positions[teamnumber]; tdf.Append("StartPosX", position.x ); tdf.Append("StartPosZ", position.y ); } } } else { if ( startpostype == IBattle::ST_Pick ) { tdf.Append("StartPosX", status.pos.x ); tdf.Append("StartPosZ", status.pos.y ); } } tdf.Append("AllyTeam",status.ally ); wxString colourstring = TowxString( status.colour.Red()/255.0 ) + _T(' ') + TowxString( status.colour.Green()/255.0 ) + _T(' ') + TowxString( status.colour.Blue()/255.0 ); tdf.Append("RGBColor", STD_STRING(colourstring)); unsigned int side = status.side; if ( side < sides.size() ) tdf.Append("Side", sides[side] ); tdf.Append("Handicap", status.handicap ); tdf.LeaveSection(); } tdf.AppendLineBreak(); unsigned int maxiter = std::max( NumUsers, battle.GetLastRectIdx() + 1 ); std::set<int> parsedallys; for ( unsigned int i = 0; i < maxiter; i++ ) { User& usr = battle.GetUser( i ); UserBattleStatus& status = usr.BattleStatus(); BattleStartRect sr = battle.GetStartRect( i ); if ( status.spectator && !sr.IsOk() ) continue; int ally = status.ally; if ( status.spectator ) ally = i; if ( parsedallys.find( ally ) != parsedallys.end() ) continue; // skip duplicates sr = battle.GetStartRect( ally ); parsedallys.insert( ally ); tdf.EnterSection( stdprintf("ALLYTEAM%d", ally)); tdf.Append("NumAllies", 0 ); if ( startpostype == IBattle::ST_Choose ) { if ( sr.IsOk() ) { const char* old_locale = std::setlocale(LC_NUMERIC, "C"); tdf.Append("StartRectLeft", wxFormat( _T("%.3f") ) % ( sr.left / 200.0 ) ); tdf.Append("StartRectTop", wxFormat( _T("%.3f") ) % ( sr.top / 200.0 ) ); tdf.Append("StartRectRight", wxFormat( _T("%.3f") ) % ( sr.right / 200.0 ) ); tdf.Append("StartRectBottom", wxFormat( _T("%.3f") ) % ( sr.bottom / 200.0 ) ); std::setlocale(LC_NUMERIC, old_locale); } } tdf.LeaveSection(); } tdf.LeaveSection(); return TowxString(ret.str()); }