Ejemplo n.º 1
0
bool CLuaArgument::ReadFromJSONObject(json_object* object, std::vector<CLuaArguments*>* pKnownTables)
{
    DeleteTableData();

    if (!object)
        m_iType = LUA_TNIL;
    else
    {
        switch (json_object_get_type(object))
        {
            case json_type_null:
                m_iType = LUA_TNIL;
                break;
            case json_type_boolean:
                if (json_object_get_boolean(object) == TRUE)
                    ReadBool(true);
                else
                    ReadBool(false);
                break;
            case json_type_double:
            case json_type_int:
                ReadNumber(json_object_get_double(object));
                break;
            case json_type_object:
                m_pTableData = new CLuaArguments();
                m_pTableData->ReadFromJSONObject(object, pKnownTables);
                m_bWeakTableRef = false;
                m_iType = LUA_TTABLE;
                break;
            case json_type_array:
                m_pTableData = new CLuaArguments();
                m_pTableData->ReadFromJSONArray(object, pKnownTables);
                m_bWeakTableRef = false;
                m_iType = LUA_TTABLE;
                break;
            case json_type_string:
            {
                int     iLength = json_object_get_string_len(object);
                SString strString;
                strString.assign(json_object_get_string(object), iLength);
                if (iLength > 3 && strString[0] == '^' && strString[2] == '^' && strString[1] != '^')
                {
                    switch (strString[1])
                    {
                        case 'E':            // element
                        {
                            int       id = atoi(strString.c_str() + 3);
                            CElement* element = NULL;
                            if (id != INT_MAX && id != INT_MIN && id != 0)
                                element = CElementIDs::GetElement(id);
                            if (element)
                            {
                                ReadElement(element);
                            }
                            else
                            {
                                // Appears sometimes when a player quits
                                // g_pClientGame->GetScriptDebugging()->LogError ( NULL, "Invalid element specified in JSON string '%s'.", szString );
                                m_iType = LUA_TNIL;
                            }
                            break;
                        }
                        case 'R':            // resource
                        {
                            CResource* resource = g_pGame->GetResourceManager()->GetResource(strString.c_str() + 3);
                            if (resource)
                            {
                                ReadScriptID(resource->GetScriptID());
                            }
                            else
                            {
                                g_pGame->GetScriptDebugging()->LogError(NULL, "Invalid resource specified in JSON string '%s'.", strString.c_str());
                                m_iType = LUA_TNIL;
                            }
                            break;
                        }
                        case 'T':            // Table reference
                        {
                            unsigned long ulTableID = static_cast<unsigned long>(atol(strString.c_str() + 3));
                            if (pKnownTables && ulTableID < pKnownTables->size())
                            {
                                m_pTableData = pKnownTables->at(ulTableID);
                                m_bWeakTableRef = true;
                                m_iType = LUA_TTABLE;
                            }
                            else
                            {
                                g_pGame->GetScriptDebugging()->LogError(NULL, "Invalid table reference specified in JSON string '%s'.", strString.c_str());
                                m_iType = LUA_TNIL;
                            }
                            break;
                        }
                    }
                }
                else
                    ReadString(strString);
                break;
            }
            default:
                return false;
        }
    }
    return true;
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
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;
}
Ejemplo n.º 5
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;
}