Refactor: Amf3 implementation (#998)

* Update AMFDeserializeTests.cpp

Redo Amf3 functionality

Overhaul the whole thing due to it being outdated and clunky to use

Sometimes you want to keep the value

Update AMFDeserializeTests.cpp

* Fix enum and constructors

Correct enum to a class and simplify names.
Add a proper default constructor

* Update MasterServer.cpp

* Fix bugs and add more tests

* Refactor: AMF with templates in mind

- Remove hard coded bodge
- Use templates and generics to allow for much looser typing and strengthened implementation
- Move code into header only implementation for portability

Refactor: Convert AMF implementation to templates

- Rip out previous implementation
- Remove all extraneous terminology
- Add proper overloads for all types of inserts
- Fix up tests and codebase

* Fix compiler errors

* Check for null first

* Add specialization for const char*

* Update tests for new template specialization

* Switch BitStream to use references

* Rename files

* Check enum bounds on deserialize

I did this on a phone
This commit is contained in:
David Markowitz
2023-05-13 15:22:00 -07:00
committed by GitHub
parent 9d105a287d
commit 4fe335cc66
46 changed files with 1081 additions and 1420 deletions

View File

@ -1,6 +1,6 @@
#include "AmConsoleTeleportServer.h"
#include "ChooseYourDestinationNsToNt.h"
#include "AMFFormat.h"
#include "Amf3.h"
void AmConsoleTeleportServer::OnStartup(Entity* self) {
self->SetVar(u"teleportAnim", m_TeleportAnim);

View File

@ -1,24 +1,24 @@
#include "BankInteractServer.h"
#include "GameMessages.h"
#include "Entity.h"
#include "AMFFormat.h"
#include "Amf3.h"
void BankInteractServer::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;
AMFStringValue* bank = new AMFStringValue();
bank->SetStringValue("bank");
args.InsertValue("state", bank);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", &args);
args.Insert("state", "bank");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}
void BankInteractServer::OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1,
int32_t param2, int32_t param3) {
if (args == "ToggleBank") {
AMFArrayValue args;
args.InsertValue("visible", new AMFFalseValue());
GameMessages::SendUIMessageServerToSingleClient(sender, sender->GetSystemAddress(), "ToggleBank", &args);
args.Insert("visible", false);
GameMessages::SendUIMessageServerToSingleClient(sender, sender->GetSystemAddress(), "ToggleBank", args);
GameMessages::SendNotifyClientObject(self->GetObjectID(), u"CloseBank", 0, 0, LWOOBJID_EMPTY, "", sender->GetSystemAddress());
}

View File

@ -1,19 +1,20 @@
#include "MailBoxServer.h"
#include "AMFFormat.h"
#include "Amf3.h"
#include "GameMessages.h"
#include "Entity.h"
void MailBoxServer::OnUse(Entity* self, Entity* user) {
AMFStringValue* value = new AMFStringValue();
value->SetStringValue("Mail");
AMFArrayValue args;
args.InsertValue("state", value);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", &args);
args.Insert("state", "Mail");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}
void MailBoxServer::OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) {
if (args == "toggleMail") {
AMFArrayValue args;
args.InsertValue("visible", new AMFFalseValue());
GameMessages::SendUIMessageServerToSingleClient(sender, sender->GetSystemAddress(), "ToggleMail", &args);
args.Insert("visible", false);
GameMessages::SendUIMessageServerToSingleClient(sender, sender->GetSystemAddress(), "ToggleMail", args);
}
}

View File

@ -2,7 +2,8 @@
#include "Character.h"
#include "GameMessages.h"
#include "dServer.h"
#include "AMFFormat.h"
#include "Amf3.h"
#include "Entity.h"
void StoryBoxInteractServer::OnUse(Entity* self, Entity* user) {
if (self->GetVar<bool>(u"hasCustomText")) {
@ -11,24 +12,18 @@ void StoryBoxInteractServer::OnUse(Entity* self, Entity* user) {
{
AMFArrayValue args;
auto* state = new AMFStringValue();
state->SetStringValue("Story");
args.Insert("state", "Story");
args.InsertValue("state", state);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", &args);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
}
user->AddCallbackTimer(0.1f, [user, customText]() {
AMFArrayValue args;
auto* text = new AMFStringValue();
text->SetStringValue(customText);
args.Insert("visible", true);
args.Insert("text", customText);
args.InsertValue("visible", new AMFTrueValue());
args.InsertValue("text", text);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "ToggleStoryBox", &args);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "ToggleStoryBox", args);
});
return;

View File

@ -1,7 +1,7 @@
#include "NsLegoClubDoor.h"
#include "dZoneManager.h"
#include "GameMessages.h"
#include "AMFFormat.h"
#include "Amf3.h"
void NsLegoClubDoor::OnStartup(Entity* self) {
self->SetVar(u"currentZone", (int32_t)dZoneManager::Instance()->GetZoneID().GetMapID());
@ -12,116 +12,56 @@ void NsLegoClubDoor::OnStartup(Entity* self) {
args = {};
AMFStringValue* callbackClient = new AMFStringValue();
callbackClient->SetStringValue(std::to_string(self->GetObjectID()));
args.InsertValue("callbackClient", callbackClient);
args.Insert("callbackClient", std::to_string(self->GetObjectID()));
args.Insert("strIdentifier", "choiceDoor");
args.Insert("title", "%[UI_CHOICE_DESTINATION]");
AMFStringValue* strIdentifier = new AMFStringValue();
strIdentifier->SetStringValue("choiceDoor");
args.InsertValue("strIdentifier", strIdentifier);
AMFStringValue* title = new AMFStringValue();
title->SetStringValue("%[UI_CHOICE_DESTINATION]");
args.InsertValue("title", title);
AMFArrayValue* choiceOptions = new AMFArrayValue();
AMFArrayValue* choiceOptions = args.InsertArray("options");
{
AMFArrayValue* nsArgs = new AMFArrayValue();
AMFArrayValue* nsArgs = choiceOptions->PushArray();
AMFStringValue* image = new AMFStringValue();
image->SetStringValue("textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->InsertValue("image", image);
AMFStringValue* caption = new AMFStringValue();
caption->SetStringValue("%[UI_CHOICE_NS]");
nsArgs->InsertValue("caption", caption);
AMFStringValue* identifier = new AMFStringValue();
identifier->SetStringValue("zoneID_1200");
nsArgs->InsertValue("identifier", identifier);
AMFStringValue* tooltipText = new AMFStringValue();
tooltipText->SetStringValue("%[UI_CHOICE_NS_HOVER]");
nsArgs->InsertValue("tooltipText", tooltipText);
choiceOptions->PushBackValue(nsArgs);
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 = new AMFArrayValue();
AMFArrayValue* ntArgs = choiceOptions->PushArray();
AMFStringValue* image = new AMFStringValue();
image->SetStringValue("textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->InsertValue("image", image);
AMFStringValue* caption = new AMFStringValue();
caption->SetStringValue("%[UI_CHOICE_NT]");
ntArgs->InsertValue("caption", caption);
AMFStringValue* identifier = new AMFStringValue();
identifier->SetStringValue("zoneID_1900");
ntArgs->InsertValue("identifier", identifier);
AMFStringValue* tooltipText = new AMFStringValue();
tooltipText->SetStringValue("%[UI_CHOICE_NT_HOVER]");
ntArgs->InsertValue("tooltipText", tooltipText);
choiceOptions->PushBackValue(ntArgs);
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;
args.InsertValue("options", choiceOptions);
}
void NsLegoClubDoor::OnUse(Entity* self, Entity* user) {
auto* player = user;
if (CheckChoice(self, player)) {
AMFArrayValue* multiArgs = new AMFArrayValue();
AMFArrayValue multiArgs;
AMFStringValue* callbackClient = new AMFStringValue();
callbackClient->SetStringValue(std::to_string(self->GetObjectID()));
multiArgs->InsertValue("callbackClient", callbackClient);
AMFStringValue* strIdentifier = new AMFStringValue();
strIdentifier->SetStringValue("choiceDoor");
multiArgs->InsertValue("strIdentifier", strIdentifier);
AMFStringValue* title = new AMFStringValue();
title->SetStringValue("%[UI_CHOICE_DESTINATION]");
multiArgs->InsertValue("title", title);
multiArgs->InsertValue("options", options);
multiArgs.Insert("callbackClient", std::to_string(self->GetObjectID()));
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 = new AMFArrayValue();
AMFArrayValue multiArgs;
multiArgs.Insert("state", "Lobby");
AMFStringValue* state = new AMFStringValue();
state->SetStringValue("Lobby");
multiArgs->InsertValue("state", state);
AMFArrayValue* context = new AMFArrayValue();
AMFStringValue* user = new AMFStringValue();
user->SetStringValue(std::to_string(player->GetObjectID()));
context->InsertValue("user", user);
AMFStringValue* callbackObj = new AMFStringValue();
callbackObj->SetStringValue(std::to_string(self->GetObjectID()));
context->InsertValue("callbackObj", callbackObj);
AMFStringValue* helpVisible = new AMFStringValue();
helpVisible->SetStringValue("show");
context->InsertValue("HelpVisible", helpVisible);
AMFStringValue* type = new AMFStringValue();
type->SetStringValue("Lego_Club_Valid");
context->InsertValue("type", type);
multiArgs->InsertValue("context", context);
AMFArrayValue* context = multiArgs.InsertArray("context");
context->Insert("user", std::to_string(player->GetObjectID()));
context->Insert("callbackObj", std::to_string(self->GetObjectID()));
context->Insert("HelpVisible", "show");
context->Insert("type", "Lego_Club_Valid");
GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "pushGameState", multiArgs);
} else {

View File

@ -2,7 +2,7 @@
#include "CppScripts.h"
#include "ChooseYourDestinationNsToNt.h"
#include "BaseConsoleTeleportServer.h"
#include "AMFFormat.h"
#include "Amf3.h"
class NsLegoClubDoor : public CppScripts::Script, ChooseYourDestinationNsToNt, BaseConsoleTeleportServer
{

View File

@ -1,7 +1,7 @@
#include "NsLupTeleport.h"
#include "dZoneManager.h"
#include "GameMessages.h"
#include "AMFFormat.h"
#include "Amf3.h"
void NsLupTeleport::OnStartup(Entity* self) {
self->SetVar(u"currentZone", (int32_t)dZoneManager::Instance()->GetZoneID().GetMapID());
@ -12,72 +12,36 @@ void NsLupTeleport::OnStartup(Entity* self) {
args = {};
AMFStringValue* callbackClient = new AMFStringValue();
callbackClient->SetStringValue(std::to_string(self->GetObjectID()));
args.InsertValue("callbackClient", callbackClient);
args.Insert("callbackClient", std::to_string(self->GetObjectID()));
args.Insert("strIdentifier", "choiceDoor");
args.Insert("title", "%[UI_CHOICE_DESTINATION]");
AMFStringValue* strIdentifier = new AMFStringValue();
strIdentifier->SetStringValue("choiceDoor");
args.InsertValue("strIdentifier", strIdentifier);
AMFStringValue* title = new AMFStringValue();
title->SetStringValue("%[UI_CHOICE_DESTINATION]");
args.InsertValue("title", title);
AMFArrayValue* choiceOptions = new AMFArrayValue();
AMFArrayValue* choiceOptions = args.InsertArray("options");
{
AMFArrayValue* nsArgs = new AMFArrayValue();
AMFArrayValue* nsArgs = choiceOptions->PushArray();
AMFStringValue* image = new AMFStringValue();
image->SetStringValue("textures/ui/zone_thumnails/Nimbus_Station.dds");
nsArgs->InsertValue("image", image);
AMFStringValue* caption = new AMFStringValue();
caption->SetStringValue("%[UI_CHOICE_NS]");
nsArgs->InsertValue("caption", caption);
AMFStringValue* identifier = new AMFStringValue();
identifier->SetStringValue("zoneID_1200");
nsArgs->InsertValue("identifier", identifier);
AMFStringValue* tooltipText = new AMFStringValue();
tooltipText->SetStringValue("%[UI_CHOICE_NS_HOVER]");
nsArgs->InsertValue("tooltipText", tooltipText);
choiceOptions->PushBackValue(nsArgs);
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 = new AMFArrayValue();
AMFArrayValue* ntArgs = choiceOptions->PushArray();
AMFStringValue* image = new AMFStringValue();
image->SetStringValue("textures/ui/zone_thumnails/Nexus_Tower.dds");
ntArgs->InsertValue("image", image);
AMFStringValue* caption = new AMFStringValue();
caption->SetStringValue("%[UI_CHOICE_NT]");
ntArgs->InsertValue("caption", caption);
AMFStringValue* identifier = new AMFStringValue();
identifier->SetStringValue("zoneID_1900");
ntArgs->InsertValue("identifier", identifier);
AMFStringValue* tooltipText = new AMFStringValue();
tooltipText->SetStringValue("%[UI_CHOICE_NT_HOVER]");
ntArgs->InsertValue("tooltipText", tooltipText);
choiceOptions->PushBackValue(ntArgs);
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]");
}
args.InsertValue("options", choiceOptions);
}
void NsLupTeleport::OnUse(Entity* self, Entity* user) {
auto* player = user;
if (CheckChoice(self, player)) {
GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "QueueChoiceBox", &args);
GameMessages::SendUIMessageServerToSingleClient(player, player->GetSystemAddress(), "QueueChoiceBox", args);
} else {
BaseOnUse(self, player);
}

View File

@ -2,7 +2,7 @@
#include "CppScripts.h"
#include "ChooseYourDestinationNsToNt.h"
#include "BaseConsoleTeleportServer.h"
#include "AMFFormat.h"
#include "Amf3.h"
class NsLupTeleport : public CppScripts::Script, ChooseYourDestinationNsToNt, BaseConsoleTeleportServer
{

View File

@ -1,6 +1,6 @@
#include "NtConsoleTeleportServer.h"
#include "Entity.h"
#include "AMFFormat.h"
#include "Amf3.h"
void NtConsoleTeleportServer::OnStartup(Entity* self) {
self->SetVar(u"teleportAnim", m_TeleportAnim);

View File

@ -1,7 +1,8 @@
#include "PropertyBankInteract.h"
#include "EntityManager.h"
#include "GameMessages.h"
#include "AMFFormat.h"
#include "Amf3.h"
#include "Entity.h"
void PropertyBankInteract::OnStartup(Entity* self) {
auto* zoneControl = EntityManager::Instance()->GetZoneControlEntity();
@ -20,11 +21,10 @@ void PropertyBankInteract::OnPlayerLoaded(Entity* self, Entity* player) {
void PropertyBankInteract::OnUse(Entity* self, Entity* user) {
AMFArrayValue args;
auto* value = new AMFStringValue();
value->SetStringValue("bank");
args.InsertValue("state", value);
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", &args);
args.Insert("state", "bank");
GameMessages::SendUIMessageServerToSingleClient(user, user->GetSystemAddress(), "pushGameState", args);
GameMessages::SendNotifyClientObject(self->GetObjectID(), u"OpenBank", 0, 0, LWOOBJID_EMPTY,
"", user->GetSystemAddress());
@ -34,9 +34,10 @@ void PropertyBankInteract::OnFireEventServerSide(Entity* self, Entity* sender, s
int32_t param2, int32_t param3) {
if (args == "ToggleBank") {
AMFArrayValue amfArgs;
amfArgs.InsertValue("visible", new AMFFalseValue());
GameMessages::SendUIMessageServerToSingleClient(sender, sender->GetSystemAddress(), "ToggleBank", &amfArgs);
amfArgs.Insert("visible", false);
GameMessages::SendUIMessageServerToSingleClient(sender, sender->GetSystemAddress(), "ToggleBank", amfArgs);
GameMessages::SendNotifyClientObject(self->GetObjectID(), u"CloseBank", 0, 0, LWOOBJID_EMPTY,
"", sender->GetSystemAddress());