The online racing simulator
How to edit ban files ?
(16 posts, started )
How to edit ban files ?
Hi,
does somebody knows how to read and write the ban files ? A tutorial would be great
Great, Thx !
I wrote these 3 structs:


struct demo_ban
{
in_addr IP_address;
__int64 Time;
};
//----
struct name_ban
{
char username [24];
__int64 Time;
int BanHours;
};
//----
struct File
{
char Id [6];
byte Zero [1];
byte Version [1];
int Demobans;
int Namebans;
};
//----

The bans.txt file includes the following lines:
1 integer num_demo_bans
[demo ban * num_demo_bans]
1 integer num_name_bans
[name ban * num_name_bans]

but what does it mean? i don´t know how to use this information. could somebody help me please
Quote from JogDive :The bans.txt file includes the following lines:
1 integer num_demo_bans
[demo ban * num_demo_bans]
1 integer num_name_bans
[name ban * num_name_bans]

but what does it mean? i don´t know how to use this information. could somebody help me please

Basically what Scawen is saying by
Quote : [demo ban * num_demo_bans]

is that the next num_demo_bans * sizeof(struct demo_ban) bytes contains demo_ban data. To try and make it clearer, it means that there are num_demo_ban demo_ban's structs at this point.

I must admit, if I was Scawen I would've picked your approach, which is similar to a pak file's structure (i.e. one master struct starting at byte 0, and then the rest of the file containing the data). But thats besides the point.

I'll see if I can throw together something to possibly explain it a little better and get some example data out.
I've literally just finished writing this, haven't even compiled it, so there maybe a few glaring mistakes, but it should give you the idea of what to do to read, and hopefully write, .ban's.
#include <stdio.h>
#include <stdlib.h>

// cross platform loveliness
#ifdef WIN32
#include <winsock2.h>
#else
#include <netinet/in.h>
#endif

// __int64 is a compiler specific thing. long long should be around 8 bytes, but we
// can't guarantee it :( Ideally this should be declared as a compiler argument.
#ifndef __int64
#define __int64 long long
#endif

#ifndef LFS_BAN_VER
#define LFS_BAN_VER 246
#endif

struct ban_core
{
char id[7]; // null terminated string LFSBAN\0
char version;
int num_demo;
int num_name;
};

struct ban_demo
{
in_addr ip_address;
__int64 time;
};

struct ban_name
{
char username[24];
__int64 time;
int banhours;
};

int main()
{
FILE *fio = NULL;
long fsize;
struct ban_core core;

// lets set core to all \0's
memset(core, 0, sizeof(core));

fio = fopen("bans.ban", "rb");

if (fio != NULL)
{
fseek(fio , 0 , SEEK_END);
fsize = ftell(fio);
rewind(fio);

// minimum size for a .ban
if (fsize >= 16)
{
// using your example we want to only read the first 12 bytes
fread((void *)&core, 12, 1, fio);
// we've now filled the ban_core struct with the first 12 bytes of data.
// Hopefully it's right

// You should check the .ban version
if (core.version != LFS_BAN_VER)
{
// not the revision we were expecting. Abort.
fclose(fio);
return 1;
}

// Check the num_demo bans is > 0
if (core.num_demo > 0)
{
// Lets pretend we don't care about the bans, and fast forward to the
// number of named bans
// otherwise you'd read in all the demo bans one at a time
fseek(fio, sizeof(struct ban_demo) * core.num_demo, SEEK_CUR);
}

// Read the next 4 bytes (should be the number of named bans)
// note: I've not used sizeof(int) instead, because in most places where you'd care,
// int should be 4 bytes, but I dont want to risk it
fread((void *)&core.num_name, 4, 1, fio);

// Check the num_name bans is > 0
if (core.num_name > 0)
{
// again, you'd read in the named bans here, but I'm just seeking
fseek(fio, sizeof(struct ban_name) * core.num_name, SEEK_CUR);
}

// in theory we should be at the end of the file
if (ftell(fio) != fsize)
{
// Something went horribly wrong; some how we aren't at the end of the file
//... oo er - throw an error
fclose(fio);
return 2;
}
}

fclose(fio);
}

return 0;
}

Time for a beer now I think

Edit: Just one thing to point out, I've not typedef'ed __int64 because then you couldnt use signed or unsigned with it. Whilst this doesn't get used in this context, it's still a bad idea to try and typedef it. What's even worse is that I've #define'ed it inline. As I say in the comments, you should provide this as an argument to the compiler.

Edit 2: There is another option, which I hadn't considered. This is to load the entire bans file into the memory and then immediately close it. You could then seek through it like normal memory, memcpy'ing bits out into an ordered structure.

Something to consider; rather than having 3 distinct structures, would be to use a few more struct's, and core, demo and named, with a third "master" structure and a generic linked list struct. This would allow you to easily search through and add more bans without having to do pesky realloc's. Possibly a little overkill though, depending on what you want to do.

I'm going for that beer now. Honest.
Very great work guy ! give me a try to get all workin tonight
Sorry its all inline, and "pure C", but I didnt want to make it too complicated I'll have a go and see if it compiles later, and actually does what it should Unfortunately the pedigree (beer) has gone straight to my head right now
#9 - Stuff
All right, got it working. This example shows the first banned name. the buffer buffer0 could be used anyway. i will write a while loop to parse until the file ends. the position integer of the reading stream is a bit headaching for me now so i think i don´t understood all well yet but please give me a try


struct core bancore;
FILE *file = NULL;
long fsize;


memset(&bancore, 0, sizeof(core));
file = fopen("data\\misc\\bans.ban", "rb");

if (file != NULL)
{
fseek(file , 0 , SEEK_END);
fsize = ftell(file);
rewind(file);

// minimum size for a ban
if (fsize >= 16)
{

// read the first 12 bytes
fread((void *)&bancore, 12, 1, file);



if (bancore.Demobans > 0)
{
fseek(file, sizeof(struct demo_ban) * bancore.Demobans, SEEK_CUR);
}


char buffer0[24];
fread((void *)&bancore.Namebans, 4, 1, file);
strcpy(buffer0,file->curp);
Memo2->Lines->Add(buffer0);


// Check the num_name bans is > 0
if (bancore.Namebans > 0)
{
fseek(file, sizeof(struct name_ban) * bancore.Namebans, SEEK_CUR);
}

}

}


fclose(file);

tbh I think I may have made it a little more confusing, having used the C functions and syntax rather than true pseudo code Sorry if I have..
Sorry for the delay.... I´m in a hospital for a while. My health situation makes it impossible to release LFS DediLink V2.0 in this time but i will work on when i can.

I have big problems to read (C/C++) the banned names and demo ip bans in the bans.ban file. Could somebody help me to figure it out ? the base is the code snippet above
Your health is THE most important thing.
#14 - SamH
The only person I know who's been able to read the bans.ban file effectively is CrazyICE. He's recently been working on a new version of the LFS Ban Editor, and it's looking good. Drop him a line
Thank you !
yes Dygear you are right but i want to work on each time when i get a a chance. i fixed some bugs and reworked some features...

CrazIce is working with some Delphi based stuff and i had no plans to code these specific functions into c++. this problem is very annoying
in need only the names and ips to show them in a listbox on the client to send unban commands. i figured out a small nice programm thread to monitor the bans.ban file for changes and hold the ban list on the client up to date



Happy new year !
Hey guys, i found a way to get the ban file reading method working

Please stay tuned for LFS DediLink V2.0 in one or two months
-
(felplacerad) DELETED by felplacerad

How to edit ban files ?
(16 posts, started )
FGED GREDG RDFGDR GSFDG