handle.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import re
  5. from cached_property import cached_property
  6. from anytree import NodeMixin, RenderTree, PreOrderIter
  7. from win32api import GetSystemMetrics, SendMessage, MAKELONG, PostMessage
  8. from win32print import GetDeviceCaps
  9. from win32process import GetWindowThreadProcessId
  10. from win32gui import (GetWindowText, EnumWindows, FindWindow, FindWindowEx,
  11. IsWindow, GetWindowRect, GetWindowDC, DeleteObject,
  12. SetForegroundWindow, IsWindowVisible, GetDC, GetParent,
  13. EnumChildWindows)
  14. from win32con import (SRCCOPY, DESKTOPHORZRES, DESKTOPVERTRES, WM_LBUTTONUP,
  15. WM_LBUTTONDOWN, WM_ACTIVATE, WA_ACTIVE, MK_LBUTTON,
  16. WM_NCHITTEST, WM_SETCURSOR, HTCLIENT, WM_MOUSEMOVE)
  17. from module.config.config import Config
  18. from module.exception import RequestHumanTakeover, EmulatorNotRunningError
  19. from module.logger import logger
  20. def handle_title2num(title: str) -> int:
  21. """
  22. 从标题到句柄号
  23. :param title:
  24. :return: 如果没有找到就是返回零
  25. """
  26. return FindWindow(None, title)
  27. def handle_num2title(num: int) -> str:
  28. """
  29. 通过句柄号返回窗口的标题,如果传入句柄号不合法则返回None
  30. :param num:
  31. :return:
  32. """
  33. return None if num is None or num == 0 or num == '' else GetWindowText(num)
  34. def is_handle_valid(num: int) -> bool:
  35. """
  36. 输入一个句柄号,如果还在返回True
  37. :param num:
  38. :return:
  39. """
  40. return IsWindow(num)
  41. def handle_num2pid(num :int) -> int:
  42. """
  43. 通过句柄号获取句柄进程id,如果句柄号非法则返回0
  44. :param num:
  45. :return:
  46. """
  47. return 0 if num is None or num == 0 or num == '' else GetWindowThreadProcessId(num)[1]
  48. def window_scale_rate() -> float:
  49. """
  50. 获取window的系统缩放 一遍是1
  51. :return:
  52. """
  53. hDC = GetDC(0)
  54. # 物理上(真实的)的 横纵向分辨率
  55. wReal = GetDeviceCaps(hDC, DESKTOPHORZRES)
  56. hReal = GetDeviceCaps(hDC, DESKTOPVERTRES)
  57. # 缩放后的 分辨率
  58. wAfter = GetSystemMetrics(0)
  59. hAfter = GetSystemMetrics(1)
  60. # print(wReal, wAfter)
  61. return round(wReal / wAfter, 2)
  62. class WindowNode(NodeMixin):
  63. def __init__(self, name, num, parent=None):
  64. super().__init__()
  65. self.name = name
  66. self.num = num
  67. self.parent = parent
  68. class Handle:
  69. emulator_list = ['MuMu12',
  70. 'MuMu',
  71. '雷电',
  72. '夜神',
  73. '蓝叠',
  74. '逍遥',
  75. '模拟器'] # 最后一个我又不知道还有哪些模拟器
  76. emulator_handle = {
  77. # 夜神
  78. 'nox_player': ['root_handle_title', 'Nox'],
  79. 'nox_player_64': ['root_handle_title', 'Nox'],
  80. 'nox_player_family':['root_handle_title', 'Nox'],
  81. # 雷电
  82. 'ld_player': ['TheRender'],
  83. 'ld_player_4': ['TheRender'],
  84. 'ld_player_9': ['TheRender'],
  85. 'ld_player_family': ['TheRender'],
  86. # 逍遥
  87. 'memu_player': ['root_handle_title'],
  88. 'memu_player_family': ['root_handle_title'],
  89. # mumu
  90. 'mumu_player': ['root_handle_title', 'NemuPlayer'],
  91. 'mumu_player_12': ['root_handle_title', 'MuMuPlayer'],
  92. 'mumu_player_family': ['root_handle_title', 'MuMuPlayer'],
  93. # 蓝叠
  94. 'bluestacks_5': ['root_handle_title'],
  95. 'bluestacks_family': ['root_handle_title']
  96. }
  97. config: Config = None
  98. def __init__(self, config) -> None:
  99. """
  100. :param config:
  101. """
  102. logger.hr('Handle')
  103. if self.config is None:
  104. if isinstance(config, str):
  105. self.config = Config(config, task=None)
  106. else:
  107. self.config = config
  108. # 获取根的句柄
  109. self.root_handle_title = ''
  110. self.root_handle_num = 0
  111. self.root_handle = self.config.script.device.handle
  112. if self.root_handle == "auto":
  113. logger.info('handle is auto. oas will find window emulator')
  114. window_list = Handle.all_windows()
  115. self.root_handle_title = self.auto_handle_title(window_list)
  116. self.root_handle_num = handle_title2num(self.root_handle_title)
  117. elif isinstance(self.root_handle, int):
  118. logger.info('handle is handle num. oas use it as root handle num')
  119. if is_handle_valid(self.root_handle):
  120. logger.info(f'handle number {self.root_handle} is valid')
  121. self.root_handle_title = handle_num2title(self.root_handle)
  122. self.root_handle_num = self.root_handle
  123. elif isinstance(self.root_handle, str):
  124. logger.info('handle is handle string. oas use it as root handle title')
  125. if handle_title2num(self.root_handle) != 0:
  126. self.root_handle_num = handle_title2num(self.root_handle)
  127. self.root_handle_title = self.root_handle
  128. logger.info(f'the root handle title is {self.root_handle_title} and num is {self.root_handle_num}')
  129. # 获取句柄树
  130. self.root_node = WindowNode(name=self.root_handle_title, num=self.root_handle_num)
  131. Handle.handle_tree(self.root_handle_num, self.root_node)
  132. logger.info('the emulator handle structure is')
  133. for pre, fill, node in RenderTree(self.root_node):
  134. logger.info("%s%s" % (pre, node.name))
  135. for pre, fill, node in RenderTree(self.root_node):
  136. logger.info("%s%s" % (pre, node.num))
  137. # 判断是哪一个模拟器
  138. logger.info(f'the emulator family is {self.emulator_family}')
  139. # window系统的缩放
  140. logger.info(f'your window screen scale rate is {window_scale_rate()}')
  141. _ = self.screenshot_handle_num
  142. logger.info(f'screenshot handle num is {self.screenshot_handle_num}')
  143. logger.info(f'the emulator screenshot size is {self.screenshot_size}')
  144. @staticmethod
  145. def all_windows() -> list:
  146. """
  147. 获取桌面上的所有窗体
  148. :return: 类似这样['MuMu模拟器']
  149. """
  150. def enum_windows_callback(hwnd, windows):
  151. window_text = GetWindowText(hwnd)
  152. windows.append(window_text)
  153. windows = []
  154. EnumWindows(enum_windows_callback, windows)
  155. return windows
  156. @classmethod
  157. def auto_handle_title(cls, windows: list) -> str:
  158. """
  159. 返回第一个找到的有模拟器的标题
  160. :param windows:
  161. :return:
  162. """
  163. if windows is None:
  164. logger.error("handle_auto not get all wnidow")
  165. emu_list = []
  166. for window_title in windows:
  167. for item in Handle.emulator_list:
  168. if window_title.find(item) != -1:
  169. emu_list.append(window_title)
  170. if not len(emu_list):
  171. logger.error('Can not find emulator handle, please check your emulator is running')
  172. return None
  173. emulator_title = ''
  174. # 测试mumu12的时候发现 获取的全部的窗体标题有这样的: 'MuMuPlayer', 'MuMuPlayer', 'MuMuPlayer', 'MuMu模拟器12'
  175. # 事实上 我们只需要最后一个 'MuMu模拟器12',其他的不重要
  176. if 'MuMu模拟器12' in emu_list and 'MuMuPlayer' in emu_list:
  177. emulator_title = 'MuMu模拟器12'
  178. if len(emu_list) > 1 and emulator_title == '':
  179. logger.warning(f'Find more than one emulator handle, oas will use the first one {emu_list[0]}')
  180. emulator_title = emu_list[0]
  181. if len(emu_list) == 1:
  182. emulator_title = emu_list[0]
  183. logger.info(f'Handle auto seclect to find {emulator_title} and use it as root_title')
  184. return emulator_title
  185. @staticmethod
  186. def handle_tree(hwnd, node: WindowNode, level: int =0) -> None:
  187. """
  188. 生成一个窗口的句柄树
  189. :param hwnd:
  190. :param node:
  191. :param level:
  192. :return:
  193. """
  194. child_windows = []
  195. EnumChildWindows(hwnd, lambda hwnd, param: param.append(hwnd), child_windows)
  196. if not child_windows:
  197. return
  198. for child_hwnd in child_windows:
  199. if GetParent(child_hwnd) == hwnd:
  200. child_text = GetWindowText(child_hwnd)
  201. child_node = WindowNode(name=child_text, num=child_hwnd, parent=node)
  202. # 递归遍历子窗体的子窗体
  203. Handle.handle_tree(child_hwnd, child_node, level + 1)
  204. @cached_property
  205. def emulator_family(self) -> str:
  206. """
  207. 通过句柄标题来判断这个是那个模拟器大类
  208. :return:
  209. """
  210. for emu in Handle.emulator_list:
  211. if self.root_handle_title.find(emu) != -1:
  212. if emu == 'MuMu':
  213. return 'mumu_player_family'
  214. elif emu == '雷电':
  215. return 'ld_player_family'
  216. elif emu == '夜神':
  217. return 'nox_player_family'
  218. elif emu == '蓝叠':
  219. return 'bluestacks_player_family'
  220. elif emu == '逍遥':
  221. return 'memu_player_family'
  222. @cached_property
  223. def screenshot_handle_num(self) -> int:
  224. """
  225. 截屏的句柄其实并不是根句柄
  226. :return: 出错返回None
  227. """
  228. if self.emulator_family == 'mumu_player_family':
  229. if re.match(string=self.root_handle_title, pattern=r".*12$"):
  230. logger.info('is mumu12')
  231. for node in PreOrderIter(self.root_node):
  232. if node.name == Handle.emulator_handle['mumu_player_12'][1]:
  233. return node.num
  234. else:
  235. logger.info('is mumu')
  236. for node in PreOrderIter(self.root_node):
  237. if node.name == Handle.emulator_handle['mumu_player'][1]:
  238. return node.num
  239. # 夜神
  240. elif self.emulator_family == 'nox_player_family':
  241. try:
  242. return self.root_node.children[1].children[1].num
  243. except:
  244. return self.root_node.children[2].children[1].num
  245. elif self.emulator_family == 'ld_player_family':
  246. for node in PreOrderIter(self.root_node):
  247. if node.name == Handle.emulator_handle['ld_player_family'][0]:
  248. return node.num
  249. elif self.emulator_family == 'memu_player_family':
  250. for node in PreOrderIter(self.root_node):
  251. if node.name == Handle.emulator_handle['memu_player_family']:
  252. return node.num
  253. elif self.emulator_family == 'bluestacks_family':
  254. for node in PreOrderIter(self.root_node):
  255. if node.name == Handle.emulator_handle['bluestacks_family']:
  256. return node.num
  257. return None
  258. @cached_property
  259. def screenshot_size(self) -> tuple:
  260. """
  261. 第一个是width 第二个是heigth
  262. :return:
  263. """
  264. winRect = GetWindowRect(self.screenshot_handle_num)
  265. width: int = winRect[2] - winRect[0] # 右x-左x
  266. height: int = winRect[3] - winRect[1] # 下y - 上y 计算高度
  267. return width, height
  268. @cached_property
  269. def window_scale_rate(self) -> float:
  270. """
  271. 获取window的系统缩放 一般是1
  272. :return:
  273. """
  274. hDC = GetDC(0)
  275. # 物理上(真实的)的 横纵向分辨率
  276. wReal = GetDeviceCaps(hDC, DESKTOPHORZRES)
  277. hReal = GetDeviceCaps(hDC, DESKTOPVERTRES)
  278. # 缩放后的 分辨率
  279. wAfter = GetSystemMetrics(0)
  280. hAfter = GetSystemMetrics(1)
  281. # print(wReal, wAfter)
  282. return round(wReal / wAfter, 2)
  283. if __name__ == '__main__':
  284. h = Handle(config='oas1')
  285. logger.info(h.auto_handle_title(h.all_windows()))
  286. logger.info(h.root_handle_num)
  287. logger.info(h.emulator_family)