AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [INC] nVault Utility (https://forums.alliedmods.net/showthread.php?t=139584)

Bugsy 10-02-2010 21:58

[INC] nVault Utility
 
2 Attachment(s)
nVault Utility
Update:
  • Removed nvault_get_array(), nvault_set_array(), nvault_util_read_array() and nvault_util_readall_array(). These functions were moved to the nVault Array include.

Note: The nVault array functions require that you use a fixed version of the nVault module. During the development of these new functions, a bug was discovered in the nVault module that will result in invalid values and possibly errors so an update is required. This fixed module is attached below and also available in AMX-X dev builds >= 4589. Thanks to Arkshine for fixing this so quickly for me. This fixed module is only required for nvault_set_array(), nvault_get_array(), nvault_util_read_array(), and nvault_util_readall_array(), but I recommend everyone updates anyway. You should be careful if your vault contains a mixture of regular entries and array entries if you plan to use the read and read_all functions. There is currently no code in place to differentiate the difference between a native string entry and an array entry and you could get data that you do not expect. This will be implemented in a future release.

This include file provides the ability to:
  • Retrieve the number of entries in a vault file.
  • Iterate through all entries one at a time.
  • Iterate through all entries with a single function call.
  • Store arrays in nvault.

Functions
  • nvault_util_open() - Open a vault.
  • nvault_util_close() - Close a vault.
  • nvault_util_count() - Number of items stored in vault.
  • nvault_util_pos() - Returns the file position of current vault.
  • nvault_util_read() - Read an entry.
  • nvault_util_readall() - Read all entries.
  • nvault_util_read_array() - Read an array.
  • nvault_util_readall_array() - Read all array entries from vault.

Descriptions
  • nvault_util_open( const szVault[] )
    This opens a vault file and returns a vault handle. Vault handles used by nvault and nvault_util are NOT interchangeable. If you are using nvault in your plugin in combination with nvault_util, you will be using 2 different nvault handles, one for each.
    Code:

    szVault[] - Name of vault to open (Usage is identical to nvault_open).
  • nvault_util_close( iVaultHandle )
    Close a vault handle. Again, this is only for closing vaults that were opened with nvault_util.
    Code:

    iVaultHandle - Vault handle to close.
  • nvault_util_count( iVaultHandle )
    This will return the total number of entries saved in a vault. An entry consists of [key,value,timestamp].
    Code:

    iVaultHandle - Vault handle to get number of entries.
  • nvault_util_pos( iVaultHandle )
    This function returns the current file position of the vault file. This can be used if, for any reason, you need to know exactly where in a vault file a particular entry exists. The return value is equal to ftell().
    Code:

    iVaultHandle - Vault handle to get position.
  • nvault_util_read( iVaultHandle , iOffset , szKey[] , iKeySize , szVal[] , iValSize , &iTimeStamp )
    This will read an entry from the vault file. The function returns an offset value which can then be used for the next function call to read the next entry in the vault file. Do NOT pass a random value as the offset because this will most likely cause a crash; use only 0 or a value returned by the function.
    Code:

    iVaultHandle - Vault handle to read from.
    iOffset - Offset to read from. Pass only 0 or a value that was returned from a previous nvault_util_read() call. Passing any other values will most likely result in a crash.
    szKey[] - The key for entry. (passed by ref)
    iKeySize - Max chars to read for key. (use charsmax( szKey ))
    szVal[] - The value for entry. (passed by ref) ,
    iValSize - Max chars to read for value. (use charsmax( szVal))
    iTimeStamp (Optional) - Timestamp for entry. (passed by ref)

  • nvault_util_readall( iVaultHandle , const szForwardFunc[] )
    This will read all entries in a vault file. Put all of your data handling in the forward function. You can use iCurrent and iTotal for determining exactly how many entries have been read and how many are left to read.
    Code:

    iVaultHandle - Vault handle the read from.
    szForwardFunc[] - Forward function where data from vault reading will be retrieved.

    Forward function params
    ForwardFunc( iCurrent , iTotal , const szKey[] , const szVal[] , iTimeStamp , Data[] , iSize )

    Code:

    iCurrent = Current entry being read.
    iTotal = Total number of entries in vault.
    szKey[] = Key for current entry.
    szVal[] = Value for current entry.
    iTimeStamp = Time stamp for current entry.
    Data[] = Array containing any data you need passed to forward.
    iSize = The size of data being passed.

  • nvault_util_read_array( iVaultHandle , iOffset , szKey[] , iKeySize , iArray[] , iArraySize , &iItemsRead , &iTimeStamp )
    This will read an array entry from the vault file. The function returns an offset value which can then be used for the next function call to read the next entry in the vault file. Do NOT pass a random value as the offset because this will most likely cause a crash; use only 0 or a value returned by the function.
    Code:

    iVaultHandle - Vault handle to read from.
    iOffset - Offset to read from. Pass only 0 or a value that was returned from a previous nvault_util_read() call. Passing any other values will most likely result in a crash.
    szKey[] - The key for entry. (passed by ref)
    iKeySize - Max chars to read for key. (use charsmax( szKey ))
    iArray[] - The array data for the entry. (passed by ref) ,
    iArraySize - Size of array for the data. (use sizeof( iArray ))
    iItemsRead (Optional) - The number of array items that were read (passed by ref)
    iTimeStamp (Optional) - Timestamp for entry. (passed by ref)

  • nvault_util_readall_array( iVaultHandle , const szForwardFunc[] )
    This will read all array entries in a vault file. Put all of your data handling in the forward function. You can use iCurrent and iTotal for determining exactly how many entries have been read and how many are left to read. Use iItemsRead to determine how many items in each array were read.
    Code:

    iVaultHandle - Vault handle the read from.
    szForwardFunc[] - Forward function where data from vault reading will be retrieved.

    Forward function params
    ForwardFunc( iCurrent , iTotal , const szKey[] , const iArray[] , iItemsRead , iTimeStamp , Data[] , iSize )

    Code:

    iCurrent = Current entry being read.
    iTotal = Total number of entries in vault.
    szKey[] = Key for current entry.
    iArray[] = Value for current entry.
    iItemsRead = The number of array items that were read for the current entry.
    iTimeStamp = Time stamp for current entry.
    Data[] = Array containing any data you need passed to forward.
    iSize = The size of data being passed.

nVault Utility example
Spoiler

Emp` 10-02-2010 22:55

Re: [INC] nVault Utility
 
Nicely done.

K.K.Lv 10-03-2010 02:17

Re: [INC] nVault Utility
 
good job man

xiaojian 10-08-2010 01:33

Re: [INC] nVault Utility
 
perfectly good!

Enum 10-18-2010 21:40

Re: [INC] nVault Utility
 
Perfect ;)

K.K.Lv 11-06-2010 22:44

Re: [INC] nVault Utility
 
Code:

public plugin_cfg()
{
    g_iValut = nvault_util_open( "valut" );
}
public client_authorized( id )
{
    get_user_authid( id , g_szAuthid[ id ] , charsmax( g_szAuthid[] ) );
 
    new iPos , szKey[ 32 ] , szVal[ 64 ] , iTimeStamp;
    new iCount = nvault_util_count( g_iValut );
 
    for ( new iCurrent = 1 ; iCurrent <= iCount ; iCurrent++ )
    {
        iPos = nvault_util_read( g_iValut , iPos , szKey , charsmax( szKey ) , szVal , charsmax( szVal ) , iTimeStamp );
        if ( equal( szKey , g_szAuthid[ id ] ) )
        {
            g_iVal[ id ] = str_to_num( szVal );
            break;
        }
    }
}
 
public plugin_end()
{
    nvault_util_close( g_iValut );
}

Is it right ?
if so, the problem is coming,
1. whether I should use the loop to get the player data when player is connecting to the server ?
2. If I want to set player data, just use nvault's nvault_set(), right ?

Exolent[jNr] 11-06-2010 23:32

Re: [INC] nVault Utility
 
Don't keep the vault open the whole map since it is a file handle and not an actual vault.
Open it each time it's being used.

Also, don't use a loop if you are just reading a value for a known key. Use nvault_get().

Yes, nvault_set() will work, but nvault_open() and nvault_util_open() are 2 different handles so you cannot mix handles between the nvault and nvault util.

K.K.Lv 11-07-2010 00:06

Re: [INC] nVault Utility
 
the problem is I want to get all entry from the valut when map start and new round, and set the data in clinet disconnect, round end ?

how to get it ?
just use nvalut can handle it ?

Exolent[jNr] 11-07-2010 01:40

Re: [INC] nVault Utility
 
This utility is only for when you need to read all entries in an nvault.
In all other cases, use the default natives for nvault.

Jacob 11-09-2010 21:28

Re: [INC] nVault Utility
 
Quote:

Originally Posted by Exolent[jNr] (Post 1343716)
Don't keep the vault open the whole map since it is a file handle and not an actual vault.
Open it each time it's being used.

Also, don't use a loop if you are just reading a value for a known key. Use nvault_get().

Yes, nvault_set() will work, but nvault_open() and nvault_util_open() are 2 different handles so you cannot mix handles between the nvault and nvault util.

Exolent, if keep the vault open the whole map,will it cause Reliable channel overflowed ?


All times are GMT -4. The time now is 19:07.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.