core/smn_protobuf.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sat Aug 10 12:22:09 2013 -0400 (2013-08-10)
changeset 3981 af3681eade3f
parent 3898 6b8333229344
child 4293 f61ae385f12e
permissions -rw-r--r--
Debug build fixes.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2013 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "logic_bridge.h"
    33 #include "UserMessages.h"
    34 #include "UserMessagePBHelpers.h"
    35 #include "smn_usermsgs.h"
    36 
    37 // Assumes pbuf message handle is param 1, gets message as msg
    38 #define GET_MSG_FROM_HANDLE_OR_ERR()                  \
    39 	Handle_t hndl = static_cast<Handle_t>(params[1]); \
    40 	HandleError herr;                                 \
    41 	HandleSecurity sec;                               \
    42 	SMProtobufMessage *msg;                           \
    43 	                                                  \
    44 	sec.pOwner = NULL;                                \
    45 	sec.pIdentity = g_pCoreIdent;                     \
    46 	                                                  \
    47 	if ((herr=handlesys->ReadHandle(hndl, g_ProtobufType, &sec, (void **)&msg)) \
    48 		!= HandleError_None)                          \
    49 	{                                                 \
    50 		return pCtx->ThrowNativeError("Invalid protobuf message handle %x (error %d)", hndl, herr); \
    51 	}
    52 
    53 // Assumes message field name is param 2, gets as strField
    54 #define GET_FIELD_NAME_OR_ERR()                                           \
    55 	int err;                                                              \
    56 	char *strField;                                                       \
    57 	if ((err=pCtx->LocalToString(params[2], &strField)) != SP_ERROR_NONE) \
    58 	{                                                                     \
    59 		pCtx->ThrowNativeErrorEx(err, NULL);                              \
    60 		return 0;                                                         \
    61 	}
    62 
    63 static cell_t smn_PbReadInt(IPluginContext *pCtx, const cell_t *params)
    64 {
    65 	GET_MSG_FROM_HANDLE_OR_ERR();
    66 	GET_FIELD_NAME_OR_ERR();
    67 
    68 	int ret;
    69 
    70 	int index = params[0] >= 3 ? params[3] : -1;
    71 	if (index < 0)
    72 	{
    73 		if (!msg->GetInt32OrUnsignedOrEnum(strField, &ret))
    74 		{
    75 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
    76 		}
    77 	}
    78 	else
    79 	{
    80 		if (!msg->GetRepeatedInt32OrUnsignedOrEnum(strField, index, &ret))
    81 		{
    82 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
    83 		}
    84 	}
    85 
    86 	return ret;
    87 }
    88 
    89 static cell_t smn_PbReadFloat(IPluginContext *pCtx, const cell_t *params)
    90 {
    91 	GET_MSG_FROM_HANDLE_OR_ERR();
    92 	GET_FIELD_NAME_OR_ERR();
    93 
    94 	float ret;
    95 
    96 	int index = params[0] >= 3 ? params[3] : -1;
    97 	if (index < 0)
    98 	{
    99 		if (!msg->GetFloatOrDouble(strField, &ret))
   100 		{
   101 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   102 		}
   103 	}
   104 	else
   105 	{
   106 		if (!msg->GetRepeatedFloatOrDouble(strField, index, &ret))
   107 		{
   108 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   109 		}
   110 	}
   111 
   112 	return sp_ftoc(ret);
   113 }
   114 
   115 static cell_t smn_PbReadBool(IPluginContext *pCtx, const cell_t *params)
   116 {
   117 	GET_MSG_FROM_HANDLE_OR_ERR();
   118 	GET_FIELD_NAME_OR_ERR();
   119 
   120 	bool ret;
   121 
   122 	int index = params[0] >= 3 ? params[3] : -1;
   123 	if (index < 0)
   124 	{
   125 		if (!msg->GetBool(strField, &ret))
   126 		{
   127 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   128 		}
   129 	}
   130 	else
   131 	{
   132 		if (!msg->GetRepeatedBool(strField, index, &ret))
   133 		{
   134 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   135 		} 
   136 	}
   137 
   138 	return ret ? 1 : 0;
   139 }
   140 
   141 static cell_t smn_PbReadString(IPluginContext *pCtx, const cell_t *params)
   142 {
   143 	GET_MSG_FROM_HANDLE_OR_ERR();
   144 	GET_FIELD_NAME_OR_ERR();
   145 
   146 	char *buf;
   147 	pCtx->LocalToPhysAddr(params[3], (cell_t **)&buf);
   148 
   149 	int index = params[0] >= 5 ? params[5] : -1;
   150 
   151 	if (index < 0)
   152 	{
   153 		if (!msg->GetString(strField, buf, params[4]))
   154 		{
   155 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   156 		}
   157 	}
   158 	else
   159 	{
   160 		if (!msg->GetRepeatedString(strField, index, buf, params[4]))
   161 		{
   162 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   163 		}
   164 	}
   165 
   166 	return 1;
   167 }
   168 
   169 static cell_t smn_PbReadColor(IPluginContext *pCtx, const cell_t *params)
   170 {
   171 	GET_MSG_FROM_HANDLE_OR_ERR();
   172 	GET_FIELD_NAME_OR_ERR();
   173 
   174 	cell_t *out;
   175 	pCtx->LocalToPhysAddr(params[3], &out);
   176 
   177 	Color clr;
   178 	int index = params[0] >= 4 ? params[4] : -1;
   179 	if (index < 0)
   180 	{
   181 		if (!msg->GetColor(strField, &clr))
   182 		{
   183 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   184 		}
   185 	}
   186 	else
   187 	{
   188 		if (!msg->GetRepeatedColor(strField, index, &clr))
   189 		{
   190 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   191 		}
   192 	}
   193 
   194 	out[0] = clr.r();
   195 	out[1] = clr.g();
   196 	out[2] = clr.b();
   197 	out[3] = clr.a();
   198 
   199 	return 1;
   200 }
   201 
   202 static cell_t smn_PbReadAngle(IPluginContext *pCtx, const cell_t *params)
   203 {
   204 	GET_MSG_FROM_HANDLE_OR_ERR();
   205 	GET_FIELD_NAME_OR_ERR();
   206 
   207 	cell_t *out;
   208 	pCtx->LocalToPhysAddr(params[3], &out);
   209 
   210 	QAngle ang;
   211 	int index = params[0] >= 4 ? params[4] : -1;
   212 	if (index < 0)
   213 	{
   214 		if (!msg->GetQAngle(strField, &ang))
   215 		{
   216 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   217 		}
   218 	}
   219 	else
   220 	{
   221 		if (!msg->GetRepeatedQAngle(strField, index, &ang))
   222 		{
   223 		return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   224 		}
   225 	}
   226 
   227 	out[0] = sp_ftoc(ang.x);
   228 	out[1] = sp_ftoc(ang.y);
   229 	out[2] = sp_ftoc(ang.z);
   230 
   231 	return 1;
   232 }
   233 
   234 static cell_t smn_PbReadVector(IPluginContext *pCtx, const cell_t *params)
   235 {
   236 	GET_MSG_FROM_HANDLE_OR_ERR();
   237 	GET_FIELD_NAME_OR_ERR();
   238 
   239 	cell_t *out;
   240 	pCtx->LocalToPhysAddr(params[3], &out);
   241 
   242 	Vector vec;
   243 	int index = params[0] >= 4 ? params[4] : -1;
   244 	if (index < 0)
   245 	{
   246 		if (!msg->GetVector(strField, &vec))
   247 		{
   248 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   249 		}
   250 	}
   251 	else
   252 	{
   253 		if (!msg->GetRepeatedVector(strField, index, &vec))
   254 		{
   255 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   256 		}
   257 	}
   258 
   259 	out[0] = sp_ftoc(vec.x);
   260 	out[1] = sp_ftoc(vec.y);
   261 	out[2] = sp_ftoc(vec.z);
   262 
   263 	return 1;
   264 }
   265 
   266 static cell_t smn_PbReadVector2D(IPluginContext *pCtx, const cell_t *params)
   267 {
   268 	GET_MSG_FROM_HANDLE_OR_ERR();
   269 	GET_FIELD_NAME_OR_ERR();
   270 
   271 	cell_t *out;
   272 	pCtx->LocalToPhysAddr(params[3], &out);
   273 
   274 	Vector2D vec;
   275 	int index = params[0] >= 4 ? params[4] : -1;
   276 	if (index < 0)
   277 	{
   278 		if (!msg->GetVector2D(strField, &vec))
   279 		{
   280 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   281 		}
   282 	}
   283 	else
   284 	{
   285 		if (!msg->GetRepeatedVector2D(strField, index, &vec))
   286 		{
   287 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   288 		}
   289 	}
   290 
   291 	out[0] = sp_ftoc(vec.x);
   292 	out[1] = sp_ftoc(vec.y);
   293 
   294 	return 1;
   295 }
   296 
   297 static cell_t smn_PbGetRepeatedFieldCount(IPluginContext *pCtx, const cell_t *params)
   298 {
   299 	GET_MSG_FROM_HANDLE_OR_ERR();
   300 	GET_FIELD_NAME_OR_ERR();
   301 
   302 	int cnt = msg->GetRepeatedFieldCount(strField);
   303 	if (cnt == -1)
   304 	{
   305 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   306 	}
   307 
   308 	return cnt;
   309 }
   310 
   311 static cell_t smn_PbSetInt(IPluginContext *pCtx, const cell_t *params)
   312 {
   313 	GET_MSG_FROM_HANDLE_OR_ERR();
   314 	GET_FIELD_NAME_OR_ERR();
   315 
   316 	int index = params[0] >= 4 ? params[4] : -1;
   317 	if (index < 0)
   318 	{
   319 		if (!msg->SetInt32OrUnsignedOrEnum(strField, params[3]))
   320 		{
   321 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   322 		}
   323 	}
   324 	else
   325 	{
   326 		if (!msg->SetRepeatedInt32OrUnsignedOrEnum(strField, index, params[3]))
   327 		{
   328 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   329 		}
   330 	}
   331 
   332 	return 1;
   333 }
   334 
   335 static cell_t smn_PbSetFloat(IPluginContext *pCtx, const cell_t *params)
   336 {
   337 	GET_MSG_FROM_HANDLE_OR_ERR();
   338 	GET_FIELD_NAME_OR_ERR();
   339 
   340 	int index = params[0] >= 4 ? params[4] : -1;
   341 	if (index < 0)
   342 	{
   343 		if (!msg->SetFloatOrDouble(strField, sp_ctof(params[3])))
   344 		{
   345 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   346 		}
   347 	}
   348 	else
   349 	{
   350 		if (!msg->SetRepeatedFloatOrDouble(strField, index, sp_ctof(params[3])))
   351 		{
   352 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   353 		}
   354 	}
   355 
   356 	return 1;
   357 }
   358 
   359 static cell_t smn_PbSetBool(IPluginContext *pCtx, const cell_t *params)
   360 {
   361 	GET_MSG_FROM_HANDLE_OR_ERR();
   362 	GET_FIELD_NAME_OR_ERR();
   363 
   364 	bool value = (params[3] == 0 ? false : true);
   365 	int index = params[0] >= 4 ? params[4] : -1;
   366 	if (index < 0)
   367 	{
   368 		if (!msg->SetBool(strField, value))
   369 		{
   370 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   371 		}
   372 	}
   373 	else
   374 	{
   375 		if (!msg->SetRepeatedBool(strField, index, value))
   376 		{
   377 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   378 		}
   379 	}
   380 
   381 	return 1;
   382 }
   383 
   384 static cell_t smn_PbSetString(IPluginContext *pCtx, const cell_t *params)
   385 {
   386 	GET_MSG_FROM_HANDLE_OR_ERR();
   387 	GET_FIELD_NAME_OR_ERR();
   388 
   389 	char *strValue;
   390 	if ((err=pCtx->LocalToString(params[3], &strValue)) != SP_ERROR_NONE)
   391 	{
   392 		pCtx->ThrowNativeErrorEx(err, NULL);
   393 		return 0;
   394 	}
   395 
   396 	int index = params[0] >= 4 ? params[4] : -1;
   397 	if (index < 0)
   398 	{
   399 		if (!msg->SetString(strField, strValue))
   400 		{
   401 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   402 		}
   403 	}
   404 	else
   405 	{
   406 		if (!msg->SetRepeatedString(strField, index, strValue))
   407 		{
   408 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   409 		}
   410 	}
   411 
   412 	return 1;
   413 }
   414 
   415 static cell_t smn_PbSetColor(IPluginContext *pCtx, const cell_t *params)
   416 {
   417 	GET_MSG_FROM_HANDLE_OR_ERR();
   418 	GET_FIELD_NAME_OR_ERR();
   419 
   420 	cell_t *clrParams;
   421 	if ((err=pCtx->LocalToPhysAddr(params[3], &clrParams)) != SP_ERROR_NONE)
   422 	{
   423 		pCtx->ThrowNativeErrorEx(err, NULL);
   424 		return 0;
   425 	}
   426 
   427 	Color clr(
   428 		clrParams[0],
   429 		clrParams[1],
   430 		clrParams[2],
   431 		clrParams[3]);
   432 
   433 	int index = params[0] >= 4 ? params[4] : -1;
   434 	if (index < 0)
   435 	{
   436 		if (!msg->SetColor(strField, clr))
   437 		{
   438 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   439 		}
   440 	}
   441 	else
   442 	{
   443 		if (!msg->SetRepeatedColor(strField, index, clr))
   444 		{
   445 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   446 		}
   447 	}
   448 
   449 	return 1;
   450 }
   451 
   452 static cell_t smn_PbSetAngle(IPluginContext *pCtx, const cell_t *params)
   453 {
   454 	GET_MSG_FROM_HANDLE_OR_ERR();
   455 	GET_FIELD_NAME_OR_ERR();
   456 
   457 	cell_t *angParams;
   458 	if ((err=pCtx->LocalToPhysAddr(params[3], &angParams)) != SP_ERROR_NONE)
   459 	{
   460 		pCtx->ThrowNativeErrorEx(err, NULL);
   461 		return 0;
   462 	}
   463 
   464 	QAngle ang(
   465 		sp_ctof(angParams[0]),
   466 		sp_ctof(angParams[1]),
   467 		sp_ctof(angParams[2]));
   468 
   469 	int index = params[0] >= 4 ? params[4] : -1;
   470 	if (index < 0)
   471 	{
   472 		if (!msg->SetQAngle(strField, ang))
   473 		{
   474 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   475 		}
   476 	}
   477 	else
   478 	{
   479 		if (!msg->SetRepeatedQAngle(strField, index, ang))
   480 		{
   481 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   482 		}
   483 	}
   484 
   485 	return 1;
   486 }
   487 
   488 static cell_t smn_PbSetVector(IPluginContext *pCtx, const cell_t *params)
   489 {
   490 	GET_MSG_FROM_HANDLE_OR_ERR();
   491 	GET_FIELD_NAME_OR_ERR();
   492 
   493 	cell_t *vecParams;
   494 	if ((err=pCtx->LocalToPhysAddr(params[3], &vecParams)) != SP_ERROR_NONE)
   495 	{
   496 		pCtx->ThrowNativeErrorEx(err, NULL);
   497 		return 0;
   498 	}
   499 
   500 	Vector vec(
   501 		sp_ctof(vecParams[0]),
   502 		sp_ctof(vecParams[1]),
   503 		sp_ctof(vecParams[2]));
   504 
   505 	int index = params[0] >= 4 ? params[4] : -1;
   506 	if (index < 0)
   507 	{
   508 		if (!msg->SetVector(strField, vec))
   509 		{
   510 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   511 		}
   512 	}
   513 	else
   514 	{
   515 		if (!msg->SetRepeatedVector(strField, index, vec))
   516 		{
   517 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   518 		}
   519 	}
   520 
   521 	return 1;
   522 }
   523 
   524 static cell_t smn_PbSetVector2D(IPluginContext *pCtx, const cell_t *params)
   525 {
   526 	GET_MSG_FROM_HANDLE_OR_ERR();
   527 	GET_FIELD_NAME_OR_ERR();
   528 
   529 	cell_t *vecParams;
   530 	if ((err=pCtx->LocalToPhysAddr(params[3], &vecParams)) != SP_ERROR_NONE)
   531 	{
   532 		pCtx->ThrowNativeErrorEx(err, NULL);
   533 		return 0;
   534 	}
   535 
   536 	Vector2D vec(
   537 		sp_ctof(vecParams[0]),
   538 		sp_ctof(vecParams[1]));
   539 
   540 	int index = params[0] >= 4 ? params[4] : -1;
   541 	if (index < 0)
   542 	{
   543 		if (!msg->SetVector2D(strField, vec))
   544 		{
   545 			return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   546 		}
   547 	}
   548 	else
   549 	{
   550 		if (!msg->SetRepeatedVector2D(strField, index, vec))
   551 		{
   552 			return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, index, msg->GetProtobufMessage()->GetTypeName().c_str());
   553 		}
   554 	}
   555 
   556 	return 1;
   557 }
   558 
   559 static cell_t smn_PbAddInt(IPluginContext *pCtx, const cell_t *params)
   560 {
   561 	GET_MSG_FROM_HANDLE_OR_ERR();
   562 	GET_FIELD_NAME_OR_ERR();
   563 
   564 	if (!msg->AddInt32OrUnsignedOrEnum(strField, params[3]))
   565 	{
   566 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   567 	}
   568 
   569 	return 1;
   570 }
   571 
   572 static cell_t smn_PbAddFloat(IPluginContext *pCtx, const cell_t *params)
   573 {
   574 	GET_MSG_FROM_HANDLE_OR_ERR();
   575 	GET_FIELD_NAME_OR_ERR();
   576 
   577 	if (!msg->AddFloatOrDouble(strField, sp_ctof(params[3])))
   578 	{
   579 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   580 	}
   581 
   582 	return 1;
   583 }
   584 
   585 static cell_t smn_PbAddBool(IPluginContext *pCtx, const cell_t *params)
   586 {
   587 	GET_MSG_FROM_HANDLE_OR_ERR();
   588 	GET_FIELD_NAME_OR_ERR();
   589 
   590 	bool value = (params[3] == 0 ? false : true);
   591 	if (!msg->AddBool(strField, value))
   592 	{
   593 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   594 	}
   595 
   596 	return 1;
   597 }
   598 
   599 static cell_t smn_PbAddString(IPluginContext *pCtx, const cell_t *params)
   600 {
   601 	GET_MSG_FROM_HANDLE_OR_ERR();
   602 	GET_FIELD_NAME_OR_ERR();
   603 
   604 	char *strValue;
   605 	if ((err=pCtx->LocalToString(params[3], &strValue)) != SP_ERROR_NONE)
   606 	{
   607 		pCtx->ThrowNativeErrorEx(err, NULL);
   608 		return 0;
   609 	}
   610 
   611 	if (!msg->AddString(strField, strValue))
   612 	{
   613 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   614 	}
   615 
   616 	return 1;
   617 }
   618 
   619 static cell_t smn_PbAddColor(IPluginContext *pCtx, const cell_t *params)
   620 {
   621 	GET_MSG_FROM_HANDLE_OR_ERR();
   622 	GET_FIELD_NAME_OR_ERR();
   623 
   624 	cell_t *clrParams;
   625 	if ((err=pCtx->LocalToPhysAddr(params[3], &clrParams)) != SP_ERROR_NONE)
   626 	{
   627 		pCtx->ThrowNativeErrorEx(err, NULL);
   628 		return 0;
   629 	}
   630 
   631 	Color clr(
   632 		clrParams[0],
   633 		clrParams[1],
   634 		clrParams[2],
   635 		clrParams[3]);
   636 
   637 	if (!msg->AddColor(strField, clr))
   638 	{
   639 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   640 	}
   641 
   642 	return 1;
   643 }
   644 
   645 static cell_t smn_PbAddAngle(IPluginContext *pCtx, const cell_t *params)
   646 {
   647 	GET_MSG_FROM_HANDLE_OR_ERR();
   648 	GET_FIELD_NAME_OR_ERR();
   649 
   650 	cell_t *angParams;
   651 	if ((err=pCtx->LocalToPhysAddr(params[3], &angParams)) != SP_ERROR_NONE)
   652 	{
   653 		pCtx->ThrowNativeErrorEx(err, NULL);
   654 		return 0;
   655 	}
   656 
   657 	QAngle ang(
   658 		sp_ctof(angParams[0]),
   659 		sp_ctof(angParams[1]),
   660 		sp_ctof(angParams[2]));
   661 
   662 	if (!msg->AddQAngle(strField, ang))
   663 	{
   664 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   665 	}
   666 
   667 	return 1;
   668 }
   669 
   670 static cell_t smn_PbAddVector(IPluginContext *pCtx, const cell_t *params)
   671 {
   672 	GET_MSG_FROM_HANDLE_OR_ERR();
   673 	GET_FIELD_NAME_OR_ERR();
   674 
   675 	cell_t *vecParams;
   676 	if ((err=pCtx->LocalToPhysAddr(params[3], &vecParams)) != SP_ERROR_NONE)
   677 	{
   678 		pCtx->ThrowNativeErrorEx(err, NULL);
   679 		return 0;
   680 	}
   681 
   682 	Vector vec(
   683 		sp_ctof(vecParams[0]),
   684 		sp_ctof(vecParams[1]),
   685 		sp_ctof(vecParams[2]));
   686 
   687 	if (!msg->AddVector(strField, vec))
   688 	{
   689 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   690 	}
   691 
   692 	return 1;
   693 }
   694 
   695 static cell_t smn_PbAddVector2D(IPluginContext *pCtx, const cell_t *params)
   696 {
   697 	GET_MSG_FROM_HANDLE_OR_ERR();
   698 	GET_FIELD_NAME_OR_ERR();
   699 
   700 	cell_t *vecParams;
   701 	if ((err=pCtx->LocalToPhysAddr(params[3], &vecParams)) != SP_ERROR_NONE)
   702 	{
   703 		pCtx->ThrowNativeErrorEx(err, NULL);
   704 		return 0;
   705 	}
   706 
   707 	Vector2D vec(
   708 		sp_ctof(vecParams[0]),
   709 		sp_ctof(vecParams[1]));
   710 
   711 	if (!msg->AddVector2D(strField, vec))
   712 	{
   713 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   714 	}
   715 
   716 	return 1;
   717 }
   718 
   719 static cell_t smn_PbReadMessage(IPluginContext *pCtx, const cell_t *params)
   720 {
   721 	GET_MSG_FROM_HANDLE_OR_ERR();
   722 	GET_FIELD_NAME_OR_ERR();
   723 
   724 	protobuf::Message *innerMsg;
   725 	if (!msg->GetMessage(strField, &innerMsg))
   726 	{
   727 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   728 	}
   729 
   730 	Handle_t outHndl = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(innerMsg), NULL, g_pCoreIdent, NULL);
   731 	msg->AddChildHandle(outHndl);
   732 
   733 	return outHndl;
   734 }
   735 
   736 static cell_t smn_PbReadRepeatedMessage(IPluginContext *pCtx, const cell_t *params)
   737 {
   738 	GET_MSG_FROM_HANDLE_OR_ERR();
   739 	GET_FIELD_NAME_OR_ERR();
   740 
   741 	const protobuf::Message *innerMsg;
   742 	if (!msg->GetRepeatedMessage(strField, params[3], &innerMsg))
   743 	{
   744 		return pCtx->ThrowNativeError("Invalid field \"%s\"[%d] for message \"%s\"", strField, params[3], msg->GetProtobufMessage()->GetTypeName().c_str());
   745 	}
   746 
   747 	Handle_t outHndl = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(const_cast<protobuf::Message *>(innerMsg)), NULL, g_pCoreIdent, NULL);
   748 	msg->AddChildHandle(outHndl);
   749 
   750 	return outHndl;
   751 }
   752 
   753 static cell_t smn_PbAddMessage(IPluginContext *pCtx, const cell_t *params)
   754 {
   755 	GET_MSG_FROM_HANDLE_OR_ERR();
   756 	GET_FIELD_NAME_OR_ERR();
   757 
   758 	protobuf::Message *innerMsg;
   759 	if (!msg->AddMessage(strField, &innerMsg))
   760 	{
   761 		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
   762 	}
   763 
   764 	Handle_t outHndl = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(innerMsg), NULL, g_pCoreIdent, NULL);
   765 	msg->AddChildHandle(outHndl);
   766 
   767 	return outHndl;
   768 }
   769 
   770 REGISTER_NATIVES(protobufnatives)
   771 {
   772 	{"PbReadInt",					smn_PbReadInt},
   773 	{"PbReadFloat",					smn_PbReadFloat},
   774 	{"PbReadBool",					smn_PbReadBool},
   775 	{"PbReadString",				smn_PbReadString},
   776 	{"PbReadColor",					smn_PbReadColor},
   777 	{"PbReadAngle",					smn_PbReadAngle},
   778 	{"PbReadVector",				smn_PbReadVector},
   779 	{"PbReadVector2D",				smn_PbReadVector2D},
   780 	{"PbGetRepeatedFieldCount",		smn_PbGetRepeatedFieldCount},
   781 	{"PbSetInt",					smn_PbSetInt},
   782 	{"PbSetFloat",					smn_PbSetFloat},
   783 	{"PbSetBool",					smn_PbSetBool},
   784 	{"PbSetString",					smn_PbSetString},
   785 	{"PbSetColor",					smn_PbSetColor},
   786 	{"PbSetAngle",					smn_PbSetAngle},
   787 	{"PbSetVector",					smn_PbSetVector},
   788 	{"PbSetVector2D",				smn_PbSetVector2D},
   789 	{"PbAddInt",					smn_PbAddInt},
   790 	{"PbAddFloat",					smn_PbAddFloat},
   791 	{"PbAddBool",					smn_PbAddBool},
   792 	{"PbAddString",					smn_PbAddString},
   793 	{"PbAddColor",					smn_PbAddColor},
   794 	{"PbAddAngle",					smn_PbAddAngle},
   795 	{"PbAddVector",					smn_PbAddVector},
   796 	{"PbAddVector2D",				smn_PbAddVector2D},
   797 	{"PbReadMessage",				smn_PbReadMessage},
   798 	{"PbReadRepeatedMessage",		smn_PbReadRepeatedMessage},
   799 	{"PbAddMessage",				smn_PbAddMessage},
   800 	{NULL,							NULL}
   801 };
   802 
   803 /*
   804                                        @@@@@@@@@@@@@ 
   805                                      @@@@@@@@@@@@@@@@@@
   806                                      @@@@@@@@@@@@@@@@@@@@@      
   807                                     @@@@@@@@@@@@@@@@@@@@@@@@               
   808                                    @@@@@@@@    @@@@@@@@@@@@@
   809                                    @@@@@@@@    @@@@@@@@@@@@@          
   810                                       @@@@@@  @@@@@@@@@@@@@@@             
   811                                  0000   @@@@@@@@@@@@@@@@@@@@@                
   812                               000000000   @@@@@@@@@@@@@@@@@@@           
   813                        0000000000000000000   @@@@@@@@@@@@@@@          
   814                        00000000000000000000  @@@@@@@@@@@@@@@            
   815                             000000000       @@@@@@@@@@@@@@@           
   816                                          @@@@@@@@@@@@@@@@@               
   817                                         @@@@@@@@@@@@@@@@@@      
   818                                        @@@@@@@@@@@@@@@@@@       
   819                                        @@@@@@@@@@@@@@@@@@                                                                @@@@
   820                                       @@@@@@@@@@@@@@@@@@@                                                             @@@@@@@  
   821                                      @@@   @@@@    @@@                                                             @@@@@@@@@@     
   822                                     @@@@@@@@@@@@@@@@@@@@@@@@@@                                                 @@@@@@@@@@  @@@@@    
   823                                    @@@@@@@@@@@@@@@@@@@@@@@@@@ @@@   @@@@@@@@                                @@  @@@@@@@@@@@@@@@ 
   824                                   @@@@@@@@@@@@@@@@@@@@@@@@@@ @@   @@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@@@@@@@@@  @@  @@@@@@@@@        
   825                                   @@@@@@@@@@@@@@@@@@@@@@@ @@@@   @@@@@@@@@@@@@@@@  @@@@@@@@@@@@@@@@@@@@@@@@@  @@  @@@@@@@@@@@@@
   826                                   @@@@@@@@@@@@@@@@@@@@@ @@@@    @@@@@@@@@@@@@@@@@    @@@@@@@@@@@@@@@@@@@@@@  @@@  @@@@@@@@@@@@     
   827                                  @@@@@@@@@@@@@@@@@@@@ @@@@@@   @@@@@@@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@@@@@   @@@@  @@@@@@@@@@@     
   828                                  @@@@@@@@@@@@@@@@@  @@@@@@    @@@@@@@@@@@@@@@@@@@  @@@@@@@@@@@@@@@@@@@      @@@@@ @@@@@@      
   829                                  @@@@@@@@@       @@@@@@@@@   @@@@@@@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@     @@  @@@@@ @@@@@@@@   
   830                                            @@@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@@@@   @@@@@@@@@@@@@@   @@@@@@  @@@@  @@@@@@  
   831                                   @@@@@@@@@@@@@@@@@@@@@@    @@@@@@@@@@@@@@@@@@@@@  @@@@@@         @@@@@@  @@@@@@
   832                                  @@@@@@@@@@@@@@@@@@@@@@@    @@@@@@@@@@@@@@@@@@@@@              @@@@@@@@   @@@@
   833                                   @@@@@@@@@@@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@@@@       @@@@@@@@@@@@@@  @  @@@@@  
   834                                    @@@@@@@@@@@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@    @@@@@  @@@@    ////
   835                      ////////////   @@@@@@@@@@@ @@@@@@       @@@@@@@@@@@@@@@@@@@@  @@      @@@@@@@@@  @@@@    //// //////////// 
   836               ///////////////// ///    @@@@@          @@@@    @@@@@@@@@@@@@@@@@@@   @@@@@@@@@@@@@     @@@@   ///// ///////  /////////    
   837         /////////////////////  /////         @@@@@@@@@@@@@@   @@@@@@@@@@@@@@@@@@@   @@@@@@@      @  @@@@    ////// ///////  ///////////////
   838     ///////////////////////// ///////    @@@@@@@@@@@@@@@@@@@    @@@@@@@@@@@@@@@           @@@@@   @@@@    //////  ////////  //////////////////// 
   839  ////////////////////////////  //////////   @@@@@@@@@@@@@@@@@@    @@@@@@@@@@@@   @@@@@@@@@     @@@      //////  ////////// ////////////////////////
   840 //////////////////// //////////  ///////////   @@@@@@@@@@@@@@@@@     @@@@@@@@   @@@      @@@@@@      ////// ////////////  //////////////////////////
   841 //////////////////// ////////////   ///////////   @@@@@@@@@@@@@@@@               @@@@            /////////////////////  /////////////////////////////
   842 /////////////////////  //////////////       //////              @@@@@@@@@                ///////////////////////////////////////////////////////////
   843    ////////////////////    /////////////////////////////////////          ///////////////////////////////////////////////////////////////////////
   844        /////////////////////     ////////////////////////////////////////////////////////////////////////////////////////////////////////////  
   845             ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   846                    //////////////////////////////////////////////////////////////////////////////////////////////////////////////   
   847                              ///////////////////////////////////////////////////////////////////////////////////////// 
   848                                              //////////////////////////////////////////////////////////
   849 */