From 015424eb35cdb7b25abd07cdb4ee8ef77b47306e Mon Sep 17 00:00:00 2001 From: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> Date: Fri, 30 Jul 2021 11:51:21 -0700 Subject: [PATCH] Conversion to unicode, everything except StreamerConfiguration Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> --- Code/Framework/AzCore/AzCore/Debug/Trace.cpp | 1 - Code/Framework/AzCore/AzCore/Debug/Trace.h | 5 ++ .../AzCore/AzCore/std/string/conversions.h | 82 +++++++++++++++++++ .../Common/Apple/AzCore/Debug/Trace_Apple.cpp | 1 + .../WinAPI/AzCore/Debug/Trace_WinAPI.cpp | 19 +++-- .../Linux/AzCore/Debug/Trace_Linux.cpp | 4 +- .../Mac/AzCore/IPC/SharedMemory_Mac.cpp | 2 +- .../Mac/AzCore/IPC/SharedMemory_Mac.h | 1 - .../StreamerConfiguration_Windows.cpp | 4 +- .../AzCore/IPC/SharedMemory_Windows.cpp | 32 ++++---- .../Windows/AzCore/IPC/SharedMemory_Windows.h | 3 - 11 files changed, 121 insertions(+), 33 deletions(-) diff --git a/Code/Framework/AzCore/AzCore/Debug/Trace.cpp b/Code/Framework/AzCore/AzCore/Debug/Trace.cpp index 6d491c97b8..bd3b12a3b8 100644 --- a/Code/Framework/AzCore/AzCore/Debug/Trace.cpp +++ b/Code/Framework/AzCore/AzCore/Debug/Trace.cpp @@ -38,7 +38,6 @@ namespace AZ void DebugBreak(); #endif void Terminate(int exitCode); - void OutputToDebugger(const char* window, const char* message); } } diff --git a/Code/Framework/AzCore/AzCore/Debug/Trace.h b/Code/Framework/AzCore/AzCore/Debug/Trace.h index a680cbdfed..bae074281c 100644 --- a/Code/Framework/AzCore/AzCore/Debug/Trace.h +++ b/Code/Framework/AzCore/AzCore/Debug/Trace.h @@ -15,6 +15,11 @@ namespace AZ { namespace Debug { + namespace Platform + { + void OutputToDebugger(const char* window, const char* message); + } + /// Global instance to the tracer. extern class Trace g_tracer; diff --git a/Code/Framework/AzCore/AzCore/std/string/conversions.h b/Code/Framework/AzCore/AzCore/std/string/conversions.h index 3e5a393cf8..4dc5bbd547 100644 --- a/Code/Framework/AzCore/AzCore/std/string/conversions.h +++ b/Code/Framework/AzCore/AzCore/std/string/conversions.h @@ -49,6 +49,25 @@ namespace AZStd } } + template + static inline void to_string(AZStd::basic_fixed_string& dest, const wchar_t* first, const wchar_t* last) + { + if constexpr (Size == 2) + { + Utf8::Unchecked::utf16to8(first, last, AZStd::back_inserter(dest)); + } + else if constexpr (Size == 4) + { + Utf8::Unchecked::utf32to8(first, last, AZStd::back_inserter(dest)); + } + else + { + // Workaround to defer static_assert evaluation until this function is invoked by using the template parameter + using StringType = AZStd::basic_string; + static_assert(!AZStd::is_same_v, "only wchar_t types of size 2 or 4 can be converted to utf8"); + } + } + template static inline void to_wstring(AZStd::basic_string& dest, const char* first, const char* last) { @@ -67,6 +86,25 @@ namespace AZStd static_assert(!AZStd::is_same_v, "Cannot convert a utf8 string to a wchar_t that isn't size 2 or 4"); } } + + template + static inline void to_wstring(AZStd::basic_fixed_string& dest, const char* first, const char* last) + { + if constexpr (Size == 2) + { + Utf8::Unchecked::utf8to16(first, last, AZStd::back_inserter(dest)); + } + else if constexpr (Size == 4) + { + Utf8::Unchecked::utf8to32(first, last, AZStd::back_inserter(dest)); + } + else + { + // Workaround to defer static_assert evaluation until this function is invoked by using the template parameter + using StringType = AZStd::basic_string; + static_assert(!AZStd::is_same_v, "Cannot convert a utf8 string to a wchar_t that isn't size 2 or 4"); + } + } }; } // 21.5: numeric conversions @@ -266,6 +304,28 @@ namespace AZStd return to_string(dest, src.c_str(), src.length()); } + template + void to_string(AZStd::basic_fixed_string& dest, const wchar_t* str, size_t srcLen = 0) + { + dest.clear(); + + if (srcLen == 0) + { + srcLen = wcslen(str); + } + + if (srcLen > 0) + { + Internal::WCharTPlatformConverter<>::to_string(dest, str, str + srcLen); + } + } + + template + void to_string(AZStd::basic_fixed_string& dest, const AZStd::basic_fixed_string& src) + { + return to_string(dest, src.c_str(), src.length()); + } + template int stoi(const AZStd::basic_string& str, AZStd::size_t* idx = 0, int base = 10) { @@ -384,6 +444,28 @@ namespace AZStd return to_wstring(dest, src.c_str(), src.length()); } + template + void to_wstring(AZStd::basic_fixed_string& dest, const char* str, size_t strLen = 0) + { + dest.clear(); + + if (strLen == 0) + { + strLen = strlen(str); + } + + if (strLen > 0) + { + Internal::WCharTPlatformConverter<>::to_wstring(dest, str, str + strLen); + } + } + + template + void to_wstring(AZStd::basic_fixed_string& dest, const AZStd::basic_fixed_string& src) + { + return to_wstring(dest, src.c_str(), src.length()); + } + // Convert a range of chars to lower case #if defined(AZSTD_USE_OLD_RW_STL) template diff --git a/Code/Framework/AzCore/Platform/Common/Apple/AzCore/Debug/Trace_Apple.cpp b/Code/Framework/AzCore/Platform/Common/Apple/AzCore/Debug/Trace_Apple.cpp index 62fe849c36..4ae25bfaf9 100644 --- a/Code/Framework/AzCore/Platform/Common/Apple/AzCore/Debug/Trace_Apple.cpp +++ b/Code/Framework/AzCore/Platform/Common/Apple/AzCore/Debug/Trace_Apple.cpp @@ -72,6 +72,7 @@ namespace AZ void OutputToDebugger(const char*, const char*) { + // std::cout << title << ": " << message; } } } diff --git a/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Debug/Trace_WinAPI.cpp b/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Debug/Trace_WinAPI.cpp index 143bd8f9d0..99ea10e4bc 100644 --- a/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Debug/Trace_WinAPI.cpp +++ b/Code/Framework/AzCore/Platform/Common/WinAPI/AzCore/Debug/Trace_WinAPI.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include @@ -22,7 +24,7 @@ namespace AZ LPTOP_LEVEL_EXCEPTION_FILTER g_previousExceptionHandler = nullptr; #endif - const int g_maxMessageLength = 4096; + constexpr int g_maxMessageLength = 4096; namespace Debug { @@ -58,15 +60,18 @@ namespace AZ TerminateProcess(GetCurrentProcess(), exitCode); } - void OutputToDebugger(const char* window, const char* message) + void OutputToDebugger([[maybe_unused]] const char* window, const char* message) { - AZ_UNUSED(window); - wchar_t messageW[g_maxMessageLength]; - size_t numCharsConverted; - if (mbstowcs_s(&numCharsConverted, messageW, message, g_maxMessageLength - 1) == 0) + AZStd::fixed_wstring tmpW; + if(window) { - OutputDebugStringW(messageW); + AZStd::to_wstring(tmpW, window); + tmpW += L": "; + OutputDebugStringW(tmpW.c_str()); + tmpW.clear(); } + AZStd::to_wstring(tmpW, message); + OutputDebugStringW(tmpW.c_str()); } } } diff --git a/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp b/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp index 3f9e475153..86c7e14ae6 100644 --- a/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp +++ b/Code/Framework/AzCore/Platform/Linux/AzCore/Debug/Trace_Linux.cpp @@ -7,6 +7,7 @@ */ #include +#include namespace AZ { @@ -14,8 +15,9 @@ namespace AZ { namespace Platform { - void OutputToDebugger(const char*, const char*) + void OutputToDebugger(const char* title, const char* message) { + // std::cout << title << ": " << message; } } } diff --git a/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.cpp b/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.cpp index 7f13ec442a..5555704040 100644 --- a/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.cpp +++ b/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.cpp @@ -29,7 +29,7 @@ namespace AZ return errno; } - void SharedMemory_Mac::ComposeMutexName(char* dest, size_t length, const char* name) + void ComposeMutexName(char* dest, size_t length, const char* name) { azstrncpy(m_name, AZ_ARRAY_SIZE(m_name), name, strlen(name)); diff --git a/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.h b/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.h index 8e172d920f..c0d5d7c2e9 100644 --- a/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.h +++ b/Code/Framework/AzCore/Platform/Mac/AzCore/IPC/SharedMemory_Mac.h @@ -29,7 +29,6 @@ namespace AZ static int GetLastError(); - void ComposeMutexName(char* dest, size_t length, const char* name); CreateResult Create(const char* name, unsigned int size, bool openIfCreated); bool Open(const char* name); void Close(); diff --git a/Code/Framework/AzCore/Platform/Windows/AzCore/IO/Streamer/StreamerConfiguration_Windows.cpp b/Code/Framework/AzCore/Platform/Windows/AzCore/IO/Streamer/StreamerConfiguration_Windows.cpp index 35835c374c..0813d2d057 100644 --- a/Code/Framework/AzCore/Platform/Windows/AzCore/IO/Streamer/StreamerConfiguration_Windows.cpp +++ b/Code/Framework/AzCore/Platform/Windows/AzCore/IO/Streamer/StreamerConfiguration_Windows.cpp @@ -290,7 +290,7 @@ namespace AZ::IO static bool CollectHardwareInfo(HardwareInformation& hardwareInfo, bool addAllDrives, bool reportHardware) { char drives[512]; - if (::GetLogicalDriveStrings(sizeof(drives) - 1, drives)) + if (::GetLogicalDriveStringsA(sizeof(drives) - 1, drives)) { AZStd::unordered_map driveMappings; char* driveIt = drives; @@ -318,7 +318,7 @@ namespace AZ::IO deviceName += driveIt; deviceName.erase(deviceName.length() - 1); // Erase the slash. - HANDLE deviceHandle = ::CreateFile( + HANDLE deviceHandle = ::CreateFileA( deviceName.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr); if (deviceHandle != INVALID_HANDLE_VALUE) { diff --git a/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.cpp b/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.cpp index 21b2a76ba0..20c126f7a8 100644 --- a/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.cpp +++ b/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.cpp @@ -11,6 +11,7 @@ #include #include #include +#include namespace AZ { @@ -21,16 +22,17 @@ namespace AZ { } - void SharedMemory_Windows::ComposeMutexName(char* dest, size_t length, const char* name) + void ComposeName(AZStd::fixed_wstring<256>& dest, const char* name, const wchar_t* suffix) { - azstrncpy(m_name, AZ_ARRAY_SIZE(m_name), name, strlen(name)); - azsnprintf(dest, length, "%s_Mutex", name); + AZStd::to_wstring(dest, name); + dest += L"_"; + dest += suffix; } SharedMemory_Common::CreateResult SharedMemory_Windows::Create(const char* name, unsigned int size, bool openIfCreated) { - char fullName[256]; - ComposeMutexName(fullName, AZ_ARRAY_SIZE(fullName), name); + AZStd::fixed_wstring<256> fullName; + ComposeName(fullName, name, L"Mutex"); // Security attributes SECURITY_ATTRIBUTES secAttr; @@ -42,9 +44,7 @@ namespace AZ SetSecurityDescriptorDacl(secAttr.lpSecurityDescriptor, TRUE, 0, FALSE); // Obtain global mutex - AZStd::wstring fullNameW; - AZStd::to_wstring(fullNameW, fullName); - m_globalMutex = CreateMutexW(&secAttr, FALSE, fullNameW.c_str()); + m_globalMutex = CreateMutexW(&secAttr, FALSE, fullName.c_str()); DWORD error = GetLastError(); if (m_globalMutex == NULL || (error == ERROR_ALREADY_EXISTS && openIfCreated == false)) { @@ -53,8 +53,8 @@ namespace AZ } // Create the file mapping. - azsnprintf(fullName, AZ_ARRAY_SIZE(fullName), "%s_Data", name); - m_mapHandle = CreateFileMappingW(INVALID_HANDLE_VALUE, &secAttr, PAGE_READWRITE, 0, size, fullNameW.c_str()); + ComposeName(fullName, name, L"Data"); + m_mapHandle = CreateFileMappingW(INVALID_HANDLE_VALUE, &secAttr, PAGE_READWRITE, 0, size, fullName.c_str()); error = GetLastError(); if (m_mapHandle == NULL || (error == ERROR_ALREADY_EXISTS && openIfCreated == false)) { @@ -67,12 +67,10 @@ namespace AZ bool SharedMemory_Windows::Open(const char* name) { - char fullName[256]; - ComposeMutexName(fullName, AZ_ARRAY_SIZE(fullName), name); - AZStd::wstring fullNameW; - AZStd::to_wstring(fullNameW, fullName); + AZStd::fixed_wstring<256> fullName; + ComposeName(fullName, name, L"Mutex"); - m_globalMutex = OpenMutex(SYNCHRONIZE, TRUE, fullNameW.c_str()); + m_globalMutex = OpenMutex(SYNCHRONIZE, TRUE, fullName.c_str()); AZ_Warning("AZSystem", m_globalMutex != NULL, "Failed to open OS mutex [%s]\n", m_name); if (m_globalMutex == NULL) { @@ -80,8 +78,8 @@ namespace AZ return false; } - azsnprintf(fullName, AZ_ARRAY_SIZE(fullName), "%s_Data", name); - m_mapHandle = OpenFileMapping(FILE_MAP_WRITE, false, fullNameW.c_str()); + ComposeName(fullName, name, L"Data"); + m_mapHandle = OpenFileMapping(FILE_MAP_WRITE, false, fullName.c_str()); if (m_mapHandle == NULL) { AZ_TracePrintf("AZSystem", "OpenFileMapping %s failed with error %d\n", m_name, GetLastError()); diff --git a/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.h b/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.h index 7f206f5413..7b10b9163e 100644 --- a/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.h +++ b/Code/Framework/AzCore/Platform/Windows/AzCore/IPC/SharedMemory_Windows.h @@ -41,9 +41,6 @@ namespace AZ HANDLE m_mapHandle; HANDLE m_globalMutex; int m_lastLockResult; - - private: - void ComposeMutexName(char* dest, size_t length, const char* name); }; using SharedMemory_Platform = SharedMemory_Windows;