This commit is contained in:
恍兮惚兮 2024-11-03 20:31:46 +08:00
parent 0310af6c82
commit 8ccecb045a
12 changed files with 794 additions and 686 deletions

View File

@ -18,7 +18,7 @@ void cast_back(const HookParam &hp, void *data, size_t *len, const std::wstring
}
else
{
astr = WideStringToString(trans, hp.codepage ? hp.codepage : ((hp.type & CODEC_UTF8) ? CP_UTF8 : embedsharedmem->codepage));
astr = WideStringToString(trans, hp.codepage ? hp.codepage : ((hp.type & CODEC_UTF8) ? CP_UTF8 : commonsharedmem->codepage));
}
write_string_overwrite(data, len, astr);
}
@ -158,7 +158,7 @@ static std::wstring insertSpacesAfterUnencodableSTD(const std::wstring &text, Ho
for (const wchar_t &c : text)
{
ret.push_back(c);
if (!charEncodableSTD(c, hp.codepage ? hp.codepage : embedsharedmem->codepage))
if (!charEncodableSTD(c, hp.codepage ? hp.codepage : commonsharedmem->codepage))
ret.push_back(L' ');
}
return ret;
@ -178,15 +178,16 @@ bool isPauseKeyPressed()
std::unordered_map<UINT64, std::wstring> translatecache;
bool check_is_thread_selected(const ThreadParam &tp)
{
for (int i = 0; i < 10; i++)
if (embedsharedmem->use[i])
if ((embedsharedmem->addr[i] == tp.addr) && (embedsharedmem->ctx1[i] == tp.ctx) && (embedsharedmem->ctx2[i] == tp.ctx2))
return true;
for (int i = 0; i < ARRAYSIZE(commonsharedmem->embedtps); i++)
{
if (commonsharedmem->embedtps[i].use && (commonsharedmem->embedtps[i].tp == tp))
return true;
}
return false;
}
bool check_embed_able(const ThreadParam &tp)
{
return host_connected && check_is_thread_selected(tp) && ((isPauseKeyPressed() == false) ? true : !embedsharedmem->fastskipignore);
return host_connected && check_is_thread_selected(tp) && ((isPauseKeyPressed() == false) ? true : !commonsharedmem->fastskipignore);
}
bool waitforevent(UINT32 timems, const ThreadParam &tp, const std::wstring &origin)
{
@ -238,7 +239,7 @@ UINT64 texthash(void *data, size_t len)
}
bool checktranslatedok(void *data, size_t len)
{
ZeroMemory(embedsharedmem->text, sizeof(embedsharedmem->text)); // clear trans before call
ZeroMemory(commonsharedmem->text, sizeof(commonsharedmem->text)); // clear trans before call
if (len > 1000)
return true;
return (translatecache.find(texthash(data, len)) != translatecache.end());
@ -246,7 +247,7 @@ bool checktranslatedok(void *data, size_t len)
bool TextHook::waitfornotify(TextOutput_T *buffer, void *data, size_t *len, ThreadParam tp)
{
std::wstring origin;
if (auto t = commonparsestring(data, *len, &hp, embedsharedmem->codepage))
if (auto t = commonparsestring(data, *len, &hp, commonsharedmem->codepage))
origin = t.value();
else
return false;
@ -259,9 +260,9 @@ bool TextHook::waitfornotify(TextOutput_T *buffer, void *data, size_t *len, Thre
}
else
{
if (waitforevent(embedsharedmem->waittime, tp, origin) == false)
if (waitforevent(commonsharedmem->waittime, tp, origin) == false)
return false;
translate = embedsharedmem->text;
translate = commonsharedmem->text;
if ((translate.size() == 0))
return false;
translatecache.insert(std::make_pair(hash, translate));
@ -269,7 +270,7 @@ bool TextHook::waitfornotify(TextOutput_T *buffer, void *data, size_t *len, Thre
if (hp.newlineseperator)
strReplace(translate, L"\n", hp.newlineseperator);
translate = adjustSpacesSTD(translate, hp);
if (embedsharedmem->keeprawtext)
if (commonsharedmem->keeprawtext)
translate = origin + L" " + translate;
solvefont(hp);
cast_back(hp, data, len, translate, false);

View File

@ -1,7 +1,7 @@
#ifndef __LUNA_EMBED_ENGINE_H
#define __LUNA_EMBED_ENGINE_H
extern EmbedSharedMem *embedsharedmem;
extern CommonSharedMem *commonsharedmem;
extern DynamicShiftJISCodec *dynamiccodec;
namespace WinKey

File diff suppressed because it is too large Load Diff

View File

@ -1422,7 +1422,7 @@ namespace Private {
bool hookBefore(hook_stack*s,void* data1, size_t* len,uintptr_t*role)
{
static std::wstring fontFace_;
auto fontFamily=std::wstring(embedsharedmem->fontFamily);
auto fontFamily=std::wstring(commonsharedmem->fontFamily);
if (!fontFamily.empty()) {
if (fontFace_ != fontFamily)

View File

@ -6,16 +6,16 @@ extern "C" __declspec(dllexport) const wchar_t *internal_renpy_call_host(const w
}
bool Luna_checkisusingembed(uint64_t address, uint64_t ctx2, bool usingsplit)
{
auto sm = embedsharedmem;
auto sm = commonsharedmem;
if (!sm)
return false;
for (int i = 0; i < 10; i++)
for (int i = 0; i < ARRAYSIZE(sm->embedtps); i++)
{
if (sm->use[i])
if (sm->embedtps[i].use)
{
if (!usingsplit)
return true;
if ((sm->addr[i] == address) && (sm->ctx2[i] == ctx2))
if ((sm->embedtps[i].tp.addr == address) && (sm->embedtps[i].tp.ctx2 == ctx2))
return true;
}
}
@ -270,13 +270,13 @@ namespace
}
extern "C" __declspec(dllexport) const wchar_t *internal_renpy_get_font()
{
if (wcslen(embedsharedmem->fontFamily) == 0)
if (wcslen(commonsharedmem->fontFamily) == 0)
return NULL;
fnDWriteCreateFactory = (decltype(fnDWriteCreateFactory))GetProcAddress(LoadLibrary(L"Dwrite.dll"), "DWriteCreateFactory");
if (fnDWriteCreateFactory)
{
auto fonts_filename_list = get_fonts_path(embedsharedmem->fontFamily, false, false, DEFAULT_CHARSET);
auto fonts_filename_list = get_fonts_path(commonsharedmem->fontFamily, false, false, DEFAULT_CHARSET);
if (fonts_filename_list.size() == 0)
return NULL;
return *fonts_filename_list.begin();
@ -284,10 +284,10 @@ extern "C" __declspec(dllexport) const wchar_t *internal_renpy_get_font()
else
{
static auto fontname2fontfile = std::move(loadfontfiles());
if (fontname2fontfile.find(embedsharedmem->fontFamily) == fontname2fontfile.end())
if (fontname2fontfile.find(commonsharedmem->fontFamily) == fontname2fontfile.end())
return NULL;
else
return fontname2fontfile.at(embedsharedmem->fontFamily).c_str();
return fontname2fontfile.at(commonsharedmem->fontFamily).c_str();
}
}
bool hookrenpy(HMODULE module)

View File

@ -41,7 +41,7 @@ namespace
std::wstring parseafter(void *data, size_t len)
{
std::wstring transwithfont = magicrecv;
transwithfont += embedsharedmem->fontFamily;
transwithfont += commonsharedmem->fontFamily;
transwithfont += L'\x02';
transwithfont += std::wstring((wchar_t *)data, len / 2);
return transwithfont;

View File

@ -109,9 +109,9 @@ namespace
void customizeLogFontA(LOGFONTA *lplf)
{
if (embedsharedmem->fontCharSetEnabled)
if (commonsharedmem->fontCharSetEnabled)
{
auto charSet = embedsharedmem->fontCharSet;
auto charSet = commonsharedmem->fontCharSet;
if (!charSet)
charSet = systemCharSet();
if (charSet)
@ -131,7 +131,7 @@ namespace
{
customizeLogFontA((LOGFONTA *)lplf);
std::wstring s = embedsharedmem->fontFamily;
std::wstring s = commonsharedmem->fontFamily;
if (!s.empty())
{
lplf->lfFaceName[s.size()] = 0;
@ -217,7 +217,7 @@ namespace
}
bool isFontCustomized()
{
return embedsharedmem->fontCharSetEnabled || wcslen(embedsharedmem->fontFamily);
return commonsharedmem->fontCharSetEnabled || wcslen(commonsharedmem->fontFamily);
}
DCFontSwitcher::DCFontSwitcher(HDC hdc)
: hdc_(hdc), oldFont_(nullptr), newFont_(nullptr), newfontname(L"")
@ -247,18 +247,18 @@ namespace
customizeLogFontW(&lf);
if (std::wstring(embedsharedmem->fontFamily).empty())
if (std::wstring(commonsharedmem->fontFamily).empty())
::GetTextFaceW(hdc_, LF_FACESIZE, lf.lfFaceName);
else
{
wcscpy(lf.lfFaceName, embedsharedmem->fontFamily);
wcscpy(lf.lfFaceName, commonsharedmem->fontFamily);
}
newFont_ = fonts_.get(lf);
if ((!newFont_) || (newfontname != std::wstring(embedsharedmem->fontFamily)))
if ((!newFont_) || (newfontname != std::wstring(commonsharedmem->fontFamily)))
{
newFont_ = Hijack::oldCreateFontIndirectW(&lf);
fonts_.add(newFont_, lf);
newfontname = std::wstring(embedsharedmem->fontFamily);
newfontname = std::wstring(commonsharedmem->fontFamily);
}
oldFont_ = (HFONT)SelectObject(hdc_, newFont_);
}
@ -275,7 +275,7 @@ HFONT WINAPI Hijack::newCreateFontIndirectA(const LOGFONTA *lplf)
// DOUT("width:" << lplf->lfWidth << ", height:" << lplf->lfHeight << ", weight:" << lplf->lfWeight);
// if (auto p = HijackHelper::instance()) {
// auto s = p->settings();
std::wstring fontFamily = embedsharedmem->fontFamily;
std::wstring fontFamily = commonsharedmem->fontFamily;
if (lplf && isFontCustomized())
{
union
@ -324,9 +324,9 @@ HFONT WINAPI Hijack::newCreateFontA(int nHeight, int nWidth, int nEscapement, in
if (isFontCustomized())
{
if (embedsharedmem->fontCharSetEnabled)
if (commonsharedmem->fontCharSetEnabled)
{
auto charSet = embedsharedmem->fontCharSet;
auto charSet = commonsharedmem->fontCharSet;
if (!charSet)
charSet = systemCharSet();
if (charSet)
@ -340,7 +340,7 @@ HFONT WINAPI Hijack::newCreateFontA(int nHeight, int nWidth, int nEscapement, in
nHeight *= s->fontScale;
}
*/
std::wstring fontFamily = embedsharedmem->fontFamily;
std::wstring fontFamily = commonsharedmem->fontFamily;
if (!fontFamily.empty())
{
if (all_ascii(fontFamily.c_str(), fontFamily.size()))
@ -363,9 +363,9 @@ HFONT WINAPI Hijack::newCreateFontW(int nHeight, int nWidth, int nEscapement, in
if (isFontCustomized())
{
if (embedsharedmem->fontCharSetEnabled)
if (commonsharedmem->fontCharSetEnabled)
{
auto charSet = embedsharedmem->fontCharSet;
auto charSet = commonsharedmem->fontCharSet;
if (!charSet)
charSet = systemCharSet();
if (charSet)
@ -378,8 +378,8 @@ HFONT WINAPI Hijack::newCreateFontW(int nHeight, int nWidth, int nEscapement, in
nWidth *= s->fontScale;
nHeight *= s->fontScale;
}*/
if (!std::wstring(embedsharedmem->fontFamily).empty())
lpszFace = (LPCWSTR)embedsharedmem;
if (!std::wstring(commonsharedmem->fontFamily).empty())
lpszFace = (LPCWSTR)commonsharedmem;
}
return oldCreateFontW(CREATE_FONT_ARGS);
}

View File

@ -4,7 +4,7 @@ void HIJACK();
void detachall();
HMODULE hLUNAHOOKDLL;
WinMutex viewMutex;
EmbedSharedMem *embedsharedmem;
CommonSharedMem *commonsharedmem;
namespace
{
AutoHandle<> hookPipe = INVALID_HANDLE_VALUE,
@ -178,7 +178,7 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID)
};
hooks = (decltype(hooks))new TextHook[MAX_HOOK];
VirtualProtect((LPVOID)hooks, sizeof(TextHook) * MAX_HOOK, PAGE_EXECUTE_READWRITE, DUMMY);
createfm(mappedFile3, (void **)&embedsharedmem, sizeof(EmbedSharedMem), EMBED_SHARED_MEM + std::to_wstring(GetCurrentProcessId()));
createfm(mappedFile3, (void **)&commonsharedmem, sizeof(CommonSharedMem), EMBED_SHARED_MEM + std::to_wstring(GetCurrentProcessId()));
MH_Initialize();
@ -190,7 +190,7 @@ BOOL WINAPI DllMain(HINSTANCE hModule, DWORD fdwReason, LPVOID)
MH_Uninitialize();
detachall();
delete[] hooks;
UnmapViewOfFile(embedsharedmem);
UnmapViewOfFile(commonsharedmem);
}
break;
}

View File

@ -20,7 +20,7 @@ typedef void (*ThreadEvent_maybe_embed)(const wchar_t *, const char *, ThreadPar
typedef void (*ThreadEvent)(const wchar_t *, const char *, ThreadParam);
typedef bool (*OutputCallback)(const wchar_t *, const char *, ThreadParam, const wchar_t *);
typedef void (*ConsoleHandler)(const wchar_t *);
typedef void (*HookInsertHandler)(uint64_t, const wchar_t *);
typedef void (*HookInsertHandler)(DWORD, uint64_t, const wchar_t *);
typedef void (*EmbedCallback)(const wchar_t *, ThreadParam);
template <typename T>
std::optional<T> checkoption(bool check, T &&t)
@ -42,8 +42,8 @@ C_LUNA_API void Luna_Start(ProcessEvent Connect, ProcessEvent Disconnect, Thread
{ return Output(thread.hp.hookcode, thread.hp.name, thread.tp, output.c_str()); }),
checkoption(console, [=](const std::wstring &output)
{ console(output.c_str()); }),
checkoption(hookinsert, [=](uint64_t addr, const std::wstring &output)
{ hookinsert(addr, output.c_str()); }),
checkoption(hookinsert, [=](DWORD pid, uint64_t addr, const std::wstring &output)
{ hookinsert(pid, addr, output.c_str()); }),
checkoption(embed, [=](const std::wstring &output, const ThreadParam &tp)
{ embed(output.c_str(), tp); }),
checkoption(Warning, [=](const std::wstring &output)
@ -104,7 +104,7 @@ C_LUNA_API void Luna_FindHooks(DWORD pid, SearchParam sp, findhookcallback_t fin
}
C_LUNA_API void Luna_EmbedSettings(DWORD pid, UINT32 waittime, UINT8 fontCharSet, bool fontCharSetEnabled, wchar_t *fontFamily, UINT32 keeprawtext, bool fastskipignore)
{
auto sm = Host::GetEmbedSharedMem(pid);
auto sm = Host::GetCommonSharedMem(pid);
if (!sm)
return;
sm->waittime = waittime;
@ -114,59 +114,45 @@ C_LUNA_API void Luna_EmbedSettings(DWORD pid, UINT32 waittime, UINT8 fontCharSet
sm->keeprawtext = keeprawtext;
sm->fastskipignore = fastskipignore;
}
C_LUNA_API bool Luna_checkisusingembed(DWORD pid, uint64_t address, uint64_t ctx1, uint64_t ctx2)
C_LUNA_API bool Luna_checkisusingembed(ThreadParam tp)
{
auto sm = Host::GetEmbedSharedMem(pid);
auto sm = Host::GetCommonSharedMem(tp.processId);
if (!sm)
return false;
for (int i = 0; i < 10; i++)
for (int i = 0; i < ARRAYSIZE(sm->embedtps); i++)
{
if (sm->use[i])
{
if ((sm->addr[i] == address) && (sm->ctx1[i] == ctx1) && (sm->ctx2[i] == ctx2))
return true;
}
if (sm->embedtps[i].use && (sm->embedtps[i].tp == tp))
return true;
}
return false;
}
C_LUNA_API void Luna_useembed(DWORD pid, uint64_t address, uint64_t ctx1, uint64_t ctx2, bool use)
C_LUNA_API void Luna_useembed(ThreadParam tp, bool use)
{
auto sm = Host::GetEmbedSharedMem(pid);
auto sm = Host::GetCommonSharedMem(tp.processId);
if (!sm)
return;
sm->codepage = Host::defaultCodepage;
for (int i = 0; i < 10; i++)
for (int i = 0; i < ARRAYSIZE(sm->embedtps); i++)
{
if (sm->use[i])
{
if ((sm->addr[i] == address) && (sm->ctx1[i] == ctx1) && (sm->ctx2[i] == ctx2))
{
if (use == false)
{
sm->addr[i] = sm->ctx1[i] = sm->ctx2[i] = sm->use[i] = 0;
}
}
}
if (sm->embedtps[i].use && (sm->embedtps[i].tp == tp))
if (!use)
ZeroMemory(sm->embedtps + i, sizeof(sm->embedtps[i]));
}
if (use)
{
for (int i = 0; i < 10; i++)
for (int i = 0; i < ARRAYSIZE(sm->embedtps); i++)
{
if (sm->use[i] == 0)
if (!sm->embedtps[i].use)
{
sm->use[i] = 1;
sm->addr[i] = address;
sm->ctx1[i] = ctx1;
sm->ctx2[i] = ctx2;
sm->embedtps[i].use = true;
sm->embedtps[i].tp = tp;
break;
}
}
}
}
C_LUNA_API void Luna_embedcallback(DWORD pid, LPCWSTR text, LPCWSTR trans)
{
auto sm = Host::GetEmbedSharedMem(pid);
auto sm = Host::GetCommonSharedMem(pid);
if (!sm)
return;
wcsncpy(sm->text, trans, ARRAYSIZE(sm->text));
@ -174,4 +160,11 @@ C_LUNA_API void Luna_embedcallback(DWORD pid, LPCWSTR text, LPCWSTR trans)
sprintf(eventname, LUNA_EMBED_notify_event, pid, simplehash::djb2_n2((const unsigned char *)(text), wcslen(text) * 2));
win_event event1(eventname);
event1.signal(true);
}
C_LUNA_API void Luna_SyncThread(ThreadParam tp, bool sync)
{
auto sm = Host::GetCommonSharedMem(tp.processId);
if (!sm)
return;
}

View File

@ -12,13 +12,13 @@ namespace
viewMutex(ITH_HOOKMAN_MUTEX_ + std::to_wstring(processId))
{
embedsharedmem = (EmbedSharedMem *)MapViewOfFile(mappedFile2, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(EmbedSharedMem));
commonsharedmem = (CommonSharedMem *)MapViewOfFile(mappedFile2, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(CommonSharedMem));
// 放到构造表里就不行,不知道为何。
}
~ProcessRecord()
{
UnmapViewOfFile(embedsharedmem);
UnmapViewOfFile(commonsharedmem);
}
template <typename T>
@ -35,7 +35,7 @@ namespace
Host::AddConsoleOutput(std::wstring(hp.hookcode) + L": " + text);
};
EmbedSharedMem *embedsharedmem;
CommonSharedMem *commonsharedmem;
private:
HANDLE pipe;
@ -132,7 +132,7 @@ namespace
{
if(HookInsert){
auto info = (HookInsertingNotif*)buffer;
HookInsert(info->addr,info->hookcode);
HookInsert(processId, info->addr,info->hookcode);
}
}
break;
@ -374,12 +374,12 @@ namespace Host
return &thread;
return nullptr;
}
EmbedSharedMem *GetEmbedSharedMem(DWORD processId)
CommonSharedMem *GetCommonSharedMem(DWORD processId)
{
auto &prs = processRecordsByIds.Acquire().contents;
if (prs.find(processId) == prs.end())
return 0;
return prs.at(processId).embedsharedmem;
return prs.at(processId).commonsharedmem;
}
void AddConsoleOutput(std::wstring text)
{

View File

@ -7,7 +7,7 @@ namespace Host
using ProcessEventHandler = std::function<void(DWORD)>;
using ThreadEventHandler = std::function<void(TextThread &)>;
using HookEventHandler = std::function<void(const HookParam &, const std::wstring &text)>;
using HookInsertHandler = std::function<void(uint64_t, const std::wstring &)>;
using HookInsertHandler = std::function<void(DWORD, uint64_t, const std::wstring &)>;
using EmbedCallback = std::function<void(const std::wstring &, const ThreadParam &)>;
void Start(ProcessEventHandler Connect, ProcessEventHandler Disconnect, ThreadEventHandler Create, ThreadEventHandler Destroy, TextThread::OutputCallback Output, bool createconsole = true);
void StartEx(std::optional<ProcessEventHandler> Connect, std::optional<ProcessEventHandler> Disconnect, std::optional<ThreadEventHandler> Create, std::optional<ThreadEventHandler> Destroy, std::optional<TextThread::OutputCallback> Output, std::optional<ConsoleHandler> console, std::optional<HookInsertHandler> hookinsert, std::optional<EmbedCallback> embed, std::optional<ConsoleHandler> warning);
@ -19,7 +19,7 @@ namespace Host
void InsertHook(DWORD processId, HookParam hp);
void RemoveHook(DWORD processId, uint64_t address);
void FindHooks(DWORD processId, SearchParam sp, HookEventHandler HookFound = {});
EmbedSharedMem *GetEmbedSharedMem(DWORD pid);
CommonSharedMem *GetCommonSharedMem(DWORD pid);
TextThread *GetThread(int64_t handle);
TextThread &GetThread(ThreadParam tp);

View File

@ -12,12 +12,8 @@ inline std::atomic<bool (*)(LPVOID addr, hook_stack *stack)> trigger_fun = nullp
// jichi 9/25/2013: This class will be used by NtMapViewOfSectionfor
// interprocedure communication, where constructor/destructor will NOT work.
struct EmbedSharedMem
struct CommonSharedMem
{
uint64_t use[10];
uint64_t addr[10];
uint64_t ctx1[10];
uint64_t ctx2[10];
UINT32 waittime;
UINT32 keeprawtext;
uint64_t hash;
@ -27,6 +23,11 @@ struct EmbedSharedMem
wchar_t fontFamily[100];
UINT codepage;
bool fastskipignore;
struct
{
bool use;
ThreadParam tp;
} embedtps[32];
};
class TextHook
{