/
ArchivedMapResource.cpp
81 lines (63 loc) · 1.93 KB
/
ArchivedMapResource.cpp
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
#include "ArchivedMapResource.h"
#include "i18n.h"
#include "ifilesystem.h"
namespace map
{
ArchivedMapResource::ArchivedMapResource(const std::string& archivePath, const std::string& filePathWithinArchive) :
MapResource(filePathWithinArchive),
_archivePath(archivePath),
_filePathWithinArchive(filePathWithinArchive)
{}
bool ArchivedMapResource::isReadOnly()
{
return true;
}
void ArchivedMapResource::save(const MapFormatPtr& mapFormat)
{
assert(false);
rError() << "ArchivedMapResources cannot be saved." << std::endl;
}
stream::MapResourceStream::Ptr ArchivedMapResource::openMapfileStream()
{
ensureArchiveOpened();
return openFileInArchive(_filePathWithinArchive);
}
stream::MapResourceStream::Ptr ArchivedMapResource::openInfofileStream()
{
ensureArchiveOpened();
try
{
auto infoFilename = _filePathWithinArchive.substr(0, _filePathWithinArchive.rfind('.'));
infoFilename += GetInfoFileExtension();
return openFileInArchive(infoFilename);
}
catch (const OperationException& ex)
{
// Info file load file does not stop us, just issue a warning
rWarning() << ex.what() << std::endl;
return stream::MapResourceStream::Ptr();
}
}
stream::MapResourceStream::Ptr ArchivedMapResource::openFileInArchive(const std::string& filePathWithinArchive)
{
assert(_archive);
auto archiveFile = _archive->openTextFile(filePathWithinArchive);
if (!archiveFile)
{
throw OperationException(fmt::format(_("Could not open file in archive: {0}"), _archivePath));
}
return stream::MapResourceStream::OpenFromArchiveFile(archiveFile);
}
void ArchivedMapResource::ensureArchiveOpened()
{
if (_archive)
{
return;
}
_archive = GlobalFileSystem().openArchiveInAbsolutePath(_archivePath);
if (!_archive)
{
throw OperationException(fmt::format(_("Could not open archive: {0}"), _archivePath));
}
}
}