add replacer extension

This commit is contained in:
Akash Mozumdar 2019-01-23 16:11:14 -05:00
parent b9590a1c36
commit c57e97bd83
6 changed files with 104 additions and 1 deletions

View File

@ -15,6 +15,7 @@ foreach ($arch in @("86", "64")) {
"Google Translate.dll", "Google Translate.dll",
"Regex Filter.dll", "Regex Filter.dll",
"Remove Repetition.dll", "Remove Repetition.dll",
"Replacer.dll",
"Thread Linker.dll", "Thread Linker.dll",
"platforms", "platforms",
"styles" "styles"

View File

@ -11,6 +11,7 @@ add_library(Extra\ Window SHARED extrawindow.cpp extensionimpl.cpp)
add_library(Google\ Translate SHARED googletranslate.cpp extensionimpl.cpp) add_library(Google\ Translate SHARED googletranslate.cpp extensionimpl.cpp)
add_library(Regex\ Filter SHARED regexfilter.cpp extensionimpl.cpp) add_library(Regex\ Filter SHARED regexfilter.cpp extensionimpl.cpp)
add_library(Remove\ Repetition SHARED removerepeat.cpp extensionimpl.cpp) add_library(Remove\ Repetition SHARED removerepeat.cpp extensionimpl.cpp)
add_library(Replacer SHARED replacer.cpp extensionimpl.cpp)
add_library(Thread\ Linker SHARED threadlinker.cpp extensionimpl.cpp) add_library(Thread\ Linker SHARED threadlinker.cpp extensionimpl.cpp)
add_executable(Extension_Tests extensiontester.cpp) add_executable(Extension_Tests extensiontester.cpp)

View File

@ -66,6 +66,6 @@ TEST(
ProcessSentence(empty, { &tester }); ProcessSentence(empty, { &tester });
ProcessSentence(one, { &tester }); ProcessSentence(one, { &tester });
ProcessSentence(normal, { &tester }); ProcessSentence(normal, { &tester });
assert(empty == L"" && one == L" " && normal == L"This is a normal sentence. はい"); assert(empty == L"" && one == L" " && normal == L"This is a normal sentence. はい");
} }
); );

100
extensions/replacer.cpp Normal file
View File

@ -0,0 +1,100 @@
#include "extension.h"
#include "defs.h"
#include "text.h"
#include <cwctype>
#include <fstream>
std::shared_mutex m;
struct
{
public:
void Put(std::wstring original, std::wstring replacement)
{
Node* current = &root;
for (auto c : original)
if (Ignore(c));
else if (auto& next = current->next[c]) current = next.get();
else current = (next = std::make_unique<Node>()).get();
current->value = replacement;
}
std::pair<int, std::wstring> Lookup(const std::wstring& text)
{
int length = 0;
Node* current = &root;
for (auto c : text)
if (Ignore(c)) ++length;
else if (auto& next = current->next[c]) ++length, current = next.get();
else break;
return { length, current->value };
}
private:
static bool Ignore(wchar_t c)
{
return c <= 0x20 || std::iswspace(c);
}
struct Node
{
std::unordered_map<wchar_t, std::unique_ptr<Node>> next;
std::wstring value;
} root;
} replacementTrie;
void Parse(const std::wstring& file)
{
std::lock_guard l(m);
size_t end = 0;
while (true)
{
size_t original = file.find(L"|ORIG|", end);
size_t becomes = file.find(L"|BECOMES|", original);
end = file.find(L"|END|", becomes);
if (end != std::wstring::npos) replacementTrie.Put(file.substr(original + 6, becomes - original - 6), file.substr(becomes + 9, end - becomes - 9));
else break;
}
}
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::binary)), {});
Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t)));
}
break;
case DLL_PROCESS_DETACH:
{
}
break;
}
return TRUE;
}
bool ProcessSentence(std::wstring& sentence, SentenceInfo)
{
std::shared_lock l(m);
for (int i = 0; i < sentence.size(); ++i)
if (sentence.size() > 10000) return false; // defend against infinite looping
else if (auto[length, replacement] = replacementTrie.Lookup(sentence.substr(i)); !replacement.empty()) sentence.replace(i, length, replacement);
return true;
}
TEST(
{
Parse(LR"(|ORIG|さよなら|BECOMES|goodbye|END|
|ORIG||BECOMES|idiot|END|
|ORIG||BECOMES|hello|END|)");
std::wstring replaced = LR"(hello 
)";
ProcessSentence(replaced, { nullptr });
assert(replaced.rfind(L"さよなら") == std::wstring::npos);
assert(replaced.rfind(L"バカ") == std::wstring::npos);
assert(replaced.rfind(L"こんにちは") == std::wstring::npos);
replacementTrie = {};
}
);

View File

@ -25,6 +25,7 @@ constexpr auto CONFIG_FILE = u8"Textractor.ini";
constexpr auto HOOK_SAVE_FILE = u8"SavedHooks.txt"; constexpr auto HOOK_SAVE_FILE = u8"SavedHooks.txt";
constexpr auto GAME_SAVE_FILE = u8"SavedGames.txt"; constexpr auto GAME_SAVE_FILE = u8"SavedGames.txt";
constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt"; constexpr auto EXTEN_SAVE_FILE = u8"SavedExtensions.txt";
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
// Functions // Functions

Binary file not shown.