OpenTTD Source 15.0-beta2
base_media_base.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#ifndef BASE_MEDIA_BASE_H
11#define BASE_MEDIA_BASE_H
12
13#include "fileio_func.h"
14#include "textfile_type.h"
15#include "textfile_gui.h"
16#include "3rdparty/md5/md5.h"
17#include <unordered_map>
18
19/* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
20struct IniFile;
21struct ContentInfo;
22
24struct MD5File {
32
33 std::string filename;
34 MD5Hash hash;
35 std::string missing_warning;
37
38 ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
39};
40
42template <class T> struct BaseSetTraits;
43
48template <class T>
49struct BaseSet {
50 typedef std::unordered_map<std::string, std::string> TranslatedStrings;
51
53 static constexpr size_t NUM_FILES = BaseSetTraits<T>::num_files;
54
57
59 static constexpr std::string_view SET_TYPE = BaseSetTraits<T>::set_type;
60
61 std::string name;
62 std::string url;
63 TranslatedStrings description;
64 uint32_t shortname = 0;
65 uint32_t version = 0;
66 bool fallback = false;
67
68 std::array<MD5File, BaseSet<T>::NUM_FILES> files{};
69 uint found_files = 0;
70 uint valid_files = 0;
71
72 T *next = nullptr;
73
76 {
77 delete this->next;
78 }
79
84 int GetNumMissing() const
85 {
87 }
88
94 int GetNumInvalid() const
95 {
97 }
98
99 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename = true);
100 void CopyCompatibleConfig([[maybe_unused]] const T &src) {}
101
110 const std::string &GetDescription(const std::string &isocode) const
111 {
112 if (!isocode.empty()) {
113 /* First the full ISO code */
114 auto desc = this->description.find(isocode);
115 if (desc != this->description.end()) return desc->second;
116
117 /* Then the first two characters */
118 desc = this->description.find(isocode.substr(0, 2));
119 if (desc != this->description.end()) return desc->second;
120 }
121 /* Then fall back */
122 return this->description.at(std::string{});
123 }
124
135 {
136 return file->CheckMD5(subdir, SIZE_MAX);
137 }
138
144 std::optional<std::string> GetTextfile(TextfileType type) const
145 {
146 for (const auto &file : this->files) {
147 auto textfile = ::GetTextfile(type, BASESET_DIR, file.filename);
148 if (textfile.has_value()) {
149 return textfile;
150 }
151 }
152 return std::nullopt;
153 }
154
159 static std::span<const std::string_view> GetFilenames();
160};
161
166template <class Tbase_set>
168protected:
169 static inline Tbase_set *available_sets = nullptr;
170 static inline Tbase_set *duplicate_sets = nullptr;
171 static inline const Tbase_set *used_set = nullptr;
172
173 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
174
179 static const char *GetExtension();
180public:
186 static bool DetermineBestSet();
187
189 static uint FindSets()
190 {
192 /* Searching in tars is only done in the old "data" directories basesets. */
193 uint num = fs.Scan(GetExtension(), Tbase_set::SEARCH_IN_TARS ? OLD_DATA_DIR : OLD_GM_DIR, Tbase_set::SEARCH_IN_TARS);
194 return num + fs.Scan(GetExtension(), BASESET_DIR, Tbase_set::SEARCH_IN_TARS);
195 }
196
197 static Tbase_set *GetAvailableSets();
198
199 static bool SetSet(const Tbase_set *set);
200 static bool SetSetByName(const std::string &name);
201 static bool SetSetByShortname(uint32_t shortname);
202 static void GetSetsList(std::back_insert_iterator<std::string> &output_iterator);
203 static int GetNumSets();
204 static int GetIndexOfUsedSet();
205 static const Tbase_set *GetSet(int index);
206 static const Tbase_set *GetUsedSet();
207
214 static bool HasSet(const ContentInfo &ci, bool md5sum);
215};
216
224template <class Tbase_set>
225const char *TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s);
226
227#endif /* BASE_MEDIA_BASE_H */
const char * TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
Base for all base media (graphics, sounds)
static const Tbase_set * GetUsedSet()
Return the used set.
static Tbase_set * duplicate_sets
All sets that aren't available, but needed for not downloading base sets when a newer version than th...
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
static bool SetSetByShortname(uint32_t shortname)
Set the set to be used.
static void GetSetsList(std::back_insert_iterator< std::string > &output_iterator)
Returns a list with the sets.
static int GetNumSets()
Count the number of available graphics sets.
static uint FindSets()
Do the scan for files.
static bool HasSet(const ContentInfo &ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
static const Tbase_set * used_set
The currently used set.
static const Tbase_set * GetSet(int index)
Get the name of the graphics set at the specified index.
static Tbase_set * GetAvailableSets()
Return the available sets.
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
static Tbase_set * available_sets
All available sets.
static bool SetSetByName(const std::string &name)
Set the set to be used.
static const char * GetExtension()
Get the extension that is used to identify this set.
static bool SetSet(const Tbase_set *set)
Set the set to be used.
Helper for scanning for files with a given name.
Definition fileio_func.h:37
uint Scan(std::string_view extension, Subdirectory sd, bool tars=true, bool recursive=true)
Scan for files with the given extension in the given search path.
Definition fileio.cpp:1113
Functions for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ OLD_DATA_DIR
Old subdirectory for the data.
@ OLD_GM_DIR
Old subdirectory for the music.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Defines the traits of a BaseSet type.
Information about a single base set.
uint found_files
Number of the files that could be found.
std::array< MD5File, BaseSet< T >::NUM_FILES > files
All files part of this set.
uint32_t version
The version of this base set.
uint valid_files
Number of the files that could be found and are valid.
std::string url
URL for information about the base set.
~BaseSet()
Free everything we allocated.
TranslatedStrings description
Description of the base set.
static constexpr size_t NUM_FILES
Number of files in this set.
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this base media.
static constexpr bool SEARCH_IN_TARS
Whether to search in the tars or not.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
static std::span< const std::string_view > GetFilenames()
Get the internal names of the files in this set.
const std::string & GetDescription(const std::string &isocode) const
Get the description for the given ISO code.
static constexpr std::string_view SET_TYPE
BaseSet type name.
std::string name
The name of the base set.
bool fallback
This set is a fallback set, i.e. it should be used only as last resort.
bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
T * next
The next base set in this list.
int GetNumMissing() const
Get the number of missing files.
uint32_t shortname
Four letter short variant of the name.
int GetNumInvalid() const
Get the number of invalid files.
Container for all important information about a piece of content.
Ini file that supports both loading and saving.
Definition ini_type.h:88
Structure holding filename and MD5 information about a single file.
std::string missing_warning
warning when this file is missing
ChecksumResult check_result
cached result of md5 check
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
Definition gfxinit.cpp:443
ChecksumResult
The result of a checksum check.
@ CR_MATCH
The file did exist and the md5 checksum did match.
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
@ CR_NO_FILE
The file did not exist.
@ CR_UNKNOWN
The file has not been checked yet.
MD5Hash hash
md5 sum of the file
std::string filename
filename
GUI functions related to textfiles.
Types related to textfiles.
TextfileType
Additional text files accompanying Tar archives.