Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9f4dae
commit 1f5a51f
Showing
4 changed files
with
99 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,85 @@ | ||
#pragma once | ||
|
||
#include "i18n.h" | ||
#include "ifilesystem.h" | ||
#include "MapResource.h" | ||
#include "stream/MapResourceStream.h" | ||
|
||
namespace map | ||
{ | ||
|
||
class ArchivedMapResource : | ||
public MapResource | ||
{ | ||
private: | ||
std::string _archivePath; | ||
std::string _filePathWithinArchive; | ||
|
||
IArchive::Ptr _archive; | ||
|
||
public: | ||
ArchivedMapResource(const std::string& archivePath, const std::string& filePathWithinArchive) : | ||
MapResource(filePathWithinArchive) | ||
MapResource(filePathWithinArchive), | ||
_archivePath(archivePath), | ||
_filePathWithinArchive(filePathWithinArchive) | ||
{} | ||
|
||
protected: | ||
virtual stream::MapResourceStream::Ptr openMapfileStream() override | ||
{ | ||
ensureArchiveOpened(); | ||
|
||
return openFileInArchive(_filePathWithinArchive); | ||
} | ||
|
||
virtual stream::MapResourceStream::Ptr openInfofileStream() override | ||
{ | ||
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(); | ||
} | ||
} | ||
|
||
private: | ||
stream::MapResourceStream::Ptr 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 ensureArchiveOpened() | ||
{ | ||
if (_archive) | ||
{ | ||
return; | ||
} | ||
|
||
_archive = GlobalFileSystem().openArchiveInAbsolutePath(_archivePath); | ||
|
||
if (!_archive) | ||
{ | ||
throw OperationException(fmt::format(_("Could not open archive: {0}"), _archivePath)); | ||
} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters