OpenTTD Source  20241108-master-g80f628063a
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 "gfx_type.h"
15 #include "textfile_type.h"
16 #include "textfile_gui.h"
17 #include "3rdparty/md5/md5.h"
18 #include <unordered_map>
19 
20 /* Forward declare these; can't do 'struct X' in functions as older GCCs barf on that */
21 struct IniFile;
22 struct ContentInfo;
23 
25 struct MD5File {
32  };
33 
34  std::string filename;
35  MD5Hash hash;
36  std::string missing_warning;
38 
39  ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const;
40 };
41 
48 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
49 struct BaseSet {
50  typedef std::unordered_map<std::string, std::string> TranslatedStrings;
51 
53  static const size_t NUM_FILES = Tnum_files;
54 
56  static const bool SEARCH_IN_TARS = Tsearch_in_tars;
57 
59  static const char * const *file_names;
60 
61  std::string name;
62  std::string url;
63  TranslatedStrings description;
64  uint32_t shortname;
65  uint32_t version;
66  bool fallback;
67 
69  uint found_files;
70  uint valid_files;
71 
72  T *next;
73 
76  {
77  delete this->next;
78  }
79 
84  int GetNumMissing() const
85  {
86  return Tnum_files - this->found_files;
87  }
88 
94  int GetNumInvalid() const
95  {
96  return Tnum_files - this->valid_files;
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 
131  std::string GetListLabel() const
132  {
133  if (this->GetNumInvalid() == 0) return this->name;
134 
135  SetDParamStr(0, this->name);
136  SetDParam(1, this->GetNumInvalid());
137  return GetString(STR_BASESET_STATUS);
138  }
139 
150  {
151  return file->CheckMD5(subdir, SIZE_MAX);
152  }
153 
159  std::optional<std::string> GetTextfile(TextfileType type) const
160  {
161  for (const auto &file : this->files) {
162  auto textfile = ::GetTextfile(type, BASESET_DIR, file.filename);
163  if (textfile.has_value()) {
164  return textfile;
165  }
166  }
167  return std::nullopt;
168  }
169 };
170 
175 template <class Tbase_set>
177 protected:
178  static Tbase_set *available_sets;
179  static Tbase_set *duplicate_sets;
180  static const Tbase_set *used_set;
181 
182  bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
183 
188  static const char *GetExtension();
189 public:
195  static bool DetermineBestSet();
196 
198  static uint FindSets()
199  {
201  /* Searching in tars is only done in the old "data" directories basesets. */
202  uint num = fs.Scan(GetExtension(), Tbase_set::SEARCH_IN_TARS ? OLD_DATA_DIR : OLD_GM_DIR, Tbase_set::SEARCH_IN_TARS);
203  return num + fs.Scan(GetExtension(), BASESET_DIR, Tbase_set::SEARCH_IN_TARS);
204  }
205 
206  static Tbase_set *GetAvailableSets();
207 
208  static bool SetSet(const Tbase_set *set);
209  static bool SetSetByName(const std::string &name);
210  static bool SetSetByShortname(uint32_t shortname);
211  static void GetSetsList(std::back_insert_iterator<std::string> &output_iterator);
212  static int GetNumSets();
213  static int GetIndexOfUsedSet();
214  static const Tbase_set *GetSet(int index);
215  static const Tbase_set *GetUsedSet();
216 
223  static bool HasSet(const ContentInfo *ci, bool md5sum);
224 };
225 
226 template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
227 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
228 template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::duplicate_sets;
229 
237 template <class Tbase_set>
238 const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s);
239 
249 };
250 
255 };
256 
257 struct GRFConfig;
258 
260 struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT, true> {
261 private:
262  mutable std::unique_ptr<GRFConfig> extra_cfg;
263 public:
266 
267  GraphicsSet();
268  ~GraphicsSet();
269 
270  bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename);
271  GRFConfig *GetExtraConfig() const { return this->extra_cfg.get(); }
273  bool IsConfigurable() const;
274  void CopyCompatibleConfig(const GraphicsSet &src);
275 
276  static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir);
277 };
278 
280 class BaseGraphics : public BaseMedia<GraphicsSet> {
281 public:
283  struct Ini {
284  std::string name;
285  uint32_t shortname;
286  uint32_t extra_version;
287  std::vector<uint32_t> extra_params;
288  };
289  static inline Ini ini_data;
290 
291 };
292 
294 struct SoundsSet : BaseSet<SoundsSet, 1, true> {
295 };
296 
298 class BaseSounds : public BaseMedia<SoundsSet> {
299 public:
301  static inline std::string ini_set;
302 
303 };
304 
306 static const uint NUM_SONGS_CLASS = 10;
308 static const uint NUM_SONG_CLASSES = 3;
311 
313 static const uint NUM_SONGS_PLAYLIST = 32;
314 
315 /* Functions to read DOS music CAT files, similar to but not quite the same as sound effect CAT files */
316 std::optional<std::string> GetMusicCatEntryName(const std::string &filename, size_t entrynum);
317 std::optional<std::vector<uint8_t>> GetMusicCatEntryData(const std::string &filename, size_t entrynum);
318 
322 };
323 
326  std::string songname;
327  uint8_t tracknr;
328  std::string filename;
330  int cat_index;
331  bool loop;
334 };
335 
337 struct MusicSet : BaseSet<MusicSet, NUM_SONGS_AVAILABLE, false> {
341  uint8_t num_available;
342 
343  bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename);
344 };
345 
347 class BaseMusic : public BaseMedia<MusicSet> {
348 public:
350  static inline std::string ini_set;
351 
352 };
353 
354 #endif /* BASE_MEDIA_BASE_H */
static const uint NUM_SONG_CLASSES
Number of classes for songs.
static const uint NUM_SONGS_AVAILABLE
Maximum number of songs in the full playlist; theme song + the classes.
static const uint NUM_SONGS_CLASS
Maximum number of songs in the 'class' playlists.
MusicTrackType
@ MTT_MPSMIDI
MPS GM driver MIDI format (contained in a CAT file)
@ MTT_STANDARDMIDI
Standard MIDI file.
GraphicsFileType
Types of graphics in the base graphics set.
@ GFT_TOYLAND
Landscape replacement sprites for toyland.
@ GFT_LOGOS
Logos, landscape icons and original terrain generator sprites.
@ MAX_GFT
We are looking for this amount of GRFs.
@ GFT_EXTRA
Extra sprites that were not part of the original sprites.
@ GFT_ARCTIC
Landscape replacement sprites for arctic.
@ GFT_TROPICAL
Landscape replacement sprites for tropical.
@ GFT_BASE
Base sprites for all climates.
static const uint NUM_SONGS_PLAYLIST
Maximum number of songs in the (custom) playlist.
BlitterType
Blitter type for base graphics sets.
@ BLT_32BPP
Base set has both 8 bpp and 32 bpp sprites.
@ BLT_8BPP
Base set has 8 bpp sprites only.
std::optional< std::string > GetMusicCatEntryName(const std::string &filename, size_t entrynum)
Read the name of a music CAT file entry.
Definition: music.cpp:28
std::optional< std::vector< uint8_t > > GetMusicCatEntryData(const std::string &filename, size_t entrynum)
Read the full data of a music CAT file entry.
Definition: music.cpp:52
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
All data/functions related with replacing the base graphics.
Base for all base media (graphics, sounds)
static bool DetermineBestSet()
Determine the graphics pack that has to be used.
Definition: gfxinit.cpp:466
static const Tbase_set * GetUsedSet()
Return the used set.
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
static Tbase_set * duplicate_sets
All sets that aren't available, but needed for not downloading base sets when a newer version than th...
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 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 const char * GetExtension()
Get the extension that is used to identify this set.
Definition: gfxinit.cpp:490
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
static Tbase_set * available_sets
All available sets.
static bool SetSetByName(const std::string &name)
Set the set to be used.
static bool SetSet(const Tbase_set *set)
Set the set to be used.
All data/functions related with replacing the base music.
static std::string ini_set
The set as saved in the config file.
All data/functions related with replacing the base sounds.
static std::string ini_set
The set as saved in the config file.
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:1114
Functions for Standard In/Out file operations.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
@ OLD_DATA_DIR
Old subdirectory for the data.
Definition: fileio_type.h:122
@ OLD_GM_DIR
Old subdirectory for the music.
Definition: fileio_type.h:121
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:123
Types related to the graphics and/or input devices.
PaletteType
Palettes OpenTTD supports.
Definition: gfx_type.h:304
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings.cpp:104
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
Definition: strings.cpp:319
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:357
Values loaded from config file.
uint32_t extra_version
version of the extra GRF
std::vector< uint32_t > extra_params
parameters for the extra GRF
uint32_t shortname
unique key for base set
Information about a single base set.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
T * next
The next base set in this list.
bool fallback
This set is a fallback set, i.e. it should be used only as last resort.
static const bool SEARCH_IN_TARS
Whether to search in the tars or not.
int GetNumInvalid() const
Get the number of invalid files.
uint found_files
Number of the files that could be found.
TranslatedStrings description
Description of the base set.
~BaseSet()
Free everything we allocated.
int GetNumMissing() const
Get the number of missing files.
const std::string & GetDescription(const std::string &isocode) const
Get the description for the given ISO code.
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.
uint32_t version
The version of this base set.
static const char *const * file_names
Internal names of the files in this set.
static const size_t NUM_FILES
Number of files in this set.
std::string name
The name of the base set.
std::string GetListLabel() const
Get string to use when listing this set in the settings window.
std::optional< std::string > GetTextfile(TextfileType type) const
Search a textfile file next to this base media.
uint valid_files
Number of the files that could be found and are valid.
MD5File files[NUM_FILES]
All files part of this set.
std::string url
URL for information about the base set.
uint32_t shortname
Four letter short variant of the name.
Container for all important information about a piece of content.
Information about GRF, used in the game and (part of it) in savegames.
All data of a graphics set.
std::unique_ptr< GRFConfig > extra_cfg
Parameters for extra GRF.
PaletteType palette
Palette of this graphics set.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied GRF.
Definition: gfxinit.cpp:415
BlitterType blitter
Blitter of this graphics set.
GRFConfig & GetOrCreateExtraConfig() const
Return configuration for the extra GRF, or lazily create it.
Definition: gfxinit.cpp:370
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
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.
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:436
MD5Hash hash
md5 sum of the file
std::string filename
filename
All data of a music set.
MusicSongInfo songinfo[NUM_SONGS_AVAILABLE]
Data about individual songs in set.
uint8_t num_available
Number of valid songs in set.
Metadata about a music track.
MusicTrackType filetype
decoder required for song file
std::string songname
name of song displayed in UI
uint8_t tracknr
track number of song displayed in UI
std::string filename
file on disk containing song (when used in MusicSet class)
int override_start
MIDI ticks to skip over in beginning.
bool loop
song should play in a tight loop if possible, never ending
int cat_index
entry index in CAT file, for filetype==MTT_MPSMIDI
int override_end
MIDI tick to end the song at (0 if no override)
All data of a sounds set.
GUI functions related to textfiles.
Types related to textfiles.
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:14