예제 #1
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;
}
예제 #2
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;
}
예제 #3
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;
}
예제 #4
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;
}
예제 #5
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;
}
예제 #6
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;
}
예제 #7
0
int CLuaColShapeDefs::CreateColTube(lua_State* luaVM)
{
    CVector vecPosition;
    float   fHeight, fRadius;

    CScriptArgReader argStream(luaVM);
    argStream.ReadVector3D(vecPosition);
    argStream.ReadNumber(fRadius);
    argStream.ReadNumber(fHeight);

    if (!argStream.HasErrors())
    {
        if (fRadius < 0.0f)
            fRadius = 0.1f;
        if (fHeight < 0.0f)
            fHeight = 0.1f;

        CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine(luaVM);
        if (pLuaMain)
        {
            CResource* pResource = pLuaMain->GetResource();
            if (pResource)
            {
                // Create it and return it
                CColTube* pShape = CStaticFunctionDefinitions::CreateColTube(pResource, vecPosition, fRadius, fHeight);
                if (pShape)
                {
                    CElementGroup* pGroup = pResource->GetElementGroup();
                    if (pGroup)
                    {
                        pGroup->Add(pShape);
                    }
                    lua_pushelement(luaVM, pShape);
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

    lua_pushboolean(luaVM, false);
    return 1;
}
예제 #8
0
int CLuaFunctionDefs::CreateTeam ( lua_State* luaVM )
{
    SString strName;
    unsigned char ucRed;
    unsigned char ucGreen;
    unsigned char ucBlue;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strName );
    argStream.ReadNumber ( ucRed, 235 );
    argStream.ReadNumber ( ucGreen, 221 );
    argStream.ReadNumber ( ucBlue, 178 );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = g_pGame->GetLuaManager ()->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                CTeam* pTeam = CStaticFunctionDefinitions::CreateTeam ( pResource, strName, ucRed, ucGreen, ucBlue );
                if ( pTeam )
                {
                    CElementGroup * pGroup = pResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( pTeam );
                    }
                    lua_pushelement ( luaVM, pTeam );
                    return 1;
                }
            }
        }


    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #9
0
int CLuaPedDefs::CreatePed ( lua_State* luaVM )
{
    unsigned short usModel;
    CVector vecPosition;
    float fRotation;
    bool bSynced;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadNumber ( usModel );
    argStream.ReadVector3D ( vecPosition );
    argStream.ReadNumber ( fRotation, 0.0f );
    argStream.ReadBool ( bSynced, true );

    if ( !argStream.HasErrors () )
    {
        CLuaMain * pLuaMain = g_pGame->GetLuaManager ()->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource * pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                // Create the ped and return its handle
                CPed* pPed = CStaticFunctionDefinitions::CreatePed ( pResource, usModel, vecPosition, fRotation, bSynced );
                if ( pPed )
                {
                    CElementGroup * pGroup = pResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( pPed );
                    }
                    lua_pushelement ( luaVM, pPed );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #10
0
int CLuaColShapeDefs::CreateColPolygon ( lua_State* luaVM )
{
    CVector2D vecPosition;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadVector2D ( vecPosition );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                // Create it and return it
                CClientColPolygon* pShape = CStaticFunctionDefinitions::CreateColPolygon ( *pResource, vecPosition );
                if ( pShape )
                {
                    // Get the points
                    while ( argStream.NextCouldBeNumber () && argStream.NextCouldBeNumber ( 1 ) )
                    {
                        argStream.ReadVector2D ( vecPosition );
                        pShape->AddPoint ( vecPosition );
                    }

                    CElementGroup * pGroup = pResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( pShape );
                    }
                    lua_pushelement ( luaVM, pShape );
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #11
0
int CLuaColShapeDefs::CreateColPolygon(lua_State* luaVM)
{
    //  colshape createColPolygon ( float fX, float fY, float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, ... )
    std::vector<CVector2D> vecPointList;

    CScriptArgReader argStream(luaVM);
    for (uint i = 0; i < 4 || argStream.NextCouldBeNumber(); i++)
    {
        CVector2D vecPoint;
        argStream.ReadVector2D(vecPoint);
        vecPointList.push_back(vecPoint);
    }

    if (!argStream.HasErrors())
    {
        CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine(luaVM);
        if (pLuaMain)
        {
            CResource* pResource = pLuaMain->GetResource();
            if (pResource)
            {
                CColPolygon* pShape = CStaticFunctionDefinitions::CreateColPolygon(pResource, vecPointList);
                if (pShape)
                {
                    CElementGroup* pGroup = pResource->GetElementGroup();
                    if (pGroup)
                    {
                        pGroup->Add(pShape);
                    }
                    lua_pushelement(luaVM, pShape);
                    return 1;
                }
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

    lua_pushboolean(luaVM, false);
    return 1;
}
예제 #12
0
CElement* CMapManager::LoadMapData ( CResource& Loader, CElement& Parent, CXMLNode& Node )
{
    // Load the elements
    vector < CElement* > ElementsAdded;
    CElement* pLoadedRoot = LoadNode ( Loader, Node, &Parent, &ElementsAdded, false );
    if ( pLoadedRoot )
    {
        // Add all the elements that are entities to a sync packet
        CEntityAddPacket AddPacket;
        vector < CElement* > ::const_iterator iter = ElementsAdded.begin ();
        for ( ; iter != ElementsAdded.end (); iter++ )
        {
            // Is it a per-player entity? Sync it. Otherwize add it to the packet.
            if ( IS_PERPLAYER_ENTITY ( *iter ) )
            {
                static_cast < CPerPlayerEntity* > ( *iter )->Sync ( true );
            }
            else
            {
                AddPacket.Add ( *iter );
            }
        }

        // Send it to everyone
        m_pPlayerManager->BroadcastOnlyJoined ( AddPacket );
        return pLoadedRoot;
    }

    // If unsuccessfull, destroy the new elements. Remember removing it from our element group.
    CElementGroup* pElementGroup = Loader.GetElementGroup ();
    vector < CElement* > ::const_iterator iter = ElementsAdded.begin ();
    for ( ; iter != ElementsAdded.end (); iter++ )
    {
        pElementGroup->Remove ( *iter );
        delete *iter;
    }

    // Failed
    return NULL;
}
예제 #13
0
int CLuaSearchLightDefs::CreateSearchLight(lua_State* luaVM)
{
    //  searchlight createSearchLight ( float startX, float startY, float startZ, float endX, float endY, float endZ, float startRadius, float endRadius [, bool
    //  renderSpot = true ] )
    CVector vecStart, vecEnd;
    float   startRadius, endRadius;
    bool    renderSpot;

    CScriptArgReader argStream(luaVM);
    argStream.ReadVector3D(vecStart);
    argStream.ReadVector3D(vecEnd);
    argStream.ReadNumber(startRadius);
    argStream.ReadNumber(endRadius);
    argStream.ReadBool(renderSpot, true);

    if (!argStream.HasErrors())
    {
        CLuaMain*  pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
        CResource* pResource = pLuaMain ? pLuaMain->GetResource() : nullptr;

        if (pResource)
        {
            auto pLight = CStaticFunctionDefinitions::CreateSearchLight(*pResource, vecStart, vecEnd, startRadius, endRadius, renderSpot);
            if (pLight)
            {
                CElementGroup* pGroup = pResource->GetElementGroup();
                if (pGroup)
                    pGroup->Add(pLight);

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

    lua_pushboolean(luaVM, false);
    return 1;
}
int CLuaFunctionDefs::CreateWeapon ( lua_State* luaVM )
{
    CVector vecPos;
    eWeaponType weaponType;
    CScriptArgReader argStream ( luaVM );
    argStream.ReadEnumStringOrNumber ( weaponType );
    argStream.ReadNumber ( vecPos.fX );
    argStream.ReadNumber ( vecPos.fY );
    argStream.ReadNumber ( vecPos.fZ );

    if ( !argStream.HasErrors () )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResource* pResource = pLuaMain->GetResource ();
            if ( pResource )
            {
                CClientWeapon * pWeapon = CStaticFunctionDefinitions::CreateWeapon ( *pResource, weaponType, vecPos );
                if ( pWeapon )
                {
                    CElementGroup * pGroup = pResource->GetElementGroup();
                    if ( pGroup )
                    {
                        pGroup->Add ( ( CClientEntity* ) pWeapon );
                    }

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

    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #15
0
int CLuaFileDefs::fileCreate ( lua_State* luaVM )
{
//  file fileCreate ( string filePath )
    SString filePath;

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

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );

        if ( !g_pNet->ValidateBinaryFileName ( filePath ) )
        {
            argStream.SetCustomError ( SString ( "Filename not allowed %s", *filePath ), "File error" );
        }
        else
        if ( pLuaMain )
        {
            SString strAbsPath;
            SString strMetaPath;
            CResource* pThisResource = pLuaMain->GetResource ();
            CResource* pResource = pThisResource;
            if ( CResourceManager::ParseResourcePathInput ( filePath, pResource, strAbsPath, strMetaPath ) )
            {
                // Inform file verifier
                g_pClientGame->GetResourceManager()->FileModifedByScript( strAbsPath );

                // Make sure the destination folder exist so we can create the file
                MakeSureDirExists ( strAbsPath.c_str () );

                // Create the file to create
                eAccessType accessType = filePath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC;
                CScriptFile* pFile = new CScriptFile( pThisResource->GetScriptID( ), strMetaPath.c_str(), DEFAULT_MAX_FILESIZE, accessType);
                assert ( pFile );

                // Try to load it
                if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) )
                {
                    // Make it a child of the resource's file root
                    pFile->SetParent ( pResource->GetResourceDynamicEntity () );

                    // 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", *filePath ), "File error" );
                }
            }
        }
    }

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

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #16
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;
}
예제 #17
0
int CLuaFileDefs::fileOpen ( lua_State* luaVM )
{
//  file fileOpen ( string filePath [, bool readOnly = false ] )
    SString filePath; bool readOnly;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( filePath );
    argStream.ReadBool ( readOnly, false );

    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 ( filePath, pResource, strAbsPath, strMetaPath ) )
            {
                // Inform file verifier
                if ( !readOnly )
                    g_pClientGame->GetResourceManager()->FileModifedByScript( strAbsPath );

                // Create the file to create
                eAccessType accessType = filePath[0] == '@' ? eAccessType::ACCESS_PRIVATE : eAccessType::ACCESS_PUBLIC;
                CScriptFile* pFile = new CScriptFile( pThisResource->GetScriptID( ), strMetaPath.c_str( ), DEFAULT_MAX_FILESIZE, accessType );
                assert ( pFile );

                // Try to load it
                if ( pFile->Load ( pResource, readOnly ? CScriptFile::MODE_READ : CScriptFile::MODE_READWRITE ) )
                {
                    // Make it a child of the resource's file root
                    pFile->SetParent ( pResource->GetResourceDynamicEntity () );

                    // 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'", *filePath ) );
                }
            }
        }
    }
    if ( argStream.HasErrors () )
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage() );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #18
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;
}
예제 #19
0
int CLuaFileDefs::fileCreate ( lua_State* luaVM )
{
//  file fileCreate ( string filePath )
    SString strFile;

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

    if ( argStream.NextIsUserData () )
        m_pScriptDebugging->LogCustom ( luaVM, "fileCreate may be using an outdated syntax. Please check and update." );

    if ( !argStream.HasErrors () )
    {
        // Grab our lua VM
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            // Grab the filename
            std::string strAbsPath;
            std::string strSubPath;

            // We have a resource argument?
            CResource* pThisResource = pLuaMain->GetResource ();
            CResource* pResource = pThisResource;
            if ( CResourceManager::ParseResourcePathInput ( strFile, pResource, &strAbsPath, &strSubPath ) )
            {
                // Do we have permissions?
                if ( pResource == pThisResource ||
                     m_pACLManager->CanObjectUseRight ( pThisResource->GetName ().c_str (),
                                                        CAccessControlListGroupObject::OBJECT_TYPE_RESOURCE,
                                                        "ModifyOtherObjects",
                                                        CAccessControlListRight::RIGHT_TYPE_GENERAL,
                                                        false ) )
                {
                    // Make sure the destination folder exist so we can create the file
                    MakeSureDirExists ( strAbsPath.c_str () );
                    
                    // Create the file to create
                    CScriptFile* pFile = new CScriptFile ( pThisResource->GetScriptID(), strSubPath.c_str (), DEFAULT_MAX_FILESIZE );
                    assert ( pFile );

                    // Try to load it
                    if ( pFile->Load ( pResource, CScriptFile::MODE_CREATE ) )
                    {
                        // Add it to the scrpt resource element group
                        CElementGroup* pGroup = pThisResource->GetElementGroup ();
                        if ( pGroup )
                        {
                            pGroup->Add ( pFile );
                        }

                        // Success. Return the file.
                        lua_pushelement ( luaVM, pFile );
                        return 1;
                    }
                    else
                    {
                        // Delete the file again
                        delete pFile;

                        // Output error
                        m_pScriptDebugging->LogWarning ( luaVM, "%s; unable to load file", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ) );
                    }
                }
                else
                    m_pScriptDebugging->LogError ( luaVM, "%s failed; ModifyOtherObjects in ACL denied resource %s to access %s", lua_tostring ( luaVM, lua_upvalueindex ( 1 ) ), pThisResource->GetName ().c_str (), pResource->GetName ().c_str () );
            }
        }
    }
    else
        m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );

    // Failed
    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #20
0
int CLuaBlipDefs::CreateBlip(lua_State* luaVM)
{
    CVector          vecPosition;
    unsigned char    ucIcon = 0;
    int              iSize = 2;
    SColorRGBA       color(255, 0, 0, 255);
    int              iOrdering = 0;
    int              iVisibleDistance = 16383;
    CScriptArgReader argStream(luaVM);
    argStream.ReadVector3D(vecPosition);
    argStream.ReadNumber(ucIcon, 0);
    argStream.ReadNumber(iSize, 2);
    argStream.ReadNumber(color.R, 255);
    argStream.ReadNumber(color.G, 0);
    argStream.ReadNumber(color.B, 0);
    argStream.ReadNumber(color.A, 255);
    argStream.ReadNumber(iOrdering, 0);
    argStream.ReadNumber(iVisibleDistance, 16383);

    if (!CClientRadarMarkerManager::IsValidIcon(ucIcon))
    {
        argStream.SetCustomError("Invalid icon");
    }

    if (iSize < 0 || iSize > 25)
        argStream.SetCustomWarning(SString("Blip size beyond 25 is no longer supported (got %i). It will be clamped between 0 and 25.", iSize));

    if (!argStream.HasErrors())
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
        if (pLuaMain)
        {
            CResource* pResource = pLuaMain->GetResource();
            if (pResource)
            {
                unsigned char  ucSize = Clamp(0, iSize, 25);
                short          sOrdering = Clamp(-32768, iOrdering, 32767);
                unsigned short usVisibleDistance = Clamp(0, iVisibleDistance, 65535);

                // Create the blip
                CClientRadarMarker* pMarker =
                    CStaticFunctionDefinitions::CreateBlip(*pResource, vecPosition, ucIcon, ucSize, color, sOrdering, usVisibleDistance);
                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;
}
예제 #21
0
bool CMapManager::HandleNode ( CResource& Loader, CXMLNode& Node, CElement* pParent, vector < CElement* >* pAdded, bool bIsDuringStart, CElement** pCreated )
{
    // Grab the name
    std::string strBuffer = Node.GetTagName ();

    // Handle it based on the tag name
    CElement* pNode = NULL;
    if ( strBuffer.compare ( "vehicle" ) == 0 )
    {
        pNode = m_pVehicleManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
    }
    else if ( strBuffer.compare ( "object" ) == 0 )
    {
        bool bIsLowLod = false;
        pNode = m_pObjectManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents, bIsLowLod );
    }
    else if ( strBuffer.compare ( "blip" ) == 0 )
    {
        CBlip* pBlip = m_pBlipManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
        pNode = pBlip;
        if ( pBlip )
        {
            pBlip->SetIsSynced ( bIsDuringStart );
        }
    }
    else if ( strBuffer.compare ( "pickup" ) == 0 )
    {
        pNode = m_pPickupManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
    }
    else if ( strBuffer.compare ( "marker" ) == 0 )
    {
        CMarker* pMarker = m_pMarkerManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
        pNode = pMarker;
        if ( pMarker )
        {
            pMarker->SetIsSynced ( bIsDuringStart );
        }
    }
    else if ( strBuffer.compare ( "radararea" ) == 0 )
    {
        CRadarArea* pRadarArea = m_pRadarAreaManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
        pNode = pRadarArea;
        if ( pRadarArea )
        {
            pRadarArea->SetIsSynced ( bIsDuringStart );
        }
    }
    else if ( strBuffer.compare ( "team" ) == 0 )
    {
        pNode = m_pTeamManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
    }
    else if ( strBuffer.compare ( "ped" ) == 0 )
    {
        pNode = m_pPedManager->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
    }
    else if ( strBuffer.empty () )
    {
        // Comment, return true
        return true;
    }
    else
    {
        pNode = m_pGroups->CreateFromXML ( pParent, Node, Loader.GetVirtualMachine (), m_pEvents );
    }

    // Set the node we created in the pointer we were given
    if ( pCreated )
    {
        *pCreated = pNode;
    }

    // Got a node created?
    if ( pNode )
    {
        // Set its typename to the name of the tag
        pNode->SetTypeName ( strBuffer );

        // Add it to our list over elements added
        if ( pAdded )
        {
            pAdded->push_back ( pNode );
        }

        // Add this element to the resource's element group so it's deleted with it
        CElementGroup* pElementGroup = Loader.GetElementGroup ();
        if ( pElementGroup )
            pElementGroup->Add ( pNode );

        // Load the elements below it
        return LoadSubNodes ( Loader, Node, pNode, pAdded, bIsDuringStart );
    }
    return false;
}
예제 #22
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;
}
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, "createProjectile", "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, "createProjectile", "element", 1 );
    }
    else
        m_pScriptDebugging->LogBadType ( luaVM, "createProjectile" );

    lua_pushboolean ( luaVM, false );
    return 1;
}
예제 #24
0
int CLuaDatabaseDefs::DbConnect ( lua_State* luaVM )
{
    //  element dbConnect ( string type, string host, string username, string password, string options )
    SString strType; SString strHost; SString strUsername; SString strPassword; SString strOptions;

    CScriptArgReader argStream ( luaVM );
    argStream.ReadString ( strType );
    argStream.ReadString ( strHost );
    argStream.ReadString ( strUsername, "" );
    argStream.ReadString ( strPassword, "" );
    argStream.ReadString ( strOptions, "" );

    if ( !argStream.HasErrors () )
    {
        CResource* pThisResource = m_pLuaManager->GetVirtualMachineResource ( luaVM );
        if ( pThisResource )
        {
            // If type is sqlite, and has a host, try to resolve path
            if ( strType == "sqlite" && !strHost.empty () )
            {
                // If path starts with :/ then use global database directory
                if ( strHost.BeginsWith ( ":/" ) )
                {
                    strHost = strHost.SubStr ( 1 );
                    if ( !IsValidFilePath ( strHost ) )
                    {
                        argStream.SetCustomError( SString( "host path %s not valid", *strHost ) );
                    }
                    else
                    {
                        strHost = PathJoin ( g_pGame->GetConfig ()->GetGlobalDatabasesPath (), strHost );
                    }
                }
                else
                {
                    std::string strAbsPath;

                    // Parse path
                    CResource* pPathResource = pThisResource;
                    if ( CResourceManager::ParseResourcePathInput ( strHost, pPathResource, &strAbsPath ) )
                    {
                        strHost = strAbsPath;
                        CheckCanModifyOtherResource( argStream, pThisResource, pPathResource );
                    }
                    else
                    {
                        argStream.SetCustomError( SString( "host path %s not found", *strHost ) );
                    }
                }
            }

            if ( !argStream.HasErrors() )
            {
                if ( strType == "mysql" )
                    pThisResource->SetUsingDbConnectMysql( true );

                // Add logging options
                bool bLoggingEnabled;
                SString strLogTag;
                SString strQueueName;
                // Set default values if required
                GetOption < CDbOptionsMap > ( strOptions, "log", bLoggingEnabled, 1 );
                GetOption < CDbOptionsMap > ( strOptions, "tag", strLogTag, "script" );
                GetOption < CDbOptionsMap > ( strOptions, "queue", strQueueName, (strType == "mysql") ? strHost : DB_SQLITE_QUEUE_NAME_DEFAULT );
                SetOption < CDbOptionsMap > ( strOptions, "log", bLoggingEnabled );
                SetOption < CDbOptionsMap > ( strOptions, "tag", strLogTag );
                SetOption < CDbOptionsMap > ( strOptions, "queue", strQueueName );
                // Do connect
                SConnectionHandle connection = g_pGame->GetDatabaseManager ()->Connect ( strType, strHost, strUsername, strPassword, strOptions );
                if ( connection == INVALID_DB_HANDLE )
                {
                    argStream.SetCustomError( g_pGame->GetDatabaseManager()->GetLastErrorMessage() );
                }
                else
                {
                    // Use an element to wrap the connection for auto disconnected when the resource stops
                    // Don't set a parent because the element should not be accessible from other resources
                    CDatabaseConnectionElement* pElement = new CDatabaseConnectionElement ( NULL, connection );
                    CElementGroup * pGroup = pThisResource->GetElementGroup ();
                    if ( pGroup )
                    {
                        pGroup->Add ( pElement );
                    }

                    lua_pushelement ( luaVM, pElement );
                    return 1;
                }
            }
        }
    }

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

    lua_pushboolean ( luaVM, false );
    return 1;
}