Raised This Month: $12 Target: $400
 3% 

[API] Cvar Util


Post New Thread Reply   
 
Thread Tools Display Modes
Plugin Info:     Modification:   ALL        Category:   Technical/Development       
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-07-2011 , 15:27   [API] Cvar Util
Reply With Quote #1

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
Attached Files
File Type: inc cvar_util.inc (3.0 KB, 637 views)
File Type: sma Get Plugin or Get Source (cvar_util.sma - 1227 views - 17.1 KB)
File Type: zip [signatures]cvar_util.zip (17.3 KB, 499 views)
__________________

Last edited by Arkshine; 04-11-2011 at 11:15. Reason: Added 'Known Bugs'
Arkshine is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-07-2011 , 15:28   Re: [API] Cvar Util
Reply With Quote #2

* reserved *
__________________
Arkshine is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 01-07-2011 , 15:36   Re: [API] Cvar Util
Reply With Quote #3

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.
__________________

Last edited by Arkshine; 01-07-2011 at 16:45.
Arkshine is offline
Xalus
Veteran Member
Join Date: Dec 2009
Location: Belgium
Old 01-07-2011 , 16:21   Re: [API] Cvar Util
Reply With Quote #4

Looks awesome!

Good job!
__________________
Retired.
Xalus is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 01-07-2011 , 16:23   Re: [API] Cvar Util
Reply With Quote #5

Wicked sick!
__________________
xPaw is offline
Pantheon
Member
Join Date: Jan 2011
Old 01-07-2011 , 16:39   Re: [API] Cvar Util
Reply With Quote #6

God Like!
Pantheon is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 01-07-2011 , 21:21   Re: [API] Cvar Util
Reply With Quote #7

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.
__________________
In Flames we trust!
Nextra is offline
nikhilgupta345
Veteran Member
Join Date: Aug 2009
Location: Virginia
Old 01-08-2011 , 00:08   Re: [API] Cvar Util
Reply With Quote #8

Great plugin

<3

new plugin possibilities now with this
__________________
Quote:
Originally Posted by DarkGod View Post
nikhilgupta generates his plugins using sheer awesome.
If you like my work, please
nikhilgupta345 is offline
Send a message via ICQ to nikhilgupta345 Send a message via Yahoo to nikhilgupta345
abdul-rehman
Veteran Member
Join Date: Jan 2010
Location: Khi, Pakistan
Old 01-08-2011 , 03:28   Re: [API] Cvar Util
Reply With Quote #9

Nice job as usual, i prefer that if you make this into a module it will be much better
__________________

My Plugins For ZP

Inactive due to College and Studies
abdul-rehman is offline
Send a message via Yahoo to abdul-rehman Send a message via Skype™ to abdul-rehman
Melisko
Senior Member
Join Date: Feb 2010
Old 01-08-2011 , 05:12   Re: [API] Cvar Util
Reply With Quote #10

Is this plugin useful for normal person like me?
Melisko is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:09.


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