| 1 | /*************************************************************************** |
| 2 | * fheroes2: https://github.com/ihhub/fheroes2 * |
| 3 | * Copyright (C) 2021 - 2024 * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 2 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the * |
| 17 | * Free Software Foundation, Inc., * |
| 18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <cstdint> |
| 24 | #include <functional> |
| 25 | #include <map> |
| 26 | #include <set> |
| 27 | #include <string> |
| 28 | #include <utility> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "serialize.h" |
| 32 | |
| 33 | namespace fheroes2 |
| 34 | { |
| 35 | class Sprite; |
| 36 | |
| 37 | // Heroes 2 Data (H2D) file format used for storing files needed for the project. This format is not a part of original HoMM II. |
| 38 | class H2DReader |
| 39 | { |
| 40 | public: |
| 41 | // Returns true if file opening is successful. |
| 42 | bool open( const std::string & path ); |
| 43 | |
| 44 | // Returns non-empty vector if requested file exists. |
| 45 | std::vector<uint8_t> getFile( const std::string & fileName ); |
| 46 | |
| 47 | std::set<std::string, std::less<>> getAllFileNames() const; |
| 48 | |
| 49 | private: |
| 50 | // Relationship between file name in non-capital letters and its offset from the start of the archive. |
| 51 | std::map<std::string, std::pair<uint32_t, uint32_t>, std::less<>> _fileNameAndOffset; |
| 52 | |
| 53 | // Stream for reading h2d file. |
| 54 | StreamFile _fileStream; |
| 55 | }; |
| 56 | |
| 57 | // This class is not designed to be performance optimized as it will be used very rarely and out of game running session. |
| 58 | class H2DWriter |
| 59 | { |
| 60 | public: |
| 61 | // Returns true if file opening is successful. |
| 62 | bool write( const std::string & path ) const; |
| 63 | |
| 64 | bool add( const std::string & name, const std::vector<uint8_t> & data ); |
| 65 | |
| 66 | // Add all entries from a H2D reader. |
| 67 | bool add( H2DReader & reader ); |
| 68 | |
| 69 | private: |
| 70 | std::map<std::string, std::vector<uint8_t>, std::less<>> _fileData; |
| 71 | }; |
| 72 | |
| 73 | bool readImageFromH2D( H2DReader & reader, const std::string & name, Sprite & image ); |
| 74 | |
| 75 | bool writeImageToH2D( H2DWriter & writer, const std::string & name, const Sprite & image ); |
| 76 | } |