void Doom3FileSystem::initPakFile(ArchiveLoader& archiveModule, const std::string& filename)
{
    std::string fileExt(os::getExtension(filename));
    boost::to_lower(fileExt);

    if (_allowedExtensions.find(fileExt) != _allowedExtensions.end())
    {
        // Matched extension for archive (e.g. "pk3", "pk4")
        ArchiveDescriptor entry;

        entry.name = filename;
        entry.archive = archiveModule.openArchive(filename);
        entry.is_pakfile = true;
        _archives.push_back(entry);

        rMessage() << "[vfs] pak file: " << filename << std::endl;
    }
    else if (_allowedExtensionsDir.find(fileExt) != _allowedExtensionsDir.end())
    {
        // Matched extension for archive dir (e.g. "pk3dir", "pk4dir")
        ArchiveDescriptor entry;

        std::string path = os::standardPathWithSlash(filename);
        entry.name = path;
        entry.archive = DirectoryArchivePtr(new DirectoryArchive(path));
        entry.is_pakfile = false;
        _archives.push_back(entry);

        rMessage() << "[vfs] pak dir:  " << path << std::endl;
    }
}
void Doom3FileSystem::initDirectory(const std::string& inputPath)
{
    if (_numDirectories == (VFS_MAXDIRS-1)) {
        return;
    }

    // greebo: Normalise path: Replace backslashes and ensure trailing slash
    _directories[_numDirectories] = os::standardPathWithSlash(inputPath);

    // Shortcut
    const std::string& path = _directories[_numDirectories];

    _numDirectories++;

    {
        ArchiveDescriptor entry;
        entry.name = path;
        entry.archive = DirectoryArchivePtr(new DirectoryArchive(path));
        entry.is_pakfile = false;
        _archives.push_back(entry);
    }

    // Instantiate a new sorting container for the filenames
    SortedFilenames filenameList;

    // Traverse the directory using the filename list as functor
    try
    {
		os::foreachItemInDirectory(path, [&] (const boost::filesystem::path& file)
		{
			// Just insert the name, it will get sorted correctly.
			filenameList.insert(file.filename().string());
		});
    }
    catch (os::DirectoryNotFoundException&)
    {
        std::cout << "[vfs] Directory '" << path << "' not found."
                  << std::endl;
    }

    if (filenameList.empty())
    {
        return; // nothing found
    }

    rMessage() << "[vfs] searched directory: " << path << std::endl;

    // Get the ArchiveLoader and try to load each file
    ArchiveLoader& archiveModule = GlobalArchive("PK4");

    // add the entries to the vfs
    for (SortedFilenames::iterator i = filenameList.begin(); i != filenameList.end(); ++i) {
        // Assemble the filename and try to load the archive
        initPakFile(archiveModule, path + *i);
    }
}