int CLuaFunctionDefs::XMLCreateFile ( lua_State* luaVM ) { // xmlnode xmlCreateFile ( string filePath, string rootNodeName ) SString filePath; SString rootNodeName; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( filePath ); argStream.ReadString ( rootNodeName ); // Safety check: Don't allow the rootNodeName "private" incase user forget to declare a node name if ( rootNodeName == EnumToString ( ACCESS_PRIVATE ) ) { argStream.SetCustomError( "Expected string at argument 2, got access-type" ); } if ( !argStream.HasErrors () ) { CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM); if ( pLuaMain ) { CResource* pResource = pLuaMain->GetResource(); SString strFile; if ( CResourceManager::ParseResourcePathInput( filePath, pResource, strFile ) ) { // Make sure the directory exists MakeSureDirExists ( strFile.c_str () ); // Create the XML CXMLFile * xmlFile = pLuaMain->CreateXML ( strFile.c_str () ); if ( xmlFile ) { // Create its root node CXMLNode* pRootNode = xmlFile->CreateRootNode ( rootNodeName ); if ( pRootNode ) { lua_pushxmlnode ( luaVM, pRootNode ); return 1; } // Delete the XML again pLuaMain->DestroyXML ( xmlFile ); } } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::XMLLoadFile ( lua_State* luaVM ) { // xmlnode xmlLoadFile ( string filePath ) SString filePath; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( filePath ); if ( !argStream.HasErrors () ) { CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( luaMain ) { CResource* pResource = luaMain->GetResource(); SString strFilename; if ( CResourceManager::ParseResourcePathInput( filePath, pResource, strFilename ) ) { // Create the XML CXMLFile * xmlFile = luaMain->CreateXML ( strFilename ); if ( xmlFile ) { // Parse it if ( xmlFile->Parse() ) { // Create the root node if it doesn't exist CXMLNode* pRootNode = xmlFile->GetRootNode (); if ( !pRootNode ) pRootNode = xmlFile->CreateRootNode ( "root" ); // Got a root node? if ( pRootNode ) { lua_pushxmlnode ( luaVM, pRootNode ); return 1; } } // Destroy the XML luaMain->DestroyXML ( xmlFile ); } } } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); lua_pushboolean ( luaVM, false ); return 1; }
int CLuaXMLDefs::xmlCreateFile ( lua_State* luaVM ) { #ifndef MTA_CLIENT if ( lua_type ( luaVM, 3 ) == LUA_TLIGHTUSERDATA ) m_pScriptDebugging->LogCustom ( luaVM, "xmlCreateFile may be using an outdated syntax. Please check and update." ); #endif // !MTA_CLIENT // Grab our resource CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLuaMain ) { SString strInputPath, strRootNodeName; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strInputPath ); argStream.ReadString ( strRootNodeName ); if ( !argStream.HasErrors () ) { SString strPath; CResource* pThisResource = pLuaMain->GetResource (); CResource* pOtherResource = pThisResource; // clientside, this variable will always be pThisResource // Resolve other resource from name if ( CResourceManager::ParseResourcePathInput ( strInputPath, pOtherResource, &strPath, nullptr ) ) { #ifndef MTA_CLIENT // We have access to modify other resource? if ( pOtherResource == pThisResource || m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "ModifyOtherObjects", CAccessControlListRight::RIGHT_TYPE_GENERAL, false ) ) #endif // !MTA_CLIENT { // Make sure the dir exists so we can successfully make the file MakeSureDirExists ( strPath ); // Create the XML file CXMLFile * xmlFile = pLuaMain->CreateXML ( strPath ); if ( xmlFile ) { // Create its root node CXMLNode* pRootNode = xmlFile->CreateRootNode ( strRootNodeName ); if ( pRootNode ) { lua_pushxmlnode ( luaVM, pRootNode ); return 1; } // Destroy it if we failed pLuaMain->DestroyXML ( xmlFile ); } } #ifndef MTA_CLIENT else argStream.SetCustomError ( SString ( "ModifyOtherObjects in ACL denied resource '%s' to access '%s'", pThisResource->GetName ().c_str (), pOtherResource->GetName ().c_str () ), "Access denied" ); #endif // !MTA_CLIENT } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); } lua_pushboolean ( luaVM, false ); return 1; }
int CLuaXMLDefs::xmlCopyFile ( lua_State* luaVM ) { #ifndef MTA_CLIENT if ( lua_type ( luaVM, 3 ) == LUA_TLIGHTUSERDATA ) m_pScriptDebugging->LogCustom ( luaVM, "xmlCopyFile may be using an outdated syntax. Please check and update." ); #endif // !MTA_CLIENT // Grab our resource CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLUA ) { SString strFile; CXMLNode* pSourceNode; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pSourceNode ); argStream.ReadString ( strFile ); if ( !argStream.HasErrors () ) { SString strPath; CResource* pThisResource = pLUA->GetResource (); CResource* pOtherResource = pThisResource; // Resolve other resource from name if ( CResourceManager::ParseResourcePathInput ( strFile, pOtherResource, &strPath, NULL ) ) { #ifndef MTA_CLIENT // We have access to modify other resource? if ( pOtherResource == pThisResource || m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (), CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE, "ModifyOtherObjects", CAccessControlListRight::RIGHT_TYPE_GENERAL, false ) ) #endif // !MTA_CLIENT { if ( pSourceNode ) { // Make sure the dir exists so we can successfully make the file MakeSureDirExists ( strPath ); // Grab the roots tag name std::string strRootTagName; strRootTagName = pSourceNode->GetTagName (); // Create the new XML file and its root node CXMLFile* pNewXML = pLUA->CreateXML ( strPath.c_str () ); if ( pNewXML ) { // Grab the root of the new XML CXMLNode* pNewRoot = pNewXML->CreateRootNode ( strRootTagName ); if ( pNewRoot ) { // Copy over the attributes from the root int iAttributeCount = pSourceNode->GetAttributes ().Count (); int i = 0; CXMLAttribute* pAttribute; for ( ; i < iAttributeCount; i++ ) { pAttribute = pSourceNode->GetAttributes ().Get ( i ); if ( pAttribute ) pNewRoot->GetAttributes ().Create ( *pAttribute ); } // Copy the stuff from the given source node to the destination root if ( pSourceNode->CopyChildrenInto ( pNewRoot, true ) ) { lua_pushxmlnode ( luaVM, pNewRoot ); return 1; } } // Delete the XML again pLUA->DestroyXML ( pNewXML ); } } else argStream.SetCustomError ( SString ( "Unable to copy XML file %s", strFile.c_str () ), "Bad filepath" ); } #ifndef MTA_CLIENT else argStream.SetCustomError ( SString ( "ModifyOtherObjects in ACL denied resource '%s' to access '%s'", pThisResource->GetName ().c_str (), pOtherResource->GetName ().c_str () ), "Access denied" ); #endif } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); } // Error lua_pushboolean ( luaVM, false ); return 1; }
int CLuaFunctionDefs::XMLCopyFile ( lua_State* luaVM ) { // xmlnode xmlCopyFile ( xmlnode nodeToCopy, string newFilePath ) CXMLNode* pSourceNode; SString newFilePath; CScriptArgReader argStream ( luaVM ); argStream.ReadUserData ( pSourceNode ); argStream.ReadString ( newFilePath ); if ( !argStream.HasErrors () ) { // Grab the virtual machine for this luastate CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLUA ) { CResource* pResource = pLUA->GetResource(); SString strFilename; if ( CResourceManager::ParseResourcePathInput( newFilePath, pResource, strFilename ) ) { if ( pSourceNode ) { // Grab the roots tag name std::string strRootTagName; strRootTagName = pSourceNode->GetTagName (); // Grab our lua VM CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); // Create the new XML file and its root node CXMLFile* pNewXML = pLUA->CreateXML ( strFilename ); if ( pNewXML ) { // Create root for new XML CXMLNode* pNewRoot = pNewXML->CreateRootNode ( strRootTagName ); if ( pNewRoot ) { // Copy over the attributes from the root int iAttributeCount = pSourceNode->GetAttributes ().Count (); int i = 0; CXMLAttribute* pAttribute; for ( ; i < iAttributeCount; i++ ) { pAttribute = pSourceNode->GetAttributes ().Get ( i ); if ( pAttribute ) pNewRoot->GetAttributes ().Create ( *pAttribute ); } // Copy the stuff from the given source node to the destination root if ( pSourceNode->CopyChildrenInto ( pNewRoot, true ) ) { lua_pushxmlnode ( luaVM, pNewRoot ); return 1; } } // Delete the XML again pLUA->DestroyXML ( pNewXML ); } } } else CLogger::ErrorPrintf ( "Unable to copy xml file; bad filepath" ); } } else m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() ); // Error lua_pushboolean ( luaVM, false ); return 1; }
int CLuaXMLDefs::xmlLoadFile ( lua_State* luaVM ) { if ( lua_type ( luaVM, 2 ) == LUA_TLIGHTUSERDATA ) m_pScriptDebugging->LogCustom ( luaVM, "xmlLoadFile may be using an outdated syntax. Please check and update." ); // Grab our resource CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLUA ) { SString strFile; CScriptArgReader argStream ( luaVM ); argStream.ReadString ( strFile ); if ( !argStream.HasErrors () ) { SString strPath; CResource* pThisResource = pLUA->GetResource (); CResource* pOtherResource = pThisResource; // Resolve other resource from name if ( CResourceManager::ParseResourcePathInput ( strFile, pOtherResource, &strPath, NULL ) ) { // We have access to modify other resource? if ( pOtherResource == 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 successfully make the file MakeSureDirExists ( strPath ); // Create the XML CXMLFile* xmlFile = pLUA->CreateXML ( strPath.c_str () ); if ( xmlFile ) { // Try to parse it if ( xmlFile->Parse () ) { // Grab the root node. If it didn't exist, create one CXMLNode * pRootNode = xmlFile->GetRootNode (); if ( !pRootNode ) pRootNode = xmlFile->CreateRootNode ( "root" ); // Could we create one? if ( pRootNode ) { // Return the root node lua_pushxmlnode ( luaVM, pRootNode ); return 1; } } // Destroy it if we failed pLUA->DestroyXML ( xmlFile ); } } else argStream.SetCustomError( SString( "ModifyOtherObjects in ACL denied resource '%s' to access '%s'", pThisResource->GetName ().c_str (), pOtherResource->GetName ().c_str () ), "Access denied" ); } } if ( argStream.HasErrors () ) m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () ); } lua_pushboolean ( luaVM, false ); return 1; }
int CLuaXMLDefs::xmlCreateFile ( lua_State* luaVM ) { if ( lua_type ( luaVM, 3 ) == LUA_TLIGHTUSERDATA ) m_pScriptDebugging->LogCustom ( luaVM, "xmlCreateFile may be using an outdated syntax. Please check and update." ); // Grab our resource CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLUA ) { CResource* pThisResource = pLUA->GetResource (); CResource* pResource = pThisResource; // Filename if ( ( lua_type ( luaVM, 1 ) != LUA_TSTRING ) || ( lua_type ( luaVM, 2 ) != LUA_TSTRING ) ) { m_pScriptDebugging->LogBadType ( luaVM, "xmlCreateFile" ); lua_pushboolean ( luaVM, false ); return 1; } else { std::string strFile = lua_tostring ( luaVM, 1 ); std::string strPath; if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strPath, NULL ) ) { // We have access to modify this resource? 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 successfully make the file MakeSureDirExists ( strPath.c_str () ); // Grab the root const char* szRootName = lua_tostring ( luaVM, 2 ); // Create the XML file CXMLFile * xmlFile = pLUA->CreateXML ( strPath.c_str () ); if ( xmlFile ) { // Create its root node CXMLNode* pRootNode = xmlFile->CreateRootNode ( szRootName ); if ( pRootNode ) { lua_pushxmlnode ( luaVM, pRootNode ); return 1; } // Delete it again pLUA->DestroyXML ( xmlFile ); } } } } } lua_pushboolean ( luaVM, false ); return 1; }
int CLuaXMLDefs::xmlCopyFile ( lua_State* luaVM ) { if ( lua_type ( luaVM, 3 ) == LUA_TLIGHTUSERDATA ) m_pScriptDebugging->LogCustom ( luaVM, "xmlCopyFile may be using an outdated syntax. Please check and update." ); // Grab our resource CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLUA ) { CResource* pThisResource = pLUA->GetResource (); CResource* pResource = pThisResource; // Verify the argument types passed if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA && lua_type ( luaVM, 2 ) == LUA_TSTRING ) { // Grab the filename passed std::string strFile = lua_tostring ( luaVM, 2 ); std::string strPath; if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strPath, NULL ) ) { // We have access to modify this resource? 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 successfully make the file MakeSureDirExists ( strPath.c_str () ); // Grab the source node CXMLNode* pSourceNode = lua_toxmlnode ( luaVM, 1 ); if ( pSourceNode ) { // Grab the roots tag name std::string strRootTagName; strRootTagName = pSourceNode->GetTagName (); // Create the new XML file and its root node CXMLFile* pNewXML = pLUA->CreateXML ( strPath.c_str () ); if ( pNewXML ) { // Grab the root of the new XML CXMLNode* pNewRoot = pNewXML->CreateRootNode ( strRootTagName ); if ( pNewRoot ) { // Copy over the attributes from the root int iAttributeCount = pSourceNode->GetAttributes ().Count (); int i = 0; CXMLAttribute* pAttribute; for ( ; i < iAttributeCount; i++ ) { pAttribute = pSourceNode->GetAttributes ().Get ( i ); if ( pAttribute ) pNewRoot->GetAttributes ().Create ( *pAttribute ); } // Copy the stuff from the given source node to the destination root if ( pSourceNode->CopyChildrenInto ( pNewRoot, true ) ) { lua_pushxmlnode ( luaVM, pNewRoot ); return 1; } } // Delete the XML again pLUA->DestroyXML ( pNewXML ); } } else CLogger::ErrorPrintf ( "Unable to copy xml file; bad filepath" ); } else m_pScriptDebugging->LogError ( luaVM,"xmlCopyFile failed; ModifyOtherObjects in ACL denied resource %s to access %s", pThisResource->GetName ().c_str (), pResource->GetName ().c_str () ); } } else m_pScriptDebugging->LogBadType ( luaVM, "xmlCopyFile" ); } // Error lua_pushboolean ( luaVM, false ); return 1; }
int CLuaXMLDefs::xmlLoadFile ( lua_State* luaVM ) { if ( lua_type ( luaVM, 2 ) == LUA_TLIGHTUSERDATA ) m_pScriptDebugging->LogCustom ( luaVM, "xmlLoadFile may be using an outdated syntax. Please check and update." ); // Grab our resource CLuaMain* pLUA = m_pLuaManager->GetVirtualMachine ( luaVM ); if ( pLUA ) { CResource* pThisResource = pLUA->GetResource (); CResource* pResource = pThisResource; // Filename if ( lua_type ( luaVM, 1 ) != LUA_TSTRING ) { m_pScriptDebugging->LogBadType ( luaVM, "xmlLoadFile" ); lua_pushboolean ( luaVM, false ); return 1; } // Grab the filename passed std::string strFile = lua_tostring ( luaVM, 1 ); std::string strPath; if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strPath, NULL ) ) { 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 successfully make the file MakeSureDirExists ( strPath.c_str () ); // Create the XML CXMLFile* xmlFile = pLUA->CreateXML ( strPath.c_str () ); if ( xmlFile ) { // Try to parse it if ( xmlFile->Parse () ) { // Grab the root node. If it didn't exist, create one CXMLNode * pRootNode = xmlFile->GetRootNode (); if ( !pRootNode ) pRootNode = xmlFile->CreateRootNode ( "root" ); // Could we create one? if ( pRootNode ) { // Return the root node lua_pushxmlnode ( luaVM, pRootNode ); return 1; } } // Destroy it if we failed pLUA->DestroyXML ( xmlFile ); } } else m_pScriptDebugging->LogError ( luaVM, "xmlLoadFile failed; ModifyOtherObjects in ACL denied resource %s to access %s", pThisResource->GetName ().c_str (), pResource->GetName ().c_str () ); } } lua_pushboolean ( luaVM, false ); return 1; }