core/smn_bitbuffer.cpp
changeset 598 991d7e247a2c
parent 511 c29f8796bcf4
child 600 f6d811158ad3
     1.1 --- a/core/smn_bitbuffer.cpp	Sun Mar 11 19:26:47 2007 +0000
     1.2 +++ b/core/smn_bitbuffer.cpp	Sun Mar 11 21:09:05 2007 +0000
     1.3 @@ -328,7 +328,310 @@
     1.4  	return 1;
     1.5  }
     1.6  
     1.7 -REGISTER_NATIVES(wrbitbufnatives)
     1.8 +static cell_t smn_BfReadBool(IPluginContext *pCtx, const cell_t *params)
     1.9 +{
    1.10 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.11 +	HandleError herr;
    1.12 +	HandleSecurity sec;
    1.13 +	bf_read *pBitBuf;
    1.14 +
    1.15 +	sec.pOwner = NULL;
    1.16 +	sec.pIdentity = g_pCoreIdent;
    1.17 +
    1.18 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
    1.19 +		!= HandleError_None)
    1.20 +	{
    1.21 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.22 +	}
    1.23 +
    1.24 +	return pBitBuf->ReadOneBit() ? 1 : 0;
    1.25 +}
    1.26 +
    1.27 +static cell_t smn_BfReadByte(IPluginContext *pCtx, const cell_t *params)
    1.28 +{
    1.29 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.30 +	HandleError herr;
    1.31 +	HandleSecurity sec;
    1.32 +	bf_read *pBitBuf;
    1.33 +
    1.34 +	sec.pOwner = NULL;
    1.35 +	sec.pIdentity = g_pCoreIdent;
    1.36 +
    1.37 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
    1.38 +		!= HandleError_None)
    1.39 +	{
    1.40 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.41 +	}
    1.42 +
    1.43 +	return pBitBuf->ReadByte();
    1.44 +}
    1.45 +
    1.46 +static cell_t smn_BfReadChar(IPluginContext *pCtx, const cell_t *params)
    1.47 +{
    1.48 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.49 +	HandleError herr;
    1.50 +	HandleSecurity sec;
    1.51 +	bf_read *pBitBuf;
    1.52 +
    1.53 +	sec.pOwner = NULL;
    1.54 +	sec.pIdentity = g_pCoreIdent;
    1.55 +
    1.56 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
    1.57 +		!= HandleError_None)
    1.58 +	{
    1.59 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.60 +	}
    1.61 +
    1.62 +	return pBitBuf->ReadChar();
    1.63 +}
    1.64 +
    1.65 +static cell_t smn_BfReadShort(IPluginContext *pCtx, const cell_t *params)
    1.66 +{
    1.67 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.68 +	HandleError herr;
    1.69 +	HandleSecurity sec;
    1.70 +	bf_read *pBitBuf;
    1.71 +
    1.72 +	sec.pOwner = NULL;
    1.73 +	sec.pIdentity = g_pCoreIdent;
    1.74 +
    1.75 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
    1.76 +		!= HandleError_None)
    1.77 +	{
    1.78 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.79 +	}
    1.80 +
    1.81 +	return pBitBuf->ReadShort();
    1.82 +}
    1.83 +
    1.84 +static cell_t smn_BfReadWord(IPluginContext *pCtx, const cell_t *params)
    1.85 +{
    1.86 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
    1.87 +	HandleError herr;
    1.88 +	HandleSecurity sec;
    1.89 +	bf_read *pBitBuf;
    1.90 +
    1.91 +	sec.pOwner = NULL;
    1.92 +	sec.pIdentity = g_pCoreIdent;
    1.93 +
    1.94 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
    1.95 +		!= HandleError_None)
    1.96 +	{
    1.97 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
    1.98 +	}
    1.99 +
   1.100 +	return pBitBuf->ReadWord();
   1.101 +}
   1.102 +
   1.103 +static cell_t smn_BfReadNum(IPluginContext *pCtx, const cell_t *params)
   1.104 +{
   1.105 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.106 +	HandleError herr;
   1.107 +	HandleSecurity sec;
   1.108 +	bf_read *pBitBuf;
   1.109 +
   1.110 +	sec.pOwner = NULL;
   1.111 +	sec.pIdentity = g_pCoreIdent;
   1.112 +
   1.113 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.114 +		!= HandleError_None)
   1.115 +	{
   1.116 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.117 +	}
   1.118 +
   1.119 +	return static_cast<cell_t>(pBitBuf->ReadLong());
   1.120 +}
   1.121 +
   1.122 +static cell_t smn_BfReadFloat(IPluginContext *pCtx, const cell_t *params)
   1.123 +{
   1.124 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.125 +	HandleError herr;
   1.126 +	HandleSecurity sec;
   1.127 +	bf_read *pBitBuf;
   1.128 +
   1.129 +	sec.pOwner = NULL;
   1.130 +	sec.pIdentity = g_pCoreIdent;
   1.131 +
   1.132 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.133 +		!= HandleError_None)
   1.134 +	{
   1.135 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.136 +	}
   1.137 +
   1.138 +	return sp_ftoc(pBitBuf->ReadFloat());
   1.139 +}
   1.140 +
   1.141 +static cell_t smn_BfReadString(IPluginContext *pCtx, const cell_t *params)
   1.142 +{
   1.143 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.144 +	HandleError herr;
   1.145 +	HandleSecurity sec;
   1.146 +	bf_read *pBitBuf;
   1.147 +	char *buf;
   1.148 +
   1.149 +	sec.pOwner = NULL;
   1.150 +	sec.pIdentity = g_pCoreIdent;
   1.151 +
   1.152 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.153 +		!= HandleError_None)
   1.154 +	{
   1.155 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.156 +	}
   1.157 +
   1.158 +	pCtx->LocalToPhysAddr(params[2], (cell_t **)&buf);
   1.159 +	if (!pBitBuf->ReadString(buf, params[3], params[4] ? true : false))
   1.160 +	{
   1.161 +		return pCtx->ThrowNativeError("Destination string buffer is too short, try increasing its size");
   1.162 +	}
   1.163 +
   1.164 +	return 1;
   1.165 +}
   1.166 +
   1.167 +static cell_t smn_BfReadEntity(IPluginContext *pCtx, const cell_t *params)
   1.168 +{
   1.169 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.170 +	HandleError herr;
   1.171 +	HandleSecurity sec;
   1.172 +	bf_read *pBitBuf;
   1.173 +
   1.174 +	sec.pOwner = NULL;
   1.175 +	sec.pIdentity = g_pCoreIdent;
   1.176 +
   1.177 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.178 +		!= HandleError_None)
   1.179 +	{
   1.180 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.181 +	}
   1.182 +
   1.183 +	return pBitBuf->ReadShort();
   1.184 +}
   1.185 +
   1.186 +static cell_t smn_BfReadAngle(IPluginContext *pCtx, const cell_t *params)
   1.187 +{
   1.188 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.189 +	HandleError herr;
   1.190 +	HandleSecurity sec;
   1.191 +	bf_read *pBitBuf;
   1.192 +
   1.193 +	sec.pOwner = NULL;
   1.194 +	sec.pIdentity = g_pCoreIdent;
   1.195 +
   1.196 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.197 +		!= HandleError_None)
   1.198 +	{
   1.199 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.200 +	}
   1.201 +
   1.202 +	return sp_ftoc(pBitBuf->ReadBitAngle(params[2]));
   1.203 +}
   1.204 +
   1.205 +static cell_t smn_BfReadCoord(IPluginContext *pCtx, const cell_t *params)
   1.206 +{
   1.207 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.208 +	HandleError herr;
   1.209 +	HandleSecurity sec;
   1.210 +	bf_read *pBitBuf;
   1.211 +
   1.212 +	sec.pOwner = NULL;
   1.213 +	sec.pIdentity = g_pCoreIdent;
   1.214 +
   1.215 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.216 +		!= HandleError_None)
   1.217 +	{
   1.218 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.219 +	}
   1.220 +
   1.221 +	return sp_ftoc(pBitBuf->ReadBitCoord());
   1.222 +}
   1.223 +
   1.224 +static cell_t smn_BfReadVecCoord(IPluginContext *pCtx, const cell_t *params)
   1.225 +{
   1.226 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.227 +	HandleError herr;
   1.228 +	HandleSecurity sec;
   1.229 +	bf_read *pBitBuf;
   1.230 +
   1.231 +	sec.pOwner = NULL;
   1.232 +	sec.pIdentity = g_pCoreIdent;
   1.233 +
   1.234 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.235 +		!= HandleError_None)
   1.236 +	{
   1.237 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.238 +	}
   1.239 +
   1.240 +	cell_t *pVec;
   1.241 +	pCtx->LocalToPhysAddr(params[2], &pVec);
   1.242 +
   1.243 +	Vector vec;
   1.244 +	pBitBuf->ReadBitVec3Coord(vec);
   1.245 +
   1.246 +	pVec[0] = sp_ftoc(vec.x);
   1.247 +	pVec[1] = sp_ftoc(vec.y);
   1.248 +	pVec[2] = sp_ftoc(vec.z);
   1.249 +
   1.250 +	return 1;
   1.251 +}
   1.252 +
   1.253 +static cell_t smn_BfReadVecNormal(IPluginContext *pCtx, const cell_t *params)
   1.254 +{
   1.255 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.256 +	HandleError herr;
   1.257 +	HandleSecurity sec;
   1.258 +	bf_read *pBitBuf;
   1.259 +
   1.260 +	sec.pOwner = NULL;
   1.261 +	sec.pIdentity = g_pCoreIdent;
   1.262 +
   1.263 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.264 +		!= HandleError_None)
   1.265 +	{
   1.266 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.267 +	}
   1.268 +
   1.269 +	cell_t *pVec;
   1.270 +	pCtx->LocalToPhysAddr(params[2], &pVec);
   1.271 +
   1.272 +	Vector vec;
   1.273 +	pBitBuf->ReadBitVec3Normal(vec);
   1.274 +
   1.275 +	pVec[0] = sp_ftoc(vec.x);
   1.276 +	pVec[1] = sp_ftoc(vec.y);
   1.277 +	pVec[2] = sp_ftoc(vec.z);
   1.278 +
   1.279 +	return 1;
   1.280 +}
   1.281 +
   1.282 +static cell_t smn_BfReadAngles(IPluginContext *pCtx, const cell_t *params)
   1.283 +{
   1.284 +	Handle_t hndl = static_cast<Handle_t>(params[1]);
   1.285 +	HandleError herr;
   1.286 +	HandleSecurity sec;
   1.287 +	bf_read *pBitBuf;
   1.288 +
   1.289 +	sec.pOwner = NULL;
   1.290 +	sec.pIdentity = g_pCoreIdent;
   1.291 +
   1.292 +	if ((herr=g_HandleSys.ReadHandle(hndl, g_RdBitBufType, &sec, (void **)&pBitBuf))
   1.293 +		!= HandleError_None)
   1.294 +	{
   1.295 +		return pCtx->ThrowNativeError("Invalid bit buffer handle %x (error %d)", hndl, herr);
   1.296 +	}
   1.297 +
   1.298 +	cell_t *pAng;
   1.299 +	pCtx->LocalToPhysAddr(params[2], &pAng);
   1.300 +
   1.301 +	QAngle ang;
   1.302 +	pBitBuf->ReadBitAngles(ang);
   1.303 +
   1.304 +	pAng[0] = sp_ftoc(ang.x);
   1.305 +	pAng[1] = sp_ftoc(ang.y);
   1.306 +	pAng[2] = sp_ftoc(ang.z);
   1.307 +
   1.308 +	return 1;
   1.309 +}
   1.310 +
   1.311 +REGISTER_NATIVES(bitbufnatives)
   1.312  {
   1.313  	{"BfWriteBool",				smn_BfWriteBool},
   1.314  	{"BfWriteByte",				smn_BfWriteByte},
   1.315 @@ -344,5 +647,19 @@
   1.316  	{"BfWriteVecCoord",			smn_BfWriteVecCoord},
   1.317  	{"BfWriteVecNormal",		smn_BfWriteVecNormal},
   1.318  	{"BfWriteAngles",			smn_BfWriteAngles},
   1.319 +	{"BfReadBool",				smn_BfReadBool},
   1.320 +	{"BfReadByte",				smn_BfReadByte},
   1.321 +	{"BfReadChar",				smn_BfReadChar},
   1.322 +	{"BfReadShort",				smn_BfReadShort},
   1.323 +	{"BfReadWord",				smn_BfReadWord},
   1.324 +	{"BfReadNum",				smn_BfReadNum},
   1.325 +	{"BfReadFloat",				smn_BfReadFloat},
   1.326 +	{"BfReadString",			smn_BfReadString},
   1.327 +	{"BfReadEntity",			smn_BfReadEntity},
   1.328 +	{"BfReadAngle",				smn_BfReadAngle},
   1.329 +	{"BfReadCoord",				smn_BfReadCoord},
   1.330 +	{"BfReadVecCoord",			smn_BfReadVecCoord},
   1.331 +	{"BfReadVecNormal",			smn_BfReadVecNormal},
   1.332 +	{"BfReadAngles",			smn_BfReadAngles},
   1.333  	{NULL,						NULL}
   1.334  };