-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathUtils.cpp
124 lines (103 loc) · 2.59 KB
/
Utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include "pch.h"
#include "Utils.hpp"
#include <string>
#include <string_view>
#include <vector>
constexpr auto LOG_LINES = 64;
namespace moonlight_xbox_dx {
namespace Utils {
std::vector<std::wstring> logLines;
bool showLogs = false;
bool showStats = false;
StreamingStats stats;
std::mutex logMutex;
Platform::String^ StringPrintf(const char* fmt, ...) {
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 std::string_view& msg) {
try {
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 (...) {
}
}
void Log(const char* msg) {
if (msg) {
Log(std::string_view(msg));
}
}
std::vector<std::wstring> GetLogLines() {
return logLines;
}
Platform::String^ StringFromChars(const char* chars)
{
if (chars == nullptr) {
return nullptr;
}
return ref new Platform::String(NarrowToWideString(std::string_view(chars)).c_str());
}
Platform::String^ StringFromStdString(std::string input) {
return ref new Platform::String(NarrowToWideString(input).c_str());
}
std::string PlatformStringToStdString(Platform::String ^input) {
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;
}
}
}