Raised This Month: $ Target: $400
 0% 

fgets, format (general messup)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
marky_uk
Junior Member
Join Date: Aug 2008
Old 04-10-2009 , 16:57   fgets, format (general messup)
Reply With Quote #1

Hey,
This should have been an easy plugin to make but incoporating a file to retrieve data from has been the hardest thing I've come across yet.

What its suppose to do is:
get mapname,
open mapfile associated with mapname,
read all the lines in the mapfile,
remove all the entities listed in the mapfile

What it does:
Compiles fine :p
Crashes on server start

Im guessing the crash is caused by the malformed find_ent_by_model/remove ent because fgets is not supplying the right data I wanted.

I have yet to find a decent tutorial on file reading, so if you could provide while helping me fix my code.

Code:
#include <amxmodx>
#include <engine>

#define PLUGIN "WepEntityRemover"
#define AUTHOR "Marky_UK"
#define VERSION "1.00"

#define FILEPATH "addons/amxmodx/configs/WepEntityRemover"

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    new mapfile[256]
    new mapname[256]
    get_mapname(mapname, 255)
    format(mapfile,255,"%s/%s.txt", FILEPATH, mapname)
    new filepointer = fopen(mapfile,"r")
    if(filepointer == 0)
    {
        return PLUGIN_HANDLED;
    }

    new mapfiledata[681],weapon[19]
    
    while(fgets(filepointer,mapfiledata,680))
    {
        parse(mapfiledata,weapon,18)
        new WeaponEnt[65]
        format(WeaponEnt,64,"find_ent_by_model(-1,'armoury_entity','models/%s')", weapon)
        while((WeaponEnt[0]) != 0)
        {
            remove_entity(WeaponEnt[0])
        }
    }
    return PLUGIN_HANDLED;
}
Thanks, +karma

Last edited by marky_uk; 04-10-2009 at 16:59. Reason: new mapfiledata[681],weapon[18] TO new mapfiledata[681],weapon[19]
marky_uk is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-10-2009 , 17:09   Re: fgets, format (general messup)
Reply With Quote #2

WeaponEnt contains a string. That string happens to be the text that makes up a function. This "function" will never get executed because it is just a string of characters.

Basically:
WeaponEnt[0] = "f"

Also, I don't think fgets returns a value. Here is what I use to take in two parameters (mapname and number):

PHP Code:
    new maproundsfile[64]
    
get_configsdir(maproundsfilesizeof(maproundsfile) - 1)
    
add(maproundsfilesizeof(maproundsfile) - 1"/map_rounds.ini")
    
    if( !
file_exists(maproundsfile) ) return
    
    new 
curmap[64]
    
get_mapname(curmapsizeof(curmap) - 1)
    
    new 
fopen(maproundsfile"rt")
    
    new 
data[128], map[64], rounds[16]
    while( !
feof(f) ) 
    { 
        
fgets(fdatasizeof(data) - 1)
        
// I think trim(data) needs to go here since fgets includes newline and carriage return characters.
        
if( !data[0] || data[0] == ';'
           
|| data[0] == '/' && data[1] == '/' ) continue;
         
        
parse(data
              
mapsizeof(map) - 1,
              
roundssizeof(rounds) - 1
             
)
         
        if( !
strcmp(curmapmap) )
        { 
            
set_pcvar_num(cvar_mp_maxroundsstr_to_num(rounds))
            
mapfound true
            
break
        }
    }
    
    
fclose(f
__________________

Last edited by fysiks; 04-10-2009 at 17:16.
fysiks is offline
marky_uk
Junior Member
Join Date: Aug 2008
Old 04-10-2009 , 17:14   Re: fgets, format (general messup)
Reply With Quote #3

Is what I thought aswell, I can't think right now because some guy was just talking to me and has completely confused me so I have yet to come to a solution if you could give me a hint please :p
marky_uk is offline
marky_uk
Junior Member
Join Date: Aug 2008
Old 04-10-2009 , 17:16   Re: fgets, format (general messup)
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
WeaponEnt contains a string. That string happens to be the text that makes up a function. This "function" will never get executed because it is just a string of characters.

Basically:
WeaponEnt[0] = "f"

Also, I don't think fgets returns a value. Here is what I use to take in two parameters (mapname and number):

PHP Code:
    new maproundsfile[64]
    
get_configsdir(maproundsfilesizeof(maproundsfile) - 1)
    
add(maproundsfilesizeof(maproundsfile) - 1"/map_rounds.ini")
    
    if( !
file_exists(maproundsfile) ) return
    
    new 
curmap[64]
    
get_mapname(curmapsizeof(curmap) - 1)
    
    new 
fopen(maproundsfile"rt")
    
    new 
data[128], map[64], rounds[16]
    while( !
feof(f) ) 
    { 
        
fgets(fdatasizeof(data) - 1)
         
        if( !
data[0] || data[0] == ';'
           
|| data[0] == '/' && data[1] == '/' ) continue;
         
        
parse(data
              
mapsizeof(map) - 1,
              
roundssizeof(rounds) - 1
             
)
         
        if( !
strcmp(curmapmap) )
        { 
            
set_pcvar_num(cvar_mp_maxroundsstr_to_num(rounds))
            
mapfound true
            
break
        }
    }
    
    
fclose(f

fgets stores data into the second parameter and returns 0 on error

EDIT: OR DOES IT :p
EDIT2: NO IT DOESNT >.<

Last edited by marky_uk; 04-10-2009 at 17:18.
marky_uk is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-10-2009 , 17:20   Re: fgets, format (general messup)
Reply With Quote #5

Quote:
Originally Posted by marky_uk View Post
fgets stores data into the second parameter and returns 0 on error

EDIT: OR DOES IT :p
EDIT2: NO IT DOESNT >.<
I looked at the funcwiki and in the include files and it doesn't say anything about returning a value.
__________________
fysiks is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 04-10-2009 , 17:23   Re: fgets, format (general messup)
Reply With Quote #6

Code:
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
    FILE *fp = (FILE *)params[1];

    if (!fp)
        return 0;

    static char buffer[4096];
    buffer[0] = '\0';
    fgets(buffer, sizeof(buffer)-1, fp);
    return set_amxstring(amx, params[2], buffer, params[3]);
}
The code seems right, returns 0 on invalid file handle, and set_amxstring() returns the lenght of the string, so it should be 0 when nothing was read.
__________________

Community / No support through PM
danielkza is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-10-2009 , 17:26   Re: fgets, format (general messup)
Reply With Quote #7

Quote:
Originally Posted by danielkza View Post
Code:
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
    FILE *fp = (FILE *)params[1];

    if (!fp)
        return 0;

    static char buffer[4096];
    buffer[0] = '\0';
    fgets(buffer, sizeof(buffer)-1, fp);
    return set_amxstring(amx, params[2], buffer, params[3]);
}
The code seems right, returns 0 on invalid file handle, and set_amxstring() returns the lenght of the string, so it should be 0 when nothing was read.
But it reads \r\n according to the include. So, it will only be zero on eof. Correct me if I'm wrong.
__________________
fysiks is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 18:22   Re: fgets, format (general messup)
Reply With Quote #8

PHP Code:
    while(fgets(filepointer,mapfiledata,680))
    {
        
parse(mapfiledata,weapon,18)
        
        new 
WeaponModel[26]
        
format(WeaponModel,25,"models/%s",weapon)
        
        new 
WeaponEnt = -1;
        
        while(
find_ent_by_model(WeaponEnt,"armoury_entity",WeaponModel))
        {
            
remove_entity(WeaponEnt);
        }
    } 
__________________
joaquimandrade is offline
marky_uk
Junior Member
Join Date: Aug 2008
Old 04-10-2009 , 21:03   Re: fgets, format (general messup)
Reply With Quote #9

Final code, works well. Thanks guys for all your help. Im going to convert this t fakemeta sometime in the future.

Thanks to joaquimandrade although your code wasn t 100% correct :p and to the others +karma <3

PHP Code:
// Weapon Remover
// by: Marky_UK
//
// Required Modules:
// amxmodx
// engine
//
// Instructions:
// Create a folder called "WeaponRemover" at "addons/amxmodx/configs"
// Inside the folder, create a text file "<mapname>.txt"
// Inside the "<mapname>.txt" file, add the list of weapons you want removing
//
// Example:
// To block AWP's and scouts on the map, fy_snow
// Create a text file, "fy_snow.txt" in "addons/amxmodx/configs/WeaponRemover"
// Inside "fy_snow.txt" type:
//                            awp
//                            scout

#include <amxmodx>
#include <engine>

#define PLUGIN "Weapon Remover"
#define AUTHOR "Marky_UK"
#define VERSION "1.00"

#define FILEPATH "addons/amxmodx/configs/WeaponRemover"

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)

    new 
mapfile[256]
    new 
mapname[256]
    
get_mapname(mapnamesizeof(mapname))
    
format(mapfile255"%s/%s.txt"FILEPATHmapname)
    new 
filepointer fopen(mapfile,"r")
    if(
filepointer == 0)
    {
        return 
PLUGIN_HANDLED;
    }

    new 
mapfiledata[681],weapon[13]
    while(
fgets(filepointer,mapfiledata,sizeof(mapfiledata) -1))
    {
        
trim(mapfiledata)
        
parse(mapfiledata,weapon,12)
        new 
WeaponModel[26]
        
format(WeaponModel,sizeof(weapon) + 13,"models/w_%s.mdl",weapon)
        
        new 
WeaponEnt = -1
        
new WepID
        
while((WepID find_ent_by_model(WeaponEnt,"armoury_entity",WeaponModel)) != 0)
        {
            
remove_entity(WepID)
        }
    }
    return 
PLUGIN_HANDLED

marky_uk is offline
joaquimandrade
Veteran Member
Join Date: Dec 2008
Location: Portugal
Old 04-10-2009 , 21:23   Re: fgets, format (general messup)
Reply With Quote #10

Quote:
Originally Posted by marky_uk View Post
Final code, works well. Thanks guys for all your help. Im going to convert this t fakemeta sometime in the future.

Thanks to joaquimandrade although your code wasn t 100% correct :p and to the others +karma <3

PHP Code:
// Weapon Remover
// by: Marky_UK
//
// Required Modules:
// amxmodx
// engine
//
// Instructions:
// Create a folder called "WeaponRemover" at "addons/amxmodx/configs"
// Inside the folder, create a text file "<mapname>.txt"
// Inside the "<mapname>.txt" file, add the list of weapons you want removing
//
// Example:
// To block AWP's and scouts on the map, fy_snow
// Create a text file, "fy_snow.txt" in "addons/amxmodx/configs/WeaponRemover"
// Inside "fy_snow.txt" type:
//                            awp
//                            scout

#include <amxmodx>
#include <engine>

#define PLUGIN "Weapon Remover"
#define AUTHOR "Marky_UK"
#define VERSION "1.00"

#define FILEPATH "addons/amxmodx/configs/WeaponRemover"

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR)

    new 
mapfile[256]
    new 
mapname[256]
    
get_mapname(mapnamesizeof(mapname))
    
format(mapfile255"%s/%s.txt"FILEPATHmapname)
    new 
filepointer fopen(mapfile,"r")
    if(
filepointer == 0)
    {
        return 
PLUGIN_HANDLED;
    }

    new 
mapfiledata[681],weapon[13]
    while(
fgets(filepointer,mapfiledata,sizeof(mapfiledata) -1))
    {
        
trim(mapfiledata)
        
parse(mapfiledata,weapon,12)
        new 
WeaponModel[26]
        
format(WeaponModel,sizeof(weapon) + 13,"models/w_%s.mdl",weapon)
        
        new 
WeaponEnt = -1
        
new WepID
        
while((WepID find_ent_by_model(WeaponEnt,"armoury_entity",WeaponModel)) != 0)
        {
            
remove_entity(WepID)
        }
    }
    return 
PLUGIN_HANDLED

Change this:

new WeaponEnt = -1
new WepID

to

new WepID = -1

and

find_ent_by_model(WeaponEnt,"armoury_entity",WeaponModel)) != 0)

to

find_ent_by_model(WepID,"armoury_entity",WeaponModel)) != 0)

(You just need one variable)
__________________

Last edited by joaquimandrade; 04-10-2009 at 21:31.
joaquimandrade is offline
Reply


Thread Tools
Display Modes

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 02:15.


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