From f2b3b0bf660a8e3ce0584d92942edc00daef7b64 Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Mon, 3 Jan 2022 21:40:07 -0800 Subject: [PATCH 1/7] Check user input for the "/runmacro" command Presents users from specifying a file path, and looking up macros in the expected folder. --- dGame/dUtilities/SlashCommandHandler.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index 47704d2b..5147cf16 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -611,6 +611,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "runmacro" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { if (args.size() != 1) return; + // Only process if input does not contain separator charaters + if (args[0].find("/") != std::string::npos) return; + if (args[0].find("\\") != std::string::npos) return; + std::ifstream infile("./res/macros/" + args[0] + ".scm"); if (infile.good()) { From 590ccc78aa97ea4d0c055f61d4e3061e9ba721bf Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Wed, 5 Jan 2022 21:52:33 -0500 Subject: [PATCH 2/7] Add proper sanitizing for input to SQLite database. Adds function `ExecuteQueryWithArgs(query, ...)` to allow for queries with user input. There is a known issue, that the funciton does not work with std::string. All strings must be converted to c strings. --- dDatabase/CDClientDatabase.h | 14 ++ dGame/dBehaviors/Behavior.cpp | 39 ++--- dGame/dUtilities/SlashCommandHandler.cpp | 203 ++++++++++++----------- 3 files changed, 137 insertions(+), 119 deletions(-) diff --git a/dDatabase/CDClientDatabase.h b/dDatabase/CDClientDatabase.h index fbb1dc86..5028e0bd 100644 --- a/dDatabase/CDClientDatabase.h +++ b/dDatabase/CDClientDatabase.h @@ -40,4 +40,18 @@ namespace CDClientDatabase { */ CppSQLite3Query ExecuteQuery(const std::string& query); + //! Queries the CDClient and parses arguments + /*! + \param query The query with formatted arguments + \return the results of the query + */ + // Due to the template, implementation must be in the header. + template + CppSQLite3Query ExecuteQueryWithArgs(const std::string& query, Args... args) { + CppSQLite3Buffer sqlBuf; + sqlBuf.format(query.c_str(), args...); + + std::string safe_query = (const char *) sqlBuf; + return ExecuteQuery(safe_query); + } }; diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 4769df7e..56a09c57 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -89,21 +89,21 @@ Behavior* Behavior::GetBehavior(const uint32_t behaviorId) Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) { auto* cached = GetBehavior(behaviorId); - + if (cached != nullptr) { return cached; } - + if (behaviorId == 0) { return new EmptyBehavior(0); } - + const auto templateId = GetBehaviorTemplate(behaviorId); Behavior* behavior = nullptr; - + switch (templateId) { case BehaviorTemplates::BEHAVIOR_EMPTY: break; @@ -266,7 +266,7 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) if (behavior == nullptr) { //Game::logger->Log("Behavior", "Failed to load unimplemented template id (%i)!\n", templateId); - + behavior = new EmptyBehavior(behaviorId); } @@ -293,7 +293,7 @@ BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) return BehaviorTemplates::BEHAVIOR_EMPTY; } - + const auto id = static_cast(result.getIntField(0)); result.finalize(); @@ -319,7 +319,7 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID return; } - + auto* renderComponent = targetEntity->GetComponent(); const auto typeString = GeneralUtils::UTF16ToWTF8(type); @@ -342,29 +342,30 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID if (renderComponent == nullptr) { GameMessages::SendPlayFXEffect(targetEntity, effectId, type, pair->second, secondary, 1, 1, true); - + return; } renderComponent->PlayEffect(effectId, type, pair->second, secondary); - + return; } } - - std::stringstream query; - + + CppSQLite3Query result; if (!type.empty()) { - query << "SELECT effectName FROM BehaviorEffect WHERE effectType = '" << typeString << "' AND effectID = " << std::to_string(effectId) << ";"; + result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT effectName FROM BehaviorEffect WHERE effectType = %Q AND effectID = %u;", + typeString.c_str(), effectId); } else { - query << "SELECT effectName, effectType FROM BehaviorEffect WHERE effectID = " << std::to_string(effectId) << ";"; + result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT effectName, effectType FROM BehaviorEffect WHERE effectID = %u;", + effectId); } - auto result = CDClientDatabase::ExecuteQuery(query.str()); - if (result.eof() || result.fieldIsNull(0)) { return; @@ -375,7 +376,7 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID if (type.empty()) { const auto typeResult = result.getStringField(1); - + type = GeneralUtils::ASCIIToUTF16(typeResult); m_effectType = new std::string(typeResult); @@ -388,7 +389,7 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID if (renderComponent == nullptr) { GameMessages::SendPlayFXEffect(targetEntity, effectId, type, name, secondary, 1, 1, true); - + return; } @@ -431,7 +432,7 @@ Behavior::Behavior(const uint32_t behaviorId) } this->m_templateId = static_cast(result.getIntField(0)); - + this->m_effectId = result.getIntField(1); if (!result.fieldIsNull(2)) diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index 6b1c932b..fa601946 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -66,22 +66,22 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entity* entity, const SystemAddress& sysAddr) { std::string chatCommand; std::vector args; - + uint32_t breakIndex = 0; for (uint32_t i = 1; i < command.size(); ++i) { if (command[i] == L' ') { breakIndex = i; break; } - + chatCommand.push_back(static_cast(command[i])); breakIndex++; } - + uint32_t index = ++breakIndex; while (true) { std::string arg; - + while (index < command.size()) { if (command[index] == L' ') { args.push_back(arg); @@ -89,24 +89,24 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit index++; continue; } - + arg.push_back(static_cast(command[index])); index++; } - + if (arg != "") { args.push_back(arg); } - + break; } //Game::logger->Log("SlashCommandHandler", "Received chat command \"%s\"\n", GeneralUtils::UTF16ToWTF8(command).c_str()); - + User* user = UserManager::Instance()->GetUser(sysAddr); if ((chatCommand == "setgmlevel" || chatCommand == "makegm" || chatCommand == "gmlevel") && user->GetMaxGMLevel() > GAME_MASTER_LEVEL_CIVILIAN) { if (args.size() != 1) return; - + uint32_t level; if (!GeneralUtils::TryParse(args[0], level)) @@ -129,7 +129,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (level == entity->GetGMLevel()) return; bool success = user->GetMaxGMLevel() >= level; - + if (success) { if (entity->GetGMLevel() > GAME_MASTER_LEVEL_CIVILIAN && level == GAME_MASTER_LEVEL_CIVILIAN) { @@ -174,10 +174,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit character->SetPvpEnabled(!character->GetPvpEnabled()); EntityManager::Instance()->SerializeEntity(entity); - + std::stringstream message; message << character->GetName() << " changed their PVP flag to " << std::to_string(character->GetPvpEnabled()) << "!"; - + ChatPackets::SendSystemMessage(UNASSIGNED_SYSTEM_ADDRESS, GeneralUtils::ASCIIToUTF16(message.str()), true); return; @@ -206,14 +206,14 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit { std::stringstream message; message << "Your latest ping: " << std::to_string(Game::server->GetLatestPing(sysAddr)) << "ms"; - + ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(message.str())); } else { std::stringstream message; message << "Your average ping: " << std::to_string(Game::server->GetPing(sysAddr)) << "ms"; - + ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(message.str())); } return; @@ -293,7 +293,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit Game::logger->Log("SlashCommandHandler", "Sending \n%s\n", customText.c_str()); GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "ToggleStoryBox", &args); - + delete visiable; delete text; }); @@ -301,7 +301,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit return; } - if ((chatCommand == "leave-zone")) { + if (chatCommand == "leave-zone") { const auto currentZone = dZoneManager::Instance()->GetZone()->GetZoneID().GetMapID(); auto newZone = 1100; @@ -322,10 +322,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (currentZone == newZone) { ChatPackets::SendSystemMessage(sysAddr, u"You are not in an instanced zone."); - + return; } - + ChatPackets::SendSystemMessage(sysAddr, u"Leaving zone..."); const auto objid = entity->GetObjectID(); @@ -354,10 +354,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit }); } - if ((chatCommand == "join" && !args.empty())) { + if (chatCommand == "join" && !args.empty()) { ChatPackets::SendSystemMessage(sysAddr, u"Requesting private map..."); const auto& password = args[0]; - + ZoneInstanceManager::Instance()->RequestPrivateZone(Game::server, false, password, [=](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) { Game::logger->Log("UserManager", "Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i\n", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort); @@ -386,12 +386,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "resurrect") { ScriptedActivityComponent* scriptedActivityComponent = dZoneManager::Instance()->GetZoneControlObject()->GetComponent(); - + if (scriptedActivityComponent) { // check if user is in activity world and if so, they can't resurrect ChatPackets::SendSystemMessage(sysAddr, u"You cannot resurrect in an activity world."); return; } - + GameMessages::SendResurrect(entity); } @@ -414,7 +414,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit stmt->setString(2, GeneralUtils::UTF16ToWTF8(command).c_str()); stmt->execute(); delete stmt; - + if (chatCommand == "setminifig" && args.size() == 2 && entity->GetGMLevel() >= GAME_MASTER_LEVEL_FORUM_MODERATOR) { // could break characters so only allow if GM > 0 int32_t minifigItemId; if (!GeneralUtils::TryParse(args[1], minifigItemId)) { @@ -452,7 +452,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid Minifig item to change, try one of the following: Eyebrows, Eyes, HairColor, HairStyle, Pants, LeftHand, Mouth, RightHand, Shirt, Hands"); return; } - + EntityManager::Instance()->ConstructEntity(entity); ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(lowerName) + u" set to " + (GeneralUtils::to_u16string(minifigItemId))); @@ -463,15 +463,15 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit for (const auto& pair : EntityManager::Instance()->GetSpawnPointEntities()) { ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(pair.first)); } - + ChatPackets::SendSystemMessage(sysAddr, u"Current: " + GeneralUtils::ASCIIToUTF16(entity->GetCharacter()->GetTargetScene())); return; } - + if (chatCommand == "unlock-emote" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { int32_t emoteID; - + if (!GeneralUtils::TryParse(args[0], emoteID)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid emote ID."); @@ -503,7 +503,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "speedboost" && args.size() == 1 && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { float boost; - + if (!GeneralUtils::TryParse(args[0], boost)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid boost."); @@ -527,7 +527,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit entity->SetVar(u"freecam", state); GameMessages::SendSetPlayerControlScheme(entity, static_cast(state ? 9 : 1)); - + ChatPackets::SendSystemMessage(sysAddr, u"Toggled freecam."); return; } @@ -540,9 +540,9 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid control scheme."); return; } - + GameMessages::SendSetPlayerControlScheme(entity, static_cast(scheme)); - + ChatPackets::SendSystemMessage(sysAddr, u"Switched control scheme."); return; } @@ -564,7 +564,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit AMFArrayValue args; args.InsertValue("state", value); GameMessages::SendUIMessageServerToSingleClient(entity, sysAddr, "pushGameState", &args); - + ChatPackets::SendSystemMessage(sysAddr, u"Switched UI state."); delete value; @@ -578,7 +578,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit AMFArrayValue amfArgs; amfArgs.InsertValue("visible", value); GameMessages::SendUIMessageServerToSingleClient(entity, sysAddr, args[0], &amfArgs); - + ChatPackets::SendSystemMessage(sysAddr, u"Toggled UI state."); delete value; @@ -596,12 +596,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid size."); return; } - + InventoryComponent* inventory = static_cast(entity->GetComponent(COMPONENT_TYPE_INVENTORY)); if (inventory) { auto* items = inventory->GetInventory(ITEMS); - + items->SetSize(size); } @@ -626,7 +626,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit else { ChatPackets::SendSystemMessage(sysAddr, u"Unknown macro! Is the filename right?"); } - + return; } @@ -640,7 +640,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission id."); return; } - + auto comp = static_cast(entity->GetComponent(COMPONENT_TYPE_MISSION)); if (comp) comp->AcceptMission(missionID, true); return; @@ -656,7 +656,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission id."); return; } - + auto comp = static_cast(entity->GetComponent(COMPONENT_TYPE_MISSION)); if (comp) comp->CompleteMission(missionID, true); return; @@ -698,7 +698,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid mission id."); return; } - + auto* comp = static_cast(entity->GetComponent(COMPONENT_TYPE_MISSION)); if (comp == nullptr) { @@ -710,7 +710,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (mission == nullptr) { return; } - + mission->SetMissionState(MissionState::MISSION_STATE_ACTIVE); return; @@ -792,7 +792,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage(sysAddr, u"Invalid item LOT."); return; } - + InventoryComponent * inventory = static_cast(entity->GetComponent(COMPONENT_TYPE_INVENTORY)); inventory->AddItem(itemLOT, 1); @@ -840,7 +840,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (receiverID == 0) { ChatPackets::SendSystemMessage(sysAddr, u"Failed to find that player"); - + return; } @@ -867,7 +867,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ins->setInt(11, 1); ins->execute(); delete ins; - + ChatPackets::SendSystemMessage(sysAddr, u"Mail sent"); return; @@ -884,7 +884,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit GameMessages::SendSetName(entity->GetObjectID(), GeneralUtils::ASCIIToUTF16(name), UNASSIGNED_SYSTEM_ADDRESS); } - + if (chatCommand == "title" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { std::string name = entity->GetCharacter()->GetName() + " - "; @@ -902,19 +902,19 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (args.size() == 3) { float x, y, z; - + if (!GeneralUtils::TryParse(args[0], x)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid x."); return; } - + if (!GeneralUtils::TryParse(args[1], y)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid y."); return; } - + if (!GeneralUtils::TryParse(args[2], z)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid z."); @@ -924,7 +924,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit pos.SetX(x); pos.SetY(y); pos.SetZ(z); - + Game::logger->Log("SlashCommandHandler", "Teleporting objectID: %llu to %f, %f, %f\n", entity->GetObjectID(), pos.x, pos.y, pos.z); GameMessages::SendTeleport(entity->GetObjectID(), pos, NiQuaternion(), sysAddr); } else if (args.size() == 2) { @@ -946,7 +946,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit pos.SetY(0.0f); pos.SetZ(z); - + Game::logger->Log("SlashCommandHandler", "Teleporting objectID: %llu to X: %f, Z: %f\n", entity->GetObjectID(), pos.x, pos.z); GameMessages::SendTeleport(entity->GetObjectID(), pos, NiQuaternion(), sysAddr); } else { @@ -1019,7 +1019,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (player == nullptr) { auto* accountQuery = Database::CreatePreppedStmt("SELECT account_id, id FROM charinfo WHERE name=? LIMIT 1;"); - + accountQuery->setString(1, args[0]); auto result = accountQuery->executeQuery(); @@ -1029,7 +1029,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit while (result->next()) { accountId = result->getUInt(1); characterId = result->getUInt64(2); - + characterId = GeneralUtils::SetBit(characterId, OBJECT_BIT_CHARACTER); characterId = GeneralUtils::SetBit(characterId, OBJECT_BIT_PERSISTENT); } @@ -1087,7 +1087,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit userUpdate->executeUpdate(); delete userUpdate; - + char buffer[32] = "brought up for review.\0"; if (expire != 1) @@ -1096,12 +1096,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit // Format: Mo, 15.06.2009 20:20:00 std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm); } - + const auto timeStr = GeneralUtils::ASCIIToUTF16(std::string(buffer)); ChatPackets::SendSystemMessage(sysAddr, u"Muted: " + GeneralUtils::ASCIIToUTF16(args[0]) + u" until " + timeStr); - //Notify chat about it + //Notify chat about it CBITSTREAM; PacketUtils::WriteHeader(bitStream, CHAT_INTERNAL, MSG_CHAT_INTERNAL_MUTE_UPDATE); @@ -1144,7 +1144,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (player == nullptr) { auto* accountQuery = Database::CreatePreppedStmt("SELECT account_id FROM charinfo WHERE name=? LIMIT 1;"); - + accountQuery->setString(1, args[0]); auto result = accountQuery->executeQuery(); @@ -1190,7 +1190,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } //------------------------------------------------- - + if (chatCommand == "buffme" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { auto dest = static_cast(entity->GetComponent(COMPONENT_TYPE_DESTROYABLE)); if (dest) { @@ -1201,13 +1201,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit dest->SetImagination(999); dest->SetMaxImagination(999.0f); } - + EntityManager::Instance()->SerializeEntity(entity); } if (chatCommand == "startcelebration" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() == 1) { int32_t celebration; - + if (!GeneralUtils::TryParse(args[0], celebration)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid celebration."); @@ -1216,7 +1216,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit GameMessages::SendStartCelebrationEffect(entity, entity->GetSystemAddress(), celebration); } - + if (chatCommand == "buffmed" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { auto dest = static_cast(entity->GetComponent(COMPONENT_TYPE_DESTROYABLE)); if (dest) { @@ -1230,7 +1230,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit EntityManager::Instance()->SerializeEntity(entity); } - + if (chatCommand == "refillstats" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { auto dest = static_cast(entity->GetComponent(COMPONENT_TYPE_DESTROYABLE)); if (dest) { @@ -1241,23 +1241,27 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit EntityManager::Instance()->SerializeEntity(entity); } - + if (chatCommand == "lookup" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() == 1) { - std::string query = "SELECT `id`, `name` FROM `Objects` WHERE `displayName` LIKE '%" + args[0] + "%' OR `name` LIKE '%" + args[0] + "%' OR `description` LIKE '%" + args[0] + "%'"; - auto tables = CDClientDatabase::ExecuteQuery(query.c_str()); + std::string query = "%" + args[0] + "%"; + const char* query_cstr = query.c_str(); + auto tables = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT `id`, `name` FROM `Objects` WHERE `displayName` LIKE %Q OR `name` LIKE %Q OR `description` LIKE %Q LIMIT 50", + query_cstr, query_cstr, query_cstr); + while (!tables.eof()) { std::string message = std::to_string(tables.getIntField(0)) + " - " + tables.getStringField(1); ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(message, message.size())); tables.nextRow(); } } - + if (chatCommand == "spawn" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() >= 1) { ControllablePhysicsComponent* comp = static_cast(entity->GetComponent(COMPONENT_TYPE_CONTROLLABLE_PHYSICS)); if (!comp) return; uint32_t lot; - + if (!GeneralUtils::TryParse(args[0], lot)) { ChatPackets::SendSystemMessage(sysAddr, u"Invalid lot."); @@ -1301,7 +1305,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit const auto position = entity->GetPosition(); ChatPackets::SendSystemMessage(sysAddr, u"<" + (GeneralUtils::to_u16string(position.x)) + u", " + (GeneralUtils::to_u16string(position.y)) + u", " + (GeneralUtils::to_u16string(position.z)) + u">"); - + std::cout << position.x << ", " << position.y << ", " << position.z << std::endl; } @@ -1358,7 +1362,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit if (chatCommand == "gminvis" && entity->GetParentUser()->GetMaxGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { GameMessages::SendToggleGMInvis(entity->GetObjectID(), true, UNASSIGNED_SYSTEM_ADDRESS); - + return; } @@ -1378,7 +1382,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit { destroyableComponent->SetIsGMImmune(state); } - + return; } @@ -1405,7 +1409,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit { buffComponent->ApplyBuff(id, duration, entity->GetObjectID()); } - + return; } @@ -1440,10 +1444,10 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } const auto objid = entity->GetObjectID(); - + if (force || CheckIfAccessibleZone(reqZone)) { // to prevent tomfoolery bool darwin = true; //Putting this on true, as I'm sick of having to wait 3-4 seconds on a transfer while trying to quickly moderate properties - + Character* character = entity->GetCharacter(); if (character) { std::string lowerName = character->GetName(); @@ -1488,7 +1492,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit WorldPackets::SendTransferToWorld(sysAddr, serverIP, serverPort, mythranShift); }); - + return; }); } else { @@ -1519,7 +1523,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit const auto& password = args[2]; ZoneInstanceManager::Instance()->CreatePrivateZone(Game::server, zone, clone, password); - + ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16("Sent request for private zone with password: " + password)); return; @@ -1594,7 +1598,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } } } - + if (chatCommand == "triggerspawner" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() >= 1) { auto spawners = dZoneManager::Instance()->GetSpawnersByName(args[0]); @@ -1676,13 +1680,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit ChatPackets::SendSystemMessage( sysAddr, - GeneralUtils::ASCIIToUTF16(Metrics::MetricVariableToString(variable)) + - u": " + - GeneralUtils::to_u16string(Metrics::ToMiliseconds(metric->average)) + + GeneralUtils::ASCIIToUTF16(Metrics::MetricVariableToString(variable)) + + u": " + + GeneralUtils::to_u16string(Metrics::ToMiliseconds(metric->average)) + u"ms" ); } - + ChatPackets::SendSystemMessage( sysAddr, u"Peak RSS: " + GeneralUtils::to_u16string((float) ((double) Metrics::GetPeakRSS() / 1.024e6)) + @@ -1729,11 +1733,11 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } std::u16string message = u"Ran loot drops looking for " - + GeneralUtils::to_u16string(targetLot) - + u", " - + GeneralUtils::to_u16string(loops) - + u" times. It ran " - + GeneralUtils::to_u16string(totalRuns) + + GeneralUtils::to_u16string(targetLot) + + u", " + + GeneralUtils::to_u16string(loops) + + u" times. It ran " + + GeneralUtils::to_u16string(totalRuns) + u" times. Averaging out at " + GeneralUtils::to_u16string((float) totalRuns / loops); @@ -1764,7 +1768,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit auto closestDistance = 0.0f; const auto candidates = EntityManager::Instance()->GetEntitiesByComponent(component); - + for (auto* candidate : candidates) { if (candidate->GetLOT() == 1 || candidate->GetLOT() == 8092) @@ -1776,16 +1780,16 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit { continue; } - + if (closest == nullptr) { closest = candidate; closestDistance = NiPoint3::Distance(candidate->GetPosition(), reference); - + continue; } - + const auto distance = NiPoint3::Distance(candidate->GetPosition(), reference); if (distance < closestDistance) @@ -1812,7 +1816,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit header << info.name << " [" << std::to_string(info.id) << "]" << " " << std::to_string(closestDistance) << " " << std::to_string(closest->IsSleeping()); ChatPackets::SendSystemMessage(sysAddr, GeneralUtils::ASCIIToUTF16(header.str())); - + for (const auto& pair : closest->GetComponents()) { auto id = pair.first; @@ -1825,7 +1829,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (args.size() >= 2) - { + { if (args[1] == "-m" && args.size() >= 3) { auto* movingPlatformComponent = closest->GetComponent(); @@ -1869,7 +1873,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit const auto postion = closest->GetPosition(); ChatPackets::SendSystemMessage( - sysAddr, + sysAddr, GeneralUtils::ASCIIToUTF16("< " + std::to_string(postion.x) + ", " + std::to_string(postion.y) + ", " + std::to_string(postion.z) + " >") ); } @@ -1936,35 +1940,35 @@ bool SlashCommandHandler::CheckIfAccessibleZone(const unsigned int zoneID) { case 98: case 1000: case 1001: - + case 1100: case 1101: case 1150: case 1151: case 1152: - + case 1200: case 1201: case 1250: case 1251: case 1260: - + case 1300: case 1350: case 1351: - + case 1400: case 1401: case 1450: case 1451: - + case 1600: case 1601: case 1602: case 1603: case 1604: - + case 1800: case 1900: case 2000: @@ -1973,11 +1977,11 @@ bool SlashCommandHandler::CheckIfAccessibleZone(const unsigned int zoneID) { case 58005: case 58006: return true; - + default: return false; } - + return false; } @@ -1998,7 +2002,7 @@ void SlashCommandHandler::SendAnnouncement(const std::string& title, const std:: titleValue = nullptr; messageValue = nullptr; - //Notify chat about it + //Notify chat about it CBITSTREAM; PacketUtils::WriteHeader(bitStream, CHAT_INTERNAL, MSG_CHAT_INTERNAL_ANNOUNCEMENT); @@ -2010,4 +2014,3 @@ void SlashCommandHandler::SendAnnouncement(const std::string& title, const std:: Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false); } - From 4796b551addd73395f87a10baf1309664127b78f Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Thu, 6 Jan 2022 16:05:03 -0500 Subject: [PATCH 3/7] Additional SQLite lookup sanitizing with CDClientDatabase::ExecuteQueryWithArgs() --- dDatabase/Tables/CDBehaviorParameterTable.cpp | 8 +++----- dGame/dComponents/RenderComponent.cpp | 14 ++++++-------- dGame/dInventory/ItemSet.cpp | 16 ++++++---------- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/dDatabase/Tables/CDBehaviorParameterTable.cpp b/dDatabase/Tables/CDBehaviorParameterTable.cpp index b29a8267..2a1554cb 100644 --- a/dDatabase/Tables/CDBehaviorParameterTable.cpp +++ b/dDatabase/Tables/CDBehaviorParameterTable.cpp @@ -59,11 +59,9 @@ float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::s } #ifndef CDCLIENT_CACHE_ALL - std::stringstream query; - - query << "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = " << std::to_string(behaviorID); - - auto tableData = CDClientDatabase::ExecuteQuery(query.str()); + auto tableData = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = %u;", + behaviorID); m_Entries.insert_or_assign(behaviorID, 0); diff --git a/dGame/dComponents/RenderComponent.cpp b/dGame/dComponents/RenderComponent.cpp index aeb56f56..b4787d40 100644 --- a/dGame/dComponents/RenderComponent.cpp +++ b/dGame/dComponents/RenderComponent.cpp @@ -198,14 +198,12 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e return; } - std::stringstream query; + const std::string effectType_str = GeneralUtils::UTF16ToWTF8(effectType); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT animation_length FROM Animations WHERE animation_type IN (SELECT animationName FROM BehaviorEffect WHERE effectID = %d AND effectType = %Q);", + effectId, effectType_str.c_str()); - query << "SELECT animation_length FROM Animations WHERE animation_type IN (SELECT animationName FROM BehaviorEffect WHERE effectID = " << std::to_string(effectId) << " AND effectType = '" << GeneralUtils::UTF16ToWTF8(effectType) << "');"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); - - if (result.eof() || result.fieldIsNull(0)) - { + if (result.eof() || result.fieldIsNull(0)) { result.finalize(); m_DurationCache[effectId] = 0; @@ -214,7 +212,7 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e return; } - + effect->time = static_cast(result.getFloatField(0)); result.finalize(); diff --git a/dGame/dInventory/ItemSet.cpp b/dGame/dInventory/ItemSet.cpp index d1e74df4..93e86a81 100644 --- a/dGame/dInventory/ItemSet.cpp +++ b/dGame/dInventory/ItemSet.cpp @@ -15,11 +15,9 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) this->m_PassiveAbilities = ItemSetPassiveAbility::FindAbilities(id, m_InventoryComponent->GetParent(), this); - std::stringstream query; - - query << "SELECT skillSetWith2, skillSetWith3, skillSetWith4, skillSetWith5, skillSetWith6, itemIDs FROM ItemSets WHERE setID = " << std::to_string(id); - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT skillSetWith2, skillSetWith3, skillSetWith4, skillSetWith5, skillSetWith6, itemIDs FROM ItemSets WHERE setID = %u;", + id); if (result.eof()) { @@ -33,11 +31,9 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) continue; } - std::stringstream skillQuery; - - skillQuery << "SELECT SkillID FROM ItemSetSkills WHERE SkillSetID = " << std::to_string(result.getIntField(i)); - - auto skillResult = CDClientDatabase::ExecuteQuery(skillQuery.str()); + auto skillResult = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT SkillID FROM ItemSetSkills WHERE SkillSetID = %d;", + result.getIntField(i)); if (skillResult.eof()) { From e5f7d164cbf958cabd6f6f7da9cf56356e565c18 Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Thu, 6 Jan 2022 21:12:47 -0500 Subject: [PATCH 4/7] Additional SQLite lookup sanitizing. Using CDClientDatabase::ExecuteQueryWithArgs() across all known lookups. --- dGame/dBehaviors/Behavior.cpp | 31 ++++++------------- dGame/dBehaviors/SwitchMultipleBehavior.cpp | 16 +++++----- dGame/dComponents/BaseCombatAIComponent.cpp | 17 ++++------ dGame/dComponents/BuffComponent.cpp | 8 ++--- dGame/dComponents/DestroyableComponent.cpp | 7 ++--- dGame/dComponents/InventoryComponent.cpp | 18 +++++------ dGame/dComponents/MissionComponent.cpp | 7 ++--- dGame/dComponents/PetComponent.cpp | 11 +++---- .../PropertyManagementComponent.cpp | 16 ++++------ .../RocketLaunchpadControlComponent.cpp | 8 ++--- dGame/dComponents/SkillComponent.cpp | 25 ++++++--------- dGame/dGameMessages/GameMessages.cpp | 8 ++--- dGame/dInventory/Item.cpp | 8 ++--- dGame/dUtilities/Preconditions.cpp | 11 +++---- dWorldServer/WorldServer.cpp | 8 ++--- dZoneManager/dZoneManager.cpp | 5 +-- 16 files changed, 75 insertions(+), 129 deletions(-) diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 56a09c57..2e3290f7 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -275,13 +275,10 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) return behavior; } -BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) -{ - std::stringstream query; - - query << "SELECT templateID FROM BehaviorTemplate WHERE behaviorID = " << std::to_string(behaviorId); - - auto result = CDClientDatabase::ExecuteQuery(query.str()); +BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) { + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT templateID FROM BehaviorTemplate WHERE behaviorID = %u;", + behaviorId); // Make sure we do not proceed if we are trying to load an invalid behavior if (result.eof()) @@ -409,15 +406,9 @@ Behavior::Behavior(const uint32_t behaviorId) this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY; } - /* - * Get standard info - */ - - std::stringstream query; - - query << "SELECT templateID, effectID, effectHandle FROM BehaviorTemplate WHERE behaviorID = " << std::to_string(behaviorId); - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT templateID, effectID, effectHandle FROM BehaviorTemplate WHERE behaviorID = %u;", + behaviorId); // Make sure we do not proceed if we are trying to load an invalid behavior if (result.eof()) @@ -490,11 +481,9 @@ std::map Behavior::GetParameterNames() const { std::map parameters; - std::stringstream query; - - query << "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = " << std::to_string(this->m_behaviorId); - - auto tableData = CDClientDatabase::ExecuteQuery(query.str()); + auto tableData = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = %u;", + this->m_behaviorId); while (!tableData.eof()) { diff --git a/dGame/dBehaviors/SwitchMultipleBehavior.cpp b/dGame/dBehaviors/SwitchMultipleBehavior.cpp index 691e8b53..062ae9f0 100644 --- a/dGame/dBehaviors/SwitchMultipleBehavior.cpp +++ b/dGame/dBehaviors/SwitchMultipleBehavior.cpp @@ -39,15 +39,13 @@ void SwitchMultipleBehavior::Calculate(BehaviorContext* context, RakNet::BitStre // TODO } -void SwitchMultipleBehavior::Load() -{ - const auto b = std::to_string(this->m_behaviorId); - std::stringstream query; - query << "SELECT replace(bP1.parameterID, 'behavior ', '') as key, bP1.value as behavior, " - << "(select bP2.value FROM BehaviorParameter bP2 WHERE bP2.behaviorID = " << b << " AND bP2.parameterID LIKE 'value %' " - << "AND replace(bP1.parameterID, 'behavior ', '') = replace(bP2.parameterID, 'value ', '')) as value " - << "FROM BehaviorParameter bP1 WHERE bP1.behaviorID = " << b << " AND bP1.parameterID LIKE 'behavior %'"; - auto result = CDClientDatabase::ExecuteQuery(query.str()); +void SwitchMultipleBehavior::Load() { + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT replace(bP1.parameterID, 'behavior ', '') as key, bP1.value as behavior, " + "(select bP2.value FROM BehaviorParameter bP2 WHERE bP2.behaviorID = %u AND bP2.parameterID LIKE 'value %' " + "AND replace(bP1.parameterID, 'behavior ', '') = replace(bP2.parameterID, 'value ', '')) as value " + "FROM BehaviorParameter bP1 WHERE bP1.behaviorID = %u AND bP1.parameterID LIKE 'behavior %';", + this->m_behaviorId, this->m_behaviorId); while (!result.eof()) { const auto behavior_id = static_cast(result.getFloatField(1)); diff --git a/dGame/dComponents/BaseCombatAIComponent.cpp b/dGame/dComponents/BaseCombatAIComponent.cpp index ae929d57..994f49c4 100644 --- a/dGame/dComponents/BaseCombatAIComponent.cpp +++ b/dGame/dComponents/BaseCombatAIComponent.cpp @@ -35,11 +35,9 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id) m_SoftTimer = 5.0f; //Grab the aggro information from BaseCombatAI: - std::stringstream componentQuery; - - componentQuery << "SELECT aggroRadius, tetherSpeed, pursuitSpeed, softTetherRadius, hardTetherRadius FROM BaseCombatAIComponent WHERE id = " << std::to_string(id); - - auto componentResult = CDClientDatabase::ExecuteQuery(componentQuery.str()); + auto componentResult = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT aggroRadius, tetherSpeed, pursuitSpeed, softTetherRadius, hardTetherRadius FROM BaseCombatAIComponent WHERE id = %u;", + id); if (!componentResult.eof()) { @@ -64,12 +62,9 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id) /* * Find skills */ - - std::stringstream query; - - query << "SELECT skillID, cooldown, behaviorID FROM SkillBehavior WHERE skillID IN (SELECT skillID FROM ObjectSkills WHERE objectTemplate = " << std::to_string(parent->GetLOT()) << " )"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT skillID, cooldown, behaviorID FROM SkillBehavior WHERE skillID IN (SELECT skillID FROM ObjectSkills WHERE objectTemplate = %d);", + parent->GetLOT()); while (!result.eof()) { const auto skillId = static_cast(result.getIntField(0)); diff --git a/dGame/dComponents/BuffComponent.cpp b/dGame/dComponents/BuffComponent.cpp index 06c859c8..afa5b321 100644 --- a/dGame/dComponents/BuffComponent.cpp +++ b/dGame/dComponents/BuffComponent.cpp @@ -371,11 +371,9 @@ const std::vector& BuffComponent::GetBuffParameters(int32_t buffI return pair->second; } - std::stringstream query; - - query << "SELECT * FROM BuffParameters WHERE BuffID = " << std::to_string(buffId) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT * FROM BuffParameters WHERE BuffID = %d;", + buffId); std::vector parameters {}; diff --git a/dGame/dComponents/DestroyableComponent.cpp b/dGame/dComponents/DestroyableComponent.cpp index fb4dfe61..91ff8b6e 100644 --- a/dGame/dComponents/DestroyableComponent.cpp +++ b/dGame/dComponents/DestroyableComponent.cpp @@ -373,11 +373,8 @@ void DestroyableComponent::AddFaction(const int32_t factionID, const bool ignore m_FactionIDs.push_back(factionID); m_DirtyHealth = true; - std::stringstream query; - - query << "SELECT enemyList FROM Factions WHERE faction = " << std::to_string(factionID); - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT enemyList FROM Factions WHERE faction = %d;", factionID); if (result.eof()) return; diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 50ea26b1..1f9b4cc6 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -1136,22 +1136,18 @@ bool InventoryComponent::IsEquipped(const LOT lot) const return false; } -void InventoryComponent::CheckItemSet(const LOT lot) -{ +void InventoryComponent::CheckItemSet(const LOT lot) { // Check if the lot is in the item set cache - if (std::find(m_ItemSetsChecked.begin(), m_ItemSetsChecked.end(), lot) != m_ItemSetsChecked.end()) - { + if (std::find(m_ItemSetsChecked.begin(), m_ItemSetsChecked.end(), lot) != m_ItemSetsChecked.end()) { return; } - std::stringstream query; + std::cout << "INVENTORY CHECK" << std::endl; + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT setID FROM ItemSets WHERE itemIDs LIKE '%%%d%%';", + lot); - query << "SELECT setID FROM ItemSets WHERE itemIDs LIKE '%" << std::to_string(lot) << "%'"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); - - while (!result.eof()) - { + while (!result.eof()) { const auto id = result.getIntField(0); bool found = false; diff --git a/dGame/dComponents/MissionComponent.cpp b/dGame/dComponents/MissionComponent.cpp index a0e63914..5945394b 100644 --- a/dGame/dComponents/MissionComponent.cpp +++ b/dGame/dComponents/MissionComponent.cpp @@ -450,11 +450,8 @@ const std::vector& MissionComponent::QueryAchievements(MissionTaskType } bool MissionComponent::RequiresItem(const LOT lot) { - std::stringstream query; - - query << "SELECT type FROM Objects WHERE id = " << std::to_string(lot); - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT type FROM Objects WHERE id = %d;", lot); if (result.eof()) { return false; diff --git a/dGame/dComponents/PetComponent.cpp b/dGame/dComponents/PetComponent.cpp index e605908f..8c51791a 100644 --- a/dGame/dComponents/PetComponent.cpp +++ b/dGame/dComponents/PetComponent.cpp @@ -166,13 +166,10 @@ void PetComponent::OnUse(Entity* originator) std::string buildFile; - if (cached == buildCache.end()) - { - std::stringstream query; - - query << "SELECT ValidPiecesLXF, PuzzleModelLot, Timelimit, NumValidPieces, imagCostPerBuild FROM TamingBuildPuzzles WHERE NPCLot = " << std::to_string(m_Parent->GetLOT()) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + if (cached == buildCache.end()) { + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT ValidPiecesLXF, PuzzleModelLot, Timelimit, NumValidPieces, imagCostPerBuild FROM TamingBuildPuzzles WHERE NPCLot = %d;", + m_Parent->GetLOT()); if (result.eof()) { diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index bf89eb22..543f99a2 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -40,11 +40,9 @@ PropertyManagementComponent::PropertyManagementComponent(Entity* parent) : Compo const auto zoneId = worldId.GetMapID(); const auto cloneId = worldId.GetCloneID(); - std::stringstream query; - - query << "SELECT id FROM PropertyTemplate WHERE mapID = " << std::to_string(zoneId) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT id FROM PropertyTemplate WHERE mapID = %d;", + (int) zoneId); if (result.eof() || result.fieldIsNull(0)) { @@ -97,12 +95,10 @@ void PropertyManagementComponent::SetOwner(Entity* value) std::vector PropertyManagementComponent::GetPaths() const { const auto zoneId = dZoneManager::Instance()->GetZone()->GetWorldID(); - - std::stringstream query {}; - query << "SELECT path FROM PropertyTemplate WHERE mapID = " << std::to_string(zoneId) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT path FROM PropertyTemplate WHERE mapID = %u;", + zoneId); std::vector paths {}; diff --git a/dGame/dComponents/RocketLaunchpadControlComponent.cpp b/dGame/dComponents/RocketLaunchpadControlComponent.cpp index 049a44b6..a117fffc 100644 --- a/dGame/dComponents/RocketLaunchpadControlComponent.cpp +++ b/dGame/dComponents/RocketLaunchpadControlComponent.cpp @@ -18,11 +18,9 @@ #include "PacketUtils.h" RocketLaunchpadControlComponent::RocketLaunchpadControlComponent(Entity* parent, int rocketId) : Component(parent) { - std::stringstream query; - - query << "SELECT targetZone, defaultZoneID, targetScene, altLandingPrecondition, altLandingSpawnPointName FROM RocketLaunchpadControlComponent WHERE id = " << std::to_string(rocketId); - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT targetZone, defaultZoneID, targetScene, altLandingPrecondition, altLandingSpawnPointName FROM RocketLaunchpadControlComponent WHERE id = %d;", + rocketId); if (!result.eof() && !result.fieldIsNull(0)) { diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index 0846f014..4396c669 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -86,14 +86,11 @@ void SkillComponent::SyncPlayerProjectile(const LWOOBJID projectileId, RakNet::B const auto sync_entry = this->m_managedProjectiles.at(index); - std::stringstream query; + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = %d);", + sync_entry.lot); - query << "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = " << std::to_string(sync_entry.lot) << ")"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); - - if (result.eof()) - { + if (result.eof()) { Game::logger->Log("SkillComponent", "Failed to find skill id for (%i)!\n", sync_entry.lot); return; @@ -428,8 +425,7 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry) { auto* other = EntityManager::Instance()->GetEntity(entry.branchContext.target); - if (other == nullptr) - { + if (other == nullptr) { if (entry.branchContext.target != LWOOBJID_EMPTY) { Game::logger->Log("SkillComponent", "Invalid projectile target (%llu)!\n", entry.branchContext.target); @@ -438,14 +434,11 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry) return; } - std::stringstream query; + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = %d);", + entry.lot); - query << "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = " << std::to_string(entry.lot) << ")"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); - - if (result.eof()) - { + if (result.eof()) { Game::logger->Log("SkillComponent", "Failed to find skill id for (%i)!\n", entry.lot); return; diff --git a/dGame/dGameMessages/GameMessages.cpp b/dGame/dGameMessages/GameMessages.cpp index cd7910b4..0e30244b 100644 --- a/dGame/dGameMessages/GameMessages.cpp +++ b/dGame/dGameMessages/GameMessages.cpp @@ -2509,11 +2509,9 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream* inStream, Entity* ent const auto zoneId = worldId.GetMapID(); const auto cloneId = worldId.GetCloneID(); - std::stringstream query; - - query << "SELECT id FROM PropertyTemplate WHERE mapID = " << std::to_string(zoneId) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT id FROM PropertyTemplate WHERE mapID = %d;", + (int) zoneId); if (result.eof() || result.fieldIsNull(0)) { return; diff --git a/dGame/dInventory/Item.cpp b/dGame/dInventory/Item.cpp index 20f5321a..92a208cb 100644 --- a/dGame/dInventory/Item.cpp +++ b/dGame/dInventory/Item.cpp @@ -386,11 +386,9 @@ void Item::DisassembleModel() const auto componentId = table->GetByIDAndType(GetLot(), COMPONENT_TYPE_RENDER); - std::stringstream query; - - query << "SELECT render_asset FROM RenderComponent WHERE id = " << std::to_string(componentId) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT render_asset FROM RenderComponent WHERE id = %d;", + componentId); if (result.eof()) { diff --git a/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index b29af130..5b68685c 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -15,13 +15,10 @@ std::map Preconditions::cache = {}; -Precondition::Precondition(const uint32_t condition) -{ - std::stringstream query; - - query << "SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = " << std::to_string(condition) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); +Precondition::Precondition(const uint32_t condition) { + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = %u;", + condition); if (result.eof()) { diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index 2a6cbaa5..ce3774e9 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -1059,11 +1059,9 @@ void HandlePacket(Packet* packet) { const auto zoneId = Game::server->GetZoneID(); const auto cloneId = g_CloneID; - std::stringstream query; - - query << "SELECT id FROM PropertyTemplate WHERE mapID = " << std::to_string(zoneId) << ";"; - - auto result = CDClientDatabase::ExecuteQuery(query.str()); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT id FROM PropertyTemplate WHERE mapID = %u;", + zoneId); if (result.eof() || result.fieldIsNull(0)) { Game::logger->Log("WorldServer", "No property templates found for zone %d, not sending BBB\n", zoneId); diff --git a/dZoneManager/dZoneManager.cpp b/dZoneManager/dZoneManager.cpp index d2188f4f..6ed6c719 100644 --- a/dZoneManager/dZoneManager.cpp +++ b/dZoneManager/dZoneManager.cpp @@ -26,8 +26,9 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { LOT zoneControlTemplate = 2365; - std::stringstream query; - auto result = CDClientDatabase::ExecuteQuery("SELECT zoneControlTemplate, ghostdistance_min, ghostdistance FROM ZoneTable WHERE zoneID = " + std::to_string(zoneID.GetMapID())); + auto result = CDClientDatabase::ExecuteQueryWithArgs( + "SELECT zoneControlTemplate, ghostdistance_min, ghostdistance FROM ZoneTable WHERE zoneID = %d;", + (int) zoneID.GetMapID()); if (!result.eof()) { zoneControlTemplate = result.getIntField("zoneControlTemplate", 2365); From 3de393250340ad27aa2dcac39afe9ed93c114959 Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Wed, 12 Jan 2022 22:48:27 -0500 Subject: [PATCH 5/7] Comply with Xiphoseer required changes. Remove the CDClientDatabase::ExecuteQueryWithArgs() function and replace it with CDClientDatabase::CreatePreppedStmt(). This prevents a developer from accidently using %s, or incorrectly passing std::string, and causing a silent error. --- dDatabase/CDClientDatabase.cpp | 5 + dDatabase/CDClientDatabase.h | 12 +- dDatabase/Tables/CDBehaviorParameterTable.cpp | 8 +- dGame/dBehaviors/Behavior.cpp | 52 +++-- dGame/dBehaviors/SwitchMultipleBehavior.cpp | 10 +- dGame/dComponents/BaseCombatAIComponent.cpp | 16 +- dGame/dComponents/BuffComponent.cpp | 8 +- dGame/dComponents/DestroyableComponent.cpp | 7 +- dGame/dComponents/InventoryComponent.cpp | 186 +++++++++--------- dGame/dComponents/MissionComponent.cpp | 7 +- dGame/dComponents/PetComponent.cpp | 8 +- .../PropertyManagementComponent.cpp | 16 +- dGame/dComponents/RenderComponent.cpp | 10 +- .../RocketLaunchpadControlComponent.cpp | 8 +- dGame/dComponents/SkillComponent.cpp | 15 +- dGame/dGameMessages/GameMessages.cpp | 8 +- dGame/dInventory/Item.cpp | 8 +- dGame/dInventory/ItemSet.cpp | 16 +- dGame/dUtilities/Preconditions.cpp | 8 +- dGame/dUtilities/SlashCommandHandler.cpp | 12 +- dWorldServer/WorldServer.cpp | 8 +- dZoneManager/dZoneManager.cpp | 8 +- 22 files changed, 247 insertions(+), 189 deletions(-) diff --git a/dDatabase/CDClientDatabase.cpp b/dDatabase/CDClientDatabase.cpp index d61d6fe7..d09b2daa 100644 --- a/dDatabase/CDClientDatabase.cpp +++ b/dDatabase/CDClientDatabase.cpp @@ -13,3 +13,8 @@ void CDClientDatabase::Connect(const std::string& filename) { CppSQLite3Query CDClientDatabase::ExecuteQuery(const std::string& query) { return conn->execQuery(query.c_str()); } + +//! Makes prepared statements +CppSQLite3Statement CDClientDatabase::CreatePreppedStmt(const std::string& query) { + return conn->compileStatement(query.c_str()); +} diff --git a/dDatabase/CDClientDatabase.h b/dDatabase/CDClientDatabase.h index 5028e0bd..91e9ee10 100644 --- a/dDatabase/CDClientDatabase.h +++ b/dDatabase/CDClientDatabase.h @@ -43,15 +43,7 @@ namespace CDClientDatabase { //! Queries the CDClient and parses arguments /*! \param query The query with formatted arguments - \return the results of the query + \return prepared SQLite Statement */ - // Due to the template, implementation must be in the header. - template - CppSQLite3Query ExecuteQueryWithArgs(const std::string& query, Args... args) { - CppSQLite3Buffer sqlBuf; - sqlBuf.format(query.c_str(), args...); - - std::string safe_query = (const char *) sqlBuf; - return ExecuteQuery(safe_query); - } + CppSQLite3Statement CreatePreppedStmt(const std::string& query); }; diff --git a/dDatabase/Tables/CDBehaviorParameterTable.cpp b/dDatabase/Tables/CDBehaviorParameterTable.cpp index 2a1554cb..3087ba6d 100644 --- a/dDatabase/Tables/CDBehaviorParameterTable.cpp +++ b/dDatabase/Tables/CDBehaviorParameterTable.cpp @@ -59,9 +59,11 @@ float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::s } #ifndef CDCLIENT_CACHE_ALL - auto tableData = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = %u;", - behaviorID); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); + query.bind(1, (int) behaviorID); + + auto tableData = query.execQuery(); m_Entries.insert_or_assign(behaviorID, 0); diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 2e3290f7..351a4ada 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -276,9 +276,11 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) } BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) { - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT templateID FROM BehaviorTemplate WHERE behaviorID = %u;", - behaviorId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT templateID FROM BehaviorTemplate WHERE behaviorID = ?;"); + query.bind(1, (int) behaviorId); + + auto result = query.execQuery(); // Make sure we do not proceed if we are trying to load an invalid behavior if (result.eof()) @@ -349,18 +351,24 @@ void Behavior::PlayFx(std::u16string type, const LWOOBJID target, const LWOOBJID } } + // The SQlite result object becomes invalid if the query object leaves scope. + // So both queries are defined before the if statement CppSQLite3Query result; - if (!type.empty()) - { - result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT effectName FROM BehaviorEffect WHERE effectType = %Q AND effectID = %u;", - typeString.c_str(), effectId); - } - else - { - result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT effectName, effectType FROM BehaviorEffect WHERE effectID = %u;", - effectId); + auto typeQuery = CDClientDatabase::CreatePreppedStmt( + "SELECT effectName FROM BehaviorEffect WHERE effectType = ? AND effectID = ?;"); + + auto idQuery = CDClientDatabase::CreatePreppedStmt( + "SELECT effectName, effectType FROM BehaviorEffect WHERE effectID = ?;"); + + if (!type.empty()) { + typeQuery.bind(1, typeString.c_str()); + typeQuery.bind(2, (int) effectId); + + result = typeQuery.execQuery(); + } else { + idQuery.bind(1, (int) effectId); + + result = idQuery.execQuery(); } if (result.eof() || result.fieldIsNull(0)) @@ -406,9 +414,11 @@ Behavior::Behavior(const uint32_t behaviorId) this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY; } - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT templateID, effectID, effectHandle FROM BehaviorTemplate WHERE behaviorID = %u;", - behaviorId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT templateID, effectID, effectHandle FROM BehaviorTemplate WHERE behaviorID = ?;"); + query.bind(1, (int) behaviorId); + + auto result = query.execQuery(); // Make sure we do not proceed if we are trying to load an invalid behavior if (result.eof()) @@ -481,9 +491,11 @@ std::map Behavior::GetParameterNames() const { std::map parameters; - auto tableData = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = %u;", - this->m_behaviorId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); + query.bind(1, (int) this->m_behaviorId); + + auto tableData = query.execQuery(); while (!tableData.eof()) { diff --git a/dGame/dBehaviors/SwitchMultipleBehavior.cpp b/dGame/dBehaviors/SwitchMultipleBehavior.cpp index 062ae9f0..93662060 100644 --- a/dGame/dBehaviors/SwitchMultipleBehavior.cpp +++ b/dGame/dBehaviors/SwitchMultipleBehavior.cpp @@ -40,12 +40,14 @@ void SwitchMultipleBehavior::Calculate(BehaviorContext* context, RakNet::BitStre } void SwitchMultipleBehavior::Load() { - auto result = CDClientDatabase::ExecuteQueryWithArgs( + auto query = CDClientDatabase::CreatePreppedStmt( "SELECT replace(bP1.parameterID, 'behavior ', '') as key, bP1.value as behavior, " - "(select bP2.value FROM BehaviorParameter bP2 WHERE bP2.behaviorID = %u AND bP2.parameterID LIKE 'value %' " + "(select bP2.value FROM BehaviorParameter bP2 WHERE bP2.behaviorID = ?1 AND bP2.parameterID LIKE 'value %' " "AND replace(bP1.parameterID, 'behavior ', '') = replace(bP2.parameterID, 'value ', '')) as value " - "FROM BehaviorParameter bP1 WHERE bP1.behaviorID = %u AND bP1.parameterID LIKE 'behavior %';", - this->m_behaviorId, this->m_behaviorId); + "FROM BehaviorParameter bP1 WHERE bP1.behaviorID = ?1 AND bP1.parameterID LIKE 'behavior %';"); + query.bind(1, (int) this->m_behaviorId); + + auto result = query.execQuery(); while (!result.eof()) { const auto behavior_id = static_cast(result.getFloatField(1)); diff --git a/dGame/dComponents/BaseCombatAIComponent.cpp b/dGame/dComponents/BaseCombatAIComponent.cpp index 994f49c4..4d82036f 100644 --- a/dGame/dComponents/BaseCombatAIComponent.cpp +++ b/dGame/dComponents/BaseCombatAIComponent.cpp @@ -35,9 +35,11 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id) m_SoftTimer = 5.0f; //Grab the aggro information from BaseCombatAI: - auto componentResult = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT aggroRadius, tetherSpeed, pursuitSpeed, softTetherRadius, hardTetherRadius FROM BaseCombatAIComponent WHERE id = %u;", - id); + auto componentQuery = CDClientDatabase::CreatePreppedStmt( + "SELECT aggroRadius, tetherSpeed, pursuitSpeed, softTetherRadius, hardTetherRadius FROM BaseCombatAIComponent WHERE id = ?;"); + componentQuery.bind(1, (int) id); + + auto componentResult = componentQuery.execQuery(); if (!componentResult.eof()) { @@ -62,9 +64,11 @@ BaseCombatAIComponent::BaseCombatAIComponent(Entity* parent, const uint32_t id) /* * Find skills */ - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT skillID, cooldown, behaviorID FROM SkillBehavior WHERE skillID IN (SELECT skillID FROM ObjectSkills WHERE objectTemplate = %d);", - parent->GetLOT()); + auto skillQuery = CDClientDatabase::CreatePreppedStmt( + "SELECT skillID, cooldown, behaviorID FROM SkillBehavior WHERE skillID IN (SELECT skillID FROM ObjectSkills WHERE objectTemplate = ?);"); + skillQuery.bind(1, (int) parent->GetLOT()); + + auto result = skillQuery.execQuery(); while (!result.eof()) { const auto skillId = static_cast(result.getIntField(0)); diff --git a/dGame/dComponents/BuffComponent.cpp b/dGame/dComponents/BuffComponent.cpp index afa5b321..9c12e87d 100644 --- a/dGame/dComponents/BuffComponent.cpp +++ b/dGame/dComponents/BuffComponent.cpp @@ -371,10 +371,12 @@ const std::vector& BuffComponent::GetBuffParameters(int32_t buffI return pair->second; } - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT * FROM BuffParameters WHERE BuffID = %d;", - buffId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT * FROM BuffParameters WHERE BuffID = ?;"); + query.bind(1, (int) buffId); + auto result = query.execQuery(); + std::vector parameters {}; while (!result.eof()) diff --git a/dGame/dComponents/DestroyableComponent.cpp b/dGame/dComponents/DestroyableComponent.cpp index 91ff8b6e..1afd4b58 100644 --- a/dGame/dComponents/DestroyableComponent.cpp +++ b/dGame/dComponents/DestroyableComponent.cpp @@ -373,8 +373,11 @@ void DestroyableComponent::AddFaction(const int32_t factionID, const bool ignore m_FactionIDs.push_back(factionID); m_DirtyHealth = true; - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT enemyList FROM Factions WHERE faction = %d;", factionID); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT enemyList FROM Factions WHERE faction = ?;"); + query.bind(1, (int) factionID); + + auto result = query.execQuery(); if (result.eof()) return; diff --git a/dGame/dComponents/InventoryComponent.cpp b/dGame/dComponents/InventoryComponent.cpp index 1f9b4cc6..bedb912d 100644 --- a/dGame/dComponents/InventoryComponent.cpp +++ b/dGame/dComponents/InventoryComponent.cpp @@ -50,18 +50,18 @@ InventoryComponent::InventoryComponent(Entity* parent, tinyxml2::XMLDocument* do auto items = inventoryComponentTable->Query([=](const CDInventoryComponent entry) { return entry.id == componentId; }); auto slot = 0u; - + for (const auto& item : items) { if (!item.equip || !Inventory::IsValidItem(item.itemid)) { continue; } - + const LWOOBJID id = ObjectIDManager::Instance()->GenerateObjectID(); const auto& info = Inventory::FindItemComponent(item.itemid); - + UpdateSlot(info.equipLocation, { id, static_cast(item.itemid), item.count, slot++ }); } } @@ -86,7 +86,7 @@ Inventory* InventoryComponent::GetInventory(const eInventoryType type) case eInventoryType::VAULT_ITEMS: size = 40u; break; - + default: break; } @@ -173,7 +173,7 @@ void InventoryComponent::AddItem( auto* missions = static_cast(this->m_Parent->GetComponent(COMPONENT_TYPE_MISSION)); auto* inventory = GetInventory(inventoryType); - + if (!config.empty() || bound) { const auto slot = inventory->FindEmptySlot(); @@ -184,7 +184,7 @@ void InventoryComponent::AddItem( return; } - + auto* item = new Item(lot, inventory, slot, count, config, parent, showFlyingLoot, isModMoveAndEquip, subKey, bound); if (missions != nullptr && !IsTransferInventory(inventoryType)) @@ -196,7 +196,7 @@ void InventoryComponent::AddItem( } const auto info = Inventory::FindItemComponent(lot); - + auto left = count; int32_t outOfSpace = 0; @@ -211,7 +211,7 @@ void InventoryComponent::AddItem( { stack = 1; } - + auto* existing = FindItemByLot(lot, inventoryType); if (existing != nullptr) @@ -235,7 +235,7 @@ void InventoryComponent::AddItem( const auto size = std::min(left, stack); left -= size; - + int32_t slot; if (preferredSlot != -1 && inventory->IsSlotEmpty(preferredSlot)) @@ -248,7 +248,7 @@ void InventoryComponent::AddItem( { slot = inventory->FindEmptySlot(); } - + if (slot == -1) { auto* player = dynamic_cast(GetParent()); @@ -271,9 +271,9 @@ void InventoryComponent::AddItem( { GameMessages::SendDropClientLoot(this->m_Parent, this->m_Parent->GetObjectID(), lot, 0, this->m_Parent->GetPosition(), 1); } - + break; - + default: break; } @@ -323,7 +323,7 @@ void InventoryComponent::RemoveItem(const LOT lot, const uint32_t count, eInvent { break; } - + const auto delta = std::min(left, item->GetCount()); item->SetCount(item->GetCount() - delta); @@ -338,11 +338,11 @@ void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType in { return; } - + auto* origin = item->GetInventory(); - + const auto lot = item->GetLot(); - + if (item->GetConfig().empty() && !item->GetBound() || (item->GetBound() && item->GetInfo().isBOP)) { auto left = std::min(count, origin->GetLotCount(lot)); @@ -380,7 +380,7 @@ void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType in { config.push_back(data->Copy()); } - + const auto delta = std::min(item->GetCount(), count); AddItem(lot, delta, inventory, config, LWOOBJID_EMPTY, showFlyingLot, isModMoveAndEquip, LWOOBJID_EMPTY, origin->GetType(), 0, item->GetBound(), preferredSlot); @@ -438,7 +438,7 @@ Item* InventoryComponent::FindItemByLot(const LOT lot, eInventoryType inventoryT return inventory->FindItemByLot(lot, ignoreEquipped, ignoreBound); } -Item* InventoryComponent::FindItemBySubKey(LWOOBJID id, eInventoryType inventoryType) +Item* InventoryComponent::FindItemBySubKey(LWOOBJID id, eInventoryType inventoryType) { if (inventoryType == INVALID) { @@ -597,7 +597,7 @@ void InventoryComponent::LoadXml(tinyxml2::XMLDocument* document) unsigned int count; bool bound; LWOOBJID subKey = LWOOBJID_EMPTY; - + itemElement->QueryAttribute("id", &id); itemElement->QueryAttribute("l", &lot); itemElement->QueryAttribute("eq", &equipped); @@ -608,23 +608,23 @@ void InventoryComponent::LoadXml(tinyxml2::XMLDocument* document) // Begin custom xml auto parent = LWOOBJID_EMPTY; - + itemElement->QueryAttribute("parent", &parent); // End custom xml std::vector config; auto* extraInfo = itemElement->FirstChildElement("x"); - + if (extraInfo) { std::string modInfo = extraInfo->Attribute("ma"); - + LDFBaseData* moduleAssembly = new LDFData(u"assemblyPartLOTs", GeneralUtils::ASCIIToUTF16(modInfo.substr(2, modInfo.size() - 1))); - + config.push_back(moduleAssembly); } - + const auto* item = new Item(id, lot, inventory, slot, count, bound, config, parent, subKey); if (equipped) @@ -702,7 +702,7 @@ void InventoryComponent::UpdateXml(tinyxml2::XMLDocument* document) bags->LinkEndChild(bag); } - + auto* items = inventoryElement->FirstChildElement("items"); if (items == nullptr) @@ -756,10 +756,10 @@ void InventoryComponent::UpdateXml(tinyxml2::XMLDocument* document) itemElement->LinkEndChild(extraInfo); } - + bagElement->LinkEndChild(itemElement); } - + items->LinkEndChild(bagElement); } } @@ -769,13 +769,13 @@ void InventoryComponent::Serialize(RakNet::BitStream* outBitStream, const bool b if (bIsInitialUpdate || m_Dirty) { outBitStream->Write(true); - + outBitStream->Write(m_Equipped.size()); for (const auto& pair : m_Equipped) { const auto item = pair.second; - + if (bIsInitialUpdate) { AddItemSkills(item.lot); @@ -783,19 +783,19 @@ void InventoryComponent::Serialize(RakNet::BitStream* outBitStream, const bool b outBitStream->Write(item.id); outBitStream->Write(item.lot); - + outBitStream->Write0(); - + outBitStream->Write(item.count > 0); if (item.count > 0) outBitStream->Write(item.count); - + outBitStream->Write(item.slot != 0); if (item.slot != 0) outBitStream->Write(item.slot); - + outBitStream->Write0(); - + outBitStream->Write0(); //TODO: This is supposed to be true and write the assemblyPartLOTs when they're present. - + outBitStream->Write1(); } @@ -805,7 +805,7 @@ void InventoryComponent::Serialize(RakNet::BitStream* outBitStream, const bool b { outBitStream->Write(false); } - + outBitStream->Write(false); } @@ -814,7 +814,7 @@ void InventoryComponent::ResetFlags() m_Dirty = false; } -void InventoryComponent::Update(float deltaTime) +void InventoryComponent::Update(float deltaTime) { for (auto* set : m_Itemsets) { @@ -843,7 +843,7 @@ void InventoryComponent::UpdateSlot(const std::string& location, EquippedItem it UnEquipItem(old); } } - + m_Equipped.insert_or_assign(location, item); m_Dirty = true; @@ -855,14 +855,14 @@ void InventoryComponent::RemoveSlot(const std::string& location) { return; } - + m_Equipped.erase(location); m_Dirty = true; } void InventoryComponent::EquipItem(Item* item, const bool skipChecks) -{ +{ if (!Inventory::IsValidItem(item->GetLot())) { return; @@ -908,7 +908,7 @@ void InventoryComponent::EquipItem(Item* item, const bool skipChecks) const auto building = character->GetBuildMode(); const auto type = static_cast(item->GetInfo().itemType); - + if (item->GetLot() == 8092 && m_Parent->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER) { EntityInfo info {}; @@ -976,7 +976,7 @@ void InventoryComponent::EquipItem(Item* item, const bool skipChecks) { return; } - + if (type == ITEM_TYPE_LOOT_MODEL || type == ITEM_TYPE_VEHICLE) { return; @@ -995,12 +995,12 @@ void InventoryComponent::EquipItem(Item* item, const bool skipChecks) const auto lot = item->GetLot(); CheckItemSet(lot); - + for (auto* set : m_Itemsets) { set->OnEquip(lot); } - + if (lot == 1727) GameMessages::SendSetJetpackMode(m_Parent, false, true, false); if (lot == 7292) GameMessages::SendSetJetpackMode(m_Parent, true, true, false); if (lot == 14442) GameMessages::SendSetJetpackMode(m_Parent, false, true, true); @@ -1011,11 +1011,11 @@ void InventoryComponent::EquipItem(Item* item, const bool skipChecks) } GenerateProxies(item); - + UpdateSlot(item->GetInfo().equipLocation, { item->GetId(), item->GetLot(), item->GetCount(), item->GetSlot() }); ApplyBuff(item->GetLot()); - + AddItemSkills(item->GetLot()); EntityManager::Instance()->SerializeEntity(m_Parent); @@ -1029,7 +1029,7 @@ void InventoryComponent::UnEquipItem(Item* item) } const auto lot = item->GetLot(); - + if (!Inventory::IsValidItem(lot)) { return; @@ -1047,11 +1047,11 @@ void InventoryComponent::UnEquipItem(Item* item) if (lot == 14442) GameMessages::SendSetJetpackMode(m_Parent, false, false, true); RemoveBuff(item->GetLot()); - + RemoveItemSkills(item->GetLot()); RemoveSlot(item->GetInfo().equipLocation); - + PurgeProxies(item); EntityManager::Instance()->SerializeEntity(m_Parent); @@ -1069,7 +1069,7 @@ void InventoryComponent::ApplyBuff(const LOT lot) const const auto buffs = FindBuffs(lot, true); for (const auto buff : buffs) - { + { SkillComponent::HandleUnmanaged(buff, m_Parent->GetObjectID()); } } @@ -1142,10 +1142,13 @@ void InventoryComponent::CheckItemSet(const LOT lot) { return; } - std::cout << "INVENTORY CHECK" << std::endl; - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT setID FROM ItemSets WHERE itemIDs LIKE '%%%d%%';", - lot); + const std::string lot_query = "%" + std::to_string(lot) + "%"; + + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT setID FROM ItemSets WHERE itemIDs LIKE ?;"); + query.bind(1, lot_query.c_str()); + + auto result = query.execQuery(); while (!result.eof()) { const auto id = result.getIntField(0); @@ -1173,11 +1176,11 @@ void InventoryComponent::CheckItemSet(const LOT lot) { } m_ItemSetsChecked.push_back(lot); - + result.finalize(); } -void InventoryComponent::SetConsumable(LOT lot) +void InventoryComponent::SetConsumable(LOT lot) { m_Consumable = lot; } @@ -1197,13 +1200,13 @@ void InventoryComponent::AddItemSkills(const LOT lot) { return; } - + const auto index = m_Skills.find(slot); if (index != m_Skills.end()) { const auto old = index->second; - + GameMessages::SendRemoveSkill(m_Parent, old); } @@ -1213,7 +1216,7 @@ void InventoryComponent::AddItemSkills(const LOT lot) { return; } - + GameMessages::SendAddSkill(m_Parent, skill, static_cast(slot)); m_Skills.insert_or_assign(slot, skill); @@ -1222,14 +1225,14 @@ void InventoryComponent::AddItemSkills(const LOT lot) void InventoryComponent::RemoveItemSkills(const LOT lot) { const auto info = Inventory::FindItemComponent(lot); - + const auto slot = FindBehaviorSlot(static_cast(info.itemType)); - + if (slot == BehaviorSlot::Invalid) { return; } - + const auto index = m_Skills.find(slot); if (index == m_Skills.end()) @@ -1242,7 +1245,7 @@ void InventoryComponent::RemoveItemSkills(const LOT lot) GameMessages::SendRemoveSkill(m_Parent, old); m_Skills.erase(slot); - + if (slot == BehaviorSlot::Primary) { m_Skills.insert_or_assign(BehaviorSlot::Primary, 1); @@ -1251,7 +1254,7 @@ void InventoryComponent::RemoveItemSkills(const LOT lot) } } -void InventoryComponent::TriggerPassiveAbility(PassiveAbilityTrigger trigger) +void InventoryComponent::TriggerPassiveAbility(PassiveAbilityTrigger trigger) { for (auto* set : m_Itemsets) { @@ -1278,7 +1281,7 @@ bool InventoryComponent::HasAnyPassive(const std::vectorGetObjectID()); @@ -1288,7 +1291,7 @@ void InventoryComponent::DespawnPet() } } -void InventoryComponent::SpawnPet(Item* item) +void InventoryComponent::SpawnPet(Item* item) { auto* current = PetComponent::GetActivePet(m_Parent->GetObjectID()); @@ -1307,11 +1310,11 @@ void InventoryComponent::SpawnPet(Item* item) info.pos = m_Parent->GetPosition(); info.rot = NiQuaternion::IDENTITY; info.spawnerID = m_Parent->GetObjectID(); - + auto* pet = EntityManager::Instance()->CreateEntity(info); auto* petComponent = pet->GetComponent(); - + if (petComponent != nullptr) { petComponent->Activate(item); @@ -1320,7 +1323,7 @@ void InventoryComponent::SpawnPet(Item* item) EntityManager::Instance()->ConstructEntity(pet); } -void InventoryComponent::SetDatabasePet(LWOOBJID id, const DatabasePet& data) +void InventoryComponent::SetDatabasePet(LWOOBJID id, const DatabasePet& data) { m_Pets.insert_or_assign(id, data); } @@ -1341,7 +1344,7 @@ bool InventoryComponent::IsPet(LWOOBJID id) const return pair != m_Pets.end(); } -void InventoryComponent::RemoveDatabasePet(LWOOBJID id) +void InventoryComponent::RemoveDatabasePet(LWOOBJID id) { m_Pets.erase(id); } @@ -1364,7 +1367,7 @@ BehaviorSlot InventoryComponent::FindBehaviorSlot(const eItemType type) } } -bool InventoryComponent::IsTransferInventory(eInventoryType type) +bool InventoryComponent::IsTransferInventory(eInventoryType type) { return type == VENDOR_BUYBACK || type == VAULT_ITEMS || type == VAULT_MODELS || type == TEMP_ITEMS || type == TEMP_MODELS; } @@ -1415,12 +1418,12 @@ std::vector InventoryComponent::FindBuffs(const LOT lot, bool castOnEq continue; } - + if (missions != nullptr && castOnEquip) { missions->Progress(MissionTaskType::MISSION_TASK_TYPE_SKILL, result.skillID); } - + buffs.push_back(static_cast(entry.behaviorID)); } } @@ -1428,18 +1431,18 @@ std::vector InventoryComponent::FindBuffs(const LOT lot, bool castOnEq return buffs; } -void InventoryComponent::SetNPCItems(const std::vector& items) +void InventoryComponent::SetNPCItems(const std::vector& items) { m_Equipped.clear(); auto slot = 0u; - + for (const auto& item : items) { const LWOOBJID id = ObjectIDManager::Instance()->GenerateObjectID(); const auto& info = Inventory::FindItemComponent(item); - + UpdateSlot(info.equipLocation, { id, static_cast(item), 1, slot++ }, true); } @@ -1474,9 +1477,9 @@ std::vector InventoryComponent::GenerateProxies(Item* parent) { return proxies; } - + subItems.erase(std::remove_if(subItems.begin(), subItems.end(), ::isspace), subItems.end()); - + std::stringstream stream(subItems); std::string segment; std::vector lots; @@ -1491,7 +1494,7 @@ std::vector InventoryComponent::GenerateProxies(Item* parent) { Game::logger->Log("InventoryComponent", "Failed to parse proxy (%s): (%s)!\n", segment.c_str(), exception.what()); } - } + } for (const auto lot : lots) { @@ -1508,7 +1511,7 @@ std::vector InventoryComponent::GenerateProxies(Item* parent) proxies.push_back(proxy); } - + return proxies; } @@ -1517,7 +1520,7 @@ std::vector InventoryComponent::FindProxies(const LWOOBJID parent) auto* inventory = GetInventory(ITEM_SETS); std::vector proxies; - + for (const auto& pair : inventory->GetItems()) { auto* item = pair.second; @@ -1559,7 +1562,7 @@ bool InventoryComponent::IsParentValid(Item* root) } const auto id = root->GetId(); - + for (const auto& pair : m_Inventories) { const auto items = pair.second->GetItems(); @@ -1581,7 +1584,7 @@ bool InventoryComponent::IsParentValid(Item* root) void InventoryComponent::CheckProxyIntegrity() { std::vector dead; - + for (const auto& pair : m_Inventories) { const auto& items = pair.second->GetItems(); @@ -1596,7 +1599,7 @@ void InventoryComponent::CheckProxyIntegrity() { continue; } - + if (IsValidProxy(parent)) { continue; @@ -1621,7 +1624,7 @@ void InventoryComponent::CheckProxyIntegrity() for (const auto& candidate : items) { auto* item = candidate.second; - + const auto parent = item->GetParent(); if (parent != LWOOBJID_EMPTY) @@ -1653,7 +1656,7 @@ void InventoryComponent::CheckProxyIntegrity() void InventoryComponent::PurgeProxies(Item* item) { const auto root = item->GetParent(); - + if (root != LWOOBJID_EMPTY) { item = FindItemById(root); @@ -1662,7 +1665,7 @@ void InventoryComponent::PurgeProxies(Item* item) { UnEquipItem(item); } - + return; } @@ -1676,7 +1679,7 @@ void InventoryComponent::PurgeProxies(Item* item) } } -void InventoryComponent::LoadPetXml(tinyxml2::XMLDocument* document) +void InventoryComponent::LoadPetXml(tinyxml2::XMLDocument* document) { auto* petInventoryElement = document->FirstChildElement("obj")->FirstChildElement("pet"); @@ -1694,7 +1697,7 @@ void InventoryComponent::LoadPetXml(tinyxml2::XMLDocument* document) LWOOBJID id; LOT lot; int32_t moderationStatus; - + petElement->QueryAttribute("id", &id); petElement->QueryAttribute("l", &lot); petElement->QueryAttribute("m", &moderationStatus); @@ -1711,7 +1714,7 @@ void InventoryComponent::LoadPetXml(tinyxml2::XMLDocument* document) } } -void InventoryComponent::UpdatePetXml(tinyxml2::XMLDocument* document) +void InventoryComponent::UpdatePetXml(tinyxml2::XMLDocument* document) { auto* petInventoryElement = document->FirstChildElement("obj")->FirstChildElement("pet"); @@ -1733,8 +1736,7 @@ void InventoryComponent::UpdatePetXml(tinyxml2::XMLDocument* document) petElement->SetAttribute("m", pet.second.moderationState); petElement->SetAttribute("n", pet.second.name.c_str()); petElement->SetAttribute("t", 0); - + petInventoryElement->LinkEndChild(petElement); } } - diff --git a/dGame/dComponents/MissionComponent.cpp b/dGame/dComponents/MissionComponent.cpp index 5945394b..1b809f48 100644 --- a/dGame/dComponents/MissionComponent.cpp +++ b/dGame/dComponents/MissionComponent.cpp @@ -450,8 +450,11 @@ const std::vector& MissionComponent::QueryAchievements(MissionTaskType } bool MissionComponent::RequiresItem(const LOT lot) { - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT type FROM Objects WHERE id = %d;", lot); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT type FROM Objects WHERE id = ?;"); + query.bind(1, (int) lot); + + auto result = query.execQuery(); if (result.eof()) { return false; diff --git a/dGame/dComponents/PetComponent.cpp b/dGame/dComponents/PetComponent.cpp index 8c51791a..b31cd35f 100644 --- a/dGame/dComponents/PetComponent.cpp +++ b/dGame/dComponents/PetComponent.cpp @@ -167,9 +167,11 @@ void PetComponent::OnUse(Entity* originator) std::string buildFile; if (cached == buildCache.end()) { - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT ValidPiecesLXF, PuzzleModelLot, Timelimit, NumValidPieces, imagCostPerBuild FROM TamingBuildPuzzles WHERE NPCLot = %d;", - m_Parent->GetLOT()); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT ValidPiecesLXF, PuzzleModelLot, Timelimit, NumValidPieces, imagCostPerBuild FROM TamingBuildPuzzles WHERE NPCLot = ?;"); + query.bind(1, (int) m_Parent->GetLOT()); + + auto result = query.execQuery(); if (result.eof()) { diff --git a/dGame/dComponents/PropertyManagementComponent.cpp b/dGame/dComponents/PropertyManagementComponent.cpp index 543f99a2..d685e8e9 100644 --- a/dGame/dComponents/PropertyManagementComponent.cpp +++ b/dGame/dComponents/PropertyManagementComponent.cpp @@ -40,9 +40,11 @@ PropertyManagementComponent::PropertyManagementComponent(Entity* parent) : Compo const auto zoneId = worldId.GetMapID(); const auto cloneId = worldId.GetCloneID(); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT id FROM PropertyTemplate WHERE mapID = %d;", - (int) zoneId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT id FROM PropertyTemplate WHERE mapID = ?;"); + query.bind(1, (int) zoneId); + + auto result = query.execQuery(); if (result.eof() || result.fieldIsNull(0)) { @@ -96,9 +98,11 @@ std::vector PropertyManagementComponent::GetPaths() const { const auto zoneId = dZoneManager::Instance()->GetZone()->GetWorldID(); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT path FROM PropertyTemplate WHERE mapID = %u;", - zoneId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT path FROM PropertyTemplate WHERE mapID = ?;"); + query.bind(1, (int) zoneId); + + auto result = query.execQuery(); std::vector paths {}; diff --git a/dGame/dComponents/RenderComponent.cpp b/dGame/dComponents/RenderComponent.cpp index b4787d40..faec4ab6 100644 --- a/dGame/dComponents/RenderComponent.cpp +++ b/dGame/dComponents/RenderComponent.cpp @@ -199,9 +199,13 @@ void RenderComponent::PlayEffect(const int32_t effectId, const std::u16string& e } const std::string effectType_str = GeneralUtils::UTF16ToWTF8(effectType); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT animation_length FROM Animations WHERE animation_type IN (SELECT animationName FROM BehaviorEffect WHERE effectID = %d AND effectType = %Q);", - effectId, effectType_str.c_str()); + + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT animation_length FROM Animations WHERE animation_type IN (SELECT animationName FROM BehaviorEffect WHERE effectID = ? AND effectType = ?);"); + query.bind(1, effectId); + query.bind(2, effectType_str.c_str()); + + auto result = query.execQuery(); if (result.eof() || result.fieldIsNull(0)) { result.finalize(); diff --git a/dGame/dComponents/RocketLaunchpadControlComponent.cpp b/dGame/dComponents/RocketLaunchpadControlComponent.cpp index a117fffc..507c75cb 100644 --- a/dGame/dComponents/RocketLaunchpadControlComponent.cpp +++ b/dGame/dComponents/RocketLaunchpadControlComponent.cpp @@ -18,10 +18,12 @@ #include "PacketUtils.h" RocketLaunchpadControlComponent::RocketLaunchpadControlComponent(Entity* parent, int rocketId) : Component(parent) { - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT targetZone, defaultZoneID, targetScene, altLandingPrecondition, altLandingSpawnPointName FROM RocketLaunchpadControlComponent WHERE id = %d;", - rocketId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT targetZone, defaultZoneID, targetScene, altLandingPrecondition, altLandingSpawnPointName FROM RocketLaunchpadControlComponent WHERE id = ?;"); + query.bind(1, rocketId); + auto result = query.execQuery(); + if (!result.eof() && !result.fieldIsNull(0)) { m_TargetZone = result.getIntField(0); diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index 4396c669..b5896d16 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -86,9 +86,11 @@ void SkillComponent::SyncPlayerProjectile(const LWOOBJID projectileId, RakNet::B const auto sync_entry = this->m_managedProjectiles.at(index); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = %d);", - sync_entry.lot); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = ?);"); + query.bind(1, (int) sync_entry.lot); + + auto result = query.execQuery(); if (result.eof()) { Game::logger->Log("SkillComponent", "Failed to find skill id for (%i)!\n", sync_entry.lot); @@ -434,9 +436,10 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry) return; } - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = %d);", - entry.lot); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = ?);"); + query.bind(1, (int) entry.lot); + auto result = query.execQuery(); if (result.eof()) { Game::logger->Log("SkillComponent", "Failed to find skill id for (%i)!\n", entry.lot); diff --git a/dGame/dGameMessages/GameMessages.cpp b/dGame/dGameMessages/GameMessages.cpp index 0e30244b..5b6b8a8c 100644 --- a/dGame/dGameMessages/GameMessages.cpp +++ b/dGame/dGameMessages/GameMessages.cpp @@ -2509,9 +2509,11 @@ void GameMessages::HandleBBBSaveRequest(RakNet::BitStream* inStream, Entity* ent const auto zoneId = worldId.GetMapID(); const auto cloneId = worldId.GetCloneID(); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT id FROM PropertyTemplate WHERE mapID = %d;", - (int) zoneId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT id FROM PropertyTemplate WHERE mapID = ?;"); + query.bind(1, (int) zoneId); + + auto result = query.execQuery(); if (result.eof() || result.fieldIsNull(0)) { return; diff --git a/dGame/dInventory/Item.cpp b/dGame/dInventory/Item.cpp index 92a208cb..77256a20 100644 --- a/dGame/dInventory/Item.cpp +++ b/dGame/dInventory/Item.cpp @@ -386,9 +386,11 @@ void Item::DisassembleModel() const auto componentId = table->GetByIDAndType(GetLot(), COMPONENT_TYPE_RENDER); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT render_asset FROM RenderComponent WHERE id = %d;", - componentId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT render_asset FROM RenderComponent WHERE id = ?;"); + query.bind(1, (int) componentId); + + auto result = query.execQuery(); if (result.eof()) { diff --git a/dGame/dInventory/ItemSet.cpp b/dGame/dInventory/ItemSet.cpp index 93e86a81..70b77a70 100644 --- a/dGame/dInventory/ItemSet.cpp +++ b/dGame/dInventory/ItemSet.cpp @@ -15,9 +15,11 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) this->m_PassiveAbilities = ItemSetPassiveAbility::FindAbilities(id, m_InventoryComponent->GetParent(), this); - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT skillSetWith2, skillSetWith3, skillSetWith4, skillSetWith5, skillSetWith6, itemIDs FROM ItemSets WHERE setID = %u;", - id); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT skillSetWith2, skillSetWith3, skillSetWith4, skillSetWith5, skillSetWith6, itemIDs FROM ItemSets WHERE setID = ?;"); + query.bind(1, (int) id); + + auto result = query.execQuery(); if (result.eof()) { @@ -31,9 +33,11 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) continue; } - auto skillResult = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT SkillID FROM ItemSetSkills WHERE SkillSetID = %d;", - result.getIntField(i)); + auto skillQuery = CDClientDatabase::CreatePreppedStmt( + "SELECT SkillID FROM ItemSetSkills WHERE SkillSetID = ?;"); + skillQuery.bind(1, result.getIntField(i)); + + auto skillResult = skillQuery.execQuery(); if (skillResult.eof()) { diff --git a/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index 5b68685c..da149498 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -16,9 +16,11 @@ std::map Preconditions::cache = {}; Precondition::Precondition(const uint32_t condition) { - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = %u;", - condition); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = ?;"); + query.bind(1, (int) condition); + + auto result = query.execQuery(); if (result.eof()) { diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index fa601946..feb85d1a 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -1243,11 +1243,13 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit } if (chatCommand == "lookup" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() == 1) { - std::string query = "%" + args[0] + "%"; - const char* query_cstr = query.c_str(); - auto tables = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT `id`, `name` FROM `Objects` WHERE `displayName` LIKE %Q OR `name` LIKE %Q OR `description` LIKE %Q LIMIT 50", - query_cstr, query_cstr, query_cstr); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT `id`, `name` FROM `Objects` WHERE `displayName` LIKE ?1 OR `name` LIKE ?1 OR `description` LIKE ?1 LIMIT 50"); + + const std::string query_text = "%" + args[0] + "%"; + query.bind(1, query_text.c_str()); + + auto tables = query.execQuery(); while (!tables.eof()) { std::string message = std::to_string(tables.getIntField(0)) + " - " + tables.getStringField(1); diff --git a/dWorldServer/WorldServer.cpp b/dWorldServer/WorldServer.cpp index ce3774e9..1c7f42a7 100644 --- a/dWorldServer/WorldServer.cpp +++ b/dWorldServer/WorldServer.cpp @@ -1059,9 +1059,11 @@ void HandlePacket(Packet* packet) { const auto zoneId = Game::server->GetZoneID(); const auto cloneId = g_CloneID; - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT id FROM PropertyTemplate WHERE mapID = %u;", - zoneId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT id FROM PropertyTemplate WHERE mapID = ?;"); + query.bind(1, (int) zoneId); + + auto result = query.execQuery(); if (result.eof() || result.fieldIsNull(0)) { Game::logger->Log("WorldServer", "No property templates found for zone %d, not sending BBB\n", zoneId); diff --git a/dZoneManager/dZoneManager.cpp b/dZoneManager/dZoneManager.cpp index 6ed6c719..181d8310 100644 --- a/dZoneManager/dZoneManager.cpp +++ b/dZoneManager/dZoneManager.cpp @@ -26,9 +26,11 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { LOT zoneControlTemplate = 2365; - auto result = CDClientDatabase::ExecuteQueryWithArgs( - "SELECT zoneControlTemplate, ghostdistance_min, ghostdistance FROM ZoneTable WHERE zoneID = %d;", - (int) zoneID.GetMapID()); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT zoneControlTemplate, ghostdistance_min, ghostdistance FROM ZoneTable WHERE zoneID = ?;"); + query.bind(1, (int) zoneID.GetMapID()); + + auto result = query.execQuery(); if (!result.eof()) { zoneControlTemplate = result.getIntField("zoneControlTemplate", 2365); From 4ff84e073060f8bebacb07a711a01e5a65f45abc Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Sat, 15 Jan 2022 13:37:43 -0500 Subject: [PATCH 6/7] Fix whitespace --- dDatabase/Tables/CDBehaviorParameterTable.cpp | 36 +++++++++---------- dGame/dBehaviors/Behavior.cpp | 8 ++--- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/dDatabase/Tables/CDBehaviorParameterTable.cpp b/dDatabase/Tables/CDBehaviorParameterTable.cpp index 3087ba6d..6044a858 100644 --- a/dDatabase/Tables/CDBehaviorParameterTable.cpp +++ b/dDatabase/Tables/CDBehaviorParameterTable.cpp @@ -4,19 +4,19 @@ //! Constructor CDBehaviorParameterTable::CDBehaviorParameterTable(void) { #ifdef CDCLIENT_CACHE_ALL - auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter"); - while (!tableData.eof()) { - CDBehaviorParameter entry; - entry.behaviorID = tableData.getIntField(0, -1); - entry.parameterID = tableData.getStringField(1, ""); - entry.value = tableData.getFloatField(2, -1.0f); + auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM BehaviorParameter"); + while (!tableData.eof()) { + CDBehaviorParameter entry; + entry.behaviorID = tableData.getIntField(0, -1); + entry.parameterID = tableData.getStringField(1, ""); + entry.value = tableData.getFloatField(2, -1.0f); //Check if we have an entry with this ID: auto it = m_entries.find(entry.behaviorID); if (it != m_entries.end()) { it->second.insert(std::make_pair(entry.parameterID, entry.value)); } - else { + else { //Otherwise, insert it: m_entries.insert(std::make_pair(entry.behaviorID, std::map())); auto jit = m_entries.find(entry.behaviorID); @@ -25,8 +25,8 @@ CDBehaviorParameterTable::CDBehaviorParameterTable(void) { jit->second.insert(std::make_pair(entry.parameterID, entry.value)); } - tableData.nextRow(); - } + tableData.nextRow(); + } tableData.finalize(); #endif @@ -37,10 +37,10 @@ CDBehaviorParameterTable::~CDBehaviorParameterTable(void) { } //! Returns the table's name std::string CDBehaviorParameterTable::GetName(void) const { - return "BehaviorParameter"; + return "BehaviorParameter"; } -float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::string& name) +float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::string& name) { size_t hash = 0; GeneralUtils::hash_combine(hash, behaviorID); @@ -60,16 +60,16 @@ float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::s #ifndef CDCLIENT_CACHE_ALL auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); - query.bind(1, (int) behaviorID); + "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); + query.bind(1, (int) behaviorID); auto tableData = query.execQuery(); m_Entries.insert_or_assign(behaviorID, 0); - + while (!tableData.eof()) { - const std::string parameterID = tableData.getStringField(0, ""); - const float value = tableData.getFloatField(1, 0); + const std::string parameterID = tableData.getStringField(0, ""); + const float value = tableData.getFloatField(1, 0); size_t parameterHash = 0; GeneralUtils::hash_combine(parameterHash, behaviorID); @@ -77,8 +77,8 @@ float CDBehaviorParameterTable::GetEntry(const uint32_t behaviorID, const std::s m_Entries.insert_or_assign(parameterHash, value); - tableData.nextRow(); - } + tableData.nextRow(); + } const auto& it2 = m_Entries.find(hash); if (it2 != m_Entries.end()) { diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 351a4ada..1f666c05 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -276,11 +276,11 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) } BehaviorTemplates Behavior::GetBehaviorTemplate(const uint32_t behaviorId) { - auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT templateID FROM BehaviorTemplate WHERE behaviorID = ?;"); - query.bind(1, (int) behaviorId); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT templateID FROM BehaviorTemplate WHERE behaviorID = ?;"); + query.bind(1, (int) behaviorId); - auto result = query.execQuery(); + auto result = query.execQuery(); // Make sure we do not proceed if we are trying to load an invalid behavior if (result.eof()) From 9dfa401b27d2bdd81ec0ad4f67057556f658977e Mon Sep 17 00:00:00 2001 From: TheMatt2 Date: Sat, 15 Jan 2022 14:02:14 -0500 Subject: [PATCH 7/7] Fix whitespace Convert to tabs and remove trailing whitespace --- dGame/dBehaviors/Behavior.cpp | 10 ++-- dGame/dComponents/DestroyableComponent.cpp | 60 +++++++++---------- .../RocketLaunchpadControlComponent.cpp | 20 +++---- dGame/dComponents/SkillComponent.cpp | 50 ++++++++-------- dGame/dInventory/ItemSet.cpp | 28 ++++----- dGame/dUtilities/Preconditions.cpp | 20 +++---- dGame/dUtilities/SlashCommandHandler.cpp | 6 +- dZoneManager/dZoneManager.cpp | 22 +++---- 8 files changed, 108 insertions(+), 108 deletions(-) diff --git a/dGame/dBehaviors/Behavior.cpp b/dGame/dBehaviors/Behavior.cpp index 1f666c05..668329b1 100644 --- a/dGame/dBehaviors/Behavior.cpp +++ b/dGame/dBehaviors/Behavior.cpp @@ -185,8 +185,8 @@ Behavior* Behavior::CreateBehavior(const uint32_t behaviorId) break; case BehaviorTemplates::BEHAVIOR_JETPACK: break; case BehaviorTemplates::BEHAVIOR_SKILL_EVENT: - behavior = new SkillEventBehavior(behaviorId); - break; + behavior = new SkillEventBehavior(behaviorId); + break; case BehaviorTemplates::BEHAVIOR_CONSUME_ITEM: break; case BehaviorTemplates::BEHAVIOR_SKILL_CAST_FAILED: behavior = new SkillCastFailedBehavior(behaviorId); @@ -414,11 +414,11 @@ Behavior::Behavior(const uint32_t behaviorId) this->m_templateId = BehaviorTemplates::BEHAVIOR_EMPTY; } - auto query = CDClientDatabase::CreatePreppedStmt( + auto query = CDClientDatabase::CreatePreppedStmt( "SELECT templateID, effectID, effectHandle FROM BehaviorTemplate WHERE behaviorID = ?;"); query.bind(1, (int) behaviorId); - auto result = query.execQuery(); + auto result = query.execQuery(); // Make sure we do not proceed if we are trying to load an invalid behavior if (result.eof()) @@ -493,7 +493,7 @@ std::map Behavior::GetParameterNames() const auto query = CDClientDatabase::CreatePreppedStmt( "SELECT parameterID, value FROM BehaviorParameter WHERE behaviorID = ?;"); - query.bind(1, (int) this->m_behaviorId); + query.bind(1, (int) this->m_behaviorId); auto tableData = query.execQuery(); diff --git a/dGame/dComponents/DestroyableComponent.cpp b/dGame/dComponents/DestroyableComponent.cpp index 1afd4b58..ed12a439 100644 --- a/dGame/dComponents/DestroyableComponent.cpp +++ b/dGame/dComponents/DestroyableComponent.cpp @@ -105,7 +105,7 @@ void DestroyableComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsIn outBitStream->Write0(); //Contains info about immunities this object has, but it's left out for now. } - outBitStream->Write(m_DirtyHealth || bIsInitialUpdate); + outBitStream->Write(m_DirtyHealth || bIsInitialUpdate); if (m_DirtyHealth || bIsInitialUpdate) { outBitStream->Write(m_iHealth); outBitStream->Write(m_fMaxHealth); @@ -113,12 +113,12 @@ void DestroyableComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsIn outBitStream->Write(m_fMaxArmor); outBitStream->Write(m_iImagination); outBitStream->Write(m_fMaxImagination); - + outBitStream->Write(m_DamageToAbsorb); outBitStream->Write(IsImmune()); outBitStream->Write(m_IsGMImmune); outBitStream->Write(m_IsShielded); - + outBitStream->Write(m_fMaxHealth); outBitStream->Write(m_fMaxArmor); outBitStream->Write(m_fMaxImagination); @@ -129,14 +129,14 @@ void DestroyableComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsIn } outBitStream->Write(m_IsSmashable); - + if (bIsInitialUpdate) { outBitStream->Write(m_IsDead); outBitStream->Write(m_IsSmashed); - + if (m_IsSmashable) { outBitStream->Write(m_HasBricks); - + if (m_ExplodeFactor != 1.0f) { outBitStream->Write1(); outBitStream->Write(m_ExplodeFactor); @@ -145,10 +145,10 @@ void DestroyableComponent::Serialize(RakNet::BitStream* outBitStream, bool bIsIn } } } - + m_DirtyHealth = false; } - + if (m_DirtyThreatList || bIsInitialUpdate) { outBitStream->Write1(); outBitStream->Write(m_HasThreats); @@ -166,7 +166,7 @@ void DestroyableComponent::LoadFromXML(tinyxml2::XMLDocument* doc) { } auto* buffComponent = m_Parent->GetComponent(); - + if (buffComponent != nullptr) { buffComponent->LoadFromXML(doc); } @@ -186,9 +186,9 @@ void DestroyableComponent::UpdateXml(tinyxml2::XMLDocument* doc) { Game::logger->Log("DestroyableComponent", "Failed to find dest tag!\n"); return; } - + auto* buffComponent = m_Parent->GetComponent(); - + if (buffComponent != nullptr) { buffComponent->UpdateXml(doc); } @@ -233,7 +233,7 @@ void DestroyableComponent::SetMaxHealth(float value, bool playAnim) { args.InsertValue("type", type); GameMessages::SendUIMessageServerToSingleClient(m_Parent, m_Parent->GetParentUser()->GetSystemAddress(), "MaxPlayerBarUpdate", &args); - + delete amount; delete type; } @@ -279,7 +279,7 @@ void DestroyableComponent::SetMaxArmor(float value, bool playAnim) { args.InsertValue("type", type); GameMessages::SendUIMessageServerToSingleClient(m_Parent, m_Parent->GetParentUser()->GetSystemAddress(), "MaxPlayerBarUpdate", &args); - + delete amount; delete type; } @@ -325,7 +325,7 @@ void DestroyableComponent::SetMaxImagination(float value, bool playAnim) { args.InsertValue("type", type); GameMessages::SendUIMessageServerToSingleClient(m_Parent, m_Parent->GetParentUser()->GetSystemAddress(), "MaxPlayerBarUpdate", &args); - + delete amount; delete type; } @@ -340,7 +340,7 @@ void DestroyableComponent::SetDamageToAbsorb(int32_t value) m_DamageToAbsorb = value; } -void DestroyableComponent::SetDamageReduction(int32_t value) +void DestroyableComponent::SetDamageReduction(int32_t value) { m_DirtyHealth = true; m_DamageReduction = value; @@ -352,7 +352,7 @@ void DestroyableComponent::SetIsImmune(bool value) m_ImmuneStacks = value ? 1 : 0; } -void DestroyableComponent::SetIsGMImmune(bool value) +void DestroyableComponent::SetIsGMImmune(bool value) { m_DirtyHealth = true; m_IsGMImmune = value; @@ -375,7 +375,7 @@ void DestroyableComponent::AddFaction(const int32_t factionID, const bool ignore auto query = CDClientDatabase::CreatePreppedStmt( "SELECT enemyList FROM Factions WHERE faction = ?;"); - query.bind(1, (int) factionID); + query.bind(1, (int) factionID); auto result = query.execQuery(); @@ -387,10 +387,10 @@ void DestroyableComponent::AddFaction(const int32_t factionID, const bool ignore std::stringstream ss(list_string); std::string token; - + while (std::getline(ss, token, ',')) { if (token.empty()) continue; - + auto id = std::stoi(token); auto exclude = std::find(m_FactionIDs.begin(), m_FactionIDs.end(), id) != m_FactionIDs.end(); @@ -404,7 +404,7 @@ void DestroyableComponent::AddFaction(const int32_t factionID, const bool ignore { continue; } - + AddEnemyFaction(id); } @@ -521,7 +521,7 @@ bool DestroyableComponent::CheckValidity(const LWOOBJID target, const bool ignor if (quickbuild != nullptr) { const auto state = quickbuild->GetState(); - + if (state != REBUILD_COMPLETED) { return false; @@ -568,7 +568,7 @@ void DestroyableComponent::Imagine(const int32_t deltaImagination) { auto current = static_cast(GetImagination()); const auto max = static_cast(GetMaxImagination()); - + current += deltaImagination; current = std::min(current, max); @@ -641,7 +641,7 @@ void DestroyableComponent::Damage(uint32_t damage, const LWOOBJID source, bool e damage -= absorbDamage; absorb -= absorbDamage; - + const auto armorDamage = std::min(damage, armor); damage -= armorDamage; @@ -663,7 +663,7 @@ void DestroyableComponent::Damage(uint32_t damage, const LWOOBJID source, bool e { EntityManager::Instance()->SerializeEntity(m_Parent); } - + auto* attacker = EntityManager::Instance()->GetEntity(source); m_Parent->OnHit(attacker); m_Parent->OnHitOrHealResult(attacker, sourceDamage); @@ -741,7 +741,7 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType } } } - + const auto isPlayer = m_Parent->IsPlayer(); GameMessages::SendDie( @@ -783,14 +783,14 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType auto* member = EntityManager::Instance()->GetEntity(specificOwner); if (member) LootGenerator::Instance().DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins()); - } + } else { for (const auto memberId : team->members) { // Free for all auto* member = EntityManager::Instance()->GetEntity(memberId); if (member == nullptr) continue; - LootGenerator::Instance().DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins()); + LootGenerator::Instance().DropLoot(member, m_Parent, lootMatrixId, GetMinCoins(), GetMaxCoins()); } } } @@ -821,7 +821,7 @@ void DestroyableComponent::Smash(const LWOOBJID source, const eKillType killType } coinsTotal -= coinsToLoose; - + LootGenerator::Instance().DropLoot(m_Parent, m_Parent, -1, coinsToLoose, coinsToLoose); } @@ -862,7 +862,7 @@ void DestroyableComponent::PopImmunity(int32_t stacks) m_ImmuneStacks -= stacks; } -void DestroyableComponent::FixStats() +void DestroyableComponent::FixStats() { auto* entity = GetParent(); @@ -920,7 +920,7 @@ void DestroyableComponent::FixStats() // Add the stats const auto& info = mission->GetClientInfo(); - + maxHealth += info.reward_maxhealth; maxImagination += info.reward_maximagination; } diff --git a/dGame/dComponents/RocketLaunchpadControlComponent.cpp b/dGame/dComponents/RocketLaunchpadControlComponent.cpp index 507c75cb..2a4c138e 100644 --- a/dGame/dComponents/RocketLaunchpadControlComponent.cpp +++ b/dGame/dComponents/RocketLaunchpadControlComponent.cpp @@ -19,11 +19,11 @@ RocketLaunchpadControlComponent::RocketLaunchpadControlComponent(Entity* parent, int rocketId) : Component(parent) { auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT targetZone, defaultZoneID, targetScene, altLandingPrecondition, altLandingSpawnPointName FROM RocketLaunchpadControlComponent WHERE id = ?;"); + "SELECT targetZone, defaultZoneID, targetScene, altLandingPrecondition, altLandingSpawnPointName FROM RocketLaunchpadControlComponent WHERE id = ?;"); query.bind(1, rocketId); auto result = query.execQuery(); - + if (!result.eof() && !result.fieldIsNull(0)) { m_TargetZone = result.getIntField(0); @@ -59,7 +59,7 @@ void RocketLaunchpadControlComponent::Launch(Entity* originator, LWOOBJID option { return; } - + TellMasterToPrepZone(zone); // This also gets triggered by a proximity monitor + item equip, I will set that up when havok is ready @@ -80,7 +80,7 @@ void RocketLaunchpadControlComponent::Launch(Entity* originator, LWOOBJID option { rocket = inventoryComponent->FindItemById(optionalRocketID); } - + if (rocket == nullptr) { rocket = inventoryComponent->FindItemById(characterComponent->GetLastRocketItemID()); @@ -131,7 +131,7 @@ void RocketLaunchpadControlComponent::Launch(Entity* originator, LWOOBJID option } } } - + // Store the last used rocket item's ID characterComponent->SetLastRocketItemID(rocket->GetId()); @@ -140,13 +140,13 @@ void RocketLaunchpadControlComponent::Launch(Entity* originator, LWOOBJID option character->SaveXMLToDatabase(); SetSelectedMapId(originator->GetObjectID(), zone); - + GameMessages::SendFireEventClientSide(m_Parent->GetObjectID(), originator->GetSystemAddress(), u"RocketEquipped", rocket->GetId(), cloneId, -1, originator->GetObjectID()); rocket->Equip(true); - + GameMessages::SendChangeObjectWorldState(rocket->GetId(), WORLDSTATE_ATTACHED, UNASSIGNED_SYSTEM_ADDRESS); - + EntityManager::Instance()->SerializeEntity(originator); } @@ -167,7 +167,7 @@ void RocketLaunchpadControlComponent::OnProximityUpdate(Entity* entering, std::s // Proximity rockets are handled by item equipment } -void RocketLaunchpadControlComponent::SetSelectedMapId(LWOOBJID player, LWOMAPID mapID) +void RocketLaunchpadControlComponent::SetSelectedMapId(LWOOBJID player, LWOMAPID mapID) { m_SelectedMapIds[player] = mapID; } @@ -181,7 +181,7 @@ LWOMAPID RocketLaunchpadControlComponent::GetSelectedMapId(LWOOBJID player) cons return index->second; } -void RocketLaunchpadControlComponent::SetSelectedCloneId(LWOOBJID player, LWOCLONEID cloneId) +void RocketLaunchpadControlComponent::SetSelectedCloneId(LWOOBJID player, LWOCLONEID cloneId) { m_SelectedCloneIds[player] = cloneId; } diff --git a/dGame/dComponents/SkillComponent.cpp b/dGame/dComponents/SkillComponent.cpp index b5896d16..7e8b20e3 100644 --- a/dGame/dComponents/SkillComponent.cpp +++ b/dGame/dComponents/SkillComponent.cpp @@ -36,11 +36,11 @@ bool SkillComponent::CastPlayerSkill(const uint32_t behaviorId, const uint32_t s auto* behavior = Behavior::CreateBehavior(behaviorId); const auto branch = BehaviorBranchContext(target, 0); - + behavior->Handle(context, bitStream, branch); context->ExecuteUpdates(); - + return !context->failed; } @@ -76,11 +76,11 @@ void SkillComponent::SyncPlayerProjectile(const LWOOBJID projectileId, RakNet::B break; } } - + if (index == -1) { Game::logger->Log("SkillComponent", "Failed to find projectile id (%llu)!\n", projectileId); - + return; } @@ -88,7 +88,7 @@ void SkillComponent::SyncPlayerProjectile(const LWOOBJID projectileId, RakNet::B auto query = CDClientDatabase::CreatePreppedStmt( "SELECT behaviorID FROM SkillBehavior WHERE skillID = (SELECT skillID FROM ObjectSkills WHERE objectTemplate = ?);"); - query.bind(1, (int) sync_entry.lot); + query.bind(1, (int) sync_entry.lot); auto result = query.execQuery(); @@ -112,7 +112,7 @@ void SkillComponent::SyncPlayerProjectile(const LWOOBJID projectileId, RakNet::B { branch.target = target; } - + behavior->Handle(sync_entry.context, bitStream, branch); this->m_managedProjectiles.erase(this->m_managedProjectiles.begin() + index); @@ -126,7 +126,7 @@ void SkillComponent::RegisterPlayerProjectile(const LWOOBJID projectileId, Behav entry.branchContext = branch; entry.lot = lot; entry.id = projectileId; - + this->m_managedProjectiles.push_back(entry); } @@ -138,7 +138,7 @@ void SkillComponent::Update(const float deltaTime) } std::map keep {}; - + for (const auto& pair : this->m_managedBehaviors) { auto* context = pair.second; @@ -147,7 +147,7 @@ void SkillComponent::Update(const float deltaTime) { continue; } - + if (context->clientInitalized) { context->CalculateUpdate(deltaTime); @@ -161,7 +161,7 @@ void SkillComponent::Update(const float deltaTime) if (context->syncEntries.empty() && context->timerEntries.empty()) { auto any = false; - + for (const auto& projectile : this->m_managedProjectiles) { if (projectile.context == context) @@ -177,13 +177,13 @@ void SkillComponent::Update(const float deltaTime) context->Reset(); delete context; - + context = nullptr; continue; } } - + keep.insert_or_assign(pair.first, context); } @@ -251,9 +251,9 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c context->caster = m_Parent->GetObjectID(); context->clientInitalized = clientInitalized; - + context->foundTarget = target != LWOOBJID_EMPTY || ignoreTarget || clientInitalized; - + behavior->Calculate(context, bitStream, { target, 0}); for (auto* script : CppScripts::GetEntityScripts(m_Parent)) { @@ -275,7 +275,7 @@ SkillExecutionResult SkillComponent::CalculateBehavior(const uint32_t skillId, c { // Echo start skill GameMessages::EchoStartSkill start; - + start.iCastType = 0; start.skillID = skillId; start.uiSkillHandle = context->skillUId; @@ -350,7 +350,7 @@ void SkillComponent::CalculateUpdate(const float deltaTime) const auto targetPosition = target->GetPosition(); const auto closestPoint = Vector3::ClosestPointOnLine(entry.lastPosition, position, targetPosition); - + const auto distance = Vector3::DistanceSquared(targetPosition, closestPoint); if (distance > 3 * 3) @@ -396,12 +396,12 @@ void SkillComponent::CalculateUpdate(const float deltaTime) } entry.lastPosition = position; - + managedProjectile = entry; } - + std::vector valid; - + for (auto& entry : this->m_managedProjectiles) { if (entry.calculation) @@ -409,7 +409,7 @@ void SkillComponent::CalculateUpdate(const float deltaTime) if (entry.time >= entry.maxTime) { entry.branchContext.target = LWOOBJID_EMPTY; - + SyncProjectileCalculation(entry); continue; @@ -450,13 +450,13 @@ void SkillComponent::SyncProjectileCalculation(const ProjectileSyncEntry& entry) const auto behaviorId = static_cast(result.getIntField(0)); result.finalize(); - + auto* behavior = Behavior::CreateBehavior(behaviorId); auto* bitStream = new RakNet::BitStream(); behavior->Calculate(entry.context, bitStream, entry.branchContext); - + GameMessages::DoClientProjectileImpact projectileImpact; projectileImpact.sBitStream.assign((char*) bitStream->GetData(), bitStream->GetNumberOfBytesUsed()); @@ -492,19 +492,19 @@ void SkillComponent::HandleUnmanaged(const uint32_t behaviorId, const LWOOBJID t delete bitStream; - delete context; + delete context; } void SkillComponent::HandleUnCast(const uint32_t behaviorId, const LWOOBJID target) { auto* context = new BehaviorContext(target); - + context->caster = target; auto* behavior = Behavior::CreateBehavior(behaviorId); behavior->UnCast(context, { target }); - + delete context; } diff --git a/dGame/dInventory/ItemSet.cpp b/dGame/dInventory/ItemSet.cpp index 70b77a70..feddd757 100644 --- a/dGame/dInventory/ItemSet.cpp +++ b/dGame/dInventory/ItemSet.cpp @@ -25,7 +25,7 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) { return; } - + for (auto i = 0; i < 5; ++i) { if (result.fieldIsNull(i)) @@ -35,7 +35,7 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) auto skillQuery = CDClientDatabase::CreatePreppedStmt( "SELECT SkillID FROM ItemSetSkills WHERE SkillSetID = ?;"); - skillQuery.bind(1, result.getIntField(i)); + skillQuery.bind(1, result.getIntField(i)); auto skillResult = skillQuery.execQuery(); @@ -49,10 +49,10 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) if (skillResult.fieldIsNull(0)) { skillResult.nextRow(); - + continue; } - + const auto skillId = skillResult.getIntField(0); switch (i) @@ -75,7 +75,7 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) default: break; } - + skillResult.nextRow(); } } @@ -83,7 +83,7 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) std::string ids = result.getStringField(5); ids.erase(std::remove_if(ids.begin(), ids.end(), ::isspace), ids.end()); - + std::istringstream stream(ids); std::string token; @@ -99,9 +99,9 @@ ItemSet::ItemSet(const uint32_t id, InventoryComponent* inventoryComponent) m_Items.push_back(value); } } - + m_Equipped = {}; - + for (const auto item : m_Items) { if (inventoryComponent->IsEquipped(item)) @@ -141,11 +141,11 @@ void ItemSet::OnEquip(const LOT lot) auto* skillComponent = m_InventoryComponent->GetParent()->GetComponent(); auto* missionComponent = m_InventoryComponent->GetParent()->GetComponent(); - + for (const auto skill : skillSet) { auto* skillTable = CDClientManager::Instance()->GetTable("SkillBehavior"); - + const auto behaviorId = skillTable->GetSkillByID(skill).behaviorID; missionComponent->Progress(MissionTaskType::MISSION_TASK_TYPE_SKILL, skill); @@ -167,11 +167,11 @@ void ItemSet::OnUnEquip(const LOT lot) { return; } - + const auto& skillSet = GetSkillSet(m_Equipped.size()); m_Equipped.erase(index); - + if (skillSet.empty()) { return; @@ -199,7 +199,7 @@ uint32_t ItemSet::GetID() const return m_ID; } -void ItemSet::Update(float deltaTime) +void ItemSet::Update(float deltaTime) { for (auto& passiveAbility : m_PassiveAbilities) { @@ -207,7 +207,7 @@ void ItemSet::Update(float deltaTime) } } -void ItemSet::TriggerPassiveAbility(PassiveAbilityTrigger trigger) +void ItemSet::TriggerPassiveAbility(PassiveAbilityTrigger trigger) { for (auto& passiveAbility : m_PassiveAbilities) { diff --git a/dGame/dUtilities/Preconditions.cpp b/dGame/dUtilities/Preconditions.cpp index da149498..6921343b 100644 --- a/dGame/dUtilities/Preconditions.cpp +++ b/dGame/dUtilities/Preconditions.cpp @@ -16,18 +16,18 @@ std::map Preconditions::cache = {}; Precondition::Precondition(const uint32_t condition) { - auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = ?;"); + auto query = CDClientDatabase::CreatePreppedStmt( + "SELECT type, targetLOT, targetCount FROM Preconditions WHERE id = ?;"); query.bind(1, (int) condition); - auto result = query.execQuery(); + auto result = query.execQuery(); if (result.eof()) { this->type = PreconditionType::ItemEquipped; this->count = 1; this->values = { 0 }; - + Game::logger->Log("Precondition", "Failed to find precondition of id (%i)!\n", condition); return; @@ -98,11 +98,11 @@ bool Precondition::Check(Entity* player, bool evaluateCosts) const } auto passedAny = false; - + for (const auto value : values) { const auto passed = CheckValue(player, value, evaluateCosts); - + if (passed && any) { return true; @@ -221,7 +221,7 @@ PreconditionExpression::PreconditionExpression(const std::string& conditions) return; } - + std::stringstream a; std::stringstream b; @@ -309,16 +309,16 @@ bool PreconditionExpression::Check(Entity* player, bool evaluateCosts) const { return true; } - + const auto a = Preconditions::Check(player, condition, evaluateCosts); if (!a) { GameMessages::SendNotifyClientFailedPrecondition(player->GetObjectID(), player->GetSystemAddress(), u"", condition); } - + const auto b = next == nullptr ? true : next->Check(player); - + return m_or ? a || b : a && b; } diff --git a/dGame/dUtilities/SlashCommandHandler.cpp b/dGame/dUtilities/SlashCommandHandler.cpp index feb85d1a..966b80eb 100644 --- a/dGame/dUtilities/SlashCommandHandler.cpp +++ b/dGame/dUtilities/SlashCommandHandler.cpp @@ -1242,12 +1242,12 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit EntityManager::Instance()->SerializeEntity(entity); } - if (chatCommand == "lookup" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() == 1) { + if (chatCommand == "lookup" && entity->GetGMLevel() >= GAME_MASTER_LEVEL_DEVELOPER && args.size() == 1) { auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT `id`, `name` FROM `Objects` WHERE `displayName` LIKE ?1 OR `name` LIKE ?1 OR `description` LIKE ?1 LIMIT 50"); + "SELECT `id`, `name` FROM `Objects` WHERE `displayName` LIKE ?1 OR `name` LIKE ?1 OR `description` LIKE ?1 LIMIT 50"); const std::string query_text = "%" + args[0] + "%"; - query.bind(1, query_text.c_str()); + query.bind(1, query_text.c_str()); auto tables = query.execQuery(); diff --git a/dZoneManager/dZoneManager.cpp b/dZoneManager/dZoneManager.cpp index 181d8310..70dcec90 100644 --- a/dZoneManager/dZoneManager.cpp +++ b/dZoneManager/dZoneManager.cpp @@ -16,7 +16,7 @@ dZoneManager* dZoneManager::m_Address = nullptr; void dZoneManager::Initialize(const LWOZONEID& zoneID) { Game::logger->Log("dZoneManager", "Preparing zone: %i/%i/%i\n", zoneID.GetMapID(), zoneID.GetInstanceID(), zoneID.GetCloneID()); - + int64_t startTime = 0; int64_t endTime = 0; @@ -27,7 +27,7 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { LOT zoneControlTemplate = 2365; auto query = CDClientDatabase::CreatePreppedStmt( - "SELECT zoneControlTemplate, ghostdistance_min, ghostdistance FROM ZoneTable WHERE zoneID = ?;"); + "SELECT zoneControlTemplate, ghostdistance_min, ghostdistance FROM ZoneTable WHERE zoneID = ?;"); query.bind(1, (int) zoneID.GetMapID()); auto result = query.execQuery(); @@ -54,7 +54,7 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { m_pZone->Initalize(); endTime = std::chrono::duration_cast(std::chrono::steady_clock::now().time_since_epoch()).count(); - + Game::logger->Log("dZoneManager", "Zone prepared in: %llu ms\n", (endTime - startTime)); VanityUtilities::SpawnVanity(); @@ -62,13 +62,13 @@ void dZoneManager::Initialize(const LWOZONEID& zoneID) { dZoneManager::~dZoneManager() { if (m_pZone) delete m_pZone; - + for (std::pair p : m_Spawners) { if (p.second) { delete p.second; p.second = nullptr; } - + m_Spawners.erase(p.first); } } @@ -130,7 +130,7 @@ void dZoneManager::Update(float deltaTime) { LWOOBJID dZoneManager::MakeSpawner(SpawnerInfo info) { auto objectId = info.spawnerID; - + if (objectId == LWOOBJID_EMPTY) { objectId = ObjectIDManager::Instance()->GenerateObjectID(); @@ -139,18 +139,18 @@ LWOOBJID dZoneManager::MakeSpawner(SpawnerInfo info) info.spawnerID = objectId; } - + auto* spawner = new Spawner(info); EntityInfo entityInfo{}; entityInfo.id = objectId; entityInfo.lot = 176; - + auto* entity = EntityManager::Instance()->CreateEntity(entityInfo, nullptr, nullptr, false, objectId); EntityManager::Instance()->ConstructEntity(entity); - + AddSpawner(objectId, spawner); return objectId; @@ -203,9 +203,9 @@ void dZoneManager::RemoveSpawner(const LWOOBJID id) spawner->Deactivate(); Game::logger->Log("dZoneManager", "Destroying spawner (%llu)\n", id); - + m_Spawners.erase(id); - + delete spawner; }