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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "VFSHandler.h"
#include <algorithm>
#include <set>
#include <cstring>
#include "ArchiveLoader.h"
#include "System/FileSystem/Archives/IArchive.h"
#include "FileSystem.h"
#include "ArchiveScanner.h"
#include "System/Exceptions.h"
#include "System/Log/ILog.h"
#include "System/StringUtil.h"
#define LOG_SECTION_VFS "VFS"
LOG_REGISTER_SECTION_GLOBAL(LOG_SECTION_VFS)
// use the specific section for all LOG*() calls in this source file
#ifdef LOG_SECTION_CURRENT
#undef LOG_SECTION_CURRENT
#endif
#define LOG_SECTION_CURRENT LOG_SECTION_VFS
CVFSHandler* vfsHandler = nullptr;
CVFSHandler::CVFSHandler()
{
LOG_L(L_DEBUG, "[%s]", __func__);
}
CVFSHandler::~CVFSHandler()
{
LOG_L(L_INFO, "[%s] #archives=%lu", __func__, (long unsigned) archives.size());
DeleteArchives();
}
CVFSHandler::Section CVFSHandler::GetModeSection(char mode)
{
switch (mode) {
case SPRING_VFS_MOD[0]: return Section::Mod;
case SPRING_VFS_MAP[0]: return Section::Map;
case SPRING_VFS_BASE[0]: return Section::Base;
case SPRING_VFS_MENU[0]: return Section::Menu;
default: return Section::Error;
}
}
CVFSHandler::Section CVFSHandler::GetModTypeSection(int mt)
{
switch (mt) {
case modtype::hidden: return Section::Mod;
case modtype::primary: return Section::Mod;
case modtype::map: return Section::Map;
case modtype::base: return Section::Base;
case modtype::menu: return Section::Menu;
default: return Section::Error;
}
}
static const std::string GetArchivePath(const std::string& name){
if (name.empty())
return name;
const std::string& filename = archiveScanner->ArchiveFromName(name);
if (filename == name)
return "";
const std::string& path = archiveScanner->GetArchivePath(filename);
return path + filename;
}
bool CVFSHandler::AddArchive(const std::string& archiveName, bool overwrite)
{
LOG_L(L_DEBUG, "[VFSH::%s(arName=\"%s\", overwrite=%s)]", __func__, archiveName.c_str(), overwrite ? "true" : "false");
const CArchiveScanner::ArchiveData& ad = archiveScanner->GetArchiveData(archiveName);
const Section section = GetModTypeSection(ad.GetModType());
const std::string& archivePath = GetArchivePath(archiveName);
assert(!ad.IsEmpty());
assert(section < Section::Count);
assert(!archivePath.empty());
IArchive* ar = archives[archivePath];
if (ar == nullptr) {
if ((ar = archiveLoader.OpenArchive(archivePath)) == nullptr) {
LOG_L(L_ERROR, "[VFSH::%s] failed to open archive '%s' (path '%s', type %d).", __func__, archiveName.c_str(), archivePath.c_str(), ad.GetModType());
return false;
}
archives[archivePath] = ar;
}
for (unsigned fid = 0; fid != ar->NumFiles(); ++fid) {
std::string name;
int size;
ar->FileInfo(fid, name, size);
StringToLowerInPlace(name);
if (!overwrite) {
if (files[section].find(name) != files[section].end()) {
LOG_L(L_DEBUG, "[VFSH::%s] skipping \"%s\", exists", __func__, name.c_str());
continue;
}
LOG_L(L_DEBUG, "[VFSH::%s] adding \"%s\", does not exist", __func__, name.c_str());
} else {
LOG_L(L_DEBUG, "[VFSH::%s] overriding \"%s\"", __func__, name.c_str());
}
FileData d;
d.ar = ar;
d.size = size;
files[section][name] = d;
}
return true;
}
bool CVFSHandler::AddArchiveWithDeps(const std::string& archiveName, bool overwrite)
{
const std::vector<std::string> ars = std::move(archiveScanner->GetAllArchivesUsedBy(archiveName));
if (ars.empty())
throw content_error("Could not find any archives for '" + archiveName + "'.");
for (auto it = ars.cbegin(); it != ars.cend(); ++it) {
if (!AddArchive(*it, overwrite))
throw content_error("Failed loading archive '" + *it + "', dependency of '" + archiveName + "'.");
}
return true;
}
bool CVFSHandler::RemoveArchive(const std::string& archiveName)
{
const CArchiveScanner::ArchiveData& ad = archiveScanner->GetArchiveData(archiveName);
const Section section = GetModTypeSection(ad.GetModType());
const std::string& archivePath = GetArchivePath(archiveName);
assert(!ad.IsEmpty());
assert(section < Section::Count);
assert(!archivePath.empty());
LOG_L(L_DEBUG, "[VFHS::%s(archiveName=\"%s\")]", __func__, archivePath.c_str());
const auto it = archives.find(archivePath);
if (it == archives.end())
return true;
IArchive* ar = it->second;
// archive is not loaded
if (ar == nullptr)
return true;
// remove the files loaded from the archive-to-remove
for (auto f = files[section].begin(); f != files[section].end(); ) {
if (f->second.ar == ar) {
LOG_L(L_DEBUG, "[VFHS::%s] removing \"%s\"", __func__, f->first.c_str());
f = files[section].erase(f);
} else {
++f;
}
}
delete ar;
archives.erase(archivePath);
return true;
}
void CVFSHandler::DeleteArchives()
{
LOG_L(L_INFO, "[VFSH::%s]", __func__);
for (const auto& p: archives) {
LOG_L(L_INFO, "\tarchive=%s (%p)", (p.first).c_str(), p.second);
delete p.second;
}
archives.clear();
for (auto& fdm: files) {
fdm.clear();
}
}
std::string CVFSHandler::GetNormalizedPath(const std::string& rawPath)
{
std::string path = std::move(StringToLower(rawPath));
FileSystem::ForwardSlashes(path);
return path;
}
const CVFSHandler::FileData* CVFSHandler::GetFileData(const std::string& normalizedFilePath, Section section)
{
assert(section < Section::Count);
const auto fi = files[section].find(normalizedFilePath);
if (fi != files[section].end())
return &(fi->second);
return nullptr;
}
bool CVFSHandler::LoadFile(const std::string& filePath, std::vector<std::uint8_t>& buffer, Section section)
{
assert(section < Section::Count);
LOG_L(L_DEBUG, "[VFSH::%s(filePath=\"%s\", )]", __func__, filePath.c_str());
const std::string& normalizedPath = GetNormalizedPath(filePath);
const FileData* fileData = GetFileData(normalizedPath, section);
if (fileData == nullptr) {
LOG_L(L_DEBUG, "[VFHS::%s] file \"%s\" does not exist in VFS", __func__, filePath.c_str());
return false;
}
if (!fileData->ar->GetFile(normalizedPath, buffer)) {
LOG_L(L_DEBUG, "[VFHS::%s] file \"%s\" does not exist in archive", __func__, filePath.c_str());
return false;
}
return true;
}
bool CVFSHandler::FileExists(const std::string& filePath, Section section)
{
assert(section < Section::Count);
LOG_L(L_DEBUG, "[VFSH::%s(filePath=\"%s\", )]", __func__, filePath.c_str());
const std::string& normalizedPath = GetNormalizedPath(filePath);
const FileData* fileData = GetFileData(normalizedPath, section);
// the file does not exist in the VFS
if (fileData == nullptr)
return false;
// the file does not exist in the archive
if (!fileData->ar->FileExists(normalizedPath))
return false;
return true;
}
std::vector<std::string> CVFSHandler::GetFilesInDir(const std::string& rawDir, Section section)
{
assert(section < Section::Count);
LOG_L(L_DEBUG, "[VFSH::%s(rawDir=\"%s\")]", __func__, rawDir.c_str());
std::vector<std::string> ret;
std::string dir = GetNormalizedPath(rawDir);
auto filesStart = files[section].begin();
auto filesEnd = files[section].end();
// Non-empty directories to look in should have a trailing backslash
if (!dir.empty()) {
if (dir.back() != '/')
dir += "/";
// limit the iterator range; turn '/' into '0' for filesEnd
filesStart = files[section].lower_bound(dir); dir.back() += 1;
filesEnd = files[section].upper_bound(dir); dir.back() -= 1;
}
ret.reserve(std::distance(filesStart, filesEnd));
for (; filesStart != filesEnd; ++filesStart) {
const std::string& path = FileSystem::GetDirectory(filesStart->first);
// Check if this file starts with the dir path
if (path.compare(0, dir.length(), dir) != 0)
continue;
// Strip pathname
const std::string& name = filesStart->first.substr(dir.length());
// Do not return files in subfolders
if ((name.find('/') != std::string::npos) || (name.find('\\') != std::string::npos))
continue;
ret.push_back(name);
LOG_L(L_DEBUG, "\t%s", name.c_str());
}
return ret;
}
std::vector<std::string> CVFSHandler::GetDirsInDir(const std::string& rawDir, Section section)
{
assert(section < Section::Count);
LOG_L(L_DEBUG, "[VFSH::%s(rawDir=\"%s\")]", __func__, rawDir.c_str());
std::vector<std::string> ret;
std::set<std::string> dirs;
std::string dir = GetNormalizedPath(rawDir);
auto filesStart = files[section].begin();
auto filesEnd = files[section].end();
// Non-empty directories to look in should have a trailing backslash
if (!dir.empty()) {
if (dir.back() != '/')
dir += "/";
// limit the iterator range (as in GetFilesInDir)
filesStart = files[section].lower_bound(dir); dir.back() += 1;
filesEnd = files[section].upper_bound(dir); dir.back() -= 1;
}
ret.reserve(std::distance(filesStart, filesEnd));
for (; filesStart != filesEnd; ++filesStart) {
const std::string& path = FileSystem::GetDirectory(filesStart->first);
// Test to see if this file start with the dir path
if (path.compare(0, dir.length(), dir) != 0)
continue;
// Strip pathname
const std::string& name = filesStart->first.substr(dir.length());
const std::string::size_type slash = name.find_first_of("/\\");
if (slash != std::string::npos) {
dirs.insert(name.substr(0, slash + 1));
}
}
for (auto it = dirs.cbegin(); it != dirs.cend(); ++it) {
ret.push_back(*it);
LOG_L(L_DEBUG, "\t%s", it->c_str());
}
return ret;
}
|