-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathUtils.cpp
More file actions
177 lines (148 loc) · 3.99 KB
/
Copy pathUtils.cpp
File metadata and controls
177 lines (148 loc) · 3.99 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#pragma once
#include "pch.h"
#include "Utils.hpp"
#include <chrono>
#include <cwchar>
#include <string>
#include <string_view>
#include <vector>
using namespace std::chrono;
constexpr auto LOG_LINES = 70;
namespace moonlight_xbox_dx {
namespace Utils {
std::vector<std::wstring> logLines;
bool showLogs = false;
bool showStats = false;
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());
}
std::wstring GetCurrentTimestamp() {
auto now = system_clock::now();
auto ms = duration_cast<milliseconds>(now.time_since_epoch()) % 1000;
std::time_t tt = system_clock::to_time_t(now);
std::tm local_tm{};
localtime_s(&local_tm, &tt);
wchar_t buffer[32];
swprintf(buffer, 32, L"[%02d:%02d:%02d.%03d] ",
local_tm.tm_hour,
local_tm.tm_min,
local_tm.tm_sec,
static_cast<int>(ms.count()));
return std::wstring(buffer);
}
void Log(const std::string_view& msg) {
try {
std::wstring string = GetCurrentTimestamp() + NarrowToWideString(msg);
OutputDebugString(string.c_str());
{
std::unique_lock<std::mutex> lk(logMutex);
if (logLines.size() == LOG_LINES) {
logLines.erase(logLines.begin());
}
for (auto& ch : string) {
// ModeSeven renders [ ] as left and right arrows, so we replace them
// with { } which render as brackets
if (ch == L'[') {
ch = L'{';
}
else if (ch == L']') {
ch = L'}';
}
}
logLines.push_back(string);
}
}
catch (...) {
}
}
void Log(const char* msg) {
if (msg) {
Log(std::string_view(msg));
}
}
void Logf(const char* format, ...) {
va_list args;
va_start(args, format);
char buf[1024];
std::vsnprintf(buf, sizeof(buf) - 1, format, args);
va_end(args);
Log(std::string_view(buf));
}
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;
}
bool ShowDevTools() {
// Allow Frame Capture with Andy's build
if (Windows::ApplicationModel::Package::Current->DisplayName == "Moonlight UWP (AndyG)") {
return true;
}
// I think this check for Dev Mode only works when the app is deployed from Visual Studio
if (Windows::ApplicationModel::Package::Current->IsDevelopmentMode) {
return true;
}
return false;
}
}
}