Merge pull request #7410 from Techjar/netplay-max-name-length

NetPlay: Limit nickname length
This commit is contained in:
Jordan Woyak 2020-07-16 18:15:19 -05:00 committed by GitHub
commit 01178e4fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 71 additions and 7 deletions

View File

@ -412,6 +412,12 @@ void StringPopBackIf(std::string* s, char c)
s->pop_back();
}
size_t StringUTF8CodePointCount(const std::string& str)
{
return str.size() -
std::count_if(str.begin(), str.end(), [](char c) -> bool { return (c & 0xC0) == 0x80; });
}
#ifdef _WIN32
std::wstring CPToUTF16(u32 code_page, std::string_view input)

View File

@ -167,6 +167,7 @@ void BuildCompleteFilename(std::string& complete_filename, std::string_view path
bool StringBeginsWith(std::string_view str, std::string_view begin);
bool StringEndsWith(std::string_view str, std::string_view end);
void StringPopBackIf(std::string* s, char c);
size_t StringUTF8CodePointCount(const std::string& str);
std::string CP1252ToUTF8(std::string_view str);
std::string SHIFTJISToUTF8(std::string_view str);

View File

@ -252,6 +252,9 @@ bool NetPlayClient::Connect()
case CON_ERR_GAME_RUNNING:
m_dialog->OnConnectionError(_trans("The game is currently running."));
break;
case CON_ERR_NAME_TOO_LONG:
m_dialog->OnConnectionError(_trans("Nickname is too long."));
break;
default:
m_dialog->OnConnectionError(_trans("The server sent an unknown error message."));
break;

View File

@ -171,7 +171,8 @@ enum
{
CON_ERR_SERVER_FULL = 1,
CON_ERR_GAME_RUNNING = 2,
CON_ERR_VERSION_MISMATCH = 3
CON_ERR_VERSION_MISMATCH = 3,
CON_ERR_NAME_TOO_LONG = 4
};
enum
@ -197,6 +198,7 @@ enum
constexpr u32 NETPLAY_LZO_IN_LEN = 1024 * 64;
constexpr u32 NETPLAY_LZO_OUT_LEN = NETPLAY_LZO_IN_LEN + (NETPLAY_LZO_IN_LEN / 16) + 64 + 3;
constexpr u32 MAX_NAME_LENGTH = 30;
constexpr size_t CHUNKED_DATA_UNIT_SIZE = 16384;
constexpr u8 CHANNEL_COUNT = 2;
constexpr u8 DEFAULT_CHANNEL = 0;

View File

@ -377,9 +377,6 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket, sf::Packet& rpac)
if (m_players.size() >= 255)
return CON_ERR_SERVER_FULL;
// cause pings to be updated
m_update_pings = true;
Client player;
player.pid = pid;
player.socket = socket;
@ -387,6 +384,12 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket, sf::Packet& rpac)
rpac >> player.revision;
rpac >> player.name;
if (StringUTF8CodePointCount(player.name) > MAX_NAME_LENGTH)
return CON_ERR_NAME_TOO_LONG;
// cause pings to be updated
m_update_pings = true;
// try to automatically assign new user a pad
for (PlayerId& mapping : m_pad_map)
{

View File

@ -235,6 +235,8 @@ add_executable(dolphin-emu
QtUtils/ParallelProgressDialog.h
QtUtils/ImageConverter.cpp
QtUtils/ImageConverter.h
QtUtils/UTF8CodePointCountValidator.cpp
QtUtils/UTF8CodePointCountValidator.h
QtUtils/WindowActivationEventFilter.cpp
QtUtils/WindowActivationEventFilter.h
QtUtils/WinIconHelper.cpp

View File

@ -186,6 +186,7 @@
<QtMoc Include="QtUtils\FlowLayout.h" />
<QtMoc Include="QtUtils\ModalMessageBox.h" />
<QtMoc Include="QtUtils\ParallelProgressDialog.h" />
<QtMoc Include="QtUtils\UTF8CodePointCountValidator.h" />
<QtMoc Include="QtUtils\WindowActivationEventFilter.h" />
<QtMoc Include="QtUtils\WrapInScrollArea.h" />
<QtMoc Include="RenderWidget.h" />
@ -291,6 +292,7 @@
<ClCompile Include="$(QtMocOutPrefix)NewPatchDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)PadMappingDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ParallelProgressDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)UTF8CodePointCountValidator.cpp" />
<ClCompile Include="$(QtMocOutPrefix)PatchesWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)PatchInstructionDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)PathPane.cpp" />
@ -433,6 +435,7 @@
<ClCompile Include="QtUtils\FlowLayout.cpp" />
<ClCompile Include="QtUtils\ImageConverter.cpp" />
<ClCompile Include="QtUtils\ModalMessageBox.cpp" />
<ClCompile Include="QtUtils\UTF8CodePointCountValidator.cpp" />
<ClCompile Include="QtUtils\WindowActivationEventFilter.cpp" />
<ClCompile Include="QtUtils\WrapInScrollArea.cpp" />
<ClCompile Include="RenderWidget.cpp" />

View File

@ -17,9 +17,11 @@
#include <QTabWidget>
#include "Core/Config/NetplaySettings.h"
#include "Core/NetPlayProto.h"
#include "DolphinQt/GameList/GameListModel.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"
#include "DolphinQt/QtUtils/UTF8CodePointCountValidator.h"
#include "DolphinQt/Settings.h"
#include "UICommon/NetPlayIndex.h"
@ -87,6 +89,9 @@ void NetPlaySetupDialog::CreateMainLayout()
m_reset_traversal_button = new QPushButton(tr("Reset Traversal Settings"));
m_tab_widget = new QTabWidget;
m_nickname_edit->setValidator(
new UTF8CodePointCountValidator(NetPlay::MAX_NAME_LENGTH, m_nickname_edit));
// Connection widget
auto* connection_widget = new QWidget;
auto* connection_layout = new QGridLayout;

View File

@ -0,0 +1,20 @@
// Copyright 2020 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt/QtUtils/UTF8CodePointCountValidator.h"
#include "Common/StringUtil.h"
UTF8CodePointCountValidator::UTF8CodePointCountValidator(int max_count, QObject* parent)
: QValidator(parent), m_max_count(max_count)
{
}
QValidator::State UTF8CodePointCountValidator::validate(QString& input, int& pos) const
{
if (StringUTF8CodePointCount(input.toStdString()) > m_max_count)
return QValidator::Invalid;
return QValidator::Acceptable;
}

View File

@ -0,0 +1,19 @@
// Copyright 2020 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <QString>
#include <QValidator>
class UTF8CodePointCountValidator : public QValidator
{
Q_OBJECT
public:
explicit UTF8CodePointCountValidator(int max_count, QObject* parent = nullptr);
QValidator::State validate(QString& input, int& pos) const override;
int m_max_count;
};