1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-29 03:33:48 +00:00
Aquaria/ExternalLibs/ttvfs/VFSRoot.cpp

262 lines
6 KiB
C++
Raw Normal View History

2014-04-06 17:19:33 +00:00
// VFSRoot.cpp - glues it all together and makes use simple
// For conditions of distribution and use, see copyright notice in VFS.h
#include "VFSInternal.h"
#include "VFSRoot.h"
#include "VFSTools.h"
#include "VFSDirInternal.h"
#include "VFSFile.h"
#include "VFSLoader.h"
#include "VFSArchiveLoader.h"
2014-04-15 13:04:33 +00:00
#include "VFSDirView.h"
2014-04-06 17:19:33 +00:00
#ifdef _DEBUG
# include <cassert>
# define DEBUG_ASSERT(x) assert(x)
#else
# define DEBUG_ASSERT(x)
#endif
VFS_NAMESPACE_START
// predecl is in VFS.h
bool _checkCompatInternal(_AbiCheck *used)
{
if(sizeof(_AbiCheck) != used->structSize)
return false;
_AbiCheck here;
memset(&here, 0, sizeof(here));
here.structSize = sizeof(here);
here.vfsposSize = sizeof(vfspos);
#ifdef VFS_LARGEFILE_SUPPORT
here.largefile = 1;
#endif
#ifdef VFS_IGNORE_CASE
here.nocase = 1;
#endif
return !memcmp(&here, used, sizeof(here));
}
Root::Root()
: merged(new InternalDir(""))
{
}
Root::~Root()
{
}
void Root::Clear()
{
merged->_clearDirs();
merged->_clearMounts();
loaders.clear();
archLdrs.clear();
2017-01-12 22:52:59 +00:00
loadersInfo.clear();
2014-04-06 17:19:33 +00:00
}
2014-04-07 00:25:58 +00:00
void Root::Mount(const char *src, const char *dest)
2014-04-06 17:19:33 +00:00
{
2017-01-12 22:52:59 +00:00
return AddVFSDir(GetDir(src, true), dest);
2014-04-06 17:19:33 +00:00
}
2014-04-07 00:25:58 +00:00
void Root::AddVFSDir(DirBase *dir, const char *subdir /* = NULL */)
2014-04-06 17:19:33 +00:00
{
if(!subdir)
subdir = dir->fullname();
2014-04-15 13:04:33 +00:00
InternalDir *into = safecastNonNull<InternalDir*>(merged->_getDirEx(subdir, subdir, true, true, false).first);
2014-04-06 17:19:33 +00:00
into->_addMountDir(dir);
}
2017-01-12 22:52:59 +00:00
bool Root::RemoveVFSDir(DirBase *dir, const char *subdir /* = NULL */)
{
if(!subdir)
subdir = dir->fullname();
InternalDir *vddest = safecast<InternalDir*>(GetDir(subdir, false));
if(!vddest)
return false;
vddest->_removeMountDir(dir);
return true;
}
2014-04-06 17:19:33 +00:00
bool Root::Unmount(const char *src, const char *dest)
{
DirBase *vdsrc = GetDir(src, false);
2017-01-12 22:52:59 +00:00
if(!vdsrc)
2014-04-06 17:19:33 +00:00
return false;
2017-01-12 22:52:59 +00:00
return RemoveVFSDir(vdsrc, dest);
2014-04-06 17:19:33 +00:00
}
2017-01-12 22:52:59 +00:00
int Root::AddLoader(VFSLoader *ldr, const char *path /* = NULL */)
2014-04-06 17:19:33 +00:00
{
2017-01-12 22:52:59 +00:00
DEBUG_ASSERT(ldr != NULL);
2014-04-06 17:19:33 +00:00
loaders.push_back(ldr);
2017-01-12 22:52:59 +00:00
loadersInfo.push_back(path);
2014-04-06 17:19:33 +00:00
AddVFSDir(ldr->getRoot(), path);
2017-01-12 22:52:59 +00:00
return (int)(loaders.size() - 1);
2014-04-06 17:19:33 +00:00
}
2017-01-12 22:52:59 +00:00
void Root::RemoveLoader(int index, const char *path /* = NULL */)
2014-04-06 17:19:33 +00:00
{
2017-01-12 22:52:59 +00:00
VFSLoader *ldr = loaders[index];
RemoveVFSDir(ldr->getRoot(), loadersInfo[index].getPath());
loaders.erase(loaders.begin() + index);
loadersInfo.erase(loadersInfo.begin() + index);
}
int Root::AddArchiveLoader(VFSArchiveLoader *ldr)
{
DEBUG_ASSERT(ldr != NULL);
2014-04-06 17:19:33 +00:00
archLdrs.push_back(ldr);
2017-01-12 22:52:59 +00:00
return (int)(archLdrs.size() - 1);
}
void Root::RemoveArchiveLoader(int index)
{
archLdrs.erase(archLdrs.begin() + index);
2014-04-06 17:19:33 +00:00
}
2014-04-15 13:04:33 +00:00
2014-04-06 17:19:33 +00:00
Dir *Root::AddArchive(const char *arch, void *opaque /* = NULL */)
{
2014-04-07 00:25:58 +00:00
return AddArchive(GetFile(arch), arch, opaque);
}
Dir *Root::AddArchive(File *file, const char *path /* = NULL */, void *opaque /* = NULL */)
{
if(!file || !file->open("rb"))
2014-04-06 17:19:33 +00:00
return NULL;
Dir *ad = NULL;
VFSLoader *fileLdr = NULL;
for(ArchiveLoaderArray::iterator it = archLdrs.begin(); it != archLdrs.end(); ++it)
2014-04-07 00:25:58 +00:00
if((ad = (*it)->Load(file, &fileLdr, opaque)))
2014-04-06 17:19:33 +00:00
break;
if(!ad)
return NULL;
if(fileLdr)
loaders.push_back(fileLdr);
2014-04-07 00:25:58 +00:00
AddVFSDir(ad, path ? path : file->fullname());
2014-04-06 17:19:33 +00:00
return ad;
}
inline static File *VFSHelper_GetFileByLoader(VFSLoader *ldr, const char *fn, const char *unmangled)
{
if(!ldr)
return NULL;
File *vf = ldr->Load(fn, unmangled);
if(vf)
2017-01-12 22:52:59 +00:00
ldr->getRoot()->add(vf);
2014-04-06 17:19:33 +00:00
return vf;
}
File *Root::GetFile(const char *fn)
{
const char *unmangled = fn;
std::string fixed = fn; // TODO: get rid of allocation here
FixPath(fixed);
fn = fixed.c_str();
File *vf = NULL;
vf = merged->getFile(fn);
// nothing found? maybe a loader has something.
// if so, add the newly created File to the tree.
if(!vf)
for(LoaderArray::iterator it = loaders.begin(); it != loaders.end(); ++it)
if((vf = VFSHelper_GetFileByLoader(*it, fn, unmangled)))
break;
//printf("VFS: GetFile '%s' -> '%s' (%s:%p)\n", fn, vf ? vf->fullname() : "NULL", vf ? vf->getType() : "?", vf);
return vf;
}
InternalDir *Root::_GetDirByLoader(VFSLoader *ldr, const char *fn, const char *unmangled)
{
Dir *realdir = ldr->LoadDir(fn, unmangled);
InternalDir *ret = NULL;
if(realdir)
{
2014-04-15 13:04:33 +00:00
//ret = safecastNonNull<InternalDir*>(merged->_getDirEx(fn, fn, true, true, false).first);
ret = safecastNonNull<InternalDir*>(merged->_createAndInsertSubtree(fn));
2014-04-06 17:19:33 +00:00
ret->_addMountDir(realdir);
}
return ret;
}
DirBase *Root::GetDir(const char* dn, bool create /* = false */)
{
2014-04-15 13:04:33 +00:00
//printf("Root ::getDir [%s]\n", dn);
2014-04-06 17:19:33 +00:00
const char *unmangled = dn;
std::string fixed = dn; // TODO: get rid of alloc
FixPath(fixed);
dn = fixed.c_str();
if(!*dn)
return merged;
DirBase *vd = merged->getDir(dn);
if(!vd)
{
for(LoaderArray::iterator it = loaders.begin(); it != loaders.end(); ++it)
if((vd = _GetDirByLoader(*it, dn, unmangled)))
break;
if(!vd && create)
2014-04-15 13:04:33 +00:00
vd = safecastNonNull<InternalDir*>(merged->_createAndInsertSubtree(dn)); // typecast is for debug checking only
2014-04-06 17:19:33 +00:00
}
//printf("VFS: GetDir '%s' -> '%s' (%s:%p)\n", dn, vd ? vd->fullname() : "NULL", vd ? vd->getType() : "?", vd);
return vd;
}
DirBase *Root::GetDirRoot()
{
return merged;
}
bool Root::FillDirView(const char *path, DirView& view)
{
2014-04-15 13:04:33 +00:00
//printf("Root::FillDirView [%s]\n", path);
2014-04-06 17:19:33 +00:00
return merged->fillView(path, view);
}
void Root::ClearGarbage()
{
merged->clearGarbage();
}
2017-01-12 22:52:59 +00:00
bool Root::ForEach(const char *path, FileEnumCallback fileCallback /* = NULL */, DirEnumCallback dirCallback /* = NULL */,
void *user /* = NULL */, bool safe /* = false */)
2014-04-15 13:04:33 +00:00
{
DirView view;
2017-01-12 22:52:59 +00:00
if(!FillDirView(path, view))
return false;
2014-04-06 17:19:33 +00:00
2017-01-12 22:52:59 +00:00
if(dirCallback)
view.forEachDir(dirCallback, user, safe);
if(fileCallback)
view.forEachFile(fileCallback, user, safe);
2014-04-06 17:19:33 +00:00
2017-01-12 22:52:59 +00:00
return true;
2014-04-06 17:19:33 +00:00
}
VFS_NAMESPACE_END