OpenTTD Source: src/base_media_func.h Source File

OpenTTD Source 13.2.1

base_media_func.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
13 #include "base_media_base.h"
14 #include "debug.h"
15 #include "ini_type.h"
16 #include "string_func.h"
17
22 #define fetch_metadata(name) \
23 item = metadata->GetItem(name, false); \
24 if (item == nullptr || !item->value.has_value() || item->value->empty()) { \
25 Debug(grf, 0, "Base " SET_TYPE "set detail loading: {} field missing.", name); \
26 Debug(grf, 0, " Is {} readable for the user running OpenTTD?", full_filename); \
27 return false; \
28 }
29
38 template <class T, size_t Tnum_files, bool Tsearch_in_tars>
39 bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename)
40 {
41 IniGroup *metadata = ini->GetGroup("metadata");
42 IniItem *item;
43
44 fetch_metadata("name");
45 this->name = *item->value;
46
47 fetch_metadata("description");
48 this->description[std::string{}] = *item->value;
49
50 /* Add the translations of the descriptions too. */
51 for (const IniItem *item = metadata->item; item != nullptr; item = item->next) {
52 if (item->name.compare(0, 12, "description.") != 0) continue;
53
54 this->description[item->name.substr(12)] = item->value.value_or("");
55 }
56
57 fetch_metadata("shortname");
58 for (uint i = 0; (*item->value)[i] != '\0' && i < 4; i++) {
59 this->shortname |= ((uint8)(*item->value)[i]) << (i * 8);
60 }
61
62 fetch_metadata("version");
63 this->version = atoi(item->value->c_str());
64
65 item = metadata->GetItem("fallback", false);
66 this->fallback = (item != nullptr && item->value && *item->value != "0" && *item->value != "false");
67
68 /* For each of the file types we want to find the file, MD5 checksums and warning messages. */
69 IniGroup *files = ini->GetGroup("files");
70 IniGroup *md5s = ini->GetGroup("md5s");
71 IniGroup *origin = ini->GetGroup("origin");
72 for (uint i = 0; i < Tnum_files; i++) {
73 MD5File *file = &this->files[i];
74 /* Find the filename first. */
75 item = files->GetItem(BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], false);
76 if (item == nullptr || (!item->value.has_value() && !allow_empty_filename)) {
77 Debug(grf, 0, "No " SET_TYPE " file for: {} (in {})", BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names[i], full_filename);
78 return false;
79 }
80
81 if (!item->value.has_value()) {
82 file->filename = nullptr;
83 /* If we list no file, that file must be valid */
84 this->valid_files++;
85 this->found_files++;
86 continue;
87 }
88
89 const char *filename = item->value->c_str();
90 file->filename = str_fmt("%s%s", path, filename);
91
92 /* Then find the MD5 checksum */
93 item = md5s->GetItem(filename, false);
94 if (item == nullptr || !item->value.has_value()) {
95 Debug(grf, 0, "No MD5 checksum specified for: {} (in {})", filename, full_filename);
96 return false;
97 }
98 const char *c = item->value->c_str();
99 for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
100 uint j;
101 if ('0' <= *c && *c <= '9') {
102 j = *c - '0';
103 } else if ('a' <= *c && *c <= 'f') {
104 j = *c - 'a' + 10;
105 } else if ('A' <= *c && *c <= 'F') {
106 j = *c - 'A' + 10;
107 } else {
108 Debug(grf, 0, "Malformed MD5 checksum specified for: {} (in {})", filename, full_filename);
109 return false;
110 }
111 if (i % 2 == 0) {
112 file->hash[i / 2] = j << 4;
113 } else {
114 file->hash[i / 2] |= j;
115 }
116 }
117
118 /* Then find the warning message when the file's missing */
119 item = origin->GetItem(filename, false);
120 if (item == nullptr) item = origin->GetItem("default", false);
121 if (item == nullptr || !item->value.has_value()) {
122 Debug(grf, 1, "No origin warning message specified for: {}", filename);
123 file->missing_warning = stredup("");
124 } else {
125 file->missing_warning = stredup(item->value->c_str());
126 }
127
128 file->check_result = T::CheckMD5(file, BASESET_DIR);
129 switch (file->check_result) {
130 case MD5File::CR_UNKNOWN:
131 break;
132
133 case MD5File::CR_MATCH:
134 this->valid_files++;
135 this->found_files++;
136 break;
137
138 case MD5File::CR_MISMATCH:
139 Debug(grf, 1, "MD5 checksum mismatch for: {} (in {})", filename, full_filename);
140 this->found_files++;
141 break;
142
143 case MD5File::CR_NO_FILE:
144 Debug(grf, 1, "The file {} specified in {} is missing", filename, full_filename);
145 break;
146 }
147 }
148
149 return true;
150 }
151
152 template <class Tbase_set>
153 bool BaseMedia<Tbase_set>::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
154 {
155 bool ret = false;
156 Debug(grf, 1, "Checking {} for base " SET_TYPE " set", filename);
157
158 Tbase_set *set = new Tbase_set();
159 IniFile *ini = new IniFile();
160 std::string path{ filename, basepath_length };
161 ini->LoadFromDisk(path, BASESET_DIR);
162
163 auto psep = path.rfind(PATHSEPCHAR);
164 if (psep != std::string::npos) {
165 path.erase(psep + 1);
166 } else {
167 path.clear();
168 }
169
170 if (set->FillSetDetails(ini, path.c_str(), filename.c_str())) {
171 Tbase_set *duplicate = nullptr;
172 for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != nullptr; c = c->next) {
173 if (c->name == set->name || c->shortname == set->shortname) {
174 duplicate = c;
175 break;
176 }
177 }
178 if (duplicate != nullptr) {
179 /* The more complete set takes precedence over the version number. */
180 if ((duplicate->valid_files == set->valid_files && duplicate->version >= set->version) ||
181 duplicate->valid_files > set->valid_files) {
182 Debug(grf, 1, "Not adding {} ({}) as base " SET_TYPE " set (duplicate, {})", set->name, set->version,
183 duplicate->valid_files > set->valid_files ? "less valid files" : "lower version");
184 set->next = BaseMedia<Tbase_set>::duplicate_sets;
185 BaseMedia<Tbase_set>::duplicate_sets = set;
186 } else {
187 Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
188 while (*prev != duplicate) prev = &(*prev)->next;
189
190 *prev = set;
191 set->next = duplicate->next;
192
193 /* If the duplicate set is currently used (due to rescanning this can happen)
194 * update the currently used set to the new one. This will 'lie' about the
195 * version number until a new game is started which isn't a big problem */
196 if (BaseMedia<Tbase_set>::used_set == duplicate) BaseMedia<Tbase_set>::used_set = set;
197
198 Debug(grf, 1, "Removing {} ({}) as base " SET_TYPE " set (duplicate, {})", duplicate->name, duplicate->version,
199 duplicate->valid_files < set->valid_files ? "less valid files" : "lower version");
200 duplicate->next = BaseMedia<Tbase_set>::duplicate_sets;
201 BaseMedia<Tbase_set>::duplicate_sets = duplicate;
202 ret = true;
203 }
204 } else {
205 Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
206 while (*last != nullptr) last = &(*last)->next;
207
208 *last = set;
209 ret = true;
210 }
211 if (ret) {
212 Debug(grf, 1, "Adding {} ({}) as base " SET_TYPE " set", set->name, set->version);
213 }
214 } else {
215 delete set;
216 }
217
218 delete ini;
219 return ret;
220 }
221
227 template <class Tbase_set>
228 /* static */ bool BaseMedia<Tbase_set>::SetSet(const std::string &name)
229 {
230 extern void CheckExternalFiles();
231
232 if (name.empty()) {
233 if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
234 CheckExternalFiles();
235 return true;
236 }
237
238 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
239 if (name == s->name) {
240 BaseMedia<Tbase_set>::used_set = s;
241 CheckExternalFiles();
242 return true;
243 }
244 }
245 return false;
246 }
247
254 template <class Tbase_set>
255 /* static */ char *BaseMedia<Tbase_set>::GetSetsList(char *p, const char *last)
256 {
257 p += seprintf(p, last, "List of " SET_TYPE " sets:\n");
258 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
259 p += seprintf(p, last, "%18s: %s", s->name.c_str(), s->GetDescription({}));
260 int invalid = s->GetNumInvalid();
261 if (invalid != 0) {
262 int missing = s->GetNumMissing();
263 if (missing == 0) {
264 p += seprintf(p, last, " (%i corrupt file%s)\n", invalid, invalid == 1 ? "" : "s");
265 } else {
266 p += seprintf(p, last, " (unusable: %i missing file%s)\n", missing, missing == 1 ? "" : "s");
267 }
268 } else {
269 p += seprintf(p, last, "\n");
270 }
271 }
272 p += seprintf(p, last, "\n");
273
274 return p;
275 }
276
277 #include "network/core/tcp_content_type.h"
278
279 template <class Tbase_set> const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
280 {
281 for (; s != nullptr; s = s->next) {
282 if (s->GetNumMissing() != 0) continue;
283
284 if (s->shortname != ci->unique_id) continue;
285 if (!md5sum) return s->files[0].filename;
286
287 byte md5[16];
288 memset(md5, 0, sizeof(md5));
289 for (uint i = 0; i < Tbase_set::NUM_FILES; i++) {
290 for (uint j = 0; j < sizeof(md5); j++) {
291 md5[j] ^= s->files[i].hash[j];
292 }
293 }
294 if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return s->files[0].filename;
295 }
296 return nullptr;
297 }
298
299 template <class Tbase_set>
300 /* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
301 {
302 return (TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::available_sets) != nullptr) ||
303 (TryGetBaseSetFile(ci, md5sum, BaseMedia<Tbase_set>::duplicate_sets) != nullptr);
304 }
305
310 template <class Tbase_set>
311 /* static */ int BaseMedia<Tbase_set>::GetNumSets()
312 {
313 int n = 0;
314 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
315 if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
316 n++;
317 }
318 return n;
319 }
320
325 template <class Tbase_set>
326 /* static */ int BaseMedia<Tbase_set>::GetIndexOfUsedSet()
327 {
328 int n = 0;
329 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
330 if (s == BaseMedia<Tbase_set>::used_set) return n;
331 if (s->GetNumMissing() != 0) continue;
332 n++;
333 }
334 return -1;
335 }
336
341 template <class Tbase_set>
342 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
343 {
344 for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != nullptr; s = s->next) {
345 if (s != BaseMedia<Tbase_set>::used_set && s->GetNumMissing() != 0) continue;
346 if (index == 0) return s;
347 index--;
348 }
349 error("Base" SET_TYPE "::GetSet(): index %d out of range", index);
350 }
351
356 template <class Tbase_set>
357 /* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
358 {
359 return BaseMedia<Tbase_set>::used_set;
360 }
361
366 template <class Tbase_set>
367 /* static */ Tbase_set *BaseMedia<Tbase_set>::GetAvailableSets()
368 {
369 return BaseMedia<Tbase_set>::available_sets;
370 }
371
377 #define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
378 template std::string repl_type::ini_set; \
379 template const char *repl_type::GetExtension(); \
380 template bool repl_type::AddFile(const std::string &filename, size_t pathlength, const std::string &tar_filename); \
381 template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
382 template bool repl_type::SetSet(const std::string &name); \
383 template char *repl_type::GetSetsList(char *p, const char *last); \
384 template int repl_type::GetNumSets(); \
385 template int repl_type::GetIndexOfUsedSet(); \
386 template const set_type *repl_type::GetSet(int index); \
387 template const set_type *repl_type::GetUsedSet(); \
388 template bool repl_type::DetermineBestSet(); \
389 template set_type *repl_type::GetAvailableSets(); \
390 template const char *TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const set_type *s);
391
BaseMedia::GetAvailableSets
static Tbase_set * GetAvailableSets()
Return the available sets.
Definition: base_media_func.h:367
BaseMedia::GetIndexOfUsedSet
static int GetIndexOfUsedSet()
Get the index of the currently active graphics set.
Definition: base_media_func.h:326
IniItem::next
IniItem * next
The next item in this group.
Definition: ini_type.h:26
MD5File::CR_MISMATCH
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
Definition: base_media_base.h:30
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
IniGroup
A group within an ini file.
Definition: ini_type.h:38
base_media_base.h
BaseMedia::GetSetsList
static char * GetSetsList(char *p, const char *last)
Returns a list with the sets.
Definition: base_media_func.h:255
MD5File::check_result
ChecksumResult check_result
cached result of md5 check
Definition: base_media_base.h:37
SET_TYPE
#define SET_TYPE
The type of set we're replacing.
Definition: music.cpp:14
MD5File::CR_MATCH
@ CR_MATCH
The file did exist and the md5 checksum did match.
Definition: base_media_base.h:29
MD5File::missing_warning
const char * missing_warning
warning when this file is missing
Definition: base_media_base.h:36
MD5File::hash
uint8 hash[16]
md5 sum of the file
Definition: base_media_base.h:35
tcp_content_type.h
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:28
ContentInfo
Container for all important information about a piece of content.
Definition: tcp_content_type.h:50
BaseMedia
Base for all base media (graphics, sounds)
Definition: base_media_base.h:164
ContentInfo::md5sum
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content_type.h:70
CheckExternalFiles
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
Definition: gfxinit.cpp:116
BaseMedia::AddFile
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
Add a file with the given filename.
Definition: base_media_func.h:153
BaseMedia::GetNumSets
static int GetNumSets()
Count the number of available graphics sets.
Definition: base_media_func.h:311
string_func.h
TryGetBaseSetFile
const char * TryGetBaseSetFile(const ContentInfo *ci, bool md5sum, const Tbase_set *s)
Check whether there's a base set matching some information.
Definition: base_media_func.h:279
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:89
MD5File
Structure holding filename and MD5 information about a single file.
Definition: base_media_base.h:25
str_fmt
char *CDECL str_fmt(const char *str,...)
Format, "printf", into a newly allocated string.
Definition: string.cpp:151
BaseMedia::HasSet
static bool HasSet(const ContentInfo *ci, bool md5sum)
Check whether we have an set with the exact characteristics as ci.
Definition: base_media_func.h:300
BaseSet
Information about a single base set.
Definition: base_media_base.h:49
MD5File::CR_NO_FILE
@ CR_NO_FILE
The file did not exist.
Definition: base_media_base.h:31
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
fetch_metadata
#define fetch_metadata(name)
Try to read a single piece of metadata and return false if it doesn't exist.
Definition: base_media_func.h:22
MD5File::CR_UNKNOWN
@ CR_UNKNOWN
The file has not been checked yet.
Definition: base_media_base.h:28
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
MD5File::filename
const char * filename
filename
Definition: base_media_base.h:34
ContentInfo::unique_id
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content_type.h:69
IniItem::name
std::string name
The name of this item.
Definition: ini_type.h:27
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
BaseMedia::GetSet
static const Tbase_set * GetSet(int index)
Get the name of the graphics set at the specified index.
Definition: base_media_func.h:342
IniGroup::item
IniItem * item
the first item in the group
Definition: ini_type.h:41
BaseMedia::GetUsedSet
static const Tbase_set * GetUsedSet()
Return the used set.
Definition: base_media_func.h:357
IniGroup::GetItem
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
Definition: ini_load.cpp:91
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:215
BaseSet::FillSetDetails
bool FillSetDetails(IniFile *ini, const char *path, const char *full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
Definition: base_media_func.h:39
ini_type.h
debug.h
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:163
BaseMedia::SetSet
static bool SetSet(const std::string &name)
Set the set to be used.
Definition: base_media_func.h:228