1+ import os
2+ import stat
13from pathlib import Path
24from time import sleep
35
79from rlbot .interface import RLBOT_SERVER_IP , RLBOT_SERVER_PORT , SocketRelay
810from rlbot .utils import fill_desired_game_state , gateway
911from rlbot .utils .logging import DEFAULT_LOGGER
10- from rlbot .utils .os_detector import MAIN_EXECUTABLE_NAME
12+ from rlbot .utils .os_detector import CURRENT_OS , OS , RLBOT_SERVER_NAME
1113
1214
1315class MatchManager :
@@ -21,13 +23,16 @@ class MatchManager:
2123 rlbot_server_port = RLBOT_SERVER_PORT
2224 initialized = False
2325
24- def __init__ (
25- self ,
26- main_executable_path : Path | None = None ,
27- main_executable_name : str = MAIN_EXECUTABLE_NAME ,
28- ):
29- self .main_executable_path = main_executable_path
30- self .main_executable_name = main_executable_name
26+ def __init__ (self , rlbot_server_path : Path | None = None ):
27+ """
28+ Initialize a MatchManager.
29+ Args:
30+ rlbot_server_path: The path to the RLBotServer executable. The path is used to launch the server
31+ if it is not already running. If set to None, the RLBotServer in the current working directory will
32+ be used, and otherwise the globally installed RLBotServer will be used.
33+ """
34+
35+ self .rlbot_server_path = rlbot_server_path
3136
3237 self .rlbot_interface : SocketRelay = SocketRelay ("" )
3338 self .rlbot_interface .packet_handlers .append (self ._packet_reporter )
@@ -41,27 +46,75 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
4146 def ensure_server_started (self ):
4247 """
4348 Ensures that RLBotServer is running, starting it if it is not.
49+ If no path to an RLBotServer executable was passed during initialization of the MatchManager,
50+ the function will use the RLBotServer executable in the current working directory, if any, or
51+ otherwise the global installed RLBotServer will be used, if any.
4452 """
4553
54+ exe_name = (
55+ self .rlbot_server_path .stem
56+ if self .rlbot_server_path is not None and self .rlbot_server_path .is_file ()
57+ else RLBOT_SERVER_NAME
58+ )
4659 self .rlbot_server_process , self .rlbot_server_port = gateway .find_server_process (
47- self . main_executable_name
60+ exe_name
4861 )
4962 if self .rlbot_server_process is not None :
50- self .logger .info ("Already have %s running!" , self . main_executable_name )
63+ self .logger .info ("%s is already running!" , exe_name )
5164 return
5265
53- if self .main_executable_path is None :
54- self .main_executable_path = Path .cwd ()
66+ if self .rlbot_server_path is None :
67+ # Look in cwd or localappdata
68+ path = Path .cwd () / RLBOT_SERVER_NAME
69+ if not path .exists () and CURRENT_OS == OS .WINDOWS :
70+ self .logger .debug (
71+ f"Could not find RLBotServer in cwd ('{ path .parent } '), trying %localappdata% instead."
72+ )
73+ path = (
74+ Path (os .environ .get ("LOCALAPPDATA" ))
75+ / "RLBot5"
76+ / "bin"
77+ / RLBOT_SERVER_NAME
78+ )
79+ if not path .exists ():
80+ raise FileNotFoundError (
81+ "Unable to find RLBotServer in the current working directory "
82+ "or in the default installation location. "
83+ "Is your antivirus messing you up? Check "
84+ "https://github.com/RLBot/RLBot/wiki/Antivirus-Notes."
85+ )
86+ else :
87+ # User specified path
88+ path = self .rlbot_server_path
89+ if path .exists () and path .is_dir ():
90+ path = path / RLBOT_SERVER_NAME
91+ if not path .exists ():
92+ raise FileNotFoundError (
93+ f"Unable to find RLBotServer at the specified path '{ path } '."
94+ )
95+
96+ if path is None or not os .access (path , os .F_OK ):
97+ raise FileNotFoundError (
98+ f"Unable to find RLBotServer at '{ path } '. "
99+ "Is your antivirus messing you up? Check "
100+ "https://github.com/RLBot/RLBot/wiki/Antivirus-Notes."
101+ )
102+
103+ if not os .access (path , os .X_OK ):
104+ os .chmod (path , stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
55105
56- rlbot_server_process , self .rlbot_server_port = gateway .launch (
57- self .main_executable_path ,
58- self .main_executable_name ,
59- )
106+ if not os .access (path , os .X_OK ):
107+ raise PermissionError (
108+ "Unable to execute RLBotServer due to file permissions! Is your antivirus messing you up? "
109+ f"Check https://github.com/RLBot/RLBot/wiki/Antivirus-Notes. The exact path is '{ path } '"
110+ )
111+
112+ rlbot_server_process , self .rlbot_server_port = gateway .launch (path )
60113 self .rlbot_server_process = psutil .Process (rlbot_server_process .pid )
61114
62115 self .logger .info (
63116 "Started %s with process id %s" ,
64- self . main_executable_name ,
117+ path ,
65118 self .rlbot_server_process .pid ,
66119 )
67120
0 commit comments