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

Develop #14

Merged
merged 5 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions sp/src/game/client/vscript_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,10 @@ bool VScriptClientInit()
VScriptRunScript( "vscript_client", true );
VScriptRunScript( "mapspawn", false );

#ifdef MAPBASE_VSCRIPT
RunAddonScripts();
#endif

VMPROF_SHOW( pszScriptLanguage, "virtual machine startup" );

return true;
Expand Down
10 changes: 8 additions & 2 deletions sp/src/game/server/ai_baseactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,8 +1149,14 @@ void CAI_BaseActor::UpdateHeadControl( const Vector &vHeadTarget, float flHeadIn
Warning( "================================================================================\n"
"!!!!! %s tried to set a NaN head angle (can happen when look targets have >1 importance) !!!!!\n"
"================================================================================\n", GetDebugName() );
vTargetAngles.x = 0;
vTargetAngles.y = 0;
m_goalHeadCorrection.Init();
Set( m_FlexweightHeadRightLeft, 0.0f );
Set( m_FlexweightHeadUpDown, 0.0f );
Set( m_FlexweightHeadTilt, 0.0f );
Set( m_ParameterHeadYaw, 0.0f );
Set( m_ParameterHeadPitch, 0.0f );
Set( m_ParameterHeadRoll, 0.0f );
return;
}
#endif

Expand Down
3 changes: 3 additions & 0 deletions sp/src/game/server/globalstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ class CGlobalState : public CAutoGameSystem
entity.name = m_nameList.AddString( pGlobalname );
entity.levelName = m_nameList.AddString( pMapName );
entity.state = state;
#ifdef MAPBASE
entity.counter = 0;
#endif

int index = GetIndex( m_nameList.String( entity.name ) );
if ( index >= 0 )
Expand Down
2 changes: 2 additions & 0 deletions sp/src/game/server/vscript_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ bool VScriptServerInit()
VScriptRunScript( "mapspawn", false );

#ifdef MAPBASE_VSCRIPT
RunAddonScripts();

// Since the world entity spawns before VScript is initted, RunVScripts() is called before the VM has started, so no scripts are run.
// This gets around that by calling the same function right after the VM is initted.
GetWorldEntity()->RunVScripts();
Expand Down
2 changes: 1 addition & 1 deletion sp/src/game/server/vscript_server.nut
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function __ReplaceClosures( script, scope )

local tempParent = { getroottable = function() { return null; } };
local temp = { runscript = script };
temp.set_delegate(tempParent);
temp.setdelegate(tempParent);

temp.runscript()
foreach( key,val in temp )
Expand Down
2 changes: 1 addition & 1 deletion sp/src/game/shared/mapbase/mapbase_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class CMapbaseSystem : public CAutoGameSystem

void LoadFromValue( const char *value, int type, bool bDontWarn )
{
if (!filesystem->FileExists(value, "MOD"))
if (type != MANIFEST_VSCRIPT && !filesystem->FileExists(value, "MOD"))
{
if (!bDontWarn)
{
Expand Down
193 changes: 184 additions & 9 deletions sp/src/game/shared/vscript_shared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ extern int vscript_token;
int vscript_token_hack = vscript_token;
#endif

static const char *pszExtensions[] =
{
"", // SL_NONE
".gm", // SL_GAMEMONKEY
".nut", // SL_SQUIRREL
".lua", // SL_LUA
".py", // SL_PYTHON
};



HSCRIPT VScriptCompileScript( const char *pszScriptName, bool bWarnMissing )
Expand All @@ -49,15 +58,6 @@ HSCRIPT VScriptCompileScript( const char *pszScriptName, bool bWarnMissing )
return NULL;
}

static const char *pszExtensions[] =
{
"", // SL_NONE
".gm", // SL_GAMEMONKEY
".nut", // SL_SQUIRREL
".lua", // SL_LUA
".py", // SL_PYTHON
};

const char *pszVMExtension = pszExtensions[g_pScriptVM->GetLanguage()];
const char *pszIncomingExtension = V_strrchr( pszScriptName , '.' );
if ( pszIncomingExtension && V_strcmp( pszIncomingExtension, pszVMExtension ) != 0 )
Expand Down Expand Up @@ -171,6 +171,113 @@ bool VScriptRunScript( const char *pszScriptName, HSCRIPT hScope, bool bWarnMiss
}


#ifdef MAPBASE_VSCRIPT

//
// These functions are currently only used for "mapspawn_addon" scripts.
//
HSCRIPT VScriptCompileScriptAbsolute( const char *pszScriptName, bool bWarnMissing, const char *pszRootFolderName )
{
if ( !g_pScriptVM )
{
return NULL;
}

const char *pszVMExtension = pszExtensions[g_pScriptVM->GetLanguage()];
const char *pszIncomingExtension = V_strrchr( pszScriptName , '.' );
if ( pszIncomingExtension && V_strcmp( pszIncomingExtension, pszVMExtension ) != 0 )
{
CGWarning( 0, CON_GROUP_VSCRIPT, "Script file type does not match VM type\n" );
return NULL;
}

CFmtStr scriptPath;
if ( pszIncomingExtension )
{
scriptPath = pszScriptName;
}
else
{
scriptPath.sprintf( "%s%s", pszScriptName, pszVMExtension );
}

const char *pBase;
CUtlBuffer bufferScript;

if ( g_pScriptVM->GetLanguage() == SL_PYTHON )
{
// python auto-loads raw or precompiled modules - don't load data here
pBase = NULL;
}
else
{
bool bResult = filesystem->ReadFile( scriptPath, NULL, bufferScript );

if ( !bResult && bWarnMissing )
{
CGWarning( 0, CON_GROUP_VSCRIPT, "Script not found (%s) \n", scriptPath.operator const char *() );
Assert( "Error running script" );
}

pBase = (const char *) bufferScript.Base();

if ( !pBase || !*pBase )
{
return NULL;
}
}

// Attach the folder to the script ID
const char *pszFilename = V_strrchr( scriptPath, '/' );
scriptPath.sprintf( "%s%s", pszRootFolderName, pszFilename );

HSCRIPT hScript = g_pScriptVM->CompileScript( pBase, scriptPath );
if ( !hScript )
{
CGWarning( 0, CON_GROUP_VSCRIPT, "FAILED to compile and execute script file named %s\n", scriptPath.operator const char *() );
Assert( "Error running script" );
}
return hScript;
}

bool VScriptRunScriptAbsolute( const char *pszScriptName, HSCRIPT hScope, bool bWarnMissing, const char *pszRootFolderName )
{
if ( !g_pScriptVM )
{
return false;
}

if ( !pszScriptName || !*pszScriptName )
{
CGWarning( 0, CON_GROUP_VSCRIPT, "Cannot run script: NULL script name\n" );
return false;
}

// Prevent infinite recursion in VM
if ( g_ScriptServerRunScriptDepth > 16 )
{
CGWarning( 0, CON_GROUP_VSCRIPT, "IncludeScript stack overflow\n" );
return false;
}

g_ScriptServerRunScriptDepth++;
HSCRIPT hScript = VScriptCompileScriptAbsolute( pszScriptName, bWarnMissing, pszRootFolderName );
bool bSuccess = false;
if ( hScript )
{
bSuccess = ( g_pScriptVM->Run( hScript, hScope ) != SCRIPT_ERROR );
if ( !bSuccess )
{
Warning( "Error running script named %s\n", pszScriptName );
Assert( "Error running script" );
}
}
g_ScriptServerRunScriptDepth--;
return bSuccess;
}
#endif


#ifdef GAME_DLL
#define IsCommandIssuedByServerAdmin() UTIL_IsCommandIssuedByServerAdmin()
#else
Expand Down Expand Up @@ -321,6 +428,74 @@ CON_COMMAND_F( script_dump_all, "Dump the state of the VM to the console", FCVAR

//-----------------------------------------------------------------------------

#ifdef MAPBASE_VSCRIPT
void RunAddonScripts()
{
char searchPaths[4096];
filesystem->GetSearchPath( "ADDON", true, searchPaths, sizeof( searchPaths ) );

for ( char *path = strtok( searchPaths, ";" ); path; path = strtok( NULL, ";" ) )
{
char folderName[MAX_PATH];
Q_FileBase( path, folderName, sizeof( folderName ) );

// mapspawn_addon
char fullpath[MAX_PATH];
Q_snprintf( fullpath, sizeof( fullpath ), "%sscripts/vscripts/mapspawn_addon", path );
Q_FixSlashes( fullpath );

VScriptRunScriptAbsolute( fullpath, NULL, false, folderName );
}
}

// UNDONE: "autorun" folder
/*
void RunAutorunScripts()
{
FileFindHandle_t fileHandle;
char szDirectory[MAX_PATH];
char szFileName[MAX_PATH];
char szPartialScriptPath[MAX_PATH];

// TODO: Scanning for VM extension would make this more efficient
Q_strncpy( szDirectory, "scripts/vscripts/autorun/*", sizeof( szDirectory ) );

const char *pszScriptFile = filesystem->FindFirst( szDirectory, &fileHandle );
while (pszScriptFile && fileHandle != FILESYSTEM_INVALID_FIND_HANDLE)
{
Q_FileBase( pszScriptFile, szFileName, sizeof( szFileName ) );
Q_snprintf( szPartialScriptPath, sizeof( szPartialScriptPath ), "autorun/%s", szFileName );
VScriptRunScript( szPartialScriptPath );

pszScriptFile = filesystem->FindNext( fileHandle );
}

// Non-shared scripts
#ifdef CLIENT_DLL
Q_strncpy( szDirectory, "scripts/vscripts/autorun/client/*", sizeof( szDirectory ) );
#else
Q_strncpy( szDirectory, "scripts/vscripts/autorun/server/*", sizeof( szDirectory ) );
#endif

pszScriptFile = filesystem->FindFirst( szDirectory, &fileHandle );
while (pszScriptFile && fileHandle != FILESYSTEM_INVALID_FIND_HANDLE)
{
Q_FileBase( pszScriptFile, szFileName, sizeof( szFileName ) );
#ifdef CLIENT_DLL
Q_snprintf( szPartialScriptPath, sizeof( szPartialScriptPath ), "autorun/client/%s", szFileName );
#else
Q_snprintf( szPartialScriptPath, sizeof( szPartialScriptPath ), "autorun/server/%s", szFileName );
#endif
VScriptRunScript( szPartialScriptPath );

pszScriptFile = filesystem->FindNext( fileHandle );
}
}
*/
#endif

//-----------------------------------------------------------------------------

static short VSCRIPT_SERVER_SAVE_RESTORE_VERSION = 2;

//-----------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions sp/src/game/shared/vscript_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ extern CBaseEntityScriptInstanceHelper g_BaseEntityScriptInstanceHelper;
#ifdef MAPBASE_VSCRIPT
void RegisterSharedScriptConstants();
void RegisterSharedScriptFunctions();

void RunAddonScripts();
#endif

#endif // VSCRIPT_SHARED_H