-
Notifications
You must be signed in to change notification settings - Fork 2
/
SaveDataModifier.py
118 lines (94 loc) · 3.32 KB
/
SaveDataModifier.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# Author: Beeven Yip
# Created on 2015-6-26
import hashlib
import base64
import argparse
import os.path
import io
import struct
import xml.etree.ElementTree as ET
class Modifier(object):
def __init__(self, byte_order, data, properties):
""" byte_order: '>' big_endian for Android
'<' little_endian for iOS
"""
self._data = bytearray(data)
self._byte_order = byte_order
self._known_properties = properties
def __getattr__(self, name):
if self._known_properties.has_key(name):
v = self._known_properties[name]
d = struct.unpack_from(self._byte_order+v[0],self._data,v[1])
if len(d) > 1:
return d
else:
return d[0]
else:
raise AttributeError
def __setattr__(self, name, value):
if self.__dict__.has_key("_known_properties") and self.__dict__["_known_properties"].has_key(name):
v = self._known_properties[name]
struct.pack_into(self._byte_order+v[0], self._data, v[1], value)
else:
super(Modifier,self).__setattr__(name, value)
@property
def signature(self):
return self._data[-32:]
@signature.setter
def signature(self, value):
self._data[-32:] = value
@property
def computed_hash(self):
return hashlib.md5("battlecats"+self._data[:-32]).hexdigest()
@property
def known_properties(self):
return dict( (k,self.__getattr__(k)) for k in self._known_properties.keys())
def save_to_file(self, filename):
self.signature = self.computed_hash
pass
def extract_data(self, filename):
with open(filename, "wb") as f:
f.write(self._data)
def replace_data(self, filename):
with open(filename, "rb") as f:
self._data = bytearray(f.read())
class iOS(Modifier):
def __init__(self, filename=None):
if filename is None:
filename = "SAVE_DATA"
with open(filename, "rb") as f:
data = bytearray(f.read())
properties = {
"cat_food": ("L",7),
"xp": ("L",75),
"rare_ticket": ("L",8374),
"ticket": ("L",8370),
"tracking_id": ("9s", 104154),
"medal":("192L",2504)
}
Modifier.__init__(self, "<", data, properties)
def save_to_file(self, filename):
Modifier.save_to_file(self, filename)
with open(filename, "wb") as f:
f.write(self._data)
class Android(Modifier):
def __init__(self, filename=None):
if filename is None:
filename = "save.xml"
self._xmltree = ET.parse(filename)
data = base64.b64decode(self._xmltree.getroot().find("./string[@name='SAVE_DATA']").text)
properties = {
"cat_food": ("L",7),
"xp": ("L",75),
"rare_ticket": ("L",8368),
"ticket": ("L",8364),
"tracking_id": ("9s", 104154)
}
Modifier.__init__(self, ">",data, properties)
def save_to_file(self, filename):
Modifier.save_to_file(self, filename)
node = self._xmltree.getroot().find("./string[@name='SAVE_DATA']")
node.text = base64.b64encode(self._data)
self._xmltree.write(filename)