helper.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. import ctypes
  2. import os
  3. import threading
  4. import time
  5. from dataclasses import dataclass
  6. from model.custom_struct import GameConfig, AccountInfo
  7. from model.global_manager import GM
  8. from scripts.script import Script
  9. from tools.dm_operate import Dm
  10. from tools.emulator.emulator import Emulator
  11. from tools.emulator.ld_operate import LD
  12. from tools.emulator.ys_operate import YS
  13. from tools.upload_log import LogInfo
  14. from tools.utils import Utils
  15. @dataclass
  16. class ScriptHelper:
  17. dm: Dm = None # 大漠对象
  18. bind_handle: int = None # 绑定句柄
  19. top_handle: int = None # 顶层句柄
  20. emulator_index: int = None # 模拟器下标
  21. timeout: int = None # 超时时间
  22. game_id: str = None # 游戏id
  23. game_type: str = None # 游戏类型
  24. account: str = None # 账号
  25. password: str = None # 密码
  26. retained: int = None # 新增留存
  27. log_uuid: str = None # 日志uuid
  28. log_port: int = None # 日志端口
  29. emu: Emulator = None # 模拟器对象
  30. class MyHelper:
  31. """模拟器任务助手"""
  32. _lock = threading.Lock()
  33. def __init__(self, window_id: int, game_config: GameConfig, account_info: AccountInfo):
  34. self.window_id = window_id
  35. self.game_config = game_config
  36. self.account_info = account_info
  37. self.emulator_type = self.game_config.emulator_type
  38. self.emu = self._initialize_emulator_instance()
  39. self.emulator_index = self._get_emulator()
  40. self.dm = Dm()
  41. self.emulator_info = None
  42. self.wy_ip = GM.wy_ip
  43. self.log_port = GM.script_log_port
  44. self.script = Script(self.game_config.script)
  45. self.log_uuid = f'{time.time()}_{self.account_info.account}'
  46. self.upload_log = LogInfo(self.log_uuid)
  47. self.upload_account_log(GM.device_info.pc_name, GM.device_info.officer, GM.device_mac)
  48. def _initialize_emulator_instance(self):
  49. emulator_mapping = {
  50. '雷电模拟器4-32位': lambda: LD(4),
  51. '雷电模拟器4-64位': lambda: LD(64),
  52. '雷电模拟器9': lambda: LD(9),
  53. '夜神模拟器7-32位': lambda: YS(7),
  54. '夜神模拟器7-64位': lambda: YS(8),
  55. '夜神模拟器9': lambda: YS(9)
  56. }
  57. # 仅当查找到类型时才会实例化,否则返回默认的 LD(9)
  58. return emulator_mapping.get(self.emulator_type, lambda: LD(9))()
  59. def _get_emulator(self):
  60. with MyHelper._lock:
  61. index = self.window_id
  62. while GM.get_global_control():
  63. if self.emu.is_exists(index):
  64. if GM.get_emulator_status(f'{self.emulator_type}-{index}') == 0:
  65. GM.set_emulator_status(f'{self.emulator_type}-{index}', 1)
  66. return index
  67. raise Exception(f'模拟器-{index}状态异常')
  68. else:
  69. # 模拟器不存在,添加新的模拟器
  70. self.emu.add(f'new-{index}')
  71. time.sleep(3)
  72. def get_emulator_index(self):
  73. return self.emulator_index
  74. def set_emulator_index(self, index):
  75. self.emulator_index = index
  76. def restore_emulator(self):
  77. try:
  78. self.emu.restore(self.emulator_index, os.path.join(GM.image_path, self.game_config.image))
  79. time.sleep(1)
  80. self.emu.rename(self.emulator_index, f'{self.game_config.task_id}-{self.emulator_index}')
  81. return True, '还原模拟器成功'
  82. except Exception as e:
  83. return False, str(e)
  84. def modify_emulator(self):
  85. try:
  86. self.emu.modify(self.emulator_index,
  87. cpu=self.game_config.cpu,
  88. memory=self.game_config.memory,
  89. resolution=self.game_config.resolution,
  90. manufacturer=self.account_info.manufacturer,
  91. model=self.account_info.model,
  92. pnumber=self.account_info.pnumber,
  93. imei=self.account_info.imei,
  94. imsi=self.account_info.imsi,
  95. simserial=self.account_info.simserial,
  96. androidid=self.account_info.androidid,
  97. mac=self.account_info.mac if self.emu.emulator_type == 'ld' else self.account_info.mac_colon
  98. )
  99. return True, '修改模拟器信息成功'
  100. except Exception as e:
  101. return False, str(e)
  102. def switch_emulator_area(self):
  103. if self.wy_ip is None:
  104. return False, "未初始化无忧IP"
  105. self.wy_ip.set_wy_token()
  106. if len(self.account_info.number_operatorTypes.split('_')) < 1:
  107. return False, "number_operatorTypes参数错误"
  108. return self.wy_ip.switch_emulator_area(self.emulator_index,
  109. self.account_info.number_operatorTypes.split('_')[1],
  110. self.account_info.number_operatorTypes.split('_')[0])
  111. def confirm_emulator_area(self):
  112. result, message = self.wy_ip.queryProcessProxyRegion(self.emulator_info.vbox_pid)
  113. if not result:
  114. return result, message
  115. if message != self.account_info.number_operatorTypes.split('_')[0]:
  116. return False, f'当前模拟器切换地区失败,目标地区:{self.account_info.number_operatorTypes.split("_")[0]},当前地区:{message}'
  117. return True, '当前模拟器切换地区成功'
  118. def start_emulator_and_set_position(self):
  119. """启动模拟器并设置窗口位置"""
  120. try:
  121. # 获取共享路径
  122. share_path = GM.get_share_path()
  123. emulator_share_dir = os.path.join(share_path, str(self.window_id))
  124. # 如果目录不存在则创建
  125. if not os.path.exists(emulator_share_dir):
  126. os.makedirs(emulator_share_dir)
  127. # 设置共享目录
  128. self.emu.set_share_dir(self.emulator_index, emulator_share_dir)
  129. # 启动模拟器并确认启动状态
  130. result, message = self.emu.start_and_confirm(self.emulator_index)
  131. if result:
  132. self.emulator_info = message
  133. # 设置模拟器窗口位置
  134. self.emu.set_emulator_position(self.emulator_info.top_handle, self.window_id)
  135. time.sleep(1)
  136. return True, f"模拟器编号:{self.emulator_index},启动成功"
  137. return False, f"模拟器编号:{self.emulator_index},,启动失败,错误信息:{message}"
  138. except Exception as e:
  139. return False, f"模拟器编号:{self.emulator_index},启动失败,错误信息:{str(e)}"
  140. def close_emulator_and_confirm(self):
  141. """关闭模拟器并确保所有相关进程被终止。"""
  142. # 检查模拟器是否正在运行
  143. if self.emu.is_running(self.emulator_index):
  144. self.emu.close(self.emulator_index)
  145. time.sleep(3) # 等待模拟器关闭的缓冲时间
  146. # 获取模拟器信息
  147. emu_info = self.emu.get_info(self.emulator_index)
  148. if emu_info:
  149. # 确认主进程 pid 有效
  150. if emu_info.pid > 0:
  151. Utils.kill_process(emu_info.pid)
  152. # 确认 VBox 进程 pid 有效
  153. if emu_info.vbox_pid > 0:
  154. Utils.kill_process(emu_info.vbox_pid)
  155. def bind_emulator(self):
  156. try:
  157. if self.game_config.script.endswith('.dll'):
  158. result = self.script.execute('dm_bind',
  159. self.emulator_index,
  160. self.emulator_info.bind_handle,
  161. timeout=10
  162. )
  163. return True, result
  164. elif self.game_config.script.endswith('.py'):
  165. script_helper = ScriptHelper(dm=self.dm,
  166. emu=self.emu,
  167. emulator_index=self.emulator_index,
  168. top_handle=self.emulator_info.top_handle,
  169. bind_handle=self.emulator_info.bind_handle,
  170. game_type=self.game_config.game_type,
  171. game_id=self.game_config.task_id,
  172. account=self.account_info.account,
  173. password=self.account_info.password,
  174. retained=self.account_info.retained,
  175. log_uuid=self.log_uuid,
  176. log_port=self.log_port,
  177. timeout=self.game_config.timeout * 60
  178. )
  179. result = self.script.execute('dm_bind', script_helper, timeout=10)
  180. return True, result
  181. else:
  182. return False, '脚本类型错误'
  183. except Exception as e:
  184. return False, str(e)
  185. def start_game(self):
  186. """启动游戏并处理可能的异常。
  187. Returns:
  188. tuple: 成功与否的标志和结果或错误信息。
  189. """
  190. try:
  191. if self.game_config.script.endswith('.dll'):
  192. # 获取字符串的字节数据
  193. task_id_bytes = self.game_config.task_id.encode('gbk')
  194. account_bytes = self.account_info.account.encode('gbk')
  195. script_path_bytes = self.script.script_path.encode('gbk')
  196. log_uuid_bytes = self.log_uuid.encode('gbk')
  197. # 创建 c_char_p 指针
  198. task_id_ptr = ctypes.c_char_p(task_id_bytes)
  199. account_ptr = ctypes.c_char_p(account_bytes)
  200. script_path_ptr = ctypes.c_char_p(script_path_bytes)
  201. log_uuid_ptr = ctypes.c_char_p(log_uuid_bytes)
  202. result = self.script.execute(
  203. 'script_start_game',
  204. self.emulator_index,
  205. task_id_ptr,
  206. self.emulator_info.bind_handle,
  207. account_ptr,
  208. self.game_config.timeout * 60 * 1000,
  209. script_path_ptr,
  210. GM.script_log_port,
  211. log_uuid_ptr,
  212. timeout=30 * 60
  213. )
  214. return True, result
  215. elif self.game_config.script.endswith('.py'):
  216. script_helper = ScriptHelper(dm=self.dm,
  217. emu=self.emu,
  218. emulator_index=self.emulator_index,
  219. top_handle=self.emulator_info.top_handle,
  220. bind_handle=self.emulator_info.bind_handle,
  221. game_id=self.game_config.task_id,
  222. game_type=self.game_config.game_type,
  223. account=self.account_info.account,
  224. password=self.account_info.password,
  225. retained=self.account_info.retained,
  226. log_uuid=self.log_uuid,
  227. log_port=self.log_port,
  228. timeout=self.game_config.timeout * 60
  229. )
  230. result = self.script.execute('script_start_game',script_helper, timeout=30*60)
  231. return True, result
  232. else:
  233. return False, "脚本文件格式错误"
  234. except Exception as e:
  235. return False, f"脚本_启动游戏,错误: {e}"
  236. def login_game(self):
  237. """登录游戏并处理可能的异常。
  238. Returns:
  239. tuple: 成功与否的标志和结果或错误信息。
  240. """
  241. try:
  242. if self.game_config.script.endswith('.dll'):
  243. # 获取字符串的字节数据
  244. account_bytes = self.account_info.account.encode('gbk')
  245. password_bytes = self.account_info.password.encode('gbk')
  246. wechat_order_id_bytes = self.account_info.wechat_order_id.encode('gbk')
  247. task_id_bytes = self.game_config.task_id.encode('gbk')
  248. script_path_bytes = self.script.script_path.encode('gbk')
  249. channel_id_bytes = self.game_config.channel_id.encode('gbk')
  250. # 创建 c_char_p 指针
  251. account_ptr = ctypes.c_char_p(account_bytes)
  252. password_ptr = ctypes.c_char_p(password_bytes)
  253. wechat_order_id_ptr = ctypes.c_char_p(wechat_order_id_bytes)
  254. task_id_ptr = ctypes.c_char_p(task_id_bytes)
  255. script_path_ptr = ctypes.c_char_p(script_path_bytes)
  256. channel_id_ptr = ctypes.c_char_p(channel_id_bytes)
  257. result = self.script.execute(
  258. 'script_login_game',
  259. self.emulator_index, # 窗口下标
  260. self.game_config.timeout * 60 * 1000, # 超时时间
  261. self.emulator_info.bind_handle,
  262. account_ptr,
  263. password_ptr,
  264. self.account_info.retained,
  265. wechat_order_id_ptr,
  266. 1,
  267. task_id_ptr,
  268. script_path_ptr,
  269. GM.script_log_port,
  270. channel_id_ptr,
  271. timeout=30 * 60
  272. )
  273. return True, result
  274. elif self.game_config.script.endswith('.py'):
  275. script_helper = ScriptHelper(dm=self.dm,
  276. emu=self.emu,
  277. emulator_index=self.emulator_index,
  278. top_handle=self.emulator_info.top_handle,
  279. bind_handle=self.emulator_info.bind_handle,
  280. game_id=self.game_config.task_id,
  281. game_type=self.game_config.game_type,
  282. account=self.account_info.account,
  283. password=self.account_info.password,
  284. retained=self.account_info.retained,
  285. log_uuid=self.log_uuid,
  286. log_port=self.log_port,
  287. timeout=self.game_config.timeout * 60
  288. )
  289. result = self.script.execute('script_login_game',script_helper, timeout=30 * 60)
  290. return True, result
  291. else:
  292. return False, "脚本文件格式错误"
  293. except Exception as e:
  294. return False, f'脚本_登陆游戏,错误: {e}'
  295. def main_task(self):
  296. """执行主线任务并处理可能的异常。
  297. Returns:
  298. tuple: 成功与否的标志和结果或错误信息。
  299. """
  300. try:
  301. if self.game_config.script.endswith('.dll'):
  302. # 获取字符串的字节数据
  303. account_bytes = self.account_info.account.encode('gbk')
  304. task_id_bytes = self.game_config.task_id.encode('gbk')
  305. script_path_bytes = self.script.script_path.encode('gbk')
  306. # 创建 c_char_p 指针
  307. account_ptr = ctypes.c_char_p(account_bytes)
  308. task_id_ptr = ctypes.c_char_p(task_id_bytes)
  309. script_path_ptr = ctypes.c_char_p(script_path_bytes)
  310. result = self.script.execute(
  311. 'script_main_task',
  312. self.emulator_index,
  313. self.game_config.timeout * 60 * 1000,
  314. account_ptr, # 直接传递数据地址
  315. 0,
  316. self.emulator_info.bind_handle,
  317. task_id_ptr, # 直接传递数据地址
  318. script_path_ptr, # 直接传递数据地址
  319. GM.script_log_port,
  320. timeout=60 * 60
  321. )
  322. return True, result
  323. elif self.game_config.script.endswith('.py'):
  324. script_helper = ScriptHelper(dm=self.dm,
  325. emu=self.emu,
  326. emulator_index=self.emulator_index,
  327. top_handle=self.emulator_info.top_handle,
  328. bind_handle=self.emulator_info.bind_handle,
  329. game_id=self.game_config.task_id,
  330. game_type=self.game_config.game_type,
  331. account=self.account_info.account,
  332. password=self.account_info.password,
  333. retained=self.account_info.retained,
  334. log_uuid=self.log_uuid,
  335. log_port=self.log_port,
  336. timeout=self.game_config.timeout * 60
  337. )
  338. result = self.script.execute('script_main_task', script_helper, timeout=60 * 60)
  339. return True, result
  340. else:
  341. return False, "脚本文件格式错误"
  342. except Exception as e:
  343. return False, f'脚本_教程主线,错误: {e}'
  344. def upload_account_log(self, pc_code: str, operator: str, pc_mac: str):
  345. self.upload_log.set_account_info(game_id=int(self.game_config.task_id),
  346. account_type=self.account_info.game_type,
  347. pwd=self.account_info.password,
  348. account=self.account_info.account,
  349. task_type=self.account_info.retained)
  350. self.upload_log.set_pc_info(pc_code=pc_code, operator=operator, pc_mac=pc_mac, pc_ip='')
  351. self.upload_log.upload_pull_account_log(1)
  352. def upload_device_log(self, status: int):
  353. try:
  354. self.upload_log.set_device_info(device_id=self.emu.get_android_id(self.emulator_index),
  355. device_manufacturer=self.emu.get_manufacturer(self.emulator_index),
  356. device_model=self.emu.get_model(self.emulator_index),
  357. device_imei=self.emu.get_IMEI(self.emulator_index),
  358. device_sdk=self.emu.get_android_version(self.emulator_index),
  359. device_mac=self.emu.get_mac(self.emulator_index),
  360. device_number=self.emu.get_phone_number(self.emulator_index),
  361. script_device_id='')
  362. self.upload_log.set_simulator_info(simulator_code=str(self.emulator_index),
  363. simulator_mac='',
  364. simulator_ip_city='',
  365. simulator_ip=self.emu.get_net_ip(self.emulator_index)
  366. )
  367. ret = self.upload_log.upload_start_simulator_log(status=status)
  368. return ret
  369. except Exception as e:
  370. return False, f'脚本_设备信息,错误: {e}'