Added repeated field handling to PbRead* natives. Deprecate PbReadRepeated* (bug 5633, r=asherkin).

This commit is contained in:
Nicholas Hastings 2013-03-16 13:31:35 -04:00
parent e155e258cb
commit 42b415952b
2 changed files with 134 additions and 24 deletions

View File

@ -69,10 +69,22 @@ static cell_t smn_PbReadInt(IPluginContext *pCtx, const cell_t *params)
GET_FIELD_NAME_OR_ERR(); GET_FIELD_NAME_OR_ERR();
int ret; int ret;
int index = params[0] >= 3 ? params[3] : -1;
if (index < 0)
{
if (!msg->GetInt32OrUnsigned(strField, &ret)) if (!msg->GetInt32OrUnsigned(strField, &ret))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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; return ret;
} }
@ -83,10 +95,22 @@ static cell_t smn_PbReadFloat(IPluginContext *pCtx, const cell_t *params)
GET_FIELD_NAME_OR_ERR(); GET_FIELD_NAME_OR_ERR();
float ret; float ret;
int index = params[0] >= 3 ? params[3] : -1;
if (index < 0)
{
if (!msg->GetFloatOrDouble(strField, &ret)) if (!msg->GetFloatOrDouble(strField, &ret))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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); return sp_ftoc(ret);
} }
@ -97,10 +121,22 @@ static cell_t smn_PbReadBool(IPluginContext *pCtx, const cell_t *params)
GET_FIELD_NAME_OR_ERR(); GET_FIELD_NAME_OR_ERR();
bool ret; bool ret;
int index = params[0] >= 3 ? params[3] : -1;
if (index < 0)
{
if (!msg->GetBool(strField, &ret)) if (!msg->GetBool(strField, &ret))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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; return ret ? 1 : 0;
} }
@ -113,10 +149,22 @@ static cell_t smn_PbReadString(IPluginContext *pCtx, const cell_t *params)
char *buf; char *buf;
pCtx->LocalToPhysAddr(params[3], (cell_t **)&buf); pCtx->LocalToPhysAddr(params[3], (cell_t **)&buf);
int index = params[0] >= 5 ? params[5] : -1;
if (index < 0)
{
if (!msg->GetString(strField, buf, params[4])) if (!msg->GetString(strField, buf, params[4]))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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; return 1;
} }
@ -130,10 +178,21 @@ static cell_t smn_PbReadColor(IPluginContext *pCtx, const cell_t *params)
pCtx->LocalToPhysAddr(params[3], &out); pCtx->LocalToPhysAddr(params[3], &out);
Color clr; Color clr;
int index = params[0] >= 4 ? params[4] : -1;
if (index < 0)
{
if (!msg->GetColor(strField, &clr)) if (!msg->GetColor(strField, &clr))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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(); out[0] = clr.r();
out[1] = clr.g(); out[1] = clr.g();
@ -152,10 +211,21 @@ static cell_t smn_PbReadAngle(IPluginContext *pCtx, const cell_t *params)
pCtx->LocalToPhysAddr(params[3], &out); pCtx->LocalToPhysAddr(params[3], &out);
QAngle ang; QAngle ang;
int index = params[0] >= 4 ? params[4] : -1;
if (index < 0)
{
if (!msg->GetQAngle(strField, &ang)) if (!msg->GetQAngle(strField, &ang))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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); out[0] = sp_ftoc(ang.x);
out[1] = sp_ftoc(ang.y); out[1] = sp_ftoc(ang.y);
@ -173,10 +243,21 @@ static cell_t smn_PbReadVector(IPluginContext *pCtx, const cell_t *params)
pCtx->LocalToPhysAddr(params[3], &out); pCtx->LocalToPhysAddr(params[3], &out);
Vector vec; Vector vec;
int index = params[0] >= 4 ? params[4] : -1;
if (index < 0)
{
if (!msg->GetVector(strField, &vec)) if (!msg->GetVector(strField, &vec))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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); out[0] = sp_ftoc(vec.x);
out[1] = sp_ftoc(vec.y); out[1] = sp_ftoc(vec.y);
@ -194,10 +275,21 @@ static cell_t smn_PbReadVector2D(IPluginContext *pCtx, const cell_t *params)
pCtx->LocalToPhysAddr(params[3], &out); pCtx->LocalToPhysAddr(params[3], &out);
Vector2D vec; Vector2D vec;
int index = params[0] >= 4 ? params[4] : -1;
if (index < 0)
{
if (!msg->GetVector2D(strField, &vec)) if (!msg->GetVector2D(strField, &vec))
{ {
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str()); 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); out[0] = sp_ftoc(vec.x);
out[1] = sp_ftoc(vec.y); out[1] = sp_ftoc(vec.y);

View File

@ -35,35 +35,40 @@
#endif #endif
#define _protobuf_included #define _protobuf_included
#define PB_FIELD_NOT_REPEATED -1
/** /**
* Reads an int32, uint32, sint32, fixed32, or sfixed32 from a protobuf message. * Reads an int32, uint32, sint32, fixed32, or sfixed32 from a protobuf message.
* *
* @param pb protobuf handle. * @param pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param index Index into repeated field.
* @return Integer value read. * @return Integer value read.
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * Reads a float or downcasted double from a protobuf message.
* *
* @param pb protobuf handle. * @param pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param index Index into repeated field.
* @return Float value read. * @return Float value read.
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * Reads a bool from a protobuf message.
* *
* @param pb protobuf handle. * @param pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param index Index into repeated field.
* @return Boolean value read. * @return Boolean value read.
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * Reads a string from a protobuf message.
@ -72,10 +77,11 @@ native bool:PbReadBool(Handle:pb, const String:field[]);
* @param field Field name. * @param field Field name.
* @param buffer Destination string buffer. * @param buffer Destination string buffer.
* @param maxlength Maximum length of output string buffer. * @param maxlength Maximum length of output string buffer.
* @param index Index into repeated field.
* @noreturn * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * 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 pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param buffer Destination color buffer. * @param buffer Destination color buffer.
* @param index Index into repeated field.
* @noreturn * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * 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 pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param buffer Destination angle buffer. * @param buffer Destination angle buffer.
* @param index Index into repeated field.
* @noreturn * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * 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 pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param buffer Destination vector buffer. * @param buffer Destination vector buffer.
* @param index Index into repeated field.
* @noreturn * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * 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 pb protobuf handle.
* @param field Field name. * @param field Field name.
* @param buffer Destination vector buffer. * @param buffer Destination vector buffer.
* @param index Index into repeated field.
* @noreturn * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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. * 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. * @return Integer value read.
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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); native PbReadRepeatedInt(Handle:pb, const String:field[], index);
/** /**
@ -151,6 +162,7 @@ native PbReadRepeatedInt(Handle:pb, const String:field[], index);
* @return Float value read. * @return Float value read.
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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); 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. * @return Boolean value read.
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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); native bool:PbReadRepeatedBool(Handle:pb, const String:field[], index);
/** /**
@ -175,6 +188,7 @@ native bool:PbReadRepeatedBool(Handle:pb, const String:field[], index);
* @noreturn * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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); 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 * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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]); 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 * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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]); 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 * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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]); 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 * @noreturn
* @error Invalid or incorrect Handle, non-existant field, or incorrect field type. * @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]); native PbReadRepeatedVector2D(Handle:pb, const String:field[], index, Float:buffer[2]);
/** /**