Resource files management

0 comments


First of all, a little bit of theory before I present actual implementation. Let's find out what is a resource file and why we would want to use them.

Resource files
Ok, we all know that video games consist of various components like sound effects, music, 3d models, textures, fonts, scripts etc. Each piece of these is a File.
When you build your game, you add these components, file by file and when the game is built you end up with a whole bunch of files of variable types. Of course, if you make a little planning, you would sort these files into categories by creating subfolders and it could look like:

C:\MyGame\
C:\MyGame\mygame.exe
C:\MyGame\Music\music.ogg
C:\MyGame\Music\music2.ogg
C:\MyGame\Textures\brickwall.png
...


However, you may notice that it is hard to maintain, because you need to know filenames and paths for each object you would like to load into game. It would be way better if we could reduce the amount of files. And we can!
Let's think about it in more common terms. Imagine yourself an employee working at the office. Your job involves signing documents. And here is the question, where do you store your papers?

  1. You could just throw them all over the desk.
  2. You could store them in your drawer sorted in paper-cases.
  3. All documents could be stored in a storage-room with an additional manager looking over it, whose duty is to keep these documents safe and bring documents on demand.
The first two cases means you will need to know about particular location of specific document, so in second case you will need to know in which drawer and paper-case you should look for a document, whereas first case implies that you will need to find the document in the pile of papers and the only clue is the name of a document.
And the last case is a close representation of what we would like to have in our game. All documents (files) are stored in a single storage-room (container) with an additional manager (program) looking over it. A container is a single file, called package, archive, resource file.
The simpliest example I could think of is ZIP archive. ZIP file may contain numerous files inside as well as it may compress them so the container's size will be smaller. Also, you could set the password for a ZIP archive to ensure that nobody except you has access to that archive.

Resource file structure
So, what makes file a resource file? Generally, resource file consists of system information, called header and files that we packed, called data lumps.
Header stores information used by program to perform manipulations with resource file, like retrieving, writing or deleting data. Also it may contain other information like resource file version, additional flags, checksum, etc.
Lumps are data structures which consist of stored data, i.e. file, and corresponding information like unique name of lump, position of data in the resource file, size of data. The information about lumps can be stored apart from data, so the manager program can read the information about the lumps in the resource file. Overall resource file structure looks like:

[HEADER]

// Lump data list
[Data of Lump 1]
[Data of Lump 2]
[Data of Lump 3]
...
[Data of Lump N]

// Lump information list
[Information of Lump 1]
[Information of Lump 2]
[Information of Lump 3]
...
[Information of Lump N]


Notice that information list comes after the data list, this is needed for a simplier management of resource file as you will see later.

Header consists of two basic fields: [Lump Count] and [Lump Information Offset].
The [Lump Count] field tells us how many lumps this resource file has and the [Lump Information offset] field contains position of the beginning of lump information list. This information is crucial for our manager that works with resource files, all other information may be considered optional and may be included or not in the header.

Lump information record consists of three basic fields: [Lump Name], [Lump Offset] and [Lump Size].
[Lump Name] is an unique name which is used by manager to find lumps.
[Lump Offset] is a position of lump data and [Lump Size] is a size of data in bytes.

Well, that's all about the resource file. Coming up next: resource file manager and complete realization of simple resource file management in C++

Resource files management. Part II
Resource files management. Part III
Resource files management. Part IV

Outer links: http://www.gamedev.net/reference/articles/article902.asp