EgtGeneral 1.6c6 :
- aggiunto oggetto gestione interprete lua.
This commit is contained in:
+237
@@ -0,0 +1,237 @@
|
||||
//----------------------------------------------------------------------------
|
||||
// EgalTech 2015-2015
|
||||
//----------------------------------------------------------------------------
|
||||
// File : LuaManager.cpp Data : 21.03.16 Versione : 1.6c6
|
||||
// Contenuto : Implementazione della classe LuaMgr.
|
||||
//
|
||||
//
|
||||
//
|
||||
// Modifiche : 21.03.15 DS Creazione modulo.
|
||||
//
|
||||
//
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
//--------------------------- Include ----------------------------------------
|
||||
#include "stdafx.h"
|
||||
#include "DllMain.h"
|
||||
#include "/EgtDev/Include/EGnLuaMgr.h"
|
||||
#include "/EgtDev/Include/EGnLuaAux.h"
|
||||
#include "/EgtDev/Include/EgtILogger.h"
|
||||
#include "/EgtDev/Extern/Lua/Include/lua.hpp"
|
||||
|
||||
using namespace std ;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::Init( void)
|
||||
{
|
||||
// se già aperto, lo fermo per riavviarlo
|
||||
if ( m_pL != nullptr) {
|
||||
lua_close( m_pL) ;
|
||||
m_pL = nullptr ;
|
||||
}
|
||||
// inizializzo Lua
|
||||
m_pL = luaL_newstate() ;
|
||||
if ( m_pL == nullptr) {
|
||||
LOG_ERROR( GetEGnLogger(), "Error in Lua interpreter starting (" __FUNCTION__ ")")
|
||||
return false ;
|
||||
}
|
||||
// carico le librerie standard
|
||||
luaL_openlibs( m_pL) ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::Exit( void)
|
||||
{
|
||||
if ( m_pL == nullptr)
|
||||
return false ;
|
||||
// termino Lua
|
||||
lua_close( m_pL) ;
|
||||
m_pL = nullptr ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::GetVersion( string& sLuaVer)
|
||||
{
|
||||
// verifico il parametro di ritorno
|
||||
if ( &sLuaVer == nullptr)
|
||||
return false ;
|
||||
// recupero la versione di Lua
|
||||
lua_getglobal( m_pL, "_VERSION") ;
|
||||
if ( LuaGetParam( m_pL, - 1, sLuaVer))
|
||||
return true ;
|
||||
else
|
||||
return false ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::SetLuaLibsDir( const string& sDir)
|
||||
{
|
||||
// recupero la stringa globale package.path
|
||||
lua_getglobal( m_pL, "package") ;
|
||||
lua_getfield( m_pL, -1, "path") ;
|
||||
string sCurPath = lua_tostring( m_pL, -1) ;
|
||||
// verifico se la dir ricevuta è già presente
|
||||
if ( sCurPath.find( sDir) != string::npos) {
|
||||
lua_pop( m_pL, 2) ;
|
||||
return true ;
|
||||
}
|
||||
// imposto la nuova dir
|
||||
sCurPath = sDir ;
|
||||
sCurPath += "\\?.lua" ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
lua_pushstring( m_pL, sCurPath.c_str()) ;
|
||||
lua_setfield( m_pL, -2, "path") ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::Require( const string& sFile)
|
||||
{
|
||||
lua_getglobal( m_pL, "require") ;
|
||||
lua_pushstring( m_pL, sFile.c_str()) ;
|
||||
int nErr = lua_pcall( m_pL, 1, 1, 0) ; /* call 'require(sFile)' */
|
||||
if ( nErr == LUA_OK) {
|
||||
lua_setglobal( m_pL, sFile.c_str()) ; /* global[sFile] = require return */
|
||||
m_sLastError.clear() ;
|
||||
return true ;
|
||||
}
|
||||
else {
|
||||
// recupero il messaggio di errore
|
||||
const char* szErr = lua_tostring( m_pL, -1) ;
|
||||
m_sLastError = ( szErr != nullptr) ? szErr : "Error unknown" ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
// lo scrivo nel log
|
||||
LOG_ERROR( GetEGnLogger(), m_sLastError.c_str())
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::RegisterFunction( const string& sFunName, PFLUA pFun)
|
||||
{
|
||||
if ( &sFunName == nullptr)
|
||||
return false ;
|
||||
try {
|
||||
lua_register( m_pL, sFunName.c_str(), pFun) ;
|
||||
}
|
||||
catch ( ...) {
|
||||
return false ;
|
||||
}
|
||||
return true ;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::EvalNumExpr( const string& sExpr, double& dVal)
|
||||
{
|
||||
// completo la stringa da valutare
|
||||
string sEval = "return " + sExpr ;
|
||||
// valuto l'espressione
|
||||
int nErr = luaL_loadstring( m_pL, sEval.c_str()) || lua_pcall( m_pL, 0, 1, 0) ;
|
||||
// senza errori
|
||||
if ( nErr == LUA_OK && lua_type( m_pL, -1) == LUA_TNUMBER) {
|
||||
dVal = lua_tonumber( m_pL, -1) ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
m_sLastError.clear() ;
|
||||
return true ;
|
||||
}
|
||||
// altrimenti, errore
|
||||
else {
|
||||
// recupero il messaggio di errore
|
||||
const char* szErr = lua_tostring( m_pL, -1) ;
|
||||
m_sLastError = ( szErr != nullptr) ? szErr : "Error not a number" ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
// lo scrivo nel log
|
||||
LOG_ERROR( GetEGnLogger(), m_sLastError.c_str())
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::EvalStringExpr( const string& sExpr, string& sVal)
|
||||
{
|
||||
// completo la stringa da valutare
|
||||
string sEval = "return " + sExpr ;
|
||||
// valuto l'espressione
|
||||
int nErr = luaL_loadstring( m_pL, sEval.c_str()) || lua_pcall( m_pL, 0, 1, 0) ;
|
||||
// senza errori
|
||||
if ( nErr == LUA_OK && lua_type( m_pL, -1) == LUA_TSTRING) {
|
||||
sVal = lua_tostring( m_pL, -1) ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
m_sLastError.clear() ;
|
||||
return true ;
|
||||
}
|
||||
// altrimenti, errore
|
||||
else {
|
||||
// recupero il messaggio di errore
|
||||
const char* szErr = lua_tostring( m_pL, -1) ;
|
||||
m_sLastError = ( szErr != nullptr) ? szErr : "Error not a string" ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
// lo scrivo nel log
|
||||
LOG_ERROR( GetEGnLogger(), m_sLastError.c_str())
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::ExecLine( const string& sLine)
|
||||
{
|
||||
// eseguo la linea
|
||||
int nErr = luaL_loadstring( m_pL, sLine.c_str()) || lua_pcall( m_pL, 0, LUA_MULTRET, 0) ;
|
||||
// senza errori
|
||||
if ( nErr == LUA_OK) {
|
||||
m_sLastError.clear() ;
|
||||
return true ;
|
||||
}
|
||||
// altrimenti, errore
|
||||
else {
|
||||
// recupero il messaggio di errore
|
||||
const char* szErr = lua_tostring( m_pL, -1) ;
|
||||
m_sLastError = ( szErr != nullptr) ? szErr : "Error unknown" ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
// lo scrivo nel log
|
||||
LOG_ERROR( GetEGnLogger(), m_sLastError.c_str())
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
bool
|
||||
LuaMgr::ExecFile( const string& sFile)
|
||||
{
|
||||
// eseguo la linea
|
||||
int nErr = luaL_loadfile( m_pL, sFile.c_str()) || lua_pcall( m_pL, 0, LUA_MULTRET, 0) ;
|
||||
// senza errori
|
||||
if ( nErr == LUA_OK) {
|
||||
m_sLastError.clear() ;
|
||||
return true ;
|
||||
}
|
||||
// in caso di errore
|
||||
else {
|
||||
// recupero il messaggio di errore
|
||||
const char* szErr = lua_tostring( m_pL, -1) ;
|
||||
m_sLastError = ( szErr != nullptr) ? szErr : "Error unknown" ;
|
||||
lua_pop( m_pL, 1) ;
|
||||
// lo scrivo nel log
|
||||
LOG_ERROR( GetEGnLogger(), m_sLastError.c_str())
|
||||
return false ;
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
const string&
|
||||
LuaMgr::GetLastError( void)
|
||||
{
|
||||
return m_sLastError ;
|
||||
}
|
||||
Reference in New Issue
Block a user