Textractor_test/GUI/extensions.cpp

71 lines
2.5 KiB
C++
Raw Normal View History

2018-07-27 13:42:21 +08:00
#include "extensions.h"
#include <map>
2018-07-29 03:41:21 +08:00
#include <QDir>
2018-07-27 13:42:21 +08:00
std::map<int, ExtensionFunction> extensions;
2018-07-30 09:24:46 +08:00
int processing;
2018-07-27 13:42:21 +08:00
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)
if (file.endsWith("_nexthooker_extension.dll"))
if (GetProcAddress(GetModuleHandleW(file.toStdWString().c_str()), "OnNewSentence") ||
GetProcAddress(LoadLibraryW(file.toStdWString().c_str()), "OnNewSentence"))
{
QString extensionNumber = file.split("_")[0];
newExtensions[extensionNumber.toInt()] = (ExtensionFunction)GetProcAddress(GetModuleHandleW(file.toStdWString().c_str()), "OnNewSentence");
file.chop(sizeof("_nexthooker_extension.dll") - 1);
file.remove(0, extensionNumber.length() + 1);
extensionNames[extensionNumber.toInt()] = file;
}
2018-07-30 09:24:46 +08:00
while (processing) Sleep(10);
processing = -1;
extensions = newExtensions;
processing = 0;
2018-07-27 13:42:21 +08:00
return extensionNames;
}
std::wstring DispatchSentenceToExtensions(std::wstring sentence, std::unordered_map<std::string, int> miscInfo)
{
2018-07-30 09:24:46 +08:00
while (processing < 0) Sleep(10);
processing++;
2018-07-31 02:29:36 +08:00
wchar_t* sentenceOrigBuffer = (wchar_t*)malloc((sentence.size() + 1) * sizeof(wchar_t));
2018-07-30 15:19:44 +08:00
wcscpy(sentenceOrigBuffer, sentence.c_str());
const wchar_t* sentenceBuffer = sentenceOrigBuffer;
2018-07-30 05:36:45 +08:00
InfoForExtension* miscInfoLinkedList = new InfoForExtension;
InfoForExtension* miscInfoTraverser = miscInfoLinkedList;
for (auto i : miscInfo)
{
miscInfoTraverser->propertyName = new char[i.first.size() + 1];
strcpy(miscInfoTraverser->propertyName, i.first.c_str());
miscInfoTraverser->propertyValue = i.second;
miscInfoTraverser->nextProperty = new InfoForExtension;
miscInfoTraverser = miscInfoTraverser->nextProperty;
}
miscInfoTraverser->propertyName = new char[sizeof("END")];
strcpy(miscInfoTraverser->propertyName, "END");
miscInfoTraverser->nextProperty = nullptr;
for (auto i : extensions)
2018-07-30 15:47:09 +08:00
{
const wchar_t* prev = sentenceBuffer;
2018-07-30 05:36:45 +08:00
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
2018-07-30 15:47:09 +08:00
if (sentenceBuffer == nullptr) sentence = prev;
2018-07-31 02:29:36 +08:00
if (sentenceBuffer != prev) free((void*)prev);
2018-07-30 15:47:09 +08:00
}
2018-07-30 05:36:45 +08:00
miscInfoTraverser = miscInfoLinkedList;
while (miscInfoTraverser != nullptr)
{
InfoForExtension* nextNode = miscInfoTraverser->nextProperty;
delete[] miscInfoTraverser->propertyName;
delete miscInfoTraverser;
miscInfoTraverser = nextNode;
}
std::wstring newSentence = std::wstring(sentenceBuffer);
2018-07-31 02:29:36 +08:00
free((void*)sentenceBuffer);
2018-07-30 09:24:46 +08:00
processing--;
2018-07-30 05:36:45 +08:00
return newSentence;
2018-07-27 13:42:21 +08:00
}