From 7efce3c41a3c005534a3e595277d01d402c0c619 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: Tue, 23 Jul 2024 16:20:48 +0800 Subject: [PATCH] screenshot --- LunaTranslator/LunaTranslator/gobject.py | 7 +++- LunaTranslator/LunaTranslator/myutils/hwnd.py | 39 ++++++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gobject.py b/LunaTranslator/LunaTranslator/gobject.py index e9906b79..5c534f6e 100644 --- a/LunaTranslator/LunaTranslator/gobject.py +++ b/LunaTranslator/LunaTranslator/gobject.py @@ -15,11 +15,14 @@ def GetDllpath(_, base=None): return os.path.join(base, _[isbit64]) -def getcachedir(name, basedir="cache"): +def getcachedir(name, basedir="cache", abspath=True): fd = os.path.dirname(name) fn = os.path.basename(name) - fn1 = os.path.abspath(basedir) + if abspath: + fn1 = os.path.abspath(basedir) + else: + fn1 = basedir fn1 = os.path.join(fn1, fd) os.makedirs(fn1, exist_ok=True) fn1 = os.path.join(fn1, fn) diff --git a/LunaTranslator/LunaTranslator/myutils/hwnd.py b/LunaTranslator/LunaTranslator/myutils/hwnd.py index 19c27555..ae86eb2e 100644 --- a/LunaTranslator/LunaTranslator/myutils/hwnd.py +++ b/LunaTranslator/LunaTranslator/myutils/hwnd.py @@ -2,18 +2,23 @@ import windows import threading from qtsymbols import * import gobject -import os, subprocess +import os, subprocess, functools import time, winrtutils, winsharedutils, hashlib +from myutils.config import savehook_new_data from myutils.wrapper import threader @threader -def grabwindow(app="PNG", callback=None): - if callback: +def grabwindow(app="PNG", callback_origin=None): + if callback_origin: fname = gobject.gettempdir(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())) + uid = None else: dirname = "0" - + try: + uid = gobject.baseobject.textsource.gameuid + except: + uid = None try: if gobject.baseobject.textsource.md5 != "0": dirname = gobject.baseobject.textsource.basename @@ -22,8 +27,18 @@ def grabwindow(app="PNG", callback=None): fname = gobject.getcachedir( f"screenshot/{dirname}/" + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()), + abspath=False, ) + def callback_1(callback, uid, fn): + if not os.path.exists(fn): + return + if callback: + callback(os.path.abspath(fn)) + if uid: + savehook_new_data[uid]["imagepath_all"].append(fn) + + callback = functools.partial(callback_1, callback_origin, uid) hwnd = windows.FindWindow( "Window_Magpie_967EB565-6F73-4E94-AE53-00CC42592A22", None ) @@ -32,8 +47,7 @@ def grabwindow(app="PNG", callback=None): @threader def _(): winrtutils._winrt_capture_window(fname + "_winrt_magpie." + app, hwnd) - if callback and os.path.exists(fname + "_winrt_magpie." + app): - callback(os.path.abspath(fname + "_winrt_magpie." + app)) + callback(fname + "_winrt_magpie." + app) _() hwnd = windows.FindWindow("LosslessScaling", None) @@ -42,8 +56,7 @@ def grabwindow(app="PNG", callback=None): @threader def _(): winrtutils._winrt_capture_window(fname + "_winrt_lossless." + app, hwnd) - if callback and os.path.exists(fname + "_winrt_lossless." + app): - callback(os.path.abspath(fname + "_winrt_lossless." + app)) + callback(fname + "_winrt_lossless." + app) _() try: @@ -57,10 +70,9 @@ def grabwindow(app="PNG", callback=None): p = screenshot(0, 0, _[2], _[3], hwnd).toImage() if not p.allGray(): p.save(fname + "_gdi." + app) - if callback and os.path.exists(fname + "_gdi." + app): - callback(os.path.abspath(fname + "_gdi." + app)) + callback(fname + "_gdi." + app) - if not callback: + if not callback_origin: gobject.baseobject.translation_ui.displaystatus.emit( "saved to " + fname, "red", True, True @@ -69,10 +81,9 @@ def grabwindow(app="PNG", callback=None): @threader def _(): winrtutils._winrt_capture_window(fname + "_winrt." + app, hwnd) - if callback and os.path.exists(fname + "_winrt." + app): - callback(os.path.abspath(fname + "_winrt." + app)) + callback(fname + "_winrt." + app) - if p.allGray() or (not callback): + if p.allGray() or (not callback_origin): _()