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-08-22 10:43:30 +08:00
|
|
|
std::shared_mutex extenMutex;
|
2018-07-27 13:42:21 +08:00
|
|
|
std::map<int, ExtensionFunction> extensions;
|
|
|
|
|
2018-07-29 03:41:21 +08:00
|
|
|
std::map<int, QString> LoadExtensions()
|
2018-07-27 13:42:21 +08:00
|
|
|
{
|
2018-07-30 09:24:46 +08:00
|
|
|
std::map<int, ExtensionFunction> newExtensions;
|
2018-07-29 03:41:21 +08:00
|
|
|
std::map<int, QString> extensionNames;
|
2018-08-20 05:55:50 +08:00
|
|
|
QStringList files = QDir().entryList();
|
|
|
|
for (auto file : files)
|
2018-08-26 05:08:45 +08:00
|
|
|
if (file.split("_").size() > 1 && file.split("_")[0].toInt() && file.endsWith(".dll"))
|
2018-08-20 05:55:50 +08:00
|
|
|
if (GetProcAddress(GetModuleHandleW(file.toStdWString().c_str()), "OnNewSentence") ||
|
|
|
|
GetProcAddress(LoadLibraryW(file.toStdWString().c_str()), "OnNewSentence"))
|
|
|
|
{
|
2018-08-26 05:08:45 +08:00
|
|
|
int extensionNumber = file.split("_")[0].toInt();
|
|
|
|
newExtensions[extensionNumber] = (ExtensionFunction)GetProcAddress(GetModuleHandleW(file.toStdWString().c_str()), "OnNewSentence");
|
|
|
|
file.chop(sizeof("dll"));
|
2018-10-08 11:32:31 +08:00
|
|
|
file.remove(0, file.indexOf("_") + 1);
|
|
|
|
extensionNames[extensionNumber] = file;
|
2018-08-20 05:55:50 +08:00
|
|
|
}
|
2018-10-08 11:32:31 +08:00
|
|
|
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
|
2018-07-30 09:24:46 +08:00
|
|
|
extensions = newExtensions;
|
2018-07-27 13:42:21 +08:00
|
|
|
return extensionNames;
|
|
|
|
}
|
|
|
|
|
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-07-30 05:36:45 +08:00
|
|
|
InfoForExtension* miscInfoLinkedList = new InfoForExtension;
|
|
|
|
InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
|
2018-08-23 01:22:37 +08:00
|
|
|
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->nextProperty = 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-09-01 16:23:29 +08:00
|
|
|
try
|
2018-07-30 15:47:09 +08:00
|
|
|
{
|
2018-09-01 16:23:29 +08:00
|
|
|
for (auto i : extensions)
|
|
|
|
{
|
|
|
|
wchar_t* prev = sentenceBuffer;
|
|
|
|
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
|
2018-10-07 23:02:00 +08:00
|
|
|
if (sentenceBuffer != prev) HeapFree(GetProcessHeap(), 0, prev);
|
2018-09-01 16:23:29 +08:00
|
|
|
}
|
2018-07-30 15:47:09 +08:00
|
|
|
}
|
2018-10-03 02:10:25 +08:00
|
|
|
catch (...) { success = false; }
|
2018-09-01 16:23:29 +08:00
|
|
|
sentence = std::wstring(sentenceBuffer);
|
2018-10-07 23:02:00 +08:00
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
2018-10-03 02:10:25 +08:00
|
|
|
delete miscInfoLinkedList;
|
|
|
|
return success;
|
2018-07-27 13:42:21 +08:00
|
|
|
}
|