Added argparse support

This commit is contained in:
Uttarayan Mondal
2021-01-29 19:20:31 +05:30
parent 815467a8a0
commit 53eea990f0
2 changed files with 82 additions and 17 deletions

View File

@@ -2,19 +2,38 @@
This script will convert the dsv save from drastic emulator to a melonDS save file This script will convert the dsv save from drastic emulator to a melonDS save file
How this works ? ### How to use
You need python 3 installed.
Specify the input save file
```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
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). 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 In _Importing a savefile from another emulator_ part under _How to_ section
This can be easily done manually by removing the padding using a hex editor. This script calculates the closest power of 2 to the save file and removes the padding.
Or using dd like this
### 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 ```bash
dd if=Pokemon.dsv of=Pokemon.sav bs=1 count=524288 dd if=Pokemon.dsv of=Pokemon.sav bs=1 count=524288
``` ```
This will make a 4KiB EEPROM save from drastic to melonDS save. This will make a 4KiB EEPROM save from drastic to melonDS save.
I will add argparse later.

View File

@@ -1,21 +1,67 @@
#!/usr/bin/python3 #!/usr/bin/python3
from math import log from math import log
import errno
import argparse
import sys
import os import os
DRASTIC_SAVE="Pokemon Black.dsv" sys.tracebacklimit = 0
MELONDS_SAVE="Pokemon Black.sav"
def st_size(path):
return os.stat(path).st_size
def closest2pow(n): 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)) 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__": if __name__ == "__main__":
drastic2melonds(DRASTIC_SAVE,MELONDS_SAVE) main()