AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   New Plugin Submissions (https://forums.alliedmods.net/forumdisplay.php?f=26)
-   -   [Sven Co-op] Static.cfg restoration (https://forums.alliedmods.net/showthread.php?t=279091)

gabuch2 02-15-2016 05:13

[Sven Co-op] Static.cfg restoration
 
3 Attachment(s)
When Sven Co-op 4 was released, the devs implemented a new feature called static.cfg, in which server owners could define specific cvars without worrying that maps could replace them.

This feature was removed in Sven Co-op 5 so I made this simple plugin to restore the file functionality while also including new features, like a map whitelist or the power to edit weaponmodes (you couldn't edit weaponmodes with static.cfg back in SC4). Add weaponmode_shotgun 1 to the static.cfg file and your server will have the classic shotgun in all the maps! :D

Tested, and made for Sven Co-op 5. Technically it may work in other mods but it's not really needed since those can't override their server.cfg settings.

Whitelisting maps:

This plugin allows you to whitelist maps, the plugin will not apply the static values on maps inside the static_whitelist.txt file. The reason of this? Sometimes a map will break if you try to override its variables (overriding the sv_gravity value in the map phobia makes the map unplayable, since you depend on the special gravity setting)

Changelog:
  • 1.0
    • Added a new cvar amx_static_filename, which allows you to use a different static file, useful if you run various servers that may need different configurations.
    • Small optimizations
  • 0.9
    • Tons of optimizations
  • 0.5
    • Cleaned up and optimized the plugin code
    • Fixed a logic error causing a Segmentation Fault in some maps
    • Implemented a Whitelist feature
  • 0.1B
    • Removed a couple of lines causing a redundancy
  • 0.1
    • Initial release

Instalation:
  • Install this plugin like any other
  • Download static.cfg file, edit it at your liking and then place it in your amxx/configs folder.

Cvars:
  • amx_static_check (default "0") - enforces the plugin to re-apply the static.cfg every n seconds. (to prevent map entities to change certain cvars) 0 to disable this feature.
  • amx_static_filename (default "static.cfg") - specifies which static configuration file to use. Useful if you run various servers that may need different configurations.
Credits:
  • fysiks - I used his Bot TK Apology plugin as a basis for the file read method and also helped me with the code

gabuch2 02-15-2016 08:55

Re: Static.cfg restoration
 
Updated

fysiks 02-15-2016 19:54

Re: Static.cfg restoration
 
  • You should cache the contents of static.cfg so that it is only read once at plugin_init().
  • The first argument of set_task() is a floating point value. For example, 2 should be 2.0 and any variables that you use for that argument should have the "Float" tag (or convert the value to a float first).
  • The return value of register_cvar() is a pointer, not the cvar value. You should cache the value of the cvar in public plugin_cfg(). See here for how to properly use cvars.
  • For obtaining the length of a string variable (array), use charsmax() instead of sizeof()-1.

gabuch2 02-16-2016 08:33

Re: Static.cfg restoration
 
1 Attachment(s)
Quote:

Originally Posted by fysiks (Post 2393289)
  • You should cache the contents of static.cfg so that it is only read once at plugin_init().
  • The first argument of set_task() is a floating point value. For example, 2 should be 2.0 and any variables that you use for that argument should have the "Float" tag (or convert the value to a float first).
  • The return value of register_cvar() is a pointer, not the cvar value. You should cache the value of the cvar in public plugin_cfg(). See here for how to properly use cvars.
  • For obtaining the length of a string variable (array), use charsmax() instead of sizeof()-1.

Thanks for the feedback! Let's try with this, haven't tested it though since I'm in the office, but I'll update the OP as soon I'm able to test the plugin.

I also forgot to credit you fysiks, since I used your bot apology plugin as a basis for the file read method.

fysiks 02-16-2016 20:32

Re: Static.cfg restoration
 
I did that plugin a long time ago and have learned to do things more elegantly/properly. I would change these lines:

PHP Code:

#define STATIC_MAX 64
#define STATIC_LEN 64
//...
new staticlist[STATIC_MAX][STATIC_LEN]
//...
    
new data[STATIC_LEN]
//...
    
while( !feof(f) && g_static_count STATIC_MAX
    { 
        
fgets(fdatasizeof(data) - 1

to

PHP Code:

// Removed, Not needed
//...
new staticlist[64][64]
//...
    
new data[sizeof(staticlist[])]
//...
    
while( !feof(f) && g_static_count sizeof(staticlist)) 
    { 
        
fgets(fdatacharsmax(data)) 


You forgot this:

Quote:

Originally Posted by fysiks (Post 2393289)
  • For obtaining the length of a string variable (array), use charsmax() instead of sizeof()-1.


gabuch2 02-22-2016 07:45

Re: Static.cfg restoration
 
Updated!

Anggara_nothing 02-22-2016 10:49

Re: Static.cfg restoration
 
does it works for specific "cvar"? like startarmor?

gabuch2 02-22-2016 15:40

Re: Static.cfg restoration
 
Quote:

Originally Posted by Anggara_nothing (Post 2395625)
does it works for specific "cvar"? like startarmor?

Hello!

Are the cvars 'startarmor', 'starthealth', 'maxarmor' and 'maxhealth' inputable in the server console? If the answer is yes, then yes! They will work with static.cfg

I never tried with these cvars so I do not know if they work.

HamletEagle 04-26-2016 07:15

Re: Static.cfg restoration
 
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.

gabuch2 04-26-2016 10:19

Re: Static.cfg restoration
 
Thank you for reviewing the plugin! It helped me to understand more things.

You're pretty much right about every point but I have to disagree with this one

Quote:

new data[64] since you read maps in it, buffer should be 32, not 64.
In Sven Co-op, mapcycles in the servers are huge! Since very few of them only run stock maps, the mapcycle of a regular server can easily be over 100 maps (mine is almost reaching 200 maps in the cycle). This is unlike a game like Counter-Strike where I haven't seen a server with more than 20 maps in their cycle.

I also have some questions.

Quote:

Reading always the "staticfile" is not acceptable. Please read only one time, in a place like plugin_init and save the content.
I do not understand what do you really mean here.

Quote:

Do you mind using okapi? [...]
What's the difference between that okapi method and https://forums.alliedmods.net/showthread.php?t=154642 this module?


All times are GMT -4. The time now is 22:13.

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