b0389f3fda
- aggiornamenti vari - introdotto comando per merge (cucitura) di superfici - migliorata gestione oggetti selezionati in comandi.
567 lines
17 KiB
C++
567 lines
17 KiB
C++
//----------------------------------------------------------------------------
|
|
// EgalTech 2014-2014
|
|
//----------------------------------------------------------------------------
|
|
// File : LUA_General.cpp Data : 27.09.14 Versione : 1.5i5
|
|
// Contenuto : Funzioni generali per LUA.
|
|
//
|
|
//
|
|
//
|
|
// Modifiche : 27.09.14 DS Creazione modulo.
|
|
//
|
|
//
|
|
//----------------------------------------------------------------------------
|
|
|
|
//--------------------------- Include ----------------------------------------
|
|
#include "stdafx.h"
|
|
#include "LUA.h"
|
|
#include "API.h"
|
|
#include "API_Macro.h"
|
|
#include "/EgtDev/Include/EGnStringUtils.h"
|
|
#include "/EgtDev/Include/EGnFileUtils.h"
|
|
#include "/EgtDev/Include/EGnFileCompare.h"
|
|
#include "/EgtDev/Include/EgtPerfCounter.h"
|
|
#include "/EgtDev/Extern/Lua/Include/lua.hpp"
|
|
|
|
using namespace std ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
static lua_State* s_L = nullptr ;
|
|
static string s_LastError ;
|
|
static PerformanceCounter s_Counter ;
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaInit( void)
|
|
{
|
|
// se già aperto, lo fermo per riavviarlo
|
|
if ( s_L != nullptr) {
|
|
lua_close( s_L) ;
|
|
s_L = nullptr ;
|
|
}
|
|
// inizializzo Lua
|
|
s_L = luaL_newstate() ;
|
|
if ( s_L == nullptr) {
|
|
LOG_ERROR( GetLogger(), "Error in Lua interpreter starting (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
// carico le librerie standard
|
|
luaL_openlibs( s_L) ;
|
|
// carico le funzioni speciali
|
|
if ( ! LuaInstallGeneral( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGeneral (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGeoBase( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGeoBase (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGeomDB( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGeomDB (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbCreate( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbCreate (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbCreateCurve( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbCreateCurve (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbCreateSurf( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbCreateSurf (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbModify( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbModify (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbModifyCurve( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbModifyCurve (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbModifySurf( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbModifySurf (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbPartLayer( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbPartLayer (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbObjects( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbObjects (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbObjSelection( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbObjSelection (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGdbObjAttribs( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGdbObjAttribs (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGeoSnap( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGeoSnap (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallGeoTransform( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallGeoTransform (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallScene( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallScene (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
if ( ! LuaInstallExchange( s_L)) {
|
|
LOG_ERROR( GetLogger(), "Error in LuaInstallExchange (" __FUNCTION__ ")")
|
|
return false ;
|
|
}
|
|
|
|
// recupero la versione di Lua
|
|
lua_getglobal( s_L, "_VERSION") ;
|
|
string sLua ;
|
|
if ( LuaGetParam( s_L, - 1, sLua))
|
|
sLua += " interpreter started" ;
|
|
else
|
|
sLua = "Lua *.* interpreter started" ;
|
|
lua_pop( s_L, 1) ;
|
|
LOG_INFO( GetLogger(), sLua.c_str())
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaExit( void)
|
|
{
|
|
if ( s_L == nullptr)
|
|
return false ;
|
|
// termino Lua
|
|
lua_close( s_L) ;
|
|
s_L = nullptr ;
|
|
LOG_INFO( GetLogger(), "Lua interpreter closed")
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaSetLuaLibsDir( const string& sDir)
|
|
{
|
|
// recupero la stringa globale package.path
|
|
lua_getglobal( s_L, "package") ;
|
|
lua_getfield( s_L, -1, "path") ;
|
|
string sCurPath = lua_tostring( s_L, -1) ;
|
|
// verifico se la dir ricevuta è già presente
|
|
if ( sCurPath.find( sDir) != string::npos) {
|
|
lua_pop( s_L, 2) ;
|
|
return true ;
|
|
}
|
|
// imposto la nuova dir
|
|
sCurPath = sDir ;
|
|
sCurPath += "\\?.lua" ;
|
|
lua_pop( s_L, 1) ;
|
|
lua_pushstring( s_L, sCurPath.c_str()) ;
|
|
lua_setfield( s_L, -2, "path") ;
|
|
lua_pop( s_L, 1) ;
|
|
return true ;
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaEvalNumExpr( const string& sExpr, double& dVal)
|
|
{
|
|
// completo la stringa da valutare
|
|
string sEval = "return " + sExpr ;
|
|
// valuto l'espressione
|
|
int nErr = luaL_loadstring( s_L, sEval.c_str()) || lua_pcall( s_L, 0, 1, 0) ;
|
|
// senza errori
|
|
if ( nErr == LUA_OK && lua_type( s_L, -1) == LUA_TNUMBER) {
|
|
dVal = lua_tonumber( s_L, -1) ;
|
|
lua_pop( s_L, 1) ;
|
|
s_LastError.clear() ;
|
|
return true ;
|
|
}
|
|
// altrimenti, errore
|
|
else {
|
|
// recupero il messaggio di errore
|
|
const char* szErr = lua_tostring( s_L, -1) ;
|
|
s_LastError = ( szErr != nullptr) ? szErr : "Error not a number" ;
|
|
lua_pop( s_L, 1) ;
|
|
// lo scrivo nel log
|
|
LOG_ERROR( GetLogger(), s_LastError.c_str())
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaEvalStringExpr( const string& sExpr, string& sVal)
|
|
{
|
|
// completo la stringa da valutare
|
|
string sEval = "return " + sExpr ;
|
|
// valuto l'espressione
|
|
int nErr = luaL_loadstring( s_L, sEval.c_str()) || lua_pcall( s_L, 0, 1, 0) ;
|
|
// senza errori
|
|
if ( nErr == LUA_OK && lua_type( s_L, -1) == LUA_TSTRING) {
|
|
sVal = lua_tostring( s_L, -1) ;
|
|
lua_pop( s_L, 1) ;
|
|
s_LastError.clear() ;
|
|
return true ;
|
|
}
|
|
// altrimenti, errore
|
|
else {
|
|
// recupero il messaggio di errore
|
|
const char* szErr = lua_tostring( s_L, -1) ;
|
|
s_LastError = ( szErr != nullptr) ? szErr : "Error not a string" ;
|
|
lua_pop( s_L, 1) ;
|
|
// lo scrivo nel log
|
|
LOG_ERROR( GetLogger(), s_LastError.c_str())
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaExecLine( const string& sLine)
|
|
{
|
|
// eseguo la linea
|
|
int nErr = luaL_loadstring( s_L, sLine.c_str()) || lua_pcall( s_L, 0, LUA_MULTRET, 0) ;
|
|
// senza errori
|
|
if ( nErr == LUA_OK) {
|
|
s_LastError.clear() ;
|
|
return true ;
|
|
}
|
|
// altrimenti, errore
|
|
else {
|
|
// recupero il messaggio di errore
|
|
const char* szErr = lua_tostring( s_L, -1) ;
|
|
s_LastError = ( szErr != nullptr) ? szErr : "Error unknown" ;
|
|
lua_pop( s_L, 1) ;
|
|
// lo scrivo nel log
|
|
LOG_ERROR( GetLogger(), s_LastError.c_str())
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaExecFile( const string& sFile)
|
|
{
|
|
// eseguo la linea
|
|
int nErr = luaL_loadfile( s_L, sFile.c_str()) || lua_pcall( s_L, 0, LUA_MULTRET, 0) ;
|
|
// senza errori
|
|
if ( nErr == LUA_OK) {
|
|
s_LastError.clear() ;
|
|
return true ;
|
|
}
|
|
// in caso di errore
|
|
else {
|
|
// recupero il messaggio di errore
|
|
const char* szErr = lua_tostring( s_L, -1) ;
|
|
s_LastError = ( szErr != nullptr) ? szErr : "Error unknown" ;
|
|
lua_pop( s_L, 1) ;
|
|
// lo scrivo nel log
|
|
LOG_ERROR( GetLogger(), s_LastError.c_str())
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
bool
|
|
LuaRequire( const string& sFile)
|
|
{
|
|
lua_getglobal( s_L, "require") ;
|
|
lua_pushstring( s_L, sFile.c_str()) ;
|
|
int nErr = lua_pcall( s_L, 1, 1, 0) ; /* call 'require(sFile)' */
|
|
if ( nErr == LUA_OK) {
|
|
lua_setglobal( s_L, sFile.c_str()) ; /* global[sFile] = require return */
|
|
s_LastError.clear() ;
|
|
return true ;
|
|
}
|
|
else {
|
|
// recupero il messaggio di errore
|
|
const char* szErr = lua_tostring( s_L, -1) ;
|
|
s_LastError = ( szErr != nullptr) ? szErr : "Error unknown" ;
|
|
lua_pop( s_L, 1) ;
|
|
// lo scrivo nel log
|
|
LOG_ERROR( GetLogger(), s_LastError.c_str())
|
|
return false ;
|
|
}
|
|
}
|
|
|
|
//----------------------------------------------------------------------------
|
|
const string&
|
|
LuaGetLastError( void)
|
|
{
|
|
return s_LastError ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
MyPrint( lua_State* L)
|
|
{
|
|
string sOut ;
|
|
int n = lua_gettop( L) ; /* number of arguments */
|
|
lua_getglobal( L, "tostring") ;
|
|
for ( int i = 1 ; i <= n ; ++ i) {
|
|
const char* s ;
|
|
lua_pushvalue( L, -1) ; /* function to be called */
|
|
lua_pushvalue( L, i) ; /* value to print */
|
|
lua_call( L, 1, 1) ;
|
|
s = lua_tostring( L, -1) ; /* get result */
|
|
if ( s == nullptr)
|
|
return luaL_error( L,
|
|
LUA_QL("tostring") " must return a string to " LUA_QL("print")) ;
|
|
if ( i > 1)
|
|
sOut += "\t" ;
|
|
sOut += s ;
|
|
lua_pop( L, 1) ; /* pop result */
|
|
}
|
|
if ( ! sOut.empty())
|
|
LOG_INFO( GetLogger(), sOut.c_str())
|
|
|
|
return 0 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
SetContext( lua_State* L)
|
|
{
|
|
// un solo parametro intero
|
|
int nGseCtx ;
|
|
LuaCheckParam( L, 1, nGseCtx)
|
|
LuaClearStack( L) ;
|
|
// imposto il contesto
|
|
bool bOk = SetCurrGseContext( nGseCtx) ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
GetContext( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// restituisco l'indice del contesto
|
|
LuaSetReturn( L, GetIndCurrGseContext()) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaPause( lua_State* L)
|
|
{
|
|
// 1 parametro : numero di millisecondi
|
|
int nTime ;
|
|
LuaCheckParam( L, 1, nTime)
|
|
LuaClearStack( L) ;
|
|
// controllo la durata della pausa e la eseguo
|
|
const int MIN_TIME = 0 ;
|
|
const int MAX_TIME = 10000 ;
|
|
if ( nTime < MIN_TIME)
|
|
nTime = MIN_TIME ;
|
|
else if ( nTime > MAX_TIME)
|
|
nTime = MAX_TIME ;
|
|
Sleep( nTime) ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, true) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaStartCounter( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// avvio il contatore
|
|
s_Counter.Start() ;
|
|
return 0 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaStopCounter( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// fermo il contatore
|
|
double dTime = s_Counter.Stop() ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, dTime) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaNumToString( lua_State* L)
|
|
{
|
|
// 2 parametri : dVal, nDec
|
|
double dVal ;
|
|
LuaCheckParam( L, 1, dVal)
|
|
int nDec ;
|
|
LuaCheckParam( L, 2, nDec)
|
|
LuaClearStack( L) ;
|
|
// costruisco la stringa
|
|
string sVal = ToString( dVal, nDec) ;
|
|
// ritorno il risultato
|
|
LuaSetReturn( L, sVal) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaExecTsc( lua_State* L)
|
|
{
|
|
// 1 parametro : file
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
LuaClearStack( L) ;
|
|
// eseguo lo script TSC
|
|
bool bOk = EgtTscExecFile( sFile) ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaOutLog( lua_State* L)
|
|
{
|
|
// 1 parametro : stringa da emettere
|
|
string sOut ;
|
|
LuaCheckParam( L, 1, sOut)
|
|
LuaClearStack( L) ;
|
|
// accodo il messaggio nel file di log
|
|
LOG_INFO( GetLogger(), sOut.c_str())
|
|
// non c'è risultato
|
|
return 0 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaEraseFile( lua_State* L)
|
|
{
|
|
// 1 parametro : file
|
|
string sFile ;
|
|
LuaCheckParam( L, 1, sFile)
|
|
LuaClearStack( L) ;
|
|
// svuoto il direttorio
|
|
bool bOk = EraseFile( sFile) ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaEmptyDirectory( lua_State* L)
|
|
{
|
|
// 1 parametro : dir
|
|
string sDir ;
|
|
LuaCheckParam( L, 1, sDir)
|
|
LuaClearStack( L) ;
|
|
// svuoto il direttorio
|
|
bool bOk = EmptyDirectory( sDir) ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaTextFileCompare( lua_State* L)
|
|
{
|
|
// 4 parametri : file1, file2, inizio commento, file diff
|
|
string sFile1 ;
|
|
LuaCheckParam( L, 1, sFile1)
|
|
string sFile2 ;
|
|
LuaCheckParam( L, 2, sFile2)
|
|
string sRemStart ;
|
|
LuaCheckParam( L, 3, sRemStart)
|
|
string sFileDiff ;
|
|
LuaCheckParam( L, 4, sFileDiff)
|
|
LuaClearStack( L) ;
|
|
// eseguo il confronto
|
|
bool bOk = TextFileCompare( sFile1, sFile2, sRemStart, sFileDiff) ;
|
|
LuaSetReturn( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaBinaryFileCompare( lua_State* L)
|
|
{
|
|
// 3 parametri : file1, file2, file diff
|
|
string sFile1 ;
|
|
LuaCheckParam( L, 1, sFile1)
|
|
string sFile2 ;
|
|
LuaCheckParam( L, 2, sFile2)
|
|
string sFileDiff ;
|
|
LuaCheckParam( L, 3, sFileDiff)
|
|
LuaClearStack( L) ;
|
|
// eseguo il confronto
|
|
bool bOk = BinaryFileCompare( sFile1, sFile2, sFileDiff) ;
|
|
LuaSetReturn( L, bOk) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaGetVersion( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// costruisco la stringa con le versioni
|
|
string sVer ;
|
|
if ( ! EgtGetVersionInfo( sVer, "\n"))
|
|
sVer = "VersionInfo error" ;
|
|
// restituisco il risultato
|
|
LuaSetReturn( L, sVer) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
static int
|
|
LuaIs64bit( lua_State* L)
|
|
{
|
|
// nessun parametro
|
|
LuaClearStack( L) ;
|
|
// restituisco il risultato
|
|
#if defined( _WIN64)
|
|
bool bRes = true ;
|
|
#elif defined( _WIN32)
|
|
bool bRes = false ;
|
|
#endif
|
|
LuaSetReturn( L, bRes) ;
|
|
return 1 ;
|
|
}
|
|
|
|
//-------------------------------------------------------------------------------
|
|
bool
|
|
LuaInstallGeneral( lua_State* L)
|
|
{
|
|
try {
|
|
lua_register( L, "print", MyPrint) ;
|
|
lua_register( L, "EgtSetContext", SetContext) ;
|
|
lua_register( L, "EgtGetContext", GetContext) ;
|
|
lua_register( L, "EgtPause", LuaPause) ;
|
|
lua_register( L, "EgtStartCounter", LuaStartCounter) ;
|
|
lua_register( L, "EgtStopCounter", LuaStopCounter) ;
|
|
lua_register( L, "EgtNumToString", LuaNumToString) ;
|
|
lua_register( L, "EgtExecTsc", LuaExecTsc) ;
|
|
lua_register( L, "EgtOutLog", LuaOutLog) ;
|
|
lua_register( L, "EgtEraseFile", LuaEraseFile) ;
|
|
lua_register( L, "EgtEmptyDirectory", LuaEmptyDirectory) ;
|
|
lua_register( L, "EgtTextFileCompare", LuaTextFileCompare) ;
|
|
lua_register( L, "EgtBinaryFileCompare", LuaBinaryFileCompare) ;
|
|
lua_register( L, "EgtGetVersion", LuaGetVersion) ;
|
|
lua_register( L, "EgtIs64bit", LuaIs64bit) ;
|
|
}
|
|
catch ( ...) {
|
|
return false ;
|
|
}
|
|
return true ;
|
|
}
|