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

@ -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);

View File

@ -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);

View File

@ -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));
}
}

View File

@ -756,10 +756,8 @@ Maybe<InventorySlot> PlayerInventory::secondaryHeldSlot() const {
return {};
}
List<ItemPtr> PlayerInventory::clearOverflow(){
auto list = m_inventoryLoadOverflow;
m_inventoryLoadOverflow.clear();
return list;
List<ItemPtr> 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<ItemBag>(std::move(newBag));
m_inventoryLoadOverflow.appendAll(bagPtr.get()->resize(size));
bag = make_shared<ItemBag>(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")))
Json cbl = store.get("customBar").get(i, JsonArray()).get(j, JsonArray());
auto validateLink = [this](Maybe<InventorySlot> link) -> Maybe<InventorySlot> {
if (link && link->is<BagSlot>()) {
auto& slot = link->get<BagSlot>();
if (m_bags.contains(slot.first) && size_t(slot.second) < m_bags[slot.first]->size())
return link;
else
return {};
}
return link;
return Json();
};
m_customBar.at(i, j) = CustomBarLink{
jsonToMaybe<InventorySlot>(validateLink(cbl.get(0, Json())), jsonToInventorySlot),
jsonToMaybe<InventorySlot>(validateLink(cbl.get(1, Json())), jsonToInventorySlot)
validateLink(jsonToMaybe<InventorySlot>(cbl.get(0, {}), jsonToInventorySlot)),
validateLink(jsonToMaybe<InventorySlot>(cbl.get(1, {}), jsonToInventorySlot))
};
}
}

View File

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

View File

@ -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() {