python控制雷电模拟器 代码修复

from concurrent.futures import thread
import winreg, os ,time # 雷电模拟器自动获取目录使用
import random
#----------------------------------------------------------------
def get_ldconsole(pro_name):
    def get_ld_dir():
        try:
            key = winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, "Software\ChangZhi2\dnplayer")
            v = winreg.QueryValueEx(key, "InstallDir")
            winreg.CloseKey(key)
            return v[0]
        except Exception as e:
            print(e)
            return "没有找到模拟器路径"  
    return os.path.join(get_ld_dir(), pro_name)
console1 = get_ldconsole("ldconsole.exe")
console = console1.replace('\\','\\\\')
ld1 = get_ldconsole("ld.exe")
ld = ld1.replace('\\','\\\\') 
#----------------------------------------------------------------

class Dnconsole:
    # 模拟器信息配置
    # ldpath = 'D:\\ChangZhi\\dnplayer2\\'
    # console = 'D:\\ChangZhi\\dnplayer2\\ldconsole.exe'
    # ld = 'D:\\ChangZhi\\dnplayer2\\ld.exe'
    
#获取模拟器列表  修复完成
    @staticmethod
    def get_list():
        cmd = os.popen('{} list2'.format(console))
        text = cmd.read()
        cmd.close()
        info = text.split('\n')
        result = list()
        for line in info:
            if len(line) > 1:
                dnplayer = line.split(',')
                result.append(dnplayer)
        return result
#获取正在运行的模拟器列表 修复完成  返回模拟器列表信息
    @staticmethod
    def list_running() -> list:
        result = list()
        all = Dnconsole.get_list()
        for dn in all:
            if dn[2]  != '0':
                result.append(dn)
        return result
#检测指定序号的模拟器是否正在运行 修复完成  返回模拟器信息
    @staticmethod
    def is_running(index: int) -> bool:
        all = Dnconsole.list_running()
        for i in all:
            if index == int(i[0]):
                return i
            else: return '检查【{}】号模拟器是否在运行'.format(index)
 
#执行shell命令  ld控制adb 控制模拟器本身ldconsole从中模拟器游戏动作
    @staticmethod
    def dnld(index: int, command: str, silence: bool = True):
        cmd = ld + ' -s {} {}'.format(index, command)
        if silence:
            os.system(cmd)
            return ''
        process = os.popen(cmd)
        result = process.read()
        process.close()
        return result
        
 
#启动App 指定模拟器必须已经启动
    @staticmethod  #参数:(模拟器号,点击x,y,点击时间 毫秒)
    def run_app(index: int, x: int, y: int, delay: int = 0):
        Dnconsole.touch(index, x, y, delay) #点两次,防止漏启动的
        Dnconsole.touch(index, x, y, delay)

#输入文字
    @staticmethod  # 已修复
    def input_text(index: int, text: str):
        process = os.popen('{} action --index {} --key call.input --value {}'.format(console, index, text))
        result = process.read()
        process.close()
        return result
 
 
#启动模拟器
    @staticmethod
    def launch(index: int, launchtime):
       if launchtime > 4:
            os.system("start explorer LD_launch\\LD_launch{}.bat".format(index))
            time.sleep(launchtime)
 
         
#关闭模拟器
    @staticmethod
    def quit(index: int):
        cmd = '{} quit --index {}'.format(console, str(index))
        process = os.popen(cmd)
        result = process.read()
        process.close()
        return result
 
 
#点击或者长按某点,延时不传数据默认0,设置时间长按
    @staticmethod
    def image_DIY_xy(index: int, DIY_click, delay: int = 0):
        l_x = DIY_click[0]  #目标图的左上角坐标x  
        l_y = DIY_click[1]   #左上角坐标y
        max_x = DIY_click[2] #点击范围右下角x
        max_y = DIY_click[3] # 点击区域右下角y
        x_click = random.randint(l_x , max_x) #y轴随机数
        y_click = random.randint(l_y , max_y) #y轴随机数
        if delay == 0:
            Dnconsole.dnld(index, 'input tap {} {}'.format(x_click, y_click))
        else:
            Dnconsole.dnld(index, 'input swipe {} {} {} {} {}'.format(x_click, y_click,x_click, y_click, delay)) # touch失效 
#滑动
    @staticmethod
    def move_finger(index, start_x, start_y, end_x, end_y, time_min,time_max, delay: int = 0):
        click_time = random.randint(time_min, time_max)  #随机点击时间随机 100到300毫秒
        if delay == 1:
            Dnconsole.dnld(index, 'input swipe {} {} {} {}'.format(x0, y0, x1, y1))
        else:
            Dnconsole.dnld(index, 'input swipe {} {} {} {} {}'.format(x0, y0, x1, y1, click_time))
 
 
    
 

# if __name__ == '__main__':
#  Dnconsole.launch(0)# 打开模拟器
#  time.sleep(10)# 等待启动
#  #: 其他的控制(touch)
#  Dnconsole.quit(0)# 退出模拟器
#--------获取正在运行的模拟器标题名称
#     runlist = Dnconsole.get_list()  
#     for i in runlist:
#         if i[2]  != '0':
#             print(i[1])
# # #----------------------------------
 

你可能感兴趣的:(python)