From 615ba9691a73e53af74f4639bd529b169d25ccdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:24:38 +0800 Subject: [PATCH] ... --- .gitignore | 1 + .../LunaTranslator/gui/translatorUI.py | 21 +++++++---- LunaTranslator/LunaTranslator/myutils/hwnd.py | 11 ++++-- .../translator/basetranslator.py | 2 +- .../LunaTranslator/translator/premt.py | 3 ++ .../LunaTranslator/translator/rengong.py | 2 ++ .../LunaTranslator/translator/sakura.py | 23 ++++++------ .../LunaTranslator/winsharedutils.py | 7 +++- .../defaultconfig/translatorsetting.json | 2 +- plugins/CMakeLists.txt | 4 +-- plugins/winsharedutils/CMakeLists.txt | 2 +- plugins/winsharedutils/hwnd.cpp | 36 +++++++++++++++++-- 12 files changed, 85 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 7033b5c8..5618249f 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ LunaTranslator/run3832.bat LunaTranslator/logs LunaTranslator/LunaTranslator/.vscode/settings.json .vscode/settings.json +LunaTranslator/translation_record diff --git a/LunaTranslator/LunaTranslator/gui/translatorUI.py b/LunaTranslator/LunaTranslator/gui/translatorUI.py index dfba6eb4..56836f12 100644 --- a/LunaTranslator/LunaTranslator/gui/translatorUI.py +++ b/LunaTranslator/LunaTranslator/gui/translatorUI.py @@ -13,7 +13,13 @@ from myutils.utils import ( makehtml, loadpostsettingwindowmethod_maybe, ) -from myutils.hwnd import mouseselectwindow, grabwindow, getExeIcon, getcurrexe +from myutils.hwnd import ( + mouseselectwindow, + grabwindow, + getExeIcon, + getcurrexe, + hwndratex, +) from gui.setting_about import doupdate from gui.dialog_memory import dialog_memory from gui.textbrowser import Textbrowser @@ -252,8 +258,8 @@ class TranslatorWindow(resizableframeless): move_signal = pyqtSignal(QPoint) closesignal = pyqtSignal() hotkeyuse_selectprocsignal = pyqtSignal() - changeshowhiderawsig=pyqtSignal() - changeshowhidetranssig=pyqtSignal() + changeshowhiderawsig = pyqtSignal() + changeshowhidetranssig = pyqtSignal() @threader def tracewindowposthread(self): @@ -284,11 +290,12 @@ class TranslatorWindow(resizableframeless): if not rect: lastpos = None continue + rate = hwndratex(hwnd) rect = QRect( - int(rect[0] / self.devicePixelRatioF()), - int(rect[1] / self.devicePixelRatioF()), - int((rect[2] - rect[0]) / self.devicePixelRatioF()), - int((rect[3] - rect[1]) / self.devicePixelRatioF()), + int(rect[0] / rate), + int(rect[1] / rate), + int((rect[2] - rect[0]) / rate), + int((rect[3] - rect[1]) / rate), ) if not lastpos: lastpos = rect diff --git a/LunaTranslator/LunaTranslator/myutils/hwnd.py b/LunaTranslator/LunaTranslator/myutils/hwnd.py index 4f3ec44b..ec04681e 100644 --- a/LunaTranslator/LunaTranslator/myutils/hwnd.py +++ b/LunaTranslator/LunaTranslator/myutils/hwnd.py @@ -266,11 +266,16 @@ def safepixmap(bs): return pixmap +def hwndratex(hwnd): + _dpi = windows.GetDpiForWindow(hwnd) + mdpi = winsharedutils.GetMonitorDpiScaling(hwnd) + return mdpi / _dpi + + def gdi_screenshot(x1, y1, x2, y2, hwnd=None): if hwnd: - _r = QApplication.instance().devicePixelRatio() - _dpi = windows.GetDpiForWindow(hwnd) - x1, y1, x2, y2 = (int(_ * _dpi / 96 / _r) for _ in (x1, y1, x2, y2)) + rate = hwndratex(hwnd) + x1, y1, x2, y2 = (int(_ / rate) for _ in (x1, y1, x2, y2)) bs = winsharedutils.gdi_screenshot(x1, y1, x2, y2, hwnd) return safepixmap(bs) diff --git a/LunaTranslator/LunaTranslator/translator/basetranslator.py b/LunaTranslator/LunaTranslator/translator/basetranslator.py index 5518871a..b2446fcc 100644 --- a/LunaTranslator/LunaTranslator/translator/basetranslator.py +++ b/LunaTranslator/LunaTranslator/translator/basetranslator.py @@ -369,7 +369,7 @@ class basetrans(commonbase): if not checktutukufunction(): # 检查请求队列是否空,请求队列有新的请求,则放弃当前请求。但对于内嵌翻译请求,不可以放弃。 continue - if self.using_gpt_dict: + if self.transtype == "pre" or self.using_gpt_dict: gpt_dict = None contentraw = contentsolved for _ in optimization_params: diff --git a/LunaTranslator/LunaTranslator/translator/premt.py b/LunaTranslator/LunaTranslator/translator/premt.py index a7abcd75..df43032e 100644 --- a/LunaTranslator/LunaTranslator/translator/premt.py +++ b/LunaTranslator/LunaTranslator/translator/premt.py @@ -36,6 +36,9 @@ class TS(basetrans): ) def translate(self, content): + query = json.loads(content) + content = query["contentraw"] + self.checkfilechanged( self.unsafegetcurrentgameconfig(), self.config["sqlitefile"] ) diff --git a/LunaTranslator/LunaTranslator/translator/rengong.py b/LunaTranslator/LunaTranslator/translator/rengong.py index db66ea40..7dfe2e73 100644 --- a/LunaTranslator/LunaTranslator/translator/rengong.py +++ b/LunaTranslator/LunaTranslator/translator/rengong.py @@ -115,6 +115,8 @@ class TS(basetrans): return "\n".join(collect) def translate(self, content: str): + query = json.loads(content) + content = query["contentraw"] self.checkfilechanged( self.unsafegetcurrentgameconfig(), tuple(self.config["jsonfile"]) ) diff --git a/LunaTranslator/LunaTranslator/translator/sakura.py b/LunaTranslator/LunaTranslator/translator/sakura.py index 2cf42d54..c26f5a6b 100644 --- a/LunaTranslator/LunaTranslator/translator/sakura.py +++ b/LunaTranslator/LunaTranslator/translator/sakura.py @@ -57,20 +57,21 @@ class TS(basetrans): gpt_dict_raw_text = "\n".join(gpt_dict_text_list) return gpt_dict_raw_text - def appendcontext(self, message, contextnum): - - for _i in range(min(len(self.context) // 2, contextnum)): - i = len(self.context) // 2 - min(len(self.context) // 2, contextnum) + _i - message.append(self.context[i * 2]) - message.append(self.context[i * 2 + 1]) - - def _gpt_common_parse_context_2(self, messages, context, contextnum): + def _gpt_common_parse_context_2(self, messages, context, contextnum, ja=False): msgs = [] self._gpt_common_parse_context(msgs, context, contextnum) __ja, __zh = [], [] for i, _ in enumerate(msgs): - [__ja, __zh][i % 2 == 0].append(_.strip()) - messages.append({"role": "assistant", "content": "\n".join(__zh)}) + [__zh, __ja][i % 2 == 0].append(_.strip()) + if __ja: + if ja: + messages.append( + { + "role": "user", + "content": "将下面的日文文本翻译成中文:" + "\n".join(__ja), + } + ) + messages.append({"role": "assistant", "content": "\n".join(__zh)}) def make_messages(self, query, gpt_dict=None): contextnum = ( @@ -111,7 +112,7 @@ class TS(basetrans): "content": "你是一个轻小说翻译模型,可以流畅通顺地以日本轻小说的风格将日文翻译成简体中文,并联系上下文正确使用人称代词,不擅自添加原文中没有的代词。", } ] - self._gpt_common_parse_context_2(messages, self.context, contextnum) + self._gpt_common_parse_context_2(messages, self.context, contextnum, True) gpt_dict_raw_text = self.make_gpt_dict_text(gpt_dict) if gpt_dict_raw_text: content = ( diff --git a/LunaTranslator/LunaTranslator/winsharedutils.py b/LunaTranslator/LunaTranslator/winsharedutils.py index 9b2fed5d..1092863f 100644 --- a/LunaTranslator/LunaTranslator/winsharedutils.py +++ b/LunaTranslator/LunaTranslator/winsharedutils.py @@ -19,7 +19,7 @@ from ctypes import ( CFUNCTYPE, c_long, ) -from ctypes.wintypes import WORD, HWND, DWORD, RECT +from ctypes.wintypes import WORD, HWND, DWORD, RECT, UINT import gobject utilsdll = CDLL(gobject.GetDllpath(("winsharedutils32.dll", "winsharedutils64.dll"))) @@ -354,3 +354,8 @@ def encodemp3(wav): if len(ret): return ret[0] return None + + +GetMonitorDpiScaling = utilsdll.GetMonitorDpiScaling +GetMonitorDpiScaling.argtypes = (HWND,) +GetMonitorDpiScaling.restype = UINT diff --git a/LunaTranslator/files/defaultconfig/translatorsetting.json b/LunaTranslator/files/defaultconfig/translatorsetting.json index f749d373..ab7d4c0c 100644 --- a/LunaTranslator/files/defaultconfig/translatorsetting.json +++ b/LunaTranslator/files/defaultconfig/translatorsetting.json @@ -1121,7 +1121,7 @@ "frequency_penalty": 0, "fix_degeneration": false, "流式输出": true, - "prompt_version": 0, + "prompt_version": 2, "s": "" }, "argstype": { diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 09670bbe..e03aae6d 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -28,8 +28,8 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/version) include(generate_product_version) set(VERSION_MAJOR 5) -set(VERSION_MINOR 42) -set(VERSION_PATCH 12) +set(VERSION_MINOR 43) +set(VERSION_PATCH 0) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h) diff --git a/plugins/winsharedutils/CMakeLists.txt b/plugins/winsharedutils/CMakeLists.txt index c6c691df..af235263 100644 --- a/plugins/winsharedutils/CMakeLists.txt +++ b/plugins/winsharedutils/CMakeLists.txt @@ -13,7 +13,7 @@ generate_product_version( add_library(tinymp3 ../libs/tinymp3/shine_mp3.c) add_library(winsharedutils MODULE mp3enc.cpp webview2_extra.cpp AreoAcrylic.cpp screenshot.cpp ../implsapi.cpp hwnd.cpp globalmessagelistener.cpp theme.cpp version.cpp otsu.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp ${versioninfo}) target_precompile_headers(winsharedutils REUSE_FROM pch) -target_link_libraries(winsharedutils tinymp3) +target_link_libraries(winsharedutils tinymp3 Shcore) if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) set_target_properties(winsharedutils PROPERTIES OUTPUT_NAME "winsharedutils64") else() diff --git a/plugins/winsharedutils/hwnd.cpp b/plugins/winsharedutils/hwnd.cpp index 6e196668..cbf0c3fa 100644 --- a/plugins/winsharedutils/hwnd.cpp +++ b/plugins/winsharedutils/hwnd.cpp @@ -1,5 +1,5 @@ #include "define.h" - +#include DECLARE void showintab(HWND hwnd, bool show, bool tool) { // WS_EX_TOOLWINDOW可以立即生效,WS_EX_APPWINDOW必须切换焦点才生效。但是WS_EX_TOOLWINDOW会改变窗口样式,因此只对无边框窗口使用。 @@ -95,4 +95,36 @@ DECLARE void getprocesses(void (*cb)(DWORD, const wchar_t *)) cb(pe32.th32ProcessID, pe32.szExeFile); } while (Process32Next(hSnapshot, &pe32)); } -} \ No newline at end of file +} +DECLARE UINT GetMonitorDpiScaling(HWND hwnd) +{ + HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST); + if (!hMonitor) + return 96; + auto pGetDpiForMonitor = (HRESULT(*)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *))GetProcAddress(GetModuleHandleA("Shcore.dll"), "GetDpiForMonitor"); + if (pGetDpiForMonitor) + { + UINT dpiX = 0; + UINT dpiY = 0; + HRESULT hr = pGetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY); + if (FAILED(hr)) + return 96; + else + return dpiX; + } + else + { + MONITORINFOEX info; + info.cbSize = sizeof(MONITORINFOEX); + if (!GetMonitorInfo(hMonitor, &info)) + return 96; + HDC hdc = GetDC(NULL); + HDC hdcMonitor = CreateCompatibleDC(hdc); + HDC hdcMonitorScreen = CreateIC(TEXT("DISPLAY"), info.szDevice, NULL, 0); + int dpiX = GetDeviceCaps(hdcMonitorScreen, LOGPIXELSX); + DeleteDC(hdcMonitor); + DeleteDC(hdcMonitorScreen); + ReleaseDC(NULL, hdc); + return dpiX; + } +}