Example #1
0
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 )
    {
        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 ) )
            {
                // 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 );

                    // 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( "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 () );
    }

    // Error
    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::SetBrowserAjaxHandler ( lua_State* luaVM )
{
    //  bool setBrowserAjaxHandler ( browser browser, string URL[, function callback] )
    CClientWebBrowser* pWebBrowser; SString strURL; CLuaFunctionRef callbackFunction;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pWebBrowser );
    argStream.ReadString ( strURL );

    if ( argStream.NextIsNil () || argStream.NextIsNone () )
    {
        if ( !argStream.HasErrors () )
        {
            lua_pushboolean ( luaVM, pWebBrowser->RemoveAjaxHandler ( strURL ) );
            return 1;
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
    }
    else
    {
        argStream.ReadFunction ( callbackFunction );
        argStream.ReadFunctionComplete ();
        if ( !argStream.HasErrors () )
        {
            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain && VERIFY_FUNCTION ( callbackFunction ) )
            {
                CResource* pResource = pLuaMain->GetResource ();
                CResourceManager * pResourceManager = m_pResourceManager;
                auto netId = pResource->GetNetID ();

                bool bResult = pWebBrowser->AddAjaxHandler ( strURL, 
                [=] ( std::vector<SString>& vecGet, std::vector<SString>& vecPost ) -> const SString
                {
                    // Make sure the resource is still running
                    if ( !pResourceManager->Exists ( pResource ) || pResource->GetNetID() != netId )
                    {
                        return "";
                    }

                    // Make sure the function is valid
                    if ( VERIFY_FUNCTION ( callbackFunction ) )
                    {
                        CLuaArguments arguments;
                        CLuaArguments getArguments;
                        CLuaArguments postArguments;

                        for ( auto&& param : vecGet )
                            getArguments.PushString ( param );

                        for ( auto&& param : vecPost )
                            postArguments.PushString ( param );

                        arguments.PushTable ( &getArguments );
                        arguments.PushTable ( &postArguments );

                        CLuaArguments result;
                        
                        arguments.Call ( pLuaMain, callbackFunction, &result );

                        if ( result.Count () == 0 )
                            return "";


                        CLuaArgument* returnedValue = *result.IterBegin ();
                        if ( returnedValue->GetType () == LUA_TSTRING )                       
                            return returnedValue->GetString ();
                        else
                            return "";
                    }
                    else
                        return "";

                } );

                lua_pushboolean ( luaVM, bResult );
                return 1;
            }
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #3
0
int CLuaTextDefs::textCreateTextItem ( lua_State* luaVM )
{
    
    SString strText, strHorzAlign, strVertAlign;
    float fX, fY, fScale;
    int iPriority;
    SColorRGBA color ( 255, 255, 255, 255 );
    unsigned char ucShadowAlpha;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strText, "" );
    argStream.ReadNumber ( fX, 0.5f );
    argStream.ReadNumber ( fY, 0.5f );

    if ( argStream.NextIsString( ) )
    {
        SString strPriority;
        argStream.ReadString ( strPriority );

        if ( strPriority == "high" )        iPriority = PRIORITY_HIGH;
        else if ( strPriority == "medium" ) iPriority = PRIORITY_MEDIUM;
        else                                iPriority = PRIORITY_LOW;

    }
    else
    {
        argStream.ReadNumber(iPriority, PRIORITY_LOW);
    }
    
    argStream.ReadNumber( color.R, 255 );
    argStream.ReadNumber( color.G, 255 );
    argStream.ReadNumber( color.B, 255 );
    argStream.ReadNumber( color.A, 255 );
    argStream.ReadNumber( fScale, 1 );
    argStream.ReadString( strHorzAlign, "left" );
    argStream.ReadString( strVertAlign, "top" );
    argStream.ReadNumber( ucShadowAlpha, 0);

    if ( !argStream.HasErrors ( ) )
    {
        unsigned char ucFormat = 0; 
        if ( strHorzAlign == "center" ) 
            ucFormat |= 0x00000001; // DT_CENTER
        else if ( strHorzAlign == "right" )
            ucFormat |= 0x00000002; // DT_RIGHT

        if ( strVertAlign == "center" )
            ucFormat |= 0x00000004; // DT_VCENTER
        else if ( strVertAlign == "bottom" )
            ucFormat |= 0x00000008; // DT_BOTTOM
        
        // Grab our virtual machine
        CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( luaMain )
        {
            CTextItem* pTextItem = luaMain->CreateTextItem ( strText, fX, fY, (eTextPriority) iPriority, color, fScale, ucFormat, ucShadowAlpha );
            lua_pushtextitem ( luaVM, pTextItem );
            return 1;
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );


    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #4
0
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;
}
Example #5
0
int CLuaFunctionDefs::CreateRadarArea ( lua_State* luaVM )
{
    int iArgument1 = lua_type ( luaVM, 1 );
    int iArgument2 = lua_type ( luaVM, 2 );
    int iArgument3 = lua_type ( luaVM, 3 );
    int iArgument4 = lua_type ( luaVM, 4 );
    int iArgument5 = lua_type ( luaVM, 5 );
    int iArgument6 = lua_type ( luaVM, 6 );
    int iArgument7 = lua_type ( luaVM, 7 );
    int iArgument8 = lua_type ( luaVM, 8 );

    if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
        ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
        ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
        ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
        ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING || iArgument5 == LUA_TNONE ) &&
        ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING || iArgument6 == LUA_TNONE ) &&
        ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING || iArgument7 == LUA_TNONE ) &&
        ( iArgument8 == LUA_TNUMBER || iArgument8 == LUA_TSTRING || iArgument8 == LUA_TNONE ) )
    {
        CVector2D vecPosition = CVector2D ( static_cast < float > ( lua_tonumber ( luaVM, 1 ) ), static_cast < float > ( lua_tonumber ( luaVM, 2 ) ) );
        CVector2D vecSize = CVector2D ( static_cast < float > ( lua_tonumber ( luaVM, 3 ) ), static_cast < float > ( lua_tonumber ( luaVM, 4 ) ) );

        double dRed = 255;
        double dGreen = 0;
        double dBlue = 0;
        double dAlpha = 255;
        if ( iArgument5 != LUA_TNONE && iArgument6 != LUA_TNONE && iArgument7 != LUA_TNONE && iArgument8 != LUA_TNONE )
        {
            dRed = lua_tonumber ( luaVM, 5 );
            dGreen = lua_tonumber ( luaVM, 6 );
            dBlue = lua_tonumber ( luaVM, 7 );
            dAlpha = lua_tonumber ( luaVM, 8 );
        }


        if ( dRed >= 0 && dRed <= 255 &&
            dGreen >= 0 && dGreen <= 255 &&
            dBlue >= 0 && dBlue <= 255 &&
            dAlpha >= 0 && dAlpha <= 255 )
        {     
            SColor color;
            color.R = static_cast < unsigned char > ( dRed );
            color.G = static_cast < unsigned char > ( dGreen );
            color.B = static_cast < unsigned char > ( dBlue );
            color.A = static_cast < unsigned char > ( dAlpha );

            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResource* pResource = pLuaMain->GetResource ();
                if ( pResource )
                {
                    // Create it
                    CClientRadarArea* pRadarArea = CStaticFunctionDefinitions::CreateRadarArea ( *pResource, vecPosition, vecSize, color );
                    if ( pRadarArea )
                    {
                        CElementGroup * pGroup = pResource->GetElementGroup();
                        if ( pGroup )
                        {
                            pGroup->Add ( ( CClientEntity* ) pRadarArea );
                        }
                        lua_pushelement ( luaVM, pRadarArea );
                        return 1;
                    }
                }
            }
        }
        else
            m_pScriptDebugging->LogWarning ( luaVM, "Bad color number sent to createRadarArea (0-255)" );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "createRadarArea" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #6
0
int CLuaFunctionDefs::CreateWater ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResource* pResource = pLuaMain->GetResource ();
        if ( pResource )
        {
            int iArgument1  = lua_type ( luaVM, 1 );
            int iArgument2  = lua_type ( luaVM, 2 );
            int iArgument3  = lua_type ( luaVM, 3 );
            int iArgument4  = lua_type ( luaVM, 4 );
            int iArgument5  = lua_type ( luaVM, 5 );
            int iArgument6  = lua_type ( luaVM, 6 );
            int iArgument7  = lua_type ( luaVM, 7 );
            int iArgument8  = lua_type ( luaVM, 8 );
            int iArgument9  = lua_type ( luaVM, 9 );
            int iArgument10 = lua_type ( luaVM, 10 );
            int iArgument11 = lua_type ( luaVM, 11 );
            int iArgument12 = lua_type ( luaVM, 12 );
            if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
                ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
                ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
                ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
                ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING ) &&
                ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING ) &&
                ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING ) &&
                ( iArgument8 == LUA_TNUMBER || iArgument8 == LUA_TSTRING ) &&
                ( iArgument9 == LUA_TNUMBER || iArgument9 == LUA_TSTRING ) )
            {
                CVector v1 ( (float)lua_tonumber(luaVM, 1), (float)lua_tonumber(luaVM, 2), (float)lua_tonumber(luaVM, 3) );
                CVector v2 ( (float)lua_tonumber(luaVM, 4), (float)lua_tonumber(luaVM, 5), (float)lua_tonumber(luaVM, 6) );
                CVector v3 ( (float)lua_tonumber(luaVM, 7), (float)lua_tonumber(luaVM, 8), (float)lua_tonumber(luaVM, 9) );
                if ( ( iArgument10 == LUA_TNUMBER || iArgument10 == LUA_TSTRING ) &&
                    ( iArgument11 == LUA_TNUMBER || iArgument11 == LUA_TSTRING ) &&
                    ( iArgument12 == LUA_TNUMBER || iArgument12 == LUA_TSTRING ) )
                {
                    CVector v4 ( (float)lua_tonumber(luaVM, 10), (float)lua_tonumber(luaVM, 11), (float)lua_tonumber(luaVM, 12) );
                    bool bShallow = false;
                    if ( lua_type ( luaVM, 13 ) == LUA_TBOOLEAN && lua_toboolean ( luaVM, 13 ) )
                        bShallow = true;
                    lua_pushelement ( luaVM, CStaticFunctionDefinitions::CreateWater (
                        *pResource, &v1, &v2, &v3, &v4, bShallow ) );
                    return 1;
                }
                else
                {
                    bool bShallow = false;
                    if ( lua_type ( luaVM, 10 ) == LUA_TBOOLEAN && lua_toboolean ( luaVM, 10 ) )
                        bShallow = true;
                    lua_pushelement ( luaVM, CStaticFunctionDefinitions::CreateWater (
                        *pResource, &v1, &v2, &v3, NULL, bShallow ) );
                    return 1;
                }
            }
            else
                m_pScriptDebugging->LogBadType ( luaVM, "createWater" );
        }
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #7
0
int CLuaFileDefs::fileRename ( lua_State* luaVM )
{
//  bool fileRename ( string filePath, string newFilePath )
    SString strInputSrcPath; SString strInputDestPath;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strInputSrcPath );
    argStream.ReadString ( strInputDestPath );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( !pLuaMain )
        {
            lua_pushboolean ( luaVM, false );
            return 1;
        }

#ifdef MTA_CLIENT
        if ( !g_pNet->ValidateBinaryFileName ( strInputDestPath ) )
        {
            argStream.SetCustomError ( SString ( "Filename not allowed %s", *strInputDestPath ), "File error" );
            m_pScriptDebugging->LogError ( luaVM, argStream.GetFullErrorMessage () );

            lua_pushboolean ( luaVM, false );
            return 1;
        }
#endif

        // absPath: the real absolute path to the file
        // metaPath: path relative to the target resource (as would be defined in the meta.xml file)
        SString strSrcAbsPath; SString strDestAbsPath;

        CResource* pThisResource = pLuaMain->GetResource ();
        CResource* pSrcResource = pThisResource;
        CResource* pDestResource = pThisResource;

        if ( CResourceManager::ParseResourcePathInput ( strInputSrcPath, pSrcResource, &strSrcAbsPath ) &&
                CResourceManager::ParseResourcePathInput ( strInputDestPath, pDestResource, &strDestAbsPath ) )
        {
            CheckCanModifyOtherResource( argStream, pThisResource, pSrcResource, pDestResource );
            CheckCanAccessOtherResourceFile( argStream, pThisResource, pSrcResource, strSrcAbsPath );
            CheckCanAccessOtherResourceFile( argStream, pThisResource, pDestResource, strDestAbsPath );
            if ( !argStream.HasErrors() )
            {
                // Does `current` file path exist and `new` file path doesn't exist?
                if ( FileExists(strSrcAbsPath) )
                {
                    if ( !FileExists ( strDestAbsPath ) ) {
#ifdef MTA_CLIENT
                        // Inform file verifier
                        g_pClientGame->GetResourceManager ()->OnFileModifedByScript ( strSrcAbsPath, "fileRename" );
#endif
                        // Make sure the destination folder exists so we can move the file
                        MakeSureDirExists ( strDestAbsPath );

                        if ( FileRename ( strSrcAbsPath, strDestAbsPath ) )
                        {
                            // If file renamed/moved return success
                            lua_pushboolean ( luaVM, true );
                            return 1;
                        }

                        // Output error
                        m_pScriptDebugging->LogWarning ( luaVM, "fileRename failed; unable to rename file" );
                    }
                    else
                    {
                        m_pScriptDebugging->LogWarning ( luaVM, "fileRename failed; destination file already exists" );
                    }
                }
                else
                {
                    m_pScriptDebugging->LogWarning ( luaVM, "fileRename failed; source file doesn't exist" );
                }
            }
        }
    }
    
    if ( argStream.HasErrors () )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #8
0
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;
}
Example #9
0
int CLuaResourceDefs::Call ( lua_State* luaVM )
{
    CResource * pResource = NULL;
    SString strFunctionName = "";
    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pResource );
    argStream.ReadString ( strFunctionName );
    if ( !argStream.HasErrors () )
    {
        // Grab our VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab this resource
            CResource* pThisResource = pLuaMain->GetResource ();
            if ( pThisResource )
            {
                //Get the target Lua VM
                lua_State* targetLuaVM = pResource->GetVM ()->GetVM ();

                // Read out the vargs
                CLuaArguments args;
                args.ReadArguments ( luaVM, 3 );
                CLuaArguments returns;

                LUA_CHECKSTACK ( targetLuaVM, 1 );   // Ensure some room

                                                        //Lets grab the original hidden variables so we can restore them later
                lua_getglobal ( targetLuaVM, "sourceResource" );
                CLuaArgument OldResource ( luaVM, -1 );
                lua_pop ( targetLuaVM, 1 );

                lua_getglobal ( targetLuaVM, "sourceResourceRoot" );
                CLuaArgument OldResourceRoot ( luaVM, -1 );
                lua_pop ( targetLuaVM, 1 );

                //Set the new values for the current sourceResource, and sourceResourceRoot
                lua_pushresource ( targetLuaVM, pThisResource );
                lua_setglobal ( targetLuaVM, "sourceResource" );

                lua_pushelement ( targetLuaVM, pThisResource->GetResourceEntity () );
                lua_setglobal ( targetLuaVM, "sourceResourceRoot" );

                // Call the exported function with the given name and the args
                if ( pResource->CallExportedFunction ( strFunctionName, args, returns, *pThisResource ) )
                {
                    // Push return arguments
                    returns.PushArguments ( luaVM );
                    //Restore the old variables
                    OldResource.Push ( targetLuaVM );
                    lua_setglobal ( targetLuaVM, "sourceResource" );

                    OldResourceRoot.Push ( targetLuaVM );
                    lua_setglobal ( targetLuaVM, "sourceResourceRoot" );

                    return returns.Count ();
                }
                else
                {
                    //Restore the old variables
                    OldResource.Push ( targetLuaVM );
                    lua_setglobal ( targetLuaVM, "sourceResource" );

                    OldResourceRoot.Push ( targetLuaVM );
                    lua_setglobal ( targetLuaVM, "sourceResourceRoot" );
                    m_pScriptDebugging->LogError ( luaVM, "call: failed to call '%s:%s'", pResource->GetName (), *strFunctionName );
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );


    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #10
0
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;
}
Example #11
0
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;
}
Example #12
0
///////////////////////////////////////////////////////////////
//
// CPerfStatLuaMemoryImpl::GetLuaMemoryStats
//
//
//
///////////////////////////////////////////////////////////////
void CPerfStatLuaMemoryImpl::GetLuaMemoryStats ( CPerfStatResult* pResult, const std::map < SString, int >& strOptionMap, const SString& strFilter )
{
    //
    // Set option flags
    //
    bool bHelp = MapContains ( strOptionMap, "h" );
    bool bAccurate = MapContains ( strOptionMap, "a" );

    //
    // Process help
    //
    if ( bHelp )
    {
        pResult->AddColumn ( "Lua memory help" );
        pResult->AddRow ()[0] ="Option h - This help";
        pResult->AddRow ()[0] ="Option a - More accurate memory usage - Warning: Can slow server a little";
        return;
    }


    // Fetch mem stats from Lua
    {
        for ( std::map < CLuaMain*, int >::iterator iter = m_LuaMainMap.begin () ; iter != m_LuaMainMap.end () ; ++iter )
        {
            CLuaMain* pLuaMain = iter->first;
            if ( pLuaMain->GetVM() )
            {
                if ( bAccurate )
                    lua_gc(pLuaMain->GetVM(), LUA_GCCOLLECT, 0);

                int iMemUsed = lua_getgccount( pLuaMain->GetVM() );
                UpdateLuaMemory ( pLuaMain, iMemUsed );
            }
        }
    }

    pResult->AddColumn ( "name" );
    pResult->AddColumn ( "change" );
    pResult->AddColumn ( "current" );
    pResult->AddColumn ( "max" );
    pResult->AddColumn ( "XMLFiles" );
    pResult->AddColumn ( "OpenFiles" );
    pResult->AddColumn ( "refs" );
    pResult->AddColumn ( "Timers" );
    pResult->AddColumn ( "Elements" );
    pResult->AddColumn ( "TextDisplays" );
    pResult->AddColumn ( "TextItems" );
    pResult->AddColumn ( "DB Queries" );
    pResult->AddColumn ( "DB Connections" );

    // Calc totals
    if ( strFilter == "" )
    {
        int calcedCurrent = 0;
        int calcedDelta = 0;
        int calcedMax = 0;
        for ( CLuaMainMemoryMap::iterator iter = AllLuaMemory.LuaMainMemoryMap.begin () ; iter != AllLuaMemory.LuaMainMemoryMap.end () ; ++iter )
        {
            CLuaMainMemory& LuaMainMemory = iter->second;
            calcedCurrent += LuaMainMemory.Current;
            calcedDelta += LuaMainMemory.Delta;
            calcedMax += LuaMainMemory.Max;
        }

        // Add row
        SString* row = pResult->AddRow ();

        int c = 0;
        row[c++] = "Lua VM totals";

        if ( labs(calcedDelta) >= 1 )
        {
            row[c] = SString ( "%d KB", calcedDelta );
            calcedDelta = 0;
        }
        c++;

        row[c++] = SString ( "%d KB", calcedCurrent );
        row[c++] = SString ( "%d KB", calcedMax );

        // Some extra 'all VM' things
        c += 6;
        row[c++] = !g_pStats->iDbJobDataCount ? "-" : SString ( "%d", g_pStats->iDbJobDataCount );
        row[c++] = g_pStats->iDbConnectionCount - 2 == 0 ? "-" : SString ( "%d", g_pStats->iDbConnectionCount - 2 );
    }

    // For each VM
    for ( CLuaMainMemoryMap::iterator iter = AllLuaMemory.LuaMainMemoryMap.begin () ; iter != AllLuaMemory.LuaMainMemoryMap.end () ; ++iter )
    {
        CLuaMainMemory& LuaMainMemory = iter->second;
        const SString& strResName = iter->first->GetScriptName ();

        // Apply filter
        if ( strFilter != "" && strResName.find ( strFilter ) == SString::npos )
            continue;

        // Add row
        SString* row = pResult->AddRow ();

        int c = 0;
        row[c++] = strResName;

        if ( labs ( LuaMainMemory.Delta ) >= 1 )
        {
            row[c] = SString ( "%d KB", LuaMainMemory.Delta );
            LuaMainMemory.Delta = 0;
        }
        c++;

        row[c++] = SString ( "%d KB", LuaMainMemory.Current );
        row[c++] = SString ( "%d KB", LuaMainMemory.Max );
        row[c++] = !LuaMainMemory.OpenXMLFiles ? "-" : SString ( "%d", LuaMainMemory.OpenXMLFiles );
        row[c++] = !LuaMainMemory.OpenFiles ? "-" : SString ( "%d", LuaMainMemory.OpenFiles );
        row[c++] = !LuaMainMemory.Refs ? "-" : SString ( "%d", LuaMainMemory.Refs );
        row[c++] = !LuaMainMemory.TimerCount ? "-" : SString ( "%d", LuaMainMemory.TimerCount );
        row[c++] = !LuaMainMemory.ElementCount ? "-" : SString ( "%d", LuaMainMemory.ElementCount );
        row[c++] = !LuaMainMemory.TextDisplayCount ? "-" : SString ( "%d", LuaMainMemory.TextDisplayCount );
        row[c++] = !LuaMainMemory.TextItemCount ? "-" : SString ( "%d", LuaMainMemory.TextItemCount );
    }
}
Example #13
0
int CLuaPickupDefs::createPickup ( lua_State* luaVM )
{
    // Grab all the argument types
    int iArgument1 = lua_type ( luaVM, 1 );
    int iArgument2 = lua_type ( luaVM, 2 );
    int iArgument3 = lua_type ( luaVM, 3 );
    int iArgument4 = lua_type ( luaVM, 4 );
    int iArgument5 = lua_type ( luaVM, 5 );
    int iArgument6 = lua_type ( luaVM, 6 );
    int iArgument7 = lua_type ( luaVM, 7 );

    // The first 6 are always numeric saying position, type and weapon/health/armor
    // TODO: Check argument 7 incase type is weapon
    if ( ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING ) &&
         ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
         ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
         ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) &&
         ( iArgument5 == LUA_TNUMBER || iArgument5 == LUA_TSTRING ) &&
         ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING || iArgument6 == LUA_TNONE ) &&
         ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING || iArgument7 == LUA_TNONE ) )
    {
        // Populate a position vector for it
        CVector vecPosition = CVector ( static_cast < float > ( lua_tonumber ( luaVM, 1 ) ),
                                        static_cast < float > ( lua_tonumber ( luaVM, 2 ) ),
                                        static_cast < float > ( lua_tonumber ( luaVM, 3 ) ) );

        // Is the type health or armor?
        unsigned long ulRespawnInterval = 30000;
        double dblAmmo = 50.0;
        if ( iArgument6 == LUA_TNUMBER || iArgument6 == LUA_TSTRING )
            ulRespawnInterval = static_cast < unsigned long > ( lua_tonumber ( luaVM, 6 ) );

        if ( iArgument7 == LUA_TNUMBER || iArgument7 == LUA_TSTRING )
            dblAmmo = lua_tonumber ( luaVM, 7 );

		CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
		if ( pLuaMain )
		{
			CResource* pResource = pLuaMain->GetResource();
			if ( pResource )
			{
				CPickup* pPickup = CStaticFunctionDefinitions::CreatePickup ( 
										pResource,
										vecPosition, 
										static_cast < unsigned char > ( lua_tonumber ( luaVM, 4 ) ), 
										lua_tonumber ( luaVM, 5 ), 
										ulRespawnInterval, 
										dblAmmo );
		        if ( pPickup )
		        {
					CElementGroup * pGroup = pResource->GetElementGroup();
					if ( pGroup )
					{
						pGroup->Add ( pPickup );
					}
					// Return the handle
					lua_pushelement ( luaVM, pPickup );
					return 1;
				}
			}
        }
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "createPickup" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #14
0
int CLuaFunctionDefs::SetWaterLevel ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResource* pResource = pLuaMain->GetResource ();
        if ( pResource )
        {
            int iArgument1 = lua_type ( luaVM, 1 );
            int iArgument2 = lua_type ( luaVM, 2 );
            int iArgument3 = lua_type ( luaVM, 3 );
            int iArgument4 = lua_type ( luaVM, 4 );

            if ( iArgument1 == LUA_TNUMBER || iArgument1 == LUA_TSTRING )
            {
                if ( ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) &&
                    ( iArgument3 == LUA_TNUMBER || iArgument3 == LUA_TSTRING ) &&
                    ( iArgument4 == LUA_TNUMBER || iArgument4 == LUA_TSTRING ) )
                {
                    // (x, y, z, level)
                    CVector vecPosition ( static_cast < float > ( lua_tonumber ( luaVM, 1 ) ),
                        static_cast < float > ( lua_tonumber ( luaVM, 2 ) ),
                        static_cast < float > ( lua_tonumber ( luaVM, 3 ) ) );
                    float fLevel = static_cast < float > ( lua_tonumber ( luaVM, 4 ) );
                    if ( CStaticFunctionDefinitions::SetWaterLevel ( &vecPosition, fLevel, pResource ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
                else
                {
                    // (level)
                    float fLevel = static_cast < float > ( lua_tonumber ( luaVM, 1 ) );
                    if ( CStaticFunctionDefinitions::SetWaterLevel ( (CVector *)NULL, fLevel, pResource ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
            }
            else if ( ( iArgument1 == LUA_TLIGHTUSERDATA ) &&
                ( iArgument2 == LUA_TNUMBER || iArgument2 == LUA_TSTRING ) )
            {
                // (water, level)
                CClientWater* pWater = lua_towater ( luaVM, 1 );
                float fLevel = static_cast < float > ( lua_tonumber ( luaVM, 2 ) );
                if ( pWater )
                {
                    if ( CStaticFunctionDefinitions::SetWaterLevel ( pWater, fLevel, pResource ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
                else
                    m_pScriptDebugging->LogBadPointer ( luaVM, "setWaterLevel", "water", 1 );
            }
            else
                m_pScriptDebugging->LogBadType ( luaVM, "setWaterLevel" );
        }
    }
    lua_pushboolean ( luaVM, false );
    return 1;
}
Example #15
0
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 )
    {
        SString strFile, strRootNodeName;
        
        CScriptArgReader argStream ( luaVM );
        argStream.ReadString ( strFile );
        argStream.ReadString ( strRootNodeName );

        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 file
                    CXMLFile * xmlFile = pLUA->CreateXML ( strPath );
                    if ( xmlFile )
                    {
                        // Create its root node
                        CXMLNode* pRootNode = xmlFile->CreateRootNode ( strRootNodeName );
                        if ( pRootNode )
                        {
                            lua_pushxmlnode ( luaVM, pRootNode );
                            return 1;
                        }
                        // Delete it again
                        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;
}
Example #16
0
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;
}
Example #17
0
void CScriptDebugging::LogString ( const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
{
    SString strText = ComposeErrorMessage( szPrePend, luaDebugInfo, szMessage );

    // Create a different message if type is "INFO"
    if ( uiMinimumDebugLevel > 2 )
        strText = SString ( "%s%s", szPrePend, szMessage );

    // Check whether onDebugMessage is currently being triggered
    if ( !m_bTriggeringOnDebugMessage )
    {
        // Make sure the state of onDebugMessage being triggered can be retrieved later
        m_bTriggeringOnDebugMessage = true;

        // Prepare onDebugMessage
        CLuaArguments Arguments;
        Arguments.PushString ( szMessage );
        Arguments.PushNumber ( uiMinimumDebugLevel );

        // Push the file name (if any)
        if ( !luaDebugInfo.strFile.empty() )
            Arguments.PushString ( luaDebugInfo.strFile );
        else
            Arguments.PushNil ( );

        // Push the line (if any)
        if ( luaDebugInfo.iLine != INVALID_LINE_NUMBER )
            Arguments.PushNumber ( luaDebugInfo.iLine );
        else
            Arguments.PushNil ( );
        
        // Call onDebugMessage
        g_pGame->GetMapManager ( )->GetRootElement ( )->CallEvent ( "onDebugMessage", Arguments );

        // Reset trigger state, so onDebugMessage can be called again at a later moment
        m_bTriggeringOnDebugMessage = false;
    }

    // Log it to the file if enough level
    if ( m_uiLogFileLevel >= uiMinimumDebugLevel )
    {
        PrintLog ( strText );
    }

    // Log to console
    CLogger::LogPrintf( "%s\n", strText.c_str () );

#if 0
    // Not sure what this is for, seems pretty useless
    if ( m_uiHtmlLogLevel >= uiMinimumDebugLevel )
    {
        if ( luaVM )
        {
            CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResourceFile * file = pLuaMain->GetResourceFile();
                if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
                {
                    CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                    html->AppendToPageBuffer ( strText );
                    html->AppendToPageBuffer ( "<br/>" );
                }
            }
        }
    }
#endif

    // Tell the players
    Broadcast ( CDebugEchoPacket ( strText, uiMinimumDebugLevel, ucRed, ucGreen, ucBlue ), uiMinimumDebugLevel );
}
int CLuaFunctionDefs::CreateProjectile ( lua_State* luaVM )
{
    if ( ( lua_istype ( luaVM, 1, LUA_TLIGHTUSERDATA ) ) &&
        ( lua_istype ( luaVM, 2, LUA_TNUMBER ) || lua_istype ( luaVM, 2, LUA_TSTRING ) ) )
    {
        CClientEntity* pCreator = lua_toelement ( luaVM, 1 );
        if ( pCreator )
        {
            unsigned char ucWeaponType = static_cast < unsigned char > ( lua_tonumber ( luaVM, 2 ) );
            CVector vecOrigin;
            pCreator->GetPosition ( vecOrigin );
            float fForce = 1.0f;
            CClientEntity* pTarget = NULL;
            CVector *pvecRotation = NULL, *pvecMoveSpeed = NULL;
            unsigned short usModel = 0;
            if ( ( lua_istype ( luaVM, 3, LUA_TNUMBER ) || lua_istype ( luaVM, 3, LUA_TSTRING ) ) &&
                ( lua_istype ( luaVM, 4, LUA_TNUMBER ) || lua_istype ( luaVM, 4, LUA_TSTRING ) ) &&
                ( lua_istype ( luaVM, 5, LUA_TNUMBER ) || lua_istype ( luaVM, 5, LUA_TSTRING ) ) )
            {
                vecOrigin = CVector ( static_cast < float > ( lua_tonumber ( luaVM, 3 ) ),
                    static_cast < float > ( lua_tonumber ( luaVM, 4 ) ),
                    static_cast < float > ( lua_tonumber ( luaVM, 5 ) ) );

                if ( lua_istype ( luaVM, 6, LUA_TNUMBER ) || lua_istype ( luaVM, 6, LUA_TSTRING ) )
                {
                    fForce = static_cast < float > ( lua_tonumber ( luaVM, 6 ) );

                    if ( lua_istype ( luaVM, 7, LUA_TLIGHTUSERDATA ) )
                    {
                        CClientEntity* pTemp = lua_toelement ( luaVM, 7 );
                        if ( pTemp )
                        {
                            pTarget = pTemp;
                        }
                        else
                            m_pScriptDebugging->LogBadPointer ( luaVM, "element", 7 );
                    }

                    int iArgument8 = lua_type ( luaVM, 8 );
                    int iArgument9 = lua_type ( luaVM, 9 );
                    int iArgument10 = lua_type ( luaVM, 10 );
                    if ( ( iArgument8 == LUA_TSTRING || iArgument8 == LUA_TNUMBER ) &&
                        ( iArgument9 == LUA_TSTRING || iArgument9 == LUA_TNUMBER ) &&
                        ( iArgument10 == LUA_TSTRING || iArgument10 == LUA_TNUMBER ) )
                    {
                        pvecRotation = new CVector ( static_cast < float > ( lua_tonumber ( luaVM, 8 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 9 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 10 ) ) );
                    }
                    int iArgument11 = lua_type ( luaVM, 11 );
                    int iArgument12 = lua_type ( luaVM, 12 );
                    int iArgument13 = lua_type ( luaVM, 13 );
                    if ( ( iArgument11 == LUA_TSTRING || iArgument11 == LUA_TNUMBER ) &&
                        ( iArgument12 == LUA_TSTRING || iArgument12 == LUA_TNUMBER ) &&
                        ( iArgument13 == LUA_TSTRING || iArgument13 == LUA_TNUMBER ) )
                    {
                        pvecMoveSpeed = new CVector ( static_cast < float > ( lua_tonumber ( luaVM, 11 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 12 ) ),
                            static_cast < float > ( lua_tonumber ( luaVM, 13 ) ) );

                        int iArgument14 = lua_type ( luaVM, 14 );
                        if ( iArgument14 == LUA_TSTRING || iArgument14 == LUA_TNUMBER )
                        {
                            usModel = static_cast < unsigned short > ( lua_tonumber ( luaVM, 14 ) );
                        }
                    }
                }
            }

            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResource * pResource = pLuaMain->GetResource();
                if ( pResource )
                {
                    CClientProjectile * pProjectile = CStaticFunctionDefinitions::CreateProjectile ( *pResource, *pCreator, ucWeaponType, vecOrigin, fForce, pTarget, pvecRotation, pvecMoveSpeed, usModel );
                    if ( pProjectile )
                    {
                        CElementGroup * pGroup = pResource->GetElementGroup();
                        if ( pGroup )
                        {
                            pGroup->Add ( ( CClientEntity* ) pProjectile );
                        }

                        if ( pvecRotation )
                        {
                            delete pvecRotation;
                            pvecRotation = NULL;
                        }
                        if ( pvecMoveSpeed )
                        {
                            delete pvecMoveSpeed;
                            pvecMoveSpeed = NULL;
                        }

                        lua_pushelement ( luaVM, pProjectile );
                        return 1;
                    }
                }
            }
            if ( pvecRotation )
            {
                delete pvecRotation;
                pvecRotation = NULL;
            }
            if ( pvecMoveSpeed )
            {
                delete pvecMoveSpeed;
                pvecMoveSpeed = NULL;
            }
        }
        else
            m_pScriptDebugging->LogBadPointer ( luaVM, "element", 1 );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM );

    lua_pushboolean ( luaVM, false );
    return 1;
}