diff --git a/src/content/Content.hpp b/src/content/Content.hpp index eb007399..5d0611b1 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -82,6 +82,10 @@ public: return defs.size(); } + inline const auto& getIterable() const { + return defs; + } + inline const T* const* getDefs() const { return defs.data(); } diff --git a/src/content/ContentLUT.hpp b/src/content/ContentLUT.hpp index 2e29d985..6bc2e139 100644 --- a/src/content/ContentLUT.hpp +++ b/src/content/ContentLUT.hpp @@ -37,8 +37,8 @@ public: for (size_t i = 0; i < count; i++) { indices.push_back(i); } - for (size_t i = 0; i < unitIndices.count(); i++) { - names.push_back(unitIndices.get(i)->name); //FIXME: .get(i) potentional null pointer //-V522 + for (auto unit : unitIndices.getIterable()) { + names.push_back(unit->name); } for (size_t i = unitIndices.count(); i < count; i++) { names.emplace_back(""); diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index d858427d..a9f81f6c 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -103,9 +103,8 @@ template static void write_indices( const ContentUnitIndices& indices, dynamic::List& list ) { - size_t count = indices.count(); - for (size_t i = 0; i < count; i++) { - list.put(indices.get(i)->name); //FIXME: .get(i) potential null pointer //-V522 + for (auto unit : indices.getIterable()) { + list.put(unit->name); } } diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index e77032ce..195b4fcd 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -17,10 +17,11 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : conte sideregions = std::make_unique(indices->blocks.count() * 6); auto atlas = assets->get("blocks"); - for (uint i = 0; i < indices->blocks.count(); i++) { - auto def = indices->blocks.getWriteable(i); //FIXME: Potential null pointer + const auto& blocks = indices->blocks.getIterable(); + for (uint i = 0; i < blocks.size(); i++) { + auto def = blocks[i]; for (uint side = 0; side < 6; side++) { - const std::string& tex = def->textureFaces[side]; //-V522 + const std::string& tex = def->textureFaces[side]; if (atlas->has(tex)) { sideregions[i * 6 + side] = atlas->get(tex); } else if (atlas->has(TEXTURE_NOTFOUND)) { diff --git a/src/graphics/render/BlocksPreview.cpp b/src/graphics/render/BlocksPreview.cpp index 2955a8c7..cba0f90d 100644 --- a/src/graphics/render/BlocksPreview.cpp +++ b/src/graphics/render/BlocksPreview.cpp @@ -22,30 +22,30 @@ std::unique_ptr BlocksPreview::draw( Shader* shader, Framebuffer* fbo, Batch3D* batch, - const Block* def, + const Block& def, int size ){ Window::clear(); - blockid_t id = def->rt.id; + blockid_t id = def.rt.id; const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1), cache->getRegion(id, 2), cache->getRegion(id, 3), cache->getRegion(id, 4), cache->getRegion(id, 5)}; glm::vec3 offset(0.1f, 0.5f, 0.1f); - switch (def->model) { + switch (def.model) { case BlockModel::none: // something went wrong... break; case BlockModel::block: shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); batch->blockCube(glm::vec3(size * 0.63f), texfaces, - glm::vec4(1.0f), !def->rt.emissive); + glm::vec4(1.0f), !def.rt.emissive); batch->flush(); break; case BlockModel::aabb: { glm::vec3 hitbox {}; - for (const auto& box : def->hitboxes) { + for (const auto& box : def.hitboxes) { hitbox = glm::max(hitbox, box.size()); } offset = glm::vec3(1, 1, 0.0f); @@ -55,7 +55,7 @@ std::unique_ptr BlocksPreview::draw( -hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1), hitbox * scaledSize, texfaces, glm::vec4(1.0f), - !def->rt.emissive + !def.rt.emissive ); } batch->flush(); @@ -64,32 +64,32 @@ std::unique_ptr BlocksPreview::draw( { glm::vec3 pmul = glm::vec3(size * 0.63f); glm::vec3 hitbox = glm::vec3(); - for (const auto& box : def->modelBoxes) { + for (const auto& box : def.modelBoxes) { hitbox = glm::max(hitbox, box.size()); } offset.y += (1.0f - hitbox).y * 0.5f; shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset)); - for (size_t i = 0; i < def->modelBoxes.size(); i++) { + for (size_t i = 0; i < def.modelBoxes.size(); i++) { const UVRegion (&boxtexfaces)[6] = { - def->modelUVs[i * 6], - def->modelUVs[i * 6 + 1], - def->modelUVs[i * 6 + 2], - def->modelUVs[i * 6 + 3], - def->modelUVs[i * 6 + 4], - def->modelUVs[i * 6 + 5] + def.modelUVs[i * 6], + def.modelUVs[i * 6 + 1], + def.modelUVs[i * 6 + 2], + def.modelUVs[i * 6 + 3], + def.modelUVs[i * 6 + 4], + def.modelUVs[i * 6 + 5] }; batch->cube( - def->modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul, - def->modelBoxes[i].size() * pmul, - boxtexfaces, glm::vec4(1.0f), !def->rt.emissive + def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul, + def.modelBoxes[i].size() * pmul, + boxtexfaces, glm::vec4(1.0f), !def.rt.emissive ); } - auto& points = def->modelExtraPoints; + auto& points = def.modelExtraPoints; glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f); - for (size_t i = 0; i < def->modelExtraPoints.size() / 4; i++) { - const UVRegion& reg = def->modelUVs[def->modelBoxes.size() * 6 + i]; + for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) { + const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i]; batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0)); batch->point((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0)); @@ -154,9 +154,9 @@ std::unique_ptr BlocksPreview::build( fbo.bind(); for (size_t i = 0; i < count; i++) { - auto def = indices->blocks.get(i); //FIXME: Potentional null pointer + auto& def = indices->blocks.require(i); atlas->getTexture()->bind(); - builder.add(def->name, draw(cache, shader, &fbo, &batch, def, iconSize)); //-V522 + builder.add(def.name, draw(cache, shader, &fbo, &batch, def, iconSize)); } fbo.unbind(); diff --git a/src/graphics/render/BlocksPreview.hpp b/src/graphics/render/BlocksPreview.hpp index 138eec01..0431f7e7 100644 --- a/src/graphics/render/BlocksPreview.hpp +++ b/src/graphics/render/BlocksPreview.hpp @@ -22,7 +22,7 @@ class BlocksPreview { Shader* shader, Framebuffer* framebuffer, Batch3D* batch, - const Block* block, + const Block& block, int size ); public: diff --git a/src/graphics/render/Skybox.cpp b/src/graphics/render/Skybox.cpp index 7496b56b..a7aa3c46 100644 --- a/src/graphics/render/Skybox.cpp +++ b/src/graphics/render/Skybox.cpp @@ -142,11 +142,12 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality) ctx.setFramebuffer(fbo.get()); ctx.setViewport(Viewport(size, size)); - auto cubemap = dynamic_cast(fbo->getTexture()); //FIXME: Potentional null pointer + auto cubemap = dynamic_cast(fbo->getTexture()); + assert(cubemap != nullptr); ready = true; glActiveTexture(GL_TEXTURE1); - cubemap->bind(); //-V522 + cubemap->bind(); shader->use(); const glm::vec3 xaxs[] = { diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index a0644a3f..3b64bbb1 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -121,9 +121,9 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { itemid_t itemid = bound->getItemId(); if (itemid != prevItem) { if (itemid) { - auto def = content->getIndices()->items.get(itemid); //FIXME: Potentional null pointer + auto& def = content->getIndices()->items.require(itemid); tooltip = util::pascal_case( - langs::get(util::str2wstr_utf8(def->caption)) //-V522 + langs::get(util::str2wstr_utf8(def.caption)) ); } else { tooltip.clear(); @@ -159,7 +159,7 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { auto previews = assets->get("block-previews"); auto indices = content->getIndices(); - auto& item = indices->items.require(stack.getItemId()); //FIXME: Potentional null pointer + auto& item = indices->items.require(stack.getItemId()); switch (item.iconType) { case item_icon_type::none: break; @@ -268,14 +268,15 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) { stack.setCount(halfremain); } } else { - auto stackDef = content->getIndices()->items.get(stack.getItemId()); //FIXME: Potentional null pointer + auto& stackDef = + content->getIndices()->items.require(stack.getItemId()); if (stack.isEmpty()) { stack.set(grabbed); stack.setCount(1); - grabbed.setCount(grabbed.getCount()-1); - } else if (stack.accepts(grabbed) && stack.getCount() < stackDef->stackSize){ //-V522 - stack.setCount(stack.getCount()+1); - grabbed.setCount(grabbed.getCount()-1); + grabbed.setCount(grabbed.getCount() - 1); + } else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) { + stack.setCount(stack.getCount() + 1); + grabbed.setCount(grabbed.getCount() - 1); } } } diff --git a/src/items/ItemStack.cpp b/src/items/ItemStack.cpp index 2c9b74ae..eafbfea6 100644 --- a/src/items/ItemStack.cpp +++ b/src/items/ItemStack.cpp @@ -26,8 +26,8 @@ bool ItemStack::accepts(const ItemStack& other) const { } void ItemStack::move(ItemStack& item, const ContentIndices* indices) { - auto def = indices->items.get(item.getItemId()); //FIXME: Potentional null pointer - int count = std::min(item.count, def->stackSize - this->count); //-V522 + auto& def = indices->items.require(item.getItemId()); + int count = std::min(item.count, def.stackSize - this->count); if (isEmpty()) { set(ItemStack(item.getItemId(), count)); } else { diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index d72258fc..810a1b9c 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -153,8 +153,8 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) { auto inv = chunk->getBlockInventory(lx, y, lz); if (inv == nullptr) { auto indices = level->content->getIndices(); - auto def = indices->blocks.get(chunk->voxels[vox_index(lx, y, lz)].id); //FIXME: Potentional null pointer - int invsize = def->inventorySize; //-V522 + auto& def = indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id); + int invsize = def.inventorySize; if (invsize == 0) { return 0; } diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 71779ae7..c86e01dd 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -335,8 +335,8 @@ static int determine_rotation( static void pick_block( ContentIndices* indices, Chunks* chunks, Player* player, int x, int y, int z ) { - auto block = indices->blocks.get(chunks->get(x, y, z)->id); //FIXME: Potentional null pointer - itemid_t id = block->rt.pickingItem; //-V522 + auto& block = indices->blocks.require(chunks->get(x, y, z)->id); + itemid_t id = block.rt.pickingItem; auto inventory = player->getInventory(); size_t slotid = inventory->findSlotByItem(id, 0, 10); if (slotid == Inventory::npos) { diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 4e535b57..e9d12128 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -119,11 +119,11 @@ static int l_get_x(lua::State* L) { if (vox == nullptr) { return lua::pushivec3_stack(L, 1, 0, 0); } - auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer - if (!def->rotatable) { //-V522 + auto& def = level->content->getIndices()->blocks.require(vox->id); + if (!def.rotatable) { return lua::pushivec3_stack(L, 1, 0, 0); } else { - const CoordSystem& rot = def->rotations.variants[vox->state.rotation]; + const CoordSystem& rot = def.rotations.variants[vox->state.rotation]; return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z); } } @@ -136,11 +136,11 @@ static int l_get_y(lua::State* L) { if (vox == nullptr) { return lua::pushivec3_stack(L, 0, 1, 0); } - auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer - if (!def->rotatable) { //-V522 + auto& def = level->content->getIndices()->blocks.require(vox->id); + if (!def.rotatable) { return lua::pushivec3_stack(L, 0, 1, 0); } else { - const CoordSystem& rot = def->rotations.variants[vox->state.rotation]; + const CoordSystem& rot = def.rotations.variants[vox->state.rotation]; return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z); } } @@ -153,11 +153,11 @@ static int l_get_z(lua::State* L) { if (vox == nullptr) { return lua::pushivec3_stack(L, 0, 0, 1); } - auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer - if (!def->rotatable) { //-V522 + auto& def = level->content->getIndices()->blocks.require(vox->id); + if (!def.rotatable) { return lua::pushivec3_stack(L, 0, 0, 1); } else { - const CoordSystem& rot = def->rotations.variants[vox->state.rotation]; + const CoordSystem& rot = def.rotations.variants[vox->state.rotation]; return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z); } } diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index 8436dd0c..7b9872a8 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -53,34 +53,40 @@ static DocumentNode getDocumentNode(lua::State* L, int idx = 1) { static int l_menu_back(lua::State* L) { auto node = getDocumentNode(L); - auto menu = dynamic_cast(node.node.get()); //FIXME: Potentional null pointer - menu->back(); //-V522 + if (auto menu = dynamic_cast(node.node.get())) { + menu->back(); + } return 0; } static int l_menu_reset(lua::State* L) { auto node = getDocumentNode(L); - auto menu = dynamic_cast(node.node.get()); //FIXME: Potentional null pointer - menu->reset(); //-V522 + if (auto menu = dynamic_cast(node.node.get())) { + menu->reset(); + } return 0; } static int l_textbox_paste(lua::State* L) { auto node = getDocumentNode(L); - auto box = dynamic_cast(node.node.get()); //FIXME: Potentional null pointer - auto text = lua::require_string(L, 2); - box->paste(util::str2wstr_utf8(text)); //-V522 + if (auto box = dynamic_cast(node.node.get())) { + auto text = lua::require_string(L, 2); + box->paste(util::str2wstr_utf8(text)); + } return 0; } static int l_container_add(lua::State* L) { auto docnode = getDocumentNode(L); - auto node = dynamic_cast(docnode.node.get()); //FIXME: Potentional null pointer + auto node = dynamic_cast(docnode.node.get()); + if (node == nullptr) { + return 0; + } auto xmlsrc = lua::require_string(L, 2); try { auto subnode = guiutil::create(xmlsrc, docnode.document->getEnvironment()); - node->add(subnode); //-V522 + node->add(subnode); UINode::getIndices(subnode, docnode.document->getMapWriteable()); } catch (const std::exception& err) { throw std::runtime_error(err.what()); diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index b9500126..a421aa2e 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -49,11 +49,11 @@ static int l_hud_open_block(lua::State* L) { std::to_string(y) + " " + std::to_string(z) ); } - auto def = content->getIndices()->blocks.get(vox->id); + auto& def = content->getIndices()->blocks.require(vox->id); auto assets = engine->getAssets(); - auto layout = assets->get(def->uiLayout);//FIXME: Potentional null pointer //-V522 + auto layout = assets->get(def.uiLayout); if (layout == nullptr) { - throw std::runtime_error("block '" + def->name + "' has no ui layout"); + throw std::runtime_error("block '" + def.name + "' has no ui layout"); } auto id = blocks->createBlockInventory(x, y, z); @@ -65,7 +65,7 @@ static int l_hud_open_block(lua::State* L) { ); lua::pushinteger(L, id); - lua::pushstring(L, def->uiLayout); + lua::pushstring(L, def.uiLayout); return 2; }