From 22557f3751c8c377fe9070f31b760bce16ece6a2 Mon Sep 17 00:00:00 2001 From: MoofEMP <5711800-MoofEMP@users.noreply.gitlab.com> Date: Wed, 21 Jul 2021 19:56:39 -0400 Subject: [PATCH 1/5] Add logic_substring --- sp/src/game/server/logic_substring.cpp | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 sp/src/game/server/logic_substring.cpp diff --git a/sp/src/game/server/logic_substring.cpp b/sp/src/game/server/logic_substring.cpp new file mode 100644 index 00000000..e7ed2b71 --- /dev/null +++ b/sp/src/game/server/logic_substring.cpp @@ -0,0 +1,97 @@ +//====================== By Holly Liberatore / MoofEMP ======================// +// +// Purpose: Takes a string parameter and returns a substring defined by keyvalues +// +//===========================================================================// + +#include "cbase.h" + +#define SF_SUBSTRING_START_DISABLED (1 << 0) + +class CLogicSubstring : public CLogicalEntity +{ +public: + DECLARE_CLASS( CLogicSubstring, CLogicalEntity ); + DECLARE_DATADESC(); + + CLogicSubstring( void ) { } + + void InputDisable( inputdata_t &inputData ); + void InputEnable( inputdata_t &inputData ); + void InputInValue( inputdata_t &inputData ); + void InputSetLength( inputdata_t &inputData ); + void InputSetStartPos( inputdata_t &inputData ); + + void Spawn(void); + +private: + int m_nLength; + int m_nStartPos; + + bool m_bEnabled; + + COutputString m_OutValue; +}; + +LINK_ENTITY_TO_CLASS( logic_substring, CLogicSubstring ); + +BEGIN_DATADESC( CLogicSubstring ) + + DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ), + + DEFINE_KEYFIELD(m_nLength, FIELD_INTEGER, "length" ), + DEFINE_KEYFIELD(m_nStartPos, FIELD_INTEGER, "startPos" ), + + DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ), + DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ), + DEFINE_INPUTFUNC( FIELD_STRING, "InValue", InputInValue ), + DEFINE_INPUTFUNC( FIELD_INTEGER, "SetLength", InputSetLength ), + DEFINE_INPUTFUNC( FIELD_INTEGER, "SetStartPos", InputSetStartPos ), + + DEFINE_OUTPUT( m_OutValue, "OutValue" ), + +END_DATADESC() + +//----------------------------------------------------------------------------- +// Purpose: Disable or enable the entity (disabling prevents any input functions from running) +//----------------------------------------------------------------------------- +void CLogicSubstring::InputDisable( inputdata_t &inputData ) { m_bEnabled = false; } +void CLogicSubstring::InputEnable ( inputdata_t &inputData ) { m_bEnabled = true ; } + +//----------------------------------------------------------------------------- +// Purpose: Trim substring from input +// Output: Substring +//----------------------------------------------------------------------------- +void CLogicSubstring::InputInValue( inputdata_t &inputData ) +{ + if( !m_bEnabled ) return; + + char* strOutValue = (char*)malloc( m_nLength ); + Q_strncpy( strOutValue, inputData.value.String() + m_nStartPos, m_nLength + 1 ); // note length+1 to account for null terminator + m_OutValue.Set( MAKE_STRING(strOutValue), inputData.pActivator, this ); +} + +//----------------------------------------------------------------------------- +// Purpose: Setter methods for keyvalues +//----------------------------------------------------------------------------- +void CLogicSubstring::InputSetLength( inputdata_t &inputData ) +{ + if( !m_bEnabled ) return; + + m_nLength = inputData.value.Int(); +} + +void CLogicSubstring::InputSetStartPos( inputdata_t &inputData ) +{ + if( !m_bEnabled ) return; + + m_nStartPos = inputData.value.Int(); +} + +//----------------------------------------------------------------------------- +// Purpose: Respond to spawnflags when entity spawns +//----------------------------------------------------------------------------- +void CLogicSubstring::Spawn( void ) +{ + m_bEnabled = !HasSpawnFlags( SF_SUBSTRING_START_DISABLED ); +} From 41cde5ccf735c342f34713389b13307ba90b0497 Mon Sep 17 00:00:00 2001 From: MoofEMP <5711800-MoofEMP@users.noreply.gitlab.com> Date: Tue, 27 Jul 2021 02:29:32 -0400 Subject: [PATCH 2/5] Fix logic_substring behaviour with unexpected length/startpos values --- sp/src/game/server/logic_substring.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sp/src/game/server/logic_substring.cpp b/sp/src/game/server/logic_substring.cpp index e7ed2b71..648cf2fe 100644 --- a/sp/src/game/server/logic_substring.cpp +++ b/sp/src/game/server/logic_substring.cpp @@ -66,8 +66,19 @@ void CLogicSubstring::InputInValue( inputdata_t &inputData ) { if( !m_bEnabled ) return; - char* strOutValue = (char*)malloc( m_nLength ); - Q_strncpy( strOutValue, inputData.value.String() + m_nStartPos, m_nLength + 1 ); // note length+1 to account for null terminator + int startPosCheck = m_nStartPos < 0 ? Q_strlen(inputData.value.String()) + m_nStartPos : m_nStartPos; + if( startPosCheck < 0 ) + { + startPosCheck = 0; + } + int lengthCheck = (m_nLength < 0 || m_nLength > Q_strlen(inputData.value.String()) - startPosCheck ? Q_strlen(inputData.value.String()) - startPosCheck : m_nLength) + 1; + if( lengthCheck < 1 || startPosCheck > Q_strlen(inputData.value.String()) ) + { + m_OutValue.Set( MAKE_STRING(""), inputData.pActivator, this ); + return; + } + char* strOutValue = (char*)malloc( lengthCheck ); + Q_strncpy( strOutValue, inputData.value.String() + startPosCheck, lengthCheck ); m_OutValue.Set( MAKE_STRING(strOutValue), inputData.pActivator, this ); } From 99a8bdcb3723e752741a3c7b905ee73a2254ed12 Mon Sep 17 00:00:00 2001 From: MoofEMP <5711800-MoofEMP@users.noreply.gitlab.com> Date: Tue, 27 Jul 2021 17:46:41 -0400 Subject: [PATCH 3/5] Use a variable for input string length in logic_substring --- sp/src/game/server/logic_substring.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sp/src/game/server/logic_substring.cpp b/sp/src/game/server/logic_substring.cpp index 648cf2fe..d1e9b96c 100644 --- a/sp/src/game/server/logic_substring.cpp +++ b/sp/src/game/server/logic_substring.cpp @@ -66,13 +66,14 @@ void CLogicSubstring::InputInValue( inputdata_t &inputData ) { if( !m_bEnabled ) return; - int startPosCheck = m_nStartPos < 0 ? Q_strlen(inputData.value.String()) + m_nStartPos : m_nStartPos; + int inputLength = Q_strlen(inputData.value.String()); + int startPosCheck = m_nStartPos < 0 ? inputLength + m_nStartPos : m_nStartPos; if( startPosCheck < 0 ) { startPosCheck = 0; } - int lengthCheck = (m_nLength < 0 || m_nLength > Q_strlen(inputData.value.String()) - startPosCheck ? Q_strlen(inputData.value.String()) - startPosCheck : m_nLength) + 1; - if( lengthCheck < 1 || startPosCheck > Q_strlen(inputData.value.String()) ) + int lengthCheck = (m_nLength < 0 || m_nLength > inputLength - startPosCheck ? inputLength - startPosCheck : m_nLength) + 1; + if( lengthCheck < 1 || startPosCheck > inputLength ) { m_OutValue.Set( MAKE_STRING(""), inputData.pActivator, this ); return; From 3e9d3deda2eb3428cade7b1b1cbc55d0cf0d4304 Mon Sep 17 00:00:00 2001 From: Moofles <62188664+moofemp@users.noreply.github.com> Date: Tue, 27 Jul 2021 17:50:32 -0400 Subject: [PATCH 4/5] Use AllocPooledString() to prevent memory leak in logic_substring Co-authored-by: Spencer Brown --- sp/src/game/server/logic_substring.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sp/src/game/server/logic_substring.cpp b/sp/src/game/server/logic_substring.cpp index d1e9b96c..7c7d153b 100644 --- a/sp/src/game/server/logic_substring.cpp +++ b/sp/src/game/server/logic_substring.cpp @@ -80,7 +80,8 @@ void CLogicSubstring::InputInValue( inputdata_t &inputData ) } char* strOutValue = (char*)malloc( lengthCheck ); Q_strncpy( strOutValue, inputData.value.String() + startPosCheck, lengthCheck ); - m_OutValue.Set( MAKE_STRING(strOutValue), inputData.pActivator, this ); + m_OutValue.Set( AllocPooledString(strOutValue), inputData.pActivator, this ); + free(strOutValue); } //----------------------------------------------------------------------------- From f1a8638a348af358508e9adfdad35173f432fec9 Mon Sep 17 00:00:00 2001 From: MoofEMP <5711800-MoofEMP@users.noreply.gitlab.com> Date: Tue, 27 Jul 2021 22:41:43 -0400 Subject: [PATCH 5/5] Move logic_substring.cpp to mapbase folder --- sp/src/game/server/{ => mapbase}/logic_substring.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sp/src/game/server/{ => mapbase}/logic_substring.cpp (100%) diff --git a/sp/src/game/server/logic_substring.cpp b/sp/src/game/server/mapbase/logic_substring.cpp similarity index 100% rename from sp/src/game/server/logic_substring.cpp rename to sp/src/game/server/mapbase/logic_substring.cpp