diff --git a/LunaTranslator/LunaTranslator/textsource/texthook.py b/LunaTranslator/LunaTranslator/textsource/texthook.py index 4ef2a5b1..cf017040 100644 --- a/LunaTranslator/LunaTranslator/textsource/texthook.py +++ b/LunaTranslator/LunaTranslator/textsource/texthook.py @@ -200,6 +200,8 @@ class texthook(basetext): self.runonce_line = "" gobject.baseobject.autoswitchgameuid = False self.delaycollectallselectedoutput() + self.singlethread = True + self.singlethreadlock = threading.Lock() self.autohookmonitorthread() self.initdllonce = True self.initdlllock = threading.Lock() @@ -357,6 +359,10 @@ class texthook(basetext): @threader def autohookmonitorthread(self): + with self.singlethreadlock: + if not self.singlethread: + return + self.singlethread = False while (not self.ending) and (len(self.pids) == 0): try: hwnd = windows.GetForegroundWindow() @@ -365,9 +371,14 @@ class texthook(basetext): except: print_exc() time.sleep(0.1) + with self.singlethreadlock: + self.singlethread = True def start(self, hwnd, pids, gamepath, gameuid, autostart=False): self.delayinit() + for pid in pids: + # 如果有进程一闪而逝,没来的及注入,导致无法自动重连 + self.waitend(pid) gobject.baseobject.hwnd = hwnd gobject.baseobject.gameuid = gameuid self.gameuid = gameuid @@ -463,7 +474,6 @@ class texthook(basetext): def onprocconnect(self, pid): self.pids.append(pid) - self.waitend(pid) for hookcode in self.needinserthookcode: self.Luna_InsertHookCode(pid, hookcode) gobject.baseobject.displayinfomessage(