Skip to content

Commit

Permalink
REFAC(client): Replace <codecvt> usage
Browse files Browse the repository at this point in the history
Deprecated in C++17 and slated for removal in C++26.

Unfortunately no standard replacement is available yet.
  • Loading branch information
davidebeatrici committed Jul 26, 2024
1 parent cbb6f6e commit ffddb3c
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
[submodule "3rdparty/flag-icons"]
path = 3rdparty/flag-icons
url = https://github.com/lipis/flag-icons.git
[submodule "3rdparty/utfcpp"]
path = 3rdparty/utfcpp
url = https://github.com/mumble-voip/utfcpp.git
1 change: 1 addition & 0 deletions 3rdparty/utfcpp
Submodule utfcpp added at e717ac
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ set(CMAKE_UNITY_BUILD_BATCH_SIZE 40)
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ${lto})
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_DEBUG OFF)

add_subdirectory(${3RDPARTY_DIR}/utfcpp)

if(client OR server)
add_subdirectory(src)
endif()
Expand Down
2 changes: 1 addition & 1 deletion plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ foreach(CURRENT_PLUGIN IN LISTS AVAILABLE_PLUGINS)

if(WIN32)
target_compile_definitions(${CURRENT_PLUGIN} PRIVATE "OS_WINDOWS")
target_link_libraries(${CURRENT_PLUGIN} user32.lib)
target_link_libraries(${CURRENT_PLUGIN} PRIVATE user32.lib)

# Shared library on Windows (e.g. ".dll")
set_target_properties(${CURRENT_PLUGIN} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/plugins")
Expand Down
26 changes: 22 additions & 4 deletions plugins/HostWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include "HostWindows.h"

#include "mumble_positional_audio_utils.h"

#include <windows.h>
#include <tlhelp32.h>

Expand Down Expand Up @@ -42,7 +40,7 @@ Modules HostWindows::modules() const {
MODULEENTRY32 me;
me.dwSize = sizeof(me);
for (auto ok = Module32First(snapshotHandle, &me); ok; ok = Module32Next(snapshotHandle, &me)) {
const auto name = utf16ToUtf8(reinterpret_cast< char16_t * >(me.szModule));
std::string name = utf16To8(me.szModule);
if (modules.find(name) != modules.cend()) {
continue;
}
Expand Down Expand Up @@ -76,11 +74,31 @@ Modules HostWindows::modules() const {
address += region.size;
}

modules.insert(std::make_pair(name, module));
modules.insert({ std::move(name), std::move(module) });
}

CloseHandle(processHandle);
CloseHandle(snapshotHandle);

return modules;
}

std::string HostWindows::utf16To8(const std::wstring &wstr) {
if (wstr.empty()) {
return {};
}

const int length =
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast< int >(wstr.length()), nullptr, 0, nullptr, nullptr);
if (!length) {
return {};
}

std::string str(length, '\0');
if (!WideCharToMultiByte(CP_UTF8, 0, &wstr[0], static_cast< int >(wstr.length()), &str[0], length, nullptr,
nullptr)) {
return {};
}

return str;
}
2 changes: 2 additions & 0 deletions plugins/HostWindows.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ using procid_t = uint64_t;

class HostWindows {
protected:
static std::string utf16To8(const std::wstring &wstr);

procid_t m_pid;
void *m_handle;

Expand Down
13 changes: 0 additions & 13 deletions plugins/ProcessBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,3 @@ procptr_t ProcessBase::findPattern(const std::vector< uint8_t > &pattern, procpt

return 0;
}

procid_t ProcessBase::find(const std::string &name, const std::multimap< std::wstring, unsigned long long int > &pids) {
if (pids.empty()) {
return 0;
}

const auto iter = pids.find(utf8ToUtf16(name));
if (iter == pids.cend()) {
return 0;
}

return iter->second;
}
2 changes: 0 additions & 2 deletions plugins/ProcessBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ class ProcessBase : public Host {
/// Returns 0 if the pattern is not found.
procptr_t findPattern(const std::vector< uint8_t > &pattern, procptr_t address, const size_t size);

static procid_t find(const std::string &name, const std::multimap< std::wstring, unsigned long long int > &pids);

ProcessBase(const procid_t id, const std::string &name);
virtual ~ProcessBase();
};
Expand Down
2 changes: 2 additions & 0 deletions plugins/amongus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ if(WIN32)
else()
target_sources(amongus PRIVATE "../HostLinux.cpp")
endif()

target_link_libraries(amongus PRIVATE utfcpp)
14 changes: 10 additions & 4 deletions plugins/amongus/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#include "Game.h"

#include "mumble_positional_audio_utils.h"
#include <sstream>

#include <utf8/cpp11.h>

Game::Game(const procid_t id, const std::string name) : m_proc(id, name) {
}
Expand Down Expand Up @@ -91,11 +93,15 @@ std::string Game::string(const procptr_t address) {

std::u16string string;
string.resize(object.fields.length);
if (m_proc.peek(address + sizeof(object), &string[0], sizeof(char16_t) * string.size())) {
return utf16ToUtf8(string.data());
if (!m_proc.peek(address + sizeof(object), &string[0], sizeof(char16_t) * string.size())) {
return {};
}

return {};
try {
return utf8::utf16to8(string);
} catch (const utf8::invalid_utf16 &) {
return {};
}
}

const std::string &Game::context(const AmongUsClient_Fields &fields) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/cod2/cod2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ static int fetch(float *avatarPos, float *avatarFront, float *avatarTop, float *
static int tryLock(const std::multimap< std::wstring, unsigned long long int > &pids) {
const std::string name = "CoD2MP_s.exe";

const auto id = ProcessBase::find(name, pids);
if (!id) {
const auto iter = pids.find(std::wstring(name.cbegin(), name.cend()));
if (iter == pids.cend()) {
return false;
}

process = std::make_unique< ProcessWindows >(id, name);
process = std::make_unique< ProcessWindows >(iter->second, name);
if (!process->isOk()) {
process.reset();
return false;
Expand Down
2 changes: 2 additions & 0 deletions plugins/link/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ target_include_directories(link PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(link_tester PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

set_target_properties(link_tester PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")

target_link_libraries(link PRIVATE utfcpp)
33 changes: 28 additions & 5 deletions plugins/link/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include <chrono>
#include <codecvt>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <locale>
#include <string>

#include "LinkedMem.h"
#include "MumblePlugin.h"
#include "SharedMemory.h"

#include <utf8/cpp11.h>

#define UNUSED(x) (void) x


Expand All @@ -39,6 +39,29 @@ static std::uint64_t getTimeSinceEpoch() {
return std::chrono::system_clock::now().time_since_epoch() / std::chrono::milliseconds(1);
}

static std::string wideToUtf8(const wchar_t *buf) {
switch (sizeof(wchar_t)) {
case 2: {
try {
const std::u16string str16(reinterpret_cast< const char16_t * >(buf));
return utf8::utf16to8(str16);
} catch (const utf8::invalid_utf16 &) {
return {};
}
}
case 4: {
try {
const std::u32string str32(reinterpret_cast< const char32_t * >(buf));
return utf8::utf32to8(str32);
} catch (const utf8::invalid_code_point &) {
return {};
}
}
}

return {};
}

mumble_error_t mumble_init(mumble_plugin_id_t id) {
UNUSED(id);

Expand Down Expand Up @@ -128,7 +151,7 @@ uint8_t mumble_initPositionalData(const char *const *programNames, const uint64_
if (lm.name[0]) {
wcsncpy(buff, lm.name, 256);
buff[255] = 0;
applicationName = std::wstring_convert< std::codecvt_utf8< wchar_t > >().to_bytes(buff);
applicationName = wideToUtf8(buff);

// Call the plugin itself "Link (<whatever>)"
pluginName += " (" + applicationName + ")";
Expand All @@ -137,7 +160,7 @@ uint8_t mumble_initPositionalData(const char *const *programNames, const uint64_
if (lm.description[0]) {
wcsncpy(buff, lm.description, 2048);
buff[2047] = 0;
pluginDescription = std::wstring_convert< std::codecvt_utf8< wchar_t > >().to_bytes(buff);
pluginDescription = wideToUtf8(buff);
}

return MUMBLE_PDEC_OK;
Expand Down Expand Up @@ -192,7 +215,7 @@ bool mumble_fetchPositionalData(float *avatarPos, float *avatarDir, float *avata
lm.identity[255] = 0;

pluginContext.assign(reinterpret_cast< const char * >(lm.context), lm.context_len);
pluginIdentity = std::wstring_convert< std::codecvt_utf8< wchar_t > >().to_bytes(lm.identity);
pluginIdentity = wideToUtf8(lm.identity);
} else {
for (int i = 0; i < 3; ++i) {
cameraPos[i] = lm.fAvatarPosition[i];
Expand Down
34 changes: 0 additions & 34 deletions plugins/mumble_positional_audio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
#define MUMBLE_POSITIONAL_AUDIO_UTILS_H_

#include <cmath>
#include <codecvt>
#include <fstream>
#include <locale>
#include <sstream>
#include <vector>

Expand All @@ -25,38 +23,6 @@ union SingleSplit4Bytes {
constexpr SingleSplit4Bytes(const uint32_t value) : single(value) {}
};

/// Converts from UTF-8 to UTF-16.
static inline std::wstring utf8ToUtf16(const std::string &str) {
try {
std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > conv;
return conv.from_bytes(str);
} catch (std::range_error &) {
return {};
}
}

/// Converts from UTF-16 to UTF-8.
/// Meant to be used when "wchar_t" is 2 bytes, usually with Windows processes.
static inline std::string utf16ToUtf8(const std::u16string &str) {
try {
std::wstring_convert< std::codecvt_utf8_utf16< char16_t >, char16_t > conv;
return conv.to_bytes(str);
} catch (std::range_error &) {
return {};
}
}

/// Converts from UTF-16 to UTF-8.
/// Meant to be used when "wchar_t" is 4 bytes, usually with Linux processes.
static inline std::string utf16ToUtf8(const std::u32string &str) {
try {
std::wstring_convert< std::codecvt_utf8_utf16< char32_t >, char32_t > conv;
return conv.to_bytes(str);
} catch (std::range_error &) {
return {};
}
}

// escape lossily converts the given
// string to ASCII, replacing any
// character not within the printable
Expand Down
6 changes: 4 additions & 2 deletions plugins/se/se.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ static bool tryInit(const std::multimap< std::wstring, unsigned long long int >
};

for (const auto &name : names) {
const auto id = ProcessBase::find(name, pids);
if (!id) {
const auto iter = pids.find(std::wstring(name.cbegin(), name.cend()));
if (iter == pids.cend()) {
continue;
}

const auto id = iter->second;
#ifdef OS_WINDOWS
proc.reset(new ProcessWindows(id, name));
#else
Expand Down
2 changes: 2 additions & 0 deletions plugins/ut99/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
# Mumble source tree or at <https://www.mumble.info/LICENSE>.

add_library(ut99 SHARED "ut99.cpp")

target_link_libraries(ut99 PRIVATE utfcpp)
13 changes: 9 additions & 4 deletions plugins/ut99/ut99.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
#include "mumble_legacy_plugin.h"

#include "mumble_positional_audio_main.h"
#include "mumble_positional_audio_utils.h"

#include <utf8/cpp11.h>

procptr_t posptr;
procptr_t frtptr;
Expand Down Expand Up @@ -153,9 +154,13 @@ static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, floa
}

std::ostringstream contextss;
contextss << "{"
<< "\"servername\":\"" << utf16ToUtf8(servername) << "\""
<< "}";
contextss << "{";
try {
contextss << "\"servername\":\"" << utf8::utf16to8(servername) << "\"";
} catch (const utf8::invalid_utf16 &) {
contextss << "\"servername\":\" null";
}
contextss << "}";

context = contextss.str();

Expand Down
1 change: 1 addition & 0 deletions src/mumble/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ endif()
target_link_libraries(mumble_client_object_lib
PUBLIC
shared
utfcpp
Qt5::Concurrent
Qt5::Sql
Qt5::Svg
Expand Down
Loading

0 comments on commit ffddb3c

Please sign in to comment.