From 2c0716f654f8d22dc2c73bdf460258e1a5cd3f50 Mon Sep 17 00:00:00 2001 From: Quintinon Date: Sat, 2 Jul 2022 19:49:42 -0700 Subject: [PATCH] [Cleanup] Cleanup code smells and compiler warnings in common/shareddb (#2270) * Apply refactoring for modernize-use-default-equals * Apply refactoring for const local variable * Replace c-style cast with static_cast<> * Explicit cast double to float to remove compiler warning * Make member functions const when possible * Correct [lnt-accidental-copy] by changing auto to auto& * Use static_cast instead of reinterpret_cast when appropriate * Remove unneeded initial values when assigned in all paths and joined delcaration and assignment when possible * Remove unused include * Make member function in header const --- common/shareddb.cpp | 456 +++++++++++++++++++++----------------------- common/shareddb.h | 19 +- 2 files changed, 232 insertions(+), 243 deletions(-) diff --git a/common/shareddb.cpp b/common/shareddb.cpp index e0360b5c3..27bfdbfb8 100644 --- a/common/shareddb.cpp +++ b/common/shareddb.cpp @@ -26,7 +26,6 @@ #include "classes.h" #include "eq_packet_structs.h" -#include "eqemu_exception.h" #include "faction.h" #include "features.h" #include "ipc_mutex.h" @@ -62,13 +61,12 @@ SharedDatabase::SharedDatabase(const char* host, const char* user, const char* p { } -SharedDatabase::~SharedDatabase() { -} +SharedDatabase::~SharedDatabase() = default; bool SharedDatabase::SetHideMe(uint32 account_id, uint8 hideme) { - std::string query = StringFormat("UPDATE account SET hideme = %i WHERE id = %i", hideme, account_id); - auto results = QueryDatabase(query); + const std::string query = StringFormat("UPDATE account SET hideme = %i WHERE id = %i", hideme, account_id); + const auto results = QueryDatabase(query); if (!results.Success()) { return false; } @@ -78,7 +76,7 @@ bool SharedDatabase::SetHideMe(uint32 account_id, uint8 hideme) uint8 SharedDatabase::GetGMSpeed(uint32 account_id) { - std::string query = StringFormat("SELECT gmspeed FROM account WHERE id = '%i'", account_id); + const std::string query = StringFormat("SELECT gmspeed FROM account WHERE id = '%i'", account_id); auto results = QueryDatabase(query); if (!results.Success()) { return 0; @@ -87,15 +85,15 @@ uint8 SharedDatabase::GetGMSpeed(uint32 account_id) if (results.RowCount() != 1) return 0; - auto row = results.begin(); + auto& row = results.begin(); return atoi(row[0]); } bool SharedDatabase::SetGMSpeed(uint32 account_id, uint8 gmspeed) { - std::string query = StringFormat("UPDATE account SET gmspeed = %i WHERE id = %i", gmspeed, account_id); - auto results = QueryDatabase(query); + const std::string query = StringFormat("UPDATE account SET gmspeed = %i WHERE id = %i", gmspeed, account_id); + const auto results = QueryDatabase(query); if (!results.Success()) { return false; } @@ -105,9 +103,9 @@ bool SharedDatabase::SetGMSpeed(uint32 account_id, uint8 gmspeed) uint32 SharedDatabase::GetTotalTimeEntitledOnAccount(uint32 AccountID) { uint32 EntitledTime = 0; - std::string query = StringFormat("SELECT `time_played` FROM `character_data` WHERE `account_id` = %u", AccountID); + const std::string query = StringFormat("SELECT `time_played` FROM `character_data` WHERE `account_id` = %u", AccountID); auto results = QueryDatabase(query); - for (auto row = results.begin(); row != results.end(); ++row) { + for (auto& row = results.begin(); row != results.end(); ++row) { EntitledTime += atoi(row[0]); } return EntitledTime; @@ -122,9 +120,9 @@ void SharedDatabase::SetMailKey(int CharID, int IPAddress, int MailKey) else sprintf(MailKeyString, "%08X", MailKey); - std::string query = StringFormat("UPDATE character_data SET mailkey = '%s' WHERE id = '%i'", - MailKeyString, CharID); - auto results = QueryDatabase(query); + const std::string query = StringFormat("UPDATE character_data SET mailkey = '%s' WHERE id = '%i'", + MailKeyString, CharID); + const auto results = QueryDatabase(query); if (!results.Success()) LogError("SharedDatabase::SetMailKey({}, {}) : {}", CharID, MailKeyString, results.ErrorMessage().c_str()); @@ -132,7 +130,7 @@ void SharedDatabase::SetMailKey(int CharID, int IPAddress, int MailKey) std::string SharedDatabase::GetMailKey(int CharID, bool key_only) { - std::string query = StringFormat("SELECT `mailkey` FROM `character_data` WHERE `id`='%i' LIMIT 1", CharID); + const std::string query = StringFormat("SELECT `mailkey` FROM `character_data` WHERE `id`='%i' LIMIT 1", CharID); auto results = QueryDatabase(query); if (!results.Success()) { @@ -147,7 +145,7 @@ std::string SharedDatabase::GetMailKey(int CharID, bool key_only) return std::string(); } - auto row = results.begin(); + auto& row = results.begin(); if (row != results.end()) { std::string mail_key = row[0]; @@ -169,22 +167,22 @@ std::string SharedDatabase::GetMailKey(int CharID, bool key_only) bool SharedDatabase::SaveCursor(uint32 char_id, std::list::const_iterator &start, std::list::const_iterator &end) { // Delete cursor items - std::string query = StringFormat("DELETE FROM inventory WHERE charid = %i " - "AND ((slotid >= 8000 AND slotid <= 8999) " - "OR slotid = %i OR (slotid >= %i AND slotid <= %i) )", - char_id, EQ::invslot::slotCursor, - EQ::invbag::CURSOR_BAG_BEGIN, EQ::invbag::CURSOR_BAG_END); - auto results = QueryDatabase(query); + const std::string query = StringFormat("DELETE FROM inventory WHERE charid = %i " + "AND ((slotid >= 8000 AND slotid <= 8999) " + "OR slotid = %i OR (slotid >= %i AND slotid <= %i) )", + char_id, EQ::invslot::slotCursor, + EQ::invbag::CURSOR_BAG_BEGIN, EQ::invbag::CURSOR_BAG_END); + const auto results = QueryDatabase(query); if (!results.Success()) { std::cout << "Clearing cursor failed: " << results.ErrorMessage() << std::endl; return false; } int i = 8000; - for(auto it = start; it != end; ++it, i++) { + for(auto& it = start; it != end; ++it, i++) { if (i > 8999) { break; } // shouldn't be anything in the queue that indexes this high - EQ::ItemInstance *inst = *it; - int16 use_slot = (i == 8000) ? EQ::invslot::slotCursor : i; + const EQ::ItemInstance *inst = *it; + const int16 use_slot = (i == 8000) ? EQ::invslot::slotCursor : i; if (!SaveInventory(char_id, inst, use_slot)) { return false; } @@ -196,9 +194,9 @@ bool SharedDatabase::SaveCursor(uint32 char_id, std::list::co bool SharedDatabase::VerifyInventory(uint32 account_id, int16 slot_id, const EQ::ItemInstance* inst) { // Delete cursor items - std::string query = StringFormat("SELECT itemid, charges FROM sharedbank " - "WHERE acctid = %d AND slotid = %d", - account_id, slot_id); + const std::string query = StringFormat("SELECT itemid, charges FROM sharedbank " + "WHERE acctid = %d AND slotid = %d", + account_id, slot_id); auto results = QueryDatabase(query); if (!results.Success()) { //returning true is less harmful in the face of a query error @@ -208,12 +206,12 @@ bool SharedDatabase::VerifyInventory(uint32 account_id, int16 slot_id, const EQ: if (results.RowCount() == 0) return false; - auto row = results.begin(); + auto& row = results.begin(); - uint32 id = atoi(row[0]); - uint16 charges = atoi(row[1]); + const uint32 id = atoi(row[0]); + const uint16 charges = atoi(row[1]); - uint16 expect_charges = 0; + uint16 expect_charges; if(inst->GetCharges() >= 0) expect_charges = inst->GetCharges(); @@ -264,30 +262,30 @@ bool SharedDatabase::UpdateInventorySlot(uint32 char_id, const EQ::ItemInstance* uint32 augslot[EQ::invaug::SOCKET_COUNT] = { 0, 0, 0, 0, 0, 0 }; if (inst->IsClassCommon()) { for (int i = EQ::invaug::SOCKET_BEGIN; i <= EQ::invaug::SOCKET_END; i++) { - EQ::ItemInstance *auginst = inst->GetItem(i); + const EQ::ItemInstance *auginst = inst->GetItem(i); augslot[i] = (auginst && auginst->GetItem()) ? auginst->GetItem()->ID : 0; } } - uint16 charges = 0; + uint16 charges; if(inst->GetCharges() >= 0) charges = inst->GetCharges(); else charges = 0x7FFF; // Update/Insert item - std::string query = StringFormat("REPLACE INTO inventory " - "(charid, slotid, itemid, charges, instnodrop, custom_data, color, " - "augslot1, augslot2, augslot3, augslot4, augslot5, augslot6, ornamenticon, ornamentidfile, ornament_hero_model) " - "VALUES( %lu, %lu, %lu, %lu, %lu, '%s', %lu, " - "%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu)", - (unsigned long)char_id, (unsigned long)slot_id, (unsigned long)inst->GetItem()->ID, - (unsigned long)charges, (unsigned long)(inst->IsAttuned()? 1: 0), - inst->GetCustomDataString().c_str(), (unsigned long)inst->GetColor(), - (unsigned long)augslot[0], (unsigned long)augslot[1], (unsigned long)augslot[2], - (unsigned long)augslot[3], (unsigned long)augslot[4], (unsigned long)augslot[5], (unsigned long)inst->GetOrnamentationIcon(), - (unsigned long)inst->GetOrnamentationIDFile(), (unsigned long)inst->GetOrnamentHeroModel()); - auto results = QueryDatabase(query); + const std::string query = StringFormat("REPLACE INTO inventory " + "(charid, slotid, itemid, charges, instnodrop, custom_data, color, " + "augslot1, augslot2, augslot3, augslot4, augslot5, augslot6, ornamenticon, ornamentidfile, ornament_hero_model) " + "VALUES( %lu, %lu, %lu, %lu, %lu, '%s', %lu, " + "%lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu, %lu)", + static_cast(char_id), static_cast(slot_id), static_cast(inst->GetItem()->ID), + static_cast(charges), static_cast(inst->IsAttuned() ? 1 : 0), + inst->GetCustomDataString().c_str(), static_cast(inst->GetColor()), + static_cast(augslot[0]), static_cast(augslot[1]), static_cast(augslot[2]), + static_cast(augslot[3]), static_cast(augslot[4]), static_cast(augslot[5]), static_cast(inst->GetOrnamentationIcon()), + static_cast(inst->GetOrnamentationIDFile()), static_cast(inst->GetOrnamentHeroModel())); + const auto results = QueryDatabase(query); // Save bag contents, if slot supports bag contents if (inst->IsClassBag() && EQ::InventoryProfile::SupportsContainers(slot_id)) @@ -311,29 +309,29 @@ bool SharedDatabase::UpdateSharedBankSlot(uint32 char_id, const EQ::ItemInstance uint32 augslot[EQ::invaug::SOCKET_COUNT] = { 0, 0, 0, 0, 0, 0 }; if (inst->IsClassCommon()) { for (int i = EQ::invaug::SOCKET_BEGIN; i <= EQ::invaug::SOCKET_END; i++) { - EQ::ItemInstance *auginst = inst->GetItem(i); + const EQ::ItemInstance *auginst = inst->GetItem(i); augslot[i] = (auginst && auginst->GetItem()) ? auginst->GetItem()->ID : 0; } } // Update/Insert item - uint32 account_id = GetAccountIDByChar(char_id); - uint16 charges = 0; + const uint32 account_id = GetAccountIDByChar(char_id); + uint16 charges; if(inst->GetCharges() >= 0) charges = inst->GetCharges(); else charges = 0x7FFF; - std::string query = StringFormat("REPLACE INTO sharedbank " - "(acctid, slotid, itemid, charges, custom_data, " - "augslot1, augslot2, augslot3, augslot4, augslot5, augslot6) " - "VALUES( %lu, %lu, %lu, %lu, '%s', " - "%lu, %lu, %lu, %lu, %lu, %lu)", - (unsigned long)account_id, (unsigned long)slot_id, (unsigned long)inst->GetItem()->ID, - (unsigned long)charges, inst->GetCustomDataString().c_str(), (unsigned long)augslot[0], - (unsigned long)augslot[1], (unsigned long)augslot[2], (unsigned long)augslot[3], (unsigned long)augslot[4], - (unsigned long)augslot[5]); - auto results = QueryDatabase(query); + const std::string query = StringFormat("REPLACE INTO sharedbank " + "(acctid, slotid, itemid, charges, custom_data, " + "augslot1, augslot2, augslot3, augslot4, augslot5, augslot6) " + "VALUES( %lu, %lu, %lu, %lu, '%s', " + "%lu, %lu, %lu, %lu, %lu, %lu)", + static_cast(account_id), static_cast(slot_id), static_cast(inst->GetItem()->ID), + static_cast(charges), inst->GetCustomDataString().c_str(), static_cast(augslot[0]), + static_cast(augslot[1]), static_cast(augslot[2]), static_cast(augslot[3]), static_cast(augslot[4]), + static_cast(augslot[5])); + const auto results = QueryDatabase(query); // Save bag contents, if slot supports bag contents if (inst->IsClassBag() && EQ::InventoryProfile::SupportsContainers(slot_id)) { @@ -365,7 +363,7 @@ bool SharedDatabase::DeleteInventorySlot(uint32 char_id, int16 slot_id) { if (!EQ::InventoryProfile::SupportsContainers(slot_id)) return true; - int16 base_slot_id = EQ::InventoryProfile::CalcSlotId(slot_id, EQ::invbag::SLOT_BEGIN); + const int16 base_slot_id = EQ::InventoryProfile::CalcSlotId(slot_id, EQ::invbag::SLOT_BEGIN); query = StringFormat("DELETE FROM inventory WHERE charid = %i AND slotid >= %i AND slotid < %i", char_id, base_slot_id, (base_slot_id+10)); results = QueryDatabase(query); @@ -380,7 +378,7 @@ bool SharedDatabase::DeleteInventorySlot(uint32 char_id, int16 slot_id) { bool SharedDatabase::DeleteSharedBankSlot(uint32 char_id, int16 slot_id) { // Delete item - uint32 account_id = GetAccountIDByChar(char_id); + const uint32 account_id = GetAccountIDByChar(char_id); std::string query = StringFormat("DELETE FROM sharedbank WHERE acctid=%i AND slotid=%i", account_id, slot_id); auto results = QueryDatabase(query); if (!results.Success()) { @@ -391,7 +389,7 @@ bool SharedDatabase::DeleteSharedBankSlot(uint32 char_id, int16 slot_id) { if (!EQ::InventoryProfile::SupportsContainers(slot_id)) return true; - int16 base_slot_id = EQ::InventoryProfile::CalcSlotId(slot_id, EQ::invbag::SLOT_BEGIN); + const int16 base_slot_id = EQ::InventoryProfile::CalcSlotId(slot_id, EQ::invbag::SLOT_BEGIN); query = StringFormat("DELETE FROM sharedbank WHERE acctid = %i " "AND slotid >= %i AND slotid < %i", account_id, base_slot_id, (base_slot_id+10)); @@ -407,7 +405,7 @@ bool SharedDatabase::DeleteSharedBankSlot(uint32 char_id, int16 slot_id) { int32 SharedDatabase::GetSharedPlatinum(uint32 account_id) { - std::string query = StringFormat("SELECT sharedplat FROM account WHERE id = '%i'", account_id); + const std::string query = StringFormat("SELECT sharedplat FROM account WHERE id = '%i'", account_id); auto results = QueryDatabase(query); if (!results.Success()) { return false; @@ -416,14 +414,14 @@ int32 SharedDatabase::GetSharedPlatinum(uint32 account_id) if (results.RowCount() != 1) return 0; - auto row = results.begin(); + auto& row = results.begin(); return atoi(row[0]); } bool SharedDatabase::SetSharedPlatinum(uint32 account_id, int32 amount_to_add) { - std::string query = StringFormat("UPDATE account SET sharedplat = sharedplat + %i WHERE id = %i", amount_to_add, account_id); - auto results = QueryDatabase(query); + const std::string query = StringFormat("UPDATE account SET sharedplat = sharedplat + %i WHERE id = %i", amount_to_add, account_id); + const auto results = QueryDatabase(query); if (!results.Success()) { return false; } @@ -435,7 +433,7 @@ bool SharedDatabase::SetStartingItems(PlayerProfile_Struct* pp, EQ::InventoryPro const EQ::ItemData *myitem; - std::string query = StringFormat( + const std::string query = StringFormat( "SELECT itemid, item_charges, slot FROM starting_items " "WHERE (race = %i or race = 0) AND (class = %i or class = 0) AND " "(deityid = %i or deityid = 0) AND (zoneid = %i or zoneid = 0) AND " @@ -454,16 +452,16 @@ bool SharedDatabase::SetStartingItems(PlayerProfile_Struct* pp, EQ::InventoryPro } - for (auto row = results.begin(); row != results.end(); ++row) { - int32 itemid = atoi(row[0]); - int32 charges = atoi(row[1]); + for (auto& row = results.begin(); row != results.end(); ++row) { + const int32 itemid = atoi(row[0]); + const int32 charges = atoi(row[1]); int32 slot = atoi(row[2]); myitem = GetItem(itemid); if(!myitem) continue; - EQ::ItemInstance* myinst = CreateBaseItem(myitem, charges); + const EQ::ItemInstance* myinst = CreateBaseItem(myitem, charges); if(slot < 0) slot = inv->FindFreeSlot(0, 0); @@ -499,18 +497,18 @@ bool SharedDatabase::GetSharedBank(uint32 id, EQ::InventoryProfile *inv, bool is return false; } - for (auto row = results.begin(); row != results.end(); ++row) { - int16 slot_id = (int16)atoi(row[0]); - uint32 item_id = (uint32)atoi(row[1]); - int16 charges = (int16)atoi(row[2]); + for (auto& row = results.begin(); row != results.end(); ++row) { + int16 slot_id = static_cast(atoi(row[0])); + uint32 item_id = static_cast(atoi(row[1])); + const int16 charges = static_cast(atoi(row[2])); uint32 aug[EQ::invaug::SOCKET_COUNT]; - aug[0] = (uint32)atoi(row[3]); - aug[1] = (uint32)atoi(row[4]); - aug[2] = (uint32)atoi(row[5]); - aug[3] = (uint32)atoi(row[6]); - aug[4] = (uint32)atoi(row[7]); - aug[5] = (uint32)atoi(row[8]); + aug[0] = static_cast(atoi(row[3])); + aug[1] = static_cast(atoi(row[4])); + aug[2] = static_cast(atoi(row[5])); + aug[3] = static_cast(atoi(row[6])); + aug[4] = static_cast(atoi(row[7])); + aug[5] = static_cast(atoi(row[8])); const EQ::ItemData *item = GetItem(item_id); @@ -520,8 +518,6 @@ bool SharedDatabase::GetSharedBank(uint32 id, EQ::InventoryProfile *inv, bool is continue; } - int16 put_slot_id = INVALID_INDEX; - EQ::ItemInstance *inst = CreateBaseItem(item, charges); if (inst && item->IsClassCommon()) { for (int i = EQ::invaug::SOCKET_BEGIN; i <= EQ::invaug::SOCKET_END; i++) { @@ -547,7 +543,7 @@ bool SharedDatabase::GetSharedBank(uint32 id, EQ::InventoryProfile *inv, bool is continue; } - char v = data_str[i]; + const char v = data_str[i]; if (use_id) idAsString.push_back(v); else @@ -556,7 +552,7 @@ bool SharedDatabase::GetSharedBank(uint32 id, EQ::InventoryProfile *inv, bool is } // theoretically inst can be nullptr ... this would be very bad ... - put_slot_id = inv->PutItem(slot_id, *inst); + const int16 put_slot_id = inv->PutItem(slot_id, *inst); safe_delete(inst); // Save ptr to item in inventory @@ -580,7 +576,7 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) return false; // Retrieve character inventory - std::string query = + const std::string query = StringFormat("SELECT slotid, itemid, charges, color, augslot1, augslot2, augslot3, augslot4, augslot5, " "augslot6, instnodrop, custom_data, ornamenticon, ornamentidfile, ornament_hero_model FROM " "inventory WHERE charid = %i ORDER BY slotid", @@ -593,24 +589,24 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) return false; } - auto timestamps = GetItemRecastTimestamps(char_id); + const auto timestamps = GetItemRecastTimestamps(char_id); auto cv_conflict = false; - auto pmask = inv->GetLookup()->PossessionsBitmask; - auto bank_size = inv->GetLookup()->InventoryTypeSize.Bank; + const auto pmask = inv->GetLookup()->PossessionsBitmask; + const auto bank_size = inv->GetLookup()->InventoryTypeSize.Bank; - for (auto row = results.begin(); row != results.end(); ++row) { + for (auto& row = results.begin(); row != results.end(); ++row) { int16 slot_id = atoi(row[0]); if (slot_id <= EQ::invslot::POSSESSIONS_END && slot_id >= EQ::invslot::POSSESSIONS_BEGIN) { // Titanium thru UF check - if ((((uint64)1 << slot_id) & pmask) == 0) { + if (((static_cast(1) << slot_id) & pmask) == 0) { cv_conflict = true; continue; } } else if (slot_id <= EQ::invbag::GENERAL_BAGS_END && slot_id >= EQ::invbag::GENERAL_BAGS_BEGIN) { // Titanium thru UF check - auto parent_slot = EQ::invslot::GENERAL_BEGIN + ((slot_id - EQ::invbag::GENERAL_BAGS_BEGIN) / EQ::invbag::SLOT_COUNT); - if ((((uint64)1 << parent_slot) & pmask) == 0) { + const auto parent_slot = EQ::invslot::GENERAL_BEGIN + ((slot_id - EQ::invbag::GENERAL_BAGS_BEGIN) / EQ::invbag::SLOT_COUNT); + if (((static_cast(1) << parent_slot) & pmask) == 0) { cv_conflict = true; continue; } @@ -622,7 +618,7 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) } } else if (slot_id <= EQ::invbag::BANK_BAGS_END && slot_id >= EQ::invbag::BANK_BAGS_BEGIN) { // Titanium check - auto parent_index = ((slot_id - EQ::invbag::BANK_BAGS_BEGIN) / EQ::invbag::SLOT_COUNT); + const auto parent_index = ((slot_id - EQ::invbag::BANK_BAGS_BEGIN) / EQ::invbag::SLOT_COUNT); if (parent_index < EQ::invslot::SLOT_BEGIN || parent_index >= bank_size) { cv_conflict = true; continue; @@ -630,8 +626,8 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) } uint32 item_id = atoi(row[1]); - uint16 charges = atoi(row[2]); - uint32 color = atoul(row[3]); + const uint16 charges = atoi(row[2]); + const uint32 color = atoul(row[3]); uint32 aug[EQ::invaug::SOCKET_COUNT]; @@ -642,10 +638,10 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) aug[4] = std::stoul(row[8]); aug[5] = std::stoul(row[9]); - bool instnodrop = (row[10] && (uint16)atoi(row[10])) ? true : false; + const bool instnodrop = (row[10] && static_cast(atoi(row[10]))) ? true : false; - uint32 ornament_icon = std::stoul(row[12]); - uint32 ornament_idfile = std::stoul(row[13]); + const uint32 ornament_icon = std::stoul(row[12]); + const uint32 ornament_idfile = std::stoul(row[13]); uint32 ornament_hero_model = std::stoul(row[14]); const EQ::ItemData *item = GetItem(item_id); @@ -656,8 +652,6 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) continue; } - int16 put_slot_id = INVALID_INDEX; - EQ::ItemInstance *inst = CreateBaseItem(item, charges); if (inst == nullptr) @@ -681,7 +675,7 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) continue; } - char v = data_str[i]; + const char v = data_str[i]; if (use_id) idAsString.push_back(v); else @@ -720,6 +714,7 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) } } + int16 put_slot_id; if (slot_id >= 8000 && slot_id <= 8999) { put_slot_id = inv->PushCursor(*inst); } else if (slot_id >= 3111 && slot_id <= 3179) { @@ -759,7 +754,7 @@ bool SharedDatabase::GetInventory(uint32 char_id, EQ::InventoryProfile *inv) bool SharedDatabase::GetInventory(uint32 account_id, char *name, EQ::InventoryProfile *inv) // deprecated { // Retrieve character inventory - std::string query = + const std::string query = StringFormat("SELECT slotid, itemid, charges, color, augslot1, " "augslot2, augslot3, augslot4, augslot5, augslot6, instnodrop, custom_data, ornamenticon, " "ornamentidfile, ornament_hero_model " @@ -774,27 +769,26 @@ bool SharedDatabase::GetInventory(uint32 account_id, char *name, EQ::InventoryPr return false; } - for (auto row = results.begin(); row != results.end(); ++row) { + for (auto& row = results.begin(); row != results.end(); ++row) { int16 slot_id = atoi(row[0]); uint32 item_id = atoi(row[1]); - int8 charges = atoi(row[2]); - uint32 color = atoul(row[3]); + const int8 charges = atoi(row[2]); + const uint32 color = atoul(row[3]); uint32 aug[EQ::invaug::SOCKET_COUNT]; - aug[0] = (uint32)atoi(row[4]); - aug[1] = (uint32)atoi(row[5]); - aug[2] = (uint32)atoi(row[6]); - aug[3] = (uint32)atoi(row[7]); - aug[4] = (uint32)atoi(row[8]); - aug[5] = (uint32)atoi(row[9]); + aug[0] = static_cast(atoi(row[4])); + aug[1] = static_cast(atoi(row[5])); + aug[2] = static_cast(atoi(row[6])); + aug[3] = static_cast(atoi(row[7])); + aug[4] = static_cast(atoi(row[8])); + aug[5] = static_cast(atoi(row[9])); - bool instnodrop = (row[10] && (uint16)atoi(row[10])) ? true : false; - uint32 ornament_icon = std::stoul(row[12]); - uint32 ornament_idfile = std::stoul(row[13]); + const bool instnodrop = (row[10] && static_cast(atoi(row[10]))) ? true : false; + const uint32 ornament_icon = std::stoul(row[12]); + const uint32 ornament_idfile = std::stoul(row[13]); uint32 ornament_hero_model = std::stoul(row[14]); const EQ::ItemData *item = GetItem(item_id); - int16 put_slot_id = INVALID_INDEX; if (!item) continue; @@ -823,7 +817,7 @@ bool SharedDatabase::GetInventory(uint32 account_id, char *name, EQ::InventoryPr continue; } - char v = data_str[i]; + const char v = data_str[i]; if (use_id) idAsString.push_back(v); else @@ -847,6 +841,7 @@ bool SharedDatabase::GetInventory(uint32 account_id, char *name, EQ::InventoryPr } } + int16 put_slot_id; if (slot_id >= 8000 && slot_id <= 8999) put_slot_id = inv->PushCursor(*inst); else @@ -867,32 +862,32 @@ bool SharedDatabase::GetInventory(uint32 account_id, char *name, EQ::InventoryPr std::map SharedDatabase::GetItemRecastTimestamps(uint32 char_id) { std::map timers; - std::string query = StringFormat("SELECT recast_type,timestamp FROM character_item_recast WHERE id=%u", char_id); + const std::string query = StringFormat("SELECT recast_type,timestamp FROM character_item_recast WHERE id=%u", char_id); auto results = QueryDatabase(query); if (!results.Success() || results.RowCount() == 0) return timers; - for (auto row = results.begin(); row != results.end(); ++row) + for (auto& row = results.begin(); row != results.end(); ++row) timers[atoul(row[0])] = atoul(row[1]); return timers; // RVO or move assigned } uint32 SharedDatabase::GetItemRecastTimestamp(uint32 char_id, uint32 recast_type) { - std::string query = StringFormat("SELECT timestamp FROM character_item_recast WHERE id=%u AND recast_type=%u", - char_id, recast_type); + const std::string query = StringFormat("SELECT timestamp FROM character_item_recast WHERE id=%u AND recast_type=%u", + char_id, recast_type); auto results = QueryDatabase(query); if (!results.Success() || results.RowCount() == 0) return 0; - auto row = results.begin(); + auto& row = results.begin(); return static_cast(atoul(row[0])); } void SharedDatabase::ClearOldRecastTimestamps(uint32 char_id) { // This actually isn't strictly live-like. Live your recast timestamps are forever - std::string query = + const std::string query = StringFormat("DELETE FROM character_item_recast WHERE id = %u and timestamp < UNIX_TIMESTAMP()", char_id); QueryDatabase(query); } @@ -911,7 +906,7 @@ void SharedDatabase::GetItemsCount(int32 &item_count, uint32 &max_id) if (results.RowCount() == 0) return; - auto row = results.begin(); + auto& row = results.begin(); if (row[0]) max_id = atoi(row[0]); @@ -924,13 +919,13 @@ bool SharedDatabase::LoadItems(const std::string &prefix) { items_mmf.reset(nullptr); try { - auto Config = EQEmuConfig::get(); + const auto Config = EQEmuConfig::get(); EQ::IPCMutex mutex("items"); mutex.Lock(); std::string file_name = Config->SharedMemDir + prefix + std::string("items"); LogInfo("[Shared Memory] Attempting to load file [{}]", file_name); items_mmf = std::make_unique(file_name); - items_hash = std::make_unique>(reinterpret_cast(items_mmf->Get()), items_mmf->Size()); + items_hash = std::make_unique>(static_cast(items_mmf->Get()), items_mmf->Size()); mutex.Unlock(); } catch(std::exception& ex) { LogError("Error Loading Items: {}", ex.what()); @@ -942,7 +937,7 @@ bool SharedDatabase::LoadItems(const std::string &prefix) { void SharedDatabase::LoadItems(void *data, uint32 size, int32 items, uint32 max_item_id) { - EQ::FixedMemoryHashSet hash(reinterpret_cast(data), size, items, max_item_id); + EQ::FixedMemoryHashSet hash(static_cast(data), size, items, max_item_id); std::string variable_buffer; @@ -994,7 +989,7 @@ void SharedDatabase::LoadItems(void *data, uint32 size, int32 items, uint32 max_ return; } - for (auto row = results.begin(); row != results.end(); ++row) { + for (auto& row = results.begin(); row != results.end(); ++row) { memset(&item, 0, sizeof(EQ::ItemData)); // Unique Identifier @@ -1270,7 +1265,7 @@ void SharedDatabase::LoadItems(void *data, uint32 size, int32 items, uint32 max_ } } -const EQ::ItemData *SharedDatabase::GetItem(uint32 id) +const EQ::ItemData *SharedDatabase::GetItem(uint32 id) const { if (id == 0) { return nullptr; @@ -1287,7 +1282,8 @@ const EQ::ItemData *SharedDatabase::GetItem(uint32 id) return nullptr; } -const EQ::ItemData* SharedDatabase::IterateItems(uint32* id) { +const EQ::ItemData* SharedDatabase::IterateItems(uint32* id) const +{ if(!items_hash || !id) { return nullptr; } @@ -1313,7 +1309,7 @@ std::string SharedDatabase::GetBook(const char *txtfile, int16 *language) std::string txtout; strcpy(txtfile2, txtfile); - std::string query = StringFormat("SELECT txtfile, language FROM books WHERE name = '%s'", txtfile2); + const std::string query = StringFormat("SELECT txtfile, language FROM books WHERE name = '%s'", txtfile2); auto results = QueryDatabase(query); if (!results.Success()) { txtout.assign(" ",1); @@ -1326,7 +1322,7 @@ std::string SharedDatabase::GetBook(const char *txtfile, int16 *language) return txtout; } - auto row = results.begin(); + auto& row = results.begin(); txtout.assign(row[0],strlen(row[0])); *language = static_cast(atoi(row[1])); @@ -1346,13 +1342,14 @@ void SharedDatabase::GetFactionListInfo(uint32 &list_count, uint32 &max_lists) { if (results.RowCount() == 0) return; - auto row = results.begin(); + auto& row = results.begin(); list_count = static_cast(atoul(row[0])); max_lists = static_cast(atoul(row[1] ? row[1] : "0")); } -const NPCFactionList* SharedDatabase::GetNPCFactionEntry(uint32 id) { +const NPCFactionList* SharedDatabase::GetNPCFactionEntry(uint32 id) const +{ if(!faction_hash) { return nullptr; } @@ -1365,7 +1362,7 @@ const NPCFactionList* SharedDatabase::GetNPCFactionEntry(uint32 id) { } void SharedDatabase::LoadNPCFactionLists(void *data, uint32 size, uint32 list_count, uint32 max_lists) { - EQ::FixedMemoryHashSet hash(reinterpret_cast(data), size, list_count, max_lists); + EQ::FixedMemoryHashSet hash(static_cast(data), size, list_count, max_lists); NPCFactionList faction; const std::string query = "SELECT npc_faction.id, npc_faction.primaryfaction, npc_faction.ignore_primary_assist, " @@ -1380,8 +1377,8 @@ void SharedDatabase::LoadNPCFactionLists(void *data, uint32 size, uint32 list_co uint32 current_id = 0; uint32 current_entry = 0; - for(auto row = results.begin(); row != results.end(); ++row) { - uint32 id = static_cast(atoul(row[0])); + for(auto& row = results.begin(); row != results.end(); ++row) { + const uint32 id = static_cast(atoul(row[0])); if(id != current_id) { if(current_id != 0) { hash.insert(current_id, faction); @@ -1418,13 +1415,13 @@ bool SharedDatabase::LoadNPCFactionLists(const std::string &prefix) { faction_hash.reset(nullptr); try { - auto Config = EQEmuConfig::get(); + const auto Config = EQEmuConfig::get(); EQ::IPCMutex mutex("faction"); mutex.Lock(); std::string file_name = Config->SharedMemDir + prefix + std::string("faction"); LogInfo("[Shared Memory] Attempting to load file [{}]", file_name); faction_mmf = std::make_unique(file_name); - faction_hash = std::make_unique>(reinterpret_cast(faction_mmf->Get()), faction_mmf->Size()); + faction_hash = std::make_unique>(static_cast(faction_mmf->Get()), faction_mmf->Size()); mutex.Unlock(); } catch(std::exception& ex) { LogError("Error Loading npc factions: {}", ex.what()); @@ -1437,10 +1434,9 @@ bool SharedDatabase::LoadNPCFactionLists(const std::string &prefix) { // Create appropriate EQ::ItemInstance class EQ::ItemInstance* SharedDatabase::CreateItem(uint32 item_id, int16 charges, uint32 aug1, uint32 aug2, uint32 aug3, uint32 aug4, uint32 aug5, uint32 aug6, uint8 attuned) { - const EQ::ItemData* item = nullptr; EQ::ItemInstance* inst = nullptr; - item = GetItem(item_id); + const EQ::ItemData* item = GetItem(item_id); if (item) { inst = CreateBaseItem(item, charges); @@ -1516,21 +1512,21 @@ EQ::ItemInstance* SharedDatabase::CreateBaseItem(const EQ::ItemData* item, int16 int32 SharedDatabase::DeleteStalePlayerCorpses() { if(RuleB(Zone, EnableShadowrest)) { - std::string query = StringFormat( + const std::string query = StringFormat( "UPDATE `character_corpses` SET `is_buried` = 1 WHERE `is_buried` = 0 AND " "(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_of_death)) > %d AND NOT time_of_death = 0", (RuleI(Character, CorpseDecayTimeMS) / 1000)); - auto results = QueryDatabase(query); + const auto results = QueryDatabase(query); if (!results.Success()) return -1; return results.RowsAffected(); } - std::string query = StringFormat( + const std::string query = StringFormat( "DELETE FROM `character_corpses` WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_of_death)) > %d " "AND NOT time_of_death = 0", (RuleI(Character, CorpseDecayTimeMS) / 1000)); - auto results = QueryDatabase(query); + const auto results = QueryDatabase(query); if (!results.Success()) return -1; @@ -1541,12 +1537,12 @@ bool SharedDatabase::GetCommandSettings(std::map> &injected) { if (injected.size()) { - - std::string query = fmt::format( + const std::string query = fmt::format( "REPLACE INTO `command_settings`(`command`, `access`) VALUES {}", implode( ",", @@ -1591,8 +1586,7 @@ bool SharedDatabase::UpdateInjectedCommandSettings(const std::vector &orphaned) { if (orphaned.size()) { - - std::string query = fmt::format( + const std::string query = fmt::format( "DELETE FROM `command_settings` WHERE `command` IN ({})", implode(",", std::pair('\'', '\''), orphaned) ); @@ -1614,13 +1608,8 @@ bool SharedDatabase::UpdateOrphanedCommandSettings(const std::vectorSharedMemDir + prefix + std::string("skill_caps"); @@ -1636,10 +1625,10 @@ bool SharedDatabase::LoadSkillCaps(const std::string &prefix) { } void SharedDatabase::LoadSkillCaps(void *data) { - uint32 class_count = PLAYER_CLASS_COUNT; - uint32 skill_count = EQ::skills::HIGHEST_SKILL + 1; - uint32 level_count = HARD_LEVEL_CAP + 1; - uint16 *skill_caps_table = reinterpret_cast(data); + const uint32 class_count = PLAYER_CLASS_COUNT; + const uint32 skill_count = EQ::skills::HIGHEST_SKILL + 1; + const uint32 level_count = HARD_LEVEL_CAP + 1; + uint16 *skill_caps_table = static_cast(data); const std::string query = "SELECT skillID, class, level, cap FROM skill_caps ORDER BY skillID, class, level"; auto results = QueryDatabase(query); @@ -1648,21 +1637,22 @@ void SharedDatabase::LoadSkillCaps(void *data) { return; } - for(auto row = results.begin(); row != results.end(); ++row) { - uint8 skillID = atoi(row[0]); - uint8 class_ = atoi(row[1]) - 1; - uint8 level = atoi(row[2]); - uint16 cap = atoi(row[3]); + for(auto& row = results.begin(); row != results.end(); ++row) { + const uint8 skillID = atoi(row[0]); + const uint8 class_ = atoi(row[1]) - 1; + const uint8 level = atoi(row[2]); + const uint16 cap = atoi(row[3]); if(skillID >= skill_count || class_ >= class_count || level >= level_count) continue; - uint32 index = (((class_ * skill_count) + skillID) * level_count) + level; + const uint32 index = (((class_ * skill_count) + skillID) * level_count) + level; skill_caps_table[index] = cap; } } -uint16 SharedDatabase::GetSkillCap(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level) { +uint16 SharedDatabase::GetSkillCap(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level) const +{ if(!skill_caps_mmf) { return 0; } @@ -1675,9 +1665,9 @@ uint16 SharedDatabase::GetSkillCap(uint8 Class_, EQ::skills::SkillType Skill, ui SkillMaxLevel = RuleI(Character, MaxLevel); } - uint32 class_count = PLAYER_CLASS_COUNT; - uint32 skill_count = EQ::skills::HIGHEST_SKILL + 1; - uint32 level_count = HARD_LEVEL_CAP + 1; + const uint32 class_count = PLAYER_CLASS_COUNT; + const uint32 skill_count = EQ::skills::HIGHEST_SKILL + 1; + const uint32 level_count = HARD_LEVEL_CAP + 1; if(Class_ > class_count || static_cast(Skill) > skill_count || Level > level_count) { return 0; } @@ -1686,12 +1676,13 @@ uint16 SharedDatabase::GetSkillCap(uint8 Class_, EQ::skills::SkillType Skill, ui Level = static_cast(SkillMaxLevel); } - uint32 index = ((((Class_ - 1) * skill_count) + Skill) * level_count) + Level; - uint16 *skill_caps_table = reinterpret_cast(skill_caps_mmf->Get()); + const uint32 index = ((((Class_ - 1) * skill_count) + Skill) * level_count) + Level; + const uint16 *skill_caps_table = static_cast(skill_caps_mmf->Get()); return skill_caps_table[index]; } -uint8 SharedDatabase::GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level) { +uint8 SharedDatabase::GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level) const +{ if(!skill_caps_mmf) { return 0; } @@ -1704,17 +1695,17 @@ uint8 SharedDatabase::GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, u SkillMaxLevel = RuleI(Character, MaxLevel); } - uint32 class_count = PLAYER_CLASS_COUNT; - uint32 skill_count = EQ::skills::HIGHEST_SKILL + 1; - uint32 level_count = HARD_LEVEL_CAP + 1; + const uint32 class_count = PLAYER_CLASS_COUNT; + const uint32 skill_count = EQ::skills::HIGHEST_SKILL + 1; + const uint32 level_count = HARD_LEVEL_CAP + 1; if(Class_ > class_count || static_cast(Skill) > skill_count || Level > level_count) { return 0; } uint8 ret = 0; if(Level > static_cast(SkillMaxLevel)) { - uint32 index = ((((Class_ - 1) * skill_count) + Skill) * level_count); - uint16 *skill_caps_table = reinterpret_cast(skill_caps_mmf->Get()); + const uint32 index = ((((Class_ - 1) * skill_count) + Skill) * level_count); + const uint16 *skill_caps_table = static_cast(skill_caps_mmf->Get()); for(uint8 x = 0; x < Level; x++){ if(skill_caps_table[index + x]){ ret = x; @@ -1724,8 +1715,8 @@ uint8 SharedDatabase::GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, u } else { - uint32 index = ((((Class_ - 1) * skill_count) + Skill) * level_count); - uint16 *skill_caps_table = reinterpret_cast(skill_caps_mmf->Get()); + const uint32 index = ((((Class_ - 1) * skill_count) + Skill) * level_count); + const uint16 *skill_caps_table = static_cast(skill_caps_mmf->Get()); for(int x = 0; x < SkillMaxLevel; x++){ if(skill_caps_table[index + x]){ ret = x; @@ -1741,16 +1732,15 @@ uint8 SharedDatabase::GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, u } void SharedDatabase::LoadDamageShieldTypes(SPDat_Spell_Struct* sp, int32 iMaxSpellID) { - - std::string query = StringFormat("SELECT `spellid`, `type` FROM `damageshieldtypes` WHERE `spellid` > 0 " - "AND `spellid` <= %i", iMaxSpellID); + const std::string query = StringFormat("SELECT `spellid`, `type` FROM `damageshieldtypes` WHERE `spellid` > 0 " + "AND `spellid` <= %i", iMaxSpellID); auto results = QueryDatabase(query); if (!results.Success()) { return; } - for(auto row = results.begin(); row != results.end(); ++row) { - int spellID = atoi(row[0]); + for(auto& row = results.begin(); row != results.end(); ++row) { + const int spellID = atoi(row[0]); if((spellID > 0) && (spellID <= iMaxSpellID)) sp[spellID].damage_shield_type = atoi(row[1]); } @@ -1762,13 +1752,13 @@ const EvolveInfo* SharedDatabase::GetEvolveInfo(uint32 loregroup) { } int SharedDatabase::GetMaxSpellID() { - std::string query = "SELECT MAX(id) FROM spells_new"; + const std::string query = "SELECT MAX(id) FROM spells_new"; auto results = QueryDatabase(query); if (!results.Success()) { return -1; } - auto row = results.begin(); + auto& row = results.begin(); return atoi(row[0]); } @@ -1777,15 +1767,15 @@ bool SharedDatabase::LoadSpells(const std::string &prefix, int32 *records, const spells_mmf.reset(nullptr); try { - auto Config = EQEmuConfig::get(); + const auto Config = EQEmuConfig::get(); EQ::IPCMutex mutex("spells"); mutex.Lock(); std::string file_name = Config->SharedMemDir + prefix + std::string("spells"); spells_mmf = std::make_unique(file_name); LogInfo("[Shared Memory] Attempting to load file [{}]", file_name); - *records = *reinterpret_cast(spells_mmf->Get()); - *sp = reinterpret_cast((char*)spells_mmf->Get() + 4); + *records = *static_cast(spells_mmf->Get()); + *sp = reinterpret_cast(static_cast(spells_mmf->Get()) + 4); mutex.Unlock(); } catch(std::exception& ex) { @@ -1796,8 +1786,8 @@ bool SharedDatabase::LoadSpells(const std::string &prefix, int32 *records, const } void SharedDatabase::LoadSpells(void *data, int max_spells) { - *(uint32*)data = max_spells; - SPDat_Spell_Struct *sp = reinterpret_cast((char*)data + sizeof(uint32)); + *static_cast(data) = max_spells; + SPDat_Spell_Struct *sp = reinterpret_cast(static_cast(data) + sizeof(uint32)); const std::string query = "SELECT * FROM spells_new ORDER BY id ASC"; auto results = QueryDatabase(query); @@ -1810,11 +1800,10 @@ void SharedDatabase::LoadSpells(void *data, int max_spells) { return; } - int tempid = 0; - int counter = 0; + int counter = 0; - for (auto row = results.begin(); row != results.end(); ++row) { - tempid = atoi(row[0]); + for (auto& row = results.begin(); row != results.end(); ++row) { + const int tempid = atoi(row[0]); if(tempid >= max_spells) { LogSpells("Non fatal error: spell.id >= max_spells, ignoring"); continue; @@ -1872,15 +1861,15 @@ void SharedDatabase::LoadSpells(void *data, int max_spells) { for(y=0; y< EFFECT_COUNT;y++) sp[tempid].effect_id[y]=atoi(row[86+y]); - sp[tempid].target_type = (SpellTargetType) atoi(row[98]); + sp[tempid].target_type = static_cast(atoi(row[98])); sp[tempid].base_difficulty=atoi(row[99]); - int tmp_skill = atoi(row[100]);; + int tmp_skill = atoi(row[100]); if (tmp_skill < 0 || tmp_skill > EQ::skills::HIGHEST_SKILL) sp[tempid].skill = EQ::skills::SkillBegging; /* not much better we can do. */ // can probably be changed to client-based 'SkillNone' once activated else - sp[tempid].skill = (EQ::skills::SkillType) tmp_skill; + sp[tempid].skill = static_cast(tmp_skill); sp[tempid].zone_type=atoi(row[101]); sp[tempid].environment_type=atoi(row[102]); @@ -1960,10 +1949,10 @@ void SharedDatabase::LoadSpells(void *data, int max_spells) { sp[tempid].spell_class = atoi(row[221]); sp[tempid].spell_subclass = atoi(row[222]); sp[tempid].persist_death = atoi(row[224]) != 0; - sp[tempid].min_distance = atof(row[227]); - sp[tempid].min_distance_mod = atof(row[228]); - sp[tempid].max_distance = atof(row[229]); - sp[tempid].max_distance_mod = atof(row[230]); + sp[tempid].min_distance = static_cast(atof(row[227])); + sp[tempid].min_distance_mod = static_cast(atof(row[228])); + sp[tempid].max_distance = static_cast(atof(row[229])); + sp[tempid].max_distance_mod = static_cast(atof(row[230])); sp[tempid].min_range = static_cast(atoi(row[231])); sp[tempid].no_remove = atoi(row[232]) != 0; sp[tempid].damage_shield_type = 0; @@ -1982,7 +1971,7 @@ int SharedDatabase::GetMaxBaseDataLevel() { if (results.RowCount() == 0) return -1; - auto row = results.begin(); + auto& row = results.begin(); return atoi(row[0]); } @@ -1991,7 +1980,7 @@ bool SharedDatabase::LoadBaseData(const std::string &prefix) { base_data_mmf.reset(nullptr); try { - auto Config = EQEmuConfig::get(); + const auto Config = EQEmuConfig::get(); EQ::IPCMutex mutex("base_data"); mutex.Lock(); @@ -2007,7 +1996,7 @@ bool SharedDatabase::LoadBaseData(const std::string &prefix) { } void SharedDatabase::LoadBaseData(void *data, int max_level) { - char *base_ptr = reinterpret_cast(data); + char *base_ptr = static_cast(data); const std::string query = "SELECT * FROM base_data ORDER BY level, class ASC"; auto results = QueryDatabase(query); @@ -2015,12 +2004,9 @@ void SharedDatabase::LoadBaseData(void *data, int max_level) { return; } - int lvl = 0; - int cl = 0; - - for (auto row = results.begin(); row != results.end(); ++row) { - lvl = atoi(row[0]); - cl = atoi(row[1]); + for (auto& row = results.begin(); row != results.end(); ++row) { + const int lvl = atoi(row[0]); + const int cl = atoi(row[1]); if(lvl <= 0) { LogError("Non fatal error: base_data.level <= 0, ignoring."); @@ -2054,7 +2040,8 @@ void SharedDatabase::LoadBaseData(void *data, int max_level) { } } -const BaseDataStruct* SharedDatabase::GetBaseData(int lvl, int cl) { +const BaseDataStruct* SharedDatabase::GetBaseData(int lvl, int cl) const +{ if(!base_data_mmf) { return nullptr; } @@ -2071,15 +2058,15 @@ const BaseDataStruct* SharedDatabase::GetBaseData(int lvl, int cl) { return nullptr; } - char *base_ptr = reinterpret_cast(base_data_mmf->Get()); + char *base_ptr = static_cast(base_data_mmf->Get()); - uint32 offset = ((16 * (lvl - 1)) + (cl - 1)) * sizeof(BaseDataStruct); + const uint32 offset = ((16 * (lvl - 1)) + (cl - 1)) * sizeof(BaseDataStruct); if(offset >= base_data_mmf->Size()) { return nullptr; } - BaseDataStruct *bd = reinterpret_cast(base_ptr + offset); + const BaseDataStruct *bd = reinterpret_cast(base_ptr + offset); return bd; } @@ -2100,7 +2087,7 @@ void SharedDatabase::GetLootTableInfo(uint32 &loot_table_count, uint32 &max_loot if (results.RowCount() == 0) return; - auto row = results.begin(); + auto& row = results.begin(); loot_table_count = static_cast(atoul(row[0])); max_loot_table = static_cast(atoul(row[1] ? row[1] : "0")); @@ -2125,7 +2112,7 @@ void SharedDatabase::GetLootDropInfo(uint32 &loot_drop_count, uint32 &max_loot_d if (results.RowCount() == 0) return; - auto row =results.begin(); + auto& row =results.begin(); loot_drop_count = static_cast(atoul(row[0])); max_loot_drop = static_cast(atoul(row[1] ? row[1] : "0")); @@ -2133,7 +2120,7 @@ void SharedDatabase::GetLootDropInfo(uint32 &loot_drop_count, uint32 &max_loot_d } void SharedDatabase::LoadLootTables(void *data, uint32 size) { - EQ::FixedMemoryVariableHashSet hash(reinterpret_cast(data), size); + EQ::FixedMemoryVariableHashSet hash(static_cast(data), size); uint8 loot_table[sizeof(LootTable_Struct) + (sizeof(LootTableEntries_Struct) * 128)]; LootTable_Struct *lt = reinterpret_cast(loot_table); @@ -2172,8 +2159,8 @@ void SharedDatabase::LoadLootTables(void *data, uint32 size) { uint32 current_id = 0; uint32 current_entry = 0; - for (auto row = results.begin(); row != results.end(); ++row) { - uint32 id = static_cast(atoul(row[0])); + for (auto& row = results.begin(); row != results.end(); ++row) { + const uint32 id = static_cast(atoul(row[0])); if (id != current_id) { if (current_id != 0) { hash.insert( @@ -2226,7 +2213,7 @@ void SharedDatabase::LoadLootTables(void *data, uint32 size) { void SharedDatabase::LoadLootDrops(void *data, uint32 size) { - EQ::FixedMemoryVariableHashSet hash(reinterpret_cast(data), size); + EQ::FixedMemoryVariableHashSet hash(static_cast(data), size); uint8 loot_drop[sizeof(LootDrop_Struct) + (sizeof(LootDropEntries_Struct) * 1260)]; LootDrop_Struct *p_loot_drop_struct = reinterpret_cast(loot_drop); @@ -2266,8 +2253,8 @@ void SharedDatabase::LoadLootDrops(void *data, uint32 size) { uint32 current_id = 0; uint32 current_entry = 0; - for (auto row = results.begin(); row != results.end(); ++row) { - auto id = static_cast(atoul(row[0])); + for (auto& row = results.begin(); row != results.end(); ++row) { + const auto id = static_cast(atoul(row[0])); if (id != current_id) { if (current_id != 0) { hash.insert( @@ -2315,18 +2302,18 @@ bool SharedDatabase::LoadLoot(const std::string &prefix) { loot_drop_mmf.reset(nullptr); try { - auto Config = EQEmuConfig::get(); + const auto Config = EQEmuConfig::get(); EQ::IPCMutex mutex("loot"); mutex.Lock(); std::string file_name_lt = Config->SharedMemDir + prefix + std::string("loot_table"); loot_table_mmf = std::make_unique(file_name_lt); loot_table_hash = std::make_unique>( - reinterpret_cast(loot_table_mmf->Get()), + static_cast(loot_table_mmf->Get()), loot_table_mmf->Size()); std::string file_name_ld = Config->SharedMemDir + prefix + std::string("loot_drop"); loot_drop_mmf = std::make_unique(file_name_ld); loot_drop_hash = std::make_unique>( - reinterpret_cast(loot_drop_mmf->Get()), + static_cast(loot_drop_mmf->Get()), loot_drop_mmf->Size()); mutex.Unlock(); } catch(std::exception &ex) { @@ -2337,7 +2324,8 @@ bool SharedDatabase::LoadLoot(const std::string &prefix) { return true; } -const LootTable_Struct* SharedDatabase::GetLootTable(uint32 loottable_id) { +const LootTable_Struct* SharedDatabase::GetLootTable(uint32 loottable_id) const +{ if(!loot_table_hash) return nullptr; @@ -2351,7 +2339,8 @@ const LootTable_Struct* SharedDatabase::GetLootTable(uint32 loottable_id) { return nullptr; } -const LootDrop_Struct* SharedDatabase::GetLootDrop(uint32 lootdrop_id) { +const LootDrop_Struct* SharedDatabase::GetLootDrop(uint32 lootdrop_id) const +{ if(!loot_drop_hash) return nullptr; @@ -2366,16 +2355,15 @@ const LootDrop_Struct* SharedDatabase::GetLootDrop(uint32 lootdrop_id) { } void SharedDatabase::LoadCharacterInspectMessage(uint32 character_id, InspectMessage_Struct* message) { - std::string query = StringFormat("SELECT `inspect_message` FROM `character_inspect_messages` WHERE `id` = %u LIMIT 1", character_id); + const std::string query = StringFormat("SELECT `inspect_message` FROM `character_inspect_messages` WHERE `id` = %u LIMIT 1", character_id); auto results = QueryDatabase(query); - auto row = results.begin(); memset(message, '\0', sizeof(InspectMessage_Struct)); - for (auto row = results.begin(); row != results.end(); ++row) { + for (auto& row = results.begin(); row != results.end(); ++row) { memcpy(message, row[0], sizeof(InspectMessage_Struct)); } } void SharedDatabase::SaveCharacterInspectMessage(uint32 character_id, const InspectMessage_Struct* message) { - std::string query = StringFormat("REPLACE INTO `character_inspect_messages` (id, inspect_message) VALUES (%u, '%s')", character_id, EscapeString(message->text).c_str()); + const std::string query = StringFormat("REPLACE INTO `character_inspect_messages` (id, inspect_message) VALUES (%u, '%s')", character_id, EscapeString(message->text).c_str()); auto results = QueryDatabase(query); } diff --git a/common/shareddb.h b/common/shareddb.h index 13548c0d3..165cbe380 100644 --- a/common/shareddb.h +++ b/common/shareddb.h @@ -143,15 +143,15 @@ public: void GetItemsCount(int32 &item_count, uint32 &max_id); void LoadItems(void *data, uint32 size, int32 items, uint32 max_item_id); bool LoadItems(const std::string &prefix); - const EQ::ItemData *IterateItems(uint32 *id); - const EQ::ItemData *GetItem(uint32 id); + const EQ::ItemData *IterateItems(uint32 *id) const; + const EQ::ItemData *GetItem(uint32 id) const; const EvolveInfo *GetEvolveInfo(uint32 loregroup); /** * faction */ void GetFactionListInfo(uint32 &list_count, uint32 &max_lists); - const NPCFactionList *GetNPCFactionEntry(uint32 id); + const NPCFactionList *GetNPCFactionEntry(uint32 id) const; void LoadNPCFactionLists(void *data, uint32 size, uint32 list_count, uint32 max_lists); bool LoadNPCFactionLists(const std::string &prefix); @@ -163,16 +163,16 @@ public: void LoadLootTables(void *data, uint32 size); void LoadLootDrops(void *data, uint32 size); bool LoadLoot(const std::string &prefix); - const LootTable_Struct *GetLootTable(uint32 loottable_id); - const LootDrop_Struct *GetLootDrop(uint32 lootdrop_id); + const LootTable_Struct *GetLootTable(uint32 loottable_id) const; + const LootDrop_Struct *GetLootDrop(uint32 lootdrop_id) const; /** * skills */ void LoadSkillCaps(void *data); bool LoadSkillCaps(const std::string &prefix); - uint16 GetSkillCap(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level); - uint8 GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level); + uint16 GetSkillCap(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level) const; + uint8 GetTrainLevel(uint8 Class_, EQ::skills::SkillType Skill, uint8 Level) const; /** * spells @@ -188,9 +188,10 @@ public: int GetMaxBaseDataLevel(); bool LoadBaseData(const std::string &prefix); void LoadBaseData(void *data, int max_level); - const BaseDataStruct *GetBaseData(int lvl, int cl); + const BaseDataStruct *GetBaseData(int lvl, int cl) const; - std::string CreateItemLink(uint32 item_id) { + std::string CreateItemLink(uint32 item_id) const + { EQ::SayLinkEngine linker; linker.SetLinkType(EQ::saylink::SayLinkItemData); const EQ::ItemData *item = GetItem(item_id);