-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsalt_the_spire.py
executable file
·82 lines (59 loc) · 1.85 KB
/
salt_the_spire.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/env python3
"""
Author: jessijzhao
Date: November 29, 2020
Decodes or encodes save files for Slay the Spire.
"""
import argparse
import base64
from itertools import cycle
def xor_key(bstring: bytes, key: bytes = b"key") -> bytes:
"""
Args:
bstring: bytestring to xor with cyclic key
key: key phrase (actually just "key")
Returns the XOR of the input text and the key phrase ("key").
"""
return bytes(_a ^ _b for _a, _b in zip(bstring, cycle(key)))
def decode(inbytes: bytes) -> bytes:
"""
Args:
inbytes: bytes to decode
Returns decoded bytes representing a human-readable JSON.
"""
return xor_key(base64.b64decode(inbytes))
def encode(inbytes: bytes) -> bytes:
"""
Args:
inbytes: bytes to encode
Returns encoded bytes in Slay the Spire save file format.
"""
return base64.b64encode(xor_key(inbytes))
def main() -> None:
parser = argparse.ArgumentParser()
# whether to decode or encode save file
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-d", "--decode", action="store_true", help="decode save file")
group.add_argument("-e", "--encode", action="store_true", help="encode save file")
# which files to read from / write to
parser.add_argument(
"input", type=argparse.FileType("rb"), help="save file to decode/encode"
)
parser.add_argument(
"output",
type=argparse.FileType("wb"),
help="file to store decoded/encoded save file",
)
args = parser.parse_args()
inbytes = args.input.read()
args.input.close()
if args.decode:
outbytes = decode(inbytes)
elif args.encode:
outbytes = encode(inbytes)
else:
raise RuntimeError
args.output.write(outbytes)
args.output.close()
if __name__ == "__main__":
main()