Files
ac-server-scripts/ac_tracks.py

91 lines
3.3 KiB
Python

import os
CONFIG_FILE = "server_cfg.ini"
CONTENT_FOLDER = "/home/sem/assetto-corsa/content"
CONFIG_PATH = "/home/sem/assetto-corsa/cfg"
# CONTENT_FOLDER = "./content"
# CONFIG_PATH = "./cfg"
TRACKS_FOLDER = os.path.join(CONTENT_FOLDER, "tracks")
def get_all_tracks():
return sorted([x.name for x in os.scandir(TRACKS_FOLDER) if x.is_dir()])
def get_preview_image(track: str, config: str = ""):
return get_path_image("preview", track, config)
def get_outline_image(track: str, config: str = ""):
return get_path_image("outline", track, config)
def get_path_image(image_name, track: str, config: str = ""):
img_path = os.path.join(TRACKS_FOLDER, track, "ui", config)
for name in os.listdir(img_path):
lower = name.lower()
if image_name in lower and lower.endswith(".png") or lower.endswith(".jpg"):
return os.path.join(img_path, name)
img_path = os.path.join(TRACKS_FOLDER, track, config)
for name in os.listdir(img_path):
lower = name.lower()
if image_name in lower and lower.endswith(".png") or lower.endswith(".jpg"):
return os.path.join(img_path, name)
return ""
def get_configs(track) -> list[str]:
track_path = os.path.join(TRACKS_FOLDER, track);
configdirs = [x.path.replace(track_path + "/","") for x in os.scandir(track_path) if x.is_dir()]
configdirs.sort()
standard_folders = {'ai', 'ui', 'extension', 'data', 'skins','texture', 'sfx'}
for s in standard_folders:
if s in configdirs:
configdirs.remove(s)
if (len(configdirs) == 0):
print("===== The map you entered does not have a config! =====")
return configdirs
def get_current_track() -> tuple[str, str]:
track_name = ""
config_name = ""
config_file = os.path.join(CONFIG_PATH, CONFIG_FILE)
with open(config_file,'r') as file:
for line in file:
line = line.strip()
if line.startswith("TRACK="):
track_name = line.replace("TRACK=","").strip()
elif line.startswith("CONFIG_TRACK="):
config_name = line.replace("CONFIG_TRACK=","").strip()
return track_name, config_name
def change_track(newname, config=""):
print(f"handling track change for {newname} with config {config}")
if newname not in get_all_tracks():
return False, f"Track '{newname}' does not exist!"
new_content = ""
config_file = os.path.join(CONFIG_PATH, CONFIG_FILE)
with open(config_file,'r') as file:
for line in file:
line = line.strip()
if line.startswith("TRACK="):
new_content += "TRACK=" + str(newname)
print("changing line " + line + " to " + "TRACK=" + str(newname))
elif line.startswith("CONFIG_TRACK="):
if (len(config) > 1):
new_content +="CONFIG_TRACK=" + str(config)
print("changing line " + line + " to " + "CONFIG_TRACK=" + str(config))
else:
new_content += "CONFIG_TRACK="
print("no config entered, setting config line to CONFIG_TRACK=")
else:
new_content += line
new_content += "\n"
with open(config_file,'w') as newfile:
newfile.write(new_content)
return True, f"Changed track to: {newname}"