2024-02-08 16:18:33 +08:00
|
|
|
#include"pluginmanager.h"
|
|
|
|
#include<filesystem>
|
2024-02-08 16:45:23 +08:00
|
|
|
#include"Plugin/plugindef.h"
|
2024-02-08 16:18:33 +08:00
|
|
|
#include<fstream>
|
2024-02-08 19:12:20 +08:00
|
|
|
#include <commdlg.h>
|
|
|
|
#include"LunaHost.h"
|
2024-02-08 16:45:23 +08:00
|
|
|
#include"Lang/Lang.h"
|
2024-02-08 19:12:20 +08:00
|
|
|
#include"host.h"
|
2024-02-09 09:25:26 +08:00
|
|
|
|
2024-02-08 19:12:20 +08:00
|
|
|
std::optional<std::wstring>SelectFile(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 {};
|
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
typedef HMODULE*(*QtLoadLibrary_t)(LPWSTR*,int);
|
2024-02-09 19:38:12 +08:00
|
|
|
QtLoadLibrary_t loadqtloader(const std::filesystem::path&pluginpath){
|
|
|
|
auto QtLoaderPath=pluginpath/"QtLoader.dll";
|
2024-02-09 09:25:26 +08:00
|
|
|
auto helper=LoadLibrary(QtLoaderPath.c_str());
|
|
|
|
if(helper==0)return 0;
|
|
|
|
auto QtLoadLibrary = (QtLoadLibrary_t)GetProcAddress(helper, "QtLoadLibrary");
|
|
|
|
return QtLoadLibrary;
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
void Pluginmanager::loadqtdlls(std::vector<std::wstring>&collectQtplugs){
|
2024-02-09 19:38:12 +08:00
|
|
|
if(collectQtplugs.size()==0)return;
|
|
|
|
auto pluginpath=std::filesystem::current_path()/(x64?"plugin64":"plugin32");
|
2024-02-09 23:45:10 +08:00
|
|
|
wchar_t env[65535];
|
|
|
|
GetEnvironmentVariableW(L"PATH",env,65535);
|
|
|
|
auto envs=std::wstring(env);
|
|
|
|
envs+=L";";envs+=pluginpath;
|
|
|
|
for(auto&p:collectQtplugs){
|
|
|
|
envs+=L";";envs+=std::filesystem::path(p).parent_path();
|
|
|
|
}
|
|
|
|
SetEnvironmentVariableW(L"PATH",envs.c_str());
|
2024-02-09 19:38:12 +08:00
|
|
|
|
|
|
|
auto QtLoadLibrary = loadqtloader(pluginpath);
|
2024-02-09 09:25:26 +08:00
|
|
|
if(!QtLoadLibrary){
|
|
|
|
MessageBoxW(host->winId,CantLoadQtLoader,L"Error",0);
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
std::vector<wchar_t*>saves;
|
|
|
|
for(auto&p:collectQtplugs){
|
|
|
|
auto str=new wchar_t[p.size()+1];
|
|
|
|
wcscpy(str,p.c_str());
|
|
|
|
saves.emplace_back(str);
|
|
|
|
}
|
|
|
|
auto modules=QtLoadLibrary(saves.data(),collectQtplugs.size());
|
|
|
|
for(auto str:saves)delete str;
|
|
|
|
for(int i=0;i<collectQtplugs.size();i++){
|
|
|
|
OnNewSentenceS[collectQtplugs[i]]=GetProcAddress(modules[i],"OnNewSentence");
|
|
|
|
}
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
Pluginmanager::Pluginmanager(LunaHost* _host):host(_host){
|
2024-02-08 16:18:33 +08:00
|
|
|
try {
|
|
|
|
std::scoped_lock lock(OnNewSentenceSLock);
|
2024-02-09 19:38:12 +08:00
|
|
|
|
2024-02-09 09:47:22 +08:00
|
|
|
OnNewSentenceS[L"InternalClipBoard"]=GetProcAddress(GetModuleHandle(0),"OnNewSentence");//内部链接的剪贴板插件
|
2024-02-09 09:25:26 +08:00
|
|
|
auto plgs=host->configs->pluginsget();
|
|
|
|
std::vector<std::wstring>collectQtplugs;
|
|
|
|
for (auto i=0;i<plgs.size();i++) {
|
|
|
|
bool isqt=plgs[i]["isQt"];
|
|
|
|
auto path=StringToWideString(plgs[i]["path"]);
|
|
|
|
PluginRank.push_back(path);
|
|
|
|
OnNewSentenceS[path]=0;
|
|
|
|
if(isqt){
|
|
|
|
collectQtplugs.push_back((path));
|
|
|
|
continue;
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
OnNewSentenceS[path]=GetProcAddress(LoadLibraryW(path.c_str()),"OnNewSentence");
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
2024-02-09 19:38:12 +08:00
|
|
|
loadqtdlls(collectQtplugs);
|
2024-02-09 09:47:22 +08:00
|
|
|
|
2024-02-08 16:18:33 +08:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::cerr << "Error: " << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:25:26 +08:00
|
|
|
bool Pluginmanager::dispatch(TextThread& thread, std::wstring& sentence){
|
2024-02-08 19:12:20 +08:00
|
|
|
auto sentenceInfo=GetSentenceInfo(thread).data();
|
2024-02-08 16:18:33 +08:00
|
|
|
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);
|
2024-02-09 19:38:12 +08:00
|
|
|
|
|
|
|
bool interupt=false;
|
|
|
|
for (const auto& pathl :{std::vector<std::wstring>{L"InternalClipBoard"}, PluginRank}){
|
|
|
|
for(const auto&path:pathl){
|
|
|
|
auto funptr=OnNewSentenceS[path];
|
|
|
|
if(funptr==0)continue;
|
|
|
|
if (!*(sentenceBuffer = ((OnNewSentence_t)funptr)(sentenceBuffer, sentenceInfo))){interupt=true;break;};
|
|
|
|
}
|
|
|
|
if(interupt)break;
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
2024-02-09 19:38:12 +08:00
|
|
|
|
2024-02-08 16:18:33 +08:00
|
|
|
sentence = sentenceBuffer;
|
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
|
|
|
return !sentence.empty();
|
|
|
|
}
|
2024-02-08 16:45:23 +08:00
|
|
|
|
2024-02-09 09:25:26 +08:00
|
|
|
std::optional<LPVOID> Pluginmanager::checkisvalidplugin(const std::wstring& pl){
|
2024-02-08 16:18:33 +08:00
|
|
|
auto path=std::filesystem::path(pl);
|
|
|
|
if (!std::filesystem::exists(path))return{};
|
|
|
|
if (!std::filesystem::is_regular_file(path))return{};
|
2024-02-08 17:11:33 +08:00
|
|
|
auto appendix=stolower(path.extension().wstring());
|
2024-02-08 16:45:23 +08:00
|
|
|
if((appendix!=std::wstring(L".dll"))&&(appendix!=std::wstring(L".xdll")))return {};
|
2024-02-08 16:18:33 +08:00
|
|
|
auto dll=LoadLibraryW(pl.c_str());
|
|
|
|
if(!dll)return {};
|
2024-02-09 09:25:26 +08:00
|
|
|
auto OnNewSentence=GetProcAddress(LoadLibraryW(pl.c_str()),"OnNewSentence");
|
2024-02-08 16:18:33 +08:00
|
|
|
if(!OnNewSentence){
|
|
|
|
FreeLibrary(dll);
|
|
|
|
return {};
|
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
return OnNewSentence ;
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
bool Pluginmanager::checkisdump(LPVOID ptr){
|
2024-02-08 16:18:33 +08:00
|
|
|
for(auto& p:OnNewSentenceS){
|
|
|
|
if(p.second==ptr)return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
void Pluginmanager::remove(const std::wstring& ss){
|
|
|
|
std::scoped_lock lock(OnNewSentenceSLock);
|
|
|
|
auto it = std::find(PluginRank.begin(), PluginRank.end(), ss);
|
|
|
|
if (it != PluginRank.end()) {
|
|
|
|
PluginRank.erase(it);
|
|
|
|
}
|
|
|
|
host->configs->pluginsremove(WideStringToString(ss));
|
|
|
|
}
|
|
|
|
std::optional<std::wstring>Pluginmanager::selectpluginfile(){
|
2024-02-08 19:12:20 +08:00
|
|
|
return SelectFile(host->winId,L"Plugin Files\0*.dll;*.xdll\0");
|
|
|
|
}
|
2024-02-09 19:38:12 +08:00
|
|
|
void Pluginmanager::swaprank(int a,int b){
|
|
|
|
std::scoped_lock lock(OnNewSentenceSLock);
|
|
|
|
auto _b=PluginRank[b];
|
|
|
|
PluginRank[b]=PluginRank[a];
|
|
|
|
PluginRank[a]=_b;
|
|
|
|
host->configs->pluginrankswap(a,b);
|
|
|
|
}
|
2024-02-09 09:25:26 +08:00
|
|
|
bool Pluginmanager::addplugin(const std::wstring& p,bool isQt){
|
|
|
|
if(isQt){
|
|
|
|
host->configs->pluginsadd(WideStringToString(p),isQt);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
auto plugin=GetProcAddress(LoadLibraryW(p.c_str()),"OnNewSentence");
|
|
|
|
if(plugin){
|
2024-02-08 16:18:33 +08:00
|
|
|
std::scoped_lock lock(OnNewSentenceSLock);
|
2024-02-09 09:25:26 +08:00
|
|
|
if(!checkisdump(plugin)){
|
|
|
|
PluginRank.push_back(p);
|
|
|
|
//std::swap(PluginRank.end()-2,PluginRank.end()-1);
|
|
|
|
OnNewSentenceS[p]=plugin;
|
|
|
|
host->configs->pluginsadd(WideStringToString(p),isQt);
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else{
|
2024-02-08 19:12:20 +08:00
|
|
|
MessageBoxW(host->winId,InVaildPlugin,L"Error",0);
|
2024-02-08 16:18:33 +08:00
|
|
|
return false;
|
|
|
|
}
|
2024-02-08 19:12:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-09 09:25:26 +08:00
|
|
|
std::array<InfoForExtension, 20> Pluginmanager::GetSentenceInfo(TextThread& thread)
|
2024-02-08 19:12:20 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
} };
|
2024-02-08 16:18:33 +08:00
|
|
|
}
|