def __init__(self, run_config, full_screen=False, game_version=None, data_version=None, **kwargs): self._proc = None self._sock = None self._controller = None self._tmp_dir = tempfile.mkdtemp(prefix="sc-", dir=run_config.tmp_dir) self._port = portpicker.pick_unused_port() exec_path = run_config.exec_path(game_version) self._check_exists(exec_path) args = [ exec_path, "-listen", "127.0.0.1", "-port", str(self._port), "-dataDir", os.path.join(run_config.data_dir, ""), "-tempDir", os.path.join(self._tmp_dir, ""), "-displayMode", "1" if full_screen else "0", ] if data_version: args += ["-dataVersion", data_version] try: self._proc = self._launch(run_config, args, **kwargs) self._sock = self._connect(self._port) client = protocol.StarcraftProtocol(self._sock) self._controller = remote_controller.RemoteController(client) with sw("startup"): self._controller.ping() except: self.close() raise
def connect(self, url=c.LOCALHOST, port=None, timeout=c.INITIAL_TIMEOUT, debug=False): """socket connect to an already running starcraft2 process""" if port != None: # force a selection to a new port if self._port != None: # if previously allocated port, return it portpicker.return_port(self._port) self._port = port elif self._port == None: # no connection exists self._port = portpicker.pick_unused_port() self._url = url if ":" in url and not url.startswith("["): # Support ipv6 addresses. url = "[%s]" % url for i in range(timeout): startTime = time.time() if debug: print("attempt #%d to websocket connect to %s:%s" % (i, url, port)) try: finalUrl = "ws://%s:%s/sc2api" % (url, self._port) ws = websocket.create_connection(finalUrl, timeout=timeout) #print("ws:", ws) self._client = protocol.StarcraftProtocol(ws) #super(ClientController, self).__init__(client) # ensure RemoteController initializtion is performed #if self.ping(): print("init ping()") # ensure the latest state is synced # ping returns: # game_version: "4.1.2.60604" # data_version: "33D9FE28909573253B7FC352CE7AEA40" # data_build: 60604 # base_build: 60321 return self except socket.error: pass # SC2 hasn't started listening yet. except websocket.WebSocketException as err: print(err, type(err)) if "Handshake Status 404" in str(err): pass # SC2 is listening, but hasn't set up the /sc2api endpoint yet. else: raise except Exception as e: print(type(e), e) sleepTime = max( 0, 1 - (time.time() - startTime)) # try to wait for up to 1 second total if sleepTime: time.sleep(sleepTime) raise websocket.WebSocketException( "Could not connect to game at %s on port %s" % (url, port))
def __init__(self, host, port, proc=None, timeout_seconds=None): timeout_seconds = timeout_seconds or 120 sock = self._connect(host, port, proc, timeout_seconds) self._client = protocol.StarcraftProtocol(sock) self.ping()
def __init__(self, host, port, proc=None, timeout_seconds=None): timeout_seconds = timeout_seconds or FLAGS.sc2_timeout sock = self._connect(host, port, proc, timeout_seconds) self._client = protocol.StarcraftProtocol(sock) self._last_obs = None self.ping()