Skip to content

Commit

Permalink
Merge pull request #5872 from ligfx/wxnetplayonionconfig
Browse files Browse the repository at this point in the history
WX: make Netplay use new-style config
  • Loading branch information
leoetlino committed Aug 4, 2017
2 parents 51af8d4 + dde3471 commit f274bbc
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 183 deletions.
17 changes: 17 additions & 0 deletions Source/Core/Common/Config/Section.cpp
Expand Up @@ -54,6 +54,11 @@ void Section::Set(const std::string& key, const std::string& value)
}
}

void Section::Set(const std::string& key, u16 newValue)
{
Section::Set(key, StringFromFormat("0x%04x", newValue));
}

void Section::Set(const std::string& key, u32 newValue)
{
Section::Set(key, StringFromFormat("0x%08x", newValue));
Expand Down Expand Up @@ -124,6 +129,18 @@ bool Section::Get(const std::string& key, int* value, int defaultValue) const
return false;
}

bool Section::Get(const std::string& key, u16* value, u16 defaultValue) const
{
std::string temp;
bool retval = Get(key, &temp);

if (retval && TryParse(temp, value))
return true;

*value = defaultValue;
return false;
}

bool Section::Get(const std::string& key, u32* value, u32 defaultValue) const
{
std::string temp;
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Common/Config/Section.h
Expand Up @@ -35,6 +35,7 @@ class Section
// Setters
virtual void Set(const std::string& key, const std::string& value);

void Set(const std::string& key, u16 newValue);
void Set(const std::string& key, u32 newValue);
void Set(const std::string& key, float newValue);
void Set(const std::string& key, double newValue);
Expand All @@ -57,6 +58,7 @@ class Section
const std::string& default_value = NULL_STRING) const;

bool Get(const std::string& key, int* value, int defaultValue = 0) const;
bool Get(const std::string& key, u16* value, u16 defaultValue = 0) const;
bool Get(const std::string& key, u32* value, u32 defaultValue = 0) const;
bool Get(const std::string& key, bool* value, bool defaultValue = false) const;
bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const;
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Core/CMakeLists.txt
Expand Up @@ -26,6 +26,7 @@ set(SRCS
Boot/DolReader.cpp
Boot/ElfReader.cpp
Config/GraphicsSettings.cpp
Config/NetplaySettings.cpp
ConfigLoaders/BaseConfigLoader.cpp
ConfigLoaders/GameConfigLoader.cpp
ConfigLoaders/IsSettingSaveable.cpp
Expand Down
36 changes: 36 additions & 0 deletions Source/Core/Core/Config/NetplaySettings.cpp
@@ -0,0 +1,36 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "Core/Config/NetplaySettings.h"

#include "Common/Config/Config.h"

namespace Config
{
static constexpr u16 DEFAULT_LISTEN_PORT = 2626;

// Configuration Information

// Main.NetPlay

const ConfigInfo<std::string> NETPLAY_TRAVERSAL_SERVER{{System::Main, "NetPlay", "TraversalServer"},
"stun.dolphin-emu.org"};
const ConfigInfo<u16> NETPLAY_TRAVERSAL_PORT{{System::Main, "NetPlay", "TraversalPort"}, 6262};
const ConfigInfo<std::string> NETPLAY_TRAVERSAL_CHOICE{{System::Main, "NetPlay", "TraversalChoice"},
"direct"};
const ConfigInfo<std::string> NETPLAY_HOST_CODE{{System::Main, "NetPlay", "HostCode"}, "00000000"};

const ConfigInfo<u16> NETPLAY_HOST_PORT{{System::Main, "NetPlay", "HostPort"}, DEFAULT_LISTEN_PORT};
const ConfigInfo<std::string> NETPLAY_ADDRESS{{System::Main, "NetPlay", "Address"}, "127.0.0.1"};
const ConfigInfo<u16> NETPLAY_CONNECT_PORT{{System::Main, "NetPlay", "ConnectPort"},
DEFAULT_LISTEN_PORT};
const ConfigInfo<u16> NETPLAY_LISTEN_PORT{{System::Main, "NetPlay", "ListenPort"},
DEFAULT_LISTEN_PORT};

const ConfigInfo<std::string> NETPLAY_NICKNAME{{System::Main, "NetPlay", "Nickname"}, "Player"};
const ConfigInfo<std::string> NETPLAY_SELECTED_HOST_GAME{
{System::Main, "NetPlay", "SelectedHostGame"}, ""};
const ConfigInfo<bool> NETPLAY_USE_UPNP{{System::Main, "NetPlay", "UseUPNP"}, false};

} // namespace Config
32 changes: 32 additions & 0 deletions Source/Core/Core/Config/NetplaySettings.h
@@ -0,0 +1,32 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <string>

#include "Common/CommonTypes.h"
#include "Common/Config/Config.h"

namespace Config
{
// Configuration Information

// Main.NetPlay

extern const ConfigInfo<std::string> NETPLAY_TRAVERSAL_SERVER;
extern const ConfigInfo<u16> NETPLAY_TRAVERSAL_PORT;
extern const ConfigInfo<std::string> NETPLAY_TRAVERSAL_CHOICE;
extern const ConfigInfo<std::string> NETPLAY_HOST_CODE;

extern const ConfigInfo<u16> NETPLAY_HOST_PORT;
extern const ConfigInfo<std::string> NETPLAY_ADDRESS;
extern const ConfigInfo<u16> NETPLAY_CONNECT_PORT;
extern const ConfigInfo<u16> NETPLAY_LISTEN_PORT;

extern const ConfigInfo<std::string> NETPLAY_NICKNAME;
extern const ConfigInfo<std::string> NETPLAY_SELECTED_HOST_GAME;
extern const ConfigInfo<bool> NETPLAY_USE_UPNP;

} // namespace Config
3 changes: 3 additions & 0 deletions Source/Core/Core/ConfigLoaders/IsSettingSaveable.cpp
Expand Up @@ -17,6 +17,9 @@ bool IsSettingSaveable(const Config::ConfigLocation& config_location)
if (config_location.system == Config::System::Logger)
return true;

if (config_location.system == Config::System::Main && config_location.section == "NetPlay")
return true;

const static std::vector<Config::ConfigLocation> s_setting_saveable{
// Graphics.Hardware

Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Core/Core.vcxproj
Expand Up @@ -46,6 +46,7 @@
<ClCompile Include="Boot\DolReader.cpp" />
<ClCompile Include="Boot\ElfReader.cpp" />
<ClCompile Include="Config\GraphicsSettings.cpp" />
<ClCompile Include="Config\NetplaySettings.cpp" />
<ClCompile Include="ConfigLoaders\BaseConfigLoader.cpp" />
<ClCompile Include="ConfigLoaders\GameConfigLoader.cpp" />
<ClCompile Include="ConfigLoaders\IsSettingSaveable.cpp" />
Expand Down Expand Up @@ -301,6 +302,7 @@
<ClInclude Include="Boot\ElfReader.h" />
<ClInclude Include="Boot\ElfTypes.h" />
<ClInclude Include="Config\GraphicsSettings.h" />
<ClInclude Include="Config\NetplaySettings.h" />
<ClInclude Include="ConfigLoaders\BaseConfigLoader.h" />
<ClInclude Include="ConfigLoaders\GameConfigLoader.h" />
<ClInclude Include="ConfigLoaders\IsSettingSaveable.h" />
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/Core/Core.vcxproj.filters
Expand Up @@ -874,6 +874,9 @@
<ClCompile Include="Config\GraphicsSettings.cpp">
<Filter>Config</Filter>
</ClCompile>
<ClCompile Include="Config\NetplaySettings.cpp">
<Filter>Config</Filter>
</ClCompile>
<ClCompile Include="IOS\Network\NCD\WiiNetConfig.cpp">
<Filter>IOS\Network\NCD</Filter>
</ClCompile>
Expand Down Expand Up @@ -1523,6 +1526,9 @@
<ClInclude Include="Config\GraphicsSettings.h">
<Filter>Config</Filter>
</ClInclude>
<ClInclude Include="Config\NetplaySettings.h">
<Filter>Config</Filter>
</ClInclude>
<ClInclude Include="IOS\Network\NCD\WiiNetConfig.h">
<Filter>IOS\Network\NCD</Filter>
</ClInclude>
Expand Down
14 changes: 4 additions & 10 deletions Source/Core/DolphinWX/GameListCtrl.cpp
Expand Up @@ -46,6 +46,7 @@
#include "Common/SysConf.h"
#include "Common/Thread.h"
#include "Core/Boot/Boot.h"
#include "Core/Config/NetplaySettings.h"
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/DVD/DVDInterface.h"
Expand Down Expand Up @@ -1334,20 +1335,13 @@ void GameListCtrl::OnNetPlayHost(wxCommandEvent& WXUNUSED(event))
if (!iso)
return;

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");

NetPlayHostConfig config;
config.FromIniConfig(netplay_section);
config.FromConfig();
config.game_name = iso->GetUniqueIdentifier();
config.game_list_ctrl = this;
config.SetDialogInfo(netplay_section, m_parent);

netplay_section.Set("SelectedHostGame", config.game_name);
ini_file.Save(dolphin_ini);
config.SetDialogInfo(m_parent);

Config::SetBaseOrCurrent(Config::NETPLAY_SELECTED_HOST_GAME, config.game_name);
NetPlayLauncher::Host(config);
}

Expand Down
63 changes: 14 additions & 49 deletions Source/Core/DolphinWX/NetPlay/NetPlayLauncher.cpp
Expand Up @@ -2,11 +2,12 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <wx/config.h>
#include <wx/gdicmn.h>

#include "Common/CommonTypes.h"
#include "Common/IniFile.h"
#include "Common/StringUtil.h"
#include "Core/Config/NetplaySettings.h"
#include "DolphinWX/NetPlay/NetPlayLauncher.h"
#include "DolphinWX/NetPlay/NetWindow.h"
#include "DolphinWX/WxUtils.h"
Expand Down Expand Up @@ -91,44 +92,14 @@ bool NetPlayLauncher::Join(const NetPlayJoinConfig& config)
}
}

const std::string NetPlayLaunchConfig::DEFAULT_TRAVERSAL_HOST = "stun.dolphin-emu.org";

std::string
NetPlayLaunchConfig::GetTraversalHostFromIniConfig(const IniFile::Section& netplay_section)
{
std::string host;

netplay_section.Get("TraversalServer", &host, DEFAULT_TRAVERSAL_HOST);
host = StripSpaces(host);

if (host.empty())
return DEFAULT_TRAVERSAL_HOST;

return host;
}

u16 NetPlayLaunchConfig::GetTraversalPortFromIniConfig(const IniFile::Section& netplay_section)
{
std::string port_str;
unsigned long port;

netplay_section.Get("TraversalPort", &port_str, std::to_string(DEFAULT_TRAVERSAL_PORT));
StrToWxStr(port_str).ToULong(&port);

if (port == 0)
port = DEFAULT_TRAVERSAL_PORT;

return static_cast<u16>(port);
}

void NetPlayLaunchConfig::SetDialogInfo(const IniFile::Section& section, wxWindow* parent)
void NetPlayLaunchConfig::SetDialogInfo(wxWindow* parent)
{
parent_window = parent;

section.Get("NetWindowPosX", &window_pos.x, window_defaults.GetX());
section.Get("NetWindowPosY", &window_pos.y, window_defaults.GetY());
section.Get("NetWindowWidth", &window_pos.width, window_defaults.GetWidth());
section.Get("NetWindowHeight", &window_pos.height, window_defaults.GetHeight());
wxConfig::Get()->Read("NetWindowPosX", &window_pos.x, window_defaults.GetX());
wxConfig::Get()->Read("NetWindowPosY", &window_pos.y, window_defaults.GetY());
wxConfig::Get()->Read("NetWindowWidth", &window_pos.width, window_defaults.GetWidth());
wxConfig::Get()->Read("NetWindowHeight", &window_pos.height, window_defaults.GetHeight());

if (window_pos.GetX() == window_defaults.GetX() || window_pos.GetY() == window_defaults.GetY())
{
Expand All @@ -137,26 +108,20 @@ void NetPlayLaunchConfig::SetDialogInfo(const IniFile::Section& section, wxWindo
}
}

void NetPlayHostConfig::FromIniConfig(IniFile::Section& netplay_section)
void NetPlayHostConfig::FromConfig()
{
netplay_section.Get("Nickname", &player_name, "Player");
player_name = Config::Get(Config::NETPLAY_NICKNAME);

std::string traversal_choice_setting;
netplay_section.Get("TraversalChoice", &traversal_choice_setting, "direct");
const std::string traversal_choice_setting = Config::Get(Config::NETPLAY_TRAVERSAL_CHOICE);
use_traversal = traversal_choice_setting == "traversal";

if (!use_traversal)
{
unsigned long lport = 0;
std::string port_setting;
netplay_section.Get("HostPort", &port_setting, std::to_string(DEFAULT_LISTEN_PORT));
StrToWxStr(port_setting).ToULong(&lport);

listen_port = static_cast<u16>(lport);
listen_port = Config::Get(Config::NETPLAY_HOST_PORT);
}
else
{
traversal_port = GetTraversalPortFromIniConfig(netplay_section);
traversal_host = GetTraversalHostFromIniConfig(netplay_section);
traversal_port = Config::Get(Config::NETPLAY_TRAVERSAL_PORT);
traversal_host = Config::Get(Config::NETPLAY_TRAVERSAL_SERVER);
}
}
}
11 changes: 2 additions & 9 deletions Source/Core/DolphinWX/NetPlay/NetPlayLauncher.h
Expand Up @@ -6,7 +6,6 @@

#include <string>
#include "Common/CommonTypes.h"
#include "Common/IniFile.h"

class GameListCtrl;
class wxRect;
Expand All @@ -15,12 +14,8 @@ class wxWindow;
class NetPlayLaunchConfig
{
public:
static std::string GetTraversalHostFromIniConfig(const IniFile::Section& netplay_section);
static u16 GetTraversalPortFromIniConfig(const IniFile::Section& netplay_section);
void SetDialogInfo(const IniFile::Section& section, wxWindow* parent);
void SetDialogInfo(wxWindow* parent);

static const std::string DEFAULT_TRAVERSAL_HOST;
static constexpr u16 DEFAULT_TRAVERSAL_PORT = 6262;
const wxRect window_defaults{wxDefaultCoord, wxDefaultCoord, 768, 768 - 128};

std::string player_name;
Expand All @@ -35,9 +30,7 @@ class NetPlayLaunchConfig
class NetPlayHostConfig : public NetPlayLaunchConfig
{
public:
void FromIniConfig(IniFile::Section& netplay_section);

static constexpr u16 DEFAULT_LISTEN_PORT = 2626;
void FromConfig();

std::string game_name;
u16 listen_port = 0;
Expand Down

0 comments on commit f274bbc

Please sign in to comment.