From a17b47a2dcc9b9e2de523eaa8e6b1874f33e9eeb Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sun, 25 Feb 2018 10:49:37 -0500 Subject: [PATCH] Add Protobuf.HasField native. --- core/UserMessagePBHelpers.h | 7 +++++++ core/smn_protobuf.cpp | 9 +++++++++ plugins/include/protobuf.inc | 9 ++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/UserMessagePBHelpers.h b/core/UserMessagePBHelpers.h index fba9d656..29d5bd0d 100644 --- a/core/UserMessagePBHelpers.h +++ b/core/UserMessagePBHelpers.h @@ -118,6 +118,13 @@ public: return msg; } + inline bool HasField(const char *pszFieldName) + { + GETCHECK_FIELD(); + CHECK_FIELD_NOT_REPEATED(); + return msg->GetReflection()->HasField(*msg, field); + } + inline bool GetInt32(const char *pszFieldName, int32 *out) { GETCHECK_FIELD(); diff --git a/core/smn_protobuf.cpp b/core/smn_protobuf.cpp index 919eb653..004b5554 100644 --- a/core/smn_protobuf.cpp +++ b/core/smn_protobuf.cpp @@ -304,6 +304,14 @@ static cell_t smn_PbGetRepeatedFieldCount(IPluginContext *pCtx, const cell_t *pa return cnt; } +static cell_t smn_PbHasField(IPluginContext *pCtx, const cell_t *params) +{ + GET_MSG_FROM_HANDLE_OR_ERR(); + GET_FIELD_NAME_OR_ERR(); + + return msg->HasField(strField) ? 1 : 0; +} + static cell_t smn_PbSetInt(IPluginContext *pCtx, const cell_t *params) { GET_MSG_FROM_HANDLE_OR_ERR(); @@ -778,6 +786,7 @@ REGISTER_NATIVES(protobufnatives) {"Protobuf.ReadVector", smn_PbReadVector}, {"Protobuf.ReadVector2D", smn_PbReadVector2D}, {"Protobuf.GetRepeatedFieldCount", smn_PbGetRepeatedFieldCount}, + {"Protobuf.HasField", smn_PbHasField}, {"Protobuf.SetInt", smn_PbSetInt}, {"Protobuf.SetFloat", smn_PbSetFloat}, {"Protobuf.SetBool", smn_PbSetBool}, diff --git a/plugins/include/protobuf.inc b/plugins/include/protobuf.inc index 56bebc5f..99dd38bb 100644 --- a/plugins/include/protobuf.inc +++ b/plugins/include/protobuf.inc @@ -109,8 +109,15 @@ methodmap Protobuf < Handle // // @param field Field name. // @return Number of elements in the field. - // @error Non-existent field, or incorrect field type. + // @error Non-existent field, or non-repeated field. public native int GetRepeatedFieldCount(const char[] field); + + // Returns whether or not the named, non-repeated field has a value set. + // + // @param field Field name. + // @return True if value has been set, else false. + // @error Non-existent field, or repeated field. + public native bool HasField(const char[] field); // Sets an int32, uint32, sint32, fixed32, sfixed32, or enum value on a protobuf message. //