From 9215ddcf8a882f570b3f56dee4307d0f1caa6259 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Fri, 21 Mar 2014 13:23:48 -0400 Subject: [PATCH] Add PbRemoveRepeatedFieldValue native (bug 6066, r=asherkin). --- core/UserMessagePBHelpers.h | 18 ++++++++++++++++++ core/smn_protobuf.cpp | 14 ++++++++++++++ plugins/include/core.inc | 1 + plugins/include/protobuf.inc | 11 +++++++++++ 4 files changed, 44 insertions(+) diff --git a/core/UserMessagePBHelpers.h b/core/UserMessagePBHelpers.h index eb7fa4ef..d0fc2b31 100644 --- a/core/UserMessagePBHelpers.h +++ b/core/UserMessagePBHelpers.h @@ -1093,6 +1093,24 @@ public: return msg->GetReflection()->FieldSize(*msg, field); } + inline bool RemoveRepeatedFieldValue(const char *pszFieldName, int index) + { + GETCHECK_FIELD(); + CHECK_FIELD_REPEATED(); + CHECK_REPEATED_ELEMENT(index); + + // Protobuf guarantees that repeated field values will stay in order and so must we. + const protobuf::Reflection *pReflection = msg->GetReflection(); + for (int i = index; i < elemCount - 1; ++i) + { + pReflection->SwapElements(msg, field, i, i + 1); + } + + pReflection->RemoveLast(msg, field); + + return true; + } + private: protobuf::Message *msg; PBHandleList childHandles; diff --git a/core/smn_protobuf.cpp b/core/smn_protobuf.cpp index 07aa8271..2188ed35 100644 --- a/core/smn_protobuf.cpp +++ b/core/smn_protobuf.cpp @@ -716,6 +716,19 @@ static cell_t smn_PbAddVector2D(IPluginContext *pCtx, const cell_t *params) return 1; } +static cell_t smn_PbRemoveRepeatedFieldValue(IPluginContext *pCtx, const cell_t *params) +{ + GET_MSG_FROM_HANDLE_OR_ERR(); + GET_FIELD_NAME_OR_ERR(); + + if (!msg->RemoveRepeatedFieldValue(strField, params[3])) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + + return 1; +} + static cell_t smn_PbReadMessage(IPluginContext *pCtx, const cell_t *params) { GET_MSG_FROM_HANDLE_OR_ERR(); @@ -794,6 +807,7 @@ REGISTER_NATIVES(protobufnatives) {"PbAddAngle", smn_PbAddAngle}, {"PbAddVector", smn_PbAddVector}, {"PbAddVector2D", smn_PbAddVector2D}, + {"PbRemoveRepeatedFieldValue", smn_PbRemoveRepeatedFieldValue}, {"PbReadMessage", smn_PbReadMessage}, {"PbReadRepeatedMessage", smn_PbReadRepeatedMessage}, {"PbAddMessage", smn_PbAddMessage}, diff --git a/plugins/include/core.inc b/plugins/include/core.inc index 05b12663..446d8591 100644 --- a/plugins/include/core.inc +++ b/plugins/include/core.inc @@ -223,6 +223,7 @@ public __ext_core_SetNTVOptional() MarkNativeAsOptional("PbAddAngle"); MarkNativeAsOptional("PbAddVector"); MarkNativeAsOptional("PbAddVector2D"); + MarkNativeAsOptional("PbRemoveRepeatedFieldValue"); MarkNativeAsOptional("PbReadMessage"); MarkNativeAsOptional("PbReadRepeatedMessage"); MarkNativeAsOptional("PbAddMessage"); diff --git a/plugins/include/protobuf.inc b/plugins/include/protobuf.inc index ced88975..976ca88a 100644 --- a/plugins/include/protobuf.inc +++ b/plugins/include/protobuf.inc @@ -325,6 +325,17 @@ native PbAddVector(Handle:pb, const String:field[], const Float:vec[3]); */ native PbAddVector2D(Handle:pb, const String:field[], const Float:vec[2]); +/** + * Removes a value by index from a protobuf message repeated field. + * + * @param pb protobuf handle. + * @param field Field name. + * @param index Index into repeated field. + * @noreturn + * @error Invalid or incorrect Handle, non-existent field, or incorrect field type. + */ +native PbRemoveRepeatedFieldValue(Handle:pb, const String:field[], index); + /** * Retrieve a handle to an embedded protobuf message in a protobuf message. *