Skip to content

Commit f05fb5d

Browse files
authored
improve: some fixes and adjustments (#2980)
Fixes: • In the solution build • In the debug build • Check for race id 0 in prey and taskhunting • Fixes in pop_front of some queues • Fix in Argon2::parseBitShift for debug build • Fix in NetworkMessage functions for debug build • Added try/catch in IOLoginData::savePlayer to avoid unhandled exceptions • New caseInsensitiveCompare function for use in some ongoing pull requests
1 parent e70c8c5 commit f05fb5d

File tree

11 files changed

+85
-70
lines changed

11 files changed

+85
-70
lines changed

src/io/functions/iologindata_load_player.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,10 @@ void IOLoginDataLoad::loadPlayerPreyClass(std::shared_ptr<Player> player, DBResu
734734
query << "SELECT * FROM `player_prey` WHERE `player_id` = " << player->getGUID();
735735
if ((result = db.storeQuery(query.str()))) {
736736
do {
737+
auto selectedRaceId = result->getNumber<uint16_t>("raceid");
738+
if (selectedRaceId == 0) {
739+
continue;
740+
}
737741
auto slot = std::make_unique<PreySlot>(static_cast<PreySlot_t>(result->getNumber<uint16_t>("slot")));
738742
auto state = static_cast<PreyDataState_t>(result->getNumber<uint16_t>("state"));
739743
if (slot->id == PreySlot_Two && state == PreyDataState_Locked) {
@@ -745,7 +749,7 @@ void IOLoginDataLoad::loadPlayerPreyClass(std::shared_ptr<Player> player, DBResu
745749
} else {
746750
slot->state = state;
747751
}
748-
slot->selectedRaceId = result->getNumber<uint16_t>("raceid");
752+
slot->selectedRaceId = selectedRaceId;
749753
slot->option = static_cast<PreyOption_t>(result->getNumber<uint16_t>("option"));
750754
slot->bonus = static_cast<PreyBonus_t>(result->getNumber<uint16_t>("bonus_type"));
751755
slot->bonusRarity = static_cast<uint8_t>(result->getNumber<uint16_t>("bonus_rarity"));
@@ -781,6 +785,10 @@ void IOLoginDataLoad::loadPlayerTaskHuntingClass(std::shared_ptr<Player> player,
781785
query << "SELECT * FROM `player_taskhunt` WHERE `player_id` = " << player->getGUID();
782786
if ((result = db.storeQuery(query.str()))) {
783787
do {
788+
auto selectedRaceId = result->getNumber<uint16_t>("raceid");
789+
if (selectedRaceId == 0) {
790+
continue;
791+
}
784792
auto slot = std::make_unique<TaskHuntingSlot>(static_cast<PreySlot_t>(result->getNumber<uint16_t>("slot")));
785793
auto state = static_cast<PreyTaskDataState_t>(result->getNumber<uint16_t>("state"));
786794
if (slot->id == PreySlot_Two && state == PreyTaskDataState_Locked) {
@@ -792,7 +800,7 @@ void IOLoginDataLoad::loadPlayerTaskHuntingClass(std::shared_ptr<Player> player,
792800
} else {
793801
slot->state = state;
794802
}
795-
slot->selectedRaceId = result->getNumber<uint16_t>("raceid");
803+
slot->selectedRaceId = selectedRaceId;
796804
slot->upgrade = result->getNumber<bool>("upgrade");
797805
slot->rarity = static_cast<uint8_t>(result->getNumber<uint16_t>("rarity"));
798806
slot->currentKills = result->getNumber<uint16_t>("kills");
@@ -827,7 +835,7 @@ void IOLoginDataLoad::loadPlayerForgeHistory(std::shared_ptr<Player> player, DBR
827835

828836
std::ostringstream query;
829837
query << "SELECT * FROM `forge_history` WHERE `player_id` = " << player->getGUID();
830-
if (result = Database::getInstance().storeQuery(query.str())) {
838+
if ((result = Database::getInstance().storeQuery(query.str()))) {
831839
do {
832840
auto actionEnum = magic_enum::enum_value<ForgeAction_t>(result->getNumber<uint16_t>("action_type"));
833841
ForgeHistory history;
@@ -853,7 +861,7 @@ void IOLoginDataLoad::loadPlayerBosstiary(std::shared_ptr<Player> player, DBResu
853861

854862
std::ostringstream query;
855863
query << "SELECT * FROM `player_bosstiary` WHERE `player_id` = " << player->getGUID();
856-
if (result = Database::getInstance().storeQuery(query.str())) {
864+
if ((result = Database::getInstance().storeQuery(query.str()))) {
857865
do {
858866
player->setSlotBossId(1, result->getNumber<uint16_t>("bossIdSlotOne"));
859867
player->setSlotBossId(2, result->getNumber<uint16_t>("bossIdSlotTwo"));

src/io/functions/iologindata_save_player.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ bool IOLoginDataSave::saveItems(std::shared_ptr<Player> player, const ItemBlockL
8282
const ContainerBlock &cb = queue.front();
8383
std::shared_ptr<Container> container = cb.first;
8484
int32_t parentId = cb.second;
85-
queue.pop_front();
8685

8786
if (!container) {
8887
continue; // Check for null container
@@ -137,6 +136,9 @@ bool IOLoginDataSave::saveItems(std::shared_ptr<Player> player, const ItemBlockL
137136
return false;
138137
}
139138
}
139+
140+
// Removes the object after processing everything, avoiding memory usage after freeing
141+
queue.pop_front();
140142
}
141143

142144
// Execute query

src/io/iologindata.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,21 @@ bool IOLoginData::loadPlayer(std::shared_ptr<Player> player, DBResult_ptr result
202202
}
203203

204204
bool IOLoginData::savePlayer(std::shared_ptr<Player> player) {
205-
bool success = DBTransaction::executeWithinTransaction([player]() {
206-
return savePlayerGuard(player);
207-
});
205+
try {
206+
bool success = DBTransaction::executeWithinTransaction([player]() {
207+
return savePlayerGuard(player);
208+
});
209+
210+
if (!success) {
211+
g_logger().error("[{}] Error occurred saving player", __FUNCTION__);
212+
}
208213

209-
if (!success) {
210-
g_logger().error("[{}] Error occurred saving player", __FUNCTION__);
214+
return success;
215+
} catch (const DatabaseException &e) {
216+
g_logger().error("[{}] Exception occurred: {}", __FUNCTION__, e.what());
211217
}
212218

213-
return success;
219+
return false;
214220
}
215221

216222
bool IOLoginData::savePlayerGuard(std::shared_ptr<Player> player) {

src/security/argon.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void Argon2::updateConstants() {
2525
}
2626

2727
uint32_t Argon2::parseBitShift(const std::string &bitShiftStr) const {
28-
static const std::regex pattern(R"(^\s*(\d+)\s*<<\s*(\d+)\s*$)");
28+
static const std::regex pattern(R"(^\s*(\d+)\s*<<\s*(\d+)\s*$)", std::regex_constants::ECMAScript | std::regex_constants::icase);
2929
std::smatch match;
3030

3131
if (!std::regex_match(bitShiftStr, match, pattern)) {

src/server/network/message/networkmessage.cpp

+6-11
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ void NetworkMessage::addString(const std::string &value, const std::source_locat
160160
auto len = static_cast<uint16_t>(stringLen);
161161
add<uint16_t>(len);
162162
// Using to copy the string into the buffer
163-
auto it = std::ranges::copy(value, buffer.begin() + info.position);
164-
g_logger().trace("First value copied from sourceSpan: {}, second value copied from sourceSpan: {}", *it.in, *it.out);
163+
std::ranges::copy(value, buffer.begin() + info.position);
165164
info.position += stringLen;
166165
info.length += stringLen;
167166
}
@@ -211,8 +210,7 @@ void NetworkMessage::addBytes(const char* bytes, size_t size) {
211210
return;
212211
}
213212

214-
auto it = std::ranges::copy(bytes, bytes + size, buffer.begin() + info.position);
215-
g_logger().trace("First value copied from sourceSpan: {}, second value copied from sourceSpan: {}", *it.in, *it.out);
213+
std::ranges::copy(std::span(bytes, size), buffer.begin() + info.position);
216214
info.position += size;
217215
info.length += size;
218216
}
@@ -293,13 +291,10 @@ void NetworkMessage::append(const NetworkMessage &other) {
293291
return;
294292
}
295293

296-
// Create a span for the source data (from the other message)
297-
std::span<const unsigned char> sourceSpan(other.getBuffer() + otherStartPos, otherLength);
298-
// Create a span for the destination in the current buffer
299-
std::span<unsigned char> destSpan(buffer.data() + info.position, otherLength);
300-
// Copy the data from the source span to the destination span
301-
auto it = std::ranges::copy(sourceSpan, destSpan.begin());
302-
g_logger().trace("First value copied from sourceSpan: {}, second value copied from sourceSpan: {}", *it.in, *it.out);
294+
std::ranges::copy(
295+
std::span<const unsigned char>(other.getBuffer() + otherStartPos, otherLength),
296+
buffer.data() + info.position
297+
);
303298

304299
// Update the buffer information
305300
info.length += otherLength;

src/server/network/protocol/protocolgame.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -6091,16 +6091,17 @@ void ProtocolGame::sendTradeItemRequest(const std::string &traderName, std::shar
60916091
std::list<std::shared_ptr<Container>> listContainer { tradeContainer };
60926092
std::list<std::shared_ptr<Item>> itemList { tradeContainer };
60936093
while (!listContainer.empty()) {
6094-
std::shared_ptr<Container> container = listContainer.front();
6095-
listContainer.pop_front();
6096-
6097-
for (const std::shared_ptr<Item> &containerItem : container->getItemList()) {
6098-
std::shared_ptr<Container> tmpContainer = containerItem->getContainer();
6094+
const auto &container = listContainer.front();
6095+
for (const auto &containerItem : container->getItemList()) {
6096+
const auto &tmpContainer = containerItem->getContainer();
60996097
if (tmpContainer) {
61006098
listContainer.push_back(tmpContainer);
61016099
}
61026100
itemList.push_back(containerItem);
61036101
}
6102+
6103+
// Removes the object after processing everything, avoiding memory usage after freeing
6104+
listContainer.pop_front();
61046105
}
61056106

61066107
msg.addByte(itemList.size());

src/server/network/webhook/webhook.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void Webhook::sendWebhook() {
147147
return;
148148
}
149149

150-
auto task = webhooks.front();
150+
const auto &task = webhooks.front();
151151

152152
std::string response_body;
153153
auto response_code = sendRequest(task->url.c_str(), task->payload.c_str(), &response_body);
@@ -162,8 +162,6 @@ void Webhook::sendWebhook() {
162162
return;
163163
}
164164

165-
webhooks.pop_front();
166-
167165
if (response_code >= 300) {
168166
g_logger().error(
169167
"Failed to send webhook message, error code: {} response body: {} request body: {}",
@@ -176,4 +174,7 @@ void Webhook::sendWebhook() {
176174
}
177175

178176
g_logger().debug("Webhook successfully sent to {}", task->url);
177+
178+
// Removes the object after processing everything, avoiding memory usage after freeing
179+
webhooks.pop_front();
179180
}

src/utils/tools.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -1952,3 +1952,18 @@ uint8_t convertWheelGemAffinityToDomain(uint8_t affinity) {
19521952
return 0;
19531953
}
19541954
}
1955+
1956+
bool caseInsensitiveCompare(std::string_view str1, std::string_view str2, size_t length /*= std::string_view::npos*/) {
1957+
if (length == std::string_view::npos) {
1958+
if (str1.size() != str2.size()) {
1959+
return false;
1960+
}
1961+
length = str1.size();
1962+
} else {
1963+
length = std::min({ length, str1.size(), str2.size() });
1964+
}
1965+
1966+
return std::equal(str1.begin(), str1.begin() + length, str2.begin(), [](char c1, char c2) {
1967+
return std::tolower(static_cast<unsigned char>(c1)) == std::tolower(static_cast<unsigned char>(c2));
1968+
});
1969+
}

src/utils/tools.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,5 @@ template <typename EnumType, typename UnderlyingType = std::underlying_type_t<En
220220
EnumType enumFromValue(UnderlyingType value) {
221221
return static_cast<EnumType>(value);
222222
}
223+
224+
bool caseInsensitiveCompare(std::string_view str1, std::string_view str2, size_t length = std::string_view::npos);

vcproj/canary.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@
516516
<IncludeInUnityFile>true</IncludeInUnityFile>
517517
<OpenMPSupport>true</OpenMPSupport>
518518
<LanguageStandard_C>Default</LanguageStandard_C>
519-
<AdditionalOptions>/Zc:__cplusplus /fsanitize=address %(AdditionalOptions)</AdditionalOptions>
519+
<AdditionalOptions>/Zc:__cplusplus /fsanitize=address /utf-8 %(AdditionalOptions)</AdditionalOptions>
520520
<PreprocessorDefinitions>_DISABLE_VECTOR_ANNOTATION;_DISABLE_STRING_ANNOTATION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
521521
<PrecompiledHeader>Use</PrecompiledHeader>
522522
</ClCompile>
@@ -552,7 +552,7 @@
552552
<WholeProgramOptimization>true</WholeProgramOptimization>
553553
<IncludeInUnityFile>true</IncludeInUnityFile>
554554
<OpenMPSupport>true</OpenMPSupport>
555-
<AdditionalOptions>/Zc:__cplusplus %(AdditionalOptions)</AdditionalOptions>
555+
<AdditionalOptions>/Zc:__cplusplus /utf-8 %(AdditionalOptions)</AdditionalOptions>
556556
<PreprocessorDefinitions>_DISABLE_VECTOR_ANNOTATION;_DISABLE_STRING_ANNOTATION;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
557557
<PrecompiledHeader>Use</PrecompiledHeader>
558558
</ClCompile>

vcproj/settings.props

+22-37
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,37 @@
88
_WIN32_WINNT=0x0501;
99
BUILD_TYPE="RelWithDebInfo";
1010
</PREPROCESSOR_DEFS>
11-
<CANARY_LIBDEPS>
11+
<CANARY_COMMON_LIBDEPS>
1212
comctl32.lib;
1313
User32.lib;
1414
WS2_32.lib;
1515
pugixml.lib;
16-
libprotobuf.lib;
1716
lua51.lib;
1817
mpir.lib;
1918
libmariadb.lib;
19+
abseil_dll.lib;
20+
argon2.lib;
21+
</CANARY_COMMON_LIBDEPS>
22+
<CANARY_LIBDEPS>
23+
$(CANARY_COMMON_LIBDEPS);
24+
libprotobuf.lib;
2025
zlib.lib;
2126
libcurl.lib;
2227
fmt.lib;
2328
spdlog.lib;
24-
abseil_dll.lib;
25-
argon2.lib;
26-
opentelemetry_common.lib;
27-
opentelemetry_exporter_in_memory.lib;
28-
opentelemetry_exporter_ostream_logs.lib;
29-
opentelemetry_exporter_ostream_metrics.lib;
30-
opentelemetry_exporter_ostream_span.lib;
31-
opentelemetry_exporter_otlp_http.lib;
32-
opentelemetry_exporter_otlp_http_client.lib;
33-
opentelemetry_exporter_otlp_http_log.lib;
34-
opentelemetry_exporter_otlp_http_metric.lib;
35-
opentelemetry_exporter_prometheus.lib;
36-
opentelemetry_http_client_curl.lib;
37-
opentelemetry_logs.lib;
38-
opentelemetry_metrics.lib;
39-
opentelemetry_otlp_recordable.lib;
40-
opentelemetry_proto.lib;
41-
opentelemetry_resources.lib;
42-
opentelemetry_trace.lib;
43-
opentelemetry_version.lib;
44-
prometheus-cpp-core.lib;
45-
prometheus-cpp-pull.lib;
46-
civetweb.lib;
47-
civetweb-cpp.lib
4829
</CANARY_LIBDEPS>
4930
<CANARY_LIBDEPS_D>
50-
comctl32.lib;
51-
User32.lib;
52-
WS2_32.lib;
53-
pugixml.lib;
31+
$(CANARY_COMMON_LIBDEPS);
5432
libprotobufd.lib;
55-
lua51.lib;
56-
mpir.lib;
57-
libmariadb.lib;
5833
zlibd.lib;
5934
libcurl-d.lib;
6035
fmtd.lib;
6136
spdlogd.lib;
62-
abseil_dll.lib;
63-
argon2.lib;
37+
</CANARY_LIBDEPS_D>
38+
</PropertyGroup>
39+
40+
<PropertyGroup Condition="'$(FEATURE_METRICS)' == 'true'">
41+
<OPENTELEMETRY_LIBS>
6442
opentelemetry_common.lib;
6543
opentelemetry_exporter_in_memory.lib;
6644
opentelemetry_exporter_ostream_logs.lib;
@@ -79,13 +57,19 @@
7957
opentelemetry_resources.lib;
8058
opentelemetry_trace.lib;
8159
opentelemetry_version.lib;
60+
prometheus-cpp-core.lib;
61+
prometheus-cpp-pull;
8262
civetweb.lib;
83-
civetweb-cpp.lib
84-
</CANARY_LIBDEPS_D>
63+
civetweb-cpp.lib;
64+
</OPENTELEMETRY_LIBS>
65+
<CANARY_LIBDEPS>$(CANARY_LIBDEPS);$(OPENTELEMETRY_LIBS)</CANARY_LIBDEPS>
66+
<CANARY_LIBDEPS_D>$(CANARY_LIBDEPS_D);$(OPENTELEMETRY_LIBS)</CANARY_LIBDEPS_D>
8567
</PropertyGroup>
68+
8669
<PropertyGroup>
8770
<LinkIncremental>true</LinkIncremental>
8871
</PropertyGroup>
72+
8973
<ItemDefinitionGroup>
9074
<ClCompile>
9175
<WarningLevel>Level3</WarningLevel>
@@ -101,6 +85,7 @@
10185
<EntryPointSymbol>mainCRTStartup</EntryPointSymbol>
10286
</Link>
10387
</ItemDefinitionGroup>
88+
10489
<ItemGroup>
10590
<BuildMacro Include="PREPROCESSOR_DEFS">
10691
<Value>$(PREPROCESSOR_DEFS)</Value>

0 commit comments

Comments
 (0)