From 012572c199e058e930c34cbb3b3d02efd476bd9e Mon Sep 17 00:00:00 2001 From: Zithia Satazaki Date: Mon, 30 Oct 2023 01:30:25 -0400 Subject: [PATCH] more graceful failure mode for item bag functions --- source/game/StarPlayerInventory.cpp | 1 + source/game/scripting/StarPlayerLuaBindings.cpp | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/game/StarPlayerInventory.cpp b/source/game/StarPlayerInventory.cpp index 58e2b31..4f584e5 100644 --- a/source/game/StarPlayerInventory.cpp +++ b/source/game/StarPlayerInventory.cpp @@ -424,6 +424,7 @@ BackArmorPtr PlayerInventory::backCosmetic() const { } ItemBagConstPtr PlayerInventory::bagContents(String const& type) const { + if (!m_bags.contains(type)) return nullptr; return m_bags.get(type); } diff --git a/source/game/scripting/StarPlayerLuaBindings.cpp b/source/game/scripting/StarPlayerLuaBindings.cpp index 4713be0..c26422d 100644 --- a/source/game/scripting/StarPlayerLuaBindings.cpp +++ b/source/game/scripting/StarPlayerLuaBindings.cpp @@ -204,19 +204,21 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { callbacks.registerCallback("itemBagSize", [player](String const& bag) { auto inventory = player->inventory(); auto b = inventory->bagContents(bag); - return b->size(); + if (!b) return 0; + return (int)b->size(); }); callbacks.registerCallback("itemAllowedInBag", [player](String const& bag, Json const& item) { auto inventory = player->inventory(); auto itemDatabase = Root::singleton().itemDatabase(); + if (!inventory->bagContents(bag)) return false; return inventory->itemAllowedInBag(itemDatabase->item(ItemDescriptor(item)), bag); }); callbacks.registerCallback("itemBagItem", [player](String const& bag, int slot) -> Json { auto inventory = player->inventory(); auto b = inventory->bagContents(bag); - //if (b.get() == nullptr) return {}; + if (!b || slot <= 0 || slot > (int)b->size()) return {}; return itemSafeDescriptor(b->at(slot - 1)).toJson(); }); @@ -224,6 +226,7 @@ LuaCallbacks LuaBindings::makePlayerCallbacks(Player* player) { auto inventory = player->inventory(); auto itemDatabase = Root::singleton().itemDatabase(); auto b = const_pointer_cast(inventory->bagContents(bag)); // bit of a Naughty Access Cheat here, but + if (!b || slot <= 0 || slot > (int)b->size()) return; b->setItem(slot - 1, itemDatabase->item(ItemDescriptor(item))); });