From 12963a053b55ccd495fb4d97bc5c95c83d025957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Thu, 8 Feb 2024 19:12:20 +0800 Subject: [PATCH] fix bug --- LunaHost/GUI/LunaHost.cpp | 58 ++--------------------- LunaHost/GUI/LunaHost.h | 6 +-- LunaHost/GUI/pluginmanager.cpp | 87 ++++++++++++++++++++++++++++++---- LunaHost/GUI/pluginmanager.h | 11 ++++- 4 files changed, 91 insertions(+), 71 deletions(-) diff --git a/LunaHost/GUI/LunaHost.cpp b/LunaHost/GUI/LunaHost.cpp index 582aac5..137e436 100644 --- a/LunaHost/GUI/LunaHost.cpp +++ b/LunaHost/GUI/LunaHost.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include"host.h" #include"hookcode.h" @@ -34,24 +33,7 @@ void LunaHost::on_size(int w,int h){ g_hListBox_listtext->setgeo(10, 110, w - 20, height/2); g_showtexts->setgeo(10, 120+height/2, w - 20, height/2); } -std::optionalSelectFile(HWND hwnd){ - OPENFILENAME ofn; - wchar_t szFileName[MAX_PATH] = { 0 }; - ZeroMemory(&ofn, sizeof(ofn)); - ofn.lStructSize = sizeof(ofn); - ofn.hwndOwner = hwnd; - ofn.lpstrFilter = L"Plugin Files\0*.dll;*.xdll\0"; - ofn.lpstrFile = szFileName; - ofn.nMaxFile = sizeof(szFileName); - ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; - - if (GetOpenFileName(&ofn)) - { - return szFileName; - } - else return {}; -} LunaHost::LunaHost(){ settext(WndLunaHostGui); g_selectprocessbutton =new button(this,BtnSelectProcess,830, 10, 200, 40) ; @@ -59,7 +41,7 @@ LunaHost::LunaHost(){ g_hEdit_userhook = new textedit(this,L"",10, 60, 600, 40,ES_AUTOHSCROLL); btnaddplugin=new button(this,BtnAddPlugin,830,60,200,40); btnaddplugin->onclick=[&](){ - auto f=SelectFile(winId); + auto f=plugins->selectpluginfile(); if(f.has_value()){ plugins->addplugin(f.value()); } @@ -147,7 +129,7 @@ LunaHost::LunaHost(){ }; g_showtexts = new textedit(this,L"",10, 330, 200, 200,ES_READONLY|ES_MULTILINE |ES_AUTOVSCROLL| WS_VSCROLL); - plugins=new pluginmanager; + plugins=new pluginmanager(this); Host::Start( [&](DWORD pid) {attachedprocess.push_back(pid);}, @@ -160,44 +142,10 @@ LunaHost::LunaHost(){ ); } -std::array LunaHost::GetSentenceInfo(TextThread& thread) -{ - void (*AddText)(int64_t, const wchar_t*) = [](int64_t number, const wchar_t* text) - { - if (TextThread* thread = Host::GetThread(number)) thread->Push(text); - }; - void (*AddSentence)(int64_t, const wchar_t*) = [](int64_t number, const wchar_t* sentence) - { - if (TextThread* thread = Host::GetThread(number)) thread->AddSentence(sentence);; - }; - static DWORD SelectedProcessId; - auto currthread=Host::GetThread(currentselect); - SelectedProcessId=(currthread!=0)?currthread->tp.processId:0; - DWORD (*GetSelectedProcessId)() = [] { return SelectedProcessId; }; - - return - { { - { "HostHWND",(int64_t)winId }, - { "toclipboard", check_toclipboard }, - { "current select", &thread == currthread }, - { "text number", thread.handle }, - { "process id", thread.tp.processId }, - { "hook address", (int64_t)thread.tp.addr }, - { "text handle", thread.handle }, - { "text name", (int64_t)thread.name.c_str() }, - { "add sentence", (int64_t)AddSentence }, - { "add text", (int64_t)AddText }, - { "get selected process id", (int64_t)GetSelectedProcessId }, - { "void (*AddSentence)(int64_t number, const wchar_t* sentence)", (int64_t)AddSentence }, - { "void (*AddText)(int64_t number, const wchar_t* text)", (int64_t)AddText }, - { "DWORD (*GetSelectedProcessId)()", (int64_t)GetSelectedProcessId }, - { nullptr, 0 } // nullptr marks end of info array - } }; -} bool LunaHost::on_text_recv(TextThread& thread, std::wstring& output){ std::lock_guard _(settextmutex); std::wstring lfoutput=output; - if(!plugins->dispatch(GetSentenceInfo(thread).data(),output))return false; + if(!plugins->dispatch(thread,output))return false; strReplace(lfoutput,L"\n",L"\r\n"); savetext.at(thread.handle).push_back(lfoutput); if(currentselect==thread.handle){ diff --git a/LunaHost/GUI/LunaHost.h b/LunaHost/GUI/LunaHost.h index 6510169..5c2da2d 100644 --- a/LunaHost/GUI/LunaHost.h +++ b/LunaHost/GUI/LunaHost.h @@ -3,13 +3,10 @@ #include"processlistwindow.h" #include"textthread.h" #include"pluginmanager.h" -#include"Plugin/plugindef.h" class LunaHost:public mainwindow{ - int64_t currentselect=0; std::map>savetext; std::vectorattachedprocess; - bool check_toclipboard=false; std::mutex settextmutex; textedit* g_hEdit_userhook; button* g_hButton_insert; @@ -26,8 +23,9 @@ class LunaHost:public mainwindow{ bool on_text_recv(TextThread& thread, std::wstring& sentence); void on_thread_create(TextThread& thread); void on_thread_delete(TextThread& thread); - std::array GetSentenceInfo(TextThread& thread); public: + int64_t currentselect=0; + bool check_toclipboard=false; void on_size(int w,int h); void on_close(); LunaHost(); diff --git a/LunaHost/GUI/pluginmanager.cpp b/LunaHost/GUI/pluginmanager.cpp index 2c05348..0ee8bda 100644 --- a/LunaHost/GUI/pluginmanager.cpp +++ b/LunaHost/GUI/pluginmanager.cpp @@ -2,19 +2,49 @@ #include #include"Plugin/plugindef.h" #include +#include +#include"LunaHost.h" #include"Lang/Lang.h" -std::vector readfile(const wchar_t* fname) { +#include"host.h" +std::string readfile(const wchar_t* fname) { FILE* f; _wfopen_s(&f, fname, L"rb"); if (f == 0)return {}; fseek(f, 0, SEEK_END); auto len = ftell(f); fseek(f, 0, SEEK_SET); - auto buff = std::vector(len); + std::string buff; + buff.resize(len); fread(buff.data(), 1, len, f); fclose(f); return buff; } +void appendfile(const wchar_t* fname,const std::wstring& s){ + auto u8s=WideStringToString(s); + FILE* f; + _wfopen_s(&f, fname, L"a"); + fprintf(f,"\n%s",u8s.c_str()); + fclose(f); +} +std::optionalSelectFile(HWND hwnd,LPCWSTR lpstrFilter){ + OPENFILENAME ofn; + wchar_t szFileName[MAX_PATH] = { 0 }; + + ZeroMemory(&ofn, sizeof(ofn)); + ofn.lStructSize = sizeof(ofn); + ofn.hwndOwner = hwnd; + ofn.lpstrFilter = lpstrFilter; + ofn.lpstrFile = szFileName; + ofn.nMaxFile = sizeof(szFileName); + ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; + + if (GetOpenFileName(&ofn)) + { + return szFileName; + } + else return {}; +} + std::vectorpluginmanager::readpluginfile(){ if(!std::filesystem::exists(pluginfilename)) return{}; @@ -23,13 +53,9 @@ std::vectorpluginmanager::readpluginfile(){ return pls; } void pluginmanager::writepluginfile(const std::wstring& plugf){ - auto u8s=WideStringToString(plugf); - FILE* f; - _wfopen_s(&f, pluginfilename.c_str(), L"a"); - fprintf(f,"\n%s",u8s.c_str()); - fclose(f); + appendfile(pluginfilename.c_str(),plugf); } -pluginmanager::pluginmanager(){ +pluginmanager::pluginmanager(LunaHost* _host):host(_host){ try { std::scoped_lock lock(OnNewSentenceSLock); pluginfilename=std::filesystem::current_path()/(x64?"plugin64.txt":"plugin32.txt"); @@ -50,7 +76,8 @@ pluginmanager::pluginmanager(){ } } -bool pluginmanager::dispatch(const InfoForExtension* sentenceInfo, std::wstring& sentence){ +bool pluginmanager::dispatch(TextThread& thread, std::wstring& sentence){ + auto sentenceInfo=GetSentenceInfo(thread).data(); wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, (sentence.size() + 1) * sizeof(wchar_t)); wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str()); concurrency::reader_writer_lock::scoped_lock_read readLock(OnNewSentenceSLock); @@ -83,6 +110,10 @@ bool pluginmanager::checkisdump(LPVOID ptr){ } return false; } + +std::optionalpluginmanager::selectpluginfile(){ + return SelectFile(host->winId,L"Plugin Files\0*.dll;*.xdll\0"); +} bool pluginmanager::addplugin(const std::wstring& p){ auto plugin=checkisvalidplugin(p); if(plugin.has_value()){ @@ -95,7 +126,43 @@ bool pluginmanager::addplugin(const std::wstring& p){ return true; } else{ - MessageBoxW(0,InVaildPlugin,L"Error",0); + MessageBoxW(host->winId,InVaildPlugin,L"Error",0); return false; } +} + + +std::array pluginmanager::GetSentenceInfo(TextThread& thread) +{ + void (*AddText)(int64_t, const wchar_t*) = [](int64_t number, const wchar_t* text) + { + if (TextThread* thread = Host::GetThread(number)) thread->Push(text); + }; + void (*AddSentence)(int64_t, const wchar_t*) = [](int64_t number, const wchar_t* sentence) + { + if (TextThread* thread = Host::GetThread(number)) thread->AddSentence(sentence);; + }; + static DWORD SelectedProcessId; + auto currthread=Host::GetThread(host->currentselect); + SelectedProcessId=(currthread!=0)?currthread->tp.processId:0; + DWORD (*GetSelectedProcessId)() = [] { return SelectedProcessId; }; + + return + { { + { "HostHWND",(int64_t)host->winId }, + { "toclipboard", host->check_toclipboard }, + { "current select", &thread == currthread }, + { "text number", thread.handle }, + { "process id", thread.tp.processId }, + { "hook address", (int64_t)thread.tp.addr }, + { "text handle", thread.handle }, + { "text name", (int64_t)thread.name.c_str() }, + { "add sentence", (int64_t)AddSentence }, + { "add text", (int64_t)AddText }, + { "get selected process id", (int64_t)GetSelectedProcessId }, + { "void (*AddSentence)(int64_t number, const wchar_t* sentence)", (int64_t)AddSentence }, + { "void (*AddText)(int64_t number, const wchar_t* text)", (int64_t)AddText }, + { "DWORD (*GetSelectedProcessId)()", (int64_t)GetSelectedProcessId }, + { nullptr, 0 } // nullptr marks end of info array + } }; } \ No newline at end of file diff --git a/LunaHost/GUI/pluginmanager.h b/LunaHost/GUI/pluginmanager.h index fc8412d..573e210 100644 --- a/LunaHost/GUI/pluginmanager.h +++ b/LunaHost/GUI/pluginmanager.h @@ -1,5 +1,8 @@ +#ifndef LUNA_PLUGINMANAGER_H +#define LUNA_PLUGINMANAGER_H #include"Plugin/plugindef.h" #include"textthread.h" +class LunaHost; class pluginmanager{ std::vector>OnNewSentenceS; std::optional> checkisvalidplugin(const std::wstring&); @@ -8,9 +11,13 @@ class pluginmanager{ std::wstring pluginfilename; concurrency::reader_writer_lock OnNewSentenceSLock; bool checkisdump(LPVOID); + LunaHost* host; + std::array GetSentenceInfo(TextThread& thread); public: - pluginmanager(); - bool dispatch(const InfoForExtension*, std::wstring& sentence); + pluginmanager(LunaHost*); + bool dispatch(TextThread&, std::wstring& sentence); bool addplugin(const std::wstring&); + std::optionalselectpluginfile(); }; +#endif \ No newline at end of file