custom tracks are now converted into szs in the cache

This commit is contained in:
Faraphel 2022-07-18 19:11:11 +02:00
parent 63cbe06bf3
commit 186aa55f4a
4 changed files with 49 additions and 3 deletions

View file

@ -36,7 +36,9 @@ class ExtractedGame:
destination_path.mkdir(parents=True, exist_ok=True) destination_path.mkdir(parents=True, exist_ok=True)
yield {"description": "Extracting original tracks...", "determinate": False} yield {"description": "Extracting original tracks...", "determinate": False}
for track_file in (self.path / "files/Race/Course/").glob("*.szs"): for track_file in (self.path / "files/Race/Course/").glob("*.szs"):
track_file.rename(destination_path / track_file.name) yield {"description": f"Extracting original tracks ({track_file.name})...", "determinate": False}
if not (destination_path / track_file.name).exists(): track_file.rename(destination_path / track_file.name)
else: track_file.unlink()
def install_mystuff(self) -> Generator[dict, None, None]: def install_mystuff(self) -> Generator[dict, None, None]:
""" """

View file

@ -98,6 +98,10 @@ class Game:
cache_directory: Path = Path("./.cache") cache_directory: Path = Path("./.cache")
cache_directory.mkdir(parents=True, exist_ok=True) cache_directory.mkdir(parents=True, exist_ok=True)
cache_autoadd_directory = cache_directory / "autoadd/"
cache_ogtracks_directory = cache_directory / "original-tracks/"
cache_cttracks_directory = cache_directory / f"custom-tracks/"
# get the directory where the game will be extracted # get the directory where the game will be extracted
extracted_game = ExtractedGame(self.get_output_directory(dest, mod_config), self) extracted_game = ExtractedGame(self.get_output_directory(dest, mod_config), self)
@ -108,8 +112,9 @@ class Game:
yield from self.extract(extracted_game.path) yield from self.extract(extracted_game.path)
# prepare the cache # prepare the cache
yield from extracted_game.extract_autoadd(cache_directory / "autoadd/") yield from extracted_game.extract_autoadd(cache_autoadd_directory)
yield from extracted_game.extract_original_tracks(cache_directory / "original-tracks/") yield from extracted_game.extract_original_tracks(cache_ogtracks_directory)
yield from mod_config.normalize_all_tracks(cache_autoadd_directory, cache_cttracks_directory)
# patch the game # patch the game
yield from extracted_game.install_mystuff() yield from extracted_game.install_mystuff()

View file

@ -8,6 +8,7 @@ from source.mkw.Cup import Cup
from source.mkw.Track import Track from source.mkw.Track import Track
import json import json
from source.wt.szs import SZSPath
CT_ICON_SIZE: int = 128 CT_ICON_SIZE: int = 128
@ -242,3 +243,20 @@ class ModConfig:
for i, cticon in enumerate(cticons): full_cticon.paste(cticon, (0, i * CT_ICON_SIZE)) for i, cticon in enumerate(cticons): full_cticon.paste(cticon, (0, i * CT_ICON_SIZE))
return full_cticon return full_cticon
def normalize_all_tracks(self, autoadd_path: "Path | str", destination_path: "Path | str") -> Generator[dict, None, None]:
"""
Convert all tracks of the mod to szs into the destination_path
:param autoadd_path: autoadd directory
:param destination_path: destination where the files are converted
"""
yield {"description": "Normalizing track..."}
destination_path = Path(destination_path)
destination_path.mkdir(parents=True, exist_ok=True)
for track_file in filter(lambda file: file.is_file(), (self.path.parent / "_TRACKS").rglob("*")):
yield {"description": f"Normalizing track \"{track_file.name}\"..."}
SZSPath(track_file).normalize(
autoadd_path,
destination_path / track_file.with_suffix(".szs").name,
format="szs"
)

View file

@ -53,6 +53,27 @@ class SZSPath:
def __eq__(self, other: "SZSPath") -> bool: def __eq__(self, other: "SZSPath") -> bool:
return self.path == other.path return self.path == other.path
def normalize(self, autoadd_path: "Path | str", destination_path: "Path | str", format: str = "szs") -> "SZSPath":
"""
Convert the file into a another format
:param format: format to convert the file
:param autoadd_path: Autoadd directory
:param destination_path: destination of the converted file
:return: new path of the file
"""
if not destination_path.exists() or \
(destination_path.exists() and destination_path.stat().st_mtime < self.path.stat().st_mtime):
# if the destination_path exists and is less recent than this source file, update it.
_tools_run(
"NORMALIZE",
self.path,
"--autoadd-path", autoadd_path,
"--DEST", destination_path,
f"--{format}",
"--overwrite"
)
return SZSPath(destination_path)
def cat(self, subfile: str) -> bytes: def cat(self, subfile: str) -> bytes:
""" """
Run the cat command (read a subfile) and return the output Run the cat command (read a subfile) and return the output