-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGamestatsDatabase.py
More file actions
229 lines (191 loc) · 7.3 KB
/
GamestatsDatabase.py
File metadata and controls
229 lines (191 loc) · 7.3 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#! /usr/bin/env python
"""Dummy GameSpy's gamestats database.
Work on both Python2 and Python3.
Usage:
python -i GamestatsDatabase.py
"""
import os
import sqlite3
import time
from contextlib import closing
GETPDR_SKIP_DATA_ON_ERROR = True
def dict_factory(cursor, row):
return {
col[0]: row[idx]
for idx, col in enumerate(cursor.description)
}
class PTYPE:
PD_PRIVATE_RO = 0
PD_PRIVATE_RW = 1
PD_PUBLIC_RO = 2
PD_PUBLIC_RW = 3
class DummyGamestatsDatabase():
PATH = "data"
VALID_FILENAME = \
"abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"0123456789" \
"+-_ ()[]"
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
def get_safe_filename(self, filename):
"""Return a safe filename."""
return ''.join(chr(c) for c in bytearray(filename)
if chr(c) in self.VALID_FILENAME)
def dummy_get_keydata(self, gamename, key):
"""Dummy get key data."""
gamedir = os.path.join(self.PATH, self.get_safe_filename(gamename))
if not os.path.exists(gamedir):
os.makedirs(gamedir)
key_file = self.get_safe_filename(key) + ".bin"
key_filepath = os.path.join(gamedir, key_file)
with open(key_filepath, 'rb') as f:
data_blocks = f.read().split(b'\x00')
if any(block for block in data_blocks[1:]):
# Data after nul byte?
pass
return b"".join([b'\\', key, b'\\', data_blocks[0]])
def get_data(self, gamename, pid, dindex, ptype, keys, mod):
"""Dummy get data.
TODO: Handle dindex, kv, pid, ptype.
"""
data = b''
for key in keys:
try:
key_data = self.dummy_get_keydata(gamename, key)
except:
if GETPDR_SKIP_DATA_ON_ERROR:
return b""
continue
data += key_data
return 1, data, int(time.time())
def set_data(self, gamename, pid, dindex, ptype, kv, key_values):
"""Dummy set data.
TODO: Handle dindex, kv, pid, ptype.
"""
gamedir = os.path.join(self.PATH, self.get_safe_filename(gamename))
if not os.path.exists(gamedir):
os.makedirs(gamedir)
for key, value in key_values:
key_file = self.get_safe_filename(key) + ".bin"
key_filepath = os.path.join(gamedir, key_file)
if not os.path.exists(key_filepath):
with open(key_filepath, "wb") as f:
f.write(value)
return 1, int(time.time())
class WiimmfiGamestatsDatabase():
PATH = "GamestatsDatabase.db"
DATABASE_TIMEOUT = 5.0
def __init__(self, path=None, timeout=None):
self.path = path or self.PATH
self.timeout = timeout or self.DATABASE_TIMEOUT
self.conn = sqlite3.connect(self.path, timeout=self.timeout)
self.conn.row_factory = dict_factory
self.conn.text_factory = bytes
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def init(self, path=None, timeout=None):
"""Initialize Gamestats database."""
conn = sqlite3.connect(path or self.PATH,
timeout=timeout or self.DATABASE_TIMEOUT)
c = conn.cursor()
# Gamestats
c.execute("CREATE TABLE IF NOT EXISTS gamestats"
" (gamename TEXT, pid INT, dindex TEXT, ptype INT,"
" data TEXT, mod UNSIGNED BIGINT)")
c.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_storage"
" ON gamestats (gamename, pid, dindex)")
conn.commit()
conn.close()
def close(self):
self.conn.close()
def parse(self, data):
ls = data.strip(b'\\').split(b'\\')
if len(ls) % 2:
# MUST BE EVEN!
ls.append(b'')
return list(zip(ls[0::2], ls[1::2]))
def set_data(self, gamename, pid, dindex, ptype, kv, key_values):
with closing(self.conn.cursor()) as cursor:
if kv:
success, data, mod = self.get_data(gamename, pid, dindex,
ptype)
if success and data:
old_key_values = self.parse(data)
for k, v in key_values:
for i, (old_k, old_v) in enumerate(old_key_values):
if k == old_k:
old_key_values[i] = (k, v)
break
else:
old_key_values.append((k, v))
key_values = old_key_values
data = b"".join(
b"\\" + key + b"\\" + value
for key, value in key_values
)
mod = int(time.time())
try:
cursor.execute(
"INSERT OR REPLACE INTO gamestats VALUES (?,?,?,?,?,?)",
(gamename, pid, dindex, ptype, data, mod)
)
self.conn.commit()
return 1, mod
except:
return 0, 0
def get_data(self, gamename, pid, dindex, ptype, keys=None, mod=0):
with closing(self.conn.cursor()) as cursor:
cursor.execute(
"SELECT * FROM gamestats"
" WHERE gamename = ? AND pid = ? AND dindex = ? AND ptype = ?",
(gamename, pid, dindex, ptype)
)
row = cursor.fetchone()
if not row:
return 1, b"", 0
if keys is None:
data = row["data"]
else:
key_values = self.parse(row["data"])
data = b"".join(
b"\\" + k + b"\\" + v.split(b"\0")[0]
for k, v in key_values
if k in keys
)
return 1 + (mod and row["mod"] < mod), data, row["mod"]
def get_data(gamename, pid, dindex, ptype, keys,
mod=0, is_wiimmfi=False, from_pid=None):
pid = int(pid)
ptype = int(ptype)
if from_pid is not None and pid != int(from_pid) and \
PTYPE.PD_PUBLIC_RO != ptype != PTYPE.PD_PUBLIC_RW:
raise ValueError("Private data")
if is_wiimmfi:
with WiimmfiGamestatsDatabase() as db:
return db.get_data(gamename, pid, dindex, ptype, keys, mod)
else:
with DummyGamestatsDatabase() as db:
return db.get_data(gamename, pid, dindex, ptype, keys, mod)
def set_data(gamename, pid, dindex, ptype, kv, key_values,
is_wiimmfi=False, from_pid=None):
pid = int(pid)
if from_pid is not None and pid != int(from_pid):
raise ValueError("Data can only be set by its owner")
ptype = int(ptype)
kv = int(kv)
if PTYPE.PD_PUBLIC_RW != ptype != PTYPE.PD_PRIVATE_RW:
raise ValueError("Read-only data")
if is_wiimmfi:
with WiimmfiGamestatsDatabase() as db:
return db.set_data(gamename, pid, dindex, ptype, kv, key_values)
else:
with DummyGamestatsDatabase() as db:
return db.set_data(gamename, pid, dindex, ptype, kv, key_values)
if __name__ == "__main__":
with WiimmfiGamestatsDatabase() as db:
db.init()