int CLuaFunctionDefs::SetWeaponOwner ( lua_State* luaVM ) { CClientWeapon * pWeapon; CClientPlayer * pPlayer; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pWeapon ); if ( argStream.NextIsUserData() ) { argStream.ReadUserData ( pPlayer ); if ( !argStream.HasErrors () ) { pWeapon->SetOwner( pPlayer ); lua_pushboolean ( luaVM, true ); return 1; } } else if ( argStream.NextIsNil() ) { if ( !argStream.HasErrors () ) { pWeapon->SetOwner( NULL ); lua_pushboolean ( luaVM, true ); return 1; } } if ( argStream.HasErrors() ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushnil ( luaVM ); return 1; }
int CLuaBanDefs::RemoveBan ( lua_State* luaVM ) { CBan* pBan; CPlayer* pResponsible = NULL; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pBan ); if ( argStream.NextIsUserData () ) { CElement* pResponsibleElement; argStream.ReadUserData ( pResponsibleElement ); pResponsible = dynamic_cast <CPlayer*> ( pResponsibleElement ); } if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::RemoveBan ( pBan, pResponsible ) ) { lua_pushboolean ( luaVM, true ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::GetResourceConfig ( lua_State* luaVM ) { CScriptArgReader argStream ( luaVM ); if ( argStream.NextIsUserData ( ) ) m_pScriptDebugging->LogCustom ( luaVM, "getResourceConfig may be using an outdated syntax. Please check and update." ); // Resource and config name CResource* pResource = NULL; SString strInput; SString strAbsPath; SString strMetaPath; argStream.ReadString ( strInput ); if ( !argStream.HasErrors () ) { // Grab our lua main CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Grab resource and the config name from arg pResource = pLuaMain->GetResource (); // We have both a resource file to grab the config from and a config name? if ( pResource ) { if ( CResourceManager::ParseResourcePathInput ( strInput, pResource, strAbsPath, strMetaPath ) ) { // Loop through the configs in that resource list < CResourceConfigItem* >::iterator iter = pResource->ConfigIterBegin (); for ( ; iter != pResource->ConfigIterEnd (); iter++ ) { // Matching name? if ( strcmp ( (*iter)->GetShortName(), strMetaPath.c_str() ) == 0 ) { // Return it CResourceConfigItem* pConfig = (CResourceConfigItem*) (*iter); CXMLNode* pNode = pConfig->GetRoot (); if ( pNode ) { lua_pushxmlnode ( luaVM, pNode ); return 1; } } } } } } else m_pScriptDebugging->LogBadType ( luaVM ); } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaBanDefs::AddBan ( lua_State* luaVM ) { // ban addBan ( [ string IP, string Username, string Serial, player responsibleElement, string reason, int seconds = 0 ] ) SString strIP = ""; SString strUsername = ""; SString strSerial = ""; SString strResponsible = "Console"; CPlayer * pResponsible = NULL; SString strReason = ""; time_t tUnban; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strIP, "" ); argStream.ReadString ( strUsername, "" ); argStream.ReadString ( strSerial, "" ); if ( argStream.NextIsUserData () ) { CElement* pResponsibleElement; argStream.ReadUserData ( pResponsibleElement ); if ( ( pResponsible = dynamic_cast <CPlayer*> ( pResponsibleElement ) ) ) strResponsible = pResponsible->GetNick (); } else argStream.ReadString ( strResponsible, "Console" ); argStream.ReadString ( strReason, "" ); if ( argStream.NextIsString () ) { SString strTime; argStream.ReadString ( strTime ); tUnban = atoi ( strTime ); } else if ( argStream.NextIsNumber () ) argStream.ReadNumber ( tUnban ); else tUnban = 0; if ( tUnban > 0 ) tUnban += time ( NULL ); if ( !argStream.HasErrors () ) { CBan* pBan = NULL; if ( ( pBan = CStaticFunctionDefinitions::AddBan ( strIP, strUsername, strSerial, pResponsible, strResponsible, strReason, tUnban ) ) ) { lua_pushban ( luaVM, pBan ); return 1; } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileDelete ( lua_State* luaVM ) { // bool fileDelete ( string filePath ) SString strFile; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strFile ); if ( argStream.NextIsUserData () ) m_pScriptDebugging->LogCustom ( luaVM, "fileDelete may be using an outdated syntax. Please check and update." ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { std::string strPath; // We have a resource argument? CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strPath, NULL ) ) { // Do we have permissions? if ( pResource == pThisResource || m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "ModifyOtherObjects", CAccessControlListRight::RIGHT_TYPE_GENERAL, false ) ) { // Make sure the dir exists so we can remove the file MakeSureDirExists ( strPath.c_str () ); if ( remove ( strPath.c_str () ) == 0 ) { // If file removed return success lua_pushboolean ( luaVM, true ); return 1; } else { // Output error m_pScriptDebugging->LogWarning ( luaVM, "%s; unable to delete file", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) ); } } else m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pResource->GetName ().c_str () ); } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::SetWeaponTarget ( lua_State* luaVM ) { CClientWeapon * pWeapon; CClientEntity * pTarget; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pWeapon ); if ( argStream.NextIsUserData() ) { int targetBone; argStream.ReadUserData ( pTarget ); argStream.ReadNumber ( targetBone, 255 ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponTarget ( pWeapon, pTarget, targetBone ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else if ( argStream.NextIsNumber() ) { CVector vecTarget; argStream.ReadNumber( vecTarget.fX ); argStream.ReadNumber( vecTarget.fY ); argStream.ReadNumber( vecTarget.fZ ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponTarget ( pWeapon, vecTarget ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else if ( argStream.NextIsNil() ) { if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::ClearWeaponTarget ( pWeapon ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else argStream.SetCustomError( "Expected element, number or nil at argument 2" ); if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::Reference ( lua_State* luaVM ) { CScriptArgReader argStream ( luaVM ); if ( argStream.NextIsTable ( ) || argStream.NextIsFunction ( ) || argStream.NextIsUserData ( ) ) { int iPointer = lua_ref ( luaVM, 1 ); lua_pushnumber ( luaVM, iPointer ); return 1; } lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileDelete ( lua_State* luaVM ) { // bool fileDelete ( string filePath ) SString strInputPath; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInputPath ); // This is only really necessary server side if ( argStream.NextIsUserData () ) m_pScriptDebugging->LogCustom ( luaVM, "fileDelete may be using an outdated syntax. Please check and update." ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { SString strAbsPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strInputPath, pResource, &strAbsPath ) ) { CheckCanModifyOtherResource( argStream, pThisResource, pResource ); CheckCanAccessOtherResourceFile( argStream, pThisResource, pResource, strAbsPath ); if ( !argStream.HasErrors() ) { #ifdef MTA_CLIENT // Inform file verifier g_pClientGame->GetResourceManager ()->OnFileModifedByScript ( strAbsPath, "fileDelete" ); #endif if ( FileDelete ( strAbsPath ) ) { // If file removed successfully, return true lua_pushboolean ( luaVM, true ); return 1; } // Output error "Operation failed @ 'fileDelete' [strInputPath]" argStream.SetCustomError ( strInputPath, "Operation failed" ); } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::SetCameraTarget ( lua_State* luaVM ) { // bool setCameraTarget ( element target = nil ) or setCameraTarget ( float x, float y, float z ) CScriptArgReader argStream ( luaVM ); if ( argStream.NextIsUserData () ) { CClientEntity* pTarget; argStream.ReadUserData ( pTarget ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetCameraTarget ( pTarget ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else { CVector vecTarget; argStream.ReadNumber ( vecTarget.fX ); argStream.ReadNumber ( vecTarget.fY ); argStream.ReadNumber ( vecTarget.fZ ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetCameraTarget ( vecTarget ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileCreate ( lua_State* luaVM ) { // file fileCreate ( string filePath ) SString strFile; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strFile ); if ( argStream.NextIsUserData () ) m_pScriptDebugging->LogCustom ( luaVM, "fileCreate may be using an outdated syntax. Please check and update." ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { // Grab the filename std::string strAbsPath; std::string strSubPath; // We have a resource argument? CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strAbsPath, &strSubPath ) ) { // Do we have permissions? if ( pResource == pThisResource || m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "ModifyOtherObjects", CAccessControlListRight::RIGHT_TYPE_GENERAL, false ) ) { // Make sure the destination folder exist so we can create the file MakeSureDirExists ( strAbsPath.c_str () ); // Create the file to create CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID(), strSubPath.c_str (), DEFAULT_MAX_FILESIZE ); assert ( pFile ); // Try to load it if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) ) { // Add it to the scrpt resource element group CElementGroup* pGroup = pThisResource->GetElementGroup (); if ( pGroup ) { pGroup->Add ( pFile ); } // Success. Return the file. lua_pushelement ( luaVM, pFile ); return 1; } else { // Delete the file again delete pFile; // Output error m_pScriptDebugging->LogWarning ( luaVM, "%s; unable to load file", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) ); } } else m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pResource->GetName ().c_str () ); } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::SetWeaponProperty ( lua_State* luaVM ) { // bool setWeaponProperty ( int weaponID/string weaponName, string weaponSkill, string property/int property, int/float theValue ) eWeaponSkill eWepSkill = WEAPONSKILL_STD; eWeaponType eWep = WEAPONTYPE_BRASSKNUCKLE; eWeaponProperty eProp = WEAPON_ACCURACY; CScriptArgReader argStream ( luaVM ); if ( argStream.NextIsUserData () ) { CCustomWeapon * pWeapon; eWeaponProperty weaponProperty; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pWeapon ); argStream.ReadEnumString ( weaponProperty ); if ( !argStream.HasErrors () ) { if ( weaponProperty == WEAPON_DAMAGE ) { short sData = 0; argStream.ReadNumber ( sData ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponProperty ( pWeapon, weaponProperty, sData ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } else { float fData = 0.0f; argStream.ReadNumber ( fData ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponProperty ( pWeapon, weaponProperty, fData ) ) { lua_pushboolean ( luaVM, true ); return 1; } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); } else { argStream.ReadEnumStringOrNumber ( eWep ); argStream.ReadEnumStringOrNumber ( eWepSkill ); argStream.ReadEnumString ( eProp ); if ( !argStream.HasErrors () ) { switch ( eProp ) { case WEAPON_WEAPON_RANGE: case WEAPON_TARGET_RANGE: case WEAPON_ACCURACY: case WEAPON_MOVE_SPEED: case WEAPON_ANIM_LOOP_START: case WEAPON_ANIM_LOOP_STOP: case WEAPON_ANIM_LOOP_RELEASE_BULLET_TIME: case WEAPON_ANIM2_LOOP_START: case WEAPON_ANIM2_LOOP_STOP: case WEAPON_ANIM2_LOOP_RELEASE_BULLET_TIME: case WEAPON_ANIM_BREAKOUT_TIME: { float fWeaponInfo = 0.0f; argStream.ReadNumber ( fWeaponInfo ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponProperty ( eProp, eWep, eWepSkill, fWeaponInfo ) ) { lua_pushboolean ( luaVM, true ); return 1; } } break; } case WEAPON_DAMAGE: case WEAPON_MAX_CLIP_AMMO: case WEAPON_FLAGS: { int sWeaponInfo = 0; argStream.ReadNumber ( sWeaponInfo ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponProperty ( eProp, eWep, eWepSkill, sWeaponInfo ) ) { lua_pushboolean ( luaVM, true ); return 1; } } break; } case WEAPON_FLAG_AIM_NO_AUTO: case WEAPON_FLAG_AIM_ARM: case WEAPON_FLAG_AIM_1ST_PERSON: case WEAPON_FLAG_AIM_FREE: case WEAPON_FLAG_MOVE_AND_AIM: case WEAPON_FLAG_MOVE_AND_SHOOT: case WEAPON_FLAG_TYPE_THROW: case WEAPON_FLAG_TYPE_HEAVY: case WEAPON_FLAG_TYPE_CONSTANT: case WEAPON_FLAG_TYPE_DUAL: case WEAPON_FLAG_ANIM_RELOAD: case WEAPON_FLAG_ANIM_CROUCH: case WEAPON_FLAG_ANIM_RELOAD_LOOP: case WEAPON_FLAG_ANIM_RELOAD_LONG: case WEAPON_FLAG_SHOT_SLOWS: case WEAPON_FLAG_SHOT_RAND_SPEED: case WEAPON_FLAG_SHOT_ANIM_ABRUPT: case WEAPON_FLAG_SHOT_EXPANDS: { MinServerReqCheck ( argStream, MIN_SERVER_REQ_WEAPON_PROPERTY_FLAG, "flag name is being used" ); bool bEnable; argStream.ReadBool ( bEnable ); if ( !argStream.HasErrors () ) { if ( CStaticFunctionDefinitions::SetWeaponPropertyFlag ( eProp, eWep, eWepSkill, bEnable ) ) { lua_pushboolean ( luaVM, true ); return 1; } } break; } default: { argStream.SetCustomError ( "unsupported weapon property at argument 3" ); break; } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); } // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileOpen ( lua_State* luaVM ) { // file fileOpen ( string filePath [, bool readOnly = false ] ) SString strInputPath; bool bReadOnly; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInputPath ); argStream.ReadBool ( bReadOnly, false ); if ( argStream.NextIsUserData () ) m_pScriptDebugging->LogCustom ( luaVM, "fileOpen may be using an outdated syntax. Please check and update." ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { SString strAbsPath; SString strMetaPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strInputPath, pResource, &strAbsPath, &strMetaPath ) ) { CheckCanModifyOtherResource( argStream, pThisResource, pResource ); CheckCanAccessOtherResourceFile( argStream, pThisResource, pResource, strAbsPath, &bReadOnly ); if ( !argStream.HasErrors() ) { #ifndef MTA_CLIENT // IF SERVER // Create the file to create CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE ); #else eAccessType accessType = strInputPath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC; CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE, accessType ); #endif // Try to load it if ( pFile->Load ( pResource, bReadOnly ? CScriptFile::MODE_READ : CScriptFile::MODE_READWRITE ) ) { #ifdef MTA_CLIENT // Make it a child of the resource's file root pFile->SetParent ( pResource->GetResourceDynamicEntity () ); #endif // Grab its owner resource CResource* pParentResource = pLuaMain->GetResource (); if ( pParentResource ) { // Add it to the scrpt resource element group CElementGroup* pGroup = pParentResource->GetElementGroup (); if ( pGroup ) { pGroup->Add ( pFile ); } } // Success. Return the file. lua_pushelement ( luaVM, pFile ); return 1; } else { // Delete the file again delete pFile; // Output error argStream.SetCustomError ( SString ( "unable to load file '%s'", *strInputPath ) ); } } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFileDefs::fileCreate ( lua_State* luaVM ) { // file fileCreate ( string filePath ) SString strInputPath; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInputPath ); if ( argStream.NextIsUserData () ) m_pScriptDebugging->LogCustom ( luaVM, "fileCreate may be using an outdated syntax. Please check and update." ); if ( !argStream.HasErrors () ) { // Grab our lua VM CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( !pLuaMain ) { // Failed lua_pushboolean ( luaVM, false ); return 1; } #ifdef MTA_CLIENT if ( !g_pNet->ValidateBinaryFileName ( strInputPath ) ) { argStream.SetCustomError ( SString ( "Filename not allowed %s", *strInputPath ), "File error" ); m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; } #endif // MTA_CLIENT SString strAbsPath; SString strMetaPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pResource = pThisResource; if ( CResourceManager::ParseResourcePathInput ( strInputPath, pResource, &strAbsPath, &strMetaPath ) ) { CheckCanModifyOtherResource( argStream, pThisResource, pResource ); CheckCanAccessOtherResourceFile( argStream, pThisResource, pResource, strAbsPath ); if ( !argStream.HasErrors() ) { #ifdef MTA_CLIENT // Inform file verifier g_pClientGame->GetResourceManager ()->OnFileModifedByScript ( strAbsPath, "fileCreate" ); #endif // Make sure the destination folder exist so we can create the file MakeSureDirExists ( strAbsPath ); // Create the file to create #ifdef MTA_CLIENT eAccessType accessType = strInputPath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC; CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE, accessType ); #else CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID (), strMetaPath, DEFAULT_MAX_FILESIZE ); #endif // Try to load it if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) ) { #ifdef MTA_CLIENT // Make it a child of the resource's file root pFile->SetParent ( pResource->GetResourceDynamicEntity () ); #endif // Add it to the scrpt resource element group CElementGroup* pGroup = pThisResource->GetElementGroup (); if ( pGroup ) { pGroup->Add ( pFile ); } // Success. Return the file. lua_pushelement ( luaVM, pFile ); return 1; } else { // Delete the file again delete pFile; // Output error argStream.SetCustomError ( SString ( "Unable to create %s", *strInputPath ), "File error" ); } } } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); // Failed lua_pushboolean ( luaVM, false ); return 1; }