This commit is contained in:
恍兮惚兮 2024-03-27 20:36:43 +08:00
parent d0e2bb27c2
commit 07083bd5ec
5 changed files with 98 additions and 25 deletions

View File

@ -61,12 +61,12 @@
#define MenuRemeberSelect L"Remeber Hook Selection"
#define MenuForgetSelect L"Forget Hook Selection"
#define MenuAddPlugin L"Add Plugin"
#define MenuAddQtPlugin L"Add Qt Plugin"
#define MenuRemovePlugin L"Remove Plugin"
#define MenuPluginRankUp L"Up"
#define MenuPluginRankDown L"Down"
#define DefaultFont L"Arial"
#define InVaildPlugin L"InVaild Plugin"
#define CantLoadQtLoader L"Can't Load QtLoader.dll"
#define LblPluginNotify L"Qt Plugins Will be Loaded at Start Only."
#define LblPluginRemove L"Remove Plugins Will be Useful after Restart."
#define LblPluginRemove L"Remove Plugins Will be Useful after Restart."
#define InvalidPlugin L"Invalid Plugin!"
#define MsgError L"Error"

View File

@ -61,12 +61,12 @@
#define MenuRemeberSelect L"记住选择的钩子"
#define MenuForgetSelect L"忘掉选择的钩子"
#define MenuAddPlugin L"添加插件"
#define MenuAddQtPlugin L"添加依赖Qt的插件"
#define MenuRemovePlugin L"移除插件"
#define MenuPluginRankUp L"上移"
#define MenuPluginRankDown L"下移"
#define DefaultFont L"微软雅黑"
#define InVaildPlugin L"无效的插件"
#define CantLoadQtLoader L"无法加载QtLoader.dll"
#define LblPluginNotify L"依赖于Qt的插件仅会在启动时载入。"
#define LblPluginRemove L"移除插件要在重启后生效。"
#define LblPluginRemove L"移除插件要在重启后生效。"
#define InvalidPlugin L"插件无效!"
#define MsgError L"错误"

View File

@ -402,14 +402,12 @@ Pluginwindow::Pluginwindow(mainwindow*p,Pluginmanager* pl):mainwindow(p){
};
listplugins = new listbox(this,10, 10,360,340);
#define IDM_ADD_PLUGIN 1004
#define IDM_ADD_QT_PLUGIN 1005
#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_ADD_QT_PLUGIN, MenuAddQtPlugin);
AppendMenu(hMenu, MF_STRING, IDM_REMOVE_PLUGIN, MenuRemovePlugin);
AppendMenu(hMenu, MF_STRING, IDM_RANK_UP, MenuPluginRankUp);
AppendMenu(hMenu, MF_STRING, IDM_RANK_DOWN, MenuPluginRankDown);
@ -439,13 +437,15 @@ Pluginwindow::Pluginwindow(mainwindow*p,Pluginmanager* pl):mainwindow(p){
break;
}
case IDM_ADD_PLUGIN:
case IDM_ADD_QT_PLUGIN:
{
auto f=pluginmanager->selectpluginfile();
if(f.has_value()){
if(pluginmanager->addplugin(f.value(),LOWORD(wparam)==IDM_ADD_QT_PLUGIN)){
if(pluginmanager->addplugin(f.value())){
listadd(f.value());
}
else{
MessageBoxW(winId,InvalidPlugin,MsgError,0);
}
}
break;
}

View File

@ -149,26 +149,99 @@ void Pluginmanager::swaprank(int a,int b){
PluginRank[a]=_b;
host->configs->pluginrankswap(a,b);
}
bool Pluginmanager::addplugin(const std::wstring& p,bool isQt){
DWORD Rva2Offset(DWORD rva, PIMAGE_SECTION_HEADER psh, PIMAGE_NT_HEADERS pnt)
{
size_t i = 0;
PIMAGE_SECTION_HEADER pSeh;
if (rva == 0)
{
return (rva);
}
pSeh = psh;
for (i = 0; i < pnt->FileHeader.NumberOfSections; i++)
{
if (rva >= pSeh->VirtualAddress && rva < pSeh->VirtualAddress +
pSeh->Misc.VirtualSize)
{
break;
}
pSeh++;
}
return (rva - pSeh->VirtualAddress + pSeh->PointerToRawData);
}
std::set<std::string> getimporttable(const std::wstring&pe){
AutoHandle handle = CreateFile(pe.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(!handle)return {};
DWORD byteread, size = GetFileSize(handle, NULL);
PVOID virtualpointer = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
if(!virtualpointer) return {};
ReadFile(handle, virtualpointer, size, &byteread, NULL);
struct __{
PVOID _ptr;DWORD size;
__(PVOID ptr,DWORD sz):_ptr(ptr),size(sz){}
~__(){
VirtualFree(_ptr, size, MEM_DECOMMIT);
}
}_(virtualpointer,size);
if(PIMAGE_DOS_HEADER(virtualpointer)->e_magic!=0x5a4d)
return {};
PIMAGE_NT_HEADERS ntheaders = (PIMAGE_NT_HEADERS)(PCHAR(virtualpointer) + PIMAGE_DOS_HEADER(virtualpointer)->e_lfanew);
auto magic=ntheaders->OptionalHeader.Magic;
if(x64 && (magic!=IMAGE_NT_OPTIONAL_HDR64_MAGIC))return {};
if((!x64)&&(magic!=IMAGE_NT_OPTIONAL_HDR32_MAGIC))return {};
PIMAGE_SECTION_HEADER pSech = IMAGE_FIRST_SECTION(ntheaders);//Pointer to first section header
PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor; //Pointer to import descriptor
if (ntheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size == 0)/*if size of the table is 0 - Import Table does not exist */
return {};
std::set<std::string> ret;
pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD_PTR)virtualpointer + \
Rva2Offset(ntheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress, pSech, ntheaders));
while (pImportDescriptor->Name != NULL)
{
//Get the name of each DLL
ret.insert((PCHAR)((DWORD_PTR)virtualpointer + Rva2Offset(pImportDescriptor->Name, pSech, ntheaders)));
pImportDescriptor++; //advance to next IMAGE_IMPORT_DESCRIPTOR
}
return ret;
}
bool qtchecker(const std::set<std::string>& dll)
{
for(auto qt5:{"Qt5Widgets.dll","Qt5Gui.dll","Qt5Core.dll"})
if(dll.find(qt5)!=dll.end())
return true;
return false;
}
bool Pluginmanager::addplugin(const std::wstring& p){
auto importtable=getimporttable(p);
if(importtable.empty())return false;
auto isQt=qtchecker(importtable);
if(isQt){
host->configs->pluginsadd({p,isQt});
return true;
}
auto plugin=GetProcAddress(LoadLibraryW(p.c_str()),"OnNewSentence");
if(plugin){
std::scoped_lock lock(OnNewSentenceSLock);
if(!checkisdump(plugin)){
PluginRank.push_back(p);
//std::swap(PluginRank.end()-2,PluginRank.end()-1);
OnNewSentenceS[p]=plugin;
host->configs->pluginsadd({p,isQt});
}
return true;
}
else{
MessageBoxW(host->winId,InVaildPlugin,L"Error",0);
return false;
if(!plugin)return false;
std::scoped_lock lock(OnNewSentenceSLock);
if(!checkisdump(plugin)){
PluginRank.push_back(p);
//std::swap(PluginRank.end()-2,PluginRank.end()-1);
OnNewSentenceS[p]=plugin;
host->configs->pluginsadd({p,isQt});
}
return true;
}

View File

@ -15,7 +15,7 @@ public:
std::vector<std::wstring>PluginRank;
Pluginmanager(LunaHost*);
bool dispatch(TextThread&, std::wstring& sentence);
bool addplugin(const std::wstring&,bool isQt=false);
bool addplugin(const std::wstring&);
void swaprank(int,int);
void remove(const std::wstring&);
std::optional<std::wstring>selectpluginfile();