AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Stack error with hook_cvar_change (https://forums.alliedmods.net/showthread.php?t=276710)

luxor 12-26-2015 18:15

Stack error with hook_cvar_change
 
hello, i receive stack error when i'm trying to set any value for rcon_password.
what i was doing wrong ?

Code:

#include <amxmisc>

#if AMXX_VERSION_NUM < 183
    #assert AMX Mod X v1.8.3 or later library required!
#endif

public plugin_init()
{
        register_plugin
        (
                .plugin_name = "Block changing rcon_password",
                .version    = "1.0",
                .author      = "lüxor"
        );
       
        hook_cvar_change(get_cvar_pointer("rcon_password"), "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
        set_pcvar_string(PointerCvar, OldValue);
}


Console :
Code:

] restart
Server logging data to file logs\L1227013.log
L 12/27/2015 - 01:11:05: -------- Mapchange to de_dust2 --------
[AMXX] Loaded 1 admin from file
Executing AMX Mod X Configuration File
Scrolling message displaying frequency: 10:00 minutes
] rcon_password
"rcon_password" is "123"
] rcon_password meow
L 12/27/2015 - 01:12:02: [AMXX] Displaying debug trace (plugin "block_changing_rcon_pass.amxx", version "1.0")
L 12/27/2015 - 01:12:02: [AMXX] Run time error 3: stack error


addicted2sex 12-26-2015 20:17

Re: Stack error with hook_cvar_change
 
Quote:

Originally Posted by luxor (Post 2376618)
hello, i receive stack error when i'm trying to set any value for rcon_password.
what i was doing wrong ?

Code:

#include <amxmisc>

#if AMXX_VERSION_NUM < 183
    #assert AMX Mod X v1.8.3 or later library required!
#endif

public plugin_init()
{
        register_plugin
        (
                .plugin_name = "Block changing rcon_password",
                .version    = "1.0",
                .author      = "lüxor"
        );
       
        hook_cvar_change(get_cvar_pointer("rcon_password"), "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
        set_pcvar_string(PointerCvar, OldValue);
}


Console :
Code:

] restart
Server logging data to file logs\L1227013.log
L 12/27/2015 - 01:11:05: -------- Mapchange to de_dust2 --------
[AMXX] Loaded 1 admin from file
Executing AMX Mod X Configuration File
Scrolling message displaying frequency: 10:00 minutes
] rcon_password
"rcon_password" is "123"
] rcon_password meow
L 12/27/2015 - 01:12:02: [AMXX] Displaying debug trace (plugin "block_changing_rcon_pass.amxx", version "1.0")
L 12/27/2015 - 01:12:02: [AMXX] Run time error 3: stack error


Because you're changing the cvar itself when it is changed. The situation is the following:
  • "rcon_password" is changed to "meow"
  • setDefaultValue() is called
  • setDefaultValue is setting "rcon_password" to the old value and because of the change setDefaultValue() is called again

because of that an "infinite recursion" appears and the stack overflows

luxor 12-26-2015 20:27

Re: Stack error with hook_cvar_change
 
oh... i see, that explains all, thx

PartialCloning 12-26-2015 20:58

Re: Stack error with hook_cvar_change
 
Code:

#include <amxmisc>

#if AMXX_VERSION_NUM < 183
    #assert AMX Mod X v1.8.3 or later library required!
#endif

new cvarhook:rconhandle;
public plugin_init()
{
          register_plugin
          (
                    .plugin_name = "Block changing rcon_password",
                    .version    = "1.0",
                    .author      = "lüxor"
          );

          rconhandle = hook_cvar_change(get_cvar_pointer("rcon_password"), "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
          if(!equal(NewValue, OldValue))
          {
                    disable_cvar_hook(rconhandle);

                    set_pcvar_string(PointerCvar, OldValue);
                    enable_cvar_hook(rconhandle);
          }

}


luxor 12-27-2015 05:28

Re: Stack error with hook_cvar_change
 
thanks for try to help, but your change won't affect the problem.
anyway i solved it, thanks for explaining addicted2sex

PartialCloning 12-27-2015 09:25

Re: Stack error with hook_cvar_change
 
I didn't expect it to not work with PLUGIN_HANDLED; I edited it now, it should work. I know you already found your own solution, but:

1. The code above might be simpler if you used a different method.
2. You did not share your solution, meaning if someone looks up a similar problem they won't know how to fix it, now they know.

luxor 12-27-2015 19:51

Re: Stack error with hook_cvar_change
 
Quote:

Originally Posted by PartialCloning (Post 2376826)
I didn't expect it to not work with PLUGIN_HANDLED; I edited it now, it should work. I know you already found your own solution, but:

1. The code above might be simpler if you used a different method.
2. You did not share your solution, meaning if someone looks up a similar problem they won't know how to fix it, now they know.

Code:

#include <amxmisc>

#if AMXX_VERSION_NUM < 183
    #assert AMX Mod X v1.8.3 or later library required!
#endif

new RconPasswordValue[64];

public plugin_init()
{
        register_plugin
        (
                .plugin_name = "Block changing rcon_password",
                .version    = "1.0",
                .author      = "lüxor"
        );
        new RconPasswordCvarPointer = get_cvar_pointer("rcon_password");
        get_pcvar_string(RconPasswordCvarPointer, RconPasswordValue, charsmax(RconPasswordValue));
        hook_cvar_change(RconPasswordCvarPointer, "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
        if ( !equal(NewValue, RconPasswordValue, charsmax(RconPasswordValue)) )
        {
                set_pcvar_string(PointerCvar, OldValue);
        }
}


WildCard65 12-27-2015 20:52

Re: Stack error with hook_cvar_change
 
Quote:

Originally Posted by luxor (Post 2377080)
Code:

#include <amxmisc>

#if AMXX_VERSION_NUM < 183
    #assert AMX Mod X v1.8.3 or later library required!
#endif

new RconPasswordValue[64];

public plugin_init()
{
        register_plugin
        (
                .plugin_name = "Block changing rcon_password",
                .version    = "1.0",
                .author      = "lüxor"
        );
        new RconPasswordCvarPointer = get_cvar_pointer("rcon_password");
        get_pcvar_string(RconPasswordCvarPointer, RconPasswordValue, charsmax(RconPasswordValue));
        hook_cvar_change(RconPasswordCvarPointer, "setDefaultValue");
}

public setDefaultValue(PointerCvar, const OldValue[], const NewValue[])
{
        if ( !equal(NewValue, RconPasswordValue, charsmax(RconPasswordValue)) )
        {
                set_pcvar_string(PointerCvar, OldValue);
        }
}


That is a sane solution in my opinion.

luxor 12-28-2015 04:47

Re: Stack error with hook_cvar_change
 
ik, but i told him that i solved it


All times are GMT -4. The time now is 17:52.

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