refactor. less reliance on edge cases

This commit is contained in:
Akash Mozumdar 2018-11-01 15:03:30 -04:00
parent aa2d71a078
commit 942cdd94ba
3 changed files with 8 additions and 7 deletions

View File

@ -60,7 +60,7 @@ namespace
if (textThreadsByParams[tp] == nullptr) if (textThreadsByParams[tp] == nullptr)
{ {
if (textThreadsByParams.size() > MAX_THREAD_COUNT) return Host::AddConsoleOutput(L"too many text threads: can't create more"); if (textThreadsByParams.size() > MAX_THREAD_COUNT) return Host::AddConsoleOutput(L"too many text threads: can't create more");
OnCreate(textThreadsByParams[tp] = std::make_shared<TextThread>(tp)); OnCreate(textThreadsByParams[tp] = std::make_shared<TextThread>(tp, Host::GetHookParam(tp), Host::GetHookName(tp)));
} }
textThreadsByParams[tp]->AddText(text, len); textThreadsByParams[tp]->AddText(text, len);
} }
@ -153,7 +153,7 @@ namespace Host
void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove, TextThread::OutputCallback output) void Start(ProcessEventCallback onAttach, ProcessEventCallback onDetach, ThreadEventCallback onCreate, ThreadEventCallback onRemove, TextThread::OutputCallback output)
{ {
OnAttach = onAttach; OnDetach = onDetach; OnCreate = onCreate; OnRemove = onRemove; TextThread::Output = output; OnAttach = onAttach; OnDetach = onDetach; OnCreate = onCreate; OnRemove = onRemove; TextThread::Output = output;
OnCreate(textThreadsByParams[CONSOLE] = std::make_shared<TextThread>(CONSOLE)); OnCreate(textThreadsByParams[CONSOLE] = std::make_shared<TextThread>(CONSOLE, HookParam{}, L"Console"));
StartPipe(); StartPipe();
} }
@ -238,14 +238,12 @@ namespace Host
HookParam GetHookParam(DWORD processId, uint64_t addr) HookParam GetHookParam(DWORD processId, uint64_t addr)
{ {
if (processId == 0) return {};
LOCK(hostMutex); LOCK(hostMutex);
return processRecordsByIds.at(processId)->GetHook(addr).hp; return processRecordsByIds.at(processId)->GetHook(addr).hp;
} }
std::wstring GetHookName(DWORD processId, uint64_t addr) std::wstring GetHookName(DWORD processId, uint64_t addr)
{ {
if (processId == 0) return L"Console";
LOCK(hostMutex); LOCK(hostMutex);
return StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName, CP_UTF8); return StringToWideString(processRecordsByIds.at(processId)->GetHook(addr).hookName, CP_UTF8);
} }
@ -256,7 +254,10 @@ namespace Host
return textThreadsByParams[tp]; return textThreadsByParams[tp];
} }
void AddConsoleOutput(std::wstring text) { GetThread(CONSOLE)->AddSentence(text); } void AddConsoleOutput(std::wstring text)
{
GetThread(CONSOLE)->AddSentence(text);
}
} }
// EOF // EOF

View File

@ -8,7 +8,7 @@
#include <regex> #include <regex>
#include <algorithm> #include <algorithm>
TextThread::TextThread(ThreadParam tp) : handle(threadCounter++), name(Host::GetHookName(tp)), tp(tp), hp(Host::GetHookParam(tp)) {} TextThread::TextThread(ThreadParam tp, HookParam hp, std::wstring name) : handle(threadCounter++), name(name), tp(tp), hp(hp) {}
TextThread::~TextThread() TextThread::~TextThread()
{ {

View File

@ -19,7 +19,7 @@ public:
inline static int maxBufferSize = 200; inline static int maxBufferSize = 200;
inline static int threadCounter = 0; inline static int threadCounter = 0;
TextThread(ThreadParam tp); TextThread(ThreadParam tp, HookParam hp, std::wstring name);
~TextThread(); ~TextThread();
std::wstring GetStorage(); std::wstring GetStorage();