Page 1 of 1

PhysFS C++ wrapper and Ogre integration

Posted: Wed Aug 22, 2007 8:58 pm
by CABAListic
I needed a "slightly" more sophisticated method to manage my game data, and just when I was about to implement something of my own I remembered PhysFS (http://icculus.org/physfs/). So instead of reinventing the wheel I just decided to do a simple C++ wrapper for it and then create an Archive implementation for Ogre so I can use PhysFS locations for Ogre's resource system.

For those of you who don't know PhysFS: It's sort of a virtual file system to which you can mount actual locations from your physical file system (directories or archives). Then, whenever you request a file from the PhysFS system, all locations from the search path are scanned in order and the first match is returned. In addition, you can define one write dir where all of your files will be written to. With this approach PhysFS manages to solve a couple of interesting situations for games:
  • Multi-user environments: Traditionally on Unix, but meanwhile sometimes on Windows also, common users don't necessarily have write access to your game's installation directory. But you'll still want to save configuration files, save games, replays, ... With PhysFS you just specify a path in the user's home directory as your first read location and as the write location, then add your game's data directory as the second read location. That way you can access files from both locations even though your game doesn't know because the PhysFS system hides this.
  • Security: PhysFS takes care that no write access leaves the PhysFS write directory. Might be useful to protect scripting environments from harmful scripts.
  • Support for various types of archives as read locations. Mostly game related archive types, but noticably also ZIP and 7z (!) support.

Anyway, I just more or less finished the wrapper and Ogre integration part and thought I'd share it here in case someone else wants to use PhysFS for his project. The C++ wrapper is pretty straight-forward, most of the PhysFS functions are simply wrapped in a more C++ like way, i. e. the PHYSFS_ function prefix becomes PhysFS namespace, all char* parameters become strings and all lists become vectors of strings. The PHYSFS_File structure is wrapped by a C++ stream (PhysFS::FileStream), that one behaves pretty much like std::fstream, so should feel more or less natural.

Example:

Code: Select all

#include "PhysFS++.h"

int main(int argc, char** argv)
{
  // init PhysFS system
  PhysFS::init(argv[0]);

  // add the program's base directory to the search path
  PhysFS::addToSearchPath(PhysFS::getBaseDir());
  // set the program's base directory as the write dir
  PhysFS::setWriteDir(PhysFS::getBaseDir());

  // create some text file and write to it
  PhysFS::FileStream stream ("test.txt", PhysFS::OM_WRITE);
  stream << "Just a standard stream: " << 12345 << "\n";
  stream.close();

  // shutdown PhysFS
  PhysFS::deinit();
}

The Ogre integration is also very simple, the only thing you have to do is to #include "PhysFSOgre.h" and call PhysFS::registerPhysFSToOgre() after you have initialised both PhysFS and Ogre. That's it, you can then add resource locations to the ResourceGroupManager of type "PhysFS" :)


Here's the code: click
Consider it public domain. But I haven't yet tested it very thoroughly, so don't blame me :) If you have suggestions or improvements, post them here.
The code depends on PhysFS (go figure) and boost::iostreams for the wrapper, and naturally Ogre if you're using the Ogre integration.

Posted: Wed Aug 22, 2007 9:35 pm
by PolyVox
Hey, good job! That looks like it could be handy :D

Posted: Wed Aug 22, 2007 9:48 pm
by betajaen
Wow. I was going to write my own Virtual FileSystem for Ogre one day, but then I decided I'd use a zip file instead it was quicker and less hair loss.

But this is very handy indeed. Zlib and Public Domain licences as well, even better. ;)

Re: PhysFS C++ wrapper and Ogre integration

Posted: Sun Mar 28, 2010 11:48 am
by Roman
Hello

Thank you, CABAListic. Your code is very useful.

I am revising currently my FileSystem module for my game. I decided to integrate and use PhysicsFS, but still boost::filesystem (i.e. path class) for some preparation tasks and convenience. I am going to build a mod/hook system. Additionally, PhysicsFS is using nedmalloc as memory allocator.

Re: PhysFS C++ wrapper and Ogre integration

Posted: Sun Aug 29, 2010 2:34 pm
by Wolfmanfx
Have you updated the binding?

Re: PhysFS C++ wrapper and Ogre integration

Posted: Sun Aug 29, 2010 9:33 pm
by jacmoe
Yes, he has.
But it's integrated with Poco and some other bits of their framework - you can grab it by doing a small search for physfs. :)

Re: PhysFS C++ wrapper and Ogre integration

Posted: Sun Aug 29, 2010 9:42 pm
by CABAListic
Basically, the above should still work, except you'll need to add two dummy functions to the Archive class, I think. But the compiler will hint you at them :)

Re: PhysFS C++ wrapper and Ogre integration

Posted: Sun Oct 10, 2010 3:50 pm
by Souvarine
Thanks for releasing this wrapper CABAListic.

I've integrated it into my game and it works well, except that resource loading is much slower now. I'm not using compressed archives. Did anyone had the same issue ? Any clue why PhysFS would be slower than Ogre's FileSystemArchive ?

Re: PhysFS C++ wrapper and Ogre integration

Posted: Mon Oct 11, 2010 6:39 pm
by CABAListic
Hm, I don't know, I didn't actually compare loading times. Perhaps it's because in my implementation, Ogre has to go through two wrappers - the custom PhysFS DataStream passes to a custom boost iostream for PhysFS files, which in turn calls into PhysFS. You could cut out the middle part, but I would suspect that hard disk access would be the primary time-consumer, so this shouldn't really matter.

Re: PhysFS C++ wrapper and Ogre integration

Posted: Fri Oct 07, 2011 3:02 pm
by Klaim
CABAListic > Did you think about maybe moving this in a public repository?