replacing reliance on alias for connecting with steam native connect command. downloading maps through sourcemod saying the nextmap. should assure that bots are connected much faster from now on in almost all cases.
This commit is contained in:
@@ -7,16 +7,18 @@ import pwd
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
import random
|
||||
import bz2
|
||||
import signal
|
||||
import socket
|
||||
import codecs
|
||||
import json
|
||||
import requests
|
||||
import datetime
|
||||
import time
|
||||
import glob
|
||||
import pwd
|
||||
|
||||
restart_time = datetime.datetime.now() + datetime.timedelta(hours=3)
|
||||
restart_time = datetime.datetime.now() + datetime.timedelta(hours=6)
|
||||
|
||||
whoami = subprocess.getoutput("whoami")
|
||||
with open(f'/home/{whoami}/ze_runner/config.json') as jsonfile:
|
||||
@@ -24,10 +26,6 @@ with open(f'/home/{whoami}/ze_runner/config.json') as jsonfile:
|
||||
looptestPath = f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg"
|
||||
chatmsg = ""
|
||||
|
||||
def overwrite_file_access():
|
||||
#setting defaults with setfacl for the download directory does not work seemingly
|
||||
subprocess.Popen(["chmod", "-R", "g+rwx", f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()
|
||||
|
||||
def writeCfgInput(Input_user):
|
||||
with open(looptestPath, 'w') as f:
|
||||
f.write(Input_user)
|
||||
@@ -39,6 +37,36 @@ def writeCfgInput(Input_user):
|
||||
time.sleep(0.35)
|
||||
open(looptestPath, 'w').close() #clearing file.
|
||||
|
||||
def get_display_env_from_file():
|
||||
display_path = f"/home/{whoami}/ze_runner/display.txt"
|
||||
env = {}
|
||||
try:
|
||||
with open(display_path) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if "=" in line:
|
||||
key, _, value = line.partition("=")
|
||||
env[key] = value
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
return env
|
||||
|
||||
def check_instance_conflict_dialog():
|
||||
display_env = get_display_env_from_file() or {}
|
||||
env = os.environ.copy()
|
||||
env["DISPLAY"] = display_env.get("DISPLAY")
|
||||
result = subprocess.run(
|
||||
["xdotool", "search", "--name", "Source - Warning"],
|
||||
env=env, capture_output=True, text=True,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
return 0
|
||||
return None
|
||||
|
||||
def overwrite_file_access():
|
||||
#setting defaults with setfacl for the download directory does not work seemingly
|
||||
subprocess.Popen(["chmod", "-R", "g+rwx", f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()
|
||||
|
||||
def clean_up_files():
|
||||
#deleting POSIX shared memory objects, as long as one process has them open they exist. THis is to prevent /dev/shm from being full
|
||||
#due to steam child processes.
|
||||
@@ -78,7 +106,6 @@ def delete_bsp_files():
|
||||
|
||||
def exit_handler():
|
||||
print('reached exithandler')
|
||||
#securing the looptest.cfg wont be stuck accidently with commands
|
||||
kill_owned_process("pidof cstrike_linux64")
|
||||
kill_owned_process("pidof xterm")
|
||||
|
||||
@@ -126,12 +153,19 @@ def restart_sdl_and_steam():
|
||||
|
||||
def bot_connect(data):
|
||||
#use whatever ip you want here to connect with
|
||||
str1 = ""
|
||||
server = ""
|
||||
if "connect to ze" == data:
|
||||
str1 = f"connect {data_ports['server_ip_port_ze']};"
|
||||
server = f"{data_ports['server_ip_port_ze']};"
|
||||
elif "connect to ze2" == data:
|
||||
str1 = f"connect {data_ports['server_ip_port_ze2']};" #wait 15000;
|
||||
writeCfgInput(str1)
|
||||
server = f"{data_ports['server_ip_port_ze2']};"
|
||||
|
||||
display_env = get_display_env_from_file() or {}
|
||||
env = os.environ.copy()
|
||||
env["DISPLAY"] = display_env.get("DISPLAY")
|
||||
subprocess.run(
|
||||
["/usr/games/steam", f"steam://connect/{server}"],
|
||||
env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
def cpulimit_pid_of_game():
|
||||
# ' > /dev/null' redirects stdout to /dev/null
|
||||
@@ -179,9 +213,9 @@ if __name__ == '__main__':
|
||||
sock.bind(("", local_port))
|
||||
sock.settimeout(5.0)
|
||||
messager_name = ""
|
||||
|
||||
failed_connect_count = 0
|
||||
|
||||
fail_safe_counter = 0
|
||||
|
||||
launch_game()
|
||||
try:
|
||||
while True:
|
||||
@@ -212,24 +246,51 @@ if __name__ == '__main__':
|
||||
elif "autismo connected to ze" == data:
|
||||
print('Bot connected to ze!')
|
||||
is_bot_connected_to_ze2 = False
|
||||
failed_connect_count = 0
|
||||
fail_safe_counter = 0
|
||||
elif "not connected to ze2" == data:
|
||||
is_bot_connected_to_ze2 = False
|
||||
elif "autismo connected to ze2" == data:
|
||||
print('Bot connected to ze2!')
|
||||
is_bot_connected_to_ze2 = True
|
||||
failed_connect_count = 0
|
||||
fail_safe_counter = 0
|
||||
elif data.startswith("download map:"):
|
||||
map_to_download = data.split("download map:")[1].strip()
|
||||
map_to_download_url = "https://uk-fastdl.unloze.com/css_ze/maps/" + map_to_download + ".bsp.bz2"
|
||||
map_folder = f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps/"
|
||||
bz2_path = map_folder + map_to_download + ".bsp.bz2"
|
||||
bsp_path = map_folder + map_to_download + ".bsp"
|
||||
|
||||
if os.path.exists(bsp_path):
|
||||
continue
|
||||
elif os.path.exists(bz2_path):
|
||||
continue
|
||||
else:
|
||||
with requests.get(map_to_download_url, stream=True, timeout=30) as r:
|
||||
r.raise_for_status()
|
||||
with open(bz2_path, "wb") as f:
|
||||
for chunk in r.iter_content(chunk_size=8192):
|
||||
f.write(chunk)
|
||||
with open(bz2_path, "rb") as f:
|
||||
compressed_data = f.read()
|
||||
decompressed_data = bz2.decompress(compressed_data)
|
||||
with open(bsp_path, "wb") as f:
|
||||
f.write(decompressed_data)
|
||||
os.remove(bz2_path)
|
||||
print(f"downloaded and extracted map: {bsp_path}")
|
||||
overwrite_file_access()
|
||||
elif "connect to ze" == data or ("connect to ze2" == data and not is_bot_connected_to_ze2):
|
||||
if datetime.datetime.now() >= restart_time or return_user_owned_pid("pidof cstrike_linux64") is None:
|
||||
if datetime.datetime.now() >= restart_time or return_user_owned_pid("pidof cstrike_linux64") is None or check_instance_conflict_dialog() is not None:
|
||||
kill_owned_process("pidof cstrike_linux64")
|
||||
sock.close()
|
||||
print('exiting after running for several hours.')
|
||||
sys.exit(1)
|
||||
print('data: ', data)
|
||||
overwrite_file_access()
|
||||
check_if_bz2_downloading()
|
||||
bot_connect(data)
|
||||
failed_connect_count += 1
|
||||
if failed_connect_count >= 2:
|
||||
|
||||
fail_safe_counter += 1
|
||||
if fail_safe_counter >= 3:
|
||||
kill_owned_process("pidof cstrike_linux64")
|
||||
sock.close()
|
||||
print('exiting after running for several hours.')
|
||||
|
||||
@@ -1,237 +0,0 @@
|
||||
#!/home/autismbot1/ze_runner/venv/bin/python3
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import atexit
|
||||
import pwd
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
import random
|
||||
import signal
|
||||
import socket
|
||||
import codecs
|
||||
import json
|
||||
import datetime
|
||||
import time
|
||||
import glob
|
||||
|
||||
restart_time = datetime.datetime.now() + datetime.timedelta(hours=3)
|
||||
the_undesired_crash_counter = 0 #hate this monkey solution
|
||||
|
||||
whoami = subprocess.getoutput("whoami")
|
||||
with open(f'/home/{whoami}/ze_runner/config.json') as jsonfile:
|
||||
data_ports = json.load(jsonfile)
|
||||
looptestPath = f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/cfg/looptest.cfg"
|
||||
chatmsg = ""
|
||||
|
||||
def overwrite_file_access():
|
||||
#setting defaults with setfacl for the download directory does not work seemingly
|
||||
subprocess.Popen(["chmod", "-R", "g+rwx", f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).communicate()
|
||||
|
||||
def writeCfgInput(Input_user):
|
||||
with open(looptestPath, 'w') as f:
|
||||
f.write(Input_user)
|
||||
#print("wrote to file: ", Input_user)
|
||||
if "connect" in Input_user or "retry;" in Input_user:
|
||||
#give extra time for disconnect, retry and connect commands
|
||||
time.sleep(1.0)
|
||||
else:
|
||||
time.sleep(0.35)
|
||||
open(looptestPath, 'w').close() #clearing file.
|
||||
|
||||
def clean_up_files():
|
||||
#deleting POSIX shared memory objects, as long as one process has them open they exist. THis is to prevent /dev/shm from being full
|
||||
#due to steam child processes.
|
||||
#even with steam turned offline might there be chromium web browsers left over from steam who still hold processes open.
|
||||
#only kind of potential issues from this is steam cloud being out of sync, which is really fucking irrelevant.
|
||||
for pattern in ["/tmp/steam*", "/tmp/dbus*", "/tmp/pressure*", "/tmp/tigervnc*", "/tmp/dumps*", "/dev/shm/u100*"]:
|
||||
for path in glob.glob(pattern):
|
||||
subprocess.run(["rm", "-rf", path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
|
||||
#clean up the game cache as it otherwise just keeps growing in gigabyte size
|
||||
subprocess.run(["rm", "-rf", f"/home/{whoami}/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/cache"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
|
||||
def check_if_bz2_downloading():
|
||||
#users should only have permissions to delete their own files probably
|
||||
#check if any bz2 files left over.
|
||||
for f in glob.glob(f"/home/{whoami}/.steam/steam/steamapps/common/Counter-Strike Source/cstrike/download/**/*.bz2", recursive=True):
|
||||
file_size = None
|
||||
while True:
|
||||
time.sleep(10)
|
||||
if not os.path.exists(f):
|
||||
break # finished downloading, bz2 file is removed.
|
||||
cur_file_size = os.path.getsize(f)
|
||||
if file_size == cur_file_size:
|
||||
# delete the bz2 file if its not progressing downloading
|
||||
subprocess.run(["rm", "-rf", f], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
break
|
||||
file_size = cur_file_size
|
||||
|
||||
def delete_bsp_files():
|
||||
#quite a more radical approach to just always delete all maps, well the download should be quick and the remaining bots have the bsp in memory...
|
||||
#the bots would have struggle connecting sometimes due to bsp files that were already downloaded before hand (maybe corrupted?)
|
||||
for f in glob.glob(f"/home/{whoami}/.steam/debian-installation/steamapps/common/Counter-Strike Source/cstrike/download/maps/*"):
|
||||
#ai slop IF statement for checking if file modified less than 10 minutes ago.
|
||||
if time.time() - os.path.getmtime(f) < 600:
|
||||
continue
|
||||
subprocess.run(["rm", "-rf", f], stdout=subprocess.PIPE)
|
||||
|
||||
def exit_handler():
|
||||
print('reached exithandler')
|
||||
#securing the looptest.cfg wont be stuck accidently with commands
|
||||
kill_owned_process("pidof cstrike_linux64")
|
||||
kill_owned_process("pidof xterm")
|
||||
|
||||
clean_up_files()
|
||||
check_if_bz2_downloading()
|
||||
delete_bsp_files()
|
||||
|
||||
subprocess.getoutput(f"pkill -9 -u {whoami}")
|
||||
|
||||
def kill_user_owned_pid(pid):
|
||||
#print('pid: ', pid, ' killed')
|
||||
pid = int(pid.strip())
|
||||
os.kill(pid, signal.SIGKILL)
|
||||
time.sleep(10)
|
||||
|
||||
def return_user_owned_pid(pidof):
|
||||
owner_pid = subprocess.getoutput([f"{pidof}"])
|
||||
if owner_pid:
|
||||
for pid in owner_pid.split(" "):
|
||||
username = subprocess.getoutput([f"""ps -o user= -p {pid}"""])
|
||||
if username == whoami:
|
||||
return pid
|
||||
return None
|
||||
|
||||
def kill_owned_process(pidof):
|
||||
pid = return_user_owned_pid(pidof)
|
||||
while pid:
|
||||
kill_user_owned_pid(pid)
|
||||
pid = return_user_owned_pid(pidof)
|
||||
|
||||
def restart_sdl_and_steam():
|
||||
#subprocess.getoutput(["screen -XS XTERM quit"])
|
||||
kill_owned_process("pidof cstrike_linux64")
|
||||
kill_owned_process("pidof xterm")
|
||||
|
||||
subprocess.getoutput([f'vncserver -kill']) #only displays vncservers for the specific user.
|
||||
subprocess.getoutput([f'vncserver -list -cleanstale']) #cleans stale sessions by removing files from disk
|
||||
time.sleep(5)
|
||||
|
||||
cmd = f'vncserver -localhost no -geometry 1x1 -depth 24'
|
||||
#cmd = f'vncserver -localhost no -geometry 800x800 -depth 24'
|
||||
#print(f'cmd: {cmd}')
|
||||
subprocess.getoutput(cmd)
|
||||
print('reached .bashrc executing steam and variables')
|
||||
|
||||
def bot_connect(data):
|
||||
#use whatever ip you want here to connect with
|
||||
str1 = ""
|
||||
if "connect to ze" == data:
|
||||
str1 = f"connect {data_ports['server_ip_port_ze']};"
|
||||
elif "connect to ze2" == data:
|
||||
str1 = f"connect {data_ports['server_ip_port_ze2']};" #wait 15000;
|
||||
writeCfgInput(str1)
|
||||
|
||||
def cpulimit_pid_of_game():
|
||||
# ' > /dev/null' redirects stdout to /dev/null
|
||||
# '2>&1' redirects stderr to the same place as stdout
|
||||
pid = return_user_owned_pid("pidof cstrike_linux64")
|
||||
if pid is None:
|
||||
return
|
||||
cmd = f"cpulimit --pid={pid} --limit=25 --background > /dev/null 2>&1"
|
||||
#cmd = f"cpulimit --pid={pid} --limit=55 --background > /dev/null 2>&1"
|
||||
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
|
||||
|
||||
time.sleep(3)
|
||||
for source_engine_lock in glob.glob("/tmp/source_engine_*"):
|
||||
subprocess.run(["rm", "-rf", source_engine_lock], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
overwrite_file_access()
|
||||
|
||||
if __name__ == '__main__':
|
||||
atexit.register(exit_handler)
|
||||
local_port = data_ports['udp_port']
|
||||
external_port_messages = data_ports['chat_external_port']
|
||||
buffer_size = 4096 #potentially not large enough?
|
||||
|
||||
clean_up_files()
|
||||
check_if_bz2_downloading()
|
||||
delete_bsp_files()
|
||||
|
||||
restart_sdl_and_steam()
|
||||
is_bot_connected_to_ze2 = False
|
||||
|
||||
print('preparing to launch game....')
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
sock.bind(("", local_port))
|
||||
sock.settimeout(5.0)
|
||||
messager_name = ""
|
||||
|
||||
#give the game some time to launch
|
||||
fail_safe = 5
|
||||
while return_user_owned_pid("pidof cstrike_linux64") is None:
|
||||
#just a fail safe to prevent being stuck forever.
|
||||
if fail_safe <= 0:
|
||||
sys.exit(1)
|
||||
time.sleep(10)
|
||||
print("launching game...")
|
||||
fail_safe -= 1
|
||||
time.sleep(10)
|
||||
cpulimit_pid_of_game()
|
||||
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
#print("socket waiting for recvfrom")
|
||||
data, addr = sock.recvfrom(buffer_size)
|
||||
except socket.timeout:
|
||||
continue
|
||||
data = codecs.decode(data, "utf-8", "ignore")
|
||||
ip = addr[0]
|
||||
port = addr[1]
|
||||
#print('port: ', port, " ip: ", ip)
|
||||
#print('data: ', data)
|
||||
if not data:
|
||||
continue
|
||||
|
||||
#print("ip: ", ip, " port: ", port)
|
||||
if ip == data_ports['discord_bot_ip'] and port == external_port_messages:
|
||||
if messager_name in data:
|
||||
messager_name = ""
|
||||
response_msg = f"""say {messager_name} {data}"""
|
||||
print("remote UDP packet response_msg: ", response_msg)
|
||||
writeCfgInput(response_msg)
|
||||
if ip != data_ports['ovh_ip']:
|
||||
continue
|
||||
elif data == "bot kicked server full":
|
||||
print('bot kicked server full: ', datetime.datetime.now().time())
|
||||
elif "autismo connected to ze" == data:
|
||||
print('Bot connected to ze!')
|
||||
is_bot_connected_to_ze2 = False
|
||||
the_undesired_crash_counter = 0
|
||||
elif "not connected to ze2" == data:
|
||||
is_bot_connected_to_ze2 = False
|
||||
elif "autismo connected to ze2" == data:
|
||||
print('Bot connected to ze2!')
|
||||
the_undesired_crash_counter = 0
|
||||
is_bot_connected_to_ze2 = True
|
||||
elif "connect to ze" == data or ("connect to ze2" == data and not is_bot_connected_to_ze2):
|
||||
if datetime.datetime.now() >= restart_time or return_user_owned_pid("pidof cstrike_linux64") is None or the_undesired_crash_counter >= 5:
|
||||
#it already finished downloading the contnet, it just cant connect. probably stuck. shitty solution welp
|
||||
kill_owned_process("pidof cstrike_linux64")
|
||||
sock.close()
|
||||
print('exiting after running the game for several hours or game being stuck on map change.')
|
||||
sys.exit(1)
|
||||
print('data: ', data, ' the_undesired_crash_counter: ', the_undesired_crash_counter)
|
||||
the_undesired_crash_counter += 1
|
||||
check_if_bz2_downloading()
|
||||
overwrite_file_access()
|
||||
bot_connect(data)
|
||||
elif "clientmessage:" in data:
|
||||
messager_name = data.split("clientmessage:", 1)[1].split(f" {data_ports['magic_secret']}")[0]
|
||||
databyte_send_message = messager_name + data.split(f"{data_ports['magic_secret']}")[1]
|
||||
sock.sendto(databyte_send_message.encode(), (data_ports["discord_bot_ip"], external_port_messages))
|
||||
#print('databyte_send_message: ', databyte_send_message)
|
||||
finally:
|
||||
sock.close()
|
||||
Reference in New Issue
Block a user