AMX Mod X Plugin Approver
|
01-07-2011
, 15:27
[API] Cvar Util
|
#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 CvarHookChanged( handleCvar, 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 CvarHookChange( handleCvar, 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 CvarEnableHook( handleCvar );
/**
* 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 CvarDisableHook( handleCvar );
/**
* 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 CvarLockValue( handleCvar, const value[] = "", const Float:fvalue = 0.0 );
/**
* Unlock a cvar to force to use a specific value.
*
* @param Handle to the cvar.
*/
native CvarUnlockValue( handleCvar );
/**
* 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 CvarIsLocked( handleCvar );
/**
* 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 CvarGetName( handleCvar, 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 CvarHookChanged( handleCvar, const oldValue[], const newValue[] )
{
new cvarName[ 32 ];
CvarGetName( handleCvar, cvarName, charsmax( cvarName ) );
server_print( "[Changed detected] %s : %s (%.2f) -> %s (%.2f)", cvarName, oldValue, str_to_float( oldValue ), newValue, str_to_float( newValue ) );
// 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" );
CvarHookChange( register_cvar( "test", "1" ), "OnCvarChange" );
}
public OnCvarChange( handleCvar, const oldValue[], const newValue[] )
{
server_print( "[Changed detected] %s : %s -> %s", getCvarName( handleCvar ), oldValue, newValue );
}
stock getCvarName( const handleCvar )
{
new cvarName[ 32 ];
CvarGetName( handleCvar, cvarName, charsmax( cvarName ) );
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" );
CvarHookChange( HandleCvarNextMap, "OnNextMapChange" );
}
public OnNextMapChange( handleCvar, const oldValue[], const newValue[] )
{
new cvarName[ 32 ];
CvarGetName( handleCvar, cvarName, charsmax( cvarName ) );
server_print( "[Changed detected] %s : %s -> %s", cvarName, oldValue, newValue );
return PLUGIN_HANDLED; // Block the change.
}
public ClientCommand_EnableHook()
{
CvarEnableHook( HandleCvarNextMap );
}
public ClientCommmand_DisableHook()
{
CvarDisableHook( HandleCvarNextMap );
}
- [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" );
CvarLockValue( HandleCvarFriction, "10" ); // or CvarLockValue( HandleCvarFriction, .fvalue = 10.0 );
}
public ClientCommmand_DisableLock()
{
CvarUnlockValue( HandleCvarFriction );
}
public ClientCommmand_CheckLock()
{
server_print( "Is Cvar Locked ? %s", CvarIsLocked( HandleCvarFriction ) != -1 ? "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
__________________
Last edited by Arkshine; 04-11-2011 at 11:15.
Reason: Added 'Known Bugs'
|
|