Skip to content

Commit

Permalink
Merge pull request #168 from LAGonauta/improve-utils-methods
Browse files Browse the repository at this point in the history
Utils: improve string methods
  • Loading branch information
TheElixZammuto authored Feb 26, 2025
2 parents 5af12e6 + b105118 commit c453672
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 53 deletions.
131 changes: 90 additions & 41 deletions Utils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once
#include "pch.h"
#include "Utils.hpp"
#define LOG_LINES 64

#include <string>
#include <string_view>
#include <vector>

constexpr auto LOG_LINES = 64;

namespace moonlight_xbox_dx {
namespace Utils {
Expand All @@ -12,64 +17,108 @@ namespace moonlight_xbox_dx {
std::mutex logMutex;

Platform::String^ StringPrintf(const char* fmt, ...) {
va_list list;
va_start(list, fmt);
char message[2048];
vsprintf_s(message, 2047, fmt, list);
std::string s_str = std::string(message);
std::wstring wid_str = std::wstring(s_str.begin(), s_str.end());
const wchar_t* w_char = wid_str.c_str();
Platform::String^ p_string = ref new Platform::String(w_char);
return p_string;
va_list args;
va_start(args, fmt);

va_list args_copy;
va_copy(args_copy, args);
auto size = vsnprintf(nullptr, 0, fmt, args_copy);
va_end(args_copy);

if (size < 0) {
va_end(args);
return nullptr;
}

// Needs space for NUL char
std::vector<char> message(size + 1, 0);
vsnprintf_s(message.data(), message.size(), message.size(), fmt, args);
va_end(args);

return ref new Platform::String(NarrowToWideString(std::string_view(message.data())).c_str());
}

void Log(const char* fmt) {
void Log(const std::string_view& msg) {
try {
if (fmt == nullptr || fmt == NULL)return;
int len = strlen(fmt) + 1;
wchar_t* stringBuf = (wchar_t*)malloc(sizeof(wchar_t) * len);
if (stringBuf == NULL)return;
mbstowcs(stringBuf, fmt, len);
std::wstring string(stringBuf);
logMutex.lock();
if (logLines.size() == LOG_LINES)logLines.erase(logLines.begin());
logLines.push_back(string);
logMutex.unlock();
OutputDebugStringA(fmt);
std::wstring string = NarrowToWideString(msg);
{
std::unique_lock<std::mutex> lk(logMutex);
if (logLines.size() == LOG_LINES) {
logLines.erase(logLines.begin());
}
logLines.push_back(string);
}
OutputDebugString(string.c_str());
}
catch (...){
catch (...) {

}
}

void Log(const char* msg) {
if (msg) {
Log(std::string_view(msg));
}
}

std::vector<std::wstring> GetLogLines() {
return logLines;
}

//https://stackoverflow.com/a/20707518
Platform::String^ StringFromChars(char* chars)
Platform::String^ StringFromChars(const char* chars)
{
int wchars_num = MultiByteToWideChar(CP_UTF8, 0, chars, -1, NULL, 0);
wchar_t* wstr = new wchar_t[wchars_num];
MultiByteToWideChar(CP_UTF8, 0, chars, -1, wstr, wchars_num);
Platform::String^ str = ref new Platform::String(wstr);
delete[] wstr;
return str;
if (chars == nullptr) {
return nullptr;
}
return ref new Platform::String(NarrowToWideString(std::string_view(chars)).c_str());
}

//https://stackoverflow.com/a/43628199
Platform::String^ StringFromStdString(std::string input) {
std::wstring w_str = std::wstring(input.begin(), input.end());
const wchar_t* w_chars = w_str.c_str();
return (ref new Platform::String(w_chars));
return ref new Platform::String(NarrowToWideString(input).c_str());
}

//https://stackoverflow.com/a/35905753
std::string PlatformStringToStdString(Platform::String ^input) {
std::wstring fooW(input->Begin());
std::string fooA(fooW.begin(), fooW.end());
return fooA;
return WideToNarrowString(std::wstring(input->Begin()));
}
}

}
std::string WideToNarrowString(const std::wstring_view& str) {
auto bufferSize = WideCharToMultiByte(CP_UTF8,
0,
str.data(),
str.length(),
nullptr,
0, nullptr, nullptr);

std::string result;
result.resize(bufferSize);
WideCharToMultiByte(CP_UTF8,
0,
str.data(),
str.length(),
result.data(),
result.size(), nullptr, nullptr);

return result;
}

std::wstring NarrowToWideString(const std::string_view& str) {
auto bufferSize = MultiByteToWideChar(CP_UTF8,
0,
str.data(),
str.length(),
nullptr,
0);

std::wstring result;
result.resize(bufferSize);
MultiByteToWideChar(CP_UTF8,
0,
str.data(),
str.length(),
result.data(),
result.size());

return result;
}
}
}
9 changes: 5 additions & 4 deletions Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ namespace moonlight_xbox_dx {

Platform::String^ StringPrintf(const char* fmt, ...);

void Log(const char* fmt);
void Log(const char* msg);
void Log(const std::string_view& msg);

std::vector<std::wstring> GetLogLines();
Platform::String^ StringFromChars(char* chars);
Platform::String^ StringFromChars(const char* chars);
Platform::String^ StringFromStdString(std::string st);
std::string PlatformStringToStdString(Platform::String^ input);

}
std::string WideToNarrowString(const std::wstring_view& str);
std::wstring NarrowToWideString(const std::string_view& str); }
}
24 changes: 16 additions & 8 deletions moonlight-xbox-dx.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
Expand All @@ -163,7 +164,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">
Expand All @@ -177,7 +179,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">
Expand All @@ -191,7 +194,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand All @@ -205,7 +209,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -219,7 +224,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(IntermediateOutputPath);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
Expand All @@ -233,7 +239,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)vcpkg_installed\x64-uwp\include;$(IntermediateOutputPath);third_party\moonlight-common-c\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_DEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -247,7 +254,8 @@
<AdditionalIncludeDirectories>$(ProjectDir);$(ProjectDir)vcpkg_installed\x64-uwp\include;$(IntermediateOutputPath);third_party\moonlight-common-c\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>4453;28204</DisableSpecificWarnings>
<PreprocessorDefinitions>_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;NDEBUG;%(PreprocessorDefinitions);_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING</PreprocessorDefinitions>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down

0 comments on commit c453672

Please sign in to comment.