This commit is contained in:
恍兮惚兮 2024-03-29 15:57:38 +08:00
parent 86cb1ec962
commit c1230d2271
5 changed files with 39 additions and 19 deletions

View File

@ -71,6 +71,8 @@
#define LblPluginNotify L"Qt Plugins Will be Loaded at Start Only."
#define LblPluginRemove L"Remove Plugins Will be Useful after Restart."
#define InvalidPlugin L"Invalid Plugin!"
#define InvalidDll L"Invalid Dll!"
#define InvalidDump L"Dumplicated!"
#define MsgError L"Error"
#define SEARCH_CJK L"Search for Chinese/Japanese/Korean"
#define HS_SETTINGS L"Settings"

View File

@ -71,6 +71,8 @@
#define LblPluginNotify L"依赖于Qt的插件仅会在启动时载入。"
#define LblPluginRemove L"移除插件要在重启后生效。"
#define InvalidPlugin L"插件无效!"
#define InvalidDll L"Dll无效"
#define InvalidDump L"重复!"
#define MsgError L"错误"
#define SEARCH_CJK L"搜索中文/日文/韩文"
#define HS_SETTINGS L"设置"

View File

@ -427,11 +427,18 @@ Pluginwindow::Pluginwindow(mainwindow*p,Pluginmanager* pl):mainwindow(p){
listplugins->add_menu(MenuAddPlugin,[&](){
if(auto f=pluginmanager->selectpluginfile())
if(pluginmanager->addplugin(f.value())){
switch (auto res=pluginmanager->addplugin(f.value()))
{
case addpluginresult::success:
listadd(f.value());
}
else{
MessageBoxW(winId,InvalidPlugin,MsgError,0);
break;
default:
std::map<addpluginresult,LPCWSTR> errorlog={
{addpluginresult::isnotaplugins,InvalidPlugin},
{addpluginresult::invaliddll,InvalidDll},
{addpluginresult::dumplicate,InvalidDump},
};
MessageBoxW(winId,errorlog[res],MsgError,0);
}
});

View File

@ -214,9 +214,9 @@ std::optional<LPVOID> Pluginmanager::checkisvalidplugin(const std::wstring& pl){
}
return OnNewSentence ;
}
bool Pluginmanager::checkisdump(LPVOID ptr){
bool Pluginmanager::checkisdump(const std::wstring& dll){
for(auto& p:OnNewSentenceS){
if(p.second==ptr)return true;
if(p.first==dll)return true;
}
return false;
}
@ -312,27 +312,30 @@ bool qtchecker(const std::set<std::string>& dll)
return true;
return false;
}
bool Pluginmanager::addplugin(const std::wstring& p){
addpluginresult Pluginmanager::addplugin(const std::wstring& p){
auto importtable=getimporttable(p);
if(importtable.empty())return false;
if(importtable.empty())return addpluginresult::invaliddll;
auto isQt=qtchecker(importtable);
LPVOID plugin;
if(isQt){
plugin=0;
}
else{
plugin=GetProcAddress(LoadLibraryW(p.c_str()),"OnNewSentence");
if(!plugin)return false;
auto base=LoadLibraryW(p.c_str());
if(base==0)return addpluginresult::invaliddll;
plugin=GetProcAddress(base,"OnNewSentence");
if(!plugin)return addpluginresult::isnotaplugins;
}
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;
if(checkisdump(p))return addpluginresult::dumplicate;
PluginRank.push_back(p);
//std::swap(PluginRank.end()-2,PluginRank.end()-1);
OnNewSentenceS[p]=plugin;
host->configs->pluginsadd({p,isQt});
return addpluginresult::success;
}

View File

@ -3,11 +3,17 @@
#include"Plugin/plugindef.h"
#include"textthread.h"
class LunaHost;
enum class addpluginresult{
success,
invaliddll,
isnotaplugins,
dumplicate
};
class Pluginmanager{
std::unordered_map<std::wstring,LPVOID>OnNewSentenceS;
std::optional<LPVOID> checkisvalidplugin(const std::wstring&);
concurrency::reader_writer_lock OnNewSentenceSLock;
bool checkisdump(LPVOID);
bool checkisdump(const std::wstring&);
LunaHost* host;
std::array<InfoForExtension, 20> GetSentenceInfo(TextThread& thread);
void loadqtdlls(std::vector<std::wstring>&collectQtplugs);
@ -15,7 +21,7 @@ public:
std::vector<std::wstring>PluginRank;
Pluginmanager(LunaHost*);
bool dispatch(TextThread&, std::wstring& sentence);
bool addplugin(const std::wstring&);
addpluginresult addplugin(const std::wstring&);
void swaprank(int,int);
void remove(const std::wstring&);
std::optional<std::wstring>selectpluginfile();