Skip to content

Commit

Permalink
feat: add --custom-properties to allow users to select which custom p…
Browse files Browse the repository at this point in the history
…roperties to decode (#133)

* clean up commented code

* feat: add --custom-properties to allow users to select which custom properties to decode
  • Loading branch information
cheahjs committed Feb 6, 2024
1 parent 91ccfe0 commit 4f8234f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Additional command line arguments:
1. `--output`: Override the default output path
1. `--minify-json`: Minify output JSON to help speed up processing by other tools consuming JSON
1. `--force`: Overwrite output files if they exist without prompting
1. `--custom-properties`: Comma-separated list of paths from [paltypes.py](./palworld_save_tools/paltypes.py) to decode.
This can be used to ignore processing of types that are not of interest.
For example `--custom-properties .worldSaveData.GroupSaveDataMap,.worldSaveData.CharacterSaveParameterMap.Value.RawData` will only parse guild data and character data.

## Developers

Expand Down
24 changes: 22 additions & 2 deletions palworld_save_tools/commands/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ def main():
action="store_true",
help="Convert NaN/Inf/-Inf floats to null when converting from SAV to JSON. This will lose information in the event Inf/-Inf is in the sav file (default: false)",
)
parser.add_argument(
"--custom-properties",
default=",".join(PALWORLD_CUSTOM_PROPERTIES.keys()),
type=lambda t: [s.strip() for s in t.split(",")],
help="Comma-separated list of custom properties to decode, or 'all' for all known properties. This can be used to speed up processing by excluding properties that are not of interest. (default: all)",
)

parser.add_argument("--minify-json", action="store_true", help="Minify JSON output")
args = parser.parse_args()

Expand All @@ -67,6 +74,7 @@ def main():
force=args.force,
minify=args.minify_json,
allow_nan=(not args.convert_nan_to_null),
custom_properties_keys=args.custom_properties,
)

if args.from_json or args.filename.endswith(".json"):
Expand All @@ -78,7 +86,12 @@ def main():


def convert_sav_to_json(
filename, output_path, force=False, minify=False, allow_nan=True
filename,
output_path,
force=False,
minify=False,
allow_nan=True,
custom_properties_keys=["all"],
):
print(f"Converting {filename} to JSON, saving to {output_path}")
if os.path.exists(output_path):
Expand All @@ -91,8 +104,15 @@ def convert_sav_to_json(
data = f.read()
raw_gvas, _ = decompress_sav_to_gvas(data)
print(f"Loading GVAS file")
custom_properties = {}
if len(custom_properties) > 0 and custom_properties_keys[0] == "all":
custom_properties = PALWORLD_CUSTOM_PROPERTIES
else:
for prop in PALWORLD_CUSTOM_PROPERTIES:
if prop in custom_properties_keys:
custom_properties[prop] = PALWORLD_CUSTOM_PROPERTIES[prop]
gvas_file = GvasFile.read(
raw_gvas, PALWORLD_TYPE_HINTS, PALWORLD_CUSTOM_PROPERTIES, allow_nan=allow_nan
raw_gvas, PALWORLD_TYPE_HINTS, custom_properties, allow_nan=allow_nan
)
print(f"Writing JSON to {output_path}")
with open(output_path, "w", encoding="utf8") as f:
Expand Down
27 changes: 0 additions & 27 deletions palworld_save_tools/paltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@
from palworld_save_tools.rawdata import (
base_camp,
base_camp_module,
build_process,
character,
character_container,
connector,
debug,
dynamic_item,
foliage_model,
foliage_model_instance,
group,
item_container,
item_container_slots,
map_concrete_model,
map_model,
map_object,
work,
work_collection,
Expand Down Expand Up @@ -80,7 +75,6 @@
character_container.decode,
character_container.encode,
),
# DynamicItemSaveData is problematic because serialisation is dependent on type, which is not immediately obvious
".worldSaveData.DynamicItemSaveData.DynamicItemSaveData.RawData": (
dynamic_item.decode,
dynamic_item.encode,
Expand Down Expand Up @@ -114,25 +108,4 @@
map_object.decode,
map_object.encode,
),
# ".worldSaveData.WorkSaveData.WorkSaveData.RawData": (debug.decode, debug.encode),
# ".worldSaveData.WorkSaveData.WorkSaveData.WorkAssignMap.Value.RawData": (debug.decode, debug.encode),
# ConcreteModel is problematic because serialisation is dependent on type, which is not immediately obvious
# ".worldSaveData.MapObjectSaveData.MapObjectSaveData.ConcreteModel": (
# map_concrete_model.decode,
# map_concrete_model.encode,
# ),
# ".worldSaveData.MapObjectSaveData.MapObjectSaveData.ConcreteModel.RawData": (),
# ".worldSaveData.MapObjectSaveData.MapObjectSaveData.ConcreteModel.ModuleMap.Value.RawData": (),
# ".worldSaveData.MapObjectSaveData.MapObjectSaveData.Model.BuildProcess.RawData": (
# build_process.decode,
# build_process.encode,
# ),
# ".worldSaveData.MapObjectSaveData.MapObjectSaveData.Model.Connector.RawData": (
# connector.decode,
# connector.encode,
# ),
# ".worldSaveData.MapObjectSaveData.MapObjectSaveData.Model.RawData": (
# map_model.decode,
# map_model.encode,
# ),
}

0 comments on commit 4f8234f

Please sign in to comment.