minor code polish

This commit is contained in:
Kae 2024-03-09 11:09:04 +11:00
parent 52a6f200ed
commit 165bcbefee
6 changed files with 32 additions and 28 deletions

View File

@ -204,7 +204,7 @@ String hexEncode(ByteArray const& data) {
ByteArray hexDecode(String const& encodedData) { ByteArray hexDecode(String const& encodedData) {
ByteArray res(encodedData.size() / 2, 0); 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); _unused(decoded);
starAssert(decoded == res.size()); starAssert(decoded == res.size());
return res; return res;
@ -216,7 +216,7 @@ String base64Encode(ByteArray const& data) {
ByteArray base64Decode(String const& encodedData) { ByteArray base64Decode(String const& encodedData) {
ByteArray res(encodedData.size() * 3 / 4, 0); 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); _unused(decoded);
starAssert(decoded <= res.size()); starAssert(decoded <= res.size());
res.resize(decoded); res.resize(decoded);

View File

@ -330,7 +330,8 @@ void Player::init(World* world, EntityId entityId, EntityMode mode) {
p.second->addCallbacks("celestial", LuaBindings::makeCelestialCallbacks(m_client)); p.second->addCallbacks("celestial", LuaBindings::makeCelestialCallbacks(m_client));
p.second->init(world); p.second->init(world);
} }
for (auto& p : m_inventory->clearOverflow()) {
for (auto& p : m_inventory->pullOverflow()) {
world->addEntity(ItemDrop::createRandomizedDrop(p, m_movementController->position(), true)); world->addEntity(ItemDrop::createRandomizedDrop(p, m_movementController->position(), true));
} }
} }

View File

@ -756,10 +756,8 @@ Maybe<InventorySlot> PlayerInventory::secondaryHeldSlot() const {
return {}; return {};
} }
List<ItemPtr> PlayerInventory::clearOverflow(){ List<ItemPtr> PlayerInventory::pullOverflow() {
auto list = m_inventoryLoadOverflow; return std::move(m_inventoryLoadOverflow);
m_inventoryLoadOverflow.clear();
return list;
} }
void PlayerInventory::load(Json const& store) { void PlayerInventory::load(Json const& store) {
@ -781,16 +779,16 @@ void PlayerInventory::load(Json const& store) {
for (auto const& p : itemBags) { for (auto const& p : itemBags) {
auto& bagType = p.first; auto& bagType = p.first;
auto newBag = ItemBag::loadStore(p.second); auto newBag = ItemBag::loadStore(p.second);
if (m_bags.keys().contains(bagType)) { if (m_bags.contains(bagType)) {
auto& bagPtr = m_bags[bagType]; auto& bag = m_bags[bagType];
auto size = bagPtr.get()->size(); auto size = bag->size();
if (bagPtr) if (bag)
*bagPtr = std::move(newBag); *bag = std::move(newBag);
else else
bagPtr = make_shared<ItemBag>(std::move(newBag)); bag = make_shared<ItemBag>(std::move(newBag));
m_inventoryLoadOverflow.appendAll(bagPtr.get()->resize(size)); m_inventoryLoadOverflow.appendAll(bag->resize(size));
} else { } else {
m_inventoryLoadOverflow.appendAll(ItemBag(newBag).items()); m_inventoryLoadOverflow.appendAll(newBag.items());
} }
} }
@ -804,16 +802,19 @@ void PlayerInventory::load(Json const& store) {
for (size_t i = 0; i < m_customBar.size(0); ++i) { for (size_t i = 0; i < m_customBar.size(0); ++i) {
for (size_t j = 0; j < m_customBar.size(1); ++j) { for (size_t j = 0; j < m_customBar.size(1); ++j) {
Json cbl = store.get("customBar").get(i, JsonArray()).get(j, JsonArray()); Json cbl = store.get("customBar").get(i, JsonArray()).get(j, JsonArray());
auto validateLink = [this](Json link) -> Json { auto validateLink = [this](Maybe<InventorySlot> link) -> Maybe<InventorySlot> {
if ((link.isType(Json::Type::Object)) if (link && link->is<BagSlot>()) {
&& (m_bags.keys().contains(link.getString("type"))) auto& slot = link->get<BagSlot>();
&& (m_bags[link.getString("type")].get()->size() > link.getUInt("location"))) if (m_bags.contains(slot.first) && size_t(slot.second) < m_bags[slot.first]->size())
return link;
else
return {};
}
return link; return link;
return Json();
}; };
m_customBar.at(i, j) = CustomBarLink{ m_customBar.at(i, j) = CustomBarLink{
jsonToMaybe<InventorySlot>(validateLink(cbl.get(0, Json())), jsonToInventorySlot), validateLink(jsonToMaybe<InventorySlot>(cbl.get(0, {}), jsonToInventorySlot)),
jsonToMaybe<InventorySlot>(validateLink(cbl.get(1, Json())), jsonToInventorySlot) validateLink(jsonToMaybe<InventorySlot>(cbl.get(1, {}), jsonToInventorySlot))
}; };
} }
} }

View File

@ -168,7 +168,7 @@ public:
Maybe<InventorySlot> primaryHeldSlot() const; Maybe<InventorySlot> primaryHeldSlot() const;
Maybe<InventorySlot> secondaryHeldSlot() const; Maybe<InventorySlot> secondaryHeldSlot() const;
List<ItemPtr> clearOverflow(); List<ItemPtr> pullOverflow();
void load(Json const& store); void load(Json const& store);
Json store() const; Json store() const;

View File

@ -574,8 +574,10 @@ bool UniverseClient::switchPlayer(size_t index) {
bool UniverseClient::switchPlayer(String const& name) { bool UniverseClient::switchPlayer(String const& name) {
if (auto uuid = m_playerStorage->playerUuidByName(name, mainPlayer()->uuid())) if (auto uuid = m_playerStorage->playerUuidByName(name, mainPlayer()->uuid()))
return switchPlayer(*uuid); return switchPlayer(*uuid);
else else if (name.utf8Size() == UuidSize * 2)
return switchPlayer(Uuid(name)); return switchPlayer(Uuid(name));
else
return false;
} }
UniverseClient::ReloadPlayerCallback& UniverseClient::playerReloadPreCallback() { UniverseClient::ReloadPlayerCallback& UniverseClient::playerReloadPreCallback() {