diff --git a/src/LunaTranslator/LunaTranslator_main.py b/src/LunaTranslator/LunaTranslator_main.py index f9b5f890..04fa67c0 100644 --- a/src/LunaTranslator/LunaTranslator_main.py +++ b/src/LunaTranslator/LunaTranslator_main.py @@ -1,12 +1,16 @@ -import sys, os, io, time, threading, queue +import sys, os from ctypes import windll, wintypes -def dopathexists(file): +def dopathexists(file: str): + import windows + if not file: return False if not file.strip(): return False + if windows.check_unc_not_exists(file): + return False PathFileExists = windll.Shlwapi.PathFileExistsW PathFileExists.argtypes = (wintypes.LPCWSTR,) PathFileExists.restype = wintypes.BOOL diff --git a/src/LunaTranslator/windows.py b/src/LunaTranslator/windows.py index a3dd456b..6a7f452e 100644 --- a/src/LunaTranslator/windows.py +++ b/src/LunaTranslator/windows.py @@ -327,6 +327,35 @@ def GetLongPathName(file): return path +def check_unc_file(v: str): + buf = create_unicode_buffer(65535) + for i in range(26): + A = chr(ord("A") + i) + ":" + if _QueryDosDeviceW(A, buf, 65535) != 0: + prefixdos = buf.value + if v.startswith(prefixdos): + return A + v[len(prefixdos) :] + # Get network drive + + # 我操了,使用管理员权限时,这个玩意会失败 + if _WNetGetUniversalNameW(A, 1, buf, byref(c_uint(65535))) == 0: + prefixnetwork = cast( + buf, POINTER(UNIVERSAL_NAME_INFO) + ).contents.lpUniversalName + if v.startswith(prefixnetwork): + return A + v[len(prefixnetwork) :] + return None + + +def check_unc_not_exists(v: str): + # 当路径可能为unc路径,且不存在时,返回True + # 避免访问不存在unc路径时过长等待导致卡死 + # (可能误伤) + if v.startswith("\\"): + return check_unc_file(v) is None + return False + + def _GetProcessFileName(hHandle): w = create_unicode_buffer(65535) # 我佛了,太混乱了,不同权限获取的东西完全不一样 @@ -340,28 +369,12 @@ def _GetProcessFileName(hHandle): ): return - v = w.value - if v[0] == "\\": - - buf = create_unicode_buffer(65535) - for i in range(26): - A = ord("A") + i - if _QueryDosDeviceW(chr(A) + ":", buf, 65535) != 0: - prefixdos = buf.value - if v.startswith(prefixdos): - v = chr(A) + ":" + v[len(prefixdos) :] - break - - # Get network drive - - # 我操了,使用管理员权限时,这个玩意会失败 - if _WNetGetUniversalNameW(chr(A) + ":", 1, buf, byref(c_uint(65535))) == 0: - prefixnetwork = cast( - buf, POINTER(UNIVERSAL_NAME_INFO) - ).contents.lpUniversalName - if v.startswith(prefixnetwork): - v = chr(A) + ":" + v[len(prefixnetwork) :] - break + v: str = w.value + + if v.startswith("\\"): + _f = check_unc_file(v) + if _f: + return _f return v else: return v diff --git a/src/LunaTranslator/winsharedutils.py b/src/LunaTranslator/winsharedutils.py index 1092863f..baa96e16 100644 --- a/src/LunaTranslator/winsharedutils.py +++ b/src/LunaTranslator/winsharedutils.py @@ -20,7 +20,7 @@ from ctypes import ( c_long, ) from ctypes.wintypes import WORD, HWND, DWORD, RECT, UINT -import gobject +import gobject, windows utilsdll = CDLL(gobject.GetDllpath(("winsharedutils32.dll", "winsharedutils64.dll"))) @@ -198,6 +198,8 @@ _extracticon2data.restype = c_bool def extracticon2data(fname): + if windows.check_unc_not_exists(fname): + return None ret = [] def cb(ptr, size): diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt index a621ad91..fd9d0d84 100644 --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -29,7 +29,7 @@ include(generate_product_version) set(VERSION_MAJOR 5) set(VERSION_MINOR 53) -set(VERSION_PATCH 9) +set(VERSION_PATCH 10) add_library(pch pch.cpp) target_precompile_headers(pch PUBLIC pch.h)