Skip to content

Commit fa50dcf

Browse files
author
Marek Kulik
authored
Merge pull request #145 from Necktrox/fix/player-is-joined-warning
Show a warning if a script modifies a non-joined player
2 parents 1a8bafc + 367ec74 commit fa50dcf

File tree

5 files changed

+196
-29
lines changed

5 files changed

+196
-29
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,19 @@ void CLuaDefs::DidUseFunction ( lua_CFunction f, lua_State* luaVM )
249249

250250
g_pGame->GetDebugHookManager()->OnPostFunction( f, luaVM );
251251
}
252+
253+
//
254+
// Prints a warning message in script-debug if the element is a player and not joined yet
255+
//
256+
void CLuaDefs::LogWarningIfPlayerHasNotJoinedYet ( lua_State* luaVM, CElement* pElement )
257+
{
258+
static auto szWarningText = "Modifying players before onPlayerJoin can cause desynchronization";
259+
260+
if ( pElement && IS_PLAYER ( pElement ) )
261+
{
262+
auto pPlayer = static_cast < CPlayer * > ( pElement );
263+
264+
if ( !pPlayer->IsJoined() )
265+
m_pScriptDebugging->LogWarning ( luaVM, "%s: %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), szWarningText );
266+
}
267+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@
5858
class CLuaDefs
5959
{
6060
public:
61-
static void Initialize ( class CGame* pGame );
61+
static void Initialize ( class CGame* pGame );
6262

63-
static bool CanUseFunction ( const char* szFunction, lua_State* luaVM, bool bRestricted );
64-
static int CanUseFunction ( lua_CFunction f, lua_State* luaVM );
65-
static void DidUseFunction ( lua_CFunction f, lua_State* luaVM );
63+
static bool CanUseFunction ( const char* szFunction, lua_State* luaVM, bool bRestricted );
64+
static int CanUseFunction ( lua_CFunction f, lua_State* luaVM );
65+
static void DidUseFunction ( lua_CFunction f, lua_State* luaVM );
66+
static void LogWarningIfPlayerHasNotJoinedYet ( lua_State* luaVM, CElement* pElement );
6667

6768
// This is just for the Lua funcs. Please don't public this and use it other
6869
// places in the server.

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ void CLuaElementDefs::AddClass ( lua_State* luaVM )
201201
lua_registerclass ( luaVM, "Element" );
202202
}
203203

204+
204205
int CLuaElementDefs::createElement ( lua_State* luaVM )
205206
{
206207
// element createElement ( string elementType, [ string elementID ] )
@@ -269,6 +270,7 @@ int CLuaElementDefs::destroyElement ( lua_State* luaVM )
269270
return 1;
270271
}
271272

273+
272274
int CLuaElementDefs::cloneElement ( lua_State* luaVM )
273275
{
274276
// element cloneElement ( element theElement, [ float xPos = 0, float yPos = 0, float zPos = 0, bool cloneChildren = false ] )
@@ -597,6 +599,7 @@ int CLuaElementDefs::getElementPosition ( lua_State* luaVM )
597599
return 1;
598600
}
599601

602+
600603
int CLuaElementDefs::OOP_getElementPosition ( lua_State* luaVM )
601604
{
602605
CElement* pElement = NULL;
@@ -618,6 +621,7 @@ int CLuaElementDefs::OOP_getElementPosition ( lua_State* luaVM )
618621
return 1;
619622
}
620623

624+
621625
int CLuaElementDefs::getElementMatrix ( lua_State* luaVM )
622626
{
623627
CElement* pElement = NULL;
@@ -699,6 +703,7 @@ int CLuaElementDefs::getElementMatrix ( lua_State* luaVM )
699703
return 1;
700704
}
701705

706+
702707
int CLuaElementDefs::OOP_getElementMatrix ( lua_State* luaVM )
703708
{
704709
CElement* pEntity = NULL;
@@ -751,6 +756,7 @@ int CLuaElementDefs::getElementRotation ( lua_State* luaVM )
751756
return 1;
752757
}
753758

759+
754760
int CLuaElementDefs::OOP_getElementRotation ( lua_State* luaVM )
755761
{
756762
CElement* pElement = NULL;
@@ -777,6 +783,7 @@ int CLuaElementDefs::OOP_getElementRotation ( lua_State* luaVM )
777783
return 1;
778784
}
779785

786+
780787
int CLuaElementDefs::getElementVelocity ( lua_State* luaVM )
781788
{
782789
// float float float getElementVelocity ( element theElement )
@@ -805,6 +812,7 @@ int CLuaElementDefs::getElementVelocity ( lua_State* luaVM )
805812
return 1;
806813
}
807814

815+
808816
int CLuaElementDefs::OOP_getElementVelocity ( lua_State* luaVM )
809817
{
810818
CElement* pElement = NULL;
@@ -827,6 +835,7 @@ int CLuaElementDefs::OOP_getElementVelocity ( lua_State* luaVM )
827835
return 1;
828836
}
829837

838+
830839
int CLuaElementDefs::getElementType ( lua_State* luaVM )
831840
{
832841
// string getElementType ( element theElement )
@@ -898,6 +907,7 @@ int CLuaElementDefs::getElementInterior ( lua_State* luaVM )
898907
return 1;
899908
}
900909

910+
901911
int CLuaElementDefs::isElementWithinMarker ( lua_State* luaVM )
902912
{
903913
// bool isElementWithinMarker ( element theElement, marker theMarker )
@@ -1147,6 +1157,8 @@ int CLuaElementDefs::setElementAttachedOffsets ( lua_State* luaVM )
11471157

11481158
if ( !argStream.HasErrors () )
11491159
{
1160+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1161+
11501162
if ( CStaticFunctionDefinitions::SetElementAttachedOffsets ( pElement, vecPosition, vecRotation ) )
11511163
{
11521164
lua_pushboolean ( luaVM, true );
@@ -1342,6 +1354,7 @@ int CLuaElementDefs::getElementSyncer ( lua_State* luaVM )
13421354
return 1;
13431355
}
13441356

1357+
13451358
int CLuaElementDefs::getElementCollisionsEnabled ( lua_State* luaVM )
13461359
{
13471360
// bool getElementCollisionsEnabled ( element theElement )
@@ -1483,6 +1496,8 @@ int CLuaElementDefs::setElementID ( lua_State* luaVM )
14831496

14841497
if ( !argStream.HasErrors () )
14851498
{
1499+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1500+
14861501
if ( CStaticFunctionDefinitions::SetElementID ( pElement, strId ) )
14871502
{
14881503
lua_pushboolean ( luaVM, true );
@@ -1513,6 +1528,8 @@ int CLuaElementDefs::setElementData ( lua_State* luaVM )
15131528
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
15141529
if ( pLuaMain )
15151530
{
1531+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1532+
15161533
if ( strKey.length () > MAX_CUSTOMDATA_NAME_LENGTH )
15171534
{
15181535
// Warn and truncate if key is too long
@@ -1549,6 +1566,8 @@ int CLuaElementDefs::removeElementData ( lua_State* luaVM )
15491566
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
15501567
if ( pLuaMain )
15511568
{
1569+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1570+
15521571
if ( strKey.length () > MAX_CUSTOMDATA_NAME_LENGTH )
15531572
{
15541573
// Warn and truncate if key is too long
@@ -1594,6 +1613,8 @@ int CLuaElementDefs::setElementMatrix ( lua_State* luaVM )
15941613
// Verify the arguments
15951614
if ( !argStream.HasErrors ( ) )
15961615
{
1616+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1617+
15971618
if ( CStaticFunctionDefinitions::SetElementMatrix ( pElement, matrix ) )
15981619
{
15991620
lua_pushboolean ( luaVM, true );
@@ -1620,6 +1641,8 @@ int CLuaElementDefs::setElementParent ( lua_State* luaVM )
16201641

16211642
if ( !argStream.HasErrors () )
16221643
{
1644+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1645+
16231646
if ( CStaticFunctionDefinitions::SetElementParent ( pElement, pParent ) )
16241647
{
16251648
lua_pushboolean ( luaVM, true );
@@ -1658,6 +1681,8 @@ int CLuaElementDefs::setElementPosition ( lua_State* luaVM )
16581681

16591682
if ( !argStream.HasErrors () )
16601683
{
1684+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1685+
16611686
// Set the position
16621687
if ( CStaticFunctionDefinitions::SetElementPosition ( pElement, vecPosition, bWarp ) )
16631688
{
@@ -1686,6 +1711,8 @@ int CLuaElementDefs::setElementRotation ( lua_State* luaVM )
16861711

16871712
if ( !argStream.HasErrors () )
16881713
{
1714+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1715+
16891716
// Set the rotation
16901717
if ( CStaticFunctionDefinitions::SetElementRotation ( pElement, vecRotation, rotationOrder, bNewWay ) )
16911718
{
@@ -1700,6 +1727,7 @@ int CLuaElementDefs::setElementRotation ( lua_State* luaVM )
17001727
return 1;
17011728
}
17021729

1730+
17031731
int CLuaElementDefs::OOP_setElementRotation ( lua_State* luaVM )
17041732
{
17051733
// element.rotation = Vector3
@@ -1712,6 +1740,8 @@ int CLuaElementDefs::OOP_setElementRotation ( lua_State* luaVM )
17121740

17131741
if ( !argStream.HasErrors () )
17141742
{
1743+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1744+
17151745
CMatrix matrix;
17161746

17171747
// fill in our matrix
@@ -1749,6 +1779,7 @@ int CLuaElementDefs::OOP_setElementRotation ( lua_State* luaVM )
17491779
return 1;
17501780
}
17511781

1782+
17521783
int CLuaElementDefs::setElementVelocity ( lua_State* luaVM )
17531784
{
17541785
// bool setElementVelocity ( element theElement, float speedX, float speedY, float speedZ )
@@ -1760,6 +1791,8 @@ int CLuaElementDefs::setElementVelocity ( lua_State* luaVM )
17601791

17611792
if ( !argStream.HasErrors () )
17621793
{
1794+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1795+
17631796
// Set the velocity
17641797
if ( CStaticFunctionDefinitions::SetElementVelocity ( pElement, vecVelocity ) )
17651798
{
@@ -1787,6 +1820,8 @@ int CLuaElementDefs::setElementVisibleTo ( lua_State* luaVM )
17871820

17881821
if ( !argStream.HasErrors () )
17891822
{
1823+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1824+
17901825
if ( CStaticFunctionDefinitions::SetElementVisibleTo ( pElement, pReference, bVisible ) )
17911826
{
17921827
lua_pushboolean ( luaVM, true );
@@ -1814,6 +1849,8 @@ int CLuaElementDefs::setElementInterior ( lua_State* luaVM )
18141849

18151850
if ( !argStream.HasErrors () )
18161851
{
1852+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1853+
18171854
unsigned char ucInterior = static_cast < unsigned char > ( uiInterior );
18181855
if ( CStaticFunctionDefinitions::SetElementInterior ( pElement, ucInterior, bSetPosition, vecPosition ) )
18191856
{
@@ -1853,6 +1890,8 @@ int CLuaElementDefs::setElementDimension ( lua_State* luaVM )
18531890

18541891
if ( !argStream.HasErrors () )
18551892
{
1893+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1894+
18561895
if ( bMakeVisibleInAllDimensions )
18571896
{
18581897
// Set the object visible in all dimensions
@@ -1905,6 +1944,8 @@ int CLuaElementDefs::attachElements ( lua_State* luaVM )
19051944

19061945
if ( !argStream.HasErrors () )
19071946
{
1947+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1948+
19081949
if ( CStaticFunctionDefinitions::AttachElements ( pElement, pAttachedToElement, vecPosition, vecRotation ) )
19091950
{
19101951
lua_pushboolean ( luaVM, true );
@@ -1930,6 +1971,8 @@ int CLuaElementDefs::detachElements ( lua_State* luaVM )
19301971

19311972
if ( !argStream.HasErrors () )
19321973
{
1974+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
1975+
19331976
if ( CStaticFunctionDefinitions::DetachElements ( pElement, pAttachedToElement ) )
19341977
{
19351978
lua_pushboolean ( luaVM, true );
@@ -1955,6 +1998,8 @@ int CLuaElementDefs::setElementAlpha ( lua_State* luaVM )
19551998

19561999
if ( !argStream.HasErrors () )
19572000
{
2001+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
2002+
19582003
if ( CStaticFunctionDefinitions::SetElementAlpha ( pElement, ucAlpha ) )
19592004
{
19602005
lua_pushboolean ( luaVM, true );
@@ -2005,6 +2050,8 @@ int CLuaElementDefs::setElementHealth ( lua_State* luaVM )
20052050

20062051
if ( !argStream.HasErrors() )
20072052
{
2053+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
2054+
20082055
if ( CStaticFunctionDefinitions::SetElementHealth ( pElement, fHealth ) )
20092056
{
20102057
lua_pushboolean ( luaVM, true );
@@ -2030,6 +2077,8 @@ int CLuaElementDefs::setElementModel ( lua_State* luaVM )
20302077

20312078
if ( !argStream.HasErrors () )
20322079
{
2080+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
2081+
20332082
if ( CStaticFunctionDefinitions::SetElementModel ( pElement, usModel ) )
20342083
{
20352084
lua_pushboolean ( luaVM, true );
@@ -2043,6 +2092,7 @@ int CLuaElementDefs::setElementModel ( lua_State* luaVM )
20432092
return 1;
20442093
}
20452094

2095+
20462096
int CLuaElementDefs::setElementSyncer ( lua_State* luaVM )
20472097
{
20482098
// bool setElementSyncer ( element theElement, player thePlayer )
@@ -2068,6 +2118,7 @@ int CLuaElementDefs::setElementSyncer ( lua_State* luaVM )
20682118
return 1;
20692119
}
20702120

2121+
20712122
int CLuaElementDefs::setElementCollisionsEnabled ( lua_State* luaVM )
20722123
{
20732124
// bool setElementCollisionsEnabled ( element theElement, bool enabled )
@@ -2079,6 +2130,8 @@ int CLuaElementDefs::setElementCollisionsEnabled ( lua_State* luaVM )
20792130

20802131
if ( !argStream.HasErrors () )
20812132
{
2133+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
2134+
20822135
if ( CStaticFunctionDefinitions::SetElementCollisionsEnabled ( pElement, bEnable ) )
20832136
{
20842137
lua_pushboolean ( luaVM, true );
@@ -2092,6 +2145,7 @@ int CLuaElementDefs::setElementCollisionsEnabled ( lua_State* luaVM )
20922145
return 1;
20932146
}
20942147

2148+
20952149
int CLuaElementDefs::setElementFrozen ( lua_State* luaVM )
20962150
{
20972151
// bool setElementFrozen ( element theElement, bool freezeStatus )
@@ -2103,6 +2157,8 @@ int CLuaElementDefs::setElementFrozen ( lua_State* luaVM )
21032157

21042158
if ( !argStream.HasErrors () )
21052159
{
2160+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pElement );
2161+
21062162
if ( CStaticFunctionDefinitions::SetElementFrozen ( pElement, bFrozen ) )
21072163
{
21082164
lua_pushboolean ( luaVM, true );
@@ -2141,6 +2197,7 @@ int CLuaElementDefs::getLowLODElement ( lua_State* luaVM )
21412197
return 1;
21422198
}
21432199

2200+
21442201
int CLuaElementDefs::setLowLODElement ( lua_State* luaVM )
21452202
{
21462203
// bool setLowLODElement ( element theElement )
@@ -2202,6 +2259,8 @@ int CLuaElementDefs::setElementCallPropagationEnabled ( lua_State* luaVM )
22022259

22032260
if ( !argStream.HasErrors () )
22042261
{
2262+
LogWarningIfPlayerHasNotJoinedYet ( luaVM, pEntity );
2263+
22052264
if ( CStaticFunctionDefinitions::SetElementCallPropagationEnabled ( pEntity, bEnable ) )
22062265
{
22072266
lua_pushboolean ( luaVM, true );

0 commit comments

Comments
 (0)