Skip to content

Commit deada60

Browse files
authored
Fix return value for ColPolygon procedural functions (#1585)
1 parent 117b0b2 commit deada60

File tree

4 files changed

+154
-10
lines changed

4 files changed

+154
-10
lines changed

Client/mods/deathmatch/logic/luadefs/CLuaColShapeDefs.cpp

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ void CLuaColShapeDefs::AddClass(lua_State* luaVM)
5959
lua_classfunction(luaVM, "setRadius", SetColShapeRadius);
6060
lua_classfunction(luaVM, "getSize", OOP_GetColShapeSize);
6161
lua_classfunction(luaVM, "setSize", SetColShapeSize);
62-
lua_classfunction(luaVM, "getPoints", GetColPolygonPoints);
63-
lua_classfunction(luaVM, "getPointPosition", GetColPolygonPointPosition);
62+
lua_classfunction(luaVM, "getPoints", OOP_GetColPolygonPoints);
63+
lua_classfunction(luaVM, "getPointPosition", OOP_GetColPolygonPointPosition);
6464
lua_classfunction(luaVM, "setPointPosition", SetColPolygonPointPosition);
6565
lua_classfunction(luaVM, "addPoint", AddColPolygonPoint);
6666
lua_classfunction(luaVM, "removePoint", RemoveColPolygonPoint);
@@ -70,7 +70,7 @@ void CLuaColShapeDefs::AddClass(lua_State* luaVM)
7070

7171
lua_classvariable(luaVM, "radius", SetColShapeRadius, GetColShapeRadius);
7272
lua_classvariable(luaVM, "size", SetColShapeSize, OOP_GetColShapeSize);
73-
lua_classvariable(luaVM, "points", nullptr, GetColPolygonPoints);
73+
lua_classvariable(luaVM, "points", nullptr, OOP_GetColPolygonPoints);
7474

7575
lua_registerclass(luaVM, "ColShape", "Element");
7676
}
@@ -578,6 +578,46 @@ int CLuaColShapeDefs::GetColPolygonPoints(lua_State* luaVM)
578578
CScriptArgReader argStream(luaVM);
579579
argStream.ReadUserData(pColShape);
580580

581+
if (argStream.HasErrors())
582+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
583+
584+
if (pColShape->GetShapeType() == COLSHAPE_POLYGON)
585+
{
586+
CClientColPolygon* pColPolygon = static_cast<CClientColPolygon*>(pColShape);
587+
588+
lua_newtable(luaVM);
589+
590+
uint uiIndex = 0;
591+
for (auto iter = pColPolygon->IterBegin(); iter != pColPolygon->IterEnd(); ++iter)
592+
{
593+
CVector2D vecPoint = *iter;
594+
lua_pushnumber(luaVM, ++uiIndex);
595+
lua_newtable(luaVM);
596+
{
597+
lua_pushnumber(luaVM, 1);
598+
lua_pushnumber(luaVM, vecPoint.fX);
599+
lua_settable(luaVM, -3);
600+
601+
lua_pushnumber(luaVM, 2);
602+
lua_pushnumber(luaVM, vecPoint.fY);
603+
lua_settable(luaVM, -3);
604+
}
605+
lua_settable(luaVM, -3);
606+
}
607+
return 1;
608+
}
609+
610+
argStream.SetCustomError("ColShape must be Polygon");
611+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
612+
}
613+
614+
int CLuaColShapeDefs::OOP_GetColPolygonPoints(lua_State* luaVM)
615+
{
616+
CClientColShape* pColShape;
617+
618+
CScriptArgReader argStream(luaVM);
619+
argStream.ReadUserData(pColShape);
620+
581621
if (argStream.HasErrors())
582622
return luaL_error(luaVM, argStream.GetFullErrorMessage());
583623

@@ -610,6 +650,38 @@ int CLuaColShapeDefs::GetColPolygonPointPosition(lua_State* luaVM)
610650
argStream.ReadUserData(pColShape);
611651
argStream.ReadNumber(uiPointIndex);
612652

653+
if (argStream.HasErrors())
654+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
655+
656+
if (pColShape->GetShapeType() == COLSHAPE_POLYGON)
657+
{
658+
CClientColPolygon* pColPolygon = static_cast<CClientColPolygon*>(pColShape);
659+
CVector2D vecPoint;
660+
if (uiPointIndex > 0 && CStaticFunctionDefinitions::GetColPolygonPointPosition(pColPolygon, uiPointIndex - 1, vecPoint))
661+
{
662+
lua_pushnumber(luaVM, vecPoint.fX);
663+
lua_pushnumber(luaVM, vecPoint.fY);
664+
return 2;
665+
}
666+
667+
m_pScriptDebugging->LogWarning(luaVM, "Invalid point index");
668+
lua_pushboolean(luaVM, false);
669+
return 1;
670+
}
671+
672+
argStream.SetCustomError("ColShape must be Polygon");
673+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
674+
}
675+
676+
int CLuaColShapeDefs::OOP_GetColPolygonPointPosition(lua_State* luaVM)
677+
{
678+
CClientColShape* pColShape;
679+
uint uiPointIndex;
680+
681+
CScriptArgReader argStream(luaVM);
682+
argStream.ReadUserData(pColShape);
683+
argStream.ReadNumber(uiPointIndex);
684+
613685
if (argStream.HasErrors())
614686
return luaL_error(luaVM, argStream.GetFullErrorMessage());
615687

Client/mods/deathmatch/logic/luadefs/CLuaColShapeDefs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class CLuaColShapeDefs : public CLuaDefs
2929
LUA_DECLARE(SetColShapeRadius);
3030
LUA_DECLARE_OOP(GetColShapeSize);
3131
LUA_DECLARE(SetColShapeSize);
32-
LUA_DECLARE(GetColPolygonPoints);
33-
LUA_DECLARE(GetColPolygonPointPosition);
32+
LUA_DECLARE_OOP(GetColPolygonPoints);
33+
LUA_DECLARE_OOP(GetColPolygonPointPosition);
3434
LUA_DECLARE(SetColPolygonPointPosition);
3535
LUA_DECLARE(AddColPolygonPoint);
3636
LUA_DECLARE(RemoveColPolygonPoint);

Server/mods/deathmatch/logic/luadefs/CLuaColShapeDefs.cpp

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ void CLuaColShapeDefs::AddClass(lua_State* luaVM)
5959
lua_classfunction(luaVM, "setRadius", "setColShapeRadius", SetColShapeRadius);
6060
lua_classfunction(luaVM, "getSize", "getColShapeSize", OOP_GetColShapeSize);
6161
lua_classfunction(luaVM, "setSize", "setColShapeSize", SetColShapeSize);
62-
lua_classfunction(luaVM, "getPoints", "getColPolygonPoints", GetColPolygonPoints);
63-
lua_classfunction(luaVM, "getPointPosition", "getColPolygonPointPosition", GetColPolygonPointPosition);
62+
lua_classfunction(luaVM, "getPoints", "getColPolygonPoints", OOP_GetColPolygonPoints);
63+
lua_classfunction(luaVM, "getPointPosition", "getColPolygonPointPosition", OOP_GetColPolygonPointPosition);
6464
lua_classfunction(luaVM, "setPointPosition", "setColPolygonPointPosition", SetColPolygonPointPosition);
6565
lua_classfunction(luaVM, "addPoint", "addColPolygonPoint", AddColPolygonPoint);
6666
lua_classfunction(luaVM, "removePoint", "removeColPolygonPoint", RemoveColPolygonPoint);
@@ -69,7 +69,7 @@ void CLuaColShapeDefs::AddClass(lua_State* luaVM)
6969

7070
lua_classvariable(luaVM, "radius", "setColShapeRadius", "getColShapeRadius", SetColShapeRadius, GetColShapeRadius);
7171
lua_classvariable(luaVM, "size", "setColShapeSize", "getColShapeSize", SetColShapeSize, OOP_GetColShapeSize);
72-
lua_classvariable(luaVM, "points", nullptr, "getColPolygonPoints", nullptr, GetColPolygonPoints);
72+
lua_classvariable(luaVM, "points", nullptr, "getColPolygonPoints", nullptr, OOP_GetColPolygonPoints);
7373

7474
lua_registerclass(luaVM, "ColShape", "Element");
7575
}
@@ -559,6 +559,46 @@ int CLuaColShapeDefs::GetColPolygonPoints(lua_State* luaVM)
559559
CScriptArgReader argStream(luaVM);
560560
argStream.ReadUserData(pColShape);
561561

562+
if (argStream.HasErrors())
563+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
564+
565+
if (pColShape->GetShapeType() == COLSHAPE_POLYGON)
566+
{
567+
CColPolygon* pColPolygon = static_cast<CColPolygon*>(pColShape);
568+
569+
lua_newtable(luaVM);
570+
571+
uint uiIndex = 0;
572+
for (auto iter = pColPolygon->IterBegin(); iter != pColPolygon->IterEnd(); ++iter)
573+
{
574+
CVector2D vecPoint = *iter;
575+
lua_pushnumber(luaVM, ++uiIndex);
576+
lua_newtable(luaVM);
577+
{
578+
lua_pushnumber(luaVM, 1);
579+
lua_pushnumber(luaVM, vecPoint.fX);
580+
lua_settable(luaVM, -3);
581+
582+
lua_pushnumber(luaVM, 2);
583+
lua_pushnumber(luaVM, vecPoint.fY);
584+
lua_settable(luaVM, -3);
585+
}
586+
lua_settable(luaVM, -3);
587+
}
588+
return 1;
589+
}
590+
591+
argStream.SetCustomError("ColShape must be Polygon");
592+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
593+
}
594+
595+
int CLuaColShapeDefs::OOP_GetColPolygonPoints(lua_State* luaVM)
596+
{
597+
CColShape* pColShape;
598+
599+
CScriptArgReader argStream(luaVM);
600+
argStream.ReadUserData(pColShape);
601+
562602
if (argStream.HasErrors())
563603
return luaL_error(luaVM, argStream.GetFullErrorMessage());
564604

@@ -599,6 +639,38 @@ int CLuaColShapeDefs::GetColPolygonPointPosition(lua_State* luaVM)
599639
CColPolygon* pColPolygon = static_cast<CColPolygon*>(pColShape);
600640
CVector2D vecPoint;
601641
if (uiPointIndex > 0 && CStaticFunctionDefinitions::GetColPolygonPointPosition(pColPolygon, uiPointIndex - 1, vecPoint))
642+
{
643+
lua_pushnumber(luaVM, vecPoint.fX);
644+
lua_pushnumber(luaVM, vecPoint.fY);
645+
return 2;
646+
}
647+
648+
m_pScriptDebugging->LogWarning(luaVM, "Invalid point index");
649+
lua_pushboolean(luaVM, false);
650+
return 1;
651+
}
652+
653+
argStream.SetCustomError("ColShape must be Polygon");
654+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
655+
}
656+
657+
int CLuaColShapeDefs::OOP_GetColPolygonPointPosition(lua_State* luaVM)
658+
{
659+
CColShape* pColShape;
660+
uint uiPointIndex;
661+
662+
CScriptArgReader argStream(luaVM);
663+
argStream.ReadUserData(pColShape);
664+
argStream.ReadNumber(uiPointIndex);
665+
666+
if (argStream.HasErrors())
667+
return luaL_error(luaVM, argStream.GetFullErrorMessage());
668+
669+
if (pColShape->GetShapeType() == COLSHAPE_POLYGON)
670+
{
671+
CColPolygon* pColPolygon = static_cast<CColPolygon*>(pColShape);
672+
CVector2D vecPoint;
673+
if (uiPointIndex > 0 && CStaticFunctionDefinitions::GetColPolygonPointPosition(pColPolygon, uiPointIndex - 1, vecPoint))
602674
{
603675
lua_pushvector(luaVM, vecPoint);
604676
}

Server/mods/deathmatch/logic/luadefs/CLuaColShapeDefs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class CLuaColShapeDefs : public CLuaDefs
3030
LUA_DECLARE(SetColShapeRadius);
3131
LUA_DECLARE_OOP(GetColShapeSize);
3232
LUA_DECLARE(SetColShapeSize);
33-
LUA_DECLARE(GetColPolygonPoints);
34-
LUA_DECLARE(GetColPolygonPointPosition);
33+
LUA_DECLARE_OOP(GetColPolygonPoints);
34+
LUA_DECLARE_OOP(GetColPolygonPointPosition);
3535
LUA_DECLARE(SetColPolygonPointPosition);
3636
LUA_DECLARE(AddColPolygonPoint);
3737
LUA_DECLARE(RemoveColPolygonPoint);

0 commit comments

Comments
 (0)