NetPlay: add one click host

Add a context menu entry in main game list to host a netplay game
based on saved settings.

Original commit:
commit 91aaa958e6
Author: Aestek <thib.gilles@gmail.com>
Date:   Sun Jul 24 14:51:37 2016 +0200
This commit is contained in:
Shawn Hoffman
2016-10-03 05:35:27 -07:00
parent 46b6a32b46
commit 41e7c43d0d
12 changed files with 319 additions and 182 deletions

View File

@ -19,40 +19,17 @@
#include "Core/NetPlayServer.h"
#include "DolphinWX/Frame.h"
#include "DolphinWX/Main.h"
#include "DolphinWX/NetPlay/NetPlayLauncher.h"
#include "DolphinWX/NetPlay/NetPlaySetupFrame.h"
#include "DolphinWX/NetPlay/NetWindow.h"
#include "DolphinWX/WxUtils.h"
namespace
{
const std::string DEFAULT_TRAVERSAL_SERVER = "stun.dolphin-emu.org";
const std::string DEFAULT_TRAVERSAL_PORT = "6262";
std::string GetFromINI(IniFile::Section& section, const std::string& key,
const std::string& default_value)
{
std::string result;
section.Get(key, &result, default_value);
result.erase(std::remove(result.begin(), result.end(), ' '), result.end());
if (result.empty())
return default_value;
return result;
}
std::string GetTraversalPort(IniFile::Section& section)
{
return GetFromINI(section, "TraversalPort", DEFAULT_TRAVERSAL_PORT);
}
std::string GetTraversalServer(IniFile::Section& section)
{
return GetFromINI(section, "TraversalServer", DEFAULT_TRAVERSAL_SERVER);
}
wxString GetTraversalLabelText(IniFile::Section& section)
{
std::string server = GetTraversalServer(section);
std::string port = GetTraversalPort(section);
std::string server = NetPlayLaunchConfig::GetTraversalHostFromIniConfig(section);
std::string port = std::to_string(NetPlayLaunchConfig::GetTraversalPortFromIniConfig(section));
return wxString::Format(_("Traversal Server: %s"), (server + ":" + port).c_str());
}
} // Anonymous namespace
@ -144,7 +121,8 @@ NetPlaySetupFrame::NetPlaySetupFrame(wxWindow* const parent, const CGameListCtrl
// string? w/e
std::string port;
netplay_section.Get("ConnectPort", &port, "2626");
netplay_section.Get("ConnectPort", &port,
std::to_string(NetPlayHostConfig::DEFAULT_LISTEN_PORT));
m_connect_port_text = new wxTextCtrl(connect_tab, wxID_ANY, StrToWxStr(port));
wxButton* const connect_btn = new wxButton(connect_tab, wxID_ANY, _("Connect"));
@ -184,7 +162,7 @@ NetPlaySetupFrame::NetPlaySetupFrame(wxWindow* const parent, const CGameListCtrl
// string? w/e
std::string port;
netplay_section.Get("HostPort", &port, "2626");
netplay_section.Get("HostPort", &port, std::to_string(NetPlayHostConfig::DEFAULT_LISTEN_PORT));
m_host_port_text = new wxTextCtrl(host_tab, wxID_ANY, StrToWxStr(port));
m_traversal_listen_port_enabled = new wxCheckBox(host_tab, wxID_ANY, _("Force Listen Port: "));
@ -304,63 +282,6 @@ NetPlaySetupFrame::~NetPlaySetupFrame()
main_frame->g_NetPlaySetupDiag = nullptr;
}
void NetPlaySetupFrame::MakeNetPlayDiag(int port, const std::string& game, bool is_hosting)
{
NetPlayDialog*& npd = NetPlayDialog::GetInstance();
NetPlayClient*& netplay_client = NetPlayDialog::GetNetPlayClient();
bool trav = !is_hosting && m_direct_traversal->GetCurrentSelection() == TRAVERSAL_CHOICE;
std::string ip;
npd = new NetPlayDialog(m_parent, m_game_list, game, is_hosting);
if (is_hosting)
ip = "127.0.0.1";
else if (trav)
ip = WxStrToStr(m_connect_hashcode_text->GetValue());
else
ip = WxStrToStr(m_connect_ip_text->GetValue());
IniFile inifile;
inifile.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
IniFile::Section& netplay_section = *inifile.GetOrCreateSection("NetPlay");
std::string centralPortString = GetTraversalPort(netplay_section);
unsigned long int centralPort;
StrToWxStr(centralPortString).ToULong(&centralPort);
std::string centralServer = GetTraversalServer(netplay_section);
netplay_client = new NetPlayClient(ip, (u16)port, npd, WxStrToStr(m_nickname_text->GetValue()),
trav, centralServer, (u16)centralPort);
if (netplay_client->IsConnected())
{
int winPosX, winPosY, winWidth, winHeight;
// Remember the window size and position for NetWindow
netplay_section.Get("NetWindowPosX", &winPosX, -1);
netplay_section.Get("NetWindowPosY", &winPosY, -1);
netplay_section.Get("NetWindowWidth", &winWidth, 768);
netplay_section.Get("NetWindowHeight", &winHeight, 768 - 128);
if (winPosX == -1 || winPosY == -1)
{
npd->SetSize(768, 768 - 128);
npd->Center();
}
else
{
npd->SetSize(winPosX, winPosY, winWidth, winHeight);
}
npd->Show();
Destroy();
}
else
{
npd->Destroy();
}
}
void NetPlaySetupFrame::OnHost(wxCommandEvent&)
{
DoHost();
@ -368,71 +289,46 @@ void NetPlaySetupFrame::OnHost(wxCommandEvent&)
void NetPlaySetupFrame::DoHost()
{
IniFile ini_file;
const std::string dolphin_ini = File::GetUserPath(F_DOLPHINCONFIG_IDX);
ini_file.Load(dolphin_ini);
IniFile::Section& netplay_section = *ini_file.GetOrCreateSection("NetPlay");
NetPlayDialog*& npd = NetPlayDialog::GetInstance();
NetPlayServer*& netplay_server = NetPlayDialog::GetNetPlayServer();
if (npd)
{
WxUtils::ShowErrorDialog(_("A NetPlay window is already open!"));
return;
}
if (m_game_lbox->GetSelection() == wxNOT_FOUND)
{
WxUtils::ShowErrorDialog(_("You must choose a game!"));
return;
}
std::string game(WxStrToStr(m_game_lbox->GetStringSelection()));
IniFile ini_file;
const std::string dolphin_ini = File::GetUserPath(F_DOLPHINCONFIG_IDX);
ini_file.Load(dolphin_ini);
IniFile::Section& netplay_section = *ini_file.GetOrCreateSection("NetPlay");
bool trav;
unsigned long listen_port = 0;
if (m_direct_traversal->GetCurrentSelection() == TRAVERSAL_CHOICE)
NetPlayHostConfig host_config;
host_config.game_name = WxStrToStr(m_game_lbox->GetStringSelection());
host_config.use_traversal = m_direct_traversal->GetCurrentSelection() == TRAVERSAL_CHOICE;
host_config.player_name = WxStrToStr(m_nickname_text->GetValue());
host_config.game_list_ctrl = m_game_list;
host_config.parent_window = m_parent;
host_config.forward_port = m_upnp_chk->GetValue();
if (host_config.use_traversal)
{
trav = true;
listen_port =
m_traversal_listen_port_enabled->IsChecked() ? m_traversal_listen_port->GetValue() : 0;
host_config.listen_port = static_cast<u16>(
m_traversal_listen_port_enabled->IsChecked() ? m_traversal_listen_port->GetValue() : 0);
}
else
{
trav = false;
unsigned long listen_port;
m_host_port_text->GetValue().ToULong(&listen_port);
host_config.listen_port = static_cast<u16>(listen_port);
}
std::string centralPortString = GetTraversalPort(netplay_section);
unsigned long int centralPort;
StrToWxStr(centralPortString).ToULong(&centralPort);
host_config.traversal_port = NetPlayLaunchConfig::GetTraversalPortFromIniConfig(netplay_section);
host_config.traversal_host = NetPlayLaunchConfig::GetTraversalHostFromIniConfig(netplay_section);
std::string centralServer = GetTraversalServer(netplay_section);
netplay_section.Set("SelectedHostGame", host_config.game_name);
ini_file.Save(dolphin_ini);
netplay_server = new NetPlayServer((u16)listen_port, trav, centralServer, (u16)centralPort);
if (netplay_server->is_connected)
if (NetPlayLauncher::Host(host_config))
{
netplay_server->ChangeGame(game);
netplay_server->AdjustPadBufferSize(INITIAL_PAD_BUFFER_SIZE);
#ifdef USE_UPNP
if (m_upnp_chk->GetValue())
netplay_server->TryPortmapping(listen_port);
#endif
MakeNetPlayDiag(netplay_server->GetPort(), game, true);
netplay_server->SetNetPlayUI(NetPlayDialog::GetInstance());
netplay_section.Set("SelectedHostGame", game);
ini_file.Save(dolphin_ini);
}
else
{
if (trav && m_traversal_listen_port_enabled->IsChecked())
WxUtils::ShowErrorDialog(
_("Failed to listen. Someone is probably already listening on the port you specified."));
else
WxUtils::ShowErrorDialog(
_("Failed to listen. Is another instance of the NetPlay server running?"));
Destroy();
}
}
@ -443,16 +339,33 @@ void NetPlaySetupFrame::OnJoin(wxCommandEvent&)
void NetPlaySetupFrame::DoJoin()
{
NetPlayDialog*& npd = NetPlayDialog::GetInstance();
if (npd)
{
WxUtils::ShowErrorDialog(_("A NetPlay window is already open!"));
return;
}
IniFile inifile;
inifile.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
IniFile::Section& netplay_section = *inifile.GetOrCreateSection("NetPlay");
NetPlayJoinConfig join_config;
join_config.use_traversal = m_direct_traversal->GetCurrentSelection() == TRAVERSAL_CHOICE;
join_config.player_name = WxStrToStr(m_nickname_text->GetValue());
join_config.game_list_ctrl = m_game_list;
join_config.parent_window = m_parent;
unsigned long port = 0;
m_connect_port_text->GetValue().ToULong(&port);
MakeNetPlayDiag(port, "", false);
join_config.connect_port = static_cast<u16>(port);
if (join_config.use_traversal)
join_config.connect_hash_code = WxStrToStr(m_connect_hashcode_text->GetValue());
else
join_config.connect_host = WxStrToStr(m_connect_ip_text->GetValue());
join_config.traversal_port = NetPlayLaunchConfig::GetTraversalPortFromIniConfig(netplay_section);
join_config.traversal_host = NetPlayLaunchConfig::GetTraversalHostFromIniConfig(netplay_section);
if (NetPlayLauncher::Join(join_config))
{
Destroy();
}
}
void NetPlaySetupFrame::OnResetTraversal(wxCommandEvent& event)