From f982f96af62a5c6ba60f06ad66e3cd2591812f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <1173718158@qq.com> Date: Mon, 23 Dec 2024 14:39:45 +0800 Subject: [PATCH] . --- cpp/shareddllproxy/Atlas.cpp | 34 +++++++++++++++++++++------ cpp/shareddllproxy/lec.cpp | 12 +++++----- py/LunaTranslator/translator/atlas.py | 13 ++++++---- py/LunaTranslator/translator/lec.py | 2 ++ 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/cpp/shareddllproxy/Atlas.cpp b/cpp/shareddllproxy/Atlas.cpp index dedf10c9..4e055aca 100644 --- a/cpp/shareddllproxy/Atlas.cpp +++ b/cpp/shareddllproxy/Atlas.cpp @@ -531,6 +531,28 @@ wchar_t *TranslateFullLog(wchar_t *otext) struct AtlasConfig atlcfg; +static void writestring(wchar_t *text, HANDLE hPipe) +{ + DWORD _; + auto len = text ? (2 * wcslen(text)) : 0; + if (!WriteFile(hPipe, &len, 4, &_, NULL)) + return; + if (text) + if (!WriteFile(hPipe, text, len, &_, NULL)) + return; +} +static wchar_t *readstring(HANDLE hPipe) +{ + DWORD _; + int len; + if (!ReadFile(hPipe, &len, 4, &_, NULL)) + return nullptr; + wchar_t *otext = new wchar_t[len / 2 + 1]; + if (!ReadFile(hPipe, otext, len, &_, NULL)) + return nullptr; + otext[len / 2] = 0; + return otext; +} HANDLE mutex = NULL; int atlaswmain(int argc, wchar_t *argv[]) { @@ -542,9 +564,8 @@ int atlaswmain(int argc, wchar_t *argv[]) return 0; while (true) { - wchar_t src[4096] = {0}; - DWORD _; - if (!ReadFile(hPipe, src, 4096 * 2, &_, NULL)) + wchar_t *src = readstring(hPipe); + if (!src) break; if (!mutex) @@ -583,14 +604,13 @@ int atlaswmain(int argc, wchar_t *argv[]) if (!AtlasIsLoaded()) { ReleaseMutex(mutex); - auto text = L"Atlas Load Failed"; - WriteFile(hPipe, text, wcslen(text) * 2, &_, NULL); + writestring(0, hPipe); return false; } } wchar_t *text = TranslateFull(src, 0, NULL, NULL); - - WriteFile(hPipe, text, wcslen(text) * 2, &_, NULL); + writestring(text, hPipe); + free(src); free(text); ReleaseMutex(mutex); } diff --git a/cpp/shareddllproxy/lec.cpp b/cpp/shareddllproxy/lec.cpp index 84ff0b72..9da8c97a 100644 --- a/cpp/shareddllproxy/lec.cpp +++ b/cpp/shareddllproxy/lec.cpp @@ -152,11 +152,12 @@ void SetUpLEC() static void writestring(wchar_t *text, HANDLE hPipe) { DWORD _; - auto len = 2 * wcslen(text); + auto len = text ? (2 * wcslen(text)) : 0; if (!WriteFile(hPipe, &len, 4, &_, NULL)) return; - if (!WriteFile(hPipe, text, len, &_, NULL)) - return; + if (text) + if (!WriteFile(hPipe, text, len, &_, NULL)) + return; } static wchar_t *readstring(HANDLE hPipe) { @@ -195,7 +196,6 @@ int lecwmain(int argc, wchar_t *argv[]) PATH = "Nova\\JaEn\\EngineDll_je.dll"; } DWORD _; - wchar_t __[] = L"not installed"; while (true) { wchar_t *otext = readstring(hPipe); @@ -207,14 +207,14 @@ int lecwmain(int argc, wchar_t *argv[]) SetUpLEC(); if (lecState < 0) { - writestring(__, hPipe); + writestring(0, hPipe); continue; } } if (lecState < 0) { - writestring(__, hPipe); + writestring(0, hPipe); continue; } wchar_t *text = LECTranslateFull(otext); diff --git a/py/LunaTranslator/translator/atlas.py b/py/LunaTranslator/translator/atlas.py index 4376ee11..22e7fdca 100644 --- a/py/LunaTranslator/translator/atlas.py +++ b/py/LunaTranslator/translator/atlas.py @@ -1,7 +1,6 @@ from myutils.subproc import subproc_w, autoproc from translator.basetranslator import basetrans -import os, time -from myutils.config import _TR +import ctypes, time import windows @@ -47,9 +46,13 @@ class TS(basetrans): def x64(self, content): self.checkpath() - windows.WriteFile(self.hPipe, content.encode("utf-16-le")) - - return windows.ReadFile(self.hPipe, 4096).decode("utf-16-le") + l = content.encode("utf-16-le") + windows.WriteFile(self.hPipe, bytes(ctypes.c_int(len(l)))) + windows.WriteFile(self.hPipe, l) + size = ctypes.c_int.from_buffer_copy(windows.ReadFile(self.hPipe, 4)).value + if not size: + raise Exception("not installed") + return windows.ReadFile(self.hPipe, size).decode("utf-16-le") def translate(self, content): return self.x64(content) diff --git a/py/LunaTranslator/translator/lec.py b/py/LunaTranslator/translator/lec.py index f51765e7..474b8f13 100644 --- a/py/LunaTranslator/translator/lec.py +++ b/py/LunaTranslator/translator/lec.py @@ -56,6 +56,8 @@ class TS(basetrans): windows.WriteFile(self.hPipe, bytes(ctypes.c_int(len(l)))) windows.WriteFile(self.hPipe, l) size = ctypes.c_int.from_buffer_copy(windows.ReadFile(self.hPipe, 4)).value + if not size: + raise Exception("not installed") return windows.ReadFile(self.hPipe, size).decode("utf-16-le") def translate(self, content):