ff_scriptman rename to ff_scriptman_shared

Conflicts:
	mp/src/game/shared/ff/ff_scriptman_shared.cpp
This commit is contained in:
Dexter 2013-10-28 02:56:24 +00:00 committed by squeek
parent 13b0113850
commit 1ef1520ada
2 changed files with 345 additions and 345 deletions

View file

@ -1,346 +1,346 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// includes // includes
#include "cbase.h" #include "cbase.h"
#include "ff_scriptman.h" #include "ff_scriptman_shared.h"
//#include "ff_entity_system.h" //#include "ff_entity_system.h"
//#include "ff_luacontext.h" //#include "ff_luacontext.h"
//#include "ff_lualib.h" //#include "ff_lualib.h"
//#include "ff_utils.h" //#include "ff_utils.h"
//#include "ff_item_flag.h" //#include "ff_item_flag.h"
//#include "triggers.h" //#include "triggers.h"
// engine // engine
#include "filesystem.h" #include "filesystem.h"
// TODO: check if these things are still needed for lua/luabind // TODO: check if these things are still needed for lua/luabind
//#undef MINMAX_H //#undef MINMAX_H
//#undef min //#undef min
//#undef max //#undef max
// //
// luabind // luabind
#include "lua.hpp" #include "lua.hpp"
#include "luabind/luabind.hpp" #include "luabind/luabind.hpp"
//#include "luabind/object.hpp" //#include "luabind/object.hpp"
//#include "luabind/iterator_policy.hpp" //#include "luabind/iterator_policy.hpp"
// memdbgon must be the last include file in a .cpp file!!! // memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h" #include "tier0/memdbgon.h"
#if FF_DLL #if FF_DLL
// custom game modes made so damn easy // custom game modes made so damn easy
ConVar sv_mapluasuffix( "sv_mapluasuffix", "0", FCVAR_ARCHIVE, "Have a custom lua file (game mode) loaded when the map loads. If this suffix string is set, maps\\mapname__suffix__.lua (if it exists) is used instead of maps\\mapname.lua. To reset this cvar, make it 0."); ConVar sv_mapluasuffix( "sv_mapluasuffix", "0", FCVAR_ARCHIVE, "Have a custom lua file (game mode) loaded when the map loads. If this suffix string is set, maps\\mapname__suffix__.lua (if it exists) is used instead of maps\\mapname.lua. To reset this cvar, make it 0.");
ConVar sv_luaglobalscript( "sv_globalluascript", "0", FCVAR_ARCHIVE, "Load a custom lua file globally after map scripts. Will overwrite map script. Will be loaded from maps\\globalscripts. To disable, set to 0."); ConVar sv_luaglobalscript( "sv_globalluascript", "0", FCVAR_ARCHIVE, "Load a custom lua file globally after map scripts. Will overwrite map script. Will be loaded from maps\\globalscripts. To disable, set to 0.");
#endif #endif
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
using namespace luabind; using namespace luabind;
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// globals // globals
CFFScriptManager _scriptman; CFFScriptManager _scriptman;
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
CFFScriptManager::CFFScriptManager() CFFScriptManager::CFFScriptManager()
: L(NULL) : L(NULL)
, m_isLoading(false) , m_isLoading(false)
, m_scriptCRC(0) , m_scriptCRC(0)
, m_ScriptExists(false) , m_ScriptExists(false)
{ {
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
CFFScriptManager::~CFFScriptManager() CFFScriptManager::~CFFScriptManager()
{ {
Shutdown(); Shutdown();
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void CFFScriptManager::Shutdown() void CFFScriptManager::Shutdown()
{ {
m_ScriptExists = false; m_ScriptExists = false;
// shtutdown VM // shtutdown VM
if(L) if(L)
{ {
lua_close(L); lua_close(L);
L = NULL; L = NULL;
} }
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void CFFScriptManager::Init() void CFFScriptManager::Init()
{ {
// shutdown VM if already running // shutdown VM if already running
if(L) if(L)
{ {
lua_close(L); lua_close(L);
L = NULL; L = NULL;
} }
// initialize VM // initialize VM
Msg("[SCRIPT] Attempting to start up the entity system...\n"); Msg("[SCRIPT] Attempting to start up the entity system...\n");
L = lua_open(); L = lua_open();
// no need to continue if VM failed to initialize // no need to continue if VM failed to initialize
if(!L) if(!L)
{ {
Msg("[SCRIPT] Unable to initialize Lua VM.\n"); Msg("[SCRIPT] Unable to initialize Lua VM.\n");
return; return;
} }
// load base libraries // load base libraries
luaopen_base(L); luaopen_base(L);
luaopen_table(L); luaopen_table(L);
luaopen_string(L); luaopen_string(L);
luaopen_math(L); luaopen_math(L);
// initialize luabind // initialize luabind
luabind::open(L); luabind::open(L);
// initialize game-specific library // initialize game-specific library
//FF_TODO: CFFLuaLib::Init(L); //FF_TODO: CFFLuaLib::Init(L);
Msg("[SCRIPT] Entity system initialization successful.\n"); Msg("[SCRIPT] Entity system initialization successful.\n");
} }
#ifdef FF_DLL #ifdef FF_DLL
void CFFScriptManager::LevelInit(const char* szMapName) void CFFScriptManager::LevelInit(const char* szMapName)
{ {
const char* default_luafile = "maps/default.lua"; const char* default_luafile = "maps/default.lua";
//FF_TODO: VPROF_BUDGET("CFFScriptManager::LevelInit", VPROF_BUDGETGROUP_FF_LUA); //FF_TODO: VPROF_BUDGET("CFFScriptManager::LevelInit", VPROF_BUDGETGROUP_FF_LUA);
if(!szMapName) if(!szMapName)
return; return;
//FF_TODO: g_Disable_Timelimit = false; //FF_TODO: g_Disable_Timelimit = false;
// setup VM // setup VM
Init(); Init();
// load lua files // load lua files
BeginScriptLoad(); BeginScriptLoad();
LoadFile(L, "maps/includes/base.lua"); LoadFile(L, "maps/includes/base.lua");
char filename[256] = {0}; char filename[256] = {0};
char globalscript_filename[256] = {0}; char globalscript_filename[256] = {0};
// Even though LoadFile already checks to see if the file exists, we'll check now so at least the default map lua file is loaded. // Even though LoadFile already checks to see if the file exists, we'll check now so at least the default map lua file is loaded.
// That way servers can keep their suffix set without worrying about every map having whatever game mode they always want to use. // That way servers can keep their suffix set without worrying about every map having whatever game mode they always want to use.
if ( sv_mapluasuffix.GetString()[0] != '0' ) if ( sv_mapluasuffix.GetString()[0] != '0' )
{ {
Msg( "[SCRIPT] sv_mapluasuffix set to %s | finding maps\\%s__%s__.lua\n", sv_mapluasuffix.GetString(), szMapName, sv_mapluasuffix.GetString() ); Msg( "[SCRIPT] sv_mapluasuffix set to %s | finding maps\\%s__%s__.lua\n", sv_mapluasuffix.GetString(), szMapName, sv_mapluasuffix.GetString() );
if ( filesystem->FileExists( UTIL_VarArgs( "maps/%s__%s__.lua", szMapName, sv_mapluasuffix.GetString() ) ) ) if ( filesystem->FileExists( UTIL_VarArgs( "maps/%s__%s__.lua", szMapName, sv_mapluasuffix.GetString() ) ) )
{ {
Q_snprintf( filename, sizeof(filename), "maps/%s__%s__.lua", szMapName, sv_mapluasuffix.GetString() ); Q_snprintf( filename, sizeof(filename), "maps/%s__%s__.lua", szMapName, sv_mapluasuffix.GetString() );
Msg( "[SCRIPT] maps\\%s__%s__.lua found\n", szMapName, sv_mapluasuffix.GetString() ); Msg( "[SCRIPT] maps\\%s__%s__.lua found\n", szMapName, sv_mapluasuffix.GetString() );
} }
else else
{ {
Msg( "[SCRIPT] maps\\%s__%s__.lua not found | reverting to maps\\%s.lua\n", szMapName, sv_mapluasuffix.GetString(), szMapName); Msg( "[SCRIPT] maps\\%s__%s__.lua not found | reverting to maps\\%s.lua\n", szMapName, sv_mapluasuffix.GetString(), szMapName);
} }
} }
// Load global include script, overwriting previously loaded stuff per map // Load global include script, overwriting previously loaded stuff per map
if( sv_luaglobalscript.GetString()[0] != '0' ) if( sv_luaglobalscript.GetString()[0] != '0' )
{ {
const char* scriptname = sv_luaglobalscript.GetString(); const char* scriptname = sv_luaglobalscript.GetString();
Msg("[SCRIPT] sv_luaglobalscript set to %s | loading global script maps maps\\globalscripts\\%s.lua\n", scriptname, scriptname ); Msg("[SCRIPT] sv_luaglobalscript set to %s | loading global script maps maps\\globalscripts\\%s.lua\n", scriptname, scriptname );
if( filesystem->FileExists( UTIL_VarArgs( "maps/globalscripts/%s.lua", scriptname ) ) ) if( filesystem->FileExists( UTIL_VarArgs( "maps/globalscripts/%s.lua", scriptname ) ) )
{ {
Q_snprintf( globalscript_filename, sizeof(globalscript_filename), "maps/globalscripts/%s.lua", scriptname ); Q_snprintf( globalscript_filename, sizeof(globalscript_filename), "maps/globalscripts/%s.lua", scriptname );
Msg("[SCRIPT] maps\\globalscripts\\%s.lua found\n", scriptname );\ Msg("[SCRIPT] maps\\globalscripts\\%s.lua found\n", scriptname );\
} }
else else
{ {
Msg("[SCRIPT] global script maps\\globalscripts\\%s.lua not found - nothing loaded post map lua.\n", scriptname ); Msg("[SCRIPT] global script maps\\globalscripts\\%s.lua not found - nothing loaded post map lua.\n", scriptname );
} }
} }
if ( !filename[0] ) if ( !filename[0] )
Q_snprintf( filename, sizeof(filename), "maps/%s.lua", szMapName ); Q_snprintf( filename, sizeof(filename), "maps/%s.lua", szMapName );
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Try a precache, rumor has it this will cause the engine to send the lua files to clients // Try a precache, rumor has it this will cause the engine to send the lua files to clients
// FF_TODO: dexter - disabled for now, doesnt work anyway right? // FF_TODO: dexter - disabled for now, doesnt work anyway right?
//if(PRECACHE_LUA_FILES) //if(PRECACHE_LUA_FILES)
//{ //{
// V_FixSlashes(filename); // V_FixSlashes(filename);
// if(filesystem->FileExists(filename)) // if(filesystem->FileExists(filename))
// { // {
// Util_AddDownload(filename); // Util_AddDownload(filename);
// if(!engine->IsGenericPrecached(filename)) // if(!engine->IsGenericPrecached(filename))
// engine->PrecacheGeneric(filename, true); // engine->PrecacheGeneric(filename, true);
// } // }
// else // - if no map lua is found, send default (for testing mainly) // else // - if no map lua is found, send default (for testing mainly)
// { // {
// // no check - this file should *always* be there // // no check - this file should *always* be there
// Util_AddDownload(default_luafile); // Util_AddDownload(default_luafile);
// if(!engine->IsGenericPrecached(default_luafile)) // if(!engine->IsGenericPrecached(default_luafile))
// engine->PrecacheGeneric(default_luafile, true); // engine->PrecacheGeneric(default_luafile, true);
// } // }
// //
// // if we have a globalscript, precache it as well // // if we have a globalscript, precache it as well
// if( sv_luaglobalscript.GetString()[0] != '0' && globalscript_filename[0] ) // if( sv_luaglobalscript.GetString()[0] != '0' && globalscript_filename[0] )
// { // {
// V_FixSlashes(globalscript_filename); // V_FixSlashes(globalscript_filename);
// if(filesystem->FileExists(globalscript_filename)) // if(filesystem->FileExists(globalscript_filename))
// { // {
// Util_AddDownload(globalscript_filename); // Util_AddDownload(globalscript_filename);
// if(!engine->IsGenericPrecached(globalscript_filename)) // if(!engine->IsGenericPrecached(globalscript_filename))
// engine->PrecacheGeneric(globalscript_filename, true); // engine->PrecacheGeneric(globalscript_filename, true);
// } // }
// } // }
// ////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////
// /*char testfile[] = {"maps/ff_dm.txt"}; // /*char testfile[] = {"maps/ff_dm.txt"};
// V_FixSlashes(testfile); // V_FixSlashes(testfile);
// if(filesystem->FileExists(testfile)) // if(filesystem->FileExists(testfile))
// { // {
// Util_AddDownload(testfile); // Util_AddDownload(testfile);
// if(!engine->IsGenericPrecached(testfile)) // if(!engine->IsGenericPrecached(testfile))
// engine->PrecacheGeneric(testfile, true); // engine->PrecacheGeneric(testfile, true);
// }*/ // }*/
// ////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////
//} //}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
if(filesystem->FileExists(filename)) if(filesystem->FileExists(filename))
m_ScriptExists = LoadFile(L, filename); m_ScriptExists = LoadFile(L, filename);
else else
{ {
Msg("[SCRIPT] File %s not found! Loaded fallback lua %s\n", filename, default_luafile); Msg("[SCRIPT] File %s not found! Loaded fallback lua %s\n", filename, default_luafile);
m_ScriptExists = LoadFile(L, default_luafile); m_ScriptExists = LoadFile(L, default_luafile);
} }
// force loading global script in another call :/ // force loading global script in another call :/
if( sv_luaglobalscript.GetString()[0] != '0' && globalscript_filename[0] ) if( sv_luaglobalscript.GetString()[0] != '0' && globalscript_filename[0] )
{ {
//BeginScriptLoad(); //BeginScriptLoad();
LoadFile(L, globalscript_filename); LoadFile(L, globalscript_filename);
//EndScriptLoad(); //EndScriptLoad();
} }
EndScriptLoad(); EndScriptLoad();
// spawn the helper entity // spawn the helper entity
//FF_TODO: CFFEntitySystemHelper::Create(); //FF_TODO: CFFEntitySystemHelper::Create();
} }
#endif #endif
#ifdef FF_DLL #ifdef FF_DLL
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void CFFScriptManager::LevelShutdown() void CFFScriptManager::LevelShutdown()
{ {
// shutdown the VM // shutdown the VM
if(L) if(L)
{ {
lua_close(L); lua_close(L);
L = NULL; L = NULL;
} }
} }
#endif #endif
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void CFFScriptManager::OnScriptLoad(const char* szFileName, void CFFScriptManager::OnScriptLoad(const char* szFileName,
const char* szFileContents) const char* szFileContents)
{ {
// ignore the message if we are not still in the "loading" phase // ignore the message if we are not still in the "loading" phase
if(!m_isLoading) if(!m_isLoading)
return; return;
// compute checksums of file contents // compute checksums of file contents
CRC32_ProcessBuffer(&m_scriptCRC, CRC32_ProcessBuffer(&m_scriptCRC,
szFileContents, szFileContents,
strlen(szFileContents)); strlen(szFileContents));
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void CFFScriptManager::BeginScriptLoad() void CFFScriptManager::BeginScriptLoad()
{ {
CRC32_Init(&m_scriptCRC); CRC32_Init(&m_scriptCRC);
m_isLoading = true; m_isLoading = true;
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
void CFFScriptManager::EndScriptLoad() void CFFScriptManager::EndScriptLoad()
{ {
CRC32_Final(&m_scriptCRC); CRC32_Final(&m_scriptCRC);
m_isLoading = false; m_isLoading = false;
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
bool CFFScriptManager::LoadFile( lua_State *L, const char *filename) bool CFFScriptManager::LoadFile( lua_State *L, const char *filename)
{ {
//FF_TODO: VPROF_BUDGET( "CFFScriptManager::LoadFile", VPROF_BUDGETGROUP_FF_LUA ); //FF_TODO: VPROF_BUDGET( "CFFScriptManager::LoadFile", VPROF_BUDGETGROUP_FF_LUA );
// don't allow scripters to sneak in scripts after the initial load // don't allow scripters to sneak in scripts after the initial load
if(!_scriptman.m_isLoading) if(!_scriptman.m_isLoading)
{ {
Warning("[SCRIPT] Loading of scripts after initial map load is not allowed.\n"); Warning("[SCRIPT] Loading of scripts after initial map load is not allowed.\n");
return false; return false;
} }
// open the file // open the file
Msg("[SCRIPT] Loading Lua File: %s\n", filename); Msg("[SCRIPT] Loading Lua File: %s\n", filename);
FileHandle_t hFile = filesystem->Open(filename, "rb", "MOD"); FileHandle_t hFile = filesystem->Open(filename, "rb", "MOD");
if (!hFile) if (!hFile)
{ {
Warning("[SCRIPT] %s either does not exist or could not be opened.\n", filename); Warning("[SCRIPT] %s either does not exist or could not be opened.\n", filename);
return false; return false;
} }
// allocate buffer for file contents // allocate buffer for file contents
int fileSize = filesystem->Size(hFile); int fileSize = filesystem->Size(hFile);
char *buffer = (char*)MemAllocScratch(fileSize + 1); char *buffer = (char*)MemAllocScratch(fileSize + 1);
Assert(buffer); Assert(buffer);
// load file contents into a null-terminated buffer // load file contents into a null-terminated buffer
filesystem->Read(buffer, fileSize, hFile); filesystem->Read(buffer, fileSize, hFile);
buffer[fileSize] = 0; buffer[fileSize] = 0;
filesystem->Close(hFile); filesystem->Close(hFile);
// preprocess script data // preprocess script data
_scriptman.OnScriptLoad(filename, buffer); _scriptman.OnScriptLoad(filename, buffer);
// load script // load script
int errorCode = luaL_loadbuffer(L, buffer, fileSize, filename); int errorCode = luaL_loadbuffer(L, buffer, fileSize, filename);
// check if load was successful // check if load was successful
if(errorCode) if(errorCode)
{ {
if(errorCode == LUA_ERRSYNTAX ) if(errorCode == LUA_ERRSYNTAX )
{ {
// syntax error, lookup description for the error // syntax error, lookup description for the error
const char *error = lua_tostring(L, -1); const char *error = lua_tostring(L, -1);
if (error) if (error)
{ {
Warning("Error loading %s: %s\n", filename, error); Warning("Error loading %s: %s\n", filename, error);
lua_pop( L, 1 ); lua_pop( L, 1 );
} }
else else
Warning("Unknown Syntax Error loading %s\n", filename); Warning("Unknown Syntax Error loading %s\n", filename);
} }
else else
{ {
Msg("Unknown Error loading %s\n", filename); Msg("Unknown Error loading %s\n", filename);
} }
return false; return false;
} }
// execute script. script at top scrop gets exectued // execute script. script at top scrop gets exectued
lua_pcall(L, 0, 0, 0); lua_pcall(L, 0, 0, 0);
Msg( "[SCRIPT] Successfully loaded %s\n", filename ); Msg( "[SCRIPT] Successfully loaded %s\n", filename );
// cleanup // cleanup
MemFreeScratch(); MemFreeScratch();
return true; return true;
} }