Textractor/GUI/extensions.cpp

70 lines
2.5 KiB
C++
Raw Normal View History

2018-07-26 22:42:21 -07:00
#include "extensions.h"
#include <map>
2018-07-28 12:41:21 -07:00
#include <QDir>
2018-07-26 22:42:21 -07:00
std::map<int, ExtensionFunction> extensions;
2018-07-29 18:24:46 -07:00
int processing;
2018-07-26 22:42:21 -07:00
2018-07-28 12:41:21 -07:00
std::map<int, QString> LoadExtensions()
2018-07-26 22:42:21 -07:00
{
2018-07-29 18:24:46 -07:00
std::map<int, ExtensionFunction> newExtensions;
2018-07-28 12:41:21 -07:00
std::map<int, QString> extensionNames;
wchar_t path[MAX_PATH] = {};
(QDir::currentPath() + "/*_nexthooker_extension.dll").toWCharArray(path);
2018-07-26 22:42:21 -07:00
WIN32_FIND_DATAW fileData;
HANDLE file = FindFirstFileW(path, &fileData);
do
2018-07-28 12:41:21 -07:00
if (GetProcAddress(GetModuleHandleW(fileData.cFileName), "OnNewSentence") ||
GetProcAddress(LoadLibraryW(fileData.cFileName), "OnNewSentence")
)
{
2018-07-29 18:24:46 -07:00
newExtensions[std::wcstol(fileData.cFileName, nullptr, 10)] = (ExtensionFunction)GetProcAddress(GetModuleHandleW(fileData.cFileName), "OnNewSentence");
2018-07-28 12:41:21 -07:00
QString name = QString::fromWCharArray(fileData.cFileName);
name.chop(sizeof("_nexthooker_extension.dll") - 1);
name.remove(0, name.split("_")[0].length() + 1);
extensionNames[std::wcstol(fileData.cFileName, nullptr, 10)] = name;
}
2018-07-26 22:42:21 -07:00
while (FindNextFileW(file, &fileData) != 0);
2018-07-29 18:24:46 -07:00
while (processing) Sleep(10);
processing = -1;
extensions = newExtensions;
processing = 0;
2018-07-26 22:42:21 -07:00
return extensionNames;
}
std::wstring DispatchSentenceToExtensions(std::wstring sentence, std::unordered_map<std::string, int> miscInfo)
{
2018-07-29 18:24:46 -07:00
while (processing < 0) Sleep(10);
processing++;
2018-07-30 00:19:44 -07:00
wchar_t* sentenceOrigBuffer = new wchar_t[sentence.size() + 1];
wcscpy(sentenceOrigBuffer, sentence.c_str());
const wchar_t* sentenceBuffer = sentenceOrigBuffer;
2018-07-29 14:36:45 -07: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)
sentenceBuffer = i.second(sentenceBuffer, miscInfoLinkedList);
miscInfoTraverser = miscInfoLinkedList;
while (miscInfoTraverser != nullptr)
{
InfoForExtension* nextNode = miscInfoTraverser->nextProperty;
delete[] miscInfoTraverser->propertyName;
delete miscInfoTraverser;
miscInfoTraverser = nextNode;
}
std::wstring newSentence = std::wstring(sentenceBuffer);
delete[] sentenceBuffer;
2018-07-29 18:24:46 -07:00
processing--;
2018-07-29 14:36:45 -07:00
return newSentence;
2018-07-26 22:42:21 -07:00
}