Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic_substring #140

Merged
merged 5 commits into from
Sep 25, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions sp/src/game/server/logic_substring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
//====================== 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;

int startPosCheck = m_nStartPos < 0 ? Q_strlen(inputData.value.String()) + m_nStartPos : m_nStartPos;
moofemp marked this conversation as resolved.
Show resolved Hide resolved
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 );
moofemp marked this conversation as resolved.
Show resolved Hide resolved
}

//-----------------------------------------------------------------------------
// 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 );
}