Skip to content

Commit

Permalink
Merge pull request #75 from fortressforever/features/lua-core-improve…
Browse files Browse the repository at this point in the history
…ments

Lua: Core improvements
  • Loading branch information
squeek502 committed Dec 8, 2014
2 parents 519808c + 5735629 commit 76b67d1
Show file tree
Hide file tree
Showing 17 changed files with 373 additions and 577 deletions.
109 changes: 0 additions & 109 deletions cl_dll/ff/clientmode_ff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ void CFFModeManager::LevelShutdown( void )
//-----------------------------------------------------------------------------
ClientModeFFNormal::ClientModeFFNormal()
{
Q_strcpy(m_szValidationFile, "validationkeys.txt");
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -147,11 +146,6 @@ void ClientModeFFNormal::PostRenderVGui()
//-----------------------------------------------------------------------------
void ClientModeFFNormal::Init( void )
{
m_servercriptCRC = 0;
m_serverScriptValid = false;

usermessages->HookMessage("FFScriptCRC", &ClientModeFFNormal::FFScriptCRC_MsgHandler);

gameeventmanager->AddListener( this, "ff_restartround", false );

ClientModeShared::Init();
Expand Down Expand Up @@ -185,106 +179,3 @@ void ClientModeFFNormal::FireGameEvent( IGameEvent *pEvent )

BaseClass::FireGameEvent( pEvent );
}

//-----------------------------------------------------------------------------
void ClientModeFFNormal::SetNextValidationFilePath(const char* szFilePath)
{
Q_strncpy(m_szValidationFile, szFilePath, sizeof(m_szValidationFile));
}

//-----------------------------------------------------------------------------
void ClientModeFFNormal::FFScriptCRC_MsgHandler(bf_read& msg)
{
ClientModeFFNormal* pClientMode = GetClientModeFFNormal();

unsigned long crc = (unsigned long)msg.ReadLong();
pClientMode->m_servercriptCRC = crc;

// lookup the valid crc and check against it
char szLevelName[256];
Q_strcpy(szLevelName, engine->GetLevelName() + 5); // Skip the "maps/" part
szLevelName[(int)strlen(szLevelName) - 4] = '\0'; // Skip the ".bsp" part

char szDescription[256];
bool bValidated = ValidateLevel(pClientMode->m_szValidationFile,
szLevelName,
crc,
szDescription,
256);

Msg("\n");
Msg("Server Script Validation\n");
Msg("Level Name: %s\n", szLevelName);
Msg("Checksum: 0x%x\n", crc);

if(bValidated)
{
Msg("Validated: Yes\n");
Msg("Description: %s\n", szDescription);
}
else
{
Msg("Validated: No\n");
}

Msg("\n");

// reset the validation filepath
Q_strcpy(pClientMode->m_szValidationFile, "validationkeys.txt");
}

//-----------------------------------------------------------------------------
bool ClientModeFFNormal::ValidateLevel(const char* szValidateFilePath,
const char* szLevelName,
CRC32_t checksum,
char* szDescription,
int descMaxLength)
{
// load validation file
KeyValues* pKvRoot = new KeyValues("Levels");
bool bRes = pKvRoot->LoadFromFile(::filesystem, szValidateFilePath);

if(!bRes)
{
Warning("Error loading validation keys file '%s'\n", szValidateFilePath);
pKvRoot->deleteThis();
return false;
}

bool bValidate = false;

// find section with level name
KeyValues* pKvLevel = pKvRoot->FindKey(szLevelName);
if(pKvLevel)
{
// find section with the checksum
char szChecksum[32];
Q_snprintf(szChecksum, sizeof(szChecksum), "0x%x", checksum);

KeyValues* pKvEntry = pKvLevel->FindKey(szChecksum);
if(pKvEntry)
{
// copy the description
const char* szDesc = pKvEntry->GetString("description", "");
Q_strncpy(szDescription, szDesc, descMaxLength);
bValidate = true;
}
}

pKvRoot->deleteThis();
return bValidate;
}

//-----------------------------------------------------------------------------
CON_COMMAND(ff_validate, "Requests a validation of the crc checksum of the server's current level scripts." )
{
if(engine->Cmd_Argc() > 1)
{
const char* szFilePath = engine->Cmd_Argv(1);

ClientModeFFNormal* pClientMode = GetClientModeFFNormal();
pClientMode->SetNextValidationFilePath(szFilePath);
}

engine->ClientCmd("ff_scriptcrc");
}
18 changes: 0 additions & 18 deletions cl_dll/ff/clientmode_ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,6 @@ DECLARE_CLASS( ClientModeFFNormal, ClientModeShared );
virtual void Init( void );
virtual void FireGameEvent( IGameEvent *pEvent );

void SetNextValidationFilePath(const char* szFilePath);

public:
static bool ValidateLevel(const char* szValidateFilePath,
const char* szLevelName,
CRC32_t checksum,
char* szDescription,
int descMaxLength);
static void FFScriptCRC_MsgHandler(bf_read& msg);

private:
// void UpdateSpectatorMode( void );

private:
bool m_serverScriptValid;
CRC32_t m_servercriptCRC;
char m_szValidationFile[1024];

};


Expand Down
10 changes: 0 additions & 10 deletions dlls/ff/ff_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,6 @@ extern CBaseEntity *FindPickerEntity( CBasePlayer *pPlayer );

extern bool g_fGameOver;

void SendScriptChecksumToClient(CBasePlayer* pPlayer, unsigned long scriptCRC)
{
CSingleUserRecipientFilter filter(pPlayer);
filter.MakeReliable();

UserMessageBegin(filter, "FFScriptCRC");
WRITE_LONG(scriptCRC);
MessageEnd();
}

void FinishClientPutInServer( CFFPlayer *pPlayer )
{
pPlayer->m_flNextSpawnDelay = 0;
Expand Down
4 changes: 2 additions & 2 deletions dlls/ff/ff_luacontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ bool CFFLuaSC::CallFunction(CBaseEntity* pEntity, const char* szFunctionName, co
lua_State* L = _scriptman.GetLuaState();

// If there is no active script then allow the ents to always go
if(!_scriptman.ScriptExists() || !L)
if(!L)
return false;

// set lua's reference to the calling entity
Expand Down Expand Up @@ -259,7 +259,7 @@ bool CFFLuaSC::CallFunction(CBaseEntity* pEntity, const char* szFunctionName, co
if(lua_pcall(L, pEntity||szTargetEntName ? nParams + 1 : nParams, 1, 0) != 0)
{
const char* szErrorMsg = lua_tostring(L, -1);
Msg("[SCRIPT] Error calling %s (%s) ent: %s\n",
_scriptman.LuaWarning("Error calling %s (%s) ent: %s\n",
szFunctionName,
szErrorMsg,
pEntity ? STRING(pEntity->GetEntityName()) : "NULL");
Expand Down
23 changes: 11 additions & 12 deletions dlls/ff/ff_lualib_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C"
}

#include "luabind/luabind.hpp"
#include "luabind/operator.hpp"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
Expand All @@ -30,13 +31,16 @@ extern "C"
using namespace luabind;

//---------------------------------------------------------------------------
// TODO: evaluate these functions off lua's BaseEntity types
namespace FFLib
/// tostring implemenation for CBaseEntity
std::ostream& operator<<(std::ostream& stream, const CBaseEntity& entity)
{
bool IsDispenser(CBaseEntity* pEntity);
bool IsGrenade(CBaseEntity* pEntity);
bool IsSentrygun(CBaseEntity* pEntity);
bool IsDetpack(CBaseEntity* pEntity);
stream << const_cast<CBaseEntity&>(entity).GetClassname() << ":";
const char *szEntityName = const_cast<CBaseEntity&>(entity).GetName();
if (szEntityName[0])
{
stream << szEntityName << ":";
}
return stream << entity.entindex();
}

//---------------------------------------------------------------------------
Expand All @@ -48,6 +52,7 @@ void CFFLuaLib::InitBase(lua_State* L)
[
// CBaseEntity
class_<CBaseEntity>("BaseEntity")
.def(tostring(self))
.def("EmitSound", &CBaseEntity::PlaySound)
.def("StopSound", (void(CBaseEntity::*)(const char*))&CBaseEntity::StopSound)
.def("GetClassName", &CBaseEntity::GetClassname)
Expand All @@ -58,12 +63,6 @@ void CFFLuaLib::InitBase(lua_State* L)
.def("GetVelocity", &CBaseEntity::GetAbsVelocity)
.def("SetVelocity", &CBaseEntity::SetAbsVelocity)
.def("GetOwner", &CBaseEntity::GetOwnerEntity)
// Use the global stuff for these
//.def("IsDispenser", &FFLib::IsDispenser)
//.def("IsGrenade", &FFLib::IsGrenade)
//.def("IsPlayer", &CBaseEntity::IsPlayer)
//.def("IsSentryGun", &FFLib::IsSentrygun)
//.def("IsDetpack", &FFLib::IsDetpack)
.def("SetModel", (void(CBaseEntity::*)(const char*))&CBaseEntity::SetModel)
.def("SetModel", (void(CBaseEntity::*)(const char*, int))&CBaseEntity::SetModel)
.def("SetSkin", &CBaseEntity::SetSkin)
Expand Down
2 changes: 1 addition & 1 deletion dlls/ff/ff_lualib_buildables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void CFFLuaLib::InitBuildables(lua_State* L)
module(L)
[
// Buildable base
class_<CFFBuildableObject>("BaseBuildable")
class_<CFFBuildableObject, CBaseEntity>("BaseBuildable")
.def("GetTeamId", &CFFBuildableObject::GetTeamNumber)
.def("GetOwner", &CFFBuildableObject::GetOwnerPlayer)
.def("GetTeam", &CFFBuildableObject::GetOwnerTeam),
Expand Down
14 changes: 7 additions & 7 deletions dlls/ff/ff_lualib_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace FFLib

if ( f == FILESYSTEM_INVALID_HANDLE )
{
Msg("[SCRIPT] Error saving data (File \"%s\" could not be opened for writing)\n",
_scriptman.LuaWarning("Error saving data (File \"%s\" could not be opened for writing)\n",
filename?filename:"NULL");
return false;
}
Expand Down Expand Up @@ -220,7 +220,7 @@ namespace FFLib

if ( f == FILESYSTEM_INVALID_HANDLE )
{
Msg("[SCRIPT] Error loading data (File \"%s\" could not be opened for writing)\n",
_scriptman.LuaWarning("Error loading data (File \"%s\" could not be opened for writing)\n",
filename?filename:"NULL" );
return luabind::adl::object();
}
Expand Down Expand Up @@ -300,7 +300,7 @@ namespace FFLib
break;
}
default:
Msg("[SCRIPT] Error loading data (Data format is not recognized)\n");
_scriptman.LuaWarning("Error loading data (Data format is not recognized)\n");
return false;
break;
}
Expand Down Expand Up @@ -353,7 +353,7 @@ namespace FFLib
// if the validated suffix string is blank
if (!validSuffix[0])
{
Msg("[SCRIPT] Error saving data to %s_%s.luadat (Suffix must contain alphanumeric characters only)\n",
_scriptman.LuaWarning("Error saving data to %s_%s.luadat (Suffix must contain alphanumeric characters only)\n",
STRING(gpGlobals->mapname), suffix);
return false;
}
Expand All @@ -373,7 +373,7 @@ namespace FFLib
// if the validated suffix string is blank
if (!validSuffix[0])
{
Msg("[SCRIPT] Error loading data from %s_%s.luadat (Suffix must contain alphanumeric characters only)\n",
_scriptman.LuaWarning("Error loading data from %s_%s.luadat (Suffix must contain alphanumeric characters only)\n",
STRING(gpGlobals->mapname), suffix);
return luabind::adl::object();
}
Expand All @@ -393,7 +393,7 @@ namespace FFLib
// if the validated suffix string is blank
if (!validSuffix[0])
{
Msg("[SCRIPT] Error saving data to global_%s.luadat (Suffix must contain alphanumeric characters only)\n",
_scriptman.LuaWarning("Error saving data to global_%s.luadat (Suffix must contain alphanumeric characters only)\n",
suffix);
return false;
}
Expand All @@ -413,7 +413,7 @@ namespace FFLib
// if the validated suffix string is blank
if (!validSuffix[0])
{
Msg("[SCRIPT] Error loading data from global_%s.luadat (Suffix must contain alphanumeric characters only)\n",
_scriptman.LuaWarning("Error loading data from global_%s.luadat (Suffix must contain alphanumeric characters only)\n",
suffix);
return luabind::adl::object();
}
Expand Down
67 changes: 9 additions & 58 deletions dlls/ff/ff_lualib_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,69 +501,20 @@ namespace FFLib

void ConsoleToAll(const char* szMessage)
{
DevMsg( "Debug: " );
DevMsg( szMessage );
DevMsg( "\n" );

Msg( szMessage );
Msg( "\n" );
}

void IncludeScript(const char* script)
luabind::adl::object IncludeScript(const char* script)
{
char realscript[255];

// make sure it's a valid filename (alphanum only)
bool good = true;
for (unsigned int i=0; i<strlen(script); i++)
{
if (script[i]=='/' || script[i]=='\\') continue;
if (script[i]>='a' && script[i]<='z') continue;
if (script[i]>='A' && script[i]<='Z') continue;
if (script[i]>='0' && script[i]<='9') continue;
if (script[i]=='_') continue;

good = false;
}

// if it's a good filename, then go ahead and include it
if (good)
{
// Let's use a little more control
/*
strcpy(realscript, "maps/includes/" );
strcat(realscript, script);
strcat(realscript, ".lua");
*/

Q_snprintf( realscript, sizeof( realscript ), "maps/includes/%s.lua", script );

//////////////////////////////////////////////////////////////////////////
// Try a precache, rumor has it this will cause the engine to send the lua files to clients
if(PRECACHE_LUA_FILES)
{
V_FixSlashes(realscript);
if(filesystem->FileExists(realscript))
{
Util_AddDownload(realscript);

if(!engine->IsGenericPrecached(realscript))
engine->PrecacheGeneric(realscript, true);
}
}
//////////////////////////////////////////////////////////////////////////

if( !CFFScriptManager::LoadFile( _scriptman.GetLuaState(), realscript ) )
{
// Try looking in the maps directory
Q_snprintf( realscript, sizeof( realscript ), "maps/%s.lua", script );
CFFScriptManager::LoadFile( _scriptman.GetLuaState(), realscript );
}
}
else
{
Msg("[SCRIPT] Warning: Invalid filename: %s\n", script);
}
lua_State *L = _scriptman.GetLuaState();
lua_getglobal(L, "require");
lua_pushvalue(L, -1); /* function to be called */
lua_pushstring(L, script);
lua_call(L, 1, 1);
luabind::adl::object objReturnValue = luabind::adl::object(luabind::from_stack(L, -1));
lua_pop(L, 2); /* pop result and function */
return objReturnValue;
}

void RemoveEntity(CBaseEntity* pEntity)
Expand Down
Loading

0 comments on commit 76b67d1

Please sign in to comment.