Reading game data from archive

Started by
5 comments, last by Stefan Fischlschweiger 8 years, 6 months ago

Right now I only have a few files in my game that get loaded at runtime. But that number will probably increase as I progress with it, so I though of implementing a system that reads game data (like models, textures, sounds, etc.) from archive files (like zip) since that would be faster than loading many small files residing individually on disk.

Thing is, while it is very easy to create such an archive, I have no clue on how to get my engine to load specific files from it or (more importantly) how to know where a file is.

Currently I pass in file locations as a path relative to the game folder, using the FileSystemContentResolver class to get my content manager to actually find and load the file. File paths are both hardcoded in the source and specified in .ini files

For example:

[Ship]

...

model = Content\Ships\someship.3df

Now, the content manager has an IContentResolver interface which FileSystemContentResolver implements, and it is set up so it will use all registered content resolvers to find a given file.

So, how would I go ahead to:

1. Tell the content manager/resolver which file to look for in which archive (or just try all there are)

2. Have the content resolver assert that the file actually exists in this/any archive

3. Load the file from archive, while preferable keeping the archive open for future file loading.

Preferable using C# framework built-in functionality

Advertisement
Generally this is done by having the internal archive folder structure be identical to the loose file structure. The game is then told about each archive in some manner (ini setting, asking your OS for every "*.zip" file in a certain folder, having the archive name match the level name, etc). Once the game knows what archives exist, it can open them and read a table of contents telling it what files are in said archive.

Once you've done that, it's just a matter of asking your resource loader for the file like you've always done - the resource loader then checks its TOC for the archive and the position in the archive, and reads in the file.

If you want to get fancy, when your loader gets a request for a file it might first check to see if a loose file exists on disk before falling back to the archive - this lets you iterate quickly on a small selection of assets "overriding" your packaged files.

You mean like, e.g. the fallout or elder scrolls games, where you can put mod files either in an esp packages or as loose files on disk?

You mean like, e.g. the fallout or elder scrolls games, where you can put mod files either in an esp packages or as loose files on disk?


Pretty much exactly like that, though the BGS games go one step farther and let you pick the order in which archives load, "later" archives overriding the contents of earlier ones. (Basically, registering the first archive, then registering the second - letting the entries in the second one overwrite any matching ones in the table of contents)
A simple way to handle this is to use PhysFS. This is exactly the sort of problem it is intended to solve.

Take a look at the c# bindings for physfs, which allow for everything you're trying to do here, reading from archives and overriding specific paths and filenames with non-archived versions.

http://freecode.com/projects/physicsfs

It supports many file formats including 7z, zip, etc and the license is very permissive.

I used this in a previous project however I didn't use C# bindings, instead using it natively inside a C++ project. It worked very well.

Edit: ninja'd by Aldacron :P

Well, I gave it a look. Tried to compile it with CLR support but just couldn't get it to compile (and I pretty much suck at C++ so I have no clue)

This topic is closed to new replies.

Advertisement