Skip to content

Commit

Permalink
DolphinQt / InputCommon - add DSU string validator to avoid crashes, …
Browse files Browse the repository at this point in the history
…limited backwards compatibility support
  • Loading branch information
iwubcode committed Jul 13, 2020
1 parent 5a39622 commit e9e451c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Source/Core/DolphinQt/CMakeLists.txt
Expand Up @@ -63,6 +63,8 @@ add_executable(dolphin-emu
Config/ControllerInterface/DualShockUDPClientWidget.h
Config/ControllerInterface/ControllerInterfaceWindow.cpp
Config/ControllerInterface/ControllerInterfaceWindow.h
Config/ControllerInterface/ServerStringValidator.cpp
Config/ControllerInterface/ServerStringValidator.h
Config/ControllersWindow.cpp
Config/ControllersWindow.h
Config/FilesystemWidget.cpp
Expand Down
Expand Up @@ -14,9 +14,11 @@
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QString>
#include <QWidget>

#include "Common/Config/Config.h"
#include "DolphinQt/Config/ControllerInterface/ServerStringValidator.h"
#include "InputCommon/ControllerInterface/DualShockUDPClient/DualShockUDPClient.h"

DualShockUDPClientAddServerDialog::DualShockUDPClientAddServerDialog(QWidget* parent)
Expand All @@ -35,9 +37,11 @@ void DualShockUDPClientAddServerDialog::CreateWidgets()

m_description = new QLineEdit();
m_description->setPlaceholderText(tr("BetterJoy, DS4Windows, etc"));
m_description->setValidator(new ServerStringValidator(m_description));

m_server_address =
new QLineEdit(QString::fromStdString(ciface::DualShockUDPClient::DEFAULT_SERVER_ADDRESS));
m_server_address->setValidator(new ServerStringValidator(m_server_address));

m_server_port = new QSpinBox();
m_server_port->setMaximum(65535);
Expand Down
Expand Up @@ -75,6 +75,22 @@ void DualShockUDPClientWidget::RefreshServerList()
{
m_server_list->clear();

const auto server_address_setting =
Config::Get(ciface::DualShockUDPClient::Settings::SERVER_ADDRESS);
const auto server_port_setting = Config::Get(ciface::DualShockUDPClient::Settings::SERVER_PORT);

// Update our servers setting if the user is using old configuration
if (!server_address_setting.empty() && server_port_setting != 0)
{
const auto& servers_setting = Config::Get(ciface::DualShockUDPClient::Settings::SERVERS);
Config::SetBaseOrCurrent(ciface::DualShockUDPClient::Settings::SERVERS,
servers_setting + fmt::format("{}:{}:{};", "DS4",
server_address_setting,
server_port_setting));
Config::SetBase(ciface::DualShockUDPClient::Settings::SERVER_ADDRESS, "");
Config::SetBase(ciface::DualShockUDPClient::Settings::SERVER_PORT, 0);
}

const auto& servers_setting = Config::Get(ciface::DualShockUDPClient::Settings::SERVERS);
const auto server_details = SplitString(servers_setting, ';');
for (const std::string& server_detail : server_details)
Expand Down
@@ -0,0 +1,23 @@
// Copyright 2020 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include "DolphinQt/Config/ControllerInterface/ServerStringValidator.h"

ServerStringValidator::ServerStringValidator(QObject* parent) : QValidator(parent)
{
}

QValidator::State ServerStringValidator::validate(QString& input, int& pos) const
{
if (input.isEmpty())
return Invalid;

if (input.contains(QStringLiteral(":")))
return Invalid;

if (input.contains(QStringLiteral(";")))
return Invalid;

return Acceptable;
}
@@ -0,0 +1,16 @@
// Copyright 2020 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <QValidator>

class ServerStringValidator : public QValidator
{
Q_OBJECT
public:
explicit ServerStringValidator(QObject* parent);

State validate(QString& input, int& pos) const override;
};
3 changes: 3 additions & 0 deletions Source/Core/DolphinQt/DolphinQt.vcxproj
Expand Up @@ -127,6 +127,7 @@
<QtMoc Include="Config\ControllerInterface\DualShockUDPClientAddServerDialog.h" />
<QtMoc Include="Config\ControllerInterface\DualShockUDPClientWidget.h" />
<QtMoc Include="Config\ControllerInterface\ControllerInterfaceWindow.h" />
<QtMoc Include="Config\ControllerInterface\ServerStringValidator.h" />
<QtMoc Include="Config\InfoWidget.h" />
<QtMoc Include="Config\PatchesWidget.h" />
<QtMoc Include="Config\PropertiesDialog.h" />
Expand Down Expand Up @@ -216,6 +217,7 @@
<ClCompile Include="$(QtMocOutPrefix)DualShockUDPClientAddServerDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)DualShockUDPClientWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ControllerInterfaceWindow.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ServerStringValidator.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ControllersWindow.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ConvertDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)DiscordHandler.cpp" />
Expand Down Expand Up @@ -330,6 +332,7 @@
<ClCompile Include="Config\ControllerInterface\DualShockUDPClientAddServerDialog.cpp" />
<ClCompile Include="Config\ControllerInterface\DualShockUDPClientWidget.cpp" />
<ClCompile Include="Config\ControllerInterface\ControllerInterfaceWindow.cpp" />
<ClCompile Include="Config\ControllerInterface\ServerStringValidator.cpp" />
<ClCompile Include="Config\ControllersWindow.cpp" />
<ClCompile Include="Config\FilesystemWidget.cpp" />
<ClCompile Include="Config\GameConfigEdit.cpp" />
Expand Down
Expand Up @@ -30,6 +30,9 @@ namespace ciface::DualShockUDPClient
{
namespace Settings
{
const Config::Info<std::string> SERVER_ADDRESS{
{Config::System::DualShockUDPClient, "Server", "IPAddress"}, ""};
const Config::Info<int> SERVER_PORT{{Config::System::DualShockUDPClient, "Server", "Port"}, 0};
const Config::Info<std::string> SERVERS{{Config::System::DualShockUDPClient, "Server", "Entries"},
""};
const Config::Info<bool> SERVERS_ENABLED{{Config::System::DualShockUDPClient, "Server", "Enabled"},
Expand Down Expand Up @@ -361,6 +364,21 @@ static void ConfigChanged()

void Init()
{
// The following is added for backwards compatibility
const auto server_address_setting = Config::Get(Settings::SERVER_ADDRESS);
const auto server_port_setting = Config::Get(Settings::SERVER_PORT);

if (!server_address_setting.empty() && server_port_setting != 0)
{
const auto& servers_setting = Config::Get(ciface::DualShockUDPClient::Settings::SERVERS);
Config::SetBaseOrCurrent(ciface::DualShockUDPClient::Settings::SERVERS,
servers_setting + fmt::format("{}:{}:{};", "DS4",
server_address_setting,
server_port_setting));
Config::SetBase(Settings::SERVER_ADDRESS, "");
Config::SetBase(Settings::SERVER_PORT, 0);
}

Config::AddConfigChangedCallback(ConfigChanged);
}

Expand Down
Expand Up @@ -13,6 +13,10 @@ constexpr u16 DEFAULT_SERVER_PORT = 26760;

namespace Settings
{
// These two kept for backwards compatibility
extern const Config::Info<std::string> SERVER_ADDRESS;
extern const Config::Info<int> SERVER_PORT;

extern const Config::Info<std::string> SERVERS;
extern const Config::Info<bool> SERVERS_ENABLED;
} // namespace Settings
Expand Down

0 comments on commit e9e451c

Please sign in to comment.