This commit is contained in:
恍兮惚兮 2024-08-14 18:55:50 +08:00
parent 0c606eb784
commit 07fb07ee34
8 changed files with 115 additions and 95 deletions

View File

@ -877,35 +877,27 @@ class MAINUI:
return return
elif self.AttachProcessDialog and self.AttachProcessDialog.isVisible(): elif self.AttachProcessDialog and self.AttachProcessDialog.isVisible():
return return
if self.textsource is None: if self.textsource is not None:
hwnd = windows.GetForegroundWindow() return
pid = windows.GetWindowThreadProcessId(hwnd) hwnd = windows.GetForegroundWindow()
name_ = getpidexe(pid) pid = windows.GetWindowThreadProcessId(hwnd)
if not name_: name_ = getpidexe(pid)
return if not name_:
found = findgameuidofpath(name_) return
if not found: found = findgameuidofpath(name_)
return if not found:
uid, reflist = found return
pids = ListProcess(name_) uid, reflist = found
if self.textsource is not None: pids = ListProcess(name_)
return if self.textsource is not None:
if not globalconfig["sourcestatus2"]["texthook"]["use"]: return
return if not globalconfig["sourcestatus2"]["texthook"]["use"]:
if globalconfig["startgamenototop"] == False: return
idx = reflist.index(uid) if globalconfig["startgamenototop"] == False:
reflist.insert(0, reflist.pop(idx)) idx = reflist.index(uid)
self.textsource = texthook(pids, hwnd, name_, uid, autostart=True) reflist.insert(0, reflist.pop(idx))
self.textsource.start() self.textsource = texthook(pids, hwnd, name_, uid, autostart=True)
self.textsource.start()
else:
pids = self.textsource.pids
if len(collect_running_pids(pids)) != 0:
return
self.textsource = None
self.translation_ui.thistimenotsetop = False
if globalconfig["keepontop"]:
self.translation_ui.settop()
while self.isrunning: while self.isrunning:
try: try:
@ -915,49 +907,6 @@ class MAINUI:
time.sleep(0.5) time.sleep(0.5)
# 太短了的话,中间存在一瞬间,后台进程比前台窗口内存占用要大。。。 # 太短了的话,中间存在一瞬间,后台进程比前台窗口内存占用要大。。。
def autocheckhwndexists(self):
def setandrefresh(b):
if self.translation_ui.isbindedwindow != b:
self.translation_ui.isbindedwindow = b
self.translation_ui.refreshtooliconsignal.emit()
@tryprint
def __do():
if not self.textsource:
setandrefresh(False)
return
hwnd = self.textsource.hwnd
if hwnd == 0:
if not globalconfig["sourcestatus2"]["texthook"]["use"]:
setandrefresh(False)
else:
fhwnd = windows.GetForegroundWindow()
pids = self.textsource.pids
notdone = "once" not in dir(self.textsource)
isgoodproc = windows.GetWindowThreadProcessId(fhwnd) in pids
if isgoodproc and notdone:
self.textsource.once = True
self.textsource.hwnd = fhwnd
setandrefresh(True)
else:
if windows.GetWindowThreadProcessId(hwnd) == 0:
self.textsource.hwnd = 0
setandrefresh(False)
elif "once" not in dir(self.textsource):
self.textsource.once = True
setandrefresh(True)
if self.textsource.pids:
_mute = winsharedutils.GetProcessMute(self.textsource.pids[0])
if self.translation_ui.processismuteed != _mute:
self.translation_ui.processismuteed = _mute
self.translation_ui.refreshtooliconsignal.emit()
while self.isrunning:
__do()
time.sleep(0.5)
def setdarkandbackdrop(self, widget, dark): def setdarkandbackdrop(self, widget, dark):
if self.__dontshowintaborsetbackdrop(widget): if self.__dontshowintaborsetbackdrop(widget):
return return
@ -1131,7 +1080,6 @@ class MAINUI:
self.searchwordW = searchwordW(self.commonstylebase) self.searchwordW = searchwordW(self.commonstylebase)
self.hookselectdialog = hookselect(self.commonstylebase) self.hookselectdialog = hookselect(self.commonstylebase)
self.starttextsource() self.starttextsource()
threading.Thread(target=self.autocheckhwndexists).start()
threading.Thread(target=self.autohookmonitorthread).start() threading.Thread(target=self.autohookmonitorthread).start()
threading.Thread( threading.Thread(
target=minmaxmoveobservefunc, args=(self.translation_ui,) target=minmaxmoveobservefunc, args=(self.translation_ui,)

View File

@ -1189,8 +1189,6 @@ class TranslatorWindow(resizableframeless):
gameuid = findgameuidofpath(getpidexe(pid)) gameuid = findgameuidofpath(getpidexe(pid))
if gameuid: if gameuid:
gobject.baseobject.textsource.gameuid = gameuid[0] gobject.baseobject.textsource.gameuid = gameuid[0]
self.isbindedwindow = pid != _pid
self.refreshtoolicon()
def changeshowhideraw(self): def changeshowhideraw(self):
try: try:

View File

@ -1,8 +1,6 @@
import time
from textsource.textsourcebase import basetext from textsource.textsourcebase import basetext
from myutils.config import globalconfig from myutils.config import globalconfig
import winsharedutils, os, queue import winsharedutils, queue
import windows
class copyboard(basetext): class copyboard(basetext):

View File

@ -1,11 +1,12 @@
import time import time, os
from myutils.config import globalconfig from myutils.config import globalconfig
import winsharedutils import winsharedutils, windows
from gui.rangeselect import rangeadjust from gui.rangeselect import rangeadjust
from myutils.ocrutil import imageCut, ocr_run, ocr_end, ocr_init from myutils.ocrutil import imageCut, ocr_run, ocr_end, ocr_init
import time, gobject import time, gobject
from qtsymbols import * from qtsymbols import *
from traceback import print_exc from traceback import print_exc
from collections import Counter
from textsource.textsourcebase import basetext from textsource.textsourcebase import basetext
@ -70,7 +71,28 @@ class ocrtext(basetext):
_r.traceoffsetsignal.emit(curr) _r.traceoffsetsignal.emit(curr)
def setrect(self, rect): def setrect(self, rect):
p1, p2 = rect
h1 = windows.WindowFromPoint(windows.POINT(*p1))
h2 = windows.WindowFromPoint(windows.POINT(*p2))
h3 = windows.WindowFromPoint(windows.POINT(p1[0], p2[1]))
h4 = windows.WindowFromPoint(windows.POINT(p2[0], p1[1]))
self.range_ui[-1].setrect(rect) self.range_ui[-1].setrect(rect)
usehwnds = []
for _ in (h1, h2, h3, h4):
if windows.GetWindowThreadProcessId(_) == os.getpid():
continue
usehwnds.append(_)
if not usehwnds:
return
hwnd, count = Counter(usehwnds).most_common()[0]
if self.hwnd:
if count == len(usehwnds):
self.hwnd = hwnd
else:
if count >= len(usehwnds) - 1:
self.hwnd = hwnd
def setstyle(self): def setstyle(self):
[_.setstyle() for _ in self.range_ui] [_.setstyle() for _ in self.range_ui]

View File

@ -226,10 +226,15 @@ class texthook(basetext):
self.Luna_FreePtr(ws) self.Luna_FreePtr(ws)
return string return string
def procdisc(self, pid):
self.connectedpids.remove(pid)
if len(self.connectedpids) == 0:
gobject.baseobject.textsource = None
def prepares(self): def prepares(self):
procs = [ procs = [
ProcessEvent(self.onprocconnect), ProcessEvent(self.onprocconnect),
ProcessEvent(self.connectedpids.remove), ProcessEvent(self.procdisc),
ThreadEvent(self.onnewhook), ThreadEvent(self.onnewhook),
ThreadEvent(self.onremovehook), ThreadEvent(self.onremovehook),
OutputCallback(self.handle_output), OutputCallback(self.handle_output),

View File

@ -1,16 +1,71 @@
import threading, gobject, queue import threading, gobject, queue
import time, sqlite3, json, os import time, sqlite3, json, os, windows, winsharedutils
from traceback import print_exc from traceback import print_exc
from myutils.config import globalconfig, savehook_new_data from myutils.config import globalconfig, savehook_new_data
from myutils.utils import autosql from myutils.utils import autosql
from myutils.wrapper import threader
class hwndchecker:
def __del__(self):
if self.ref.hwnd:
return
gobject.baseobject.translation_ui.processismuteed = False
gobject.baseobject.translation_ui.isbindedwindow = False
gobject.baseobject.translation_ui.refreshtooliconsignal.emit()
gobject.baseobject.translation_ui.thistimenotsetop = False
if globalconfig["keepontop"]:
gobject.baseobject.translation_ui.settop()
def __init__(self, hwnd, ref) -> None:
self.hwnd = hwnd
self.ref = ref
self.end = False
_mute = winsharedutils.GetProcessMute(
windows.GetWindowThreadProcessId(self.hwnd)
)
gobject.baseobject.translation_ui.processismuteed = _mute
gobject.baseobject.translation_ui.isbindedwindow = True
gobject.baseobject.translation_ui.refreshtooliconsignal.emit()
self.__checkthread()
@threader
def __checkthread(self):
while not self.end:
pid = windows.GetWindowThreadProcessId(self.hwnd)
if not pid:
self.hwnd = None
self.__del__()
break
_mute = winsharedutils.GetProcessMute(pid)
if gobject.baseobject.translation_ui.processismuteed != _mute:
gobject.baseobject.translation_ui.processismuteed = _mute
gobject.baseobject.translation_ui.refreshtooliconsignal.emit()
time.sleep(0.5)
class basetext: class basetext:
@property
def hwnd(self):
if self.__hwnd is None:
return None
return self.__hwnd.hwnd
@hwnd.setter
def hwnd(self, _hwnd):
if self.__hwnd:
self.__hwnd.end = True
self.__hwnd = None
if _hwnd:
self.__hwnd = hwndchecker(_hwnd, self)
def __init__(self, md5, basename): def __init__(self, md5, basename):
self.md5 = md5 self.md5 = md5
self.basename = basename self.basename = basename
self.hwnd = None self.__hwnd = None
self.pids = [] self.pids = []
self.gameuid = None self.gameuid = None
# #

View File

@ -398,6 +398,8 @@ def GetClipboardOwner():
def GetWindowThreadProcessId(hwnd): def GetWindowThreadProcessId(hwnd):
pid = c_uint() pid = c_uint()
handle = _GetWindowThreadProcessId(hwnd, pointer(pid)) handle = _GetWindowThreadProcessId(hwnd, pointer(pid))
if handle == 0:
return 0
return pid.value return pid.value

View File

@ -28,11 +28,11 @@ import gobject
utilsdll = CDLL(gobject.GetDllpath(("winsharedutils32.dll", "winsharedutils64.dll"))) utilsdll = CDLL(gobject.GetDllpath(("winsharedutils32.dll", "winsharedutils64.dll")))
_SetProcessMute = utilsdll.SetProcessMute SetProcessMute = utilsdll.SetProcessMute
_SetProcessMute.argtypes = c_uint, c_bool SetProcessMute.argtypes = c_uint, c_bool
_GetProcessMute = utilsdll.GetProcessMute GetProcessMute = utilsdll.GetProcessMute
_GetProcessMute.restype = c_bool GetProcessMute.restype = c_bool
_SAPI_List = utilsdll.SAPI_List _SAPI_List = utilsdll.SAPI_List
_SAPI_List.argtypes = (c_uint, c_void_p) _SAPI_List.argtypes = (c_uint, c_void_p)
@ -70,14 +70,6 @@ _clipboard_set.argtypes = (
) )
def SetProcessMute(pid, mute):
_SetProcessMute(pid, mute)
def GetProcessMute(pid):
return _GetProcessMute(pid)
def SAPI_List(v): def SAPI_List(v):
ret = [] ret = []
_SAPI_List(v, CFUNCTYPE(None, c_wchar_p)(ret.append)) _SAPI_List(v, CFUNCTYPE(None, c_wchar_p)(ret.append))