Page 1 of 1

Making your own filetypes

Posted: Thu Oct 19, 2006 12:35 am
by monkeycracks
How would you make your own filetype? Like IrrEdit can do XML or .irr

Max has 3ds, max, etc

I just want something similar to a text/xml file.

Posted: Thu Oct 19, 2006 4:43 am
by CodeDog
It's C++, you can use the same binary or text output to file routines you would use in any C++ application.

Code: Select all

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

Posted: Thu Oct 19, 2006 11:05 am
by monkeycracks
ah yes, but I mean how would you convert it to your own file type (lets say .datatype) and then still be able to read it?

Posted: Sat Oct 21, 2006 6:24 pm
by monkeycracks
Anyone?

Posted: Sat Oct 21, 2006 7:17 pm
by Dibalo
You have to make some kind of structure into your file. Very simple example:

Code: Select all

// myConfig.myFile

playerId = 23
playerStartHp = 100
enemyCount = 77
maxEnemies = 500
enemyHp = 30
etc...
First argument is your variable and second (after = operator) is value. Then you have to make your own config-file reading and writing functions. Simple example based on objects:

Code: Select all

ConfigReader file("myConfig.myFile");

try
{
    int playerHp = file.search("playerStartHp");
    int enemyHp = file.search("enemyHp");
}
catch(OwnExeption& e)
{
    // handle problem, if you didnĀ“t find variables named "playerStartHp" or "enemyHp"
    ...
}

Posted: Sat Oct 21, 2006 7:23 pm
by bitplane
file extentions are nothing to do with the "data type", they are just the letters at the end of the file name.

The traditional way to make your own data type was like so-

Code: Select all

struct my_file_header
{
  long MagicNumber;
  // file options and stuff
} filehead;

struct my_mesh_struct
{
  int number_of_verts;
  int number_of_indices;
} mesh1;
...

filehead.MagicNumber = 0x1337;
then write out all the structs to disk in binary mode. when you read the file header back in, you check to see if your "magic number" matches. if it does, the data inside the file is probably the same.

since we're not living in 1980, i'd recommend using xml (edit: Dibalo's way works too of course) as the carrier for your data. it might take up more disk space, but disk space is cheap and man-hours are expensive. just write it out using Irrlicht's XML writer, and read it using the XML reader

Posted: Sun Oct 22, 2006 5:07 pm
by monkeycracks
Yes, I don't really want to use XML because users will be able to read/change the contents. I'll try the other methods though.

Posted: Sun Oct 22, 2006 6:18 pm
by CodeDog
Users will be able to change the contents no matter what file type you use.
Encryption will slow them down but not stop them.

Most users can edit text files.
Some users will know enough to edit binary files and those users will make programs to edit the binary files which they will give to the rest of the users to use.

Just remember, ANYTHING client side is in the hands of the enemy.
Store ALL important game info server side.

Posted: Sun Apr 15, 2007 8:39 pm
by Midnight
what if the server is also in the hands of the enemy?

how is it normally done?

there is no way to stop hackers?

Posted: Sun Apr 15, 2007 8:40 pm
by monkeycracks
Midnight, this post was last year.
The server is in my hands, and I hope I'm not my own enemy.