Raised This Month: $51 Target: $400
 12% 

[ISSUE] Not storing all strings


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 05-24-2020 , 13:00   [ISSUE] Not storing all strings
Reply With Quote #1

Hey

This plugin is not storing everything from the config file and not replacing a chat message

PHP Code:
#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "SpirT"
#define PLUGIN_VERSION "1.0"

#include <sourcemod>
#include <sdktools>
#include <chat-processor>
#include <cp-scp-wrapper>

int BadWordNumber;
//int BadWordAmount;

char BadWord[256][128];

ConVar g_replaceBadWord;
char BadWordReplace[128];

char folder[] = "cfg/SpirT";

#pragma newdecls required

char configFile[512];

public 
Plugin myinfo 
{
    
name "[SpirT] Block Chat Bad Words",
    
author PLUGIN_AUTHOR,
    
description "",
    
version PLUGIN_VERSION,
    
url ""
};

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_list"Command_List);
    
BuildPath(Path_SMconfigFilesizeof(configFile), "configs/SpirT/badwords.cfg");
    
    if(!
FileExists(configFile))
    {
        
LogError("[SpirT - BAD Words] Could not get a list of the badwords because the config file does not exist.");
    }
    else
    {
        
ListAllBadWords();
    }
    
    
g_replaceBadWord CreateConVar("spirt_badwords_replace""@!#$?""String to replace for the chat BadWord.");
    
GetConVarString(g_replaceBadWordBadWordReplacesizeof(BadWordReplace));
    
    if(!
DirExists(folder))
    {
        
CreateDirectory(folder511);
    }
    
AutoExecConfig(true"spirt.badwords""SpirT");
}

public 
Action Command_List(int clientint args)
{
    
PrintToChat(clientBadWord[0]);
    
PrintToChat(clientBadWord[1]);
    
PrintToChat(clientBadWord[2]);
}

void ListAllBadWords()
{
    
KeyValues kv = new KeyValues("BadWords");
    
kv.ImportFromFile(configFile);
    
    
BadWordNumber 0;
    
    do
    {
        
char numberkey[64];
        
IntToString(BadWordNumbernumberkeysizeof(numberkey));
        
        if(
KvJumpToKey(kvnumberkeyfalse))
        {
            
char word[64];
            
KvGetString(kv"word"wordsizeof(word));
            
BadWord[BadWordNumber] = word;
            
BadWordNumber++;
        }
    } while (
KvGotoNextKey(kv));
}

public 
Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    for (
int i 0sizeof(BadWord[]); i++)
    {
        
char newMessage[256];
        
strcopy(newMessagesizeof(newMessage), message);
            
        if(
StrContains(newMessageBadWord[i]))
        {
            
ReplaceString(newMessagesizeof(newMessage), BadWord[i], BadWordReplace);
            return 
Plugin_Changed;
        }
        
        return 
Plugin_Handled;
    }
    
    return 
Plugin_Handled;

Code:
"BadWords"
{
	"0"
	{
		"word" "fdp"
	}
	"1"
	{
		"word" "merda"
	}
	"2"
	{
		"word" "mrd"
	}
}
What is the fix for my code?

Best Regards,

SpirT.
__________________
SpirT is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 05-24-2020 , 13:12   Re: [ISSUE] Not storing all strings
Reply With Quote #2

sizeof(BadWord[]); // is 128. You should be using: sizeof(BadWord); // to get 256 value.

You're not deleting the KV handle after usage, causing memory leak.

Theres no point in creating and copying to newMessage, just use the message char provided by the callback.

StrContains returns -1 on failure, any other value is a position within the string.

Depending on the chat-processor you're using, "OnChatMessage" might not be a valid forward therefore not being used or triggered. The latest one from Drixevel uses CP_OnChatMessage and has a different function prototype.

You should create the char variables "numberkey" and "word" outside of the loop instead of constantly creating new ones with each iteration.

Debug print messages to find out where it's not working.

There are over 10 existing chat filter plugins, why bother writing one when plenty others exist and some much better supporting regex to catch for example m3rda.
__________________

Last edited by Silvers; 05-24-2020 at 13:18.
Silvers is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 05-24-2020 , 13:30   Re: [ISSUE] Not storing all strings
Reply With Quote #3

Quote:
Originally Posted by Silvers View Post
sizeof(BadWord[]); // is 128. You should be using: sizeof(BadWord); // to get 256 value.

You're not deleting the KV handle after usage, causing memory leak.

Theres no point in creating and copying to newMessage, just use the message char provided by the callback.

StrContains returns -1 on failure, any other value is a position within the string.

Depending on the chat-processor you're using, "OnChatMessage" might not be a valid forward therefore not being used or triggered. The latest one from Drixevel uses CP_OnChatMessage and has a different function prototype.

You should create the char variables "numberkey" and "word" outside of the loop instead of constantly creating new ones with each iteration.

Debug print messages to find out where it's not working.

There are over 10 existing chat filter plugins, why bother writing one when plenty others exist and some much better supporting regex to catch for example m3rda.
Thanks for the reply. I have done the changes on the code as changing sizeof(BadWord) and closing kv handle with delete kv.

I am using this cp (https://forums.alliedmods.net/showthread.php?t=286913). BadWords are not being replaced, even "fdp". When I use !list only the key with "0" on the cfg file is being listed.

Why bothering writing this? Because I want to try to do some stuff by my own, so any errors that might appear I will fix them by my own and not doing a lot of research
__________________
SpirT is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 05-24-2020 , 13:41   Re: [ISSUE] Not storing all strings
Reply With Quote #4

The problem is probably KvGotoNextKey(kv), instead of using that, use KvJumpToKey until not true then break out the loop, you could use for(;;) to loop unspecified times.
__________________
Silvers is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 05-24-2020 , 17:13   Re: [ISSUE] Not storing all strings
Reply With Quote #5

Quote:
Originally Posted by Silvers View Post
The problem is probably KvGotoNextKey(kv), instead of using that, use KvJumpToKey until not true then break out the loop, you could use for(;;) to loop unspecified times.
Hey, I changed my code to this:
PHP Code:
#pragma semicolon 1

#define DEBUG

#define PLUGIN_AUTHOR "SpirT"
#define PLUGIN_VERSION "1.0"

#include <sourcemod>
#include <sdktools>
#include <chat-processor>
#include <cp-scp-wrapper>

int BadWordNumber;
//int BadWordAmount;

char BadWord[256][128];

ConVar g_replaceBadWord;
char BadWordReplace[128];

char folder[] = "cfg/SpirT";

#pragma newdecls required

char configFile[512];

public 
Plugin myinfo 
{
    
name "[SpirT] Block Chat Bad Words",
    
author PLUGIN_AUTHOR,
    
description "",
    
version PLUGIN_VERSION,
    
url ""
};

public 
void OnPluginStart()
{
    
RegConsoleCmd("sm_list"Command_List);
    
BuildPath(Path_SMconfigFilesizeof(configFile), "configs/SpirT/badwords.cfg");
    
    if(!
FileExists(configFile))
    {
        
LogError("[SpirT - BAD Words] Could not get a list of the badwords because the config file does not exist.");
    }
    else
    {
        
ListAllBadWords();
    }
    
    
g_replaceBadWord CreateConVar("spirt_badwords_replace""@!#$?""String to replace for the chat BadWord.");
    
GetConVarString(g_replaceBadWordBadWordReplacesizeof(BadWordReplace));
    
    if(!
DirExists(folder))
    {
        
CreateDirectory(folder511);
    }
    
AutoExecConfig(true"spirt.badwords""SpirT");
}

public 
Action Command_List(int clientint args)
{
    
PrintToChat(clientBadWord[0]);
    
PrintToChat(clientBadWord[1]);
    
PrintToChat(clientBadWord[2]);
}

void ListAllBadWords()
{
    
KeyValues kv = new KeyValues("BadWords");
    
kv.ImportFromFile(configFile);
    
    
BadWordNumber 0;
    
    do
    {
        
char numberkey[64];
        
IntToString(BadWordNumbernumberkeysizeof(numberkey));
        
        if(
KvJumpToKey(kvnumberkeyfalse))
        {
            
char word[64];
            
KvGetString(kv"word"wordsizeof(word));
            
BadWord[BadWordNumber] = word;
        }
    } while (
BadWordNumber++);
    
    
CloseHandle(kv);
}

public 
Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    for (
int i 0sizeof(BadWord); i++)
    {
        
char newMessage[256];
        
strcopy(newMessagesizeof(newMessage), message);
            
        if(
StrContains(newMessageBadWord[i]))
        {
            
ReplaceString(newMessagesizeof(newMessage), BadWord[i], BadWordReplace);
            return 
Plugin_Changed;
        }
        
        return 
Plugin_Handled;
    }
    
    return 
Plugin_Handled;

Still just storing first "word" key. Any fix?
__________________
SpirT is offline
Tilex
Junior Member
Join Date: May 2020
Old 05-25-2020 , 19:17   Re: [ISSUE] Not storing all strings
Reply With Quote #6

PHP Code:
public Action foo
{
    for (
int i 0sizeof(BadWord); i++)
    {
     
bar;
     
bar:
     
bar;


     return 
Plugin_Handled;
    }
    
    return 
Plugin_Handled;

you see it?
You have a pre-mature return in your for loop

Also the "return Plugin_Changed" needs to go.
I'm not sure what it does, but if it's important, you should rather define a flag before your for and then check what to return afterwards:

PHP Code:
public Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    
bool changesMade false;
    for (
int i 0sizeof(BadWord); i++)
    {
        
char newMessage[256];
        
strcopy(newMessagesizeof(newMessage), message);
            
        if(
StrContains(newMessageBadWord[i]))
        {
            
ReplaceString(newMessagesizeof(newMessage), BadWord[i], BadWordReplace);
            
changesMade true;
        }
        
        
    }
    
    if(
changesMade)
       return 
Plugin_Changed;
    else
       return 
Plugin_Handled;

Tilex is offline
SpirT
Senior Member
Join Date: Sep 2018
Location: Portugal
Old 05-26-2020 , 08:47   Re: [ISSUE] Not storing all strings
Reply With Quote #7

Quote:
Originally Posted by Tilex View Post
PHP Code:
public Action foo
{
    for (
int i 0sizeof(BadWord); i++)
    {
     
bar;
     
bar:
     
bar;


     return 
Plugin_Handled;
    }
    
    return 
Plugin_Handled;

you see it?
You have a pre-mature return in your for loop

Also the "return Plugin_Changed" needs to go.
I'm not sure what it does, but if it's important, you should rather define a flag before your for and then check what to return afterwards:

PHP Code:
public Action OnChatMessage(intauthorHandle recipientschar[] namechar[] message)
{
    
bool changesMade false;
    for (
int i 0sizeof(BadWord); i++)
    {
        
char newMessage[256];
        
strcopy(newMessagesizeof(newMessage), message);
            
        if(
StrContains(newMessageBadWord[i]))
        {
            
ReplaceString(newMessagesizeof(newMessage), BadWord[i], BadWordReplace);
            
changesMade true;
        }
        
        
    }
    
    if(
changesMade)
       return 
Plugin_Changed;
    else
       return 
Plugin_Handled;

Hey! Thanks for the reply. I changed the OnChatMessage action to yours. Still not replacing the badword either listing all badwords.

What changes need to be made?
__________________
SpirT is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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