From 5c6bc80f20c9481c6b8131e84c4bbfc4ef066386 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Tue, 2 Jan 2007 02:40:32 +0000 Subject: [PATCH] Registered player and float natives. Added error strings to these natives. --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40265 --- core/smn_float.cpp | 73 ++++++++++++++++++++++----------- core/smn_player.cpp | 82 +++++++++++++++++++++++++++----------- plugins/include/string.inc | 2 + 3 files changed, 110 insertions(+), 47 deletions(-) diff --git a/core/smn_float.cpp b/core/smn_float.cpp index 875f0b4d..baadc443 100644 --- a/core/smn_float.cpp +++ b/core/smn_float.cpp @@ -1,12 +1,11 @@ #include #include +#include "sm_autonatives.h" #include "sp_vm_api.h" #include "sp_typeutil.h" using namespace SourcePawn; -//:TODO: these need to be registered.... - /**************************************** * * * FLOATING POINT NATIVE IMPLEMENTATIONS * @@ -21,7 +20,7 @@ static cell_t sm_float(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatAbs(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = (val >= 0) ? val : -val; @@ -29,35 +28,35 @@ static cell_t sm_floatabs(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatadd(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatAdd(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]) + sp_ctof(params[2]); return sp_ftoc(val); } -static cell_t sm_floatsub(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatSub(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]) - sp_ctof(params[2]); return sp_ftoc(val); } -static cell_t sm_floatmul(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatMul(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]) * sp_ctof(params[2]); return sp_ftoc(val); } -static cell_t sm_floatdiv(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatDiv(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]) / sp_ctof(params[2]); return sp_ftoc(val); } -static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatCompare(IPluginContext *pCtx, const cell_t *params) { float val1 = sp_ctof(params[1]); float val2 = sp_ctof(params[2]); @@ -72,14 +71,14 @@ static cell_t sm_floatcmp(IPluginContext *pCtx, const cell_t *params) return 0; } -static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_Logarithm(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); float base = sp_ctof(params[2]); if ((val <= 0) || (base <= 0)) { - //:TODO: error out! logs cant take in negative numbers and log 0=-inf + return pCtx->ThrowNativeError("Cannot evaluate the logarithm of zero or a negative number (val:%f base:%f).", val, base); } if (base == 10.0) { @@ -91,14 +90,14 @@ static cell_t sm_floatlog(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatexp(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_Exponential(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); return sp_ftoc(exp(val)); } -static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_Pow(IPluginContext *pCtx, const cell_t *params) { float base = sp_ctof(params[1]); float exponent = sp_ctof(params[2]); @@ -106,19 +105,19 @@ static cell_t sm_floatpower(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(pow(base, exponent)); } -static cell_t sm_floatsqroot(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_SquareRoot(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); if (val < 0.0) { - //:TODO: error out! we dont support complex numbers + return pCtx->ThrowNativeError("Cannot evaluate the square root of a negative number (val:%f).", val); } return sp_ftoc(sqrt(val)); } -static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatRound(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); @@ -154,7 +153,7 @@ static cell_t sm_floatround(IPluginContext *pCtx, const cell_t *params) return static_cast(val); } -static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatStr(IPluginContext *pCtx, const cell_t *params) { char *str; @@ -167,7 +166,7 @@ static cell_t sm_floatstr(IPluginContext *pCtx, const cell_t *params) return sp_ftoc((float)atof(str)); } -static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_FloatFraction(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = val - floor(val); @@ -175,7 +174,7 @@ static cell_t sm_floatfract(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_Sine(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = sin(val); @@ -183,7 +182,7 @@ static cell_t sm_floatsin(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_Cosine(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = cos(val); @@ -191,7 +190,7 @@ static cell_t sm_floatcos(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_Tangent(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = tan(val); @@ -199,7 +198,7 @@ static cell_t sm_floattan(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_ArcSine(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = asin(val); @@ -207,7 +206,7 @@ static cell_t sm_floatasin(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_ArcCosine(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = acos(val); @@ -215,7 +214,7 @@ static cell_t sm_floatacos(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_ArcTangent(IPluginContext *pCtx, const cell_t *params) { float val = sp_ctof(params[1]); val = atan(val); @@ -223,7 +222,7 @@ static cell_t sm_floatatan(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val); } -static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_ArcTangent2(IPluginContext *pCtx, const cell_t *params) { float val1 = sp_ctof(params[1]); float val2 = sp_ctof(params[2]); @@ -231,3 +230,29 @@ static cell_t sm_floatatan2(IPluginContext *pCtx, const cell_t *params) return sp_ftoc(val1); } + +REGISTER_NATIVES(floatnatives) +{ + {"float", sm_float}, + {"FloatStr", sm_FloatStr}, + {"FloatMul", sm_FloatMul}, + {"FloatDiv", sm_FloatDiv}, + {"FloatAdd", sm_FloatAdd}, + {"FloatSub", sm_FloatSub}, + {"FloatFraction", sm_FloatFraction}, + {"FloatRound", sm_FloatRound}, + {"FloatCompare", sm_FloatCompare}, + {"SquareRoot", sm_SquareRoot}, + {"Pow", sm_Pow}, + {"Exponential", sm_Exponential}, + {"Logarithm", sm_Logarithm}, + {"Sine", sm_Sine}, + {"Cosine", sm_Cosine}, + {"Tangent", sm_Tangent}, + {"FloatAbs", sm_FloatAbs}, + {"ArcTangent", sm_ArcTangent}, + {"ArcCosine", sm_ArcCosine}, + {"ArcSine", sm_ArcSine}, + {"ArcTangent2", sm_ArcTangent2}, + {NULL, NULL} +}; \ No newline at end of file diff --git a/core/smn_player.cpp b/core/smn_player.cpp index d8c64dcb..441d0ec4 100644 --- a/core/smn_player.cpp +++ b/core/smn_player.cpp @@ -1,8 +1,6 @@ #include "CPlayerManager.h" -//:TODO: register these and use the same capitalization conventions as the natives - -static cell_t sm_getclientcount(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params) { if (params[1]) { @@ -23,87 +21,125 @@ static cell_t sm_getclientcount(IPluginContext *pCtx, const cell_t *params) return (g_PlayerManager.GetPlayerCount() + count); } -static cell_t sm_getmaxclients(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params) { return g_PlayerManager.GetMaxClients(); } -static cell_t sm_getclientname(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } - pCtx->StringToLocalUTF8(params[2], static_cast(params[3]), g_PlayerManager.GetPlayerByIndex(index)->PlayerName(), NULL); + CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); + if (!pPlayer->IsPlayerConnected()) + { + return pCtx->ThrowNativeError("Client %d is not connected.", index); + } + + pCtx->StringToLocalUTF8(params[2], static_cast(params[3]), pPlayer->PlayerName(), NULL); return 1; } -static cell_t sm_getclientip(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } - pCtx->StringToLocal(params[2], static_cast(params[3]), g_PlayerManager.GetPlayerByIndex(index)->PlayerIP()); + CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); + if (!pPlayer->IsPlayerConnected()) + { + return pCtx->ThrowNativeError("Client %d is not connected.", index); + } + + pCtx->StringToLocal(params[2], static_cast(params[3]), pPlayer->PlayerIP()); return 1; } -static cell_t sm_getclientauthstr(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } - pCtx->StringToLocal(params[2], static_cast(params[3]), g_PlayerManager.GetPlayerByIndex(index)->PlayerAuthString()); + CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); + if (!pPlayer->IsPlayerConnected()) + { + return pCtx->ThrowNativeError("Client %d is not connected.", index); + } + + pCtx->StringToLocal(params[2], static_cast(params[3]), pPlayer->PlayerAuthString()); return 1; } -static cell_t sm_isplayerconnected(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_IsPlayerConnected(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerConnected()) ? 1 : 0; } -static cell_t sm_isplayeringame(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_IsPlayerIngame(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerInGame()) ? 1 : 0; } -static cell_t sm_isplayerauthorized(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_IsPlayerAuthorized(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerAuthorized()) ? 1 : 0; } -static cell_t sm_isplayerfakeclient(IPluginContext *pCtx, const cell_t *params) +static cell_t sm_IsPlayerFakeClient(IPluginContext *pCtx, const cell_t *params) { int index = params[1]; if ((index < 1) || (index > g_PlayerManager.GetMaxClients())) { - //:TODO: runtime error + return pCtx->ThrowNativeError("Invalid client index %d.", index); } - return (g_PlayerManager.GetPlayerByIndex(index)->IsPlayerFakeClient()) ? 1 : 0; -} \ No newline at end of file + CPlayer *pPlayer = g_PlayerManager.GetPlayerByIndex(index); + if (!pPlayer->IsPlayerConnected()) + { + return pCtx->ThrowNativeError("Client %d is not connected.", index); + } + + return (pPlayer->IsPlayerFakeClient()) ? 1 : 0; +} + +REGISTER_NATIVES(playernatives) +{ + {"GetMaxClients", sm_GetMaxClients}, + {"GetClientCount", sm_GetClientCount}, + {"GetClientName", sm_GetClientName}, + {"GetClientIP", sm_GetClientIP}, + {"GetClientAuthString", sm_GetClientAuthStr}, + {"IsPlayerConnected", sm_IsPlayerConnected}, + {"IsPlayerInGame", sm_IsPlayerIngame}, + {"IsPlayerAuthorized", sm_IsPlayerAuthorized}, + {"IsPlayerFakeClient", sm_IsPlayerFakeClient}, + {NULL, NULL} +}; \ No newline at end of file diff --git a/plugins/include/string.inc b/plugins/include/string.inc index de4c72b5..639dd8b7 100644 --- a/plugins/include/string.inc +++ b/plugins/include/string.inc @@ -81,6 +81,7 @@ native StrCopy(String:dest[], destLen, const String:source[]); * @param maxlength Maximum length of output string buffer. * @param format Formatting rules. * @param ... Variable number of format parameters. + * @return Number of cells written. */ native Format(String:buffer[], maxlength, const String:format[], {Handle,Float,String,_}:...); @@ -93,6 +94,7 @@ native Format(String:buffer[], maxlength, const String:format[], {Handle,Float,S * @param maxlength Maximum length of output string buffer. * @param format Formatting rules. * @param ... Variable number of format parameters. + * @return Number of cells written. */ native FormatEx(String:buffer[], maxlength, const String:format[], {Handle,Float,String,_}:...);