Horizon Official Technical Documentation
MapCache.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_LIBRARIES_MAPCACHE_HPP
31#define HORIZON_LIBRARIES_MAPCACHE_HPP
32
33#include "Libraries/GRF/GRF.hpp"
34
35#include <cstdint>
36#include <map>
37#include <boost/optional.hpp>
38#include <unordered_map>
39
40#pragma pack(push, 1)
42{
43 uint32_t getFileSize() { return file_size; }
44 void setFileSize(uint32_t size) { file_size = size; }
45
46 uint16_t getMapCount() { return map_count; }
47 void setMapCount(uint16_t count) { map_count = count; }
48
49 uint16_t getVersion() { return version; }
50 void setVersion(uint16_t v) { version = v; }
51
52 uint32_t getChecksum() { return checksum; }
53 void setChecksum(uint32_t c) { checksum = c; }
54
55private:
56 uint32_t file_size{0};
57 uint16_t map_count{0};
58 uint32_t version{0};
59 uint32_t checksum{0};
60};
61
63{
64 char name[12];
65 int16_t total_x{0};
66 int16_t total_y{0};
67 uint32_t length{0};
68};
69#pragma pack(pop)
70
72{
73 const std::vector<uint8_t> &getCells() { return cells; }
74 std::string name() { return info.name; }
75 uint16_t width() { return info.total_x; }
76 uint16_t height() { return info.total_y; }
77 uint32_t size() { return info.length; }
78
80 std::vector<uint8_t> cells;
81};
82
84{
86 void setHeader(mapcache_header &header) { _header = header; }
87
88 void addMap(map_data &data) { maps.insert(std::make_pair(data.info.name, data)); }
89 void removeMap(std::string &name)
90 {
91 auto it = maps.find(name);
92
93 if (it != maps.end())
94 maps.erase(it);
95 }
96
97 boost::optional<map_data> getMap(std::string name)
98 {
99 auto it = maps.find(name);
100
101 if (it != maps.end())
102 return boost::optional<map_data>(it->second);
103
104 return boost::optional<map_data>();
105 }
106
107 std::size_t getMapCount() { return maps.size(); }
108
109 std::map<std::string, map_data> maps;
110
111private:
113};
114
116{
125
127{
131};
132
134{
140
142{
150};
151
153{
156};
157
158namespace Horizon
159{
160namespace Libraries
161{
162// Used internally, this structure contains the physical map cells
164{
165public:
166 MapCache();
167 ~MapCache();
168
170 bool Exists();
171
173
174 int BuildInternalCache();
175 bool BuildExternalCache();
178 bool AppendToCache(std::string const &name);
179
180 bool GetMapFromGRF(GRF &grf, std::string const &name);
181
182 bool ParseGRFReadResult(GRF &grf, std::string const &filename, grf_read_error_type error);
183
184 /* */
186 /* */
188 /* */
189 std::pair<uint8_t, grf_load_result_type> LoadGRFs();
190
191 void PrintCacheForMap(std::string const &map_name);
192
193 /* GRF Library */
194 GRF &getGRF(uint8_t id) { return _grfs.at(id); }
195 void addGRF(uint8_t id, GRF &grf) { _grfs[id] = grf; }
196
197 /* Map Cache Path */
198 boost::filesystem::path &getMapCachePath() { return _map_cache_path; }
199 void setMapCachePath(std::string const &file) { _map_cache_path = file; }
200
201 /* Compression Level */
203 void setCompressionLevel(int level) { _compression_level = level; }
204
205 /* Map List Config Path */
206 const boost::filesystem::path &getMapListPath() const { return _map_list_path; }
207 void setMapListPath(std::string const &path) { _map_list_path = path; }
208
209 /* GRF List Config Path */
210 const boost::filesystem::path &getGRFListPath() const { return _grf_list_path; }
211 void setGRFListPath(std::string const &path) { _grf_list_path = path; }
212
213 /* Map List */
214 void addToMapList(std::string const &map) { _map_list.push_back(map); }
215
216 /* GRF Path */
217 const boost::filesystem::path &getGRFPath(uint8_t id) { return _grfs[id].getGRFPath(); }
218 void setGRFPath(uint8_t id, std::string const &path) { _grfs[id].setGRFPath(path); }
219
220 /* Resource Path */
221 const boost::filesystem::path &getResourcePath() { return _resource_path; }
222 void setResourcePath(std::string const &path) { _resource_path = path; }
223
224 /* Verbose */
225 void setVerbose() { _verbose = true; }
226 void unsetVerbose() { _verbose = false; }
227 bool getVerbose() { return _verbose; }
228
229 /* MCache */
230 std::shared_ptr<map_cache> getMCache() { return m_cache; }
231
232 const std::unordered_map<uint8_t, GRF> &getGRFs() { return _grfs; }
233private:
234 boost::filesystem::path _map_list_path;
235 boost::filesystem::path _grf_list_path, _resource_path;
236 boost::filesystem::path _map_cache_path;
237 std::unordered_map<uint8_t, GRF> _grfs;
239 std::map<std::string, map_data> _map_cache_data;
240 std::vector<std::string> _map_list;
241 std::shared_ptr<map_cache> m_cache;
242 bool _verbose{false};
243};
244}
245}
246
247#endif // HORIZON_LIBRARIES_MAPCACHE_HPP
grf_read_error_type
Definition: GRF.hpp:66
mcache_grf_config_error_type
Definition: MapCache.hpp:134
@ MCACHE_GRF_CONF_OK
Definition: MapCache.hpp:135
@ MCACHE_GRF_CONF_INVALID_VALUE_TYPE
Definition: MapCache.hpp:138
@ MCACHE_GRF_CONF_INVALID_FILE
Definition: MapCache.hpp:136
@ MCACHE_GRF_CONF_PARSE_ERROR
Definition: MapCache.hpp:137
mcache_map_cell_type
Definition: MapCache.hpp:153
@ MAP_CELL_WALKABLE_UNDER_WATER
Definition: MapCache.hpp:155
@ MAP_CELL_WALKABLE
Definition: MapCache.hpp:154
mcache_import_error_type
Definition: MapCache.hpp:142
@ MCACHE_IMPORT_INVALID_CHECKSUM
Definition: MapCache.hpp:146
@ MCACHE_IMPORT_NONEXISTENT_FILE
Definition: MapCache.hpp:144
@ MCACHE_IMPORT_OK
Definition: MapCache.hpp:143
@ MCACHE_IMPORT_READ_ERROR
Definition: MapCache.hpp:145
@ MCACHE_IMPORT_DECOMPRESS_ERROR
Definition: MapCache.hpp:147
@ MCACHE_IMPORT_MAPINFO_ERROR
Definition: MapCache.hpp:148
@ MCACHE_IMPORT_CELLINFO_ERROR
Definition: MapCache.hpp:149
mcache_error_type
Definition: MapCache.hpp:116
@ MCACHE_INVALID_OUTPUT_PATH
Definition: MapCache.hpp:120
@ MCACHE_INVALID_GRF_PATH
Definition: MapCache.hpp:118
@ MCACHE_CONFIG_READ_ERROR
Definition: MapCache.hpp:121
@ MCACHE_INVALID_CONFIG_PATH
Definition: MapCache.hpp:119
@ MCACHE_OK
Definition: MapCache.hpp:117
@ MCACHE_GRF_LOAD_ERROR
Definition: MapCache.hpp:123
@ MCACHE_GRF_CONFIG_READ_ERROR
Definition: MapCache.hpp:122
mcache_config_error_type
Definition: MapCache.hpp:127
@ MCACHE_CONFIG_PARSE_ERROR
Definition: MapCache.hpp:129
@ MCACHE_CONFIG_INVALID_VALUE_TYPE
Definition: MapCache.hpp:130
@ MCACHE_CONFIG_OK
Definition: MapCache.hpp:128
Definition: GRF.hpp:86
Definition: MapCache.hpp:164
const std::unordered_map< uint8_t, GRF > & getGRFs()
Definition: MapCache.hpp:232
bool getVerbose()
Definition: MapCache.hpp:227
void unsetVerbose()
Definition: MapCache.hpp:226
boost::filesystem::path _resource_path
Definition: MapCache.hpp:235
int getCompressionLevel()
Definition: MapCache.hpp:202
boost::filesystem::path _map_list_path
Definition: MapCache.hpp:234
int _compression_level
Definition: MapCache.hpp:238
std::shared_ptr< map_cache > m_cache
Definition: MapCache.hpp:241
const boost::filesystem::path & getGRFListPath() const
Definition: MapCache.hpp:210
void PrintCacheForMap(std::string const &map_name)
Definition: MapCache.cpp:304
void setVerbose()
Definition: MapCache.hpp:225
void setResourcePath(std::string const &path)
Definition: MapCache.hpp:222
bool AppendToCache(std::string const &name)
bool GetMapFromGRF(GRF &grf, std::string const &name)
Reads a map from GRF's GAT and RSW files.
Definition: MapCache.cpp:423
mcache_error_type initialize()
Definition: MapCache.cpp:146
mcache_config_error_type ReadMapListConfig()
Definition: MapCache.cpp:172
std::unordered_map< uint8_t, GRF > _grfs
Definition: MapCache.hpp:237
bool BuildExternalCache()
Definition: MapCache.cpp:349
void setMapListPath(std::string const &path)
Definition: MapCache.hpp:207
void setMapCachePath(std::string const &file)
Definition: MapCache.hpp:199
boost::filesystem::path & getMapCachePath()
Definition: MapCache.hpp:198
GRF & getGRF(uint8_t id)
Definition: MapCache.hpp:194
MapCache()
Definition: MapCache.cpp:46
void setCompressionLevel(int level)
Definition: MapCache.hpp:203
const boost::filesystem::path & getResourcePath()
Definition: MapCache.hpp:221
int BuildInternalCache()
Definition: MapCache.cpp:284
~MapCache()
Definition: MapCache.cpp:52
void addGRF(uint8_t id, GRF &grf)
Definition: MapCache.hpp:195
std::pair< uint8_t, grf_load_result_type > LoadGRFs()
Definition: MapCache.cpp:273
const boost::filesystem::path & getGRFPath(uint8_t id)
Definition: MapCache.hpp:217
bool ParseGRFReadResult(GRF &grf, std::string const &filename, grf_read_error_type error)
Definition: MapCache.cpp:395
std::map< std::string, map_data > _map_cache_data
Definition: MapCache.hpp:239
void setGRFPath(uint8_t id, std::string const &path)
Definition: MapCache.hpp:218
const boost::filesystem::path & getMapListPath() const
Definition: MapCache.hpp:206
mcache_grf_config_error_type ReadGRFListConfig()
Definition: MapCache.cpp:221
std::vector< std::string > _map_list
Definition: MapCache.hpp:240
std::shared_ptr< map_cache > getMCache()
Definition: MapCache.hpp:230
boost::filesystem::path _map_cache_path
Definition: MapCache.hpp:236
bool Exists()
Definition: MapCache.cpp:166
void setGRFListPath(std::string const &path)
Definition: MapCache.hpp:211
mcache_import_error_type ImportFromCacheFile()
Definition: MapCache.cpp:56
boost::filesystem::path _grf_list_path
Definition: MapCache.hpp:235
bool _verbose
Definition: MapCache.hpp:242
void addToMapList(std::string const &map)
Definition: MapCache.hpp:214
size_t count(GridTypeListContainer< SPECIFIC_TYPE > const &elements, SPECIFIC_TYPE *)
Definition: GridReferenceContainer.hpp:100
Definition: Element.hpp:7
Definition: MapCache.hpp:84
void removeMap(std::string &name)
Definition: MapCache.hpp:89
std::map< std::string, map_data > maps
Definition: MapCache.hpp:109
std::size_t getMapCount()
Definition: MapCache.hpp:107
mapcache_header & getHeader()
Definition: MapCache.hpp:85
boost::optional< map_data > getMap(std::string name)
Definition: MapCache.hpp:97
void addMap(map_data &data)
Definition: MapCache.hpp:88
mapcache_header _header
Definition: MapCache.hpp:112
void setHeader(mapcache_header &header)
Definition: MapCache.hpp:86
Definition: MapCache.hpp:72
uint16_t height()
Definition: MapCache.hpp:76
std::vector< uint8_t > cells
Definition: MapCache.hpp:80
std::string name()
Definition: MapCache.hpp:74
uint16_t width()
Definition: MapCache.hpp:75
map_info info
Definition: MapCache.hpp:79
uint32_t size()
Definition: MapCache.hpp:77
const std::vector< uint8_t > & getCells()
Definition: MapCache.hpp:73
Definition: MapCache.hpp:63
int16_t total_x
Definition: MapCache.hpp:65
char name[12]
Definition: MapCache.hpp:64
uint32_t length
Definition: MapCache.hpp:67
int16_t total_y
Definition: MapCache.hpp:66
Definition: MapCache.hpp:42
void setChecksum(uint32_t c)
Definition: MapCache.hpp:53
uint32_t getFileSize()
Definition: MapCache.hpp:43
uint16_t getVersion()
Definition: MapCache.hpp:49
uint32_t checksum
Definition: MapCache.hpp:59
uint32_t file_size
Definition: MapCache.hpp:56
void setMapCount(uint16_t count)
Definition: MapCache.hpp:47
uint32_t getChecksum()
Definition: MapCache.hpp:52
uint32_t version
Definition: MapCache.hpp:58
void setVersion(uint16_t v)
Definition: MapCache.hpp:50
uint16_t getMapCount()
Definition: MapCache.hpp:46
void setFileSize(uint32_t size)
Definition: MapCache.hpp:44
uint16_t map_count
Definition: MapCache.hpp:57