AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Unapproved/Old Plugins (https://forums.alliedmods.net/forumdisplay.php?f=27)
-   -   [API] Cvar Util (https://forums.alliedmods.net/showthread.php?t=147286)

Arkshine 01-07-2011 15:27

[API] Cvar Util
 
3 Attachment(s)
UNSUPPORTED.
SEE THE IMPROVED MODULE VERSION INSTEAD : https://forums.alliedmods.net/showthread.php?t=154642

Cvar Util
- v1.0.0, last updated : 7 jan 2010

This plugin allows you mainly to [un]hook any cvar change or [un]lock any cvar value.

How it works ?

It simply hooks the engine function Cvar_DirectSet() with Orpheu.
In others words, it's called only when a value is set, so the best way.
This function is called at the end from the others.

Code:
pfnCVarSetFloat   -> CVarSetFloat     -> Cvar_SetValue -> Cvar_Set -> Cvar_DirectSet pfnCvarSetString  -> CVarSetString    -> Cvar_Set                  -> Cvar_DirectSet pfnCvar_DirectSet -> PF_Cvar_DirectSet                             -> Cvar_DirectSet Cvar_Command                                                       -> Cvar_DirectSet

Contents :

Requirements top
All Mods.
AMX Mod X 1.8.2 or higher. (dev builds, TrieSetArray is now fixed.)
Orpheu 2.3a or higher.

API top
Available natives/forward :
  • forward CvarHookChanged( handleCvar, const oldValue[], const newValue[] );
  • native CvarHookChange( handleCvar, const callback[] );
  • native CvarEnableHook( handleCvar );
  • native CvarDisableHook( handleCvar );
  • native CvarLockValue( handleCvar, const value[] = "", const Float:fvalue = 0.0 );
  • native CvarUnlockValue( handleCvar );
  • native CvarIsLocked( handleCvar );
  • native CvarGetName( handleCvar, const cvar[], len );

In the callback function, you have the possibility to block a cvar change by returning PLUGIN_HANDLED.

PHP Code:

/**
 * Called when a cvar's value is changed.
 * Returning PLUGIN_HANDLED will block the change.
 *
 * @param handleCvar    Handle to the cvar that was changed.
 * @param oldValue      String containing the value of the convar before it was changed.
 * @param newValue      String containing the new value of the convar.
 */
forward CvarHookChangedhandleCvar, const oldValue[], const newValue[] );

/**
 * Creates a hook for when a cvar's value is changed.
 *
 * Example of callback : OnCvarChange( handleCvar, const oldValue[], const newValue[] )
 * Returning PLUGIN_HANDLED will block the change.
 *
 * @param handleCvar    Handle to the cvar.
 * @param callback      The forward to call.
 */
native CvarHookChangehandleCvar, const callback[] );

/**
 * Starts a forward back up.
 * Use the return value from register_cvar or get_cvar_pointer here.
 *
 * @param handleCvar    The forward to re-enable.
 */
native CvarEnableHookhandleCvar );

/**
 * Stops a ham forward from triggering.
 * Use the return value from register_cvar or get_cvar_pointer here.
 *
 * @param handleCvar    Handle to the cvar.
 * @param handleCvar    The forward to stop.
 */
native CvarDisableHookhandleCvar );

/**
 * Force a cvar to be locked to a provided value.
 * Value which can be either a string or a float.
 *
 * @param handleCvar    Handle to the cvar.
 * @param value         The value to lock as string.
 * @param fvalue        The value to lock as float.
 */
native CvarLockValuehandleCvar, const value[] = "", const Float:fvalue 0.0 );

/**
 * Unlock a cvar to force to use a specific value.
 *
 * @param               Handle to the cvar.
 */
native CvarUnlockValuehandleCvar );

/**
 * Check if a cvar is locked.
 *
 * @param               Handle to the cvar.
 * @return              -1 if not locked otherwise the plugin id from where it's locked.
 */
native CvarIsLockedhandleCvar );

/**
 * Get the cvar name from it's handle.
 *
 * @param handleCvar    Handle to the cvar.
 * @param cvar          The buffer to store the string in.
 * @param len           The string size of the buffer.
 * @return              The length of the cvar name.
 */
native CvarGetNamehandleCvar, const cvar[], len ); 

Example Plugins top
  • Hooking all cvars change :

    PHP Code:

    #include <amxmodx>
    #include <cvar_util>

    public plugin_init()
    {
        
    register_plugin"CU: Hook All Cvars Change""1.0.0""Arkshine" );
    }

    public 
    CvarHookChangedhandleCvar, const oldValue[], const newValue[] )
    {
        new 
    cvarName32 ];
        
    CvarGetNamehandleCvarcvarNamecharsmaxcvarName ) );
        
        
    server_print"[Changed detected] %s : %s (%.2f) -> %s (%.2f)"cvarNameoldValuestr_to_floatoldValue ), newValuestr_to_floatnewValue ) );
        
        
    // return PLUGIN_HANDLED; // You can block a change.


  • Hooking his own cvar :

    PHP Code:

    #include <amxmodx>
    #include <cvar_util>

    public plugin_init()
    {
        
    register_plugin"CU: Hook Own Cvar""1.0.0""Arkshine" );

        
    CvarHookChangeregister_cvar"test""1" ), "OnCvarChange" );
    }

    public 
    OnCvarChangehandleCvar, const oldValue[], const newValue[] )
    {
        
    server_print"[Changed detected] %s : %s -> %s"getCvarNamehandleCvar ), oldValuenewValue );
    }

    stock getCvarName( const handleCvar )
    {
        new 
    cvarName32 ];
        
    CvarGetNamehandleCvarcvarNamecharsmaxcvarName ) );
        
        return 
    cvarName;


  • [Un]Hooking some cvar :

    PHP Code:

    #include <amxmodx>
    #include <cvar_util>

    new HandleCvarNextMap;

    public 
    plugin_init()
    {
        
    register_plugin"CU: [Un]Hook Some Cvar""1.0.0""Arkshine" );

        
    HandleCvarNextMap get_cvar_pointer"amx_nextmap" );
        
        
    register_srvcmd"enablehook""ClientCommand_EnableHook" );
        
    register_srvcmd"disablehook""ClientCommmand_DisableHook" );
        
        
    CvarHookChangeHandleCvarNextMap"OnNextMapChange" );
    }

    public 
    OnNextMapChangehandleCvar, const oldValue[], const newValue[] )
    {
        new 
    cvarName32 ];
        
    CvarGetNamehandleCvarcvarNamecharsmaxcvarName ) );
        
        
    server_print"[Changed detected] %s : %s -> %s"cvarNameoldValuenewValue );
        
        return 
    PLUGIN_HANDLED// Block the change.
    }

    public 
    ClientCommand_EnableHook()
    {
        
    CvarEnableHookHandleCvarNextMap );
    }

    public 
    ClientCommmand_DisableHook()
    {
        
    CvarDisableHookHandleCvarNextMap );


  • [Un]Lock some cvar :

    PHP Code:

    #include <amxmodx>
    #include <cvar_util>

    new HandleCvarFriction;

    public 
    plugin_init()
    {
        
    register_plugin"CU : [Un]Lock Cvar Value""1.0.0""Arkshine" );

        
    HandleCvarFriction get_cvar_pointer"sv_friction" );

        
    register_srvcmd"disablelock""ClientCommmand_DisableLock" );
        
    register_srvcmd"checklock""ClientCommmand_CheckLock" );
        
        
    CvarLockValueHandleCvarFriction"10" ); // or CvarLockValue( HandleCvarFriction, .fvalue = 10.0 );
    }

    public 
    ClientCommmand_DisableLock()
    {
        
    CvarUnlockValueHandleCvarFriction );
    }

    public 
    ClientCommmand_CheckLock()
    {
        
    server_print"Is Cvar Locked ? %s"CvarIsLockedHandleCvarFriction ) != -"Yes" "No" );


Known Bugs top
  • If you put a new plugin (so, installing and restarting) on a running server and you use CvarHookChange() or CvarLockValue() with an external cvar using get_cvar_pointer(), make sure you pass an existing cvar otherwise you will get a restart in loop. It's a consequence of the nasty bug -somehow fixed- where I need to throw a restart for new cvar. I will try to think another way, but right now, be careful.

Installation top
1. Firstly, you need Orpheu. You have just to unzip the content of orpheu_base.zip in ./amxmodx/ ;
2. Then unzip the content of the provided archive here in ./amxmodx/ ;
3. Install the plugin, restart and it's ready. (compiled plugin provided)
4. Now, you can make plugins using this library.
Installation Files top

Arkshine 01-07-2011 15:28

Re: [API] Cvar Util
 
* reserved *

Arkshine 01-07-2011 15:36

Re: [API] Cvar Util
 
As notes :

- It was tested only under windows and counter-strike. Though, it should work for all HL1 mods windows and linux
- I'm not used to make API so I would appreciate any help to improve it.
- There is a very nasty bug somehow fixed with a trick. More details in the .sma. Any help would be greatly appreciated.
- Maybe doing the stuff into a module.

Xalus 01-07-2011 16:21

Re: [API] Cvar Util
 
Looks awesome!

Good job!

xPaw 01-07-2011 16:23

Re: [API] Cvar Util
 
Wicked sick!

Pantheon 01-07-2011 16:39

Re: [API] Cvar Util
 
God Like!

Nextra 01-07-2011 21:21

Re: [API] Cvar Util
 
Absolutely insane work right there! Very nice!

Sadly I don't see this getting used much unless it incorporated into the AMXX Core. This is one of the huge advantages of SourceMod, it would be great to have this.

nikhilgupta345 01-08-2011 00:08

Re: [API] Cvar Util
 
Great plugin :)

<3

new plugin possibilities now with this

abdul-rehman 01-08-2011 03:28

Re: [API] Cvar Util
 
Nice job as usual, i prefer that if you make this into a module it will be much better

Melisko 01-08-2011 05:12

Re: [API] Cvar Util
 
Is this plugin useful for normal person like me? :P


All times are GMT -4. The time now is 20:33.

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