scripts: add dir

Add 2 scripts that I just used, could be useful.
This commit is contained in:
dece 2020-05-26 01:04:57 +02:00
parent a7102917f4
commit eff19f5551
2 changed files with 76 additions and 0 deletions

48
scripts/kf4-group-sounds.py Executable file
View file

@ -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()

28
scripts/patch.py Executable file
View file

@ -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()