ab8ec30e29
- sistemato minuscole/maiuscole.
169 lines
5.6 KiB
C++
169 lines
5.6 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2015-2015
|
|
//----------------------------------------------------------------------------
|
|
// File : EGnLuaMgr.h Data : 21.03.15 Versione : 1.6c6
|
|
// Contenuto : Dichiarazione della classe LuaMgr.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 21.03.15 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
#pragma once
|
|
|
|
#include "/EgtDev/Include/EGnLuaAux.h"
|
|
#include <string>
|
|
|
|
//----------------------- Macro per import/export -----------------------------
|
|
#undef EGN_EXPORT
|
|
#if defined( I_AM_EGN) // da definirsi solo nella DLL
|
|
#define EGN_EXPORT __declspec( dllexport)
|
|
#else
|
|
#define EGN_EXPORT __declspec( dllimport)
|
|
#endif
|
|
|
|
//-----------------------------------------------------------------------------
|
|
struct lua_State ;
|
|
typedef int(*PFLUA) ( lua_State*) ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
class LuaMgr
|
|
{
|
|
public :
|
|
EGN_EXPORT LuaMgr( void)
|
|
: m_pL( nullptr), m_nRets( 0) {}
|
|
EGN_EXPORT ~LuaMgr( void)
|
|
{ Exit() ; }
|
|
EGN_EXPORT bool Init( void) ;
|
|
EGN_EXPORT bool Exit( void) ;
|
|
EGN_EXPORT bool GetVersion( std::string& sLuaVer) const ;
|
|
EGN_EXPORT bool SetLuaLibsDir( const std::string& sDir) ;
|
|
EGN_EXPORT bool Require( const std::string& sFile) ;
|
|
EGN_EXPORT bool RegisterFunction( const std::string& sFunName, PFLUA pFun) ;
|
|
EGN_EXPORT bool EvalExpr( const std::string& sExpr, double& dVal) ;
|
|
EGN_EXPORT bool EvalExpr( const std::string& sExpr, std::string& sVal) ;
|
|
EGN_EXPORT bool ExecLine( const std::string& sLine) ;
|
|
EGN_EXPORT bool ExecFile( const std::string& sFile) ;
|
|
EGN_EXPORT lua_State* GetLuaState( void)
|
|
{ return m_pL ; }
|
|
EGN_EXPORT const std::string& GetLastError( void) const
|
|
{ return m_sLastError ; }
|
|
EGN_EXPORT const std::string& GetLuaLibsDir( void) const
|
|
{ return m_sLuaLibsDir ; }
|
|
EGN_EXPORT const std::string& GetLastRequire( void) const
|
|
{ return m_sLastRequire ; }
|
|
EGN_EXPORT bool ExistsFunction( const std::string& sFunName) const ;
|
|
template<typename... Args>
|
|
bool CallFunction( const std::string& sFunName, int nRets, const Args&... params) ;
|
|
template<typename... Args>
|
|
bool GetFunctionReturns( Args&... rets) ;
|
|
|
|
private :
|
|
EGN_EXPORT void LogError( const std::string& sErr) const ;
|
|
|
|
private :
|
|
lua_State* m_pL ;
|
|
std::string m_sLastError ;
|
|
std::string m_sLuaLibsDir ;
|
|
std::string m_sLastRequire ;
|
|
int m_nRets ;
|
|
} ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
template<typename T, typename... Args>
|
|
bool
|
|
LuaSetFunParams( lua_State* L, const T param, const Args&... params)
|
|
{
|
|
return ( LuaSetParam( L, param) && LuaSetFunParams( L, params...)) ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
LuaSetFunParams( lua_State* L)
|
|
{
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template<typename... Args>
|
|
bool
|
|
LuaMgr::CallFunction( const std::string& sFunName, int nRets, const Args&... params)
|
|
{
|
|
// recupero la funzione
|
|
if ( sFunName.find( '.') == std::string::npos) {
|
|
// è direttamente il nome
|
|
lua_getglobal( m_pL, sFunName.c_str()) ;
|
|
}
|
|
else {
|
|
// è in una tavola
|
|
std::string sTab, sField ;
|
|
SplitFirst( sFunName, ".", sTab, sField) ;
|
|
lua_getglobal( m_pL, sTab.c_str()) ;
|
|
if ( ! lua_istable( m_pL, -1))
|
|
return false ;
|
|
lua_getfield( m_pL, -1, sField.c_str()) ;
|
|
// porto la funzione uno slot sotto e diminuisco lo stack di uno (per essere come caso sopra)
|
|
lua_copy( m_pL, -1, -2) ;
|
|
lua_pop( m_pL, 1) ;
|
|
}
|
|
// numero di parametri
|
|
int nParNbr = sizeof...( Args) ;
|
|
// inserisco i parametri sullo stack
|
|
LuaSetFunParams( m_pL, params...) ;
|
|
// eseguo la chiamata
|
|
int nErr = lua_pcall( m_pL, nParNbr, nRets, 0) ;
|
|
// senza errori
|
|
if ( nErr == LUA_OK) {
|
|
m_nRets = nRets ;
|
|
m_sLastError.clear() ;
|
|
return true ;
|
|
}
|
|
// altrimenti, errore
|
|
else {
|
|
// non ci sono parametri di ritorno
|
|
m_nRets = 0 ;
|
|
// recupero il messaggio di errore
|
|
const char* szErr = lua_tostring( m_pL, -1) ;
|
|
m_sLastError = sFunName + " " + (( szErr != nullptr) ? szErr : "Error calling") ;
|
|
lua_pop( m_pL, 1) ;
|
|
// lo scrivo nel log
|
|
LogError( m_sLastError) ;
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template<typename T, typename... Args>
|
|
bool
|
|
LuaGetFunParams( lua_State* L, T& param, Args&... params)
|
|
{
|
|
bool bOk = LuaGetFunParams( L, params...) && LuaGetParam( L, -1, param) ;
|
|
lua_pop( L, 1) ;
|
|
return bOk ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
inline bool
|
|
LuaGetFunParams( lua_State* L)
|
|
{
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
template<typename... Args>
|
|
bool
|
|
LuaMgr::GetFunctionReturns( Args&... rets)
|
|
{
|
|
// verifico che il numero di parametri lasciati dalla funzione chiamata sia adeguato
|
|
int nParNbr = sizeof...( Args) ;
|
|
if ( m_nRets < nParNbr)
|
|
return false ;
|
|
// recupero i parametri
|
|
bool bOk = LuaGetFunParams( m_pL, rets...) ;
|
|
// decremento il contatore
|
|
m_nRets -= nParNbr ;
|
|
return bOk ;
|
|
}
|