使用带有参数的create_subprocess_exec启动Dota2

时间:2019-07-18 14:27:59

标签: python asyncio

我正在尝试使用python 3.7在Windows 10盒子上启动Dota2,并指定了参数,这些参数将使用asyncio.create_subprocess_exec()自动启动机器人游戏。该游戏目前正在启动,但似乎参数被忽略了,因为它不会自动启动机器人游戏,而只是Dota2应用程序。

使用subprocess模块和Popen()做类似的事情就可以了。

import asyncio
from sys import platform

args = [
    'C:\\Program Files (x86)\\Steam\\steamapps\\common\\dota 2 beta\\game\\bin\\win64\\dota2.exe',
    '-con_logfile scripts/vscripts/bots/console.log',
    '-con_timestamp',
    '-console',
    '-dev',
    '-insecure',
    '-noip',
    '-nowatchdog',
    '+clientport 27006',
    '+dota_1v1_skip_strategy 1',
    '+dota_surrender_on_disconnect 0',
    '+host_timescale 1',
    '+hostname dotaservice',
    '+sv_cheats 1',
    '+sv_hibernate_when_empty 0',
    '+tv_delay 0',
    '+tv_enable 1',
    '+tv_title some_uuid',
    '+tv_autorecord 1',
    '+tv_transmitall 1',
    '-fill_with_bots',
    '+map',
    'start gamemode 6',
    '+sv_lan 1'
]

def main_func():
    if platform == "win32":
        asyncio.set_event_loop(asyncio.ProactorEventLoop())
    loop = asyncio.get_event_loop()
    tasks = offline_main()
    loop.run_until_complete(tasks)

async def offline_main():
    create = asyncio.create_subprocess_exec(args[0], ' '.join(args[1:]), stdin=asyncio.subprocess.PIPE, stdout=None, stderr=None)
    await create

#main_func()

def sub_func():
    import subprocess
    p = subprocess.Popen('"'+args[0]+'" '+' '.join(args[1:]), stdin=subprocess.PIPE, stdout=None, stderr=None)

#sub_func()

如果您取消注释sub_func(),它就可以正常工作。如果您取消注释main_func(),它将启动Dota2 Application,但不会启动机器人游戏。我意识到所传递的参数存在细微差异,但是如果我使用:

create = asyncio.create_subprocess_exec('"'+args[0]+'" '+' '.join(args[1:]), stdin=asyncio.subprocess.PIPE, stdout=None, stderr=None)

由于某种原因,我得到了PermissionError: [WinError 5] Access is denied

1 个答案:

答案 0 :(得分:0)

如果您将所有参数(包括要设置为它们的值)分开,结果是可行的。

以下代码有效:

import asyncio
from sys import platform

args = [
    'C:\\Program Files (x86)\\Steam\\steamapps\\common\\dota 2 beta\\game\\bin\\win64\\dota2.exe',
    '-con_logfile', 'scripts/vscripts/bots/console.log',
    '-con_timestamp',
    '-console',
    '-dev',
    '-insecure',
    '-noip',
    '-nowatchdog',
    '+clientport', '27006',
    '+dota_1v1_skip_strategy', '1',
    '+dota_surrender_on_disconnect', '0',
    '+host_timescale', '1',
    '+hostname dotaservice',
    '+sv_cheats', '1',
    '+sv_hibernate_when_empty', '0',
    '+tv_delay', '0',
    '+tv_enable', '1',
    '+tv_title', 'some_uuid',
    '+tv_autorecord', '1',
    '+tv_transmitall', '1',
    '-fill_with_bots',
    '+map',
    'start', 'gamemode', '6',
    '+sv_lan', '1'
]

def main_func():
    if platform == "win32":
        asyncio.set_event_loop(asyncio.ProactorEventLoop())
    loop = asyncio.get_event_loop()
    tasks = offline_main()
    loop.run_until_complete(tasks)

async def offline_main():
    create = asyncio.create_subprocess_exec(*args, stdin=asyncio.subprocess.PIPE, stdout=None, stderr=None)
    await create

main_func()

def sub_func():
    import subprocess
    p = subprocess.Popen('"'+args[0]+'" '+' '.join(args[1:]), stdin=subprocess.PIPE, stdout=None, stderr=None)

#sub_func()