OpenTTD Source 20261112-master-gbcbec5e63f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef BASE_MEDIA_BASE_H
11#define BASE_MEDIA_BASE_H
12
13#include "debug_type.h"
14#include "fileio_func.h"
15#include "string_type.h"
16#include "textfile_type.h"
17#include "textfile_gui.h"
18#include "3rdparty/md5/md5.h"
19#include <unordered_map>
20
21struct IniFile;
22struct IniGroup;
23struct IniItem;
24struct ContentInfo;
25
27struct MD5File {
29 enum class ChecksumResult : uint8_t {
34 };
35
36 std::string filename;
37 MD5Hash hash;
38 std::string missing_warning;
40
41 ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
42};
43
45template <class T> struct BaseSetTraits;
46
51template <class T>
52struct BaseSet {
54 virtual ~BaseSet() = default;
55
57 using TranslatedStrings = std::unordered_map<std::string, std::string, StringHash, std::equal_to<>>;
58
60 static constexpr size_t NUM_FILES = BaseSetTraits<T>::num_files;
61
64
66 static constexpr std::string_view SET_TYPE = BaseSetTraits<T>::set_type;
67
68 std::string name;
69 std::string url;
71 uint32_t shortname = 0;
72 std::vector<uint32_t> version;
73 bool fallback = false;
74
75 std::array<MD5File, BaseSet<T>::NUM_FILES> files{};
76 uint found_files = 0;
77 uint valid_files = 0;
78
83 int GetNumMissing() const
84 {
85 return BaseSet<T>::NUM_FILES - this->found_files;
86 }
87
93 int GetNumInvalid() const
94 {
95 return BaseSet<T>::NUM_FILES - this->valid_files;
96 }
97
98 void LogError(std::string_view full_filename, std::string_view detail, Severity severity = {}) const;
99 const IniItem *GetMandatoryItem(std::string_view full_filename, const IniGroup &group, std::string_view name) const;
100
101 bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename = true);
102
107 void CopyCompatibleConfig([[maybe_unused]] const T &src) {}
108
117 const std::string &GetDescription(std::string_view isocode) const
118 {
119 if (!isocode.empty()) {
120 /* First the full ISO code */
121 auto desc = this->description.find(isocode);
122 if (desc != this->description.end()) return desc->second;
123
124 /* Then the first two characters */
125 desc = this->description.find(isocode.substr(0, 2));
126 if (desc != this->description.end()) return desc->second;
127 }
128 /* Then fall back */
129 return this->description.at(std::string{});
130 }
131
142 {
143 return file->CheckMD5(subdir, SIZE_MAX);
144 }
145
151 std::optional<std::string> GetTextfile(TextfileType type) const
152 {
153 for (const auto &file : this->files) {
154 auto textfile = ::GetTextfile(type, Subdirectory::Baseset, file.filename);
155 if (textfile.has_value()) {
156 return textfile;
157 }
158 }
159 return std::nullopt;
160 }
161
166 static std::span<const std::string_view> GetFilenames();
167};
168
173template <class Tbase_set>
175protected:
176 static inline std::vector<std::unique_ptr<Tbase_set>> available_sets;
177 static inline std::vector<std::unique_ptr<Tbase_set>> duplicate_sets;
178 static inline const Tbase_set *used_set;
179
180 bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
181
186 static std::string_view GetExtension();
187
192 static std::span<const std::unique_ptr<Tbase_set>> GetDuplicateSets() { return BaseMedia<Tbase_set>::duplicate_sets; }
193public:
199 static bool DetermineBestSet();
200
205 static uint FindSets()
206 {
208 /* Searching in tars is only done in the old "data" directories basesets. */
209 uint num = fs.Scan(GetExtension(), Tbase_set::SEARCH_IN_TARS ? Subdirectory::OldData : Subdirectory::OldGm, Tbase_set::SEARCH_IN_TARS);
210 return num + fs.Scan(GetExtension(), Subdirectory::Baseset, Tbase_set::SEARCH_IN_TARS);
211 }
212
217 static std::span<const std::unique_ptr<Tbase_set>> GetAvailableSets() { return BaseMedia<Tbase_set>::available_sets; }
218
219 static bool SetSet(const Tbase_set *set);
220 static bool SetSetByName(const std::string &name);
221 static bool SetSetByShortname(uint32_t shortname);
222 static void GetSetsList(std::back_insert_iterator<std::string> &output_iterator);
223 static int GetNumSets();
224 static int GetIndexOfUsedSet();
225 static const Tbase_set *GetSet(int index);
226 static const Tbase_set *GetUsedSet();
227
234 static bool HasSet(const ContentInfo &ci, bool md5sum);
235};
236
244template <class Tbase_set>
245std::optional<std::string_view> TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, std::span<const std::unique_ptr<Tbase_set>> sets);
246
247#endif /* BASE_MEDIA_BASE_H */
std::optional< std::string_view > TryGetBaseSetFile(const ContentInfo &ci, bool md5sum, std::span< const std::unique_ptr< Tbase_set > > sets)
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 std::vector< std::unique_ptr< Tbase_set > > available_sets
All available sets.
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 std::span< const std::unique_ptr< Tbase_set > > GetAvailableSets()
Return the available 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 std::vector< std::unique_ptr< Tbase_set > > duplicate_sets
All sets that aren't available, but needed for not downloading base sets when a newer version than th...
static const Tbase_set * GetSet(int index)
Get the base set at a specified index.
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 bool SetSetByName(const std::string &name)
Set the set to be used.
static std::span< const std::unique_ptr< Tbase_set > > GetDuplicateSets()
Return the duplicate sets.
static bool SetSet(const Tbase_set *set)
Set the set to be used.
static std::string_view GetExtension()
Get the extension that is used to identify this set.
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:1169
Types related to debugging.
Severity
Debug message severity levels.
Definition debug_type.h:14
#define T
Climate temperate.
Definition engines.h:91
Functions for standard in/out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition fileio_type.h:87
@ Baseset
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:95
@ OldData
Old subdirectory for the data.
Definition fileio_type.h:94
@ OldGm
Old subdirectory for the music.
Definition fileio_type.h:93
Types for strings.
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.
void LogError(std::string_view full_filename, std::string_view detail, Severity severity={}) const
Log error from reading basesets.
uint valid_files
Number of the files that could be found and are valid.
const IniItem * GetMandatoryItem(std::string_view full_filename, const IniGroup &group, std::string_view name) const
Try to read a single piece of metadata and return nullptr if it doesn't exist.
std::string url
URL for information about the base set.
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.
void CopyCompatibleConfig(const T &src)
Copy settings from the given set into this set when they are compatible.
static std::span< const std::string_view > GetFilenames()
Get the internal names of the files in this set.
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.
int GetNumMissing() const
Get the number of missing files.
std::vector< uint32_t > version
The version of this base set.
virtual ~BaseSet()=default
Ensure the destructor of the sub classes are called as well.
std::unordered_map< std::string, std::string, StringHash, std::equal_to<> > TranslatedStrings
Mapping of translations: language -> string.
uint32_t shortname
Four letter short variant of the name.
int GetNumInvalid() const
Get the number of invalid files.
const std::string & GetDescription(std::string_view isocode) const
Get the description for the given ISO code.
Container for all important information about a piece of content.
Ini file that supports both loading and saving.
Definition ini_type.h:87
A group within an ini file.
Definition ini_type.h:34
A single "line" in an ini file.
Definition ini_type.h:23
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:448
ChecksumResult
The result of a checksum check.
@ NoFile
The file did not exist.
@ Mismatch
The file did exist, just the md5 checksum did not match.
@ Match
The file did exist and the md5 checksum did match.
@ 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.