Files
ac-server-scripts/change_track.py
2025-11-16 15:56:57 +01:00

72 lines
2.5 KiB
Python

import os
# CONTENT_FOLDER = "/home/sem/assetto-corsa/content"
CONFIG_FILE = "server_cfg.ini"
# 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 = ""):
img_path = os.path.join(TRACKS_FOLDER, track, "ui", config, "preview.png")
if os.path.exists(img_path):
return img_path
return ""
def get_outline_image(track: str, config: str = ""):
img_path = os.path.join(TRACKS_FOLDER, track, "ui", config, "outline.png")
if os.path.exists(img_path):
return img_path
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! =====")
config = ""
return configdirs
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}"