2019-12-08 22:43:49 +01:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
|
|
|
|
from bemani.format import TwoDX
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
|
|
|
parser = argparse.ArgumentParser(description="A utility to extract/build 2dx files.")
|
|
|
|
parser.add_argument(
|
|
|
|
"file",
|
|
|
|
help="2dx file to extract/build.",
|
|
|
|
type=str,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--directory",
|
2021-08-12 17:57:54 +02:00
|
|
|
help="Directory to extract to. Specify this parameter if you want to extract an existing 2dx file.",
|
2019-12-08 22:43:49 +01:00
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-w",
|
|
|
|
"--wavfile",
|
2021-08-12 17:57:54 +02:00
|
|
|
help=(
|
|
|
|
"ADPCM wave file to add to a new or existing archive. Specify this parameter to update an "
|
|
|
|
"existing 2dx file with a new wav file or build a new archive containing a particular wav file. "
|
|
|
|
"Note that you can specify this parameter multiple times to bundle multiple wav files into one "
|
|
|
|
"archive."
|
|
|
|
),
|
2019-12-08 22:43:49 +01:00
|
|
|
action="append",
|
|
|
|
default=[],
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--name",
|
2021-08-12 17:57:54 +02:00
|
|
|
help="Name of the archive when creating a new 2dx file from scratch.",
|
2019-12-08 22:43:49 +01:00
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.directory is not None:
|
|
|
|
root = args.directory
|
|
|
|
if root[-1] != '/':
|
|
|
|
root = root + '/'
|
|
|
|
root = os.path.realpath(root)
|
|
|
|
|
|
|
|
fp = open(args.file, 'rb')
|
|
|
|
data = fp.read()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
twodx = TwoDX(data)
|
|
|
|
|
|
|
|
for fn in twodx.filenames:
|
2020-01-07 22:29:07 +01:00
|
|
|
print(f'Extracting {fn} to disk...')
|
2019-12-08 22:43:49 +01:00
|
|
|
realfn = os.path.join(root, fn)
|
|
|
|
dirof = os.path.dirname(realfn)
|
|
|
|
os.makedirs(dirof, exist_ok=True)
|
|
|
|
with open(realfn, 'wb') as fp:
|
|
|
|
fp.write(twodx.read_file(fn))
|
2021-08-12 17:57:54 +02:00
|
|
|
elif len(args.wavfile) > 0:
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
|
|
|
fp = open(args.file, 'rb')
|
|
|
|
data = fp.read()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
twodx = TwoDX(data)
|
|
|
|
except FileNotFoundError:
|
|
|
|
twodx = TwoDX()
|
|
|
|
if args.name is not None:
|
|
|
|
twodx.set_name(args.name)
|
|
|
|
else:
|
|
|
|
twodx.set_name(os.path.splitext(os.path.basename(args.file))[0])
|
|
|
|
|
|
|
|
for fn in args.wavfile:
|
|
|
|
fp = open(fn, 'rb')
|
|
|
|
data = fp.read()
|
|
|
|
fp.close()
|
|
|
|
|
|
|
|
twodx.write_file(os.path.basename(fn), data)
|
|
|
|
|
|
|
|
fp = open(args.file, 'wb')
|
|
|
|
fp.write(twodx.get_new_data())
|
|
|
|
fp.close()
|
2021-08-12 17:57:54 +02:00
|
|
|
else:
|
|
|
|
raise Exception("Please provide either a directory to extract to, or a wav file to build into a 2dx file!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|