]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - include/ifilesystem.h
Merge branch 'q3map2help' into 'master'
[xonotic/netradiant.git] / include / ifilesystem.h
index 8a9a20067c2198a06078e4aa6710db682aae1cfa..d7e128e6e1791a83f40dcc81102a01acbfb26fc3 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*/\r
-\r
-#ifndef _IFILESYSTEM_H_\r
-#define _IFILESYSTEM_H_\r
-\r
-//\r
-// Plugin interface for the virtual filesystem used by Radiant\r
-//\r
-\r
-// NOTE: If you want to write a VFS plugin then you must export\r
-// "QERPlug_ListInterfaces" and "QERPlug_RequestInterface"\r
-// (see qerplugin.h for more information)\r
-\r
-#ifdef _WIN32\r
-#define VFS_NATIVESEPARATOR '\\'\r
-#else\r
-#define VFS_NATIVESEPARATOR '/'\r
-#endif\r
-\r
-#define VFS_MAJOR "VFS"\r
-\r
-// return the file system supported by the plugin, for example: "quake1" or "quake3"\r
-//typedef const char* (WINAPI* PFN_VFSGETFORMAT) ();\r
-// add all files from a directory to the vfs\r
-typedef void (* PFN_VFSINITDIRECTORY) (const char *path);\r
-// free all resources used by the plugin\r
-typedef void (* PFN_VFSSHUTDOWN) ();\r
-// free memory allocated by VFS for this pointer\r
-typedef void (* PFN_VFSFREEFILE) (void *p);\r
-// return a GSList with all the directories under basedir \r
-typedef GSList* (* PFN_VFSGETDIRLIST) (const char *basedir);\r
-// return a GSList with all the files under basedir (extension can be NULL)\r
-typedef GSList* (* PFN_VFSGETFILELIST) (const char *basedir, const char *extension);\r
-// free a dirlist or filelist returned from one of the above functions\r
-typedef void (* PFN_VFSCLEARFILEDIRLIST) (GSList **lst);\r
-#define VFS_SEARCH_PAK 0x1\r
-#define VFS_SEARCH_DIR 0x2\r
-/*!\r
-\brief return the number of files with the exact name described in filename\r
-there can be several hits for a given file, or this can be used to check for existence\r
-\param flags is optional and can be used with VFS_SEARCH_* bits, if flag is 0, everything is searched, else only the specified bits\r
-paks are searched first, then search directories\r
-*/\r
-typedef int (* PFN_VFSGETFILECOUNT) (const char *filename, int flags);\r
-/*!\r
-\brief load file, allocate buffer\r
-\return -1 if fails or the size of the buffer allocated\r
-\param index is used to load the i-th file in the search directories (see vfsGetFileCount)\r
-this will scan in the search directories first, then it will search in the pak files\r
-WARNING: the allocated buffer must be freed with a g_free call\r
-NOTE TTimo: the g_free release is utter horror\r
- see http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=491\r
-*/\r
-typedef int (* PFN_VFSLOADFILE) (const char *filename, void **buffer, int index);\r
-// load a file from it's full path into the buffer, returns the file size or -1\r
-// the allocated buffer must be freed with a g_free call\r
-typedef int (* PFN_VFSLOADFULLPATHFILE) (const char *filename, void **buffer);\r
-// takes an absolute file path, returns a shortened relative file path if the absolute path matches a valid basedir or NULL if an error occured\r
-typedef char* (* PFN_VFSEXTRACTRELATIVEPATH) (const char *in);\r
-/*!\r
-\return the full path (in a static buff) to a file given it's relative path (NULL if not found)\r
-\param index if several files are matching (as returned in a call to vfsGetFileCount), get the index-th file\r
-\param flag 0 or a combination of VFS_SEARCH_PAK or VFS_SEARCH_DIR\r
-HYDRA:\r
-  this now searches VFS/PAK files in addition to the filesystem\r
-  if FLAG is 0 then ONLY dirs are searched.\r
-  PAK's are searched before DIRs to mimic engine behaviour\r
-  index is ignored when searching PAK files.\r
-  when searching VFS, files are searched case insensitive.\r
-\r
-WARNING: if you use index from vfsGetFileCount, it works only with a vfsGetFileCount for the search directories only (not the pak files)\r
-FIXME TTimo our VFS names are case insensitive.\r
-   this function is not able to build the full path from case-insensitive name\r
-   ( this is http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=130 )\r
-*/\r
-typedef char* (* PFN_VFSGETFULLPATH) (const char *in, int index, int flag);\r
-/*!\r
-these return a static char*, doesn't need to be freed or anything\r
-get the base path to use when raising file dialogs\r
-we manually add "maps/" or "sounds/" or "mapobjects/models/" etc.\r
-FIXME: I'm not sure this is used / relevant anymore\r
-*/\r
-typedef const char* (* PFN_VFSBASEPROMPTPATH) ();\r
-\r
-// VFS API\r
-struct _QERFileSystemTable\r
-{\r
-  int m_nSize;\r
-  PFN_VFSINITDIRECTORY        m_pfnInitDirectory;\r
-  PFN_VFSSHUTDOWN             m_pfnShutdown;\r
-  PFN_VFSFREEFILE             m_pfnFreeFile;\r
-  PFN_VFSGETDIRLIST           m_pfnGetDirList;\r
-  PFN_VFSGETFILELIST          m_pfnGetFileList;\r
-  PFN_VFSCLEARFILEDIRLIST     m_pfnClearFileDirList;\r
-  PFN_VFSGETFILECOUNT         m_pfnGetFileCount;\r
-  PFN_VFSLOADFILE             m_pfnLoadFile;\r
-  PFN_VFSLOADFULLPATHFILE     m_pfnLoadFullPathFile;\r
-  PFN_VFSEXTRACTRELATIVEPATH  m_pfnExtractRelativePath;\r
-  PFN_VFSGETFULLPATH          m_pfnGetFullPath;\r
-  PFN_VFSBASEPROMPTPATH       m_pfnBasePromptPath;\r
-};\r
-\r
-#ifdef USE_VFSTABLE_DEFINE\r
-#ifndef __VFSTABLENAME\r
-#define __VFSTABLENAME g_FileSystemTable\r
-#endif\r
-#define vfsInitDirectory __VFSTABLENAME.m_pfnInitDirectory\r
-#define vfsShutdown __VFSTABLENAME.m_pfnShutdown\r
-#define vfsFreeFile __VFSTABLENAME.m_pfnFreeFile\r
-#define vfsGetDirList __VFSTABLENAME.m_pfnGetDirList\r
-#define vfsGetFileList __VFSTABLENAME.m_pfnGetFileList\r
-#define vfsClearFileDirList __VFSTABLENAME.m_pfnClearFileDirList\r
-#define vfsGetFileCount __VFSTABLENAME.m_pfnGetFileCount\r
-#define vfsLoadFile __VFSTABLENAME.m_pfnLoadFile\r
-#define vfsLoadFullPathFile __VFSTABLENAME.m_pfnLoadFullPathFile\r
-#define vfsExtractRelativePath __VFSTABLENAME.m_pfnExtractRelativePath\r
-#define vfsGetFullPath __VFSTABLENAME.m_pfnGetFullPath\r
-#define vfsBasePromptPath __VFSTABLENAME.m_pfnBasePromptPath\r
-#endif\r
-\r
-#endif // _IFILESYSTEM_H_\r
+/*
+   Copyright (C) 2001-2006, William Joseph.
+   All Rights Reserved.
+
+   This file is part of GtkRadiant.
+
+   GtkRadiant is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   GtkRadiant is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with GtkRadiant; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#if !defined( INCLUDED_IFILESYSTEM_H )
+#define INCLUDED_IFILESYSTEM_H
+
+#include <cstddef>
+#include "generic/constant.h"
+#include "generic/callback.h"
+
+typedef Callback<void(const char*)> ArchiveNameCallback;
+typedef Callback<void(const char*)> FileNameCallback;
+
+class ArchiveFile;
+class ArchiveTextFile;
+class Archive;
+
+class ModuleObserver;
+
+typedef struct _GSList GSList;
+
+/// The Virtual File System.
+class VirtualFileSystem
+{
+public:
+INTEGER_CONSTANT( Version, 1 );
+STRING_CONSTANT( Name, "VFS" );
+
+/// \brief Adds a root search \p path.
+/// Called before \c initialise.
+virtual void initDirectory( const char *path ) = 0;
+/// \brief Initialises the filesystem.
+/// Called after all root search paths have been added.
+virtual void initialise() = 0;
+/// \brief Clear the filesystem if supported
+virtual void clear() = 0;
+/// \brief Reload the filesystem if supported
+virtual void refresh() = 0;
+/// \brief Shuts down the filesystem.
+virtual void shutdown() = 0;
+
+/// \brief Returns the file identified by \p filename opened in binary mode, or 0 if not found.
+/// The caller must \c release() the file returned if it is not 0.
+virtual ArchiveFile* openFile( const char* filename ) = 0;
+/// \brief Returns the file identified by \p filename opened in text mode, or 0 if not found.
+/// The caller must \c release() the file returned if it is not 0.
+virtual ArchiveTextFile* openTextFile( const char* filename ) = 0;
+
+/// \brief Opens the file identified by \p filename and reads it into \p buffer, or sets *\p buffer to 0 if not found.
+/// Returns the size of the buffer allocated, or undefined value if *\p buffer is 0;
+/// The caller must free the allocated buffer by calling \c freeFile
+/// \deprecated Deprecated - use \c openFile.
+virtual std::size_t loadFile( const char *filename, void **buffer ) = 0;
+/// \brief Frees the buffer returned by \c loadFile.
+/// \deprecated Deprecated.
+virtual void freeFile( void *p ) = 0;
+
+/// \brief Calls \p callback for each directory under \p basedir.
+virtual void forEachDirectory( const char* basedir, const FileNameCallback& callback, std::size_t depth = 1 ) = 0;
+/// \brief Calls \p callback for each file under \p basedir matching \p extension.
+/// Use "*" as \p extension to match all file extensions.
+virtual void forEachFile( const char* basedir, const char* extension, const FileNameCallback& callback, std::size_t depth = 1 ) = 0;
+
+/// \brief Returns a list containing the relative names of all the directories under \p basedir.
+/// The caller must free the returned list by calling \c clearFileDirList;
+/// \deprecated Deprecated - use \c forEachDirectory.
+virtual GSList* getDirList( const char *basedir ) = 0;
+/// \brief Returns a list containing the relative names of the files under \p basedir (\p extension can be "*" for all files).
+/// The caller must free the returned list by calling \c clearFileDirList.
+/// \deprecated Deprecated - use \c forEachFile.
+virtual GSList* getFileList( const char *basedir, const char *extension ) = 0;
+/// \brief Frees the \p list returned from \c getDirList or \c getFileList.
+/// \deprecated Deprecated.
+virtual void clearFileDirList( GSList **list ) = 0;
+
+/// \brief Returns the absolute filename for a relative \p name, or "" if not found.
+virtual const char* findFile( const char* name ) = 0;
+/// \brief Returns the filesystem root for an absolute \p name, or "" if not found.
+/// This can be used to convert an absolute name to a relative name.
+virtual const char* findRoot( const char* name ) = 0;
+
+/// \brief Attach an \p observer whose realise() and unrealise() methods will be called when the filesystem is initialised or shut down.
+virtual void attach( ModuleObserver& observer ) = 0;
+/// \brief Detach an \p observer previously-attached by calling \c attach.
+virtual void detach( ModuleObserver& observer ) = 0;
+
+virtual Archive* getArchive( const char* archiveName, bool pakonly = true ) = 0;
+virtual void forEachArchive( const ArchiveNameCallback& callback, bool pakonly = true, bool reverse = false ) = 0;
+};
+
+#include "modulesystem.h"
+
+template<typename Type>
+class GlobalModule;
+typedef GlobalModule<VirtualFileSystem> GlobalFileSystemModule;
+
+template<typename Type>
+class GlobalModuleRef;
+typedef GlobalModuleRef<VirtualFileSystem> GlobalFileSystemModuleRef;
+
+inline VirtualFileSystem& GlobalFileSystem(){
+       return GlobalFileSystemModule::getTable();
+}
+
+
+/// \deprecated Use \c openFile.
+inline int vfsLoadFile( const char* filename, void** buffer, int index = 0 ){
+       return static_cast<int>( GlobalFileSystem().loadFile( filename, buffer ) );
+}
+
+/// \deprecated Deprecated.
+inline void vfsFreeFile( void* p ){
+       GlobalFileSystem().freeFile( p );
+}
+
+#endif