Пример #1
0
int CLuaFunctionDefs::GetResourceExportedFunctions ( lua_State *luaVM )
{
    CResource* pResource = NULL;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pResource, NULL );

    // No resource given, get this resource's root
        if ( pResource == NULL )
        {
        // Find our vm and get the root
            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                pResource = pLuaMain->GetResource ();
            }
        }

        if ( pResource )
        {
            lua_newtable ( luaVM );
            unsigned int uiIndex = 0;
            list<CExportedFunction *>::iterator iterd = pResource->IterBeginExportedFunctions();
            for ( ; iterd != pResource->IterEndExportedFunctions(); iterd++ )
            {
                lua_pushnumber ( luaVM, ++uiIndex );
                lua_pushstring ( luaVM, (*iterd)->GetFunctionName () );
                lua_settable ( luaVM, -3 );
            }
            return 1;
        }

    m_pScriptDebugging->LogBadType ( luaVM );
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #2
0
int CLuaFunctionDefs::DownloadFile ( lua_State* luaVM )
{
    SString strFile = "";
    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strFile );

    if ( !argStream.HasErrors ( ) )
    {
        // Grab our VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab its resource
            CResource * pResource = pLuaMain->GetResource();
            if ( pResource )
            {
                std::list < CResourceFile* > ::const_iterator iter = pResource->IterBeginResourceFiles();
                for ( ; iter != pResource->IterEndResourceFiles() ; iter++ ) 
                {
                    if ( strcmp ( strFile, (*iter)->GetShortName() ) == 0 )
                    {
                        if ( CStaticFunctionDefinitions::DownloadFile ( pResource, strFile, (*iter)->GetServerChecksum() ) )
                        {
                            lua_pushboolean ( luaVM, true );
                            return 1;
                        }
                    }
                }
                m_pScriptDebugging->LogCustom ( luaVM, 255, 255, 255, "%s: File doesn't exist", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
            }
        }
    }
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #3
0
int CLuaFileDefs::fileExists ( lua_State* luaVM )
{
//  bool fileExists ( string filePath )
    SString filePath;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( filePath );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            std::string strAbsPath;
            CResource* pResource = pLuaMain->GetResource ();
            if ( CResourceManager::ParseResourcePathInput ( filePath, pResource, strAbsPath ) )
            {
                bool bResult = FileExists ( strAbsPath );
                lua_pushboolean ( luaVM, bResult );
                return 1;
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );


    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #4
0
int CLuaEngineDefs::EngineLoadTXD ( lua_State* luaVM )
{
    SString strFile = "";
    bool bFilteringEnabled = true;
    CScriptArgReader argStream ( luaVM );
    // Grab the TXD filename or data
    argStream.ReadString ( strFile );
    if ( argStream.NextIsBool() )   // Some scripts have a number here (in error)
        argStream.ReadBool ( bFilteringEnabled, true );

    if ( !argStream.HasErrors ( ) )
    {
        // Grab our virtual machine and grab our resource from that.
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab this resource
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                bool bIsRawData = CClientTXD::IsTXDData( strFile );
                SString strPath;
                // Is this a legal filepath?
                if ( bIsRawData || CResourceManager::ParseResourcePathInput( strFile, pResource, &strPath ) )
                {
                    // Grab the resource root entity
                    CClientEntity* pRoot = pResource->GetResourceTXDRoot ();

                    // Create a TXD element
                    CClientTXD* pTXD = new CClientTXD ( m_pManager, INVALID_ELEMENT_ID );

                    // Try to load the TXD file
                    if ( pTXD->LoadTXD ( bIsRawData ? strFile : strPath, bFilteringEnabled, bIsRawData ) )
                    {
                        // Success loading the file. Set parent to TXD root
                        pTXD->SetParent ( pRoot );

                        // Return the TXD
                        lua_pushelement ( luaVM, pTXD );
                        return 1;
                    }
                    else
                    {
                        // Delete it again
                        delete pTXD;
                        argStream.SetCustomError( bIsRawData ? "raw data" : strFile, "Error loading TXD" );
                    }
                }
                else
                    argStream.SetCustomError( bIsRawData ? "raw data" : strFile, "Bad file path" );
            }
        }
    }
    if ( argStream.HasErrors() )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // We failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #5
0
int CLuaFunctionDefs::ShowCursor ( lua_State* luaVM )
{
    bool bShow = false, bToggleControls = true;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadBool ( bShow );
    argStream.ReadBool ( bToggleControls, true );

    if ( !argStream.HasErrors ( ) )
    {
        // Get the VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab the resource belonging to this VM
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                // Show/hide it inside that resource
                pResource->ShowCursor ( bShow, bToggleControls );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // Fail
    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::PlaySFX ( lua_State* luaVM )
{
//  sound playSFX ( string audioContainer, int bankIndex, int audioIndex [, loop = false ] )
    eAudioLookupIndex containerIndex; int iBankIndex; int iAudioIndex; bool bLoop;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadEnumString ( containerIndex );
    argStream.ReadNumber ( iBankIndex );
    argStream.ReadNumber ( iAudioIndex );
    argStream.ReadBool ( bLoop, false );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                CClientSound* pSound;
                if ( CStaticFunctionDefinitions::PlaySFX ( pResource, containerIndex, iBankIndex, iAudioIndex, bLoop, pSound ) )
                {
                    lua_pushelement ( luaVM, pSound );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #7
0
int CLuaFunctionDefs::ShowCursor ( lua_State* luaVM )
{
    // Get the VM
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        // Boolean type passed?
        if ( lua_type ( luaVM, 1 ) == LUA_TBOOLEAN )
        {
            // Grab the argument
            bool bShow = lua_toboolean ( luaVM, 1 ) ?true:false;
            bool bToggleControls = true;
            if ( lua_type ( luaVM, 2 ) == LUA_TBOOLEAN )
                bToggleControls = ( lua_toboolean ( luaVM, 2 ) ) ? true:false;

            // Grab the resource belonging to this VM
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                // Show/hide it inside that resource
                pResource->ShowCursor ( bShow, bToggleControls );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
        }
        else
            m_pScriptDebugging->LogBadType ( luaVM, "showCursor" );
    }

    // Fail
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #8
0
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;
}
Пример #9
0
int CLuaEngineDefs::EngineLoadDFF ( lua_State* luaVM )
{
    SString strFile = "";
    CScriptArgReader argStream ( luaVM );
    // Grab the DFF filename or data (model ID ignored after 1.3.1)
    argStream.ReadString ( strFile );

    if ( !argStream.HasErrors ( ) )
    {
        // Grab our virtual machine and grab our resource from that.
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Get this resource
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                bool bIsRawData = CClientDFF::IsDFFData( strFile );
                SString strPath;
                // Is this a legal filepath?
                if ( bIsRawData || CResourceManager::ParseResourcePathInput( strFile, pResource, &strPath ) )
                {
                    // Grab the resource root entity
                    CClientEntity* pRoot = pResource->GetResourceDFFRoot ();

                    // Create a DFF element
                    CClientDFF* pDFF = new CClientDFF ( m_pManager, INVALID_ELEMENT_ID );

                    // Try to load the DFF file
                    if ( pDFF->LoadDFF ( bIsRawData ? strFile : strPath, bIsRawData ) )
                    {
                        // Success loading the file. Set parent to DFF root
                        pDFF->SetParent ( pRoot );

                        // Return the DFF
                        lua_pushelement ( luaVM, pDFF );
                        return 1;
                    }
                    else
                    {
                        // Delete it again
                        delete pDFF;
                        argStream.SetCustomError( bIsRawData ? "raw data" : strFile, "Error loading DFF" );
                    }
                }
                else
                    argStream.SetCustomError( bIsRawData ? "raw data" : strFile, "Bad file path" );
            }
        }
    }
    if ( argStream.HasErrors ( ) )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // We failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #10
0
int CLuaEngineDefs::EngineLoadCOL ( lua_State* luaVM )
{
    SString strFile = "";
    CScriptArgReader argStream ( luaVM );
    // Grab the COL filename or data
    argStream.ReadString ( strFile );

    if ( !argStream.HasErrors ( ) )
    {
        // Grab the lua main and the resource belonging to this script
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Get the resource we belong to
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                bool bIsRawData = CClientColModel::IsCOLData( strFile );
                SString strPath;
                // Is this a legal filepath?
                if ( bIsRawData || CResourceManager::ParseResourcePathInput( strFile, pResource, &strPath ) )
                {
                    // Grab the resource root entity
                    CClientEntity* pRoot = pResource->GetResourceCOLModelRoot ();

                    // Create the col model
                    CClientColModel* pCol = new CClientColModel ( m_pManager, INVALID_ELEMENT_ID );

                    // Attempt loading the file
                    if ( pCol->LoadCol ( bIsRawData ? strFile : strPath, bIsRawData ) )
                    {
                        // Success. Make it a child of the resource collision root
                        pCol->SetParent ( pRoot );

                        // Return the created col model
                        lua_pushelement ( luaVM, pCol );
                        return 1;
                    }
                    else
                    {
                        // Delete it again. We failed
                        delete pCol;
                        argStream.SetCustomError( bIsRawData ? "raw data" : strFile, "Error loading COL" );
                    }
                }
                else
                    argStream.SetCustomError( bIsRawData ? "raw data" : strFile, "Bad file path" );
            }
        }
    }
    if ( argStream.HasErrors ( ) )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // We failed for some reason
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #11
0
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::PlaySound3D ( lua_State* luaVM )
{
    SString strSound = "";
    CVector vecPosition;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strSound );
    argStream.ReadNumber ( vecPosition.fX );
    argStream.ReadNumber ( vecPosition.fY );
    argStream.ReadNumber ( vecPosition.fZ );

    if ( !argStream.HasErrors() )
    {
        CLuaMain * luaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( luaMain )
        {
            CResource* pResource = luaMain->GetResource();
            if ( pResource )
            {
                SString strFilename;
                bool bIsURL = false;
                if ( CResourceManager::ParseResourcePathInput( strSound, pResource, strFilename ) )
                    strSound = strFilename;
                else
                    bIsURL = true;

                // ParseResourcePathInput changes pResource in some cases e.g. an invalid resource URL - crun playSound( ":myNotRunningResource/music/track.mp3" )
                // Fixes #6507 - Caz
                if ( pResource )
                {
                    bool bLoop = false;
                    if ( argStream.NextIsBool ( ) )
                    {
                        argStream.ReadBool ( bLoop );
                    }

                    CClientSound* pSound = CStaticFunctionDefinitions::PlaySound3D ( pResource, strSound, bIsURL, vecPosition, bLoop );
                    if ( pSound )
                    {
                        // call onClientSoundStarted
                        CLuaArguments Arguments;
                        Arguments.PushString ( "play" );     // Reason
                        pSound->CallEvent ( "onClientSoundStarted", Arguments, false );

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

    lua_pushboolean ( luaVM, false );
    return 1;
}
int CLuaFunctionDefs::GetResourceConfig ( lua_State* luaVM )
{
    if ( lua_istype ( luaVM, 1, LUA_TLIGHTUSERDATA ) )
        m_pScriptDebugging->LogCustom ( luaVM, "getResourceConfig may be using an outdated syntax. Please check and update." );

    // Resource and config name
    CResource* pResource = NULL;
    const char* szInput = NULL;
    std::string strAbsPath;
    std::string strMetaPath;

    if ( lua_istype ( luaVM, 1, LUA_TSTRING ) )
    {
        // Grab our lua main
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab resource and the config name from arg
            pResource = pLuaMain->GetResource ();
            szInput = lua_tostring ( luaVM, 1 );
        }
    }

    // We have both a resource file to grab the config from and a config name?
    if ( pResource && szInput )
    {
        if ( CResourceManager::ParseResourcePathInput ( szInput, 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, "getResourceConfig" );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #14
0
int CLuaMarkerDefs::CreateMarker(lua_State* luaVM)
{
    CVector    vecPosition;
    float      fSize;
    SColorRGBA color(0, 0, 255, 255);
    SString    strType;
    CElement*  pVisibleTo;

    CScriptArgReader argStream(luaVM);
    argStream.ReadVector3D(vecPosition);
    argStream.ReadString(strType, "default");
    argStream.ReadNumber(fSize, 4.0f);
    argStream.ReadNumber(color.R, color.R);
    argStream.ReadNumber(color.G, color.G);
    argStream.ReadNumber(color.B, color.B);
    argStream.ReadNumber(color.A, color.A);

    if (argStream.NextIsBool() || argStream.NextIsNil())
    {
        pVisibleTo = NULL;
    }
    else
        argStream.ReadUserData(pVisibleTo, m_pRootElement);

    if (!argStream.HasErrors())
    {
        CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine(luaVM);
        if (pLuaMain)
        {
            CResource* pResource = pLuaMain->GetResource();
            if (pResource)
            {
                // Create it
                CMarker* pMarker = CStaticFunctionDefinitions::CreateMarker(pResource, vecPosition, strType, fSize, color, pVisibleTo);
                if (pMarker)
                {
                    CElementGroup* pGroup = pResource->GetElementGroup();
                    if (pGroup)
                    {
                        pGroup->Add(pMarker);
                    }
                    lua_pushelement(luaVM, pMarker);
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

    lua_pushboolean(luaVM, false);
    return 1;
}
Пример #15
0
int CLuaFunctionDefs::BindKey ( lua_State* luaVM )
{
    SString strKey = "", strHitState = "", strCommand = "", strArguments = "";
    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strKey );
    argStream.ReadString ( strHitState );

    if ( !argStream.HasErrors ( ) )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            if ( argStream.NextIsString ( ) )
            {
                // bindKey ( string key, string keyState, string commandName, [ string arguments ] )
                SString strResource = pLuaMain->GetResource()->GetName();
                argStream.ReadString ( strCommand );
                argStream.ReadString ( strArguments, "" );
                if ( !argStream.HasErrors ( ) )
                {
                    if ( CStaticFunctionDefinitions::BindKey ( strKey, strHitState, strCommand, strArguments, strResource ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }  
                }
            }
            else
            {
                // bindKey ( string key, string keyState, function handlerFunction,  [ var arguments, ... ] )
                CLuaFunctionRef iLuaFunction;
                CLuaArguments Arguments;
                argStream.ReadFunction ( iLuaFunction );
                argStream.ReadLuaArguments ( Arguments );
                argStream.ReadFunctionComplete ();
                if ( !argStream.HasErrors ( ) )
                {
                    if ( CStaticFunctionDefinitions::BindKey ( strKey, strHitState, pLuaMain, iLuaFunction, Arguments ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
            }
        }
    }

    if ( argStream.HasErrors ( ) )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #16
0
int CLuaFunctionDefs::UnbindKey ( lua_State* luaVM )
{
    SString strKey = "", strHitState = "";
    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strKey );

    if ( !argStream.HasErrors ( ) )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            if ( argStream.NextIsString ( 1 ) ) // Check if has command
            {
                // bool unbindKey ( string key, string keyState, string command )
                SString strResource = pLuaMain->GetResource()->GetName();
                SString strCommand = "";
                argStream.ReadString ( strHitState );
                argStream.ReadString ( strCommand );
                if ( !argStream.HasErrors ( ) )
                {
                    if ( CStaticFunctionDefinitions::UnbindKey ( strKey, strHitState, strCommand, strResource ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
            }
            else
            {
                // bool unbindKey ( string key, [ string keyState, function handler ] )
                CLuaFunctionRef iLuaFunction;
                argStream.ReadString ( strHitState, "" );
                argStream.ReadFunction ( iLuaFunction, LUA_REFNIL );
                argStream.ReadFunctionComplete ();
                if ( !argStream.HasErrors ( ) )
                {
                    const char* szHitState = strHitState == "" ? NULL : strHitState.c_str();
                    if ( CStaticFunctionDefinitions::UnbindKey ( strKey, pLuaMain, szHitState, iLuaFunction ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
            }
        }
    }

    if ( argStream.HasErrors ( ) )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #17
0
int CLuaFunctionDefs::UnbindKey ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        if ( lua_type ( luaVM, 1 ) == LUA_TSTRING )
        {
            const char* szKey = lua_tostring ( luaVM, 1 );
            const char* szHitState = NULL;

            if ( lua_type ( luaVM, 2 ) )
                szHitState = lua_tostring ( luaVM, 2 );

            if ( lua_type ( luaVM, 3 ) == LUA_TSTRING )
            {
                const char* szResource = pLuaMain->GetResource()->GetName();
                const char* szCommand = lua_tostring ( luaVM, 3 );
                if ( CStaticFunctionDefinitions::UnbindKey ( szKey, szHitState, szCommand, szResource ) )
                {
                    lua_pushboolean ( luaVM, true );
                    return 1;
                }
            }
            else
            {   
                int iLuaFunction = 0;
                if ( lua_type ( luaVM, 3 ) == LUA_TFUNCTION )
                    iLuaFunction = luaM_toref ( luaVM, 3 );

                if ( iLuaFunction == 0 || VERIFY_FUNCTION ( iLuaFunction ) )
                {
                    if ( CStaticFunctionDefinitions::UnbindKey ( szKey, pLuaMain, szHitState, iLuaFunction ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
                else
                {
                    m_pScriptDebugging->LogBadType ( luaVM, "unbindKey" );
                }
            }
        }
        else
        {
            m_pScriptDebugging->LogBadType ( luaVM, "unbindKey" );
        }
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #18
0
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::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;
}
Пример #20
0
int CLuaFunctionDefs::LoadBrowserURL ( lua_State* luaVM )
{
//  bool loadBrowserURL ( browser webBrowser, string url [, string postData = "", bool postURLEncoded = true ] )
    CClientWebBrowser* pWebBrowser; SString strURL; SString strPostData; bool bURLEncoded;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadUserData ( pWebBrowser );
    argStream.ReadString ( strURL );
    argStream.ReadString ( strPostData, "" );
    argStream.ReadBool ( bURLEncoded, true );

    if ( !argStream.HasErrors () )
    {
        // Are we dealing with a remote website?
        if ( strURL.substr ( 0, 7 ) == "http://" || strURL.substr ( 0, 8 ) == "https://" )
        {
            bool isLocalURL = strURL.substr ( 0, 11 ) == "http://mta/";
            if ( pWebBrowser->IsLocal () != isLocalURL )
            {
                lua_pushboolean ( luaVM, false );
                return 1;
            }

            lua_pushboolean ( luaVM, pWebBrowser->LoadURL ( strURL, !isLocalURL, strPostData, bURLEncoded ) );
            return 1;
        }

        // Are we dealing with a local website? If so, parse resource path. Otherwise, return false and load nothing
        // Todo: Add an ACL right which is necessary to load local websites or websites in general
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            SString strAbsPath;
            CResource* pResource = pLuaMain->GetResource ();
            if ( CResourceManager::ParseResourcePathInput ( strURL, pResource, strAbsPath ) && pWebBrowser->IsLocal () )
            {
                // Output deprecated warning, TODO: Remove this at a later point
                m_pScriptDebugging->LogWarning ( luaVM, "This URL scheme is deprecated and may not work in future versions. Please consider using http://mta/* instead. See https://wiki.mtasa.com/wiki/LoadBrowserURL for details" );

                lua_pushboolean ( luaVM,  pWebBrowser->LoadURL ( "mtalocal://" + strURL, false, strPostData, bURLEncoded ) );
                return 1;
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #21
0
int CLuaFunctionDefs::CreateObject ( lua_State* luaVM )
{
//  object createObject ( int modelid, float x, float y, float z, [float rx, float ry, float rz, bool lowLOD] )
    ushort usModelID; CVector vecPosition; CVector vecRotation; bool bLowLod;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadNumber ( usModelID );
    argStream.ReadNumber ( vecPosition.fX );
    argStream.ReadNumber ( vecPosition.fY );
    argStream.ReadNumber ( vecPosition.fZ );
    argStream.ReadNumber ( vecRotation.fX, 0 );
    argStream.ReadNumber ( vecRotation.fY, 0 );
    argStream.ReadNumber ( vecRotation.fZ, 0 );
    argStream.ReadBool ( bLowLod, false );

    if ( !argStream.HasErrors () )
    {
        if ( CClientObjectManager::IsValidModel  ( usModelID ) )
        {
            CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResource* pResource = pLuaMain->GetResource ();
                if ( pResource )
                {
                    CClientObject* pObject = CStaticFunctionDefinitions::CreateObject ( *pResource, usModelID, vecPosition, vecRotation, bLowLod );
                    if ( pObject )
                    {
                        CElementGroup * pGroup = pResource->GetElementGroup();
                        if ( pGroup )
                        {
                            pGroup->Add ( ( CClientEntity* ) pObject );
                        }

                        lua_pushelement ( luaVM, pObject );
                        return 1;
                    }
                }
            }
        }
        else
            m_pScriptDebugging->LogCustom ( luaVM, SString ( "Bad argument @ '%s' [%s]", "createObject", "Invalid model id" ) );
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, SString ( "Bad argument @ '%s' [%s]", "createObject", *argStream.GetErrorMessage () ) );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #22
0
int CLuaRadarAreaDefs::CreateRadarArea(lua_State* luaVM)
{
    //  radararea createRadarArea ( float startPosX, float startPosY, float sizeX, float sizeY, [ int r = 255, int g = 0, int b = 0, int a = 255, element
    //  visibleTo = getRootElement() ] )
    CVector2D vecPosition;
    CVector2D vecSize;
    float     dRed;
    float     dGreen;
    float     dBlue;
    float     dAlpha;
    CElement* pVisibleTo;

    CScriptArgReader argStream(luaVM);
    argStream.ReadVector2D(vecPosition);
    argStream.ReadVector2D(vecSize);
    argStream.ReadNumber(dRed, 255);
    argStream.ReadNumber(dGreen, 0);
    argStream.ReadNumber(dBlue, 0);
    argStream.ReadNumber(dAlpha, 255);
    argStream.ReadUserData(pVisibleTo, m_pRootElement);

    if (!argStream.HasErrors())
    {
        CLuaMain*  pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine(luaVM);
        CResource* pResource = pLuaMain ? pLuaMain->GetResource() : NULL;
        if (pResource)
        {
            SColorRGBA color(dRed, dGreen, dBlue, dAlpha);

            // Create it
            CRadarArea* pRadarArea = CStaticFunctionDefinitions::CreateRadarArea(pResource, vecPosition, vecSize, color, pVisibleTo);
            if (pRadarArea)
            {
                CElementGroup* pGroup = pResource->GetElementGroup();
                if (pGroup)
                {
                    pGroup->Add(pRadarArea);
                }
                lua_pushelement(luaVM, pRadarArea);
                return 1;
            }
        }
    }
    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;
}
Пример #24
0
int CLuaFunctionDefs::BindKey ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        if ( lua_type ( luaVM, 1 ) == LUA_TSTRING &&
            lua_type ( luaVM, 2 ) == LUA_TSTRING )
        {
            const char* szKey = lua_tostring ( luaVM, 1 );
            const char* szHitState = lua_tostring ( luaVM, 2 );

            if ( lua_type ( luaVM, 3 ) == LUA_TSTRING )
            {
                const char* szResource = pLuaMain->GetResource()->GetName();
                const char* szCommand = lua_tostring ( luaVM, 3 );
                const char* szArguments = "";
                if  ( lua_type ( luaVM, 4 ) == LUA_TSTRING )
                    szArguments = lua_tostring ( luaVM, 4 );
                if ( CStaticFunctionDefinitions::BindKey ( szKey, szHitState, szCommand, szArguments, szResource ) )
                {
                    lua_pushboolean ( luaVM, true );
                    return 1;
                }  
            }
            else
            { 
                // Jax: grab our arguments first, luaM_toref pops the stack!
                CLuaArguments Arguments;
                Arguments.ReadArguments ( luaVM, 4 );
                int iLuaFunction = luaM_toref ( luaVM, 3 );            

                if ( VERIFY_FUNCTION ( iLuaFunction ) )
                {
                    if ( CStaticFunctionDefinitions::BindKey ( szKey, szHitState, pLuaMain, iLuaFunction, Arguments ) )
                    {
                        lua_pushboolean ( luaVM, true );
                        return 1;
                    }
                }
                else
                    m_pScriptDebugging->LogBadPointer ( luaVM, "bindKey", "function", 3 );
            }
        }
    }

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #25
0
int CLuaColShapeDefs::CreateColTube ( lua_State* luaVM )
{
    CVector vecPosition;
    float fRadius = 0.1f, fHeight = 0.1f;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadVector3D ( vecPosition );
    argStream.ReadNumber ( fRadius );
    argStream.ReadNumber ( fHeight );

    if ( fRadius < 0.0f )
    {
        fRadius = 0.1f;
    }

    if ( fHeight < 0.0f )
    {
        fHeight = 0.1f;
    }

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                // Create it and return it
                CClientColTube* pShape = CStaticFunctionDefinitions::CreateColTube ( *pResource, vecPosition, fRadius, fHeight );
                if ( pShape )
                {
                    CElementGroup * pGroup = pResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( (CClientEntity*) pShape );
                    }
                    lua_pushelement ( luaVM, pShape );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #26
0
CChecksum CLuaModuleManager::GetResourceMetaChecksum ( lua_State* luaVM )
{
    if ( luaVM )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                return pResource->GetLastMetaChecksum ();
            }
        }
    }
    return CChecksum ();
}
Пример #27
0
bool CLuaModuleManager::GetResourceName ( lua_State * luaVM, std::string& strName )
{
    if ( luaVM )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                strName = pResource->GetName ();
                return true;
            }
        }
    }
    return false;
}
//
// Check min server is correct
//
void MinServerReqCheck ( CScriptArgReader& argStream, const char* szVersionReq, const char* szReason )
{
    CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine ( argStream.m_luaVM );
    if ( pLuaMain )
    {
        CResource* pResource = pLuaMain->GetResource();
        if ( pResource )
        {
            if ( pResource->GetMinServerReqFromMetaXml () < szVersionReq )
            {
                #if MTASA_VERSION_TYPE == VERSION_TYPE_RELEASE
                    argStream.SetVersionWarning ( szVersionReq, "server", szReason );
                #endif
            }
        }
    }
}
Пример #29
0
int CLuaColShapeDefs::CreateColRectangle ( lua_State* luaVM )
{
    CVector2D vecPosition;
    CVector2D vecSize;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadVector2D ( vecPosition );
    argStream.ReadVector2D ( vecSize );

    if ( vecSize.fX < 0.0f )
    {
        vecSize.fX = 0.1f;
    }
    if ( vecSize.fY < 0.0f )
    {
        vecSize.fY = 0.1f;
    }

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                // Create it and return it
                CClientColRectangle* pShape = CStaticFunctionDefinitions::CreateColRectangle ( *pResource, vecPosition, vecSize );
                if ( pShape )
                {
                    CElementGroup * pGroup = pResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( (CClientEntity*) pShape );
                    }
                    lua_pushelement ( luaVM, pShape );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
Пример #30
0
int CLuaPointLightDefs::CreateLight ( lua_State* luaVM )
{
    //  light createLight ( int lightType, float posX, float posY, float posX, [ float radius = 3, int r = 255, int g = 0, int b = 0, float dirX = 0, float dirY = 0, float dirZ = 0, bool createsShadow = false ] )
    int iMode;
    CVector vecPosition;
    float fRadius;
    SColor color;
    CVector vecDirection;
    bool bCreatesShadow;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadNumber ( iMode );
    argStream.ReadVector3D ( vecPosition );
    argStream.ReadNumber ( fRadius, 3.0f );
    argStream.ReadNumber ( color.R, 255 );
    argStream.ReadNumber ( color.G, 0 );
    argStream.ReadNumber ( color.B, 0 );
    argStream.ReadVector3D ( vecDirection, vecDirection );
    argStream.ReadBool ( bCreatesShadow, false );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        CResource* pResource = pLuaMain ? pLuaMain->GetResource () : NULL;
        if ( pResource )
        {
            // Create it
            CClientPointLights* pLight = CStaticFunctionDefinitions::CreateLight ( *pResource, iMode, vecPosition, fRadius, color, vecDirection );
            if ( pLight )
            {
                CElementGroup * pGroup = pResource->GetElementGroup ();
                if ( pGroup )
                {
                    pGroup->Add ( (CClientEntity*) pLight );
                }
                lua_pushelement ( luaVM, pLight );
                return 1;
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, SString ( "Bad argument @ '%s' [%s]", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), *argStream.GetErrorMessage () ) );

    lua_pushboolean ( luaVM, false );
    return 1;
}