This repository has been archived by the owner on Nov 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathD3Edit.py
145 lines (121 loc) · 5.37 KB
/
D3Edit.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
from json import load
from sys import getsizeof
from os.path import isfile, join
from argparse import ArgumentParser
from binascii import hexlify as _hexlify
# pip3 install protobuf
import Account_pb2
BYTE_MAX_VALUE = 255
XOR_KEY = 0x305F92D82EC9A01B
SETTINGS_FILE = "settings.json"
def hexlify(b: (bytes, bytearray)) -> (bytes, bytearray):
"""
bytes -> hex
:param b: the bytes you want to convert to hex
:return: the hex string representation of the specified bytes
"""
return _hexlify(b).decode("utf8")
def truncate(num: int, boundary: int, signed: bool, endian: str = "little") -> int:
"""
Truncate an int to a given byte boundary
:param num: the number to truncate
:param margin: the byte boundary
:param signed: whether or not the int is signed
:param endian: the integer's endianness
:return: the truncated integer
"""
return int.from_bytes(num.to_bytes(getsizeof(num), endian, signed=signed)[:boundary], endian, signed=signed)
def decrypt_save(data: (bytes, bytearray)) -> (bytes, bytearray):
"""
Decrypt a save file
:param data: the save data to decrypt
:return: the decrypted save data
"""
global XOR_KEY
if isinstance(data, bytes):
data = bytearray(data)
num = XOR_KEY
for i in range(len(data)):
data[i] ^= (num & BYTE_MAX_VALUE)
num = truncate((((num ^ data[i]) << 56) | num >> 8), 8, False)
return bytes(data)
def encrypt_save(data: (bytes, bytearray)) -> (bytes, bytearray):
"""
Encrypt a save file
:param data: the save data to encrypt
:return: the encrypted save data
"""
global XOR_KEY
if isinstance(data, bytes):
data = bytearray(data)
num1 = XOR_KEY
for i in range(len(data)):
num2 = data[i]
data[i] ^= (num1 & BYTE_MAX_VALUE)
num1 = truncate(((num1 ^ num2) << 56) | num1 >> 8, 8, False)
return bytes(data)
if __name__ == "__main__":
# make sure settings exist
assert isfile(SETTINGS_FILE), "%s doesn't exist" % (SETTINGS_FILE)
# load settings
with open(SETTINGS_FILE, "r") as f:
settings = load(f)
# make sure assets exist
assert isfile(join(settings["asset_dir"], settings["gbids_file"])), "%s doesn't exist" % (settings["gbids_file"])
assert isfile(join(settings["asset_dir"], settings["slots_file"])), "%s doesn't exist" % (settings["slots_file"])
assert isfile(join(settings["asset_dir"], settings["affixes_file"])), "%s doesn't exist" % (settings["affixes_file"])
assert isfile(join(settings["asset_dir"], settings["currencies_file"])), "%s doesn't exist" % (settings["currencies_file"])
# load assets
with open(join(settings["asset_dir"], settings["gbids_file"]), "r") as f:
gbid_list = load(f)
with open(join(settings["asset_dir"], settings["slots_file"]), "r") as f:
slot_list = load(f)
with open(join(settings["asset_dir"], settings["affixes_file"]), "r") as f:
affix_list = load(f)
with open(join(settings["asset_dir"], settings["currencies_file"]), "r") as f:
currency_list = load(f)
# parse arguments
parser = ArgumentParser(description="A script to encrypt/decrypt and modify Diablo III saves")
parser.add_argument("-i", "--in-file", type=str, required=True, help="The account file you want to work with")
parser.add_argument("-o", "--out-file", type=str, default="account_modified.dat", help="The account file you want to output to")
select_group = parser.add_argument_group("selection")
select_group.add_argument("-s", "--slot", type=int, default=0, help="The slot ID you want to work with")
mod_group = parser.add_argument_group("modifications")
for single in currency_list:
mod_group.add_argument("--" + single.lower().replace("'", "").replace(" ", "-"), type=int, help="Set the amount of %s" % (single.lower()))
mod_group.add_argument("--all-currencies", type=int, help="Set all currencies to the given value")
args = parser.parse_args()
# make sure the input file exists
assert isfile(args.in_file), "input file doesn't exist"
# account
# decrypt
with open(args.in_file, "rb") as f:
account_enc = f.read()
account_dec = decrypt_save(account_enc)
# parse
asd = Account_pb2.SavedDefinition()
asd.ParseFromString(account_dec)
# modify account here
if len(asd.partitions[args.slot].currency_data.currency) > 0:
for currency_id in range(len(currency_list)):
currency_name = currency_list[currency_id]
if len(asd.partitions[args.slot].currency_data.currency) >= (currency_id + 1):
amt = None
try:
amt = getattr(args, currency_name.lower().replace("'", "").replace(" ", "_"))
if not amt and args.all_currencies:
amt = args.all_currencies
except AttributeError:
pass
if amt:
print("Set slot %s %s to %s" % (args.slot, currency_name.lower(), amt))
asd.partitions[args.slot].currency_data.currency[currency_id].count = amt
# end account modifications
account_mod_dec = asd.SerializeToString()
account_mod_enc = encrypt_save(account_mod_dec)
# output the modified account
if args.out_file:
with open(args.out_file, "wb") as f:
f.write(account_mod_enc)
# end account output