Skip to content

Commit

Permalink
Add CMatrix 4x4 lua table in ArgumentParse
Browse files Browse the repository at this point in the history
  • Loading branch information
W3lac3 committed Nov 9, 2024
1 parent ad1da87 commit b4768dd
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,41 @@ void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMat
}
}

//
// Check 4x4 lua table
//
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex)
{
uint uiRow = 0;
uint uiCell = 0;

if (lua_type(luaVM, uiArgIndex) == LUA_TTABLE)
{
for (lua_pushnil(luaVM); lua_next(luaVM, uiArgIndex) != 0; lua_pop(luaVM, 1), uiRow++)
{
if (lua_type(luaVM, -1) != LUA_TTABLE)
return false;

uint uiCol = 0;

for (lua_pushnil(luaVM); lua_next(luaVM, -2) != 0; lua_pop(luaVM, 1), uiCol++, uiCell++)
{
int iArgumentType = lua_type(luaVM, -1);
if (iArgumentType != LUA_TNUMBER && iArgumentType != LUA_TSTRING)
return false;
}

if (uiCol != 4)
return false;
}
}

if (uiRow != 4 || uiCell != 16)
return false;

return true;
}

//
// 4x4 matrix into CMatrix
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ class CScriptArgReader;
void MixedReadDxFontString(CScriptArgReader& argStream, eFontType& outFontType, eFontType defaultFontType, CClientDxFont*& poutDxFontElement);
void MixedReadGuiFontString(CScriptArgReader& argStream, SString& strFontName, const char* szDefaultFontName, CClientGuiFont*& poutGuiFontElement);
void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMaterialElement);
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex);
bool ReadMatrix(lua_State* luaVM, uint uiArgIndex, CMatrix& outMatrix);
void MinClientReqCheck(lua_State* luaVM, const char* szVersionReq, const char* szReason);
bool MinClientReqCheck(CScriptArgReader& argStream, const char* szVersionReq, const char* szReason = nullptr);
Expand Down
35 changes: 35 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,41 @@ void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions)
}
}

//
// Check 4x4 lua table
//
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex)
{
uint uiRow = 0;
uint uiCell = 0;

if (lua_type(luaVM, uiArgIndex) == LUA_TTABLE)
{
for (lua_pushnil(luaVM); lua_next(luaVM, uiArgIndex) != 0; lua_pop(luaVM, 1), uiRow++)
{
if (lua_type(luaVM, -1) != LUA_TTABLE)
return false;

uint uiCol = 0;

for (lua_pushnil(luaVM); lua_next(luaVM, -2) != 0; lua_pop(luaVM, 1), uiCol++, uiCell++)
{
int iArgumentType = lua_type(luaVM, -1);
if (iArgumentType != LUA_TNUMBER && iArgumentType != LUA_TSTRING)
return false;
}

if (uiCol != 4)
return false;
}
}

if (uiRow != 4 || uiCell != 16)
return false;

return true;
}

//
// 4x4 matrix into CMatrix
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ void MixedReadResourceString(CScriptArgReader& argStream, CResource*& pOutRes
bool StringToBool(const SString& strText);
void MinServerReqCheck(CScriptArgReader& argStream, const char* szVersionReq, const char* szReason);
void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions);
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex);
bool ReadMatrix(lua_State* luaVM, uint uiArgIndex, CMatrix& outMatrix);

//
Expand Down
18 changes: 17 additions & 1 deletion Shared/mods/deathmatch/logic/lua/CLuaFunctionParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,12 @@ struct CLuaFunctionParserBase
return true;
return iArgument == LUA_TUSERDATA || iArgument == LUA_TLIGHTUSERDATA;
}
// CMatrix may either be represented by 3 CLuaVector or by 12 numbers
// CMatrix can be represented either by 3 CLuaVectors, 12 numbers, or a 4x4 Lua table
else if constexpr (std::is_same_v<T, CMatrix>)
{
if (IsValidMatrixLuaTable(L, index))
return true;

for (int i = 0; i < sizeof(CMatrix) / sizeof(float); i++)
{
if (!lua_isnumber(L, index + i))
Expand Down Expand Up @@ -618,6 +621,19 @@ struct CLuaFunctionParserBase
return matrix;
}

if (lua_istable(L, index))
{
CMatrix matrix;

if (!ReadMatrix(L, index, matrix))
{
SetBadArgumentError(L, "matrix", index, "table");
return T{};
}

return matrix;
}

int iType = lua_type(L, index);
bool isLightUserData = iType == LUA_TLIGHTUSERDATA;
void* pValue = lua::PopPrimitive<void*>(L, index);
Expand Down

0 comments on commit b4768dd

Please sign in to comment.