irr::IFileSystem extension

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
zola
Posts: 52
Joined: Thu Jul 15, 2004 2:31 pm
Location: switzerland
Contact:

irr::IFileSystem extension

Post by zola »

Lately I've had some trouble loading files into my application.

The Irrlicht FileSystem object is surely a fine thing and the way it allows to add ZipFiles is really cool. What I like most about it is that you can mount a ZipFile and tell the engine to forget about paths when loading textures, meshes and stuff. I decided that this would also be a nice feature for the normal file access. So, here's an extension for Irrlicht that allows You to add Paths which will be used when loading a file from the current working directory is not successfull.

Add the following to IFileSystem.h

Code: Select all

//! Adds a path to the filesystem
//! files that can't be found with the provided filename are searched for in the given paths
virtual void addPath(const c8* path)=0;
virtual void removePath(const c8* path)=0;
And this to CFileSystem.h

Code: Select all

public:
//! Adds a path to the filesystem
//! files that can't be found with the provided filename are searched for in the given paths
virtual void addPath(const c8* path);
virtual void removePath(const c8* path);

private:
core::array Paths;
Add the following to CFileSystem.cpp

Code: Select all

void CFileSystem::addPath(const c8* path)
{
	core::stringc p=path;
	if(Paths.binary_search(p)<0)
	{
		Paths.push_back(p);
	}
}

void CFileSystem::removePath(const c8* path)
{
	core::stringc p=path;
	s32 idx=Paths.binary_search(p);
	if(idx!=-1)
	{
		Paths.erase(idx);
	}
}
And change the following methods with the new implementation

Code: Select all

//! determinates if a file exists and would be able to be opened.
bool CFileSystem::existFile(const c8* filename)
{
	for (u32 i=0; i<ZipFileSystems.size(); ++i)
		if (ZipFileSystems[i]->findFile(filename)!=-1)
			return true;

	FILE* f = 0;
	f = fopen(filename, "rb");
	if (f) fclose(f);

	core::stringc cp=getWorkingDirectory();
	for(u32 idx=0; idx<Paths.size() && f==0 ;idx++)
		if(changeWorkingDirectoryTo(Paths[idx].c_str()))
		{
			f = fopen(filename, "rb");
			if(f) fclose(f);
			changeWorkingDirectoryTo(cp.c_str());
		}
	changeWorkingDirectoryTo(cp.c_str());
	
	return f!=0;
}

IReadFile* CFileSystem::createAndOpenFile(const c8* filename)
{
	using namespace core;

	IReadFile* file = 0;

	for (u32 i=0; i<ZipFileSystems.size(); ++i)
	{
		file = ZipFileSystems[i]->openFile(filename);
		if (file)
			return file;
	}

	file = createReadFile(filename);
	
	if(file==0)
	{
		stringc cp=getWorkingDirectory();
		for(u32 idx=0;idx<Paths.size() && file==0 ;idx++)
		{
			if(changeWorkingDirectoryTo(Paths[idx].c_str()))
			{
				file=createReadFile(filename);
				changeWorkingDirectoryTo(cp.c_str());
			}
		}
		changeWorkingDirectoryTo(cp.c_str());
	}

	return file;
}
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post by Electron »

nice, thanks
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.

Crucible of Stars
Post Reply