-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExample.py
More file actions
84 lines (62 loc) · 3.14 KB
/
Example.py
File metadata and controls
84 lines (62 loc) · 3.14 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
# imports to convert between .sav and .json
from SavConverter import sav_to_json, read_sav, json_to_sav, load_json
# imports to navigate and manipulate the json structure
from SavConverter import obj_to_json, print_json, get_object_by_path, insert_object_by_path, replace_object_by_path, update_property_by_path
# The following lines are an example of the .sav to .json conversion process
# Get .sav property classes
properties = read_sav('ExampleSavFiles/CrabChampions_SaveSlot_without_AutoSave.sav')
# Convert properties to json
output = sav_to_json(properties, string = True)
# Write json string to file
with open('CrabChampions_SaveSlot.json', 'w') as json_file:
json_file.write(output)
# The following lines are an example of the traversal and manipulation of this specific .json structure using paths
# load your converted json file
data = load_json('CrabChampions_SaveSlot.json')
# path to the first Weapon object (0) in the list of Weapon objects ('value') in the RankedWeapons object ({"name": "RankedWeapons"})
path_to_find = [{"name": "RankedWeapons"}, "value", 0]
# Full path to the value ('value') in Rank object ({'name':'Rank'}) within that first weapon object
path_to_update = [{"name": "RankedWeapons"}, "value", 0, {'name':'Rank'}, 'value']
# new value for the 'value' key in the Rank object
new_value = 'ECrabRank::Gold'
# find and display the first weapon object
obj = get_object_by_path(data, path_to_find)
print("Found first Weapon object:")
print_json(obj)
# update the value property of the Rank object
update_property_by_path(data, path_to_update, new_value)
print("\nUpdated Rank object value:")
print_json(get_object_by_path(data, path_to_find))
# same path for will be used to show object replacement
path_to_replace = [{"name": "RankedWeapons"}, "value", 0, {'name': 'Rank'}]
# new Rank object
new_object = {
"type": "EnumProperty",
"name": "Rank",
"enum": "ECrabRank",
"value": "ECrabRank::Bronze"
}
# replace the rank object with the new one
replace_object_by_path(data, path_to_replace, new_object)
print("\nReplaced Rank object:")
print_json(get_object_by_path(data, path_to_find))
# Check if SaveSlot has an AutoSave object and insert test AutoSave if not
path_to_autosave = [{'name': 'AutoSave'}]
autosave = get_object_by_path(data, path_to_autosave)
if autosave == None:
print('\nNo AutoSave object found. Inserting AutoSave.')
# path of object to insert new object before or after
path_to_insert = [{"type": "FileEndProperty"}]
# Reading in previously extracted AutoSave object
autosave = load_json('ExampleSavFiles/CrabChampions_AutoSave.json')
# insert AutoSave object before FileEndProperty object
insert_object_by_path(data, path_to_insert, autosave, position='before')
if get_object_by_path(data, path_to_autosave) != None:
print("\nFound inserted AutoSave.")
# The following lines show the process of converting the edited .json back to .sav
# Converting json string back to binary
binary_data = json_to_sav(data)
# write new .sav file converted from .json
with open('New_CrabChampions_SaveSlot.sav', 'wb') as file:
# Write the binary data to the file
file.write(binary_data)