From 42b415952bc606fff1ee94ff3a2cde3d721b5cc0 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Sat, 16 Mar 2013 13:31:35 -0400 Subject: [PATCH] Added repeated field handling to PbRead* natives. Deprecate PbReadRepeated* (bug 5633, r=asherkin). --- core/smn_protobuf.cpp | 124 ++++++++++++++++++++++++++++++----- plugins/include/protobuf.inc | 34 +++++++--- 2 files changed, 134 insertions(+), 24 deletions(-) diff --git a/core/smn_protobuf.cpp b/core/smn_protobuf.cpp index cd7c3456..92f8b132 100644 --- a/core/smn_protobuf.cpp +++ b/core/smn_protobuf.cpp @@ -69,9 +69,21 @@ static cell_t smn_PbReadInt(IPluginContext *pCtx, const cell_t *params) GET_FIELD_NAME_OR_ERR(); int ret; - if (!msg->GetInt32OrUnsigned(strField, &ret)) + + int index = params[0] >= 3 ? params[3] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetInt32OrUnsigned(strField, &ret)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedInt32OrUnsigned(strField, index, &ret)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } return ret; @@ -83,9 +95,21 @@ static cell_t smn_PbReadFloat(IPluginContext *pCtx, const cell_t *params) GET_FIELD_NAME_OR_ERR(); float ret; - if (!msg->GetFloatOrDouble(strField, &ret)) + + int index = params[0] >= 3 ? params[3] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetFloatOrDouble(strField, &ret)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedFloatOrDouble(strField, index, &ret)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } return sp_ftoc(ret); @@ -97,9 +121,21 @@ static cell_t smn_PbReadBool(IPluginContext *pCtx, const cell_t *params) GET_FIELD_NAME_OR_ERR(); bool ret; - if (!msg->GetBool(strField, &ret)) + + int index = params[0] >= 3 ? params[3] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetBool(strField, &ret)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedBool(strField, index, &ret)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } return ret ? 1 : 0; @@ -113,9 +149,21 @@ static cell_t smn_PbReadString(IPluginContext *pCtx, const cell_t *params) char *buf; pCtx->LocalToPhysAddr(params[3], (cell_t **)&buf); - if (!msg->GetString(strField, buf, params[4])) + int index = params[0] >= 5 ? params[5] : -1; + + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetString(strField, buf, params[4])) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedString(strField, index, buf, params[4])) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } return 1; @@ -130,9 +178,20 @@ static cell_t smn_PbReadColor(IPluginContext *pCtx, const cell_t *params) pCtx->LocalToPhysAddr(params[3], &out); Color clr; - if (!msg->GetColor(strField, &clr)) + int index = params[0] >= 4 ? params[4] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetColor(strField, &clr)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedColor(strField, index, &clr)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } out[0] = clr.r(); @@ -152,9 +211,20 @@ static cell_t smn_PbReadAngle(IPluginContext *pCtx, const cell_t *params) pCtx->LocalToPhysAddr(params[3], &out); QAngle ang; - if (!msg->GetQAngle(strField, &ang)) + int index = params[0] >= 4 ? params[4] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetQAngle(strField, &ang)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedQAngle(strField, index, &ang)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } out[0] = sp_ftoc(ang.x); @@ -173,9 +243,20 @@ static cell_t smn_PbReadVector(IPluginContext *pCtx, const cell_t *params) pCtx->LocalToPhysAddr(params[3], &out); Vector vec; - if (!msg->GetVector(strField, &vec)) + int index = params[0] >= 4 ? params[4] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetVector(strField, &vec)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedVector(strField, index, &vec)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } out[0] = sp_ftoc(vec.x); @@ -194,9 +275,20 @@ static cell_t smn_PbReadVector2D(IPluginContext *pCtx, const cell_t *params) pCtx->LocalToPhysAddr(params[3], &out); Vector2D vec; - if (!msg->GetVector2D(strField, &vec)) + int index = params[0] >= 4 ? params[4] : -1; + if (index < 0) { - return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + if (!msg->GetVector2D(strField, &vec)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); + } + } + else + { + if (!msg->GetRepeatedVector2D(strField, index, &vec)) + { + return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str()); + } } out[0] = sp_ftoc(vec.x); diff --git a/plugins/include/protobuf.inc b/plugins/include/protobuf.inc index db982f9d..c0248678 100644 --- a/plugins/include/protobuf.inc +++ b/plugins/include/protobuf.inc @@ -35,35 +35,40 @@ #endif #define _protobuf_included +#define PB_FIELD_NOT_REPEATED -1 + /** * Reads an int32, uint32, sint32, fixed32, or sfixed32 from a protobuf message. * * @param pb protobuf handle. * @param field Field name. + * @param index Index into repeated field. * @return Integer value read. * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native PbReadInt(Handle:pb, const String:field[]); +native PbReadInt(Handle:pb, const String:field[], index=PB_FIELD_NOT_REPEATED); /** * Reads a float or downcasted double from a protobuf message. * * @param pb protobuf handle. * @param field Field name. + * @param index Index into repeated field. * @return Float value read. * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native Float:PbReadFloat(Handle:pb, const String:field[]); +native Float:PbReadFloat(Handle:pb, const String:field[], index=PB_FIELD_NOT_REPEATED); /** * Reads a bool from a protobuf message. * * @param pb protobuf handle. * @param field Field name. + * @param index Index into repeated field. * @return Boolean value read. * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native bool:PbReadBool(Handle:pb, const String:field[]); +native bool:PbReadBool(Handle:pb, const String:field[], index=PB_FIELD_NOT_REPEATED); /** * Reads a string from a protobuf message. @@ -72,10 +77,11 @@ native bool:PbReadBool(Handle:pb, const String:field[]); * @param field Field name. * @param buffer Destination string buffer. * @param maxlength Maximum length of output string buffer. + * @param index Index into repeated field. * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native PbReadString(Handle:pb, const String:field[], String:buffer[], maxlength); +native PbReadString(Handle:pb, const String:field[], String:buffer[], maxlength, index=PB_FIELD_NOT_REPEATED); /** * Reads an RGBA color value from a protobuf message. @@ -83,10 +89,11 @@ native PbReadString(Handle:pb, const String:field[], String:buffer[], maxlength) * @param pb protobuf handle. * @param field Field name. * @param buffer Destination color buffer. + * @param index Index into repeated field. * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native PbReadColor(Handle:pb, const String:field[], buffer[4]); +native PbReadColor(Handle:pb, const String:field[], buffer[4], index=PB_FIELD_NOT_REPEATED); /** * Reads an XYZ angle value from a protobuf message. @@ -94,10 +101,11 @@ native PbReadColor(Handle:pb, const String:field[], buffer[4]); * @param pb protobuf handle. * @param field Field name. * @param buffer Destination angle buffer. + * @param index Index into repeated field. * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native PbReadAngle(Handle:pb, const String:field[], Float:buffer[3]); +native PbReadAngle(Handle:pb, const String:field[], Float:buffer[3], index=PB_FIELD_NOT_REPEATED); /** * Reads an XYZ vector value from a protobuf message. @@ -105,10 +113,11 @@ native PbReadAngle(Handle:pb, const String:field[], Float:buffer[3]); * @param pb protobuf handle. * @param field Field name. * @param buffer Destination vector buffer. + * @param index Index into repeated field. * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native PbReadVector(Handle:pb, const String:field[], Float:buffer[3]); +native PbReadVector(Handle:pb, const String:field[], Float:buffer[3], index=PB_FIELD_NOT_REPEATED); /** * Reads an XY vector value from a protobuf message. @@ -116,10 +125,11 @@ native PbReadVector(Handle:pb, const String:field[], Float:buffer[3]); * @param pb protobuf handle. * @param field Field name. * @param buffer Destination vector buffer. + * @param index Index into repeated field. * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ -native PbReadVector2D(Handle:pb, const String:field[], Float:buffer[2]); +native PbReadVector2D(Handle:pb, const String:field[], Float:buffer[2], index=PB_FIELD_NOT_REPEATED); /** * Gets the number of elements in a repeated field of a protobuf message. @@ -140,6 +150,7 @@ native PbGetRepeatedFieldCount(Handle:pb, const String:field[]); * @return Integer value read. * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadInt with index param native PbReadRepeatedInt(Handle:pb, const String:field[], index); /** @@ -151,6 +162,7 @@ native PbReadRepeatedInt(Handle:pb, const String:field[], index); * @return Float value read. * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadFloat with index param native Float:PbReadRepeatedFloat(Handle:pb, const String:field[], index); /** @@ -162,6 +174,7 @@ native Float:PbReadRepeatedFloat(Handle:pb, const String:field[], index); * @return Boolean value read. * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadBool with index param native bool:PbReadRepeatedBool(Handle:pb, const String:field[], index); /** @@ -175,6 +188,7 @@ native bool:PbReadRepeatedBool(Handle:pb, const String:field[], index); * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadString with index param native PbReadRepeatedString(Handle:pb, const String:field[], index, String:buffer[], size); /** @@ -187,6 +201,7 @@ native PbReadRepeatedString(Handle:pb, const String:field[], index, String:buffe * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadColor with index param native PbReadRepeatedColor(Handle:pb, const String:field[], index, buffer[4]); /** @@ -199,6 +214,7 @@ native PbReadRepeatedColor(Handle:pb, const String:field[], index, buffer[4]); * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadAngle with index param native PbReadRepeatedAngle(Handle:pb, const String:field[], index, Float:buffer[3]); /** @@ -211,6 +227,7 @@ native PbReadRepeatedAngle(Handle:pb, const String:field[], index, Float:buffer[ * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadVector with index param native PbReadRepeatedVector(Handle:pb, const String:field[], index, Float:buffer[3]); /** @@ -223,6 +240,7 @@ native PbReadRepeatedVector(Handle:pb, const String:field[], index, Float:buffer * @noreturn * @error Invalid or incorrect Handle, non-existant field, or incorrect field type. */ +#pragma deprecated use PbReadVector2D with index param native PbReadRepeatedVector2D(Handle:pb, const String:field[], index, Float:buffer[2]); /**