LunaHook-mirror/LunaHost/GUI/pluginmanager.cpp

488 lines
14 KiB
C++
Raw Normal View History

2024-07-21 19:28:47 +08:00
#include "pluginmanager.h"
#include <filesystem>
#include "Plugin/extension.h"
#include <fstream>
2024-02-08 19:12:20 +08:00
#include <commdlg.h>
2024-07-21 19:28:47 +08:00
#include "LunaHost.h"
#include "Lang/Lang.h"
#include "host.h"
2024-02-09 09:25:26 +08:00
2024-07-21 19:28:47 +08:00
std::optional<std::wstring> SelectFile(HWND hwnd, LPCWSTR lpstrFilter)
{
2024-02-08 19:12:20 +08:00
OPENFILENAME ofn;
2024-07-21 19:28:47 +08:00
wchar_t szFileName[MAX_PATH] = {0};
2024-02-08 19:12:20 +08:00
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = lpstrFilter;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = sizeof(szFileName);
2024-03-04 20:33:11 +08:00
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR;
2024-02-08 19:12:20 +08:00
if (GetOpenFileName(&ofn))
{
return szFileName;
}
2024-07-21 19:28:47 +08:00
else
return {};
2024-02-08 19:12:20 +08:00
}
2024-07-21 19:28:47 +08:00
typedef std::vector<HMODULE> *(*QtLoadLibrary_t)(std::vector<std::wstring> *dlls);
typedef std::vector<HMODULE> *(*QtLoadLibraryBatch_t)(std::vector<std::wstring> *dlls);
typedef void (*QtFreeLibrary_t)(HMODULE hd);
2024-07-21 19:28:47 +08:00
void tryaddqttoenv(std::vector<std::wstring> &collectQtplugs)
{
static HMODULE qt5core = 0;
if (qt5core == 0)
{
wchar_t env[65535];
2024-07-21 19:28:47 +08:00
GetEnvironmentVariableW(L"PATH", env, 65535);
auto envs = std::wstring(env);
for (auto &p : collectQtplugs)
{
envs += L";";
envs += std::filesystem::path(p).parent_path();
}
2024-07-21 19:28:47 +08:00
SetEnvironmentVariableW(L"PATH", envs.c_str());
qt5core = LoadLibrary(L"Qt5Core.dll");
2024-02-09 23:45:10 +08:00
}
}
2024-07-21 19:28:47 +08:00
std::vector<HMODULE> loadqtdllsX(std::vector<std::wstring> &collectQtplugs)
{
if (collectQtplugs.empty())
return {};
tryaddqttoenv(collectQtplugs);
2024-07-21 19:28:47 +08:00
#if 1
HMODULE base = GetModuleHandle(0);
#else
HMODULE base = LoadLibrary((std::filesystem::current_path() / (x64 ? "plugin64" : "plugin32") / "QtLoader.dll").wstring().c_str());
#endif
// auto QtLoadLibrary = (QtLoadLibrary_t)GetProcAddress(base, "QtLoadLibrary");
auto QtLoadLibrary = (QtLoadLibrary_t)GetProcAddress(base, "QtLoadLibraryBatch");
auto modules = QtLoadLibrary(&collectQtplugs);
std::vector<HMODULE> _{*modules};
delete modules;
return _;
}
2024-07-21 19:28:47 +08:00
HMODULE loadqtdllsX(const std::wstring &collectQtplugs)
{
std::vector<std::wstring> _{collectQtplugs};
return loadqtdllsX(_)[0];
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::loadqtdlls(std::vector<std::wstring> &collectQtplugs)
{
auto modules = loadqtdllsX(collectQtplugs);
for (int i = 0; i < collectQtplugs.size(); i++)
{
OnNewSentenceS[collectQtplugs[i]] = {collectQtplugs[i], this, true, modules[i]};
2024-02-09 09:25:26 +08:00
}
2024-02-08 16:18:33 +08:00
}
2024-07-21 19:28:47 +08:00
Pluginmanager::Pluginmanager(LunaHost *_host) : host(_host), configs(_host->configs)
{
try
{
2024-02-08 16:18:33 +08:00
std::scoped_lock lock(OnNewSentenceSLock);
2024-07-21 19:28:47 +08:00
std::vector<std::wstring> collectQtplugs;
for (auto i = 0; i < count(); i++)
{
auto plg = get(i);
bool isqt = plg.isQt;
auto path = plg.wpath();
OnNewSentenceS[path] = {};
if (isqt)
{
if (plg.enable == false)
continue;
2024-02-09 09:25:26 +08:00
collectQtplugs.push_back((path));
2024-02-08 16:18:33 +08:00
}
2024-07-21 19:28:47 +08:00
else
{
auto base = LoadLibraryW(path.c_str());
OnNewSentenceS[path] = {path, this, false, base};
}
2024-02-08 16:18:33 +08:00
}
loadqtdlls(collectQtplugs);
2024-03-26 15:06:17 +08:00
2024-07-21 19:28:47 +08:00
OnNewSentenceS[L"InternalClipBoard"] = {L"", this, false, GetModuleHandle(0)}; // 内部链接的剪贴板插件
}
catch (const std::exception &ex)
{
std::wcerr << "Error: " << ex.what() << std::endl;
2024-02-08 16:18:33 +08:00
}
}
2024-07-21 19:28:47 +08:00
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());
2024-02-08 16:18:33 +08:00
concurrency::reader_writer_lock::scoped_lock_read readLock(OnNewSentenceSLock);
2024-07-21 19:28:47 +08:00
for (int i = 0; i < count() + 1; i++)
{
std::wstring path;
2024-07-21 19:28:47 +08:00
if (i == count())
path = L"InternalClipBoard";
else
{
if (getenable(i) == false)
continue;
path = getname(i);
}
2024-07-21 19:28:47 +08:00
auto funptr = OnNewSentenceS[path].OnNewSentence;
if (funptr == 0)
continue;
2024-04-23 20:01:57 +08:00
if (!*(sentenceBuffer = funptr(sentenceBuffer, sentenceInfo)))
break;
2024-02-08 16:18:33 +08:00
}
2024-07-21 19:28:47 +08:00
sentence = sentenceBuffer;
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
return !sentence.empty();
2024-02-08 16:18:33 +08:00
}
2024-02-08 16:45:23 +08:00
2024-07-21 19:28:47 +08:00
void Pluginmanager::add(const pluginitem &item)
{
configs->configs["plugins"].push_back(item.dump());
}
2024-07-21 19:28:47 +08:00
int Pluginmanager::count()
{
return configs->configs["plugins"].size();
}
2024-07-21 19:28:47 +08:00
pluginitem Pluginmanager::get(int i)
{
return pluginitem{configs->configs["plugins"][i]};
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::set(int i, const pluginitem &item)
{
configs->configs["plugins"][i] = item.dump();
}
2024-07-21 19:28:47 +08:00
pluginitem::pluginitem(const nlohmann::json &js)
{
path = js["path"];
isQt = safequeryjson(js, "isQt", false);
isref = safequeryjson(js, "isref", false);
enable = safequeryjson(js, "enable", true);
vissetting = safequeryjson(js, "vissetting", true);
}
std::wstring pluginitem::wpath()
{
auto wp = StringToWideString(path);
if (isref)
2024-10-25 11:56:24 +08:00
return std::filesystem::absolute(wp);
2024-07-21 19:28:47 +08:00
else
return wp;
}
2024-07-21 19:28:47 +08:00
std::pair<std::wstring, bool> castabs2ref(const std::wstring &p)
{
auto curr = std::filesystem::current_path().wstring();
if (startWith(p, curr))
{
return {p.substr(curr.size() + 1), true};
}
2024-07-21 19:28:47 +08:00
return {p, false};
}
2024-07-21 19:28:47 +08:00
pluginitem::pluginitem(const std::wstring &pabs, bool _isQt)
{
isQt = _isQt;
auto [p, _isref] = castabs2ref(pabs);
isref = _isref;
path = WideStringToString(p);
enable = true;
vissetting = true;
}
nlohmann::json pluginitem::dump() const
{
return {
2024-07-21 19:28:47 +08:00
{"path", path},
{"isQt", isQt},
{"isref", isref},
{"enable", enable},
{"vissetting", vissetting}};
}
bool Pluginmanager::getvisible_setable(int idx)
{
2024-04-24 09:04:02 +08:00
return OnNewSentenceS[getname(idx)].VisSetting;
}
2024-07-21 19:28:47 +08:00
bool Pluginmanager::getvisible(int idx)
{
2024-04-24 09:04:02 +08:00
return get(idx).vissetting;
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::setvisible(int idx, bool vis)
{
auto item = get(idx);
item.vissetting = vis;
set(idx, item);
2024-04-24 09:04:02 +08:00
OnNewSentenceS[getname(idx)].VisSetting(vis);
}
2024-07-21 19:28:47 +08:00
bool Pluginmanager::getenable(int idx)
{
return get(idx).enable;
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::setenable(int idx, bool en)
{
auto item = get(idx);
item.enable = en;
set(idx, item);
}
2024-07-21 19:28:47 +08:00
std::wstring Pluginmanager::getname(int idx)
{
return get(idx).wpath();
}
2024-07-21 19:28:47 +08:00
bool Pluginmanager::checkisdump(const std::wstring &dll)
{
for (auto &p : OnNewSentenceS)
{
if (p.first == dll)
return true;
2024-02-08 16:18:33 +08:00
}
return false;
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::unload(const std::wstring &wss)
{
auto hm = OnNewSentenceS[wss].hmodule;
if (OnNewSentenceS[wss].isQt && hm)
{
((QtFreeLibrary_t)GetProcAddress(GetModuleHandle(0), "QtFreeLibrary"))(hm);
}
else
FreeLibrary(hm);
2024-07-21 19:28:47 +08:00
2024-04-23 20:01:57 +08:00
OnNewSentenceS[wss].clear();
}
2024-07-21 19:28:47 +08:00
void plugindata::clear()
{
hmodule = 0;
OnNewSentence = 0;
VisSetting = 0;
2024-04-23 20:01:57 +08:00
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::remove(const std::wstring &wss)
{
2024-04-23 20:01:57 +08:00
unload(wss);
2024-07-21 19:28:47 +08:00
auto s = WideStringToString(wss);
auto &plgs = configs->configs["plugins"];
auto it = std::remove_if(plgs.begin(), plgs.end(), [&](auto &t)
{
std::string p=t["path"];
2024-10-25 11:56:24 +08:00
return std::filesystem::absolute(p)==std::filesystem::absolute(s); });
plgs.erase(it, plgs.end());
2024-03-30 02:41:00 +08:00
OnNewSentenceS.erase(wss);
2024-02-09 09:25:26 +08:00
}
2024-07-21 19:28:47 +08:00
std::optional<std::wstring> Pluginmanager::selectpluginfile()
{
return SelectFile(0, L"Plugin Files\0*.dll;*.xdll\0");
2024-02-08 19:12:20 +08:00
}
2024-07-21 19:28:47 +08:00
void Pluginmanager::swaprank(int a, int b)
{
auto &plgs = configs->configs["plugins"];
auto _b = plgs[b];
plgs[b] = plgs[a];
plgs[a] = _b;
}
2024-03-27 20:36:43 +08:00
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 +
2024-07-21 19:28:47 +08:00
pSeh->Misc.VirtualSize)
2024-03-27 20:36:43 +08:00
{
break;
}
pSeh++;
}
2024-07-21 19:28:47 +08:00
if (pSeh->VirtualAddress == 0 || pSeh->PointerToRawData == 0)
return -1;
2024-03-27 20:36:43 +08:00
return (rva - pSeh->VirtualAddress + pSeh->PointerToRawData);
2024-07-21 19:28:47 +08:00
}
std::set<std::string> getimporttable(const std::wstring &pe)
{
2024-03-27 20:36:43 +08:00
AutoHandle handle = CreateFile(pe.c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
2024-07-21 19:28:47 +08:00
if (!handle)
return {};
2024-03-27 20:36:43 +08:00
DWORD byteread, size = GetFileSize(handle, NULL);
PVOID virtualpointer = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
2024-07-21 19:28:47 +08:00
if (!virtualpointer)
return {};
2024-03-27 20:36:43 +08:00
ReadFile(handle, virtualpointer, size, &byteread, NULL);
2024-07-21 19:28:47 +08:00
struct __
{
PVOID _ptr;
DWORD size;
__(PVOID ptr, DWORD sz) : _ptr(ptr), size(sz) {}
~__()
{
2024-03-27 20:36:43 +08:00
VirtualFree(_ptr, size, MEM_DECOMMIT);
}
2024-07-21 19:28:47 +08:00
} _(virtualpointer, size);
2024-03-27 20:36:43 +08:00
2024-07-21 19:28:47 +08:00
if (PIMAGE_DOS_HEADER(virtualpointer)->e_magic != 0x5a4d)
return {};
2024-03-27 20:36:43 +08:00
2024-07-21 19:28:47 +08:00
PIMAGE_NT_HEADERS ntheaders = (PIMAGE_NT_HEADERS)(PCHAR(virtualpointer) + PIMAGE_DOS_HEADER(virtualpointer)->e_lfanew);
2024-03-27 20:36:43 +08:00
2024-07-21 19:28:47 +08:00
auto magic = ntheaders->OptionalHeader.Magic;
if (x64 && (magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC))
return {};
if ((!x64) && (magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC))
return {};
2024-03-27 20:36:43 +08:00
2024-07-21 19:28:47 +08:00
PIMAGE_SECTION_HEADER pSech = IMAGE_FIRST_SECTION(ntheaders); // Pointer to first section header
PIMAGE_IMPORT_DESCRIPTOR pImportDescriptor; // Pointer to import descriptor
2024-03-27 20:36:43 +08:00
2024-07-21 19:28:47 +08:00
if (ntheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size == 0) /*if size of the table is 0 - Import Table does not exist */
2024-03-27 20:36:43 +08:00
return {};
std::set<std::string> ret;
2024-07-21 19:28:47 +08:00
pImportDescriptor = (PIMAGE_IMPORT_DESCRIPTOR)((DWORD_PTR)virtualpointer +
Rva2Offset(ntheaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress, pSech, ntheaders));
2024-03-27 20:36:43 +08:00
while (pImportDescriptor->Name != NULL)
{
2024-07-21 19:28:47 +08:00
// Get the name of each DLL
auto nameoffset = Rva2Offset(pImportDescriptor->Name, pSech, ntheaders);
if (nameoffset == (DWORD)-1)
// 无导入
2024-06-25 20:39:02 +08:00
return {};
2024-07-21 19:28:47 +08:00
ret.insert((PCHAR)((DWORD_PTR)virtualpointer + nameoffset));
pImportDescriptor++; // advance to next IMAGE_IMPORT_DESCRIPTOR
2024-03-27 20:36:43 +08:00
}
return ret;
}
2024-07-21 19:28:47 +08:00
bool qtchecker(const std::set<std::string> &dll)
2024-03-27 20:36:43 +08:00
{
2024-07-21 19:28:47 +08:00
for (auto qt5 : {"Qt5Widgets.dll", "Qt5Gui.dll", "Qt5Core.dll"})
if (dll.find(qt5) != dll.end())
2024-03-27 20:36:43 +08:00
return true;
return false;
}
2024-07-21 19:28:47 +08:00
addpluginresult Pluginmanager::load(const std::wstring &p, bool *isqt)
{
auto importtable = getimporttable(p);
if (importtable.empty())
return addpluginresult::invaliddll;
auto isQt = qtchecker(importtable);
if (isqt)
*isqt = isQt;
HMODULE base;
2024-07-21 19:28:47 +08:00
if (isQt)
{
base = loadqtdllsX(p);
2024-02-09 09:25:26 +08:00
}
2024-07-21 19:28:47 +08:00
else
{
base = LoadLibraryW(p.c_str());
}
if (base == 0)
return addpluginresult::invaliddll;
2024-03-27 20:36:43 +08:00
std::scoped_lock lock(OnNewSentenceSLock);
2024-07-21 19:28:47 +08:00
OnNewSentenceS[p] = {p, this, isQt, base};
if (!OnNewSentenceS[p].valid())
2024-04-23 20:01:57 +08:00
return addpluginresult::isnotaplugins;
return addpluginresult::success;
}
2024-07-21 19:28:47 +08:00
bool plugindata::valid()
{
2024-04-24 09:04:02 +08:00
return OnNewSentence;
}
2024-07-21 19:28:47 +08:00
plugindata::plugindata(const std::wstring &p, Pluginmanager *manager, bool _isQt, HMODULE hm)
{
hmodule = hm;
isQt = _isQt;
OnNewSentence = (OnNewSentence_t)GetProcAddress(hm, "OnNewSentence");
VisSetting = (VisSetting_t)GetProcAddress(hm, "VisSetting");
refpath = p;
if (VisSetting)
2024-04-24 09:04:02 +08:00
{
2024-07-21 19:28:47 +08:00
auto vis = true;
if (auto plg = manager->get(p))
vis = plg.value().vissetting;
2024-04-24 09:04:02 +08:00
VisSetting(vis);
}
}
2024-07-21 19:28:47 +08:00
void plugindata::initstatus(const pluginitem &plg)
{
if (plg.vissetting && VisSetting)
2024-04-24 09:04:02 +08:00
VisSetting(true);
}
2024-07-21 19:28:47 +08:00
std::optional<pluginitem> Pluginmanager::get(const std::wstring &p)
{
for (int i = 0; i < count(); i++)
2024-04-24 09:04:02 +08:00
{
2024-07-21 19:28:47 +08:00
if (getname(i) == p)
{
2024-04-24 09:04:02 +08:00
return get(i);
}
}
return {};
2024-04-23 20:01:57 +08:00
}
2024-07-21 19:28:47 +08:00
addpluginresult Pluginmanager::addplugin(const std::wstring &p)
{
if (checkisdump(p))
return addpluginresult::dumplicate;
2024-04-23 20:01:57 +08:00
bool isQt;
2024-07-21 19:28:47 +08:00
auto ret = load(p, &isQt);
if (ret == addpluginresult::success)
{
add({p, isQt});
2024-04-24 09:04:02 +08:00
}
2024-04-23 20:01:57 +08:00
return ret;
2024-02-08 19:12:20 +08:00
}
2024-07-21 19:28:47 +08:00
std::array<InfoForExtension, 20> Pluginmanager::GetSentenceInfo(TextThread &thread)
2024-02-08 19:12:20 +08:00
{
2024-07-21 19:28:47 +08:00
void (*AddText)(int64_t, const wchar_t *) = [](int64_t number, const wchar_t *text)
2024-02-08 19:12:20 +08:00
{
2024-07-21 19:28:47 +08:00
if (TextThread *thread = Host::GetThread(number))
thread->Push(text);
2024-02-08 19:12:20 +08:00
};
2024-07-21 19:28:47 +08:00
void (*AddSentence)(int64_t, const wchar_t *) = [](int64_t number, const wchar_t *sentence)
2024-02-08 19:12:20 +08:00
{
2024-07-21 19:28:47 +08:00
if (TextThread *thread = Host::GetThread(number))
thread->AddSentence(sentence);
;
2024-02-08 19:12:20 +08:00
};
static DWORD SelectedProcessId;
2024-07-21 19:28:47 +08:00
auto currthread = (TextThread *)host->currentselect;
SelectedProcessId = (currthread != 0) ? currthread->tp.processId : 0;
2024-10-03 14:53:59 +08:00
DWORD(*GetSelectedProcessId)
2024-07-21 19:28:47 +08:00
() = []
{ return SelectedProcessId; };
2024-02-08 19:12:20 +08:00
2024-07-21 19:28:47 +08:00
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
}