From 53eea990f0dfa8fa83968586d2f14e09f4d0fdbb Mon Sep 17 00:00:00 2001 From: Uttarayan Mondal Date: Fri, 29 Jan 2021 19:20:31 +0530 Subject: [PATCH] Added argparse support --- README.md | 35 +++++++++++++++++++++++------- dr2mds.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 82 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 379a76b..1886aa7 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,38 @@ This script will convert the dsv save from drastic emulator to a melonDS save file -How this works ? +### How to use -MelonDS determines save type using exact byte count in the save file -Which needs to be a power of 2 as mentioned [here](http://melonds.kuribo64.net/faq.php). -This script calculates the closest power of 2 to the save file and removes the padding +You need python 3 installed. +Specify the input save file -This can be easily done manually by removing the padding using a hex editor. -Or using dd like this +```bash +./dr2mds.py "Pokemon Black.dsv" +``` + +This will make a "Pokemon Black.sav" file as output +You can also specify the output like + +```bash +./dr2mds.py "Pokemon Black.dsv" -o "PKMN.sav" +``` + +### How this works ? + +MelonDS determines the type of save using exact byte count in the save file + +Which needs to be a power of 2 as mentioned [here](http://melonds.kuribo64.net/faq.php). +In _Importing a savefile from another emulator_ part under _How to_ section + +This script calculates the closest power of 2 to the save file and removes the padding. + +### Other ways of doing this + +This can be done manually by removing the padding using a hex editor. +Or using dd like this (on linux/macos) ```bash dd if=Pokemon.dsv of=Pokemon.sav bs=1 count=524288 ``` This will make a 4KiB EEPROM save from drastic to melonDS save. - -I will add argparse later. diff --git a/dr2mds.py b/dr2mds.py index 18fc91a..9e2ca06 100755 --- a/dr2mds.py +++ b/dr2mds.py @@ -1,21 +1,67 @@ #!/usr/bin/python3 from math import log +import errno +import argparse +import sys import os -DRASTIC_SAVE="Pokemon Black.dsv" -MELONDS_SAVE="Pokemon Black.sav" +sys.tracebacklimit = 0 -def st_size(path): - return os.stat(path).st_size def closest2pow(n): + # int always returns the floor of the float + # log(n)/log(2) gives log base 2 of n return 2**int(log(n) / log(2)) -def drastic2melonds(infile,outfile): - with open(infile,"rb") as inf, open(outfile,"wb") as ouf: - - ouf.write(inf.read(closest2pow(st_size(infile)))) + +def drastic2melonds(infile, outfile): + with open(infile, "rb") as inf, open(outfile, "wb") as ouf: + in_bytes = os.stat(infile).st_size + out_bytes = closest2pow(in_bytes) + print(f"Reading {in_bytes} bytes from {infile}", file=sys.stderr) + print(f"Writing {out_bytes} bytes to {outfile}", file=sys.stderr) + ouf.write(inf.read(out_bytes)) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument( + "-o", + "--output", + help="Specify the output file") + parser.add_argument( + "-f", + "--force", + help="Overwrite the output file if it exists", + action="store_true") # This stores false by default unless -f is given + + parser.add_argument( + "file", help="The drastic save file") + if len(sys.argv) == 1: + parser.print_help(sys.stderr) + sys.exit(0) + + args = parser.parse_args() + + if not os.path.isfile(args.file): + parser.print_usage() + raise FileNotFoundError( + errno.ENOENT, os.strerror(errno.ENOENT), args.file) + + if not args.output: + args.output = os.path.splitext(args.file)[0] + ".sav" + + if os.path.isfile(args.output): + if args.force: + print(f"Overwriting file {args.output}", file=sys.stderr) + drastic2melonds(args.file, args.output) + else: + print(f"Output file {args.output} already exists " + "pass -f/--force flag to overwrite the file", file=sys.stderr) + else: + drastic2melonds(args.file, args.output) + if __name__ == "__main__": - drastic2melonds(DRASTIC_SAVE,MELONDS_SAVE) + main()