From bf9a4f63201bc429e08fa36c3c50422f3ec84227 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: Fri, 29 Mar 2024 15:07:43 +0800 Subject: [PATCH] refactor --- LunaHost/GUI/LunaHost.cpp | 173 ++++++++++++--------------------- LunaHost/GUI/LunaHost.h | 2 + LunaHost/GUI/controls.cpp | 17 ++++ LunaHost/GUI/controls.h | 12 ++- LunaHost/GUI/pluginmanager.cpp | 10 +- LunaHost/GUI/window.cpp | 4 +- 6 files changed, 100 insertions(+), 118 deletions(-) diff --git a/LunaHost/GUI/LunaHost.cpp b/LunaHost/GUI/LunaHost.cpp index 443f012..8429ed8 100644 --- a/LunaHost/GUI/LunaHost.cpp +++ b/LunaHost/GUI/LunaHost.cpp @@ -101,7 +101,11 @@ void LunaHost::on_proc_connect(DWORD pid) autoattachexes.insert(WideStringToString(pexe.value())); } } - +TextThread* LunaHost::getcurrthread(){ + auto handle = g_hListBox_listtext->getdata(g_hListBox_listtext->currentidx()); + auto tt=Host::GetThread(handle); + return tt; +} LunaHost::LunaHost(){ configs=new confighelper; @@ -170,63 +174,33 @@ LunaHost::LunaHost(){ g_showtexts->settext(get); g_showtexts->scrolltoend(); }; + g_hListBox_listtext->add_menu(MenuCopyHookCode,[&](){if(auto tt=getcurrthread())toclipboard(std::wstring(tt->hp.hookcode));}); -#define IDM_REMOVE_HOOK 1001 -#define IDM_DETACH_PROCESS 1002 -#define IDM_COPY_HOOKCODE 1003 -#define IDM_REMEMBER_SELECTION 1004 -#define IDM_FORGET_SELECTION 1005 - g_hListBox_listtext->oncontextmenu=[](){ - HMENU hMenu = CreatePopupMenu(); - AppendMenu(hMenu, MF_STRING, IDM_COPY_HOOKCODE, MenuCopyHookCode); - AppendMenu(hMenu, MF_STRING, IDM_REMOVE_HOOK, MenuRemoveHook); - AppendMenu(hMenu, MF_STRING, IDM_DETACH_PROCESS, MenuDetachProcess); - AppendMenu(hMenu, MF_STRING, IDM_REMEMBER_SELECTION, MenuRemeberSelect); - AppendMenu(hMenu, MF_STRING, IDM_FORGET_SELECTION, MenuForgetSelect); - return hMenu; - }; - g_hListBox_listtext->oncontextmenucallback=[&](WPARAM wparam){ + g_hListBox_listtext->add_menu_sep(); + g_hListBox_listtext->add_menu(MenuRemoveHook,[&](){if(auto tt=getcurrthread())Host::RemoveHook(tt->tp.processId,tt->tp.addr);}); - auto handle = g_hListBox_listtext->getdata(g_hListBox_listtext->currentidx()); - auto tt=Host::GetThread(handle); - if(tt==0)return; - switch (LOWORD(wparam)) { - - case IDM_COPY_HOOKCODE: - toclipboard(std::wstring(tt->hp.hookcode)); - break; - case IDM_DETACH_PROCESS: - Host::DetachProcess(tt->tp.processId); - userdetachedpids.insert(tt->tp.processId); - break; - case IDM_REMOVE_HOOK: - Host::RemoveHook(tt->tp.processId,tt->tp.addr); - break; - case IDM_FORGET_SELECTION: - case IDM_REMEMBER_SELECTION: - { - - if(auto pexe=gmf(tt->tp.processId)) - { - auto pexev=WideStringToString(pexe.value()); - if(LOWORD(wparam)==IDM_REMEMBER_SELECTION) - { - - savedhookcontext[pexev]={ - {"hookcode",WideStringToString(tt->hp.hookcode)}, - {"ctx1",tt->tp.ctx}, - {"ctx2",tt->tp.ctx2}, - }; - } - else if(LOWORD(wparam)==IDM_FORGET_SELECTION) - { - savedhookcontext.erase(pexev); - } - } - } - break; + g_hListBox_listtext->add_menu(MenuDetachProcess,[&](){ + if(auto tt=getcurrthread()){ + Host::DetachProcess(tt->tp.processId); + userdetachedpids.insert(tt->tp.processId); } - }; + }); + g_hListBox_listtext->add_menu_sep(); + g_hListBox_listtext->add_menu(MenuRemeberSelect,[&](){ + if(auto tt=getcurrthread()) + if(auto pexe=gmf(tt->tp.processId)) + savedhookcontext[WideStringToString(pexe.value())]={ + {"hookcode",WideStringToString(tt->hp.hookcode)}, + {"ctx1",tt->tp.ctx}, + {"ctx2",tt->tp.ctx2}, + }; + }); + g_hListBox_listtext->add_menu(MenuForgetSelect,[&](){ + if(auto tt=getcurrthread()) + if(auto pexe=gmf(tt->tp.processId)) + savedhookcontext.erase(WideStringToString(pexe.value())); + }); + g_showtexts = new multilineedit(this); g_showtexts->setreadonly(true); @@ -425,6 +399,21 @@ Settingwindow::Settingwindow(LunaHost* host):mainwindow(host){ void Pluginwindow::on_size(int w,int h){ listplugins->setgeo(10,80,w-20,h-90); } +void Pluginwindow::pluginrankmove(int moveoffset){ + auto idx=listplugins->currentidx(); + if(idx==-1)return; + auto idx2=idx+moveoffset; + auto a=min(idx,idx2),b=max(idx,idx2); + if(a<0||b>=listplugins->count())return; + pluginmanager->swaprank(a,b); + + auto pa=((LPCWSTR)listplugins->getdata(a)); + auto pb=((LPCWSTR)listplugins->getdata(b)); + + listplugins->deleteitem(a); + listplugins->insertitem(b,std::filesystem::path(pa).stem()); + listplugins->setdata(b,(LONG_PTR)pa); +} Pluginwindow::Pluginwindow(mainwindow*p,Pluginmanager* pl):mainwindow(p){ pluginmanager=pl; (new label(this,LblPluginNotify))->setgeo(10,10,500,30); @@ -435,64 +424,28 @@ Pluginwindow::Pluginwindow(mainwindow*p,Pluginmanager* pl):mainwindow(p){ listplugins->setdata(idx,(LONG_PTR)_s); }; listplugins = new listbox(this); -#define IDM_ADD_PLUGIN 1004 -#define IDM_REMOVE_PLUGIN 1006 -#define IDM_RANK_UP 1007 -#define IDM_RANK_DOWN 1008 - listplugins->oncontextmenu=[](){ - HMENU hMenu = CreatePopupMenu(); - AppendMenu(hMenu, MF_STRING, IDM_ADD_PLUGIN, MenuAddPlugin); - AppendMenu(hMenu, MF_STRING, IDM_REMOVE_PLUGIN, MenuRemovePlugin); - AppendMenu(hMenu, MF_STRING, IDM_RANK_UP, MenuPluginRankUp); - AppendMenu(hMenu, MF_STRING, IDM_RANK_DOWN, MenuPluginRankDown); - return hMenu; - }; - listplugins->oncontextmenucallback=[&](WPARAM wparam){ - switch (LOWORD(wparam)) { - case IDM_RANK_DOWN: - case IDM_RANK_UP: - { - auto idx=listplugins->currentidx(); - if(idx==-1)break; - auto idx2=idx+( - (LOWORD(wparam)==IDM_RANK_UP)?-1:1 - ); - auto a=min(idx,idx2),b=max(idx,idx2); - if(a<0||b>=listplugins->count())break; - pluginmanager->swaprank(a,b); - - auto pa=((LPCWSTR)listplugins->getdata(a)); - auto pb=((LPCWSTR)listplugins->getdata(b)); - - listplugins->deleteitem(a); - listplugins->insertitem(b,std::filesystem::path(pa).stem()); - listplugins->setdata(b,(LONG_PTR)pa); - break; + listplugins->add_menu(MenuAddPlugin,[&](){ + if(auto f=pluginmanager->selectpluginfile()) + if(pluginmanager->addplugin(f.value())){ + listadd(f.value()); } - case IDM_ADD_PLUGIN: - { - auto f=pluginmanager->selectpluginfile(); - if(f.has_value()){ - if(pluginmanager->addplugin(f.value())){ - listadd(f.value()); - } - else{ - MessageBoxW(winId,InvalidPlugin,MsgError,0); - } - } - break; + else{ + MessageBoxW(winId,InvalidPlugin,MsgError,0); } - case IDM_REMOVE_PLUGIN: - { - auto idx=listplugins->currentidx(); - if(idx==-1)break; - pluginmanager->remove((LPCWSTR)listplugins->getdata(idx)); - listplugins->deleteitem(idx); - break; - } - } - }; + }); + + listplugins->add_menu(MenuRemovePlugin,[&](){ + auto idx=listplugins->currentidx(); + if(idx==-1)return; + pluginmanager->remove((LPCWSTR)listplugins->getdata(idx)); + listplugins->deleteitem(idx); + }); + + listplugins->add_menu_sep(); + listplugins->add_menu(MenuPluginRankUp,std::bind(&Pluginwindow::pluginrankmove,this,-1)); + listplugins->add_menu(MenuPluginRankDown,std::bind(&Pluginwindow::pluginrankmove,this,1)); + for(int i=0;iPluginRank.size();i++){ listadd(pluginmanager->PluginRank[i]); } diff --git a/LunaHost/GUI/LunaHost.h b/LunaHost/GUI/LunaHost.h index c572c2d..869f528 100644 --- a/LunaHost/GUI/LunaHost.h +++ b/LunaHost/GUI/LunaHost.h @@ -10,6 +10,7 @@ class Pluginwindow:public mainwindow{ public: Pluginwindow(mainwindow*,Pluginmanager*); void on_size(int w,int h); + void pluginrankmove(int); }; class Settingwindow:public mainwindow{ checkbox* ckbfilterrepeat; @@ -100,6 +101,7 @@ class LunaHost:public mainwindow{ void on_thread_delete(TextThread& thread); void on_proc_connect(DWORD pid); public: + TextThread* getcurrthread(); confighelper* configs; int64_t currentselect=0; bool check_toclipboard; diff --git a/LunaHost/GUI/controls.cpp b/LunaHost/GUI/controls.cpp index 9eb7f44..8b5acc1 100644 --- a/LunaHost/GUI/controls.cpp +++ b/LunaHost/GUI/controls.cpp @@ -313,4 +313,21 @@ gridlayout::gridlayout(int row,int col):control(0){ } void gridlayout::setmargin(int m){ margin=m; +} +void control::menu_dispatch(WPARAM wparam){ + auto idx=LOWORD(wparam); + menu_callbacks[idx].callback(); +} +HMENU control::load_menu(){ + HMENU hMenu = CreatePopupMenu(); + for(int i=0;i callback, UINT type){ + menu_callbacks.push_back({callback,type,str}); +} +void control::add_menu_sep(){ + menu_callbacks.push_back({[](){},MF_SEPARATOR,L""}); } \ No newline at end of file diff --git a/LunaHost/GUI/controls.h b/LunaHost/GUI/controls.h index 55b1a95..dd1de2b 100644 --- a/LunaHost/GUI/controls.h +++ b/LunaHost/GUI/controls.h @@ -8,8 +8,16 @@ class control:public basewindow{ control(mainwindow*); virtual void dispatch(WPARAM); virtual void dispatch_2(WPARAM wParam, LPARAM lParam); - std::functiononcontextmenu=[](){return (HMENU)nullptr;}; - std::functiononcontextmenucallback=[](WPARAM){}; + void menu_dispatch(WPARAM); + HMENU load_menu(); + struct menuinfos{ + std::function callback; + UINT type; + std::wstring str; + }; + std::vectormenu_callbacks; + void add_menu(const std::wstring&,std::function callback, UINT type=MF_STRING); + void add_menu_sep(); }; class button:public control{ diff --git a/LunaHost/GUI/pluginmanager.cpp b/LunaHost/GUI/pluginmanager.cpp index 63bab85..eabf410 100644 --- a/LunaHost/GUI/pluginmanager.cpp +++ b/LunaHost/GUI/pluginmanager.cpp @@ -315,12 +315,14 @@ bool Pluginmanager::addplugin(const std::wstring& p){ auto importtable=getimporttable(p); if(importtable.empty())return false; auto isQt=qtchecker(importtable); + LPVOID plugin; if(isQt){ - host->configs->pluginsadd({p,isQt}); - return true; + plugin=0; } - auto plugin=GetProcAddress(LoadLibraryW(p.c_str()),"OnNewSentence"); - if(!plugin)return false; + else{ + plugin=GetProcAddress(LoadLibraryW(p.c_str()),"OnNewSentence"); + if(!plugin)return false; + } std::scoped_lock lock(OnNewSentenceSLock); if(!checkisdump(plugin)){ diff --git a/LunaHost/GUI/window.cpp b/LunaHost/GUI/window.cpp index 87051ba..034a1e4 100644 --- a/LunaHost/GUI/window.cpp +++ b/LunaHost/GUI/window.cpp @@ -77,7 +77,7 @@ LRESULT mainwindow::wndproc(UINT message, WPARAM wParam, LPARAM lParam){ if(lParam==0){ for(auto ctl:controls){ if(lastcontexthwnd==ctl->winId){ - ctl->oncontextmenucallback(wParam);break; + ctl->menu_dispatch(wParam);break; } } } @@ -94,7 +94,7 @@ LRESULT mainwindow::wndproc(UINT message, WPARAM wParam, LPARAM lParam){ bool succ=false;lastcontexthwnd=0; for(auto ctl:controls){ if((HWND)wParam==ctl->winId){ - auto hm=ctl->oncontextmenu(); + auto hm=ctl->load_menu(); if(hm){ int xPos = LOWORD(lParam); int yPos = HIWORD(lParam);