This repository was archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathEmberOgreFileSystem.cpp
More file actions
257 lines (207 loc) · 8.74 KB
/
EmberOgreFileSystem.cpp
File metadata and controls
257 lines (207 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
-----------------------------------------------------------------------------
This source file is based on source files that are part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2006 Torus Knot Software Ltd
Copyright (c) 2008 Erik Ogenvik <erik@ogenvik.org>
Also see acknowledgements in Readme.html
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA., or go to
http://www.gnu.org/copyleft/lesser.txt.
You may alternatively use this source under the terms of a specific version of
the OGRE Unrestricted License provided you have obtained such a license from
Torus Knot Software Ltd.
-----------------------------------------------------------------------------
*/
#include "EmberOgreFileSystem.h"
#include <OgreFileSystem.h>
#include <OgreLogManager.h>
#include <boost/algorithm/string/replace.hpp>
using namespace Ogre;
namespace {
boost::filesystem::path concatenate_path(const boost::filesystem::path& base, const boost::filesystem::path& name) {
if (base.empty() || name.is_absolute()) {
return name;
} else {
return base / name;
}
}
}
namespace Ember {
namespace OgreView {
//-----------------------------------------------------------------------
FileSystemArchive::FileSystemArchive(const String& name, const String& archType)
: Archive(name, archType), mBaseName(name) {
}
//-----------------------------------------------------------------------
bool FileSystemArchive::isCaseSensitive() const {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
return false;
#else
return true;
#endif
}
void FileSystemArchive::findFiles(String pattern, bool recursive,
bool dirs, StringVector* simpleList, FileInfoList* detailList) const {
std::unique_ptr<std::regex> regex = nullptr;
// pattern can contain a directory name, separate it from mask
size_t pos1 = pattern.rfind('/');
size_t pos2 = pattern.rfind('\\');
if (pos1 == String::npos || ((pos2 != String::npos) && (pos1 < pos2)))
pos1 = pos2;
String directory;
if (pos1 != String::npos) {
directory = pattern.substr(0, pos1 + 1);
pattern = pattern.substr(pos1 + 1);
}
auto base_dir = mBaseName;
if (!directory.empty()) {
base_dir = concatenate_path(mBaseName, directory);
}
if (pattern != "*") {
auto patternEscaped = pattern;
boost::replace_all(patternEscaped, "\\", "\\\\");
boost::replace_all(patternEscaped, "^", "\\^");
boost::replace_all(patternEscaped, ".", "\\.");
boost::replace_all(patternEscaped, "$", "\\$");
boost::replace_all(patternEscaped, "|", "\\|");
boost::replace_all(patternEscaped, "(", "\\(");
boost::replace_all(patternEscaped, ")", "\\)");
boost::replace_all(patternEscaped, "[", "\\[");
boost::replace_all(patternEscaped, "]", "\\]");
boost::replace_all(patternEscaped, "*", "\\*");
boost::replace_all(patternEscaped, "+", "\\+");
boost::replace_all(patternEscaped, "?", "\\?");
boost::replace_all(patternEscaped, "/", "\\/");
boost::replace_all(patternEscaped, "{", "\\{");
boost::replace_all(patternEscaped, "}", "\\}");
boost::replace_all(patternEscaped, "\\?", ".");
boost::replace_all(patternEscaped, "\\*", ".*");
regex = std::make_unique<std::regex>(patternEscaped);
}
if (boost::filesystem::exists(base_dir)) {
findFiles(base_dir, regex, recursive, dirs, simpleList, detailList);
}
}
//-----------------------------------------------------------------------
void FileSystemArchive::findFiles(const boost::filesystem::path& directory,
const std::unique_ptr<std::regex>& pattern,
bool recursive,
bool dirs, StringVector* simpleList, FileInfoList* detailList) const {
//if there's a file with the name "norecurse" we shouldn't recurse further
if (boost::filesystem::exists(directory / "norecurse")) {
return;
}
boost::filesystem::directory_iterator it(directory);
boost::filesystem::directory_iterator end{};
while (it != end) {
if (boost::filesystem::is_directory(it->path())) {
if (recursive) {
if (it->path().filename().string() != "source") {
findFiles(it->path(), pattern, recursive, dirs, simpleList, detailList);
}
}
} else {
if (!pattern || std::regex_match(it->path().filename().string(), *pattern)) {
if (simpleList) {
simpleList->emplace_back(boost::filesystem::relative(it->path(), mBaseName).generic_string());
} else if (detailList) {
auto relativePath = boost::filesystem::relative(it->path(), mBaseName);
auto parentPath = relativePath.parent_path();
FileInfo fi;
fi.archive = this;
fi.filename = relativePath.generic_string();
fi.basename = it->path().filename().generic_string();
fi.path = parentPath.empty() ? "" : parentPath.generic_string() + "/";
fi.compressedSize = boost::filesystem::file_size(it->path());
fi.uncompressedSize = fi.compressedSize;
detailList->emplace_back(std::move(fi));
}
}
}
it++;
}
}
//-----------------------------------------------------------------------
FileSystemArchive::~FileSystemArchive() {
unload();
}
//-----------------------------------------------------------------------
void FileSystemArchive::load() {
// do nothing here, what has to be said will be said later
}
//-----------------------------------------------------------------------
void FileSystemArchive::unload() {
// nothing to see here, move along
}
//-----------------------------------------------------------------------
DataStreamPtr FileSystemArchive::open(const String& filename, bool readOnly) const {
auto full_path = concatenate_path(mName, filename);
// Always open in binary mode
auto* origStream = OGRE_NEW_T(std::ifstream, MEMCATEGORY_GENERAL)();
origStream->open(full_path.string().c_str(), std::ios::in | std::ios::binary);
// Should check ensure open succeeded, in case fail for some reason.
if (origStream->fail()) {
OGRE_DELETE_T(origStream, basic_ifstream, MEMCATEGORY_GENERAL);
OGRE_EXCEPT(Ogre::Exception::ERR_FILE_NOT_FOUND,
"Cannot open file: " + filename,
"FileSystemArchive::open");
}
auto size = boost::filesystem::file_size(full_path);
// Construct return stream, tell it to delete on destroy
auto* stream = OGRE_NEW FileStreamDataStream(filename,
origStream, size, true);
return DataStreamPtr(stream);
}
//-----------------------------------------------------------------------
StringVectorPtr FileSystemArchive::list(bool recursive, bool dirs) const {
// directory change requires locking due to saved returns
StringVectorPtr ret(OGRE_NEW_T(StringVector, MEMCATEGORY_GENERAL)(), SPFM_DELETE_T);
findFiles("*", recursive, dirs, ret.get(), nullptr);
return ret;
}
//-----------------------------------------------------------------------
FileInfoListPtr FileSystemArchive::listFileInfo(bool recursive, bool dirs) const {
FileInfoListPtr ret(OGRE_NEW_T(FileInfoList, MEMCATEGORY_GENERAL)(), SPFM_DELETE_T);
findFiles("*", recursive, dirs, nullptr, ret.get());
return ret;
}
//-----------------------------------------------------------------------
StringVectorPtr FileSystemArchive::find(const String& pattern,
bool recursive, bool dirs) const {
StringVectorPtr ret(OGRE_NEW_T(StringVector, MEMCATEGORY_GENERAL)(), SPFM_DELETE_T);
findFiles(pattern, recursive, dirs, ret.get(), nullptr);
return ret;
}
//-----------------------------------------------------------------------
FileInfoListPtr FileSystemArchive::findFileInfo(const String& pattern,
bool recursive, bool dirs) const {
FileInfoListPtr ret(OGRE_NEW_T(FileInfoList, MEMCATEGORY_GENERAL)(), SPFM_DELETE_T);
findFiles(pattern, recursive, dirs, nullptr, ret.get());
return ret;
}
//-----------------------------------------------------------------------
bool FileSystemArchive::exists(const String& filename) const {
auto full_path = concatenate_path(mBaseName, filename);
return boost::filesystem::exists(full_path);
}
time_t FileSystemArchive::getModifiedTime(const String& filename) const {
auto full_path = concatenate_path(mBaseName, filename);
return boost::filesystem::last_write_time(full_path);
}
//-----------------------------------------------------------------------
const String& FileSystemArchiveFactory::getType() const {
static String name = "EmberFileSystem";
return name;
}
}
}