Example #1
0
int CLuaFileDefs::fileWrite ( lua_State* luaVM )
{
//  int fileWrite ( file theFile, string string1 [, string string2, string string3 ...])
    CScriptFile* pFile;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pFile );
    
    // Ensure we have atleast one string
    if ( !argStream.NextIsString () )
        argStream.SetTypeError ( "string" );

    if ( !argStream.HasErrors () )
    {
        long lBytesWritten = 0; // Total bytes written
        
        // While we're not out of string arguments
        // (we will always have at least one string because we validated it above)
        while ( argStream.NextIsString () )
        {
            // Grab argument and length
            SString strData;
            argStream.ReadString ( strData );
            unsigned long ulDataLen = strData.length ();

            // Write the data
            long lArgBytesWritten = pFile->Write ( ulDataLen, strData );

            // Did the file mysteriously disappear?
            if ( lArgBytesWritten == -1 )
            {
                m_pScriptDebugging->LogBadPointer ( luaVM, "file", 1 );
                lua_pushnil ( luaVM );
                return 1;
            }

            // Add the number of bytes written to our counter
            lBytesWritten += lArgBytesWritten;
        }

#ifdef MTA_CLIENT
        // Inform file verifier
        if ( lBytesWritten != 0 )
            g_pClientGame->GetResourceManager ()->OnFileModifedByScript ( pFile->GetAbsPath (), "fileWrite" );
#endif
        
        // Return the number of bytes we wrote
        lua_pushnumber ( luaVM, lBytesWritten );
        return 1;
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    // Error
    lua_pushnil ( luaVM );
    return 1;
}
Example #2
0
int CLuaFileDefs::fileWrite ( lua_State* luaVM )
{
    // string fileWrite ( file, string [, string2, string3, ...] )
    CScriptFile* pFile;
    SString strMessage;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pFile );
    argStream.ReadString ( strMessage );

    if ( !argStream.HasErrors ( ) )
    {
        // While we're not out of string arguments
        long lBytesWritten = 0;
        long lArgBytesWritten = 0;
        do
        {
            unsigned long ulDataLen = strMessage.length ( );

            // Write it and add the bytes written to our total bytes written
            lArgBytesWritten = pFile->Write ( ulDataLen, strMessage.c_str ( ) );
            if ( lArgBytesWritten == -1 )
            {
                m_pScriptDebugging->LogBadPointer ( luaVM, "file", 1 );
                lua_pushnil ( luaVM );
                return 1;
            }
            lBytesWritten += lArgBytesWritten;

            if ( !argStream.NextIsString ( ) )
                break;

            argStream.ReadString ( strMessage );
        }
        while ( true );

        // Inform file verifier
        if ( lBytesWritten > 0 )
            g_pClientGame->GetResourceManager()->OnFileModifedByScript( pFile->GetAbsPath(), "fileWrite" );

        // Return the number of bytes we wrote
        lua_pushnumber ( luaVM, lBytesWritten );
        return 1;
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    lua_pushnil ( luaVM );
    return 1;
}