From b8b322f4ce908e798e71ef591d588a04bbb03565 Mon Sep 17 00:00:00 2001 From: Boondorl Date: Thu, 4 Jan 2024 16:14:55 -0500 Subject: [PATCH] Added wrapper functions for adding and reading arrays --- src/events.cpp | 116 ++++++++++++++++++++++++++++++++ wadsrc/static/zscript/events.zs | 6 ++ 2 files changed, 122 insertions(+) diff --git a/src/events.cpp b/src/events.cpp index 04bc39bc8b..98f5fb5d8d 100755 --- a/src/events.cpp +++ b/src/events.cpp @@ -934,6 +934,67 @@ DEFINE_ACTION_FUNCTION(FNetworkCommand, ReadVector3) ACTION_RETURN_VEC3(vec); } +DEFINE_ACTION_FUNCTION(FNetworkCommand, ReadIntArray) +{ + PARAM_SELF_STRUCT_PROLOGUE(FNetworkCommand); + PARAM_OUTPOINTER(values, TArray); + PARAM_INT(type) + + unsigned int size = self->ReadLong(); + for (unsigned int i = 0u; i < size; ++i) + { + switch (type) + { + case NET_BYTE: + values->Push(self->ReadByte()); + break; + + case NET_WORD: + values->Push(self->ReadWord()); + break; + + default: + values->Push(self->ReadLong()); + break; + } + } + + return 0; +} + +DEFINE_ACTION_FUNCTION(FNetworkCommand, ReadFloatArray) +{ + PARAM_SELF_STRUCT_PROLOGUE(FNetworkCommand); + PARAM_OUTPOINTER(values, TArray); + + unsigned int size = self->ReadLong(); + for (unsigned int i = 0u; i < size; ++i) + values->Push(self->ReadFloat()); + + return 0; +} + +DEFINE_ACTION_FUNCTION(FNetworkCommand, ReadStringArray) +{ + PARAM_SELF_STRUCT_PROLOGUE(FNetworkCommand); + PARAM_OUTPOINTER(values, TArray); + PARAM_BOOL(skipEmpty); + + unsigned int size = self->ReadLong(); + for (unsigned int i = 0u; i < size; ++i) + { + FString res = {}; + auto str = self->ReadString(); + if (str != nullptr) + res = str; + + if (!skipEmpty || !res.IsEmpty()) + values->Push(res); + } + + return 0; +} + DEFINE_ACTION_FUNCTION(FNetworkBuffer, AddByte) { PARAM_SELF_STRUCT_PROLOGUE(FNetworkBuffer); @@ -1004,6 +1065,61 @@ DEFINE_ACTION_FUNCTION(FNetworkBuffer, AddVector3) return 0; } +DEFINE_ACTION_FUNCTION(FNetworkBuffer, AddIntArray) +{ + PARAM_SELF_STRUCT_PROLOGUE(FNetworkBuffer); + PARAM_POINTER(values, TArray); + PARAM_INT(type); + + unsigned int size = values->Size(); + self->AddLong(size); + for (unsigned int i = 0u; i < size; ++i) + { + switch (type) + { + case NET_BYTE: + self->AddByte((*values)[i]); + break; + + case NET_WORD: + self->AddWord((*values)[i]); + break; + + default: + self->AddLong((*values)[i]); + break; + } + } + + return 0; +} + +DEFINE_ACTION_FUNCTION(FNetworkBuffer, AddFloatArray) +{ + PARAM_SELF_STRUCT_PROLOGUE(FNetworkBuffer); + PARAM_POINTER(values, TArray); + + unsigned int size = values->Size(); + self->AddLong(size); + for (unsigned int i = 0u; i < size; ++i) + self->AddFloat((*values)[i]); + + return 0; +} + +DEFINE_ACTION_FUNCTION(FNetworkBuffer, AddStringArray) +{ + PARAM_SELF_STRUCT_PROLOGUE(FNetworkBuffer); + PARAM_POINTER(values, TArray); + + unsigned int size = values->Size(); + self->AddLong(size); + for (unsigned int i = 0u; i < size; ++i) + self->AddString((*values)[i]); + + return 0; +} + DEFINE_ACTION_FUNCTION(DStaticEventHandler, SetOrder) { PARAM_SELF_PROLOGUE(DStaticEventHandler); diff --git a/wadsrc/static/zscript/events.zs b/wadsrc/static/zscript/events.zs index 6d4115210c..297753908f 100644 --- a/wadsrc/static/zscript/events.zs +++ b/wadsrc/static/zscript/events.zs @@ -22,6 +22,9 @@ struct NetworkCommand native play version("4.12") native Name ReadName(); native Vector2 ReadVector2(); native Vector3 ReadVector3(); + native void ReadIntArray(out Array values, ENetCmd intSize = NET_LONG); + native void ReadFloatArray(out Array values); + native void ReadStringArray(out Array values, bool skipEmpty = false); } struct NetworkBuffer native version("4.12") @@ -36,6 +39,9 @@ struct NetworkBuffer native version("4.12") native void AddName(Name value); native void AddVector2(Vector2 value); native void AddVector3(Vector3 value); + native void AddIntArray(Array values, ENetCmd intSize = NET_LONG); + native void AddFloatArray(Array values); + native void AddStringArray(Array values); } struct RenderEvent native ui version("2.4")