2019-01-23 16:11:14 -05:00
|
|
|
|
#include "extension.h"
|
2020-02-16 17:58:09 -07:00
|
|
|
|
#include "blockmarkup.h"
|
2019-01-23 16:11:14 -05:00
|
|
|
|
#include <cwctype>
|
|
|
|
|
#include <fstream>
|
2020-02-16 17:58:09 -07:00
|
|
|
|
#include <sstream>
|
2019-01-24 08:32:21 -05:00
|
|
|
|
#include <process.h>
|
2019-01-23 16:11:14 -05:00
|
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
|
extern const wchar_t* REPLACER_INSTRUCTIONS;
|
2019-07-03 18:02:32 +05:30
|
|
|
|
|
2019-02-18 23:12:12 -05:00
|
|
|
|
constexpr auto REPLACE_SAVE_FILE = u8"SavedReplacements.txt";
|
|
|
|
|
|
2019-07-03 18:02:32 +05:30
|
|
|
|
std::atomic<std::filesystem::file_time_type> replaceFileLastWrite = {};
|
2019-02-28 01:40:40 -05:00
|
|
|
|
std::shared_mutex m;
|
2019-01-23 16:11:14 -05:00
|
|
|
|
|
2019-07-03 18:02:32 +05:30
|
|
|
|
class Trie
|
2019-01-23 16:11:14 -05:00
|
|
|
|
{
|
|
|
|
|
public:
|
2020-02-16 17:58:09 -07:00
|
|
|
|
Trie(const std::istream& replacementScript)
|
2019-01-23 16:11:14 -05:00
|
|
|
|
{
|
2020-02-16 17:58:09 -07:00
|
|
|
|
BlockMarkupIterator replacementScriptParser(replacementScript.rdbuf(), Array<std::wstring_view>{ L"|ORIG|", L"|BECOMES|" });
|
|
|
|
|
while (auto read = replacementScriptParser.Next())
|
2019-07-03 18:02:32 +05:30
|
|
|
|
{
|
2020-02-16 17:58:09 -07:00
|
|
|
|
const auto& [original, replacement] = *read;
|
2019-07-03 18:02:32 +05:30
|
|
|
|
Node* current = &root;
|
2019-09-05 13:42:30 -04:00
|
|
|
|
for (auto ch : original) if (!Ignore(ch)) current = Next(current, ch);
|
|
|
|
|
if (current != &root)
|
2020-02-16 17:58:09 -07:00
|
|
|
|
current->value = charStorage.insert(charStorage.end(), replacement.c_str(), replacement.c_str() + replacement.size() + 1) - charStorage.begin();
|
2019-07-03 18:02:32 +05:30
|
|
|
|
}
|
2019-01-23 16:11:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 18:02:32 +05:30
|
|
|
|
std::wstring Replace(const std::wstring& sentence) const
|
2019-01-23 16:11:14 -05:00
|
|
|
|
{
|
2019-07-03 18:02:32 +05:30
|
|
|
|
std::wstring result;
|
|
|
|
|
for (int i = 0; i < sentence.size();)
|
2019-02-25 01:06:16 -05:00
|
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
|
std::wstring_view replacement(sentence.c_str() + i, 1);
|
2019-07-03 18:02:32 +05:30
|
|
|
|
int originalLength = 1;
|
|
|
|
|
|
|
|
|
|
const Node* current = &root;
|
2019-09-05 13:42:30 -04:00
|
|
|
|
for (int j = i; current && j <= sentence.size(); ++j)
|
2019-02-25 01:06:16 -05:00
|
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
|
if (current->value >= 0)
|
2019-07-03 18:02:32 +05:30
|
|
|
|
{
|
2020-02-16 17:58:09 -07:00
|
|
|
|
replacement = charStorage.data() + current->value;
|
2019-07-03 18:02:32 +05:30
|
|
|
|
originalLength = j - i;
|
|
|
|
|
}
|
2020-02-16 17:58:09 -07:00
|
|
|
|
if (!Ignore(sentence[j])) current = Next(current, sentence[j]) ? Next(current, sentence[j]) : Next(current, L'^');
|
2019-02-25 01:06:16 -05:00
|
|
|
|
}
|
2019-07-03 18:02:32 +05:30
|
|
|
|
|
|
|
|
|
result += replacement;
|
|
|
|
|
i += originalLength;
|
|
|
|
|
}
|
2019-02-25 01:06:16 -05:00
|
|
|
|
return result;
|
2019-01-23 16:11:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
|
bool Empty()
|
|
|
|
|
{
|
|
|
|
|
return root.charMap.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 16:11:14 -05:00
|
|
|
|
private:
|
2019-02-17 19:14:49 -05:00
|
|
|
|
static bool Ignore(wchar_t ch)
|
2019-01-23 16:11:14 -05:00
|
|
|
|
{
|
2019-02-17 19:14:49 -05:00
|
|
|
|
return ch <= 0x20 || std::iswspace(ch);
|
2019-01-23 16:11:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
|
template <typename Node>
|
|
|
|
|
static Node* Next(Node* node, wchar_t ch)
|
|
|
|
|
{
|
|
|
|
|
auto it = std::lower_bound(node->charMap.begin(), node->charMap.end(), ch, [](const auto& one, auto two) { return one.first < two; });
|
|
|
|
|
if (it != node->charMap.end() && it->first == ch) return it->second.get();
|
|
|
|
|
if constexpr (!std::is_const_v<Node>) return node->charMap.insert(it, { ch, std::make_unique<Node>() })->second.get();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 16:11:14 -05:00
|
|
|
|
struct Node
|
|
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
|
std::vector<std::pair<wchar_t, std::unique_ptr<Node>>> charMap;
|
|
|
|
|
ptrdiff_t value = -1;
|
2019-01-23 16:11:14 -05:00
|
|
|
|
} root;
|
2019-09-05 13:42:30 -04:00
|
|
|
|
|
2020-02-16 17:58:09 -07:00
|
|
|
|
std::vector<wchar_t> charStorage;
|
|
|
|
|
} trie = { std::istringstream("") };
|
2019-01-23 16:11:14 -05:00
|
|
|
|
|
2019-09-05 13:42:30 -04:00
|
|
|
|
void UpdateReplacements()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (replaceFileLastWrite.exchange(std::filesystem::last_write_time(REPLACE_SAVE_FILE)) == std::filesystem::last_write_time(REPLACE_SAVE_FILE)) return;
|
|
|
|
|
std::scoped_lock l(m);
|
2020-02-16 17:58:09 -07:00
|
|
|
|
trie = Trie(std::ifstream(REPLACE_SAVE_FILE, std::ios::binary));
|
2019-09-05 13:42:30 -04:00
|
|
|
|
}
|
|
|
|
|
catch (std::filesystem::filesystem_error) { replaceFileLastWrite.store({}); }
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 16:11:14 -05:00
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|
|
|
|
{
|
|
|
|
|
switch (ul_reason_for_call)
|
|
|
|
|
{
|
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
|
UpdateReplacements();
|
|
|
|
|
if (trie.Empty())
|
2019-01-24 08:32:21 -05:00
|
|
|
|
{
|
2019-09-09 22:08:24 -04:00
|
|
|
|
auto file = std::ofstream(REPLACE_SAVE_FILE, std::ios::binary) << "\xff\xfe";
|
|
|
|
|
for (auto ch : std::wstring_view(REPLACER_INSTRUCTIONS)) file << (ch == L'\n' ? std::string_view("\r\0\n", 4) : std::string_view((char*)&ch, 2));
|
2019-01-24 08:32:21 -05:00
|
|
|
|
_spawnlp(_P_DETACH, "notepad", "notepad", REPLACE_SAVE_FILE, NULL); // show file to user
|
|
|
|
|
}
|
2019-01-23 16:11:14 -05:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo)
|
|
|
|
|
{
|
2019-09-05 13:42:30 -04:00
|
|
|
|
UpdateReplacements();
|
2019-01-24 08:32:21 -05:00
|
|
|
|
|
2019-07-03 18:02:32 +05:30
|
|
|
|
std::shared_lock l(m);
|
|
|
|
|
sentence = trie.Replace(sentence);
|
|
|
|
|
return true;
|
2019-01-23 16:11:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 18:02:32 +05:30
|
|
|
|
TEST(
|
2019-01-23 16:11:14 -05:00
|
|
|
|
{
|
2020-02-16 17:58:09 -07:00
|
|
|
|
std::wstring replacementScript = LR"(
|
2019-07-03 18:02:32 +05:30
|
|
|
|
|ORIG|さよなら|BECOMES|goodbye |END|Ignore this text
|
|
|
|
|
And this text ツ
|
2019-01-23 16:11:14 -05:00
|
|
|
|
|ORIG|バカ|BECOMES|idiot|END|
|
2020-02-16 17:58:09 -07:00
|
|
|
|
|ORIG|こんにちは |BECOMES| hello|END||ORIG|delet^this|BECOMES||END|)";
|
|
|
|
|
Trie replacements(std::istringstream(std::string{ (const char*)replacementScript.c_str(), replacementScript.size() * sizeof(wchar_t) }));
|
2019-07-03 18:02:32 +05:30
|
|
|
|
std::wstring original = LR"(Don't replace this
|
|
|
|
|
さよなら バカ こんにちは delete this)";
|
2019-09-05 13:42:30 -04:00
|
|
|
|
std::wstring replaced = Trie(std::move(replacements)).Replace(original);
|
2019-07-03 18:02:32 +05:30
|
|
|
|
assert(replaced == L"Don't replace thisgoodbye idiot hello");
|
2019-01-23 16:11:14 -05:00
|
|
|
|
}
|
|
|
|
|
);
|