Atlas-Install/source/patch_track.py

123 lines
No EOL
5.6 KiB
Python

from .definition import *
from tkinter import messagebox
import subprocess
import shutil
import os
def patch_autoadd(self):
if os.path.exists("./file/auto-add"): shutil.rmtree("./file/auto-add")
if not os.path.exists(self.game.path + "/tmp/"): os.makedirs(self.game.path + "/tmp/")
subprocess.run(["./tools/szs/wszst", "AUTOADD", get_nodir(self.game.path) + "/files/Race/Course/",
"--DEST", get_nodir(self.game.path) + "/tmp/auto-add/"],
creationflags=CREATE_NO_WINDOW, cwd=get_dir(self.game.path),
check=True, stdout=subprocess.PIPE)
shutil.move(self.game.path + "/tmp/auto-add/", "./file/auto-add/")
shutil.rmtree(self.game.path + "/tmp/")
def patch_track(self):
max_process = self.intvar_process_track.get()
process_list = {}
error_count, error_max = 0, 3
def add_process(track):
nonlocal error_count, error_max, process_list
track_file = track.get_track_name()
total_track = len(self.ctconfig.all_tracks)
process_list[track_file] = None # Used for showing track in progress even if there's no process
self.Progress(statut=self.translate("Converting tracks", f"\n({i + 1}/{total_track})\n",
"\n".join(process_list.keys())), add=1)
for _track in [track.file_szs, track.file_wu8]:
if os.path.exists(_track):
if os.path.getsize(_track) < 1000: # File under this size are corrupted
os.remove(_track)
while True:
download_returncode = self.get_github_file(track.file_wu8)
if download_returncode == -1: # can't download
error_count += 1
if error_count > error_max: # Too much track wasn't correctly converted
messagebox.showerror(
self.translate("Error"),
self.translate("Too much tracks had a download issue."))
return -1
else:
messagebox.showwarning(self.translate("Warning"),
self.translate("Can't download this track !",
f" ({error_count} / {error_max})"))
elif download_returncode == 2: break # if download is disabled, don't check sha1
if track.sha1:
if not self.boolvar_dont_check_track_sha1.get():
if not track.check_sha1(): # Check si le sha1 du fichier est le bon
error_count += 1
if error_count > error_max: # Too much track wasn't correctly converted
messagebox.showerror(
self.translate("Error"),
self.translate("Too much tracks had an issue during sha1 check."))
return -1
continue
break
if not (os.path.exists(track.file_szs)) or download_returncode == 3: # returncode 3 is track has been updated
if os.path.exists(track.file_wu8):
process_list[track_file] = track.convert_wu8_to_szs()
else:
messagebox.showerror(self.translate("Error"),
self.translate("Can't convert track.\nEnable track download and retry."))
return -1
elif self.boolvar_del_track_after_conv.get(): os.remove(track.file_wu8)
return 0
def clean_process():
nonlocal error_count, error_max, process_list
for track_file, process in process_list.copy().items():
if process is not None:
if process.poll() is None:
pass # if the process is still running
else: # process ended
process_list.pop(track_file)
stderr = process.stderr.read()
if b"wszst: ERROR" in stderr: # Error occured
os.remove(track.file_szs)
error_count += 1
if error_count > error_max: # Too much track wasn't correctly converted
messagebox.showerror(
self.translate("Error"),
self.translate("Too much track had a conversion issue."))
return -1
else: # if the error max hasn't been reach
messagebox.showwarning(
self.translate("Warning"),
self.translate("The track", " ", get_track_wu8(track_file),
"do not have been properly converted.",
f" ({error_count} / {error_max})"))
else:
if self.boolvar_del_track_after_conv.get(): os.remove(track.file_wu8)
else:
process_list.pop(track_file)
if not(any(process_list.values())): return 1 # si il n'y a plus de processus
if len(process_list): return 1
else: return 0
for i, track in enumerate(self.ctconfig.all_tracks):
while True:
if len(process_list) < max_process:
returncode = add_process(track)
if returncode == 0: break
elif returncode == -1: return -1 # if error occur, stop function
elif clean_process() == -1: return -1
while True:
returncode = clean_process()
if returncode == 1: break # End the process if all process ended
elif returncode == 0: pass
else: return -1
return 0