-
Notifications
You must be signed in to change notification settings - Fork 93
/
structurer.py
executable file
·422 lines (387 loc) · 14.6 KB
/
structurer.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import copy
import datetime
import os
import sys
import zlib
import json
import time
from typing import Any, Callable
from palworld_save_tools.gvas import GvasFile
from palworld_save_tools.palsav import decompress_sav_to_gvas
from palworld_save_tools.paltypes import PALWORLD_CUSTOM_PROPERTIES, PALWORLD_TYPE_HINTS
from palworld_save_tools.archive import FArchiveReader, FArchiveWriter
from palworld_save_tools.rawdata import (
character,
group,
item_container,
item_container_slots,
)
from world_types import Player, Pal, Guild
from logger import log, redirect_stdout_stderr
# PALWORLD_CUSTOM_PROPERTIES: dict[
# str,
# tuple[
# Callable[[FArchiveReader, str, int, str], dict[str, Any]],
# Callable[[FArchiveWriter, str, dict[str, Any]], int],
# ],
# ] = {
# ".worldSaveData.GroupSaveDataMap": (group.decode, group.encode),
# ".worldSaveData.CharacterSaveParameterMap.Value.RawData": (
# character.decode,
# character.encode,
# ),
# ".worldSaveData.ItemContainerSaveData.Value.RawData": (
# item_container.decode,
# item_container.encode,
# ),
# }
wsd = None
gvas_file = None
def skip_decode(
reader: FArchiveReader, type_name: str, size: int, path: str
) -> dict[str, Any]:
if type_name == "ArrayProperty":
array_type = reader.fstring()
value = {
"skip_type": type_name,
"array_type": array_type,
"id": reader.optional_guid(),
"value": reader.read(size),
}
elif type_name == "MapProperty":
key_type = reader.fstring()
value_type = reader.fstring()
_id = reader.optional_guid()
value = {
"skip_type": type_name,
"key_type": key_type,
"value_type": value_type,
"id": _id,
"value": reader.read(size),
}
elif type_name == "StructProperty":
value = {
"skip_type": type_name,
"struct_type": reader.fstring(),
"struct_id": reader.guid(),
"id": reader.optional_guid(),
"value": reader.read(size),
}
else:
raise Exception(
f"Expected ArrayProperty or MapProperty or StructProperty, got {type_name} in {path}"
)
return value
def skip_encode(
writer: FArchiveWriter, property_type: str, properties: dict[str, Any]
) -> int:
if "skip_type" not in properties:
if properties["custom_type"] in PALWORLD_CUSTOM_PROPERTIES is not None:
return PALWORLD_CUSTOM_PROPERTIES[properties["custom_type"]][1](
writer, property_type, properties
)
if property_type == "ArrayProperty":
del properties["custom_type"]
del properties["skip_type"]
writer.fstring(properties["array_type"])
writer.optional_guid(properties.get("id", None))
writer.write(properties["value"])
return len(properties["value"])
elif property_type == "MapProperty":
del properties["custom_type"]
del properties["skip_type"]
writer.fstring(properties["key_type"])
writer.fstring(properties["value_type"])
writer.optional_guid(properties.get("id", None))
writer.write(properties["value"])
return len(properties["value"])
elif property_type == "StructProperty":
del properties["custom_type"]
del properties["skip_type"]
writer.fstring(properties["struct_type"])
writer.guid(properties["struct_id"])
writer.optional_guid(properties.get("id", None))
writer.write(properties["value"])
return len(properties["value"])
else:
raise Exception(
f"Expected ArrayProperty or MapProperty or StructProperty, got {property_type}"
)
def load_skiped_decode(wsd, skip_paths, recursive=True):
if isinstance(skip_paths, str):
skip_paths = [skip_paths]
for skip_path in skip_paths:
properties = wsd[skip_path]
if "skip_type" not in properties:
continue
parse_skiped_item(properties, skip_path, recursive)
if ".worldSaveData.%s" % skip_path in SKP_PALWORLD_CUSTOM_PROPERTIES:
del SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.%s" % skip_path]
SKP_PALWORLD_CUSTOM_PROPERTIES = copy.deepcopy(PALWORLD_CUSTOM_PROPERTIES)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.MapObjectSaveData"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.FoliageGridSaveDataMap"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.MapObjectSpawnerInStageSaveData"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.DynamicItemSaveData"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.CharacterContainerSaveData"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[
".worldSaveData.CharacterContainerSaveData.Value.Slots"
] = (skip_decode, skip_encode)
SKP_PALWORLD_CUSTOM_PROPERTIES[
".worldSaveData.CharacterContainerSaveData.Value.RawData"
] = (skip_decode, skip_encode)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.ItemContainerSaveData"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[
".worldSaveData.ItemContainerSaveData.Value.BelongInfo"
] = (skip_decode, skip_encode)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.ItemContainerSaveData.Value.Slots"] = (
skip_decode,
skip_encode,
)
SKP_PALWORLD_CUSTOM_PROPERTIES[".worldSaveData.ItemContainerSaveData.Value.RawData"] = (
skip_decode,
skip_encode,
)
def convert_sav(file):
global gvas_file, wsd
if file.endswith(".sav.json"):
log("Loading...")
with open(file, "r", encoding="utf-8") as f:
return f.read()
log("Converting...")
with redirect_stdout_stderr():
try:
with open(file, "rb") as f:
data = f.read()
raw_gvas, _ = decompress_sav_to_gvas(data)
gvas_file = GvasFile.read(
raw_gvas, PALWORLD_TYPE_HINTS, SKP_PALWORLD_CUSTOM_PROPERTIES
)
except zlib.error:
log("This .sav file is corrupted. :(", "ERROR")
sys.exit(1)
# return json.dumps(gvas_file.dump(), cls=CustomEncoder)
wsd = gvas_file.properties["worldSaveData"]["value"]
def structure_player(dir_path, data_source=None):
log("Structuring players...")
global wsd
if data_source is None:
data_source = wsd
if not data_source.get("CharacterSaveParameterMap"):
return []
uid_character = (
(
c["key"]["PlayerUId"]["value"],
c["value"]["RawData"]["value"]["object"]["SaveParameter"]["value"],
)
for c in wsd["CharacterSaveParameterMap"]["value"]
)
players = []
pals = []
for uid, c in uid_character:
if c.get("IsPlayer") and c["IsPlayer"]["value"]:
c["Items"] = getPlayerItems(uid, dir_path)
players.append(Player(uid, c).to_dict())
else:
if not c.get("OwnerPlayerUId"):
continue
pals.append(Pal(c).to_dict())
unique_players_dict = {}
for player in players:
player_uid = player["player_uid"]
if player_uid in unique_players_dict:
existing_player = unique_players_dict[player_uid]
if player["level"] > existing_player["level"]:
unique_players_dict[player_uid] = player
else:
unique_players_dict[player_uid] = player
unique_players = list(unique_players_dict.values())
for pal in pals:
for player in unique_players:
if player["player_uid"] == pal["owner"]:
pal.pop("owner")
player["pals"].append(pal)
break
sorted_players = sorted(unique_players, key=lambda p: p["level"], reverse=True)
return sorted_players
def parse_skiped_item(properties, skip_path, recursive=True):
if "skip_type" not in properties:
return properties
with FArchiveReader(
properties["value"],
PALWORLD_TYPE_HINTS,
(
SKP_PALWORLD_CUSTOM_PROPERTIES
if recursive == False
else PALWORLD_CUSTOM_PROPERTIES
),
) as reader:
if properties["skip_type"] == "ArrayProperty":
properties["value"] = reader.array_property(
properties["array_type"],
len(properties["value"]) - 4,
".worldSaveData.%s" % skip_path,
)
elif properties["skip_type"] == "StructProperty":
properties["value"] = reader.struct_value(
properties["struct_type"], ".worldSaveData.%s" % skip_path
)
elif properties["skip_type"] == "MapProperty":
reader.u32()
count = reader.u32()
path = ".worldSaveData.%s" % skip_path
key_path = path + ".Key"
key_type = properties["key_type"]
value_type = properties["value_type"]
if key_type == "StructProperty":
key_struct_type = reader.get_type_or(key_path, "Guid")
else:
key_struct_type = None
value_path = path + ".Value"
if value_type == "StructProperty":
value_struct_type = reader.get_type_or(value_path, "StructProperty")
else:
value_struct_type = None
values: list[dict[str, Any]] = []
for _ in range(count):
key = reader.prop_value(key_type, key_struct_type, key_path)
value = reader.prop_value(value_type, value_struct_type, value_path)
values.append(
{
"key": key,
"value": value,
}
)
properties["key_struct_type"] = key_struct_type
properties["value_struct_type"] = value_struct_type
properties["value"] = values
del properties["custom_type"]
del properties["skip_type"]
return properties
def parse_item(properties, skip_path):
if isinstance(properties, dict):
for key in properties:
call_skip_path = skip_path + "." + key[0].upper() + key[1:]
if (
isinstance(properties[key], dict)
and "type" in properties[key]
and properties[key]["type"]
in ["StructProperty", "ArrayProperty", "MapProperty"]
):
if "skip_type" in properties[key]:
# print("Parsing worldSaveData.%s..." % call_skip_path, end="", flush=True)
properties[key] = parse_skiped_item(
properties[key], call_skip_path, True
)
# print("Done")
else:
properties[key]["value"] = parse_item(
properties[key]["value"], call_skip_path
)
else:
properties[key] = parse_item(properties[key], call_skip_path)
elif isinstance(properties, list):
top_skip_path = ".".join(skip_path.split(".")[:-1])
for idx, item in enumerate(properties):
properties[idx] = parse_item(item, top_skip_path)
return properties
def getPlayerItems(player_uid, dir_path):
load_skiped_decode(wsd, ["ItemContainerSaveData"], False)
item_containers = {}
for item_container in wsd["ItemContainerSaveData"]["value"]:
item_containers[str(item_container["key"]["ID"]["value"])] = item_container
player_sav_file = os.path.join(
dir_path, str(player_uid).upper().replace("-", "") + ".sav"
)
if not os.path.exists(player_sav_file):
# log("Player Sav file Not exists: %s" % player_sav_file)
return
else:
with redirect_stdout_stderr():
try:
with open(player_sav_file, "rb") as f:
raw_gvas, _ = decompress_sav_to_gvas(f.read())
player_gvas_file = GvasFile.read(
raw_gvas, PALWORLD_TYPE_HINTS, PALWORLD_CUSTOM_PROPERTIES
)
player_gvas = player_gvas_file.properties["SaveData"]["value"]
except Exception as e:
log(
f"Player Sav file is corrupted: {os.path.basename(player_sav_file)}: {str(e)}",
"ERROR",
)
return
containers_data = {
"CommonContainerId": [],
"DropSlotContainerId": [],
"EssentialContainerId": [],
"FoodEquipContainerId": [],
"PlayerEquipArmorContainerId": [],
"WeaponLoadOutContainerId": [],
}
for idx_key in containers_data.keys():
container_id = str(
player_gvas["InventoryInfo"]["value"][idx_key]["value"]["ID"]["value"]
)
if container_id in item_containers:
# 解析对应的物品容器数据
item_container = parse_item(
item_containers[container_id], "ItemContainerSaveData"
)
# 提取每个物品的相关数据并保存到字典中
containers_data[idx_key] = [
{
"SlotIndex": item["SlotIndex"]["value"],
"ItemId": item["ItemId"]["value"]["StaticId"]["value"].lower(),
"StackCount": item["StackCount"]["value"],
}
for item in item_container["value"]["Slots"]["value"]["values"]
if item["ItemId"]["value"]["StaticId"]["value"].lower() != "none"
]
return containers_data
def structure_guild(filetime: int = -1):
log("Structuring guilds...")
if not wsd.get("GroupSaveDataMap"):
return []
groups = (
g["value"]["RawData"]["value"]
for g in wsd["GroupSaveDataMap"]["value"]
if g["value"]["GroupType"]["value"]["value"] == "EPalGroupType::Guild"
)
Ticks = wsd["GameTimeSaveData"]["value"]["RealDateTimeTicks"]["value"]
guilds_generator = (Guild(g, Ticks, filetime).to_dict() for g in groups)
sorted_guilds = sorted(
guilds_generator, key=lambda g: g["base_camp_level"], reverse=True
)
return list(sorted_guilds)
if __name__ == "__main__":
import time
start = time.time()
file = "./Level.sav"
converted = convert_sav(file)
players = structure_player(converted)
log("Saving players...")
with open("players.json", "w", encoding="utf-8") as f:
json.dump(players, f, indent=4, ensure_ascii=False)
guilds = structure_guild(converted)
log("Saving guilds...")
with open("guilds.json", "w", encoding="utf-8") as f:
json.dump(guilds, f, indent=4, ensure_ascii=False)
log(f"Done in {time.time() - start}s")