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 RLBOT_SERVER_NAME , OS , CURRENT_OS
1113
1214
1315class MatchManager :
@@ -23,11 +25,17 @@ class MatchManager:
2325
2426 def __init__ (
2527 self ,
26- main_executable_path : Path | None = None ,
27- main_executable_name : str = MAIN_EXECUTABLE_NAME ,
28+ rlbot_server_path : Path | None = None
2829 ):
29- self .main_executable_path = main_executable_path
30- self .main_executable_name = main_executable_name
30+ """
31+ Initialize a MatchManager.
32+ Args:
33+ rlbot_server_path: The path to the RLBotServer executable. The path is used to launch the server
34+ if it is not already running. If set to None, the RLBotServer in the current working directory will
35+ be used, and otherwise the globally installed RLBotServer will be used.
36+ """
37+
38+ self .rlbot_server_path = rlbot_server_path
3139
3240 self .rlbot_interface : SocketRelay = SocketRelay ("" )
3341 self .rlbot_interface .packet_handlers .append (self ._packet_reporter )
@@ -41,27 +49,60 @@ def __exit__(self, exc_type, exc_val, exc_tb) -> None:
4149 def ensure_server_started (self ):
4250 """
4351 Ensures that RLBotServer is running, starting it if it is not.
52+ If no path to an RLBotServer executable was passed during initialization of the MatchManager,
53+ the function will use the RLBotServer executable in the current working directory, if any, or
54+ otherwise the global installed RLBotServer will be used, if any.
4455 """
4556
46- self .rlbot_server_process , self .rlbot_server_port = gateway .find_server_process (
47- self .main_executable_name
48- )
57+ exe_name = self .rlbot_server_path .stem if self .rlbot_server_path is not None and self .rlbot_server_path .is_file () else RLBOT_SERVER_NAME
58+ self .rlbot_server_process , self .rlbot_server_port = gateway .find_server_process (exe_name )
4959 if self .rlbot_server_process is not None :
50- self .logger .info ("Already have %s running!" , self . main_executable_name )
60+ self .logger .info ("%s is already running!" , exe_name )
5161 return
5262
53- if self .main_executable_path is None :
54- self .main_executable_path = Path .cwd ()
63+ if self .rlbot_server_path is None :
64+ # Look in cwd or localappdata
65+ path = Path .cwd () / RLBOT_SERVER_NAME
66+ if not path .exists () and CURRENT_OS == OS .WINDOWS :
67+ self .logger .debug (f"Could not find RLBotServer in cwd ('{ path .parent } '), trying %localappdata% instead." )
68+ path = Path (os .environ .get ("LOCALAPPDATA" )) / "RLBot5" / "bin" / RLBOT_SERVER_NAME
69+ if not path .exists ():
70+ raise FileNotFoundError (
71+ "Unable to find RLBotServer in the current working directory "
72+ "or in the default installation location. "
73+ "Is your antivirus messing you up? Check "
74+ "https://github.com/RLBot/RLBot/wiki/Antivirus-Notes."
75+ )
76+ else :
77+ # User specified path
78+ path = self .rlbot_server_path
79+ if path .exists () and path .is_dir ():
80+ path = path / RLBOT_SERVER_NAME
81+ if not path .exists ():
82+ raise FileNotFoundError (f"Unable to find RLBotServer at the specified path '{ path } '." )
83+
84+ if path is None or not os .access (path , os .F_OK ):
85+ raise FileNotFoundError (
86+ f"Unable to find RLBotServer at '{ path } '. "
87+ "Is your antivirus messing you up? Check "
88+ "https://github.com/RLBot/RLBot/wiki/Antivirus-Notes."
89+ )
90+
91+ if not os .access (path , os .X_OK ):
92+ os .chmod (path , stat .S_IXUSR | stat .S_IXGRP | stat .S_IXOTH )
5593
56- rlbot_server_process , self .rlbot_server_port = gateway .launch (
57- self .main_executable_path ,
58- self .main_executable_name ,
59- )
94+ if not os .access (path , os .X_OK ):
95+ raise PermissionError (
96+ "Unable to execute RLBotServer due to file permissions! Is your antivirus messing you up? "
97+ f"Check https://github.com/RLBot/RLBot/wiki/Antivirus-Notes. The exact path is '{ path } '"
98+ )
99+
100+ rlbot_server_process , self .rlbot_server_port = gateway .launch (path )
60101 self .rlbot_server_process = psutil .Process (rlbot_server_process .pid )
61102
62103 self .logger .info (
63104 "Started %s with process id %s" ,
64- self . main_executable_name ,
105+ path ,
65106 self .rlbot_server_process .pid ,
66107 )
67108
0 commit comments