Загрузка данных


/* ~ Includes. ~ */
#include < amxmisc >
#include < fakemeta >
#include < reapi >
#include < json >

/* ~ We cut off old versions. ~ */
#if AMXX_VERSION_NUM < 190
    #assert Outdated version of AMX Mod X (AMXX_VERSION_NUM), please update in order to use this plugin properly.
#endif

/* ~ Sub-includes. ~ */
#include < bio >

/* ~ Special instruction. ~ */
#pragma semicolon 1

/* ~ Hash maps. ~ */
new Trie: m_trUselessEntities = Invalid_Trie;

/* ~ Forwards. ~ */
new m_fwdSpawn = INVALID_HANDLE;

/* ~ This forward allows plugins to add models, sounds and generic files to the precache tables using the precache_* set of functions. ~ */
public plugin_precache( )
{
    // Sets informations about the calling plugin.
    register_plugin( "[BIO] Useless Entities", BIO_VERSION, "oxsi. x Vision" );

    // We read the JSON file before the gameplay begins.
    @Read_JsonFile( "useless_entities" );
}

public plugin_init( )
{
    RegisterHookChain( RH_SV_ActivateServer, "@SV_ActivateServer_Post", true );
}

public plugin_end( )
{
    TrieDestroy( m_trUselessEntities );
}

@SV_ActivateServer_Post( )
{
    if ( !m_fwdSpawn )
        return;

    unregister_forward( FM_Spawn, m_fwdSpawn );
    m_fwdSpawn = INVALID_HANDLE;
    TrieDestroy( m_trUselessEntities );
}

@FM_Spawn_Pre( entity )
{
    new className[ 32 ];
    get_entvar( entity, var_classname, className, charsmax( className ) );

    if ( TrieKeyExists( m_trUselessEntities, className ) )
    {
        forward_return( FMV_CELL, NULLENT );
        return FMRES_SUPERCEDE;
    }

    return FMRES_IGNORED;
}

@Read_JsonFile( const file[ ] )
{
    new path[ PLATFORM_MAX_PATH ];
    get_localinfo( "amxx_configsdir", path, charsmax( path ) );
    add( path, charsmax( path ), "/bio" );

    if ( !dir_exists( path ) )
    {
        set_fail_state( "Directory '%s' not found.", path );
        return;
    }

    strcat( path, fmt( "/%s.json", file ), charsmax( path ) );

    if ( !file_exists( path ) )
    {
        set_fail_state( "File '%s' not found.", path );
        return;
    }

    new JSON: handle = json_parse( path, true );
    if ( handle == Invalid_JSON )
    {
        set_fail_state( "Failed to parse JSON File '%s'.", path );
        return;
    }

    new JSON: object = json_object_get_value( handle, "useless_entities" );
    if ( !json_is_array( object ) )
    {
        set_fail_state( "Filed 'useless_entities' must be an array.", path );
        json_free( object );
        return;
    }

    new count = json_array_get_count( object );
    if ( count <= 0 )
    {
        json_free( object );
        return;
    }

    m_trUselessEntities = TrieCreate( );

    new className[ 32 ];
    for ( new i = 0; i < count; i++ )
    {
        json_array_get_string( object, i, className, charsmax( className ) );

        if ( !className[ 0 ] )
            continue;

        TrieSetCell( m_trUselessEntities, className, 1 );
    }

    if ( TrieGetSize( m_trUselessEntities ) )
        m_fwdSpawn = register_forward( FM_Spawn, "@FM_Spawn_Pre" );
    else TrieDestroy( m_trUselessEntities );

    json_free( object );
}