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; }
// // Read next as resource or resource name. Result output as resource // void MixedReadResourceString ( CScriptArgReader& argStream, CResource*& pOutResource ) { if ( !argStream.NextIsString () ) { argStream.ReadUserData ( pOutResource ); } else { SString strResourceName; argStream.ReadString ( strResourceName ); pOutResource = g_pGame->GetResourceManager ()->GetResource ( strResourceName ); if ( !pOutResource ) argStream.SetTypeError ( "resource", argStream.m_iIndex - 1 ); } }
int CLuaFileDefs::fileWrite ( lua_State* luaVM ) { // int fileWrite ( file theFile, string string1 [, string string2, string string3 ...]) CScriptFile* pFile; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pFile ); if ( !argStream.NextIsString () ) argStream.SetTypeError ( "string" ); if ( !argStream.HasErrors () ) { // While we're not out of string arguments long lBytesWritten = 0; long lArgBytesWritten = 0; do { // Grab argument and length SString strData; argStream.ReadString ( strData ); unsigned long ulDataLen = strData.length (); // Write it and add the bytes written to our total bytes written lArgBytesWritten = pFile->Write ( ulDataLen, strData ); if ( lArgBytesWritten == -1 ) { m_pScriptDebugging->LogBadPointer ( luaVM, "file", 1 ); lua_pushnil ( luaVM ); return 1; } lBytesWritten += lArgBytesWritten; } while ( argStream.NextIsString () ); // 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; }