-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOneFileToSplitThemAll.py
More file actions
60 lines (50 loc) · 1.89 KB
/
OneFileToSplitThemAll.py
File metadata and controls
60 lines (50 loc) · 1.89 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
import struct
import zlib
import pickle
import json
import os
from concurrent.futures import ThreadPoolExecutor
class GPEncode(json.JSONEncoder):
def default(self, o):
try:
for e in ['Cameras', 'DockCamera', 'damageDistribution', 'salvoParams']:
o.__dict__.pop(e, o.__dict__)
return o.__dict__
except AttributeError:
return {}
def mkdir(subdir):
if not os.path.exists(subdir):
os.makedirs(subdir)
def writejson(_key, _value, index):
# Be resilient if typeinfo/type is missing
try:
t = _value.get('typeinfo', {}).get('type', 'UnknownType')
except AttributeError:
t = 'UnknownType'
typedir = subdir + os.sep + str(index) + os.sep + str(t)
mkdir(typedir)
with open(os.path.join(typedir, _key + '.json'), 'w', encoding='latin1') as ff:
json.dump(_value, ff, sort_keys=True, indent=4, separators=(',', ': '))
with open('GameParams.data', 'rb') as f:
gpd = f.read()
gpd = struct.pack('B' * len(gpd), *gpd[::-1])
gpd = zlib.decompress(gpd)
gpd = pickle.loads(gpd, encoding='latin1')
subdir = 'split'
mkdir(subdir)
ROOT_INDEX = 'root'
# Locate the dictionary under key '' and split all its values
# gpd may be a list/tuple of elements or a single dict
source_dict = None
if isinstance(gpd, (list, tuple)):
for elem in gpd:
if isinstance(elem, dict) and '' in elem and isinstance(elem[''], dict):
source_dict = elem['']
break
elif isinstance(gpd, dict) and '' in gpd and isinstance(gpd[''], dict):
source_dict = gpd['']
if source_dict:
# Clean via GPEncode then back to plain dict
elemjson = json.loads(json.dumps(source_dict, cls=GPEncode, ensure_ascii=False))
with ThreadPoolExecutor() as tpe:
tpe.map(lambda p: writejson(*p), [(k, v, ROOT_INDEX) for k, v in elemjson.items()])