AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   .INI File Cleaner or Enabler/Disabler (https://forums.alliedmods.net/showthread.php?t=309542)

EFFx 07-29-2018 19:32

.INI File Cleaner or Enabler/Disabler
 
I searched a little and didn't find something similar to what I was looking for, and I decided to try by myself and made a code that can disable and enable a whole .ini file or a single line by a command.

Put your .ini file here

PHP Code:

new const g_szIniFileName[] =    "plugins-custom.ini" 

Used an exemple when you wanna enable/disable bunnyhop plugin in the plugins-custom.ini:

Source code:

PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN ".Ini file Enabler/Disabler"
#define VERSION "1.0"
#define AUTHOR "EFFx"

new const g_szIniFileName[] =    "plugins-custom.ini"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("amx_clean""cmdClean"ADMIN_IMMUNITY"<1|0 - Clean whole file>")
    
register_clcmd("amx_remove""cmdRemove"ADMIN_IMMUNITY"<1|0 - Remove whole file>")
}

public 
cmdClean(idlevelcid)
{
    if(!
cmd_access(idlevelcid2))
        return 
PLUGIN_HANDLED

    
new szWholeFile[2], bool:bIsWholeFile
    read_argv
(1szWholeFilecharsmax(szWholeFile))
    
    
bIsWholeFile bool:str_to_num(szWholeFile)
    if(!
enableLine(bIsWholeFilebIsWholeFile "" "bunnyhop.amxx"))
    {
        
console_print(id"[AMXX]: Line not found")
        return 
PLUGIN_HANDLED
    
}
    return 
PLUGIN_HANDLED
}

public 
cmdRemove(idlevelcid)
{
    if(!
cmd_access(idlevelcid2))
        return 
PLUGIN_HANDLED

    
new szWholeFile[2], bool:bIsWholeFile
    read_argv
(1szWholeFilecharsmax(szWholeFile))

    
bIsWholeFile bool:str_to_num(szWholeFile)
    if(!
disableLine(bIsWholeFilebIsWholeFile "" "bunnyhop.amxx"))
    {
        
console_print(id"[AMXX]: Line not found")
        return 
PLUGIN_HANDLED
    
}
    return 
PLUGIN_HANDLED
}

enableLine(bool:bWholeFileszLine[])
{
    new 
szFileName[64], szText[512]
    
get_configsdir(szFileNamecharsmax(szFileName))
    
format(szFileNamecharsmax(szFileName), "%s/%s"szFileNameg_szIniFileName)

    new 
iFile fopen(szFileName"rt"), i
    
while(!feof(iFile))
    {
        
fgets(iFileszTextcharsmax(szText))
        
trim(szText)
        
        
i++

        if(
szText[0] == ';')
        {
            if(!
bWholeFile && !equal(szText[1], szLine))
                continue
                        
            
replace(szTextcharsmax(szText), ";""")
            
write_file(szFileNameszText, (1))
            return 
true
        
}
    }
    
fclose(iFile)
    return 
false


disableLine(bool:bWholeFileszLine[])
{
    new 
szFileName[64], szText[512]
    
get_configsdir(szFileNamecharsmax(szFileName))
    
format(szFileNamecharsmax(szFileName), "%s/%s"szFileNameg_szIniFileName)

    new 
iFile fopen(szFileName"rt"), i
    
while(!feof(iFile))
    {
        
fgets(iFileszTextcharsmax(szText))
        
trim(szText)
        
        
i++

        if(
szText[0] != ';')
        {
            if(!
bWholeFile && !equal(szTextszLine))
                continue

            
format(szTextcharsmax(szText), ";%s"szText)
            
write_file(szFileNameszText, (1))
            return 
true
        
}
    }
    
fclose(iFile)
    return 
false


Used an example to completely remove the plugin instead of only add a ';' before it's name:

Source Code:

PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN ".Ini file cleaner"
#define VERSION "1.0"
#define AUTHOR "EFFx"

new const g_szIniFileName[] =    "plugins-pubg.ini"

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("amx_clean""cmdClean"ADMIN_IMMUNITY"<1|0 - Clean whole file>")
}

public 
cmdClean(idlevelcid)
{
    if(!
cmd_access(idlevelcid2))
        return 
PLUGIN_HANDLED

    
new szWholeFile[2], bool:bIsWholeFile
    read_argv
(1szWholeFilecharsmax(szWholeFile))
    
    
bIsWholeFile bool:str_to_num(szWholeFile)
    if(!
cleanFile(bIsWholeFilebIsWholeFile "" "bunnyhop.amxx"))
    {
        
console_print(id"[AMXX]: Line not found")
        return 
PLUGIN_HANDLED
    
}
    return 
PLUGIN_HANDLED
}

cleanFile(bool:bWholeFileszLine[])
{
    new 
szFileName[64], szText[512]
    
get_configsdir(szFileNamecharsmax(szFileName))
    
format(szFileNamecharsmax(szFileName), "%s/%s"szFileNameg_szIniFileName)

    new 
iFile fopen(szFileName"rt"), i
    
while(!feof(iFile))
    {
        
fgets(iFileszTextcharsmax(szText))
        
trim(szText)
        
        
i++

        if((
szText[0] == EOS) || !bWholeFile && !equal(szTextszLine))
            continue

        
replace(szTextcharsmax(szText), bWholeFile szText szLine"")
        
write_file(szFileNameszText, (1))
        
        if(
bWholeFile)
        {
            
cleanFile(bWholeFileszLine)
        }
        return 
true
    
}
    
fclose(iFile)
    return 
false


OBS: If you wanna disable/enable a plugin and it contains debug mode, you must add on enableLine() and disableLine() stock too, remember that it check if the line is equal, not if contains the same name.


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

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