Skip to content

Commit

Permalink
Merge pull request #7410 from Techjar/netplay-max-name-length
Browse files Browse the repository at this point in the history
NetPlay: Limit nickname length
  • Loading branch information
jordan-woyak committed Jul 16, 2020
2 parents c596483 + 037aa21 commit 01178e4
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Source/Core/Common/StringUtil.cpp
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/StringUtil.h
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/Core/NetPlayClient.cpp
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Core/NetPlayProto.h
Expand Up @@ -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
Expand All @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions Source/Core/Core/NetPlayServer.cpp
Expand Up @@ -377,16 +377,19 @@ 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;

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)
{
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/DolphinQt/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -459,8 +461,8 @@ if(APPLE)
include(DolphinPostprocessBundle)
dolphin_postprocess_bundle(dolphin-emu)
# Fix rpath
add_custom_command(TARGET dolphin-emu
POST_BUILD COMMAND
add_custom_command(TARGET dolphin-emu
POST_BUILD COMMAND
${CMAKE_INSTALL_NAME_TOOL} -add_rpath "@executable_path/../Frameworks/"
$<TARGET_FILE:dolphin-emu>)
else()
Expand Down
5 changes: 4 additions & 1 deletion Source/Core/DolphinQt/DolphinQt.vcxproj
Expand Up @@ -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" />
Expand Down Expand Up @@ -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" />
Expand Down Expand Up @@ -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" />
Expand Down Expand Up @@ -555,4 +558,4 @@
<Message Text="Copy: @(BinaryFiles) -&gt; $(BinaryOutputDir)" Importance="High" />
<Copy SourceFiles="@(BinaryFiles)" DestinationFolder="$(BinaryOutputDir)" />
</Target>
</Project>
</Project>
5 changes: 5 additions & 0 deletions Source/Core/DolphinQt/NetPlay/NetPlaySetupDialog.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions Source/Core/DolphinQt/QtUtils/UTF8CodePointCountValidator.cpp
@@ -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;
}
19 changes: 19 additions & 0 deletions Source/Core/DolphinQt/QtUtils/UTF8CodePointCountValidator.h
@@ -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;
};

0 comments on commit 01178e4

Please sign in to comment.