int CLuaFileDefs::fileGetPath ( lua_State* luaVM ) { // string fileGetPath ( file theFile ) CScriptFile* pFile; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pFile ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { CResource* pThisResource = pLuaMain->GetResource (); CResource* pFileResource = pFile->GetResource (); SString strFilePath = pFile->GetFilePath (); // If the calling resource is not the resource the file resides in // we need to prepend :resourceName to the path if ( pThisResource != pFileResource ) { #ifdef MTA_CLIENT strFilePath = SString ( ":%s/%s", pFileResource->GetName (), *strFilePath ); #else strFilePath = SString ( ":%s/%s", *pFileResource->GetName (), *strFilePath ); #endif } lua_pushlstring ( luaVM, strFilePath, strFilePath.length () ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileGetPath( lua_State* luaVM ) { // string fileGetPath ( file theFile ) CScriptFile* pFile; CScriptArgReader argStream( luaVM ); argStream.ReadUserData( pFile ); if ( !argStream.HasErrors( ) ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine( luaVM ); if ( pLuaMain ) { // We have a resource argument? CResource* pThisResource = pLuaMain->GetResource( ); CResource* pFileResource = pFile->GetResource( ); const SString& strFilePath = pFile->GetFilePath( ); SString outPath = strFilePath; // If the calling resource is not the resource the file resides in // we need to prepend :resourceName to the path if ( pThisResource != pFileResource ) { outPath = ":" + pFileResource->GetName() + "/" + outPath; } lua_pushlstring( luaVM, outPath.c_str( ), outPath.length( ) ); return 1; } } else m_pScriptDebugging->LogCustom( luaVM, argStream.GetFullErrorMessage( ) ); // Failed lua_pushboolean( luaVM, false ); return 1; }
// Called by Lua when file userdatas are garbage collected int CLuaFileDefs::fileCloseGC ( lua_State* luaVM ) { CScriptFile* pFile; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pFile ); if ( !argStream.HasErrors () ) { // Close the file and delete it pFile->Unload (); m_pElementDeleter->Delete ( pFile ); // This file wasn't closed, so we should warn // the scripter that they forgot to close it. m_pScriptDebugging->LogWarning ( luaVM, "Unclosed file (%s) was garbage collected. Check your resource for dereferenced files.", pFile->GetFilePath ().c_str () ); // TODO: The debug info reported when Lua automatically garbage collects will // actually be the exact point Lua pauses for collection. Find a way to // remove the line number & script file completely. lua_pushboolean ( luaVM, true ); return 1; } lua_pushnil ( luaVM ); return 1; }