Skip to content

Commit

Permalink
Add CMatrix 4x4 lua table in ArgumentParser (multitheftauto#3849)
Browse files Browse the repository at this point in the history
* Add CMatrix 4x4 lua table in ArgumentParse

* Update due to code revision

---------

Co-authored-by: Uladzislau Nikalayevich <[email protected]>
Co-authored-by: TEDERIs <[email protected]>
  • Loading branch information
3 people authored Dec 23, 2024
1 parent 18f65eb commit b31b192
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,42 @@ void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMat
}
}

//
// Check 4x4 lua table
//
bool IsValidMatrixLuaTable(lua_State* luaVM, std::uint32_t argIndex) noexcept
{
std::uint32_t cell = 0;

if (lua_type(luaVM, argIndex) == LUA_TTABLE)
{
lua_pushnil(luaVM);
for (std::uint32_t row = 0; lua_next(luaVM, argIndex) != 0; lua_pop(luaVM, 1), ++row)
{
if (lua_type(luaVM, -1) != LUA_TTABLE)
return false;

std::uint32_t col = 0;

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

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

if (cell != 16)
return false;

return true;
}

//
// 4x4 matrix into CMatrix
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,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, std::uint32_t argIndex) noexcept;
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
36 changes: 36 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,42 @@ void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions)
}
}

//
// Check 4x4 lua table
//
bool IsValidMatrixLuaTable(lua_State* luaVM, std::uint32_t argIndex) noexcept
{
std::uint32_t cell = 0;

if (lua_type(luaVM, argIndex) == LUA_TTABLE)
{
lua_pushnil(luaVM);
for (std::uint32_t row = 0; lua_next(luaVM, argIndex) != 0; lua_pop(luaVM, 1), ++row)
{
if (lua_type(luaVM, -1) != LUA_TTABLE)
return false;

std::uint32_t col = 0;

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

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

if (cell != 16)
return false;

return true;
}

//
// 4x4 matrix into CMatrix
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,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, std::uint32_t argIndex) noexcept;
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 @@ -639,6 +642,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 b31b192

Please sign in to comment.