more stable and user friendly replacer

This commit is contained in:
Akash Mozumdar 2019-01-24 08:32:21 -05:00
parent c57e97bd83
commit 413c5b17cf
3 changed files with 55 additions and 19 deletions

View File

@ -3,14 +3,18 @@
#include "text.h" #include "text.h"
#include <cwctype> #include <cwctype>
#include <fstream> #include <fstream>
#include <atomic>
#include <filesystem>
#include <process.h>
std::shared_mutex m; std::atomic<std::filesystem::file_time_type> replaceFileLastWrite;
struct struct
{ {
public: public:
void Put(std::wstring original, std::wstring replacement) void Put(std::wstring original, std::wstring replacement)
{ {
if (original.empty()) return;
Node* current = &root; Node* current = &root;
for (auto c : original) for (auto c : original)
if (Ignore(c)); if (Ignore(c));
@ -43,32 +47,50 @@ private:
} root; } root;
} replacementTrie; } replacementTrie;
void Parse(const std::wstring& file) int Parse(const std::wstring& file)
{ {
std::lock_guard l(m); replacementTrie = {};
int replacementCount = 0;
size_t end = 0; size_t end = 0;
while (true) while (true)
{ {
size_t original = file.find(L"|ORIG|", end); size_t original = file.find(L"|ORIG|", end);
size_t becomes = file.find(L"|BECOMES|", original); size_t becomes = file.find(L"|BECOMES|", original);
end = file.find(L"|END|", becomes); if ((end = file.find(L"|END|", becomes)) == std::wstring::npos) break;
if (end != std::wstring::npos) replacementTrie.Put(file.substr(original + 6, becomes - original - 6), file.substr(becomes + 9, end - becomes - 9)); replacementTrie.Put(file.substr(original + 6, becomes - original - 6), file.substr(becomes + 9, end - becomes - 9));
else break; ++replacementCount;
} }
return replacementCount;
}
bool Replace(std::wstring& sentence)
{
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;
} }
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{ {
static HANDLE replacementFile; // not actually used to read/write, just to ensure it exists
switch (ul_reason_for_call) switch (ul_reason_for_call)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
{ {
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::binary)), {}); replacementFile = CreateFileA(REPLACE_SAVE_FILE, FILE_GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))); std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {});
if (Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t))) == 0)
{
std::ofstream(REPLACE_SAVE_FILE, std::ios::out | std::ios::binary | std::ios::trunc).write((char*)REPLACER_INSTRUCTIONS, wcslen(REPLACER_INSTRUCTIONS) * sizeof(wchar_t));
_spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user
}
replaceFileLastWrite = std::filesystem::last_write_time(REPLACE_SAVE_FILE);
} }
break; break;
case DLL_PROCESS_DETACH: case DLL_PROCESS_DETACH:
{ {
CloseHandle(replacementFile);
} }
break; break;
} }
@ -77,24 +99,31 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
bool ProcessSentence(std::wstring& sentence, SentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo)
{ {
static std::shared_mutex m;
static_assert(std::has_unique_object_representations_v<decltype(replaceFileLastWrite)::value_type>);
if (!replaceFileLastWrite.compare_exchange_strong(std::filesystem::last_write_time(REPLACE_SAVE_FILE), std::filesystem::last_write_time(REPLACE_SAVE_FILE)))
{
std::lock_guard l(m);
std::vector<BYTE> file(std::istreambuf_iterator<char>(std::ifstream(REPLACE_SAVE_FILE, std::ios::in | std::ios::binary)), {});
Parse(std::wstring((wchar_t*)file.data(), file.size() / sizeof(wchar_t)));
}
std::shared_lock l(m); std::shared_lock l(m);
for (int i = 0; i < sentence.size(); ++i) return Replace(sentence);
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( TEST(
{ {
Parse(LR"(|ORIG|さよなら|BECOMES|goodbye|END| assert(Parse(LR"(|ORIG| さよなら|BECOMES|goodbye|END|
|ORIG||BECOMES|idiot|END| |ORIG||BECOMES|idiot|END|
|ORIG||BECOMES|hello|END|)"); |ORIG| |BECOMES|hello|END|)") == 3);
std::wstring replaced = LR"(hello  std::wstring replaced = LR"(hello 
)"; )";
ProcessSentence(replaced, { nullptr }); Replace(replaced);
assert(replaced.rfind(L"さよなら") == std::wstring::npos); assert(replaced.find(L"さよなら") == std::wstring::npos &&
assert(replaced.rfind(L"バカ") == std::wstring::npos); replaced.find(L"バカ") == std::wstring::npos &&
assert(replaced.rfind(L"こんにちは") == std::wstring::npos); replaced.find(L"こんにちは") == std::wstring::npos
replacementTrie = {}; );
} }
); );

View File

@ -84,6 +84,13 @@ inline auto ALWAYS_ON_TOP = u8"Keep this window on top";
inline auto REGEX_FILTER = u8"Regex Filter"; inline auto REGEX_FILTER = u8"Regex Filter";
inline auto INVALID_REGEX = u8"Invalid regex"; inline auto INVALID_REGEX = u8"Invalid regex";
inline auto CURRENT_FILTER = u8"Currently filtering: "; inline auto CURRENT_FILTER = u8"Currently filtering: ";
inline auto REPLACER_INSTRUCTIONS = LR"(This file only does anything when the "Replacer" extension is used.
Replacement commands must be formatted like this:
|ORIG|original_text|BECOMES|replacement_text|END|
All text in this file outside of a replacement command is ignored.
Whitespace in original_text is ignored, but replacement_text can contain spaces, newlines, etc.
This file must be encoded in Unicode (UTF-16 little endian).
)";
inline auto THREAD_LINKER = u8"Thread Linker"; inline auto THREAD_LINKER = u8"Thread Linker";
inline auto LINK = u8"Link"; inline auto LINK = u8"Link";
inline auto THREAD_LINK_FROM = u8"Thread number to link from"; inline auto THREAD_LINK_FROM = u8"Thread number to link from";

Binary file not shown.