Page 1 of 1

irr::IFileSystem extension

Posted: Fri Dec 24, 2004 5:05 pm
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;
}

Posted: Sat Dec 25, 2004 1:32 pm
by Electron
nice, thanks