From 165bcbefee1c6c0f73553190ecffadcfdf40f16d Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sat, 9 Mar 2024 11:09:04 +1100 Subject: [PATCH] minor code polish --- source/base/StarAssets.cpp | 2 +- source/core/StarEncode.cpp | 4 +-- source/game/StarPlayer.cpp | 5 ++-- source/game/StarPlayerInventory.cpp | 43 +++++++++++++++-------------- source/game/StarPlayerInventory.hpp | 2 +- source/game/StarUniverseClient.cpp | 4 ++- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp index 0b21f5e..5628e4f 100644 --- a/source/base/StarAssets.cpp +++ b/source/base/StarAssets.cpp @@ -682,7 +682,7 @@ Json Assets::checkPatchArray(String const& path, AssetSourcePtr const& source, J auto externalRef = external.value(); auto newResult = result; for (auto const patch : patchData) { - switch(patch.type()){ + switch(patch.type()) { case Json::Type::Array: // if the patch is an array, go down recursively until we get objects try { newResult = checkPatchArray(path, source, newResult, patch.toArray(), externalRef); diff --git a/source/core/StarEncode.cpp b/source/core/StarEncode.cpp index 3a63653..798f388 100644 --- a/source/core/StarEncode.cpp +++ b/source/core/StarEncode.cpp @@ -204,7 +204,7 @@ String hexEncode(ByteArray const& data) { ByteArray hexDecode(String const& encodedData) { ByteArray res(encodedData.size() / 2, 0); - size_t decoded = hexDecode(encodedData.utf8Ptr(), encodedData.size(), res.ptr(), res.size()); + size_t decoded = hexDecode(encodedData.utf8Ptr(), encodedData.utf8Size(), res.ptr(), res.size()); _unused(decoded); starAssert(decoded == res.size()); return res; @@ -216,7 +216,7 @@ String base64Encode(ByteArray const& data) { ByteArray base64Decode(String const& encodedData) { ByteArray res(encodedData.size() * 3 / 4, 0); - size_t decoded = base64Decode(encodedData.utf8Ptr(), encodedData.size(), res.ptr(), res.size()); + size_t decoded = base64Decode(encodedData.utf8Ptr(), encodedData.utf8Size(), res.ptr(), res.size()); _unused(decoded); starAssert(decoded <= res.size()); res.resize(decoded); diff --git a/source/game/StarPlayer.cpp b/source/game/StarPlayer.cpp index 1ac1234..9790225 100644 --- a/source/game/StarPlayer.cpp +++ b/source/game/StarPlayer.cpp @@ -330,8 +330,9 @@ void Player::init(World* world, EntityId entityId, EntityMode mode) { p.second->addCallbacks("celestial", LuaBindings::makeCelestialCallbacks(m_client)); p.second->init(world); } - for (auto& p : m_inventory->clearOverflow()) { - world->addEntity(ItemDrop::createRandomizedDrop(p,m_movementController->position(),true)); + + for (auto& p : m_inventory->pullOverflow()) { + world->addEntity(ItemDrop::createRandomizedDrop(p, m_movementController->position(), true)); } } diff --git a/source/game/StarPlayerInventory.cpp b/source/game/StarPlayerInventory.cpp index 83d2266..dc8d8e4 100644 --- a/source/game/StarPlayerInventory.cpp +++ b/source/game/StarPlayerInventory.cpp @@ -756,10 +756,8 @@ Maybe PlayerInventory::secondaryHeldSlot() const { return {}; } -List PlayerInventory::clearOverflow(){ - auto list = m_inventoryLoadOverflow; - m_inventoryLoadOverflow.clear(); - return list; +List PlayerInventory::pullOverflow() { + return std::move(m_inventoryLoadOverflow); } void PlayerInventory::load(Json const& store) { @@ -781,16 +779,16 @@ void PlayerInventory::load(Json const& store) { for (auto const& p : itemBags) { auto& bagType = p.first; auto newBag = ItemBag::loadStore(p.second); - if (m_bags.keys().contains(bagType)) { - auto& bagPtr = m_bags[bagType]; - auto size = bagPtr.get()->size(); - if (bagPtr) - *bagPtr = std::move(newBag); + if (m_bags.contains(bagType)) { + auto& bag = m_bags[bagType]; + auto size = bag->size(); + if (bag) + *bag = std::move(newBag); else - bagPtr = make_shared(std::move(newBag)); - m_inventoryLoadOverflow.appendAll(bagPtr.get()->resize(size)); + bag = make_shared(std::move(newBag)); + m_inventoryLoadOverflow.appendAll(bag->resize(size)); } else { - m_inventoryLoadOverflow.appendAll(ItemBag(newBag).items()); + m_inventoryLoadOverflow.appendAll(newBag.items()); } } @@ -803,17 +801,20 @@ void PlayerInventory::load(Json const& store) { for (size_t i = 0; i < m_customBar.size(0); ++i) { for (size_t j = 0; j < m_customBar.size(1); ++j) { - Json cbl = store.get("customBar").get(i,JsonArray()).get(j,JsonArray()); - auto validateLink = [this](Json link) -> Json { - if ((link.isType(Json::Type::Object)) - && (m_bags.keys().contains(link.getString("type"))) - && (m_bags[link.getString("type")].get()->size() > link.getUInt("location"))) - return link; - return Json(); + Json cbl = store.get("customBar").get(i, JsonArray()).get(j, JsonArray()); + auto validateLink = [this](Maybe link) -> Maybe { + if (link && link->is()) { + auto& slot = link->get(); + if (m_bags.contains(slot.first) && size_t(slot.second) < m_bags[slot.first]->size()) + return link; + else + return {}; + } + return link; }; m_customBar.at(i, j) = CustomBarLink{ - jsonToMaybe(validateLink(cbl.get(0, Json())), jsonToInventorySlot), - jsonToMaybe(validateLink(cbl.get(1, Json())), jsonToInventorySlot) + validateLink(jsonToMaybe(cbl.get(0, {}), jsonToInventorySlot)), + validateLink(jsonToMaybe(cbl.get(1, {}), jsonToInventorySlot)) }; } } diff --git a/source/game/StarPlayerInventory.hpp b/source/game/StarPlayerInventory.hpp index bd5aa9f..911ffe3 100644 --- a/source/game/StarPlayerInventory.hpp +++ b/source/game/StarPlayerInventory.hpp @@ -168,7 +168,7 @@ public: Maybe primaryHeldSlot() const; Maybe secondaryHeldSlot() const; - List clearOverflow(); + List pullOverflow(); void load(Json const& store); Json store() const; diff --git a/source/game/StarUniverseClient.cpp b/source/game/StarUniverseClient.cpp index 72dd6a0..7d455ba 100644 --- a/source/game/StarUniverseClient.cpp +++ b/source/game/StarUniverseClient.cpp @@ -574,8 +574,10 @@ bool UniverseClient::switchPlayer(size_t index) { bool UniverseClient::switchPlayer(String const& name) { if (auto uuid = m_playerStorage->playerUuidByName(name, mainPlayer()->uuid())) return switchPlayer(*uuid); - else + else if (name.utf8Size() == UuidSize * 2) return switchPlayer(Uuid(name)); + else + return false; } UniverseClient::ReloadPlayerCallback& UniverseClient::playerReloadPreCallback() {