From b7df075dec82eb4e226fe15c82fd03560bbfd0fb Mon Sep 17 00:00:00 2001 From: Rosalie Wanders Date: Thu, 8 Dec 2022 13:42:53 +0100 Subject: [PATCH] RMG-Core: introduce & use ConvertStringEncoding --- Source/RMG-Core/CMakeLists.txt | 6 +- Source/RMG-Core/Callback.cpp | 15 ++++- Source/RMG-Core/ConvertStringEncoding.cpp | 78 +++++++++++++++++++++++ Source/RMG-Core/ConvertStringEncoding.hpp | 27 ++++++++ Source/RMG-Core/RomHeader.cpp | 5 +- Source/RMG-Core/RomSettings.cpp | 5 +- 6 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 Source/RMG-Core/ConvertStringEncoding.cpp create mode 100644 Source/RMG-Core/ConvertStringEncoding.hpp diff --git a/Source/RMG-Core/CMakeLists.txt b/Source/RMG-Core/CMakeLists.txt index 5af60ab1..91718731 100644 --- a/Source/RMG-Core/CMakeLists.txt +++ b/Source/RMG-Core/CMakeLists.txt @@ -9,6 +9,9 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) find_package(PkgConfig REQUIRED) pkg_check_modules(SDL2 REQUIRED sdl2) pkg_check_modules(MINIZIP REQUIRED minizip) +if (WIN32) + pkg_check_modules(ICONV REQUIRED iconv) +endif(WIN32) configure_file(Config.hpp.in Config.hpp) @@ -18,6 +21,7 @@ set(RMG_CORE_SOURCES m64p/ConfigApi.cpp m64p/PluginApi.cpp CachedRomHeaderAndSettings.cpp + ConvertStringEncoding.cpp Settings/Settings.cpp SpeedLimiter.cpp RomSettings.cpp @@ -69,7 +73,7 @@ if(UNIX) endif(UNIX) if(WIN32) - target_link_libraries(RMG-Core wsock32 ws2_32) + target_link_libraries(RMG-Core wsock32 ws2_32 ${ICONV_LIBRARIES}) endif(WIN32) target_link_libraries(RMG-Core diff --git a/Source/RMG-Core/Callback.cpp b/Source/RMG-Core/Callback.cpp index 41b2f8e6..147c6e7f 100644 --- a/Source/RMG-Core/Callback.cpp +++ b/Source/RMG-Core/Callback.cpp @@ -9,6 +9,7 @@ */ #define CORE_INTERNAL #include "Core.hpp" +#include "ConvertStringEncoding.hpp" // // Local Variables @@ -28,7 +29,19 @@ void CoreDebugCallback(void* context, int level, const char* message) return; } - l_DebugCallbackFunc((CoreDebugMessageType)level, std::string(message)); + std::string messageString(message); + + // convert string encoding accordingly + if (messageString.starts_with("IS64:")) + { + messageString = CoreConvertStringEncoding(message, CoreStringEncoding::EUC_JP); + } + else + { + messageString = CoreConvertStringEncoding(message, CoreStringEncoding::Shift_JIS); + } + + l_DebugCallbackFunc((CoreDebugMessageType)level, messageString); } void CoreStateCallback(void* context, m64p_core_param param, int value) diff --git a/Source/RMG-Core/ConvertStringEncoding.cpp b/Source/RMG-Core/ConvertStringEncoding.cpp new file mode 100644 index 00000000..02a27783 --- /dev/null +++ b/Source/RMG-Core/ConvertStringEncoding.cpp @@ -0,0 +1,78 @@ +/* + * Rosalie's Mupen GUI - https://github.com/Rosalie241/RMG + * Copyright (C) 2020 Rosalie Wanders + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#define CORE_INTERNAL +#include "ConvertStringEncoding.hpp" + +#include "Error.hpp" +#include +#include + +#include + +// +// Exported Functions +// + +std::string CoreConvertStringEncoding(std::string str, CoreStringEncoding encoding) +{ + std::string error; + std::string encodingString; + + iconv_t cd; + + char inputBuf[256] = {0}; + char outputBuf[256] = {0}; + + char* inputBufPtr = (char*)str.c_str(); + char* outputBufPtr = outputBuf; + + size_t inputBufSize = str.size(); + size_t outputBufSize = sizeof(outputBuf); + + if (encoding == CoreStringEncoding::EUC_JP) + { + encodingString = "EUC-JP"; + } + else + { + encodingString = "Shift_JIS"; + } + + cd = iconv_open("UTF-8//TRANSLIT//IGNORE", encodingString.c_str()); + if (cd == (iconv_t)-1) + { + error = "CoreConvertStringEncoding Failed: "; + error += "iconv_open Failed: "; + error += errno; + CoreSetError(error); + return str; + } + + size_t ret = iconv(cd, &inputBufPtr, &inputBufSize, &outputBufPtr, &outputBufSize); + if (ret == -1) + { + error = "CoreConvertStringEncoding Failed: "; + error += "iconv Failed: "; + error += errno; + CoreSetError(error); + + // reset iconv + iconv(cd, nullptr, nullptr, nullptr, nullptr); + iconv_close(cd); + + return str; + } + + // reset iconv + iconv(cd, nullptr, nullptr, nullptr, nullptr); + iconv_close(cd); + + return std::string(outputBuf); +} diff --git a/Source/RMG-Core/ConvertStringEncoding.hpp b/Source/RMG-Core/ConvertStringEncoding.hpp new file mode 100644 index 00000000..f6344da4 --- /dev/null +++ b/Source/RMG-Core/ConvertStringEncoding.hpp @@ -0,0 +1,27 @@ +/* + * Rosalie's Mupen GUI - https://github.com/Rosalie241/RMG + * Copyright (C) 2020 Rosalie Wanders + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef CORE_CONVERTSTRINGENCODING_HPP +#define CORE_CONVERTSTRINGENCODING_HPP +#ifdef CORE_INTERNAL + +#include + +enum class CoreStringEncoding : int +{ + EUC_JP = 0, + Shift_JIS = 1 +}; + +// attempts to convert the string (which uses encoding) to UTF-8, +// on failure, returns original string +std::string CoreConvertStringEncoding(std::string str, CoreStringEncoding encoding); + +#endif // CORE_INTERNAL +#endif // CORE_CONVERTSTRINGENCODING_HPP \ No newline at end of file diff --git a/Source/RMG-Core/RomHeader.cpp b/Source/RMG-Core/RomHeader.cpp index 9b278a37..7f86fbce 100644 --- a/Source/RMG-Core/RomHeader.cpp +++ b/Source/RMG-Core/RomHeader.cpp @@ -13,7 +13,9 @@ #include #endif // _WIN32 +#define CORE_INTERNAL #include "RomHeader.hpp" +#include "ConvertStringEncoding.hpp" #include "Emulation.hpp" #include "m64p/Api.hpp" #include "Error.hpp" @@ -46,6 +48,7 @@ bool CoreGetCurrentRomHeader(CoreRomHeader& header) header.CRC1 = ntohl(m64p_header.CRC1); header.CRC2 = ntohl(m64p_header.CRC2); header.CountryCode = m64p_header.Country_code; - header.Name = std::string((char*)m64p_header.Name); + header.Name = CoreConvertStringEncoding((char*)m64p_header.Name, CoreStringEncoding::Shift_JIS); + return true; } diff --git a/Source/RMG-Core/RomSettings.cpp b/Source/RMG-Core/RomSettings.cpp index 9113be84..5c4e9649 100644 --- a/Source/RMG-Core/RomSettings.cpp +++ b/Source/RMG-Core/RomSettings.cpp @@ -7,13 +7,16 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#define CORE_INTERNAL #include "RomSettings.hpp" #include "Emulation.hpp" +#include "RomHeader.hpp" #include "m64p/Api.hpp" #include "Error.hpp" #include "Rom.hpp" #include "Settings/Settings.hpp" +#include "ConvertStringEncoding.hpp" // // Local Variables @@ -46,7 +49,7 @@ bool CoreGetCurrentRomSettings(CoreRomSettings& settings) return false; } - settings.GoodName = std::string(m64p_settings.goodname); + settings.GoodName = CoreConvertStringEncoding(m64p_settings.goodname, CoreStringEncoding::Shift_JIS); settings.MD5 = std::string(m64p_settings.MD5); settings.SaveType = m64p_settings.savetype; settings.DisableExtraMem = m64p_settings.disableextramem;