Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: add context parameter to addString() #1826

Merged
merged 7 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function outfitMemorial.onUse(player, item, fromPosition, target, toPosition, is
for i = 1, 3 do
response:addU16(#goldenOutfitCache[i])
for j = 1, #goldenOutfitCache[i] do
response:addString(goldenOutfitCache[i][j])
response:addString(goldenOutfitCache[i][j], "outfitMemorial.onUse - goldenOutfitCache[i][j]")
end
end

Expand All @@ -52,7 +52,7 @@ function outfitMemorial.onUse(player, item, fromPosition, target, toPosition, is
for i = 1, 3 do
response:addU16(#royalOutfitCache[i])
for j = 1, #royalOutfitCache[i] do
response:addString(royalOutfitCache[i][j])
response:addString(royalOutfitCache[i][j], "outfitMemorial.onUse - royalOutfitCache[i][j]")
end
end

Expand Down
2 changes: 1 addition & 1 deletion data/libs/functions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function Player.sendExtendedOpcode(self, opcode, buffer)
local networkMessage = NetworkMessage()
networkMessage:addByte(0x32)
networkMessage:addByte(opcode)
networkMessage:addString(buffer)
networkMessage:addString(buffer, "Player.sendExtendedOpcode - buffer")
networkMessage:sendToPlayer(self)
networkMessage:delete()
return true
Expand Down
16 changes: 8 additions & 8 deletions data/libs/functions/quests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function Player.sendQuestLog(self)
for questId = 1, #Quests do
if self:questIsStarted(questId) then
msg:addU16(questId)
msg:addString(Quests[questId].name .. (self:questIsCompleted(questId) and " (completed)" or ""))
msg:addString(Quests[questId].name .. (self:questIsCompleted(questId) and " (completed)" or ""), "Player.sendQuestLog")
msg:addByte(self:questIsCompleted(questId))
end
end
Expand All @@ -319,8 +319,8 @@ function Player.sendQuestLine(self, questId)
if self:getClient().version >= 1200 then
msg:addU16(self:getMissionId(questId, missionId))
end
msg:addString(self:getMissionName(questId, missionId))
msg:addString(self:getMissionDescription(questId, missionId))
msg:addString(self:getMissionName(questId, missionId), "Player.sendQuestLine - self:getMissionName(questId, missionId)")
msg:addString(self:getMissionDescription(questId, missionId), "Player.sendQuestLine - self:getMissionDescription(questId, missionId)")
end
end
end
Expand All @@ -338,9 +338,9 @@ function Player.sendTrackedQuests(self, remainingQuests, missions)
msg:addByte(#missions)
for _, mission in ipairs(missions) do
msg:addU16(mission.missionId)
msg:addString(mission.questName)
msg:addString(mission.missionName)
msg:addString(mission.missionDesc)
msg:addString(mission.questName, "Player.sendTrackedQuests - mission.questName")
msg:addString(mission.missionName, "Player.sendTrackedQuests - mission.missionName")
msg:addString(mission.missionDesc, "Player.sendTrackedQuests - mission.missionDesc")
end
msg:sendToPlayer(self)
msg:delete()
Expand All @@ -351,8 +351,8 @@ function Player.sendUpdateTrackedQuest(self, mission)
msg:addByte(0xD0)
msg:addByte(0x00)
msg:addU16(mission.missionId)
msg:addString(mission.missionName)
msg:addString(mission.missionDesc)
msg:addString(mission.missionName, "Player.sendUpdateTrackedQuest - mission.missionName")
msg:addString(mission.missionDesc, "Player.sendUpdateTrackedQuest - mission.missionDesc")
msg:sendToPlayer(self)
msg:delete()
end
Expand Down
2 changes: 1 addition & 1 deletion data/libs/hireling_lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ function Player:sendHirelingOutfitWindow(hireling)
msg:addU16(#availableOutfits)
for _, outfit in ipairs(availableOutfits) do
msg:addU16(outfit.lookType)
msg:addString(outfit.name)
msg:addString(outfit.name, "Player:sendHirelingOutfitWindow - outfit.name")
msg:addByte(0x00) -- addons
msg:addByte(0x00) -- Store bool
end
Expand Down
2 changes: 1 addition & 1 deletion data/modules/scripts/blessings/blessings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Blessings.sendBlessDialog = function(player)
for i = 1, historyAmount do
msg:addU32(os.time()) -- timestamp
msg:addByte(0) -- Color message (1 - Red | 0 = White loss)
msg:addString("Blessing Purchased") -- History message
msg:addString("Blessing Purchased", "Blessings.sendBlessDialog - Blessing Purchased") -- History message
end

msg:sendToPlayer(player)
Expand Down
12 changes: 6 additions & 6 deletions data/modules/scripts/daily_reward/daily_reward.lua
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ function Player.sendOpenRewardWall(self, shrine)
msg:addByte(self:getDayStreak()) -- current reward? day = 0, day 1, ... this should be resetted to 0 every week imo
if DailyReward.isRewardTaken(self:getId()) then -- state (player already took reward? but just make sure noone wpe)
msg:addByte(1)
msg:addString("Sorry, you have already taken your daily reward or you are unable to collect it.") -- Unknown message
msg:addString("Sorry, you have already taken your daily reward or you are unable to collect it.", "Player.sendOpenRewardWall - Sorry, you have already taken your daily reward or you are unable to collect it.") -- Unknown message
if self:getJokerTokens() > 0 then
msg:addByte(1)
msg:addU16(self:getJokerTokens())
Expand Down Expand Up @@ -529,7 +529,7 @@ function Player.sendError(self, error)
local msg = NetworkMessage()
msg:addByte(ServerPackets.ShowDialog)
msg:addByte(0x14)
msg:addString(error)
msg:addString(error, "Player.sendError - error")
msg:sendToPlayer(self)
end

Expand Down Expand Up @@ -562,7 +562,7 @@ function Player.sendRewardHistory(self)
for k, entry in ipairs(entries) do
msg:addU32(entry.timestamp)
msg:addByte(0) -- (self:isPremium() and 0 or 0)
msg:addString(entry.description)
msg:addString(entry.description, "Player.sendRewardHistory - entry.description")
msg:addU16(entry.daystreak + 1)
end
msg:sendToPlayer(self)
Expand Down Expand Up @@ -595,7 +595,7 @@ function Player.readDailyReward(self, msg, currentDay, state)
local itemName = itemType:getArticle() .. " " .. itemType:getName()
local itemWeight = itemType:getWeight()
msg:addU16(itemId)
msg:addString(itemName)
msg:addString(itemName, "Player.readDailyReward - itemName")
msg:addU32(itemWeight)
end
end
Expand All @@ -605,7 +605,7 @@ function Player.readDailyReward(self, msg, currentDay, state)
-- for i = 1, #rewards.things do
-- msg:addByte(DAILY_REWARD_SYSTEM_TYPE_OTHER) -- type
-- msg:addU16(rewards.things[i].id * 100)
-- msg:addString(rewards.things[i].name)
-- msg:addString(rewards.things[i].name, "Player.readDailyReward - rewards.things[i].name")
-- msg:addByte(rewards.things[i].quantity)
-- end
elseif type == DAILY_REWARD_TYPE_PREY_REROLL then
Expand Down Expand Up @@ -636,7 +636,7 @@ function Player.sendDailyReward(self)
local maxBonus = 7
msg:addByte(maxBonus - 1)
for i = 2, maxBonus do
msg:addString(DailyReward.strikeBonuses[i].text)
msg:addString(DailyReward.strikeBonuses[i].text, "Player.sendDailyReward - DailyReward.strikeBonuses[i].text")
msg:addByte(i)
end
msg:addByte(1) -- Unknown
Expand Down
50 changes: 25 additions & 25 deletions data/modules/scripts/gamestore/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -572,23 +572,23 @@ function openStore(playerId)
GameStoreCategories, GameStoreCount = GameStore.Categories, #GameStore.Categories
end
local addCategory = function(category)
msg:addString(category.name)
msg:addString(category.name, "openStore - category.name")
if oldProtocol then
msg:addString(category.description)
msg:addString(category.description, "openStore - category.description")
end

msg:addByte(category.state or GameStore.States.STATE_NONE)
local size = #category.icons > 255 and 255 or #category.icons
msg:addByte(size)
for _, icon in ipairs(category.icons) do
if size > 0 then
msg:addString(icon)
msg:addString(icon, "openStore - icon")
size = size - 1
end
end

if category.parent then
msg:addString(category.parent)
msg:addString(category.parent, "openStore - category.parent")
else
msg:addU16(0)
end
Expand All @@ -608,7 +608,7 @@ function sendOfferDescription(player, offerId, description)
local msg = NetworkMessage()
msg:addByte(0xEA)
msg:addU32(offerId)
msg:addString(description)
msg:addString(description, "sendOfferDescription - description")
msg:sendToPlayer(player)
end

Expand Down Expand Up @@ -777,7 +777,7 @@ function sendShowStoreOffers(playerId, category, redirectId)
local msg = NetworkMessage()
local haveSaleOffer = 0
msg:addByte(GameStore.SendingPackets.S_StoreOffers)
msg:addString(category.name)
msg:addString(category.name, "sendShowStoreOffers - category.name")

local categoryLimit = 65535
if oldProtocol then
Expand Down Expand Up @@ -844,7 +844,7 @@ function sendShowStoreOffers(playerId, category, redirectId)

msg:addU16(#disableReasons)
for _, reason in ipairs(disableReasons) do
msg:addString(reason)
msg:addString(reason, "sendShowStoreOffers - reason")
end

if count > categoryLimit then
Expand All @@ -855,7 +855,7 @@ function sendShowStoreOffers(playerId, category, redirectId)
for name, offer in pairs(offers) do
if count > 0 then
count = count - 1
msg:addString(name)
msg:addString(name, "sendShowStoreOffers - name")
msg:addByte(#offer.offers)
sendOfferDescription(player, offer.id and offer.id or 0xFFFF, offer.description)
for _, off in ipairs(offer.offers) do
Expand Down Expand Up @@ -900,7 +900,7 @@ function sendShowStoreOffers(playerId, category, redirectId)

msg:addByte(type)
if type == GameStore.ConverType.SHOW_NONE then
msg:addString(offer.icons[1])
msg:addString(offer.icons[1], "sendShowStoreOffers - offer.icons[1]")
elseif type == GameStore.ConverType.SHOW_MOUNT then
local mount = Mount(offer.id)
msg:addU16(mount:getClientId())
Expand Down Expand Up @@ -966,7 +966,7 @@ function sendShowStoreOffersOnOldProtocol(playerId, category)
local msg = NetworkMessage()
local haveSaleOffer = 0
msg:addByte(GameStore.SendingPackets.S_StoreOffers)
msg:addString(category.name)
msg:addString(category.name, "sendShowStoreOffersOnOldProtocol - category.name")

if not category.offers then
msg:addU16(0)
Expand Down Expand Up @@ -1012,8 +1012,8 @@ function sendShowStoreOffersOnOldProtocol(playerId, category)
local disabled, disabledReason = player:canBuyOffer(offer).disabled, player:canBuyOffer(offer).disabledReason
local offerPrice = offer.type == GameStore.OfferTypes.OFFER_TYPE_EXPBOOST and GameStore.ExpBoostValues[player:getStorageValue(GameStore.Storages.expBoostCount)] or (newPrice or offer.price or 0xFFFF)
msg:addU32(offer.id and offer.id or 0xFFFF)
msg:addString(name)
msg:addString(offer.description or GameStore.getDefaultDescription(offer.type, offer.count))
msg:addString(name, "sendShowStoreOffersOnOldProtocol - name")
msg:addString(offer.description or GameStore.getDefaultDescription(offer.type, offer.count), "sendShowStoreOffersOnOldProtocol - offer.description or GameStore.getDefaultDescription(offer.type, offer.count)")
msg:addU32(offerPrice)
if offer.state then
if offer.state == GameStore.States.STATE_SALE then
Expand All @@ -1035,20 +1035,20 @@ function sendShowStoreOffersOnOldProtocol(playerId, category)

msg:addByte(disabled)
if disabled == 1 then
msg:addString(disabledReason)
msg:addString(disabledReason, "sendShowStoreOffersOnOldProtocol - disabledReason")
end

if offer.type == GameStore.OfferTypes.OFFER_TYPE_MOUNT then
msg:addByte(1)
msg:addString((offer.name):gsub("% ", "_") .. ".png")
msg:addString((offer.name):gsub("% ", "_") .. ".png", "sendShowStoreOffersOnOldProtocol - (offer.name).png")
elseif offer.type == GameStore.OfferTypes.OFFER_TYPE_OUTFIT then
msg:addByte(2)
msg:addString(offer.icons[1])
msg:addString(offer.icons[2])
msg:addString(offer.icons[1], "sendShowStoreOffersOnOldProtocol - offer.icons[1]")
msg:addString(offer.icons[2], "sendShowStoreOffersOnOldProtocol - offer.icons[2]")
else
msg:addByte(#offer.icons)
for k, icon in ipairs(offer.icons) do
msg:addString(icon)
msg:addString(icon, "sendShowStoreOffersOnOldProtocol - icon")
end
end

Expand Down Expand Up @@ -1089,7 +1089,7 @@ function sendStoreTransactionHistory(playerId, page, entriesPerPage)
if not oldProtocol then
msg:addByte(0x0) -- 0 = transferable tibia coin, 1 = normal tibia coin
end
msg:addString(entry.description)
msg:addString(entry.description, "sendStoreTransactionHistory - entry.description")
if not oldProtocol then
msg:addByte(0) -- details
end
Expand All @@ -1107,7 +1107,7 @@ function sendStorePurchaseSuccessful(playerId, message)
local msg = NetworkMessage()
msg:addByte(GameStore.SendingPackets.S_CompletePurchase)
msg:addByte(0x00)
msg:addString(message)
msg:addString(message, "sendStorePurchaseSuccessful - message")
if oldProtocol then
-- Send all coins can be used for buy store offers
local totalCoins = player:getTibiaCoins() + player:getTransferableCoins()
Expand All @@ -1129,7 +1129,7 @@ function sendStoreError(playerId, errorType, message)
msg:addByte(GameStore.SendingPackets.S_StoreError)

msg:addByte(errorType)
msg:addString(message)
msg:addString(message, "sendStoreError - message")

msg:sendToPlayer(player)
end
Expand Down Expand Up @@ -2080,7 +2080,7 @@ function sendHomePage(playerId)
local msg = NetworkMessage()
msg:addByte(GameStore.SendingPackets.S_StoreOffers)

msg:addString("Home")
msg:addString("Home", "sendHomePage - Home")
msg:addU32(0x0) -- Redirect ID (not used here)
msg:addByte(0x0) -- Window Type
msg:addByte(0x0) -- Collections Size
Expand All @@ -2106,13 +2106,13 @@ function sendHomePage(playerId)

msg:addU16(#disableReasons)
for _, reason in ipairs(disableReasons) do
msg:addString(reason)
msg:addString(reason, "sendHomePage - reason")
end

msg:addU16(#homeOffers) -- offers

for p, offer in pairs(homeOffers) do
msg:addString(offer.name)
msg:addString(offer.name, "sendHomePage - offer.name")
msg:addByte(0x1) -- ?
msg:addU32(offer.id or 0) -- id
msg:addU16(0x1)
Expand All @@ -2132,7 +2132,7 @@ function sendHomePage(playerId)

msg:addByte(type)
if type == GameStore.ConverType.SHOW_NONE then
msg:addString(offer.icons[1])
msg:addString(offer.icons[1], "sendHomePage - offer.icons[1]")
elseif type == GameStore.ConverType.SHOW_MOUNT then
local mount = Mount(offer.id)
if mount then
Expand Down Expand Up @@ -2163,7 +2163,7 @@ function sendHomePage(playerId)
local banner = HomeBanners
msg:addByte(#banner.images)
for m, image in ipairs(banner.images) do
msg:addString(image)
msg:addString(image, "sendHomePage - image")
msg:addByte(0x04) -- Banner Type (offer)
msg:addU32(0x00) -- Offer Id
msg:addByte(0)
Expand Down
5 changes: 3 additions & 2 deletions src/lua/functions/core/network/network_message_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ int NetworkMessageFunctions::luaNetworkMessageAdd64(lua_State* L) {
}

int NetworkMessageFunctions::luaNetworkMessageAddString(lua_State* L) {
// networkMessage:addString(string)
// networkMessage:addString(string, function)
const std::string &string = getString(L, 2);
const std::string &function = getString(L, 3);
const auto &message = getUserdataShared<NetworkMessage>(L, 1);
if (message) {
message->addString(string);
message->addString(string, function);
pushBoolean(L, true);
} else {
lua_pushnil(L);
Expand Down
8 changes: 4 additions & 4 deletions src/server/network/message/networkmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ Position NetworkMessage::getPosition() {
return pos;
}

void NetworkMessage::addString(const std::string &value) {
void NetworkMessage::addString(const std::string &value, const std::string &function) {
size_t stringLen = value.length();
if (value.empty()) {
g_logger().debug("[NetworkMessage::addString] - Value string is empty");
g_logger().debug("[NetworkMessage::addString] - Value string is empty, function '{}'", function);
}
if (!canAdd(stringLen + 2)) {
g_logger().error("[NetworkMessage::addString] - NetworkMessage size is wrong: {}", stringLen);
g_logger().error("[NetworkMessage::addString] - NetworkMessage size is wrong: {}, function '{}'", stringLen, function);
return;
}
if (stringLen > NETWORKMESSAGE_MAXSIZE) {
g_logger().error("[NetworkMessage::addString] - Exceded NetworkMessage max size: {}, actually size: {}", NETWORKMESSAGE_MAXSIZE, stringLen);
g_logger().error("[NetworkMessage::addString] - Exceded NetworkMessage max size: {}, actually size: {}, function '{}'", NETWORKMESSAGE_MAXSIZE, stringLen, function);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/network/message/networkmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class NetworkMessage {
void addBytes(const char* bytes, size_t size);
void addPaddingBytes(size_t n);

void addString(const std::string &value);
void addString(const std::string &value, const std::string &function);

void addDouble(double value, uint8_t precision = 2);

Expand Down
Loading
Loading