| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
#ifndef FIFE_VFS_VFS_H |
| 23 |
#define FIFE_VFS_VFS_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
#include <set> |
| 29 |
|
| 30 |
// 3rd party library includes |
| 31 |
#include <boost/shared_ptr.hpp> |
| 32 |
|
| 33 |
// FIFE includes |
| 34 |
// These includes are split up in two parts, separated by one empty line |
| 35 |
// First block: files included from the FIFE root src directory |
| 36 |
// Second block: files included from the same folder |
| 37 |
#include "util/base/singleton.h" |
| 38 |
|
| 39 |
|
| 40 |
namespace FIFE { |
| 41 |
|
| 42 |
class RawData; |
| 43 |
|
| 44 |
class VFSSourceProvider; |
| 45 |
class VFSSource; |
| 46 |
|
| 47 |
/** the main VFS (virtual file system) class |
| 48 |
* |
| 49 |
* The VFS is intended to provide transparent and portable access to files. |
| 50 |
* @note The VFS searches for a provider in the order they are added to the |
| 51 |
* VFS. Since the VFSHostSystem is added first, this implies, that host filesystem |
| 52 |
* files will override whatever might be in other VFS Sources (e.g. the DAT files) |
| 53 |
* |
| 54 |
* @note All filenames have to be @b lowercase. The VFS will convert them to lowercase |
| 55 |
* and emit a warning. This is done to avoid problems with filesystems which are not |
| 56 |
* case sensitive. |
| 57 |
*/ |
| 58 |
class VFS : public DynamicSingleton<VFS>{ |
| 59 |
public: |
| 60 |
/** Constructor |
| 61 |
* Called by the Engine on startup. Never create one yourself. |
| 62 |
*/ |
| 63 |
VFS(); |
| 64 |
|
| 65 |
/** Destructor |
| 66 |
*/ |
| 67 |
virtual ~VFS(); |
| 68 |
|
| 69 |
void cleanup(); |
| 70 |
|
| 71 |
/** add new VFSSourceProvider |
| 72 |
* |
| 73 |
* @note VFS assumes ownership over the given provider - so don't do anything with it |
| 74 |
* after you call this function, especialy don't delete it! |
| 75 |
* @param provider the new provider |
| 76 |
*/ |
| 77 |
void addProvider(VFSSourceProvider* provider); |
| 78 |
|
| 79 |
/** tries to create a new VFSSource for the given file |
| 80 |
* |
| 81 |
* all currently known VFSSourceProviders are tried until one succeeds - if no provider succeeds 0 is returned |
| 82 |
* @param path the archive-file |
| 83 |
* @return the new VFSSource or 0 if no provider was succesfull or the file was already used as source |
| 84 |
*/ |
| 85 |
VFSSource* createSource(const std::string& path); |
| 86 |
|
| 87 |
/** create a new Source and add it to VFS |
| 88 |
* @see VFSSource* createSource(const std::string& file) |
| 89 |
*/ |
| 90 |
void addNewSource(const std::string& path); |
| 91 |
|
| 92 |
/** Add a new VFSSource */ |
| 93 |
void addSource(VFSSource* source); |
| 94 |
|
| 95 |
/** remove a VFSSource */ |
| 96 |
void removeSource(VFSSource* source); |
| 97 |
void removeSource(const std::string& path); |
| 98 |
|
| 99 |
/** Check if the given file exists |
| 100 |
* |
| 101 |
* @param file the filename |
| 102 |
* @return true if it exists, false if not |
| 103 |
*/ |
| 104 |
bool exists(const std::string& file) const; |
| 105 |
|
| 106 |
/** Check if the given path is a directory |
| 107 |
* |
| 108 |
* @param path to check |
| 109 |
* @return true if it is a directory, false if not |
| 110 |
*/ |
| 111 |
bool isDirectory(const std::string& path) const; |
| 112 |
|
| 113 |
/** Open a file |
| 114 |
* |
| 115 |
* @param path the file to open |
| 116 |
* @return the opened file; delete this when done. |
| 117 |
* @throws NotFound if the file cannot be found |
| 118 |
*/ |
| 119 |
RawData* open(const std::string& path); |
| 120 |
|
| 121 |
/** Get a filelist of the given directory |
| 122 |
* |
| 123 |
* @param path the directory |
| 124 |
* @return the filelist |
| 125 |
*/ |
| 126 |
std::set<std::string> listFiles(const std::string& path) const; |
| 127 |
|
| 128 |
/** List the files of a given directory matching a regex |
| 129 |
* |
| 130 |
* The whole string has to match the regex, this means if you want all files that end with .map don't search for |
| 131 |
* "\.map" but ".*\.map" (and escape the \) |
| 132 |
* |
| 133 |
* @param path the directory |
| 134 |
* @param filterregex the regex the files have to match |
| 135 |
* @return the filelist |
| 136 |
*/ |
| 137 |
std::set<std::string> listFiles(const std::string& path, const std::string& filterregex) const; |
| 138 |
|
| 139 |
/** Get a directorylist of the given directory |
| 140 |
* |
| 141 |
* @param path the directory |
| 142 |
* @return the directorylist |
| 143 |
*/ |
| 144 |
std::set<std::string> listDirectories(const std::string& path) const; |
| 145 |
|
| 146 |
/** List the subdirectorys of a given directory matching a regex |
| 147 |
* |
| 148 |
* @param path the directory |
| 149 |
* @param filterregex the regex the files have to match |
| 150 |
* @return the filelist |
| 151 |
*/ |
| 152 |
std::set<std::string> listDirectories(const std::string& path, const std::string& filterregex) const; |
| 153 |
|
| 154 |
/** Checks if a source is already present in a provider |
| 155 |
* |
| 156 |
* @param path the directory |
| 157 |
* @return true if the source is present, false if not |
| 158 |
*/ |
| 159 |
bool hasSource(const std::string& path) const; |
| 160 |
|
| 161 |
|
| 162 |
private: |
| 163 |
typedef std::vector<VFSSourceProvider*> type_providers; |
| 164 |
type_providers m_providers; |
| 165 |
|
| 166 |
typedef std::vector<VFSSource*> type_sources; |
| 167 |
type_sources m_sources; |
| 168 |
|
| 169 |
std::set<std::string> filterList(const std::set<std::string>& list, const std::string& fregex) const; |
| 170 |
VFSSource* getSourceForFile(const std::string& file) const; |
| 171 |
}; |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
#endif |