Skip to content

Commit 8a5c224

Browse files
committed
utf-8 support
1 parent 2eb26ab commit 8a5c224

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

CMakeLists.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,12 @@ include(libraries/mat-dash/include.cmake)
77

88
project(replay-bot)
99

10-
find_file(WINDOWS_HEADER windows.h)
11-
if(NOT WINDOWS_HEADER)
12-
message(FATAL_ERROR "Can't find windows.h!")
13-
endif()
14-
15-
# TODO: not
16-
add_compile_definitions(FTG_IGNORE_UNICODE)
17-
1810
if (${CMAKE_BUILD_TYPE} STREQUAL "RelWithDebInfo")
1911
add_compile_definitions(SHOW_CONSOLE)
2012
endif()
2113

14+
add_compile_definitions(UNICODE _UNICODE)
15+
2216
file(
2317
GLOB_RECURSE SOURCE_FILES
2418
src/*.cpp

libraries/subprocess.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace subprocess {
5454
sec_attrs.lpSecurityDescriptor = nullptr;
5555

5656
m_proc_info = {0};
57-
STARTUPINFOA start_info = {0};
57+
STARTUPINFOW start_info = {0};
5858

5959
start_info.cb = sizeof(start_info);
6060
start_info.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
@@ -65,7 +65,14 @@ namespace subprocess {
6565
start_info.hStdInput = m_stdin.m_read.handle;
6666
m_stdin.m_write.set_inherit(false);
6767

68-
CreateProcessA(nullptr, const_cast<char*>(command.c_str()), nullptr, nullptr, true, 0, nullptr, nullptr, &start_info, &m_proc_info);
68+
// cant include "utils.hpp" here, so just copy paste code from myself :D
69+
auto size = MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, nullptr, 0);
70+
auto buffer = new wchar_t[size];
71+
MultiByteToWideChar(CP_UTF8, 0, command.c_str(), -1, buffer, size);
72+
73+
CreateProcessW(nullptr, buffer, nullptr, nullptr, true, 0, nullptr, nullptr, &start_info, &m_proc_info);
74+
75+
delete[] buffer;
6976

7077
m_stdin.m_read.close();
7178
}

src/recorder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ void Recorder::start(const std::string& path) {
6666
}
6767
std::cout << "video should be done now" << std::endl;
6868
if (!m_include_audio || !std::filesystem::exists(song_file)) return;
69-
char buffer[MAX_PATH];
70-
if (!GetTempFileNameA(std::filesystem::temp_directory_path().string().c_str(), "rec", 0, buffer)) {
69+
wchar_t buffer[MAX_PATH];
70+
if (!GetTempFileNameW(widen(std::filesystem::temp_directory_path().string()).c_str(), L"rec", 0, buffer)) {
7171
std::cout << "error getting temp file" << std::endl;
7272
return;
7373
}
74-
auto temp_path = std::string(buffer) + "." + std::filesystem::path(path).filename().string();
74+
auto temp_path = narrow(buffer) + "." + std::filesystem::path(path).filename().string();
7575
std::filesystem::rename(buffer, temp_path);
7676
auto total_time = m_last_frame_t; // 1 frame too short?
7777
{
@@ -92,8 +92,8 @@ void Recorder::start(const std::string& path) {
9292
return;
9393
}
9494
}
95-
std::filesystem::remove(path);
96-
std::filesystem::rename(temp_path, path);
95+
std::filesystem::remove(widen(path));
96+
std::filesystem::rename(temp_path, widen(path));
9797
std::cout << "video + audio should be done now!" << std::endl;
9898
}).detach();
9999
}

src/replay.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ constexpr const char* format_magic = "RPLY";
4949

5050
void Replay::save(const std::string& path) {
5151
std::ofstream file;
52-
file.open(path, std::ios::binary | std::ios::out);
52+
file.open(widen(path), std::ios::binary | std::ios::out);
5353
file << format_magic << format_ver << type;
5454
bin_write(file, fps);
5555
for (const auto& action : actions) {
@@ -66,7 +66,7 @@ void Replay::save(const std::string& path) {
6666
Replay Replay::load(const std::string& path) {
6767
Replay replay(0, ReplayType::XPOS);
6868
std::ifstream file;
69-
file.open(path, std::ios::binary | std::ios::in);
69+
file.open(widen(path), std::ios::binary | std::ios::in);
7070

7171
file.seekg(0, std::ios::end);
7272
size_t file_size = static_cast<size_t>(file.tellg());

src/utils.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "utils.hpp"
2+
3+
std::string narrow(const wchar_t* str) {
4+
auto size = WideCharToMultiByte(CP_UTF8, 0, str, -1, nullptr, 0, nullptr, nullptr);
5+
if (size <= 0) { /* fuck */ }
6+
auto buffer = new char[size];
7+
WideCharToMultiByte(CP_UTF8, 0, str, -1, buffer, size, nullptr, nullptr);
8+
std::string result(buffer, size_t(size) - 1);
9+
delete[] buffer;
10+
return result;
11+
}
12+
13+
std::wstring widen(const char* str) {
14+
auto size = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
15+
if (size <= 0) { /* fuck */ }
16+
auto buffer = new wchar_t[size];
17+
MultiByteToWideChar(CP_UTF8, 0, str, -1, buffer, size);
18+
std::wstring result(buffer, size_t(size) - 1);
19+
delete[] buffer;
20+
return result;
21+
}

src/utils.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ template <typename R, typename U>
2525
R& from_offset(U base, int offset) {
2626
return *cast<R*>(cast<intptr_t>(base) + offset);
2727
}
28+
29+
std::string narrow(const wchar_t* str);
30+
inline auto narrow(const std::wstring& str) { return narrow(str.c_str()); }
31+
std::wstring widen(const char* str);
32+
inline auto widen(const std::string& str) { return widen(str.c_str()); }

0 commit comments

Comments
 (0)