2018-07-27 13:42:21 +08:00
|
|
|
#include "extensions.h"
|
2018-08-22 10:43:30 +08:00
|
|
|
#include <shared_mutex>
|
2018-07-29 03:41:21 +08:00
|
|
|
#include <QDir>
|
2018-07-27 13:42:21 +08:00
|
|
|
|
2018-10-10 19:03:15 +08:00
|
|
|
std::optional<Extension> LoadExtension(QString file)
|
|
|
|
{
|
|
|
|
// Extension file format: {NUMBER}_{NAME}.dll and exports "OnNewSentence"
|
|
|
|
QRegularExpressionMatch parsedFile = QRegularExpression("^(\\d+)_(.+).dll$").match(file);
|
|
|
|
if (!parsedFile.hasMatch()) return {};
|
|
|
|
HMODULE module = GetModuleHandleW(file.toStdWString().c_str());
|
|
|
|
if (!module) module = LoadLibraryW(file.toStdWString().c_str());
|
|
|
|
if (!module) return {};
|
2018-10-10 20:16:14 +08:00
|
|
|
FARPROC callback = GetProcAddress(module, "OnNewSentence");
|
2018-10-10 19:03:15 +08:00
|
|
|
if (!callback) return {};
|
2018-10-10 20:16:14 +08:00
|
|
|
return Extension{ parsedFile.captured(1).toInt(), parsedFile.captured(2), (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback };
|
2018-10-10 19:03:15 +08:00
|
|
|
}
|
|
|
|
|
2018-08-22 10:43:30 +08:00
|
|
|
std::shared_mutex extenMutex;
|
2018-10-10 19:03:15 +08:00
|
|
|
std::set<Extension> extensions;
|
2018-07-27 13:42:21 +08:00
|
|
|
|
2018-10-10 19:03:15 +08:00
|
|
|
std::set<Extension> LoadExtensions()
|
2018-07-27 13:42:21 +08:00
|
|
|
{
|
2018-10-10 19:03:15 +08:00
|
|
|
std::set<Extension> newExtensions;
|
2018-08-20 05:55:50 +08:00
|
|
|
QStringList files = QDir().entryList();
|
|
|
|
for (auto file : files)
|
2018-10-10 19:03:15 +08:00
|
|
|
if (auto extension = LoadExtension(file)) newExtensions.insert(extension.value());
|
2018-10-08 11:32:31 +08:00
|
|
|
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
|
2018-10-10 19:03:15 +08:00
|
|
|
return extensions = newExtensions;
|
2018-07-27 13:42:21 +08:00
|
|
|
}
|
|
|
|
|
2018-09-21 10:32:47 +08:00
|
|
|
bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
|
2018-07-27 13:42:21 +08:00
|
|
|
{
|
2018-10-03 02:10:25 +08:00
|
|
|
bool success = true;
|
2018-10-07 23:02:00 +08:00
|
|
|
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
|
2018-09-23 03:45:54 +08:00
|
|
|
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
|
2018-10-10 19:03:15 +08:00
|
|
|
|
|
|
|
InfoForExtension miscInfoLinkedList{ "", 0, nullptr };
|
|
|
|
InfoForExtension* miscInfoTraverser = &miscInfoLinkedList;
|
|
|
|
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
|
|
|
|
|
2018-10-03 02:10:25 +08:00
|
|
|
std::shared_lock<std::shared_mutex> extenLock(extenMutex);
|
2018-10-10 19:03:15 +08:00
|
|
|
for (auto extension : extensions)
|
2018-07-30 15:47:09 +08:00
|
|
|
{
|
2018-10-10 19:03:15 +08:00
|
|
|
wchar_t* nextBuffer = extension.callback(sentenceBuffer, &miscInfoLinkedList);
|
2018-10-10 05:43:33 +08:00
|
|
|
if (nextBuffer == nullptr) { success = false; break; }
|
|
|
|
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
|
|
|
sentenceBuffer = nextBuffer;
|
2018-07-30 15:47:09 +08:00
|
|
|
}
|
2018-09-01 16:23:29 +08:00
|
|
|
sentence = std::wstring(sentenceBuffer);
|
2018-10-10 19:03:15 +08:00
|
|
|
|
2018-10-07 23:02:00 +08:00
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
2018-10-03 02:10:25 +08:00
|
|
|
return success;
|
2018-07-27 13:42:21 +08:00
|
|
|
}
|
2018-10-17 13:08:31 +08:00
|
|
|
|
|
|
|
void UnloadExtension(int extenNumber)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
|
|
|
|
if (extensions.find({ extenNumber }) == extensions.end()) return;
|
|
|
|
QString extenFileName = QString::number(extenNumber) + "_" + extensions.find({ extenNumber })->name + ".dll";
|
|
|
|
FreeLibrary(GetModuleHandleW(extenFileName.toStdWString().c_str()));
|
|
|
|
QString removedFileName = extenFileName;
|
|
|
|
removedFileName.remove(0, removedFileName.indexOf("_"));
|
|
|
|
QFile::remove(removedFileName);
|
|
|
|
if (!QFile::rename(extenFileName, removedFileName)) QFile::remove(extenFileName);
|
|
|
|
extensions.erase(extensions.find({ extenNumber }));
|
|
|
|
}
|