From eff19f55516d41fe5bb49f5b55c9bedfdc20202d Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 26 May 2020 01:04:57 +0200 Subject: [PATCH] scripts: add dir Add 2 scripts that I just used, could be useful. --- scripts/kf4-group-sounds.py | 48 +++++++++++++++++++++++++++++++++++++ scripts/patch.py | 28 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 scripts/kf4-group-sounds.py create mode 100755 scripts/patch.py diff --git a/scripts/kf4-group-sounds.py b/scripts/kf4-group-sounds.py new file mode 100755 index 0000000..3d7d747 --- /dev/null +++ b/scripts/kf4-group-sounds.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +"""Group KF4 sound files together in named dirs for later use w/ mkpsf2. + +Some tools to handle sounds have issues with PS2 sounds split as HD/BD/SQ, so +grouping them with this script makes it easier to batch process them with mkspf2 +so you can process them later as PSF2. + +Example: + +```bash +$ ./kf4-group-sounds.py workspace/dat/snd/ workspace/soundgroups +$ cd workspace/soundgroups +$ ls -lF +bgm000/ +bgm001/ +... +$ fd -t d -x wine mkpsf2 {}.psf2 {} +``` +""" + +import argparse +import glob +import os +import shutil +from pathlib import Path + + +def main(): + argparser = argparse.ArgumentParser() + argparser.add_argument("snd_dir") + argparser.add_argument("output_dir") + args = argparser.parse_args() + copy_files(Path(args.snd_dir), Path(args.output_dir)) + + +def copy_files(snd_path, output_path): + filenames = set([os.path.splitext(f)[0] for f in os.listdir(snd_path)]) + for filename in filenames: + subfiles = glob.glob(str(snd_path / "{}.*".format(filename))) + files_output_path = output_path / filename + if not files_output_path.exists(): + files_output_path.mkdir(parents=True) + for f in subfiles: + shutil.copyfile(f, files_output_path / os.path.basename(f)) + + +if __name__ == "__main__": + main() diff --git a/scripts/patch.py b/scripts/patch.py new file mode 100755 index 0000000..0731ee8 --- /dev/null +++ b/scripts/patch.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +"""Patch a file with another file at offset. Useful for dirty DAT patching.""" + +import argparse + + +def main(): + argparser = argparse.ArgumentParser() + argparser.add_argument("archive", help="File to be modified") + argparser.add_argument("file", help="File to patch in archive") + argparser.add_argument("offset", help="Offset as hex") + args = argparser.parse_args() + + archive_path, input_file_path = args.archive, args.file + ofs = int(args.offset.lstrip("0x").rstrip("h"), 16) + patch(archive_path, input_file_path, ofs) + + +def patch(archive_path, input_file_path, ofs): + with open(input_file_path, "rb") as input_file: + input_data = input_file.read() + with open(archive_path, "rb+") as archive_file: + archive_file.seek(ofs) + archive_file.write(input_data) + + +if __name__ == "__main__": + main()