From d860c43f79c3e1eabe59275e0a03512bc1b0c207 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Wed, 19 Jul 2023 12:25:43 +0200 Subject: [PATCH] Deduplicate code Small deduplication. --- r5dev/game/client/vscript_client.cpp | 51 +++++++++++++--------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/r5dev/game/client/vscript_client.cpp b/r5dev/game/client/vscript_client.cpp index 8c76136b..507b4523 100644 --- a/r5dev/game/client/vscript_client.cpp +++ b/r5dev/game/client/vscript_client.cpp @@ -20,6 +20,22 @@ #include "vscript_client.h" +//----------------------------------------------------------------------------- +// Purpose: checks if the server index is valid, raises an error if not +//----------------------------------------------------------------------------- +static SQBool Script_CheckServerIndex(HSQUIRRELVM v, SQInteger iServer) +{ + SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); + + if (iServer >= iCount) + { + v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); + return false; + } + + return true; +} + namespace VScriptCode { namespace Client @@ -105,13 +121,10 @@ namespace VScriptCode SQRESULT GetServerName(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; } @@ -127,13 +140,10 @@ namespace VScriptCode SQRESULT GetServerDescription(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; } @@ -149,13 +159,10 @@ namespace VScriptCode SQRESULT GetServerMap(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; } @@ -171,13 +178,10 @@ namespace VScriptCode SQRESULT GetServerPlaylist(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; } @@ -193,13 +197,10 @@ namespace VScriptCode SQRESULT GetServerCurrentPlayers(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; } @@ -215,13 +216,10 @@ namespace VScriptCode SQRESULT GetServerMaxPlayers(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; } @@ -315,13 +313,10 @@ namespace VScriptCode SQRESULT ConnectToListedServer(HSQUIRRELVM v) { std::lock_guard l(g_pServerListManager->m_Mutex); - SQInteger iServer = sq_getinteger(v, 1); - SQInteger iCount = static_cast(g_pServerListManager->m_vServerList.size()); - if (iServer >= iCount) + if (!Script_CheckServerIndex(v, iServer)) { - v_SQVM_RaiseError(v, "Index must be less than %i.\n", iCount); return SQ_ERROR; }