From 2b9f517de44ca572f47dc1ffef95a86a364b73df Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 18 Nov 2019 23:15:15 +0100 Subject: [PATCH] Improve item_location (de)serialization handling Write a null item location if we failed to unpack the character pointer to avoid invalid json. Assume g->u was the character if there is no character field on deserialization since all character item locations were on g->u before the character field was introduced. --- src/item_location.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/item_location.cpp b/src/item_location.cpp index e66f9ecb20674..903cd46cdf293 100644 --- a/src/item_location.cpp +++ b/src/item_location.cpp @@ -254,6 +254,10 @@ class item_location::impl::item_on_person : public item_location::impl void serialize( JsonOut &js ) const override { if( !ensure_who_unpacked() ) { + // Write an invalid item_location to avoid invalid json + js.start_object(); + js.member( "type", "null" ); + js.end_object(); return; } js.start_object(); @@ -541,7 +545,13 @@ void item_location::deserialize( JsonIn &js ) if( type == "character" ) { character_id who_id; - obj.read( "character", who_id ); + if( obj.has_member( "character" ) ) { + obj.read( "character", who_id ); + } else { + // This is for migrating saves before npc item locations were supported and all + // character item locations were assumed to be on g->u + who_id = g->u.getID(); + } ptr.reset( new impl::item_on_person( who_id, idx ) ); } else if( type == "map" ) {