From 4dc7b236c94e72700597826021f555a9460bd879 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 21 Sep 2018 21:27:59 -0400 Subject: [PATCH] refactor --- GUI/host/host.cc | 9 +++------ GUI/host/host.h | 8 ++++++++ GUI/host/textthread.cc | 14 +++----------- GUI/host/textthread.h | 2 ++ include/const.h | 2 +- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/GUI/host/host.cc b/GUI/host/host.cc index 9ac4206..c91a99d 100644 --- a/GUI/host/host.cc +++ b/GUI/host/host.cc @@ -6,7 +6,6 @@ #include "const.h" #include "defs.h" #include "../vnrhook/hijack/texthook.h" -#include // A2W namespace { @@ -116,8 +115,7 @@ namespace case HOST_NOTIFICATION_TEXT: { auto info = *(ConsoleOutputNotif*)buffer; - USES_CONVERSION; - Host::AddConsoleOutput(A2W(info.message)); + Host::AddConsoleOutput(ToWString(info.message, CP_UTF8)); } break; default: @@ -255,11 +253,10 @@ namespace Host if (hooks[i].hp.address == addr) { buffer.resize(hooks[i].name_length); - ReadProcessMemory(pr.processHandle, hooks[i].hook_name, &buffer[0], hooks[i].name_length, nullptr); + ReadProcessMemory(pr.processHandle, hooks[i].hook_name, buffer.data(), hooks[i].name_length, nullptr); } ReleaseMutex(pr.sectionMutex); - USES_CONVERSION; - return std::wstring(A2W(buffer.c_str())); + return ToWString(buffer.c_str(), CP_UTF8); } TextThread* GetThread(ThreadParam tp) diff --git a/GUI/host/host.h b/GUI/host/host.h index e70ffc1..c1b4c15 100644 --- a/GUI/host/host.h +++ b/GUI/host/host.h @@ -28,4 +28,12 @@ namespace Host TextThread* GetThread(ThreadParam tp); void AddConsoleOutput(std::wstring text); } + +inline std::wstring ToWString(const char* text, UINT encoding) +{ + std::wstring ret(strlen(text), 0); + ret.resize(MultiByteToWideChar(encoding, 0, text, -1, ret.data(), ret.size())); + return ret; +} + // EOF diff --git a/GUI/host/textthread.cc b/GUI/host/textthread.cc index 4136953..151ad20 100644 --- a/GUI/host/textthread.cc +++ b/GUI/host/textthread.cc @@ -27,17 +27,9 @@ void TextThread::Flush() std::wstring sentence; { LOCK(ttMutex); - if (buffer.size() < 400 && (GetTickCount() - timestamp < 250 || buffer.size() == 0)) return; // TODO: let user change delay before sentence is flushed - if (status & USING_UNICODE) - { - sentence = std::wstring((wchar_t*)buffer.data(), buffer.size() / 2); - } - else - { - wchar_t* converted = new wchar_t[buffer.size()]; - sentence = std::wstring(converted, MultiByteToWideChar(status & USING_UTF8 ? CP_UTF8 : 932, 0, buffer.data(), buffer.size(), converted, buffer.size())); - delete[] converted; - } + if (buffer.size() < 400 && (GetTickCount() - timestamp < flushDelay || buffer.size() == 0)) return; // TODO: let user change delay before sentence is flushed + if (status & USING_UNICODE) sentence = std::wstring((wchar_t*)buffer.data(), buffer.size() / 2); + else sentence = ToWString(buffer.data(), status & USING_UTF8 ? CP_UTF8 : SHIFT_JIS); buffer.clear(); } AddSentence(sentence); diff --git a/GUI/host/textthread.h b/GUI/host/textthread.h index 5193bd1..f00c61d 100644 --- a/GUI/host/textthread.h +++ b/GUI/host/textthread.h @@ -24,6 +24,8 @@ public: const std::wstring name; const ThreadParam tp; + inline static unsigned flushDelay = 250; // flush every 250ms by default + private: void Flush(); diff --git a/include/const.h b/include/const.h index b6b70ba..144fa52 100644 --- a/include/const.h +++ b/include/const.h @@ -4,7 +4,7 @@ // 8/23/2013 jichi // Branch: ITH/common.h, rev 128 -enum { MESSAGE_SIZE = 500, PIPE_BUFFER_SIZE = 0x1000 }; +enum { MESSAGE_SIZE = 500, PIPE_BUFFER_SIZE = 0x1000, SHIFT_JIS = 932 }; // jichi 375/2014: Add offset of pusha/pushad // http://faydoc.tripod.com/cpu/pushad.htm