2004. január 24., szombat

How to really make a resource file


Problem/Question/Abstract:

Creating a sort of uncompressed Zip file to store all the files required for a game or any other program that requires additional files.

Answer:

Instead of having loads of files for your games distributed all over the place, you can stick all your files into a single package, you find these used in almost every game out.

To make these files requires a header which can be a set length then all the files, followed by each files information in equal segments:

[HEADER]
[FILE1]
[FILE2]
...
[FILEN]
[FILE1INFO]
[FILE2INFO]
...
[FILENINFO]

this can easily be achieved and you can have lots of other 'addins' such as putting files in sub directories, special properties being set for each file etc....

first off you need a header, this usually consists of 4 things

type
  header = record
    Signature: array[1..4] of char;
    Version: LongInt;
    fileoffset: LongInt;
    fileentries: LongInt;
  end;

The signiture could be anything that you wish, but it is used for checking if the file is a valid package file for your program. next is the version, you may wish to improve the package file over time or have an increment system for your application so that a file in an newer package, determined by the version number would overide that of a file in an older package.

Next is the fileoffset, this points to the begining of the file info section after the last file in the package.

FILEENTRIES is used for counting how many file info entries there are, so u can have a loop running reading off the entries if you so wished.

so create ur file then write in the header

wfile.Write(head, SizeOf(head));

next comes the adding of the actual files this can be done by using TFileStream then

rfile.create('filename', fmopenread);

wfile.copyfrom(rfile, rfile.size);

continue writing the files next comes writing the file info, this must be done either after adding all the files or while adding one file to the final file you create a temporary file and add the file info to the file then, then when finished 'stick' the temp file onto the end of the final file. There is other options available, but they are upto you to discover.

The File Info entries MUST be all of the same size for this example i have used 44 bytes but you could use anything aslong as it is the same, having large file info entries will dramatically increase your file size so i would sujest someting around 44 bytes.

type
  tfilenametype = array[0..29] of char;
  direntry = record
    offset: longint;
    size: longint;
    filename: tfilenametype;
    timestamp: longint;
  end;

offset = the position from the begining of the package file. and the size value = the size of that file it refrences. so that you can seek and read the file out of the package. Filename is obvious and the timestamp would be

fileage('filename');

add this all into a file and then u have your package, reading it is just of case of reading instead of writing the file, but using this as a guide you could take this far.

Check this GDC article if this is not enought for you.

Nincsenek megjegyzések:

Megjegyzés küldése