Added argparse support
This commit is contained in:
33
README.md
33
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
|
||||
|
||||
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).
|
||||
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.
|
||||
Or using dd like this
|
||||
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.
|
||||
|
||||
58
dr2mds.py
58
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:
|
||||
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)
|
||||
|
||||
ouf.write(inf.read(closest2pow(st_size(infile))))
|
||||
|
||||
if __name__ == "__main__":
|
||||
drastic2melonds(DRASTIC_SAVE,MELONDS_SAVE)
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user