Raised This Month: $ Target: $400
 0% 

Need help with ExplodeString and Arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Miggy
Member
Join Date: Mar 2015
Old 04-23-2015 , 09:25   Need help with ExplodeString and Arrays
Reply With Quote #1

Hi folks,

I'm trying to add a function into my plugin that will grab a text file and take items in the text file and turn them into an array of strings(I believe this can be done with ExplodeString?). Then I would be grabbing info from the server and comparing them to said strings to see if there's a match.

For starters:
1) Is it possible to grab the contents of a text file and use ExplodeString to make it more manageable?
Or is there a better way?

2) I don't know diddly squat about arrays, the api doesn't really show me much and I can't find comprehensive guide. The SourceMod wiki explains it but the explanation really goes from A to Z really quick and I just get lost trying to figure out how to tie that into what I'm trying to do.

What I'm trying to do is have my plugin pull a text file of SteamID and IPs and look for a match between the txt list and players in the server. I know I could accomplish with a database but I'm wanting to keep it super simple until there is a need to have a database.
Miggy is offline
Benoist3012
Veteran Member
Join Date: Mar 2014
Location: CWave::ForceFinish()
Old 04-23-2015 , 10:54   Re: Need help with ExplodeString and Arrays
Reply With Quote #2

can you show for us a example of what you want to do.
__________________
Benoist3012 is offline
Miggy
Member
Join Date: Mar 2015
Old 04-23-2015 , 11:08   Re: Need help with ExplodeString and Arrays
Reply With Quote #3

Nothing I have coded is functioning anywhere near what I want it to do so I scrapped it all, mainly because I honestly don't understand how to work with arrays.
So currently I don't have code for that particle feature of my plugin.

I already have code that grabs the IPs and SteamIDs of the players in the server. Now I just need to grab that information and compare it to a txt file list of IPs and SteamIDs to see if there's a match.

Currently this is all I have working for the feature. I was even able to test it out so it would print the IPs and IDs to a txt file just to make sure it was pulling the information correctly:
PHP Code:
public Action:WhoAreYou(Handle:event, const String:name[], bool:dontBroadcast)
{
new 
client GetClientOfUserId(GetEventInt(event"userid"));

decl String:PlayerName[64], String:Authid[64], String:IPAddress[64];
GetClientName(clientPlayerName64);
GetClientIP(clientIPAddress64);
GetClientAuthId(clientAuthId_Steam2,Authid64);
//Do Stuff - Once figured out, Code to Compare IPs and IDs to list will go here
/**Testing out logging information just to see how it outputs so I'll know the formats I will use to compare
new Handle:FileHandle = OpenFile("cfg/player_address_log.txt", "a+");
WriteFileLine(FileHandle, "Alias: %s SteamID: %s  IP: %s", PlayerName, Authid, IPAddress);
CloseHandle(FileHandle);
**/


Last edited by Miggy; 04-23-2015 at 11:08.
Miggy is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-23-2015 , 11:18   Re: Need help with ExplodeString and Arrays
Reply With Quote #4

You can use KeyValues format
PHP Code:
"root"
{
    
"steamid_1" "ip_1"
    "steamid_2" "ip_2"
    "steamid_2" "ip_2"
    
.
    .
    .
    
"steamid_n" "ip_n"

Read it and store in adt_trie - use steamid as a key and ip as string value.

eg like this
PHP Code:
new g_hTrie CreateTrie();

// ---

if (!FileExists(sPath)) {
    
LogError("Cannot load from file '%s' - file doesn't exist!"sPath);
    return;
}

new 
Handle:hKV CreateKeyValues("root");

if (!
FileToKeyValues(hKVsPath)) {
    
CloseHandle(hKV);
    
LogError("Cannot load data from file '%s' !"sPath);
    return;
}

if (!
KvGotoFirstSubKey(hKVfalse)) {
    
CloseHandle(hKV);
    return;
}

new 
String:sSteamID[64], String:sIP[64];

do
{
    
KvGetSectionName(hKVsSteamIDsizeof(sSteamID));
    
KvGetString(hKVNULL_STRINGsIPsizeof(sIP));
    
    
SetTrieString(g_hTriesSteamIDsIP);
} while (
KvGotoNextKey(hKVfalse));

CloseHandle(hKV);

// ---

if (GetTrieArray(g_hTriesPlayersSteamIDsIPsIP)) {
    
// now we know that steamid is in the config file
    
    
if (StrEqual(sPlayersIPsIP)) {
        
// player's ip and ip in config are same
        
return true;
    }

NOT tested...
KissLick is offline
Miggy
Member
Join Date: Mar 2015
Old 04-24-2015 , 09:22   Re: Need help with ExplodeString and Arrays
Reply With Quote #5

Cool, I tried compiling it as is and got a bunch of errors so I'm gonna have to sit down and really go through it.
Looks like I'll also have to read up on some adt though cause I'm not really 100% sure of what's going on in the script.
I get the idea of what each part does but I don't understand the "how" x.x
Miggy is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-24-2015 , 10:02   Re: Need help with ExplodeString and Arrays
Reply With Quote #6

Didn't test it, but try this
PHP Code:
#include <sourcemod>

#define CONFIG_PATH "configs/myconfig.cfg"

public Plugin:myinfo =
{
    
name "test",
    
author "Raska",
    
description "",
    
version "0.1",
    
url ""
}

new 
Handle:g_hTrie;

public 
OnMapStart()
{
    new 
String:sPath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMsPathsizeof(sPath), CONFIG_PATH);
    
    
// create config file if doesn't exist
    
if (!FileExists(sPath)) {
        new 
Handle:hFile OpenFile(sPath"w");
        
WriteFileLine(hFile"\"root\"");
        
WriteFileLine(hFile"{");
        
WriteFileLine(hFile"\t// \"steamid\" \"ip\"");
        
WriteFileLine(hFile"}");
        
CloseHandle(hFile);
    }
    
    
// create hashmap (adt_trie) structure
    
if (g_hTrie != INVALID_HANDLE) {
        
CloseHandle(g_hTrie);
    }
    
g_hTrie CreateTrie();
    
    
// open config
    
new Handle:hKV CreateKeyValues("root");
    if (!
FileToKeyValues(hKVsPath)) {
        
CloseHandle(hKV);
        
LogError("Cannot load data from file '%s' !"sPath);
        return;
    }
    
    
// read fondig
    
if (!KvGotoFirstSubKey(hKVfalse)) {
        
CloseHandle(hKV);
        return;
    }
    
    new 
String:sSteamID[64], String:sIP[64];
    do
    {
        
KvGetSectionName(hKVsSteamIDsizeof(sSteamID));
        
KvGetString(hKVNULL_STRINGsIPsizeof(sIP));
        
        
SetTrieString(g_hTriesSteamIDsIP);
    } while (
KvGotoNextKey(hKVfalse));

    
CloseHandle(hKV);
}

public 
OnClientAuthorized(iClient, const String:sAuth[])
{
    new 
String:sIP[64];
    
    if (
GetTrieString(g_hTriesAuthsIPsizeof(sIP))) {
        
// connected player is in config - let's check ip
        
        
new String:sPlayersIP[64];
        
GetClientIP(iClientsPlayersIPsizeof(sPlayersIP));
        
        if (
StrEqual(sIPsPlayersIPfalse)) {
            
// connected player is in config and IPs are same
        
}
    }


Last edited by KissLick; 04-24-2015 at 10:04.
KissLick is offline
Miggy
Member
Join Date: Mar 2015
Old 04-26-2015 , 00:12   Re: Need help with ExplodeString and Arrays
Reply With Quote #7

Neat!
I'm trying to tweak it so it detects SteamID or IP rather than SteamID AND IP
Miggy is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-26-2015 , 06:51   Re: Need help with ExplodeString and Arrays
Reply With Quote #8

Quote:
Originally Posted by Miggy View Post
Neat!
I'm trying to tweak it so it detects SteamID or IP rather than SteamID AND IP
Something like this?
PHP Code:
#include <sourcemod>

#define CONFIG_PATH "configs/myconfig.cfg"

public Plugin:myinfo =
{
    
name "test",
    
author "Raska",
    
description "",
    
version "0.1",
    
url ""
}

new 
Handle:g_hPlayerList;

public 
OnMapStart()
{
    new 
String:sPath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMsPathsizeof(sPath), CONFIG_PATH);

    
// create config file if doesn't exist
    
if (!FileExists(sPath)) {
        new 
Handle:hFile OpenFile(sPath"w");
        
WriteFileLine(hFile"\"root\"");
        
WriteFileLine(hFile"{");
        
WriteFileLine(hFile"\t// \"steamid\" \"ip\"");
        
WriteFileLine(hFile"}");
        
CloseHandle(hFile);
    }

    
// create dynamic array (adt_array) structure
    
if (g_hPlayerList != INVALID_HANDLE) {
        
CloseHandle(g_hPlayerList);
    }
    
g_hPlayerList CreateArray(64);

    
// open config
    
new Handle:hKV CreateKeyValues("root");
    if (!
FileToKeyValues(hKVsPath)) {
        
CloseHandle(hKV);
        
LogError("Cannot load data from file '%s' !"sPath);
        return;
    }

    
// read condig
    
if (!KvGotoFirstSubKey(hKVfalse)) {
        
CloseHandle(hKV);
        return;
    }

    new 
String:sSteamID[64], String:sIP[64];
    do
    {
        
KvGetSectionName(hKVsSteamIDsizeof(sSteamID));
        
KvGetString(hKVNULL_STRINGsIPsizeof(sIP));

        
PushArrayString(g_hPlayerListsSteamID);
        
PushArrayString(g_hPlayerListsIP);
    } while (
KvGotoNextKey(hKVfalse));

    
CloseHandle(hKV);
}

public 
OnClientAuthorized(iClient, const String:sAuth[])
{
    new 
String:sIP[64];
    
GetClientIP(iClientsIPsizeof(sIP));

    if (
FindStringInArray(g_hPlayerListsAuth) != -|| FindStringInArray(g_hPlayerListsIP) != -) {
        
// connected player's ip or steamid is in config
    
}


Last edited by KissLick; 04-26-2015 at 06:52.
KissLick is offline
Miggy
Member
Join Date: Mar 2015
Old 04-26-2015 , 16:26   Re: Need help with ExplodeString and Arrays
Reply With Quote #9

Something more like

PHP Code:
public OnClientAuthorized(iClient, const String:sAuth[])
{
decl String:PlayerName[64], String:sIP[64];
GetClientIP(iClientsIPsizeof(sIP));
GetClientName(iClientPlayerName64);



if (
FindStringInArray(g_hPlayerListsAuth) != -1) {
        
// connected player's steamid is in config
PrintToChatAll("%s is currently Banned in UGC"PlayerName);
PrintToConsole("Banned Player SteamID Connected"
}
    
if (
FindStringInArray(g_hPlayerListsIP) != -) {
        
// connected player's ip is in config
PrintToChatAll("%s is using IP Address which is associated with a Banned UGC Player"PlayerName);
PrintToConsole("Banned Player IP Connected")

But my printtchatall/printtoconsole doesn't seem to be working.
It's always one thing after another, lol

Last edited by Miggy; 04-26-2015 at 16:31.
Miggy is offline
KissLick
Veteran Member
Join Date: Nov 2012
Location: void
Old 04-26-2015 , 17:03   Re: Need help with ExplodeString and Arrays
Reply With Quote #10

You don't need to use GetClientName, just use PrintToChatAll("%N is currently Banned in UGC", iClient);

Try add some debug print before these ifs, like PrintToChatAll("TESTING: %N:%s:%s", iClient, sAuth, sIP);
KissLick 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 12:01.


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