2018-07-26 22:42:21 -07:00
|
|
|
#include "extensions.h"
|
|
|
|
|
2018-10-28 02:27:24 -04:00
|
|
|
static std::optional<Extension> LoadExtension(QString extenName)
|
2018-10-10 07:03:15 -04:00
|
|
|
{
|
2018-10-28 02:27:24 -04:00
|
|
|
// Extension is dll and exports "OnNewSentence"
|
|
|
|
HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str());
|
|
|
|
if (!module) module = LoadLibraryW(extenName.toStdWString().c_str());
|
2018-10-10 07:03:15 -04:00
|
|
|
if (!module) return {};
|
2018-10-10 08:16:14 -04:00
|
|
|
FARPROC callback = GetProcAddress(module, "OnNewSentence");
|
2018-10-10 07:03:15 -04:00
|
|
|
if (!callback) return {};
|
2018-10-28 02:27:24 -04:00
|
|
|
return Extension{ extenName, (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback };
|
2018-10-10 07:03:15 -04:00
|
|
|
}
|
|
|
|
|
2018-10-28 02:27:24 -04:00
|
|
|
void Extension::Load(QString extenName)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
|
|
|
|
if (auto extension = LoadExtension(extenName)) extensions.push_back(extension.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Extension::SendToBack(QString extenName)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
|
|
|
|
Extension* extenIter = std::find_if(extensions.begin(), extensions.end(), [&](Extension extension) { return extension.name == extenName; });
|
|
|
|
Extension extension = *extenIter;
|
|
|
|
extensions.erase(extenIter);
|
|
|
|
extensions.push_back(extension);
|
|
|
|
}
|
2018-07-26 22:42:21 -07:00
|
|
|
|
2018-10-28 02:27:24 -04:00
|
|
|
void Extension::Unload(QString extenName)
|
2018-07-26 22:42:21 -07:00
|
|
|
{
|
2018-10-07 23:32:31 -04:00
|
|
|
std::unique_lock<std::shared_mutex> extenLock(extenMutex);
|
2018-10-28 02:27:24 -04:00
|
|
|
extensions.erase(std::find_if(extensions.begin(), extensions.end(), [&](Extension extension) { return extension.name == extenName; }));
|
|
|
|
FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str()));
|
2018-07-26 22:42:21 -07:00
|
|
|
}
|
|
|
|
|
2018-10-28 02:27:24 -04:00
|
|
|
QVector<QString> Extension::GetNames()
|
|
|
|
{
|
|
|
|
QVector<QString> ret;
|
|
|
|
for (auto extension : extensions) ret.push_back(extension.name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Extension::DispatchSentence(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
|
2018-07-26 22:42:21 -07:00
|
|
|
{
|
2018-10-02 14:10:25 -04:00
|
|
|
bool success = true;
|
2018-10-07 11:02:00 -04:00
|
|
|
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
|
2018-09-22 15:45:54 -04:00
|
|
|
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
|
2018-10-10 07:03:15 -04: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-02 14:10:25 -04:00
|
|
|
std::shared_lock<std::shared_mutex> extenLock(extenMutex);
|
2018-10-10 07:03:15 -04:00
|
|
|
for (auto extension : extensions)
|
2018-07-30 00:47:09 -07:00
|
|
|
{
|
2018-10-10 07:03:15 -04:00
|
|
|
wchar_t* nextBuffer = extension.callback(sentenceBuffer, &miscInfoLinkedList);
|
2018-10-09 17:43:33 -04:00
|
|
|
if (nextBuffer == nullptr) { success = false; break; }
|
|
|
|
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
|
|
|
sentenceBuffer = nextBuffer;
|
2018-07-30 00:47:09 -07:00
|
|
|
}
|
2018-09-01 04:23:29 -04:00
|
|
|
sentence = std::wstring(sentenceBuffer);
|
2018-10-10 07:03:15 -04:00
|
|
|
|
2018-10-07 11:02:00 -04:00
|
|
|
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
|
2018-10-02 14:10:25 -04:00
|
|
|
return success;
|
2018-07-26 22:42:21 -07:00
|
|
|
}
|