bool luasrc_SetGamemode (const char *gamemode) { lua_getglobal(L, "gamemode"); if (lua_istable(L, -1)) { lua_getfield(L, -1, "get"); if (lua_isfunction(L, -1)) { lua_remove(L, -2); lua_pushstring(L, gamemode); luasrc_pcall(L, 1, 1, 0); lua_setglobal(L, "_GAMEMODE"); Q_snprintf( contentSearchPath, sizeof( contentSearchPath ), "gamemodes\\%s\\content", gamemode ); filesystem->AddSearchPath( contentSearchPath, "MOD" ); char loadPath[MAX_PATH]; Q_snprintf( loadPath, sizeof( loadPath ), "%s\\", contentSearchPath ); luasrc_LoadWeapons( loadPath ); luasrc_LoadEntities( loadPath ); // luasrc_LoadEffects( loadPath ); BEGIN_LUA_CALL_HOOK("Initialize"); END_LUA_CALL_HOOK(0,0); return true; } else { lua_pop(L, 2); Warning( "ERROR: Failed to set gamemode!\n" ); return false; } } else { lua_pop(L, 1); Warning( "ERROR: Failed to load gamemode module!\n" ); return false; } }
bool luasrc_LoadGamemode (const char *gamemode) { lua_newtable(L); lua_pushstring(L, "__folder"); char gamemodepath[MAX_PATH]; Q_snprintf( gamemodepath, sizeof( gamemodepath ), "gamemodes\\%s", gamemode ); lua_pushstring(L, gamemodepath); lua_settable(L, -3); lua_setglobal(L, "GM"); char filename[MAX_PATH]; char fullpath[MAX_PATH]; #ifdef CLIENT_DLL Q_snprintf( filename, sizeof( filename ), "%s\\gamemode\\cl_init.lua", gamemodepath ); #else Q_snprintf( filename, sizeof( filename ), "%s\\gamemode\\init.lua", gamemodepath ); #endif if ( filesystem->FileExists( filename, "MOD" ) ) { filesystem->RelativePathToFullPath( filename, "MOD", fullpath, sizeof( fullpath ) ); if (luasrc_dofile(L, fullpath) == 0) { lua_getglobal(L, "gamemode"); lua_getfield(L, -1, "register"); lua_remove(L, -2); lua_getglobal(L, "GM"); lua_pushstring(L, gamemode); lua_getfield(L, -2, "__base"); if (lua_isnoneornil(L, -1) && Q_strcmp(gamemode, LUA_BASE_GAMEMODE) != 0) { lua_pop(L, 1); lua_pushstring(L, LUA_BASE_GAMEMODE); } luasrc_pcall(L, 3, 0, 0); lua_pushnil(L); lua_setglobal(L, "GM"); return true; } else { lua_pushnil(L); lua_setglobal(L, "GM"); Warning( "ERROR: Attempted to load an invalid gamemode!\n" ); return false; } } else { lua_pushnil(L); lua_setglobal(L, "GM"); Warning( "ERROR: Attempted to load an invalid gamemode!\n" ); return false; } }
void CBaseScripted::LoadScriptedEntity( void ) { lua_getglobal( L, "entity" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "get" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_pushstring( L, GetClassname() ); luasrc_pcall( L, 1, 1, 0 ); } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } }
void CBaseScripted::InitScriptedEntity( void ) { #if defined ( LUA_SDK ) #if 0 #ifndef CLIENT_DLL // Let the instance reinitialize itself for the client. if ( m_nTableReference != LUA_NOREF ) return; #endif #endif SetThink( &CBaseScripted::Think ); #ifdef CLIENT_DLL SetNextClientThink( gpGlobals->curtime ); #endif SetNextThink( gpGlobals->curtime ); SetTouch( &CBaseScripted::Touch ); char className[ 255 ]; #if defined ( CLIENT_DLL ) if ( strlen( GetScriptedClassname() ) > 0 ) Q_strncpy( className, GetScriptedClassname(), sizeof( className ) ); else Q_strncpy( className, GetClassname(), sizeof( className ) ); #else Q_strncpy( m_iScriptedClassname.GetForModify(), GetClassname(), sizeof( className ) ); Q_strncpy( className, GetClassname(), sizeof( className ) ); #endif Q_strlower( className ); SetClassname( className ); if ( m_nTableReference == LUA_NOREF ) { LoadScriptedEntity(); m_nTableReference = luaL_ref( L, LUA_REGISTRYINDEX ); } else { lua_getglobal( L, "table" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "merge" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_getref( L, m_nTableReference ); LoadScriptedEntity(); luasrc_pcall( L, 2, 0, 0 ); } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } } BEGIN_LUA_CALL_ENTITY_METHOD( "Initialize" ); END_LUA_CALL_ENTITY_METHOD( 0, 0 ); #endif }
void CBaseScriptedTrigger::InitScriptedTrigger( void ) { #if defined ( LUA_SDK ) #if 0 // Let the instance reinitialize itself for the client. if ( m_nTableReference != LUA_NOREF ) return; #endif SetThink( &CBaseScriptedTrigger::Think ); SetNextThink( gpGlobals->curtime ); SetTouch( &CBaseScriptedTrigger::Touch ); char className[ 255 ]; Q_strncpy( className, GetClassname(), sizeof( className ) ); Q_strlower( className ); SetClassname( className ); lua_getglobal( L, "entity" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "get" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_pushstring( L, className ); luasrc_pcall( L, 1, 1, 0 ); } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } if ( m_nTableReference == LUA_NOREF ) { LoadScriptedTrigger(); m_nTableReference = luaL_ref( L, LUA_REGISTRYINDEX ); } else { lua_getglobal( L, "table" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "merge" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_getref( L, m_nTableReference ); LoadScriptedTrigger(); luasrc_pcall( L, 2, 0, 0 ); } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } } BEGIN_LUA_CALL_TRIGGER_METHOD( "Initialize" ); END_LUA_CALL_TRIGGER_METHOD( 0, 0 ); #endif }
void CHL2MPScriptedWeapon::InitScriptedWeapon( void ) { #if defined ( LUA_SDK ) #ifndef CLIENT_DLL // Let the instance reinitialize itself for the client. if ( m_nTableReference != LUA_NOREF ) return; #endif char className[ MAX_WEAPON_STRING ]; #if defined ( CLIENT_DLL ) if ( strlen( GetScriptedClassname() ) > 0 ) Q_strncpy( className, GetScriptedClassname(), sizeof( className ) ); else Q_strncpy( className, GetClassname(), sizeof( className ) ); #else Q_strncpy( m_iScriptedClassname.GetForModify(), GetClassname(), sizeof( className ) ); Q_strncpy( className, GetClassname(), sizeof( className ) ); #endif Q_strlower( className ); // Andrew; This redundancy is pretty annoying. // Classname Q_strncpy( m_pLuaWeaponInfo->szClassName, className, MAX_WEAPON_STRING ); SetClassname( className ); lua_getglobal( L, "weapon" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "get" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_pushstring( L, className ); luasrc_pcall( L, 1, 1, 0 ); } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } m_nTableReference = luaL_ref( L, LUA_REGISTRYINDEX ); #ifndef CLIENT_DLL m_pLuaWeaponInfo->bParsedScript = true; #endif // Printable name lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "printname" ); lua_remove( L, -2 ); if ( lua_isstring( L, -1 ) ) { Q_strncpy( m_pLuaWeaponInfo->szPrintName, lua_tostring( L, -1 ), MAX_WEAPON_STRING ); } else { Q_strncpy( m_pLuaWeaponInfo->szPrintName, WEAPON_PRINTNAME_MISSING, MAX_WEAPON_STRING ); } lua_pop( L, 1 ); // View model & world model lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "viewmodel" ); lua_remove( L, -2 ); if ( lua_isstring( L, -1 ) ) { Q_strncpy( m_pLuaWeaponInfo->szViewModel, lua_tostring( L, -1 ), MAX_WEAPON_STRING ); } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "playermodel" ); lua_remove( L, -2 ); if ( lua_isstring( L, -1 ) ) { Q_strncpy( m_pLuaWeaponInfo->szWorldModel, lua_tostring( L, -1 ), MAX_WEAPON_STRING ); } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "anim_prefix" ); lua_remove( L, -2 ); if ( lua_isstring( L, -1 ) ) { Q_strncpy( m_pLuaWeaponInfo->szAnimationPrefix, lua_tostring( L, -1 ), MAX_WEAPON_PREFIX ); } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "bucket" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iSlot = lua_tonumber( L, -1 ); } else { m_pLuaWeaponInfo->iSlot = 0; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "bucket_position" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iPosition = lua_tonumber( L, -1 ); } else { m_pLuaWeaponInfo->iPosition = 0; } lua_pop( L, 1 ); // Use the console (X360) buckets if hud_fastswitch is set to 2. #ifdef CLIENT_DLL if ( hud_fastswitch.GetInt() == 2 ) #else if ( IsX360() ) #endif { lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "bucket_360" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iSlot = lua_tonumber( L, -1 ); } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "bucket_position_360" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iPosition = lua_tonumber( L, -1 ); } lua_pop( L, 1 ); } lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "clip_size" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iMaxClip1 = lua_tonumber( L, -1 ); // Max primary clips gun can hold (assume they don't use clips by default) } else { m_pLuaWeaponInfo->iMaxClip1 = WEAPON_NOCLIP; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "clip2_size" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iMaxClip2 = lua_tonumber( L, -1 ); // Max secondary clips gun can hold (assume they don't use clips by default) } else { m_pLuaWeaponInfo->iMaxClip2 = WEAPON_NOCLIP; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "default_clip" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iDefaultClip1 = lua_tonumber( L, -1 ); // amount of primary ammo placed in the primary clip when it's picked up } else { m_pLuaWeaponInfo->iDefaultClip1 = m_pLuaWeaponInfo->iMaxClip1; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "default_clip2" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iDefaultClip2 = lua_tonumber( L, -1 ); // amount of secondary ammo placed in the secondary clip when it's picked up } else { m_pLuaWeaponInfo->iDefaultClip2 = m_pLuaWeaponInfo->iMaxClip2; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "weight" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iWeight = lua_tonumber( L, -1 ); } else { m_pLuaWeaponInfo->iWeight = 0; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "rumble" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->iWeight = lua_tonumber( L, -1 ); } else { m_pLuaWeaponInfo->iWeight = -1; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "showusagehint" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->bShowUsageHint = (int)lua_tointeger( L, -1 ) != 0 ? true : false; } else { m_pLuaWeaponInfo->bShowUsageHint = false; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "autoswitchto" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->bAutoSwitchTo = (int)lua_tointeger( L, -1 ) != 0 ? true : false; } else { m_pLuaWeaponInfo->bAutoSwitchTo = true; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "autoswitchfrom" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->bAutoSwitchFrom = (int)lua_tointeger( L, -1 ) != 0 ? true : false; } else { m_pLuaWeaponInfo->bAutoSwitchFrom = true; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "BuiltRightHanded" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->m_bBuiltRightHanded = (int)lua_tointeger( L, -1 ) != 0 ? true : false; } else { m_pLuaWeaponInfo->m_bBuiltRightHanded = true; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "AllowFlipping" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->m_bAllowFlipping = (int)lua_tointeger( L, -1 ) != 0 ? true : false; } else { m_pLuaWeaponInfo->m_bAllowFlipping = true; } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "MeleeWeapon" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->m_bMeleeWeapon = (int)lua_tointeger( L, -1 ) != 0 ? true : false; } else { m_pLuaWeaponInfo->m_bMeleeWeapon = false; } lua_pop( L, 1 ); // Primary ammo used lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "primary_ammo" ); lua_remove( L, -2 ); if ( lua_isstring( L, -1 ) ) { const char *pAmmo = lua_tostring( L, -1 ); if ( strcmp("None", pAmmo) == 0 ) Q_strncpy( m_pLuaWeaponInfo->szAmmo1, "", sizeof( m_pLuaWeaponInfo->szAmmo1 ) ); else Q_strncpy( m_pLuaWeaponInfo->szAmmo1, pAmmo, sizeof( m_pLuaWeaponInfo->szAmmo1 ) ); m_pLuaWeaponInfo->iAmmoType = GetAmmoDef()->Index( m_pLuaWeaponInfo->szAmmo1 ); } lua_pop( L, 1 ); // Secondary ammo used lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "secondary_ammo" ); lua_remove( L, -2 ); if ( lua_isstring( L, -1 ) ) { const char *pAmmo = lua_tostring( L, -1 ); if ( strcmp("None", pAmmo) == 0) Q_strncpy( m_pLuaWeaponInfo->szAmmo2, "", sizeof( m_pLuaWeaponInfo->szAmmo2 ) ); else Q_strncpy( m_pLuaWeaponInfo->szAmmo2, pAmmo, sizeof( m_pLuaWeaponInfo->szAmmo2 ) ); m_pLuaWeaponInfo->iAmmo2Type = GetAmmoDef()->Index( m_pLuaWeaponInfo->szAmmo2 ); } lua_pop( L, 1 ); // Now read the weapon sounds memset( m_pLuaWeaponInfo->aShootSounds, 0, sizeof( m_pLuaWeaponInfo->aShootSounds ) ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "SoundData" ); lua_remove( L, -2 ); if ( lua_istable( L, -1 ) ) { for ( int i = EMPTY; i < NUM_SHOOT_SOUND_TYPES; i++ ) { lua_getfield( L, -1, pWeaponSoundCategories[i] ); if ( lua_isstring( L, -1 ) ) { const char *soundname = lua_tostring( L, -1 ); if ( soundname && soundname[0] ) { Q_strncpy( m_pLuaWeaponInfo->aShootSounds[i], soundname, MAX_WEAPON_STRING ); } } lua_pop( L, 1 ); } } lua_pop( L, 1 ); lua_getref( L, m_nTableReference ); lua_getfield( L, -1, "damage" ); lua_remove( L, -2 ); if ( lua_isnumber( L, -1 ) ) { m_pLuaWeaponInfo->m_iPlayerDamage = (int)lua_tointeger( L, -1 ); } lua_pop( L, 1 ); BEGIN_LUA_CALL_WEAPON_METHOD( "Initialize" ); END_LUA_CALL_WEAPON_METHOD( 0, 0 ); #endif }
void luasrc_LoadWeapons (const char *path) { FileFindHandle_t fh; if ( !path ) { path = ""; } char root[ MAX_PATH ] = { 0 }; char filename[ MAX_PATH ] = { 0 }; char fullpath[ MAX_PATH ] = { 0 }; char className[ MAX_WEAPON_STRING ] = { 0 }; Q_snprintf( root, sizeof( root ), "%s" LUA_PATH_WEAPONS "\\*", path ); char const *fn = g_pFullFileSystem->FindFirstEx( root, "MOD", &fh ); while ( fn ) { Q_strcpy( className, fn ); Q_strlower( className ); if ( fn[0] != '.' ) { if ( g_pFullFileSystem->FindIsDirectory( fh ) ) { #ifdef CLIENT_DLL Q_snprintf( filename, sizeof( filename ), "%s" LUA_PATH_WEAPONS "\\%s\\cl_init.lua", path, className ); #else Q_snprintf( filename, sizeof( filename ), "%s" LUA_PATH_WEAPONS "\\%s\\init.lua", path, className ); #endif if ( filesystem->FileExists( filename, "MOD" ) ) { filesystem->RelativePathToFullPath( filename, "MOD", fullpath, sizeof( fullpath ) ); lua_newtable( L ); char entDir[ MAX_PATH ]; Q_snprintf( entDir, sizeof( entDir ), "weapons\\%s", className ); lua_pushstring( L, entDir ); lua_setfield( L, -2, "__folder" ); lua_pushstring( L, LUA_BASE_WEAPON ); lua_setfield( L, -2, "__base" ); lua_setglobal( L, "SWEP" ); if ( luasrc_dofile( L, fullpath ) == 0 ) { lua_getglobal( L, "weapon" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "register" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_getglobal( L, "SWEP" ); lua_pushstring( L, className ); luasrc_pcall( L, 2, 0, 0 ); RegisterScriptedWeapon( className ); } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } } lua_pushnil( L ); lua_setglobal( L, "SWEP" ); } } } fn = g_pFullFileSystem->FindNext( fh ); } g_pFullFileSystem->FindClose( fh ); }
void luasrc_LoadEntities (const char *path) { FileFindHandle_t fh; if ( !path ) { path = ""; } char root[ MAX_PATH ] = { 0 }; char filename[ MAX_PATH ] = { 0 }; char fullpath[ MAX_PATH ] = { 0 }; char className[ 255 ] = { 0 }; Q_snprintf( root, sizeof( root ), "%s" LUA_PATH_ENTITIES "\\*", path ); char const *fn = g_pFullFileSystem->FindFirstEx( root, "MOD", &fh ); while ( fn ) { Q_strcpy( className, fn ); Q_strlower( className ); if ( fn[0] != '.' ) { if ( g_pFullFileSystem->FindIsDirectory( fh ) ) { #ifdef CLIENT_DLL Q_snprintf( filename, sizeof( filename ), "%s" LUA_PATH_ENTITIES "\\%s\\cl_init.lua", path, className ); #else Q_snprintf( filename, sizeof( filename ), "%s" LUA_PATH_ENTITIES "\\%s\\init.lua", path, className ); #endif if ( filesystem->FileExists( filename, "MOD" ) ) { filesystem->RelativePathToFullPath( filename, "MOD", fullpath, sizeof( fullpath ) ); lua_newtable( L ); char entDir[ MAX_PATH ]; Q_snprintf( entDir, sizeof( entDir ), "entities\\%s", className ); lua_pushstring( L, entDir ); lua_setfield( L, -2, "__folder" ); lua_pushstring( L, LUA_BASE_ENTITY_CLASS ); lua_setfield( L, -2, "__base" ); lua_pushstring( L, LUA_BASE_ENTITY_FACTORY ); lua_setfield( L, -2, "__factory" ); lua_setglobal( L, "ENT" ); if ( luasrc_dofile( L, fullpath ) == 0 ) { lua_getglobal( L, "entity" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "register" ); if ( lua_isfunction( L, -1 ) ) { lua_remove( L, -2 ); lua_getglobal( L, "ENT" ); lua_pushstring( L, className ); luasrc_pcall( L, 2, 0, 0 ); lua_getglobal( L, "ENT" ); if ( lua_istable( L, -1 ) ) { lua_getfield( L, -1, "__factory" ); if ( lua_isstring( L, -1 ) ) { const char *pszClassname = lua_tostring( L, -1 ); if (Q_strcmp(pszClassname, "CBaseAnimating") == 0) RegisterScriptedEntity( className ); #ifndef CLIENT_DLL else if (Q_strcmp(pszClassname, "CBaseTrigger") == 0) RegisterScriptedTrigger( className ); #endif } lua_pop( L, 2 ); } else { lua_pop( L, 1 ); } } else { lua_pop( L, 2 ); } } else { lua_pop( L, 1 ); } } lua_pushnil( L ); lua_setglobal( L, "ENT" ); } } } fn = g_pFullFileSystem->FindNext( fh ); } g_pFullFileSystem->FindClose( fh ); }