View Single Post
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-26-2016 , 07:15   Re: Static.cfg restoration
Reply With Quote #9

1.get_mapname(currentmap, 32) this is wrong. The last item from a string array should be the null terminator, so you need to use 32 - 1 as buffer size. Also, instead of hardcoding the dimension, use charsmax() to retrieve it. Also replace your sizeof -1 with charsmax(), for readability.
2.If map is on whitelist pause the plugin.
3.In VerifyWhitelist:
  • whitelistfile[64] increase buffer size, 64 is too low. Don't forget that configs dir path can be changed from core.ini.
  • Check if f pointer is not 0 before looping.
  • Why you count the entries from file? This does not make sense, remove g_whitelist_count variable.
  • new data[64] since you read maps in it, buffer should be 32, not 64.
  • Use fclose after you finished reading.
  • In equal you don't need to use the 3rd param.
  • If the check was passed and you set IsMapOnWhitelist = 1 break the while loop. You found what you need.
  • Not any function needs to be public. You would put public for things that will be called from outside of your plugin(like task, menu handlers, commands, forwards, etc). Here is not the case, so remove public keyword(function will become 'private').
4.In LoadStaticCFGs:
  • Reading always the "staticfile" is not acceptable. Please read only one time, in a place like plugin_init and save the content.
  • I really don't like your without-end looping task and applying cvars every cycle. Do you mind using okapi? I'm asking because in engine, there is a function called Cvar_DirectSet and every cvar change is passed in this function. You could hook it and apply cvar default values only when something tries to change them.
    That way, cvars will be applied only when needed and continous checking is not needed.

Here is how you should hook it:
PHP Code:
#include <amxmodx>
#include <okapi>

public plugin_init()
{
    new 
FunctionAddress
    
new okapi_func:HandleCvar_DirectSetFunc
    
    
//Cvar_DirectSetSignature is for identifying the function under windows
    //Cvar_DirectSetSymbol is for linux
    
new const Cvar_DirectSetSignature[] = {0x55,0x8B,0xDFF,0x81,0xDFF,0xDFF,0xDFF,0xDFF,0xDFF,0x53,0x8B,0xDFF,0xDFF,0x56,0x8B,0xDFF,0xDFF,0x85,0xDFF,0x57}
    new const 
Cvar_DirectSetSymbol   [] = "Cvar_DirectSet_0"
    
    
//okapi_engine_find_sig searches for the signature in the windows binary
    //okapi_engine_get_symbol_ptr searches for the signature in linux binary
    //this two functions retrieve the address of the function if it is found, 0 otherwise
    
if
    ( 
        (
FunctionAddress okapi_engine_get_symbol_ptr(Cvar_DirectSetSymbol)) || 
        (
FunctionAddress okapi_engine_find_sig(Cvar_DirectSetSignaturesizeof Cvar_DirectSetSignature)) 
    ) 
    { 
        
//okapi_build_function attaches okapi to our function 
        //FunctionAddress is the address we just retrieved
        //arg_void is the return type of the Cvar_DirectSet function(it does not return anything, so we use void)
        //arg_int is the first param of Cvar_DirectSet(the cvar pointer)
        //arg_string is the second param of Cvar_DirectSet(the cvar value, always passed as a string)
        
HandleCvar_DirectSetFunc okapi_build_function(FunctionAddressarg_voidarg_intarg_string)
        
        
//okapi_add_hook hooks the above function 
        //false means we hook it as pre, so you can block the change
        
okapi_add_hook(HandleCvar_DirectSetFunc"OnCvar_DirectSet"false)
    }
    
}

public 
OnCvar_DirectSet(const CvarHandle, const CvarValue[])
{

Then, when you read the cvars in init use get_cvar_pointer on them and save the return value so you can compare in Cvar_DirectSet.
To block the change, simply do return okapi_ret_supercede.
__________________

Last edited by HamletEagle; 04-26-2016 at 07:20.
HamletEagle is offline