Skip to content

Commit b4768dd

Browse files
committed
Add CMatrix 4x4 lua table in ArgumentParse
1 parent ad1da87 commit b4768dd

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,41 @@ void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMat
11091109
}
11101110
}
11111111

1112+
//
1113+
// Check 4x4 lua table
1114+
//
1115+
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex)
1116+
{
1117+
uint uiRow = 0;
1118+
uint uiCell = 0;
1119+
1120+
if (lua_type(luaVM, uiArgIndex) == LUA_TTABLE)
1121+
{
1122+
for (lua_pushnil(luaVM); lua_next(luaVM, uiArgIndex) != 0; lua_pop(luaVM, 1), uiRow++)
1123+
{
1124+
if (lua_type(luaVM, -1) != LUA_TTABLE)
1125+
return false;
1126+
1127+
uint uiCol = 0;
1128+
1129+
for (lua_pushnil(luaVM); lua_next(luaVM, -2) != 0; lua_pop(luaVM, 1), uiCol++, uiCell++)
1130+
{
1131+
int iArgumentType = lua_type(luaVM, -1);
1132+
if (iArgumentType != LUA_TNUMBER && iArgumentType != LUA_TSTRING)
1133+
return false;
1134+
}
1135+
1136+
if (uiCol != 4)
1137+
return false;
1138+
}
1139+
}
1140+
1141+
if (uiRow != 4 || uiCell != 16)
1142+
return false;
1143+
1144+
return true;
1145+
}
1146+
11121147
//
11131148
// 4x4 matrix into CMatrix
11141149
//

Client/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ class CScriptArgReader;
584584
void MixedReadDxFontString(CScriptArgReader& argStream, eFontType& outFontType, eFontType defaultFontType, CClientDxFont*& poutDxFontElement);
585585
void MixedReadGuiFontString(CScriptArgReader& argStream, SString& strFontName, const char* szDefaultFontName, CClientGuiFont*& poutGuiFontElement);
586586
void MixedReadMaterialString(CScriptArgReader& argStream, CClientMaterial*& pMaterialElement);
587+
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex);
587588
bool ReadMatrix(lua_State* luaVM, uint uiArgIndex, CMatrix& outMatrix);
588589
void MinClientReqCheck(lua_State* luaVM, const char* szVersionReq, const char* szReason);
589590
bool MinClientReqCheck(CScriptArgReader& argStream, const char* szVersionReq, const char* szReason = nullptr);

Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,41 @@ void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions)
640640
}
641641
}
642642

643+
//
644+
// Check 4x4 lua table
645+
//
646+
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex)
647+
{
648+
uint uiRow = 0;
649+
uint uiCell = 0;
650+
651+
if (lua_type(luaVM, uiArgIndex) == LUA_TTABLE)
652+
{
653+
for (lua_pushnil(luaVM); lua_next(luaVM, uiArgIndex) != 0; lua_pop(luaVM, 1), uiRow++)
654+
{
655+
if (lua_type(luaVM, -1) != LUA_TTABLE)
656+
return false;
657+
658+
uint uiCol = 0;
659+
660+
for (lua_pushnil(luaVM); lua_next(luaVM, -2) != 0; lua_pop(luaVM, 1), uiCol++, uiCell++)
661+
{
662+
int iArgumentType = lua_type(luaVM, -1);
663+
if (iArgumentType != LUA_TNUMBER && iArgumentType != LUA_TSTRING)
664+
return false;
665+
}
666+
667+
if (uiCol != 4)
668+
return false;
669+
}
670+
}
671+
672+
if (uiRow != 4 || uiCell != 16)
673+
return false;
674+
675+
return true;
676+
}
677+
643678
//
644679
// 4x4 matrix into CMatrix
645680
//

Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ void MixedReadResourceString(CScriptArgReader& argStream, CResource*& pOutRes
389389
bool StringToBool(const SString& strText);
390390
void MinServerReqCheck(CScriptArgReader& argStream, const char* szVersionReq, const char* szReason);
391391
void ReadPregFlags(CScriptArgReader& argStream, pcrecpp::RE_Options& pOptions);
392+
bool IsValidMatrixLuaTable(lua_State* luaVM, uint uiArgIndex);
392393
bool ReadMatrix(lua_State* luaVM, uint uiArgIndex, CMatrix& outMatrix);
393394

394395
//

Shared/mods/deathmatch/logic/lua/CLuaFunctionParser.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,12 @@ struct CLuaFunctionParserBase
283283
return true;
284284
return iArgument == LUA_TUSERDATA || iArgument == LUA_TLIGHTUSERDATA;
285285
}
286-
// CMatrix may either be represented by 3 CLuaVector or by 12 numbers
286+
// CMatrix can be represented either by 3 CLuaVectors, 12 numbers, or a 4x4 Lua table
287287
else if constexpr (std::is_same_v<T, CMatrix>)
288288
{
289+
if (IsValidMatrixLuaTable(L, index))
290+
return true;
291+
289292
for (int i = 0; i < sizeof(CMatrix) / sizeof(float); i++)
290293
{
291294
if (!lua_isnumber(L, index + i))
@@ -618,6 +621,19 @@ struct CLuaFunctionParserBase
618621
return matrix;
619622
}
620623

624+
if (lua_istable(L, index))
625+
{
626+
CMatrix matrix;
627+
628+
if (!ReadMatrix(L, index, matrix))
629+
{
630+
SetBadArgumentError(L, "matrix", index, "table");
631+
return T{};
632+
}
633+
634+
return matrix;
635+
}
636+
621637
int iType = lua_type(L, index);
622638
bool isLightUserData = iType == LUA_TLIGHTUSERDATA;
623639
void* pValue = lua::PopPrimitive<void*>(L, index);

0 commit comments

Comments
 (0)