PhysFS C++ wrapper and Ogre integration

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
Contact:

PhysFS C++ wrapper and Ogre integration

Post by CABAListic » Wed Aug 22, 2007 8:58 pm

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.
0 x

User avatar
PolyVox
OGRE Contributor
OGRE Contributor
Posts: 1316
Joined: Tue Nov 21, 2006 11:28 am
Location: Groningen, The Netherlands
Contact:

Post by PolyVox » Wed Aug 22, 2007 9:35 pm

Hey, good job! That looks like it could be handy :D
0 x

User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
Contact:

Post by betajaen » Wed Aug 22, 2007 9:48 pm

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. ;)
0 x

User avatar
Roman
Halfling
Posts: 92
Joined: Sun Nov 02, 2008 3:49 pm
Location: Germany

Re: PhysFS C++ wrapper and Ogre integration

Post by Roman » Sun Mar 28, 2010 11:48 am

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.
0 x

User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 1
Contact:

Re: PhysFS C++ wrapper and Ogre integration

Post by Wolfmanfx » Sun Aug 29, 2010 2:34 pm

Have you updated the binding?
0 x

User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
Contact:

Re: PhysFS C++ wrapper and Ogre integration

Post by jacmoe » Sun Aug 29, 2010 9:33 pm

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. :)
0 x
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.

CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
Contact:

Re: PhysFS C++ wrapper and Ogre integration

Post by CABAListic » Sun Aug 29, 2010 9:42 pm

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 :)
0 x

User avatar
Souvarine
Halfling
Posts: 79
Joined: Mon Apr 28, 2008 12:01 am
Location: France

Re: PhysFS C++ wrapper and Ogre integration

Post by Souvarine » Sun Oct 10, 2010 3:50 pm

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 ?
0 x
Puzzle Platform Race
Open source action reflection platform game

CABAListic
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 2903
Joined: Thu Jan 18, 2007 2:48 pm
Contact:

Re: PhysFS C++ wrapper and Ogre integration

Post by CABAListic » Mon Oct 11, 2010 6:39 pm

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.
0 x

User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
Contact:

Re: PhysFS C++ wrapper and Ogre integration

Post by Klaim » Fri Oct 07, 2011 3:02 pm

CABAListic > Did you think about maybe moving this in a public repository?
0 x

Post Reply