Archive File Support

I have refactored file opening in AgateLib. Surface, Sounds, etc. can be loaded from Streams now. This has been in there for a while, but now there is a class AgateLib.AgateFileProvider which is used internally to open Streams when creating objects and passing a filename. For example, the Surface(string) constructor looks in AgateFileProvider.Images to open image files. The reason for this approach is that AgateFileProvider.Images is a list of IFileProvider objects. There are two classes which implement IFileProvider, FileSystemProvider which opens files on the hard drive, and TgzFileProvider which reads files from a gzipped tar file. Code like the following will allow you to load images from an archive:

  1. // Add the tar archive file to the search set for images
  2. AgateFileProvider.Images.Add(new AgateLib.Utility.TgzFileProvider("images.tar.gz"));
  3.  
  4. // Loads the file "picture.png" from the archive if it exists.
  5. Surface surf = new Surface("picture.png");

There are similar providers in AgateFileProvider for sounds and music. This is not thoroughly tested, so let me know if you find any bugs.

JaredMcGuire
Posted - Thu, 02/12/2009 - 22:11

Any chance of a ZipFileProvider?

kanato
Posted - Thu, 02/12/2009 - 23:33

Zip would definitely be an ideal format for windows users. But I won't be writing one any time soon. The main reason I wrote the TgzFileProvider is because the gzip compression/decompression is part of the .net 2.0 standard library, and tar is a very easy format to parse, so it was quick to do. But writing an zip file decoder is less trivial, and I don't want to go the other route and add a dependency on something like SharpZipLib.

On the other hand, if you don't mind having a dependency on SharpZipLib, you could probably write a ZipFileProvider with it yourself rather easily, by implementing the AgateLib.Utility.IFileProvider interface. There are only four methods to implement, two which enumerate all the files in the archive, one to check if a file is in the archive, and one to get a stream to read a file in the archive.

kanato
Posted - Sun, 03/29/2009 - 15:14

It turns out I was wrong on this. System.IO.Compression has DeflateStream for de/compression of data compressed with the deflate algorithm, which is commonly used in zip files. So I've added a ZipFileProvider which supports files compressed with deflate. This is somewhat limited, because there are other compression algorithms that the zip file format can be used with, so if you want to use a zip file with AgateLib, stick to deflate.

JaredMcGuire
Posted - Thu, 02/12/2009 - 21:39

The first image loads, second throws this error: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream". I used IZArc (izarc.org) to make the tar.gz files, so that may be the prob, though I doubt it. Sounds (wav) worked fine fine though.

On another note not using tarzip. I am having trouble with Music loading ogg files. I get an access violation with a tmp file in the users local temp folder when loading the ogg file. Odd that if works fine on my development computer, but errors on two others consistently. One vista, one XP.

kanato
Posted - Thu, 02/12/2009 - 23:18

Well, it looks like there was a silly bug there. Thanks for pointing it out. It should be fixed now, but let me know if there are other problems.

It's probably better to carry on the discussion about the ogg files on the forums. But the first questions I would ask would be: which agatelib driver is being used? If you're using AgateMDX, then in order to have ogg support I think the ogg drivers have to be installed. If ogg files can be played in Windows Media Player but can't be played in AgateLib with DirectX then there is something that needs to be looked into.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.