-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdr2mds.py
executable file
·82 lines (63 loc) · 2.32 KB
/
dr2mds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python3
from math import log
import errno
import argparse
import sys
import os
sys.tracebacklimit = 0
class SaveFileTooSmallError(Exception):
def __init__(self, size, message="Save file is smaller than 128KiB"):
self.size = size
self.message = message
super().__init__(self.message)
def __str__(self):
return f"{self.size} is smaller than 131072 or 128KiB"
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
if in_bytes < 131072:
print(f"The file {infile} seems to be smaller than 128KiB"
" and not a valid save file", file=sys.stderr)
raise SaveFileTooSmallError(in_bytes)
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__":
main()