memcpy and template deduction guide

This commit is contained in:
jadebenn 2024-11-21 18:37:50 -06:00
parent 3a0e37ee8a
commit 5aa8b1395b
12 changed files with 54 additions and 49 deletions

View File

@ -1,5 +1,5 @@
#ifndef __AMF3__H__
#define __AMF3__H__
#ifndef AMF3_H
#define AMF3_H
#include "dCommonVars.h"
#include "Logger.h"
@ -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; }
@ -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;
/**
* The AMFArrayValue object holds 2 types of lists:
* An associative list where a key maps to a value
@ -345,4 +345,4 @@ private:
AMFDense m_Dense;
};
#endif //!__AMF3__H__
#endif //!AMF3_H

View File

@ -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);
}
@ -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);
}
@ -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);
}

View File

@ -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);
}

View File

@ -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);
}
@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
@ -46,8 +46,8 @@ 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);
@ -55,13 +55,13 @@ void NsLegoClubDoor::OnUse(Entity* self, Entity* user) {
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 {

View File

@ -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]");
}
}

View File

@ -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);

View File

@ -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());
}

View File

@ -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);
@ -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);
@ -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);
@ -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);