Skip to content

Commit

Permalink
memcpy and template deduction guide
Browse files Browse the repository at this point in the history
  • Loading branch information
jadebenn committed Nov 22, 2024
1 parent 3a0e37e commit 5aa8b13
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 49 deletions.
14 changes: 7 additions & 7 deletions dCommon/Amf3.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __AMF3__H__
#define __AMF3__H__
#ifndef AMF3_H
#define AMF3_H

#include "dCommonVars.h"
#include "Logger.h"
Expand Down Expand Up @@ -64,10 +64,6 @@ template class AMFValue<uint32_t>;
template class AMFValue<std::string>;
template class AMFValue<double>;

// Blank specialization to make sure AMFValue<const char*> fails
template <>
class AMFValue<const char*> {};

// AMFValue template class member function instantiations
template <> [[nodiscard]] constexpr eAmf AMFValue<std::nullptr_t>::GetValueType() const noexcept { return eAmf::Null; }
template <> [[nodiscard]] constexpr eAmf AMFValue<bool>::GetValueType() const noexcept { return m_Data ? eAmf::True : eAmf::False; }
Expand All @@ -85,6 +81,10 @@ using AMFIntValue = AMFValue<int32_t>;
using AMFStringValue = AMFValue<std::string>;
using AMFDoubleValue = AMFValue<double>;

// Template deduction guide to ensure string literals deduce
template <size_t N>
AMFValue(const char (&)[N]) -> AMFStringValue;

Check failure on line 86 in dCommon/Amf3.h

View workflow job for this annotation

GitHub Actions / Build & Test (macos-13)

deduced type 'AMFStringValue' (aka 'AMFValue<basic_string<char>>') of deduction guide is not a specialization of template 'AMFValue'

/**
* The AMFArrayValue object holds 2 types of lists:
* An associative list where a key maps to a value
Expand Down Expand Up @@ -345,4 +345,4 @@ class AMFArrayValue : public AMFBaseValue {
AMFDense m_Dense;
};

#endif //!__AMF3__H__
#endif //!AMF3_H
12 changes: 6 additions & 6 deletions dGame/dComponents/DestroyableComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ void DestroyableComponent::SetMaxHealth(float value, bool playAnim) {
if (!characterComponent) return;

AMFArrayValue args;
args.Insert<std::string>("amount", std::to_string(difference));
args.Insert<std::string>("type", "health");
args.Insert("amount", std::to_string(difference));
args.Insert("type", "health");

GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args);
}
Expand Down Expand Up @@ -300,8 +300,8 @@ void DestroyableComponent::SetMaxArmor(float value, bool playAnim) {
if (!characterComponent) return;

AMFArrayValue args;
args.Insert<std::string>("amount", std::to_string(value));
args.Insert<std::string>("type", "armor");
args.Insert("amount", std::to_string(value));
args.Insert("type", "armor");

GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args);
}
Expand Down Expand Up @@ -341,8 +341,8 @@ void DestroyableComponent::SetMaxImagination(float value, bool playAnim) {
if (!characterComponent) return;

AMFArrayValue args;
args.Insert<std::string>("amount", std::to_string(difference));
args.Insert<std::string>("type", "imagination");
args.Insert("amount", std::to_string(difference));
args.Insert("type", "imagination");

GameMessages::SendUIMessageServerToSingleClient(m_Parent, characterComponent->GetSystemAddress(), "MaxPlayerBarUpdate", args);
}
Expand Down
2 changes: 1 addition & 1 deletion dGame/dComponents/PropertyEntranceComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void PropertyEntranceComponent::OnUse(Entity* entity) {

AMFArrayValue args;

args.Insert<std::string>("state", "property_menu");
args.Insert("state", "property_menu");

GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "pushGameState", args);
}
Expand Down
4 changes: 2 additions & 2 deletions dGame/dUtilities/SlashCommands/GMZeroCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace GMZeroCommands {
{
AMFArrayValue args;

args.Insert<std::string>("state", "Story");
args.Insert("state", "Story");

GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "pushGameState", args);
}
Expand All @@ -121,7 +121,7 @@ namespace GMZeroCommands {
{
AMFArrayValue args;

args.Insert<std::string>("state", "Story");
args.Insert("state", "Story");

GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "pushGameState", args);
}
Expand Down
2 changes: 1 addition & 1 deletion dScripts/02_server/Map/General/BankInteractServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
void BankInteractServer::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;

args.Insert<std::string>("state", "bank");
args.Insert("state", "bank");

GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}
Expand Down
2 changes: 1 addition & 1 deletion dScripts/02_server/Map/General/MailBoxServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
void MailBoxServer::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;

args.Insert<std::string>("state", "Mail");
args.Insert("state", "Mail");

GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}
Expand Down
2 changes: 1 addition & 1 deletion dScripts/02_server/Map/General/StoryBoxInteractServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void StoryBoxInteractServer::OnUse(Entity* self, Entity* user) {
{
AMFArrayValue args;

args.Insert<std::string>("state", "Story");
args.Insert("state", "Story");

GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}
Expand Down
30 changes: 15 additions & 15 deletions dScripts/02_server/Map/NS/NsLegoClubDoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ void NsLegoClubDoor::OnStartup(Entity* self) {
args = {};

args.Insert("callbackClient", std::to_string(self->GetObjectID()));
args.Insert<std::string>("strIdentifier", "choiceDoor");
args.Insert<std::string>("title", "%[UI_CHOICE_DESTINATION]");
args.Insert("strIdentifier", "choiceDoor");
args.Insert("title", "%[UI_CHOICE_DESTINATION]");

AMFArrayValue* choiceOptions = args.InsertArray("options");

{
AMFArrayValue* nsArgs = choiceOptions->PushArray();

nsArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert<std::string>("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert<std::string>("identifier", "zoneID_1200");
nsArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NS_HOVER]");
nsArgs->Insert("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert("identifier", "zoneID_1200");
nsArgs->Insert("tooltipText", "%[UI_CHOICE_NS_HOVER]");
}

{
AMFArrayValue* ntArgs = choiceOptions->PushArray();

ntArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert<std::string>("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert<std::string>("identifier", "zoneID_1900");
ntArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NT_HOVER]");
ntArgs->Insert("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert("identifier", "zoneID_1900");
ntArgs->Insert("tooltipText", "%[UI_CHOICE_NT_HOVER]");
}

options = choiceOptions;
Expand All @@ -46,22 +46,22 @@ void NsLegoClubDoor::OnUse(Entity* self, Entity* user) {
AMFArrayValue multiArgs;

multiArgs.Insert("callbackClient", std::to_string(self->GetObjectID()));
multiArgs.Insert<std::string>("strIdentifier", "choiceDoor");
multiArgs.Insert<std::string>("title", "%[UI_CHOICE_DESTINATION]");
multiArgs.Insert("strIdentifier", "choiceDoor");
multiArgs.Insert("title", "%[UI_CHOICE_DESTINATION]");
multiArgs.Insert("options", static_cast<AMFBaseValue*>(options));

GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "QueueChoiceBox", multiArgs);

multiArgs.Remove("options", false); // We do not want the local amf to delete the options!
} else if (self->GetVar<int32_t>(u"currentZone") != m_ChoiceZoneID) {
AMFArrayValue multiArgs;
multiArgs.Insert<std::string>("state", "Lobby");
multiArgs.Insert("state", "Lobby");

AMFArrayValue* context = multiArgs.InsertArray("context");
context->Insert("user", std::to_string(player->GetObjectID()));
context->Insert("callbackObj", std::to_string(self->GetObjectID()));
context->Insert<std::string>("HelpVisible", "show");
context->Insert<std::string>("type", "Lego_Club_Valid");
context->Insert("HelpVisible", "show");
context->Insert("type", "Lego_Club_Valid");

GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "pushGameState", multiArgs);
} else {
Expand Down
20 changes: 10 additions & 10 deletions dScripts/02_server/Map/NS/NsLupTeleport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ void NsLupTeleport::OnStartup(Entity* self) {
args = {};

args.Insert("callbackClient", std::to_string(self->GetObjectID()));
args.Insert<std::string>("strIdentifier", "choiceDoor");
args.Insert<std::string>("title", "%[UI_CHOICE_DESTINATION]");
args.Insert("strIdentifier", "choiceDoor");
args.Insert("title", "%[UI_CHOICE_DESTINATION]");

AMFArrayValue* choiceOptions = args.InsertArray("options");

{
AMFArrayValue* nsArgs = choiceOptions->PushArray();

nsArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert<std::string>("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert<std::string>("identifier", "zoneID_1200");
nsArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NS_HOVER]");
nsArgs->Insert("image", "textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->Insert("caption", "%[UI_CHOICE_NS]");
nsArgs->Insert("identifier", "zoneID_1200");
nsArgs->Insert("tooltipText", "%[UI_CHOICE_NS_HOVER]");
}

{
AMFArrayValue* ntArgs = choiceOptions->PushArray();

ntArgs->Insert<std::string>("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert<std::string>("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert<std::string>("identifier", "zoneID_1900");
ntArgs->Insert<std::string>("tooltipText", "%[UI_CHOICE_NT_HOVER]");
ntArgs->Insert("image", "textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->Insert("caption", "%[UI_CHOICE_NT]");
ntArgs->Insert("identifier", "zoneID_1900");
ntArgs->Insert("tooltipText", "%[UI_CHOICE_NT_HOVER]");
}
}

Expand Down
2 changes: 1 addition & 1 deletion dScripts/02_server/Map/Property/PropertyBankInteract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void PropertyBankInteract::OnUse(Entity* self, Entity* user) {

AMFArrayValue args;

args.Insert<std::string>("state", "bank");
args.Insert("state", "bank");

GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);

Expand Down
5 changes: 4 additions & 1 deletion dWorldServer/WorldServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,10 @@ void HandlePacket(Packet* packet) {
}

default:
const auto messageId = static_cast<MessageType::World>(packet->data[3]);
// Need to use memcpy instead of reinterpret_cast to avoid UB
MessageType::World messageId;
std::memcpy(&messageId, &packet->data[3], sizeof(MessageType::World));

const std::string_view messageIdString = StringifiedEnum::ToString(messageId);
LOG("Unknown world packet received: %4i, %s", messageId, messageIdString.data());
}
Expand Down
8 changes: 5 additions & 3 deletions tests/dCommonTests/Amf3Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ TEST(dCommonTests, AMF3AssociativeArrayTest) {

TEST(dCommonTests, AMF3InsertionAssociativeTest) {
AMFArrayValue array;
array.Insert<std::string>("CString", "string");
array.Insert<std::string>("String", std::string("string"));
array.Insert("CString", "string");
array.Insert("String", std::string("string"));
array.Insert("False", false);
array.Insert("True", true);
array.Insert<int32_t>("Integer", 42U);
Expand All @@ -71,6 +71,7 @@ TEST(dCommonTests, AMF3InsertionAssociativeTest) {
array.Insert<std::vector<uint32_t>>("Undefined", {});
array.Insert("Null", nullptr);

ASSERT_EQ(array.Get<const char*>("CString")->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<std::string>("String")->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<bool>("False")->GetValueType(), eAmf::False);
ASSERT_EQ(array.Get<bool>("True")->GetValueType(), eAmf::True);
Expand All @@ -84,7 +85,7 @@ TEST(dCommonTests, AMF3InsertionAssociativeTest) {
TEST(dCommonTests, AMF3InsertionDenseTest) {
AMFArrayValue array;
array.Push<std::string>("string");
array.Push<std::string>("CString");
array.Push("CString");
array.Push(false);
array.Push(true);
array.Push<int32_t>(42U);
Expand All @@ -94,6 +95,7 @@ TEST(dCommonTests, AMF3InsertionDenseTest) {
array.Push<std::vector<uint32_t>>({});

ASSERT_EQ(array.Get<std::string>(0)->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<const char*>(1)->GetValueType(), eAmf::String);
ASSERT_EQ(array.Get<bool>(2)->GetValueType(), eAmf::False);
ASSERT_EQ(array.Get<bool>(3)->GetValueType(), eAmf::True);
ASSERT_EQ(array.Get<int32_t>(4)->GetValueType(), eAmf::Integer);
Expand Down

0 comments on commit 5aa8b13

Please sign in to comment.