Files
@ r25348:f05752ac4669
Branch filter:
Location: cpp/openttd-patchpack/source/src/fios.cpp
r25348:f05752ac4669
22.3 KiB
text/x-c
Fix 3d7ab09: stopped trains not updating viewport hash when reversed for a second time (#9165)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/**
* @file fios.cpp
* This file contains functions for building file lists for the save/load dialogs.
*/
#include "stdafx.h"
#include "3rdparty/md5/md5.h"
#include "fileio_func.h"
#include "fios.h"
#include "network/network_content.h"
#include "screenshot.h"
#include "string_func.h"
#include "strings_func.h"
#include "tar_type.h"
#include <sys/stat.h>
#include <functional>
#include <optional>
#ifndef _WIN32
# include <unistd.h>
#endif /* _WIN32 */
#include "table/strings.h"
#include "safeguards.h"
/* Variables to display file lists */
static std::string *_fios_path = nullptr;
SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
/* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
extern bool FiosIsRoot(const char *path);
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
extern bool FiosIsHiddenFile(const struct dirent *ent);
extern void FiosGetDrives(FileList &file_list);
extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
/* get the name of an oldstyle savegame */
extern void GetOldSaveGameName(const std::string &file, char *title, const char *last);
/**
* Compare two FiosItem's. Used with sort when sorting the file list.
* @param other The FiosItem to compare to.
* @return for ascending order: returns true if da < db. Vice versa for descending order.
*/
bool FiosItem::operator< (const FiosItem &other) const
{
int r = false;
if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
r = (*this).mtime - other.mtime;
} else {
r = strnatcmp((*this).title, other.title);
}
if (r == 0) return false;
return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
}
/**
* Construct a file list with the given kind of files, for the stated purpose.
* @param abstract_filetype Kind of files to collect.
* @param fop Purpose of the collection, either #SLO_LOAD or #SLO_SAVE.
*/
void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperation fop)
{
this->clear();
assert(fop == SLO_LOAD || fop == SLO_SAVE);
switch (abstract_filetype) {
case FT_NONE:
break;
case FT_SAVEGAME:
FiosGetSavegameList(fop, *this);
break;
case FT_SCENARIO:
FiosGetScenarioList(fop, *this);
break;
case FT_HEIGHTMAP:
FiosGetHeightmapList(fop, *this);
break;
default:
NOT_REACHED();
}
}
/**
* Find file information of a file by its name from the file list.
* @param file The filename to return information about. Can be the actual name
* or a numbered entry into the filename list.
* @return The information on the file, or \c nullptr if the file is not available.
*/
const FiosItem *FileList::FindItem(const char *file)
{
for (const auto &it : *this) {
const FiosItem *item = ⁢
if (strcmp(file, item->name) == 0) return item;
if (strcmp(file, item->title) == 0) return item;
}
/* If no name matches, try to parse it as number */
char *endptr;
int i = strtol(file, &endptr, 10);
if (file == endptr || *endptr != '\0') i = -1;
if (IsInsideMM(i, 0, this->size())) return &this->at(i);
/* As a last effort assume it is an OpenTTD savegame and
* that the ".sav" part was not given. */
char long_file[MAX_PATH];
seprintf(long_file, lastof(long_file), "%s.sav", file);
for (const auto &it : *this) {
const FiosItem *item = ⁢
if (strcmp(long_file, item->name) == 0) return item;
if (strcmp(long_file, item->title) == 0) return item;
}
return nullptr;
}
/**
* Get descriptive texts. Returns the path and free space
* left on the device
* @param path string describing the path
* @param total_free total free space in megabytes, optional (can be nullptr)
* @return StringID describing the path (free space or failure)
*/
StringID FiosGetDescText(const char **path, uint64 *total_free)
{
*path = _fios_path->c_str();
return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
}
/**
* Browse to a new path based on the passed \a item, starting at #_fios_path.
* @param *item Item telling us what to do.
* @return A filename w/path if we reached a file, otherwise \c nullptr.
*/
const char *FiosBrowseTo(const FiosItem *item)
{
switch (item->type) {
case FIOS_TYPE_DRIVE:
#if defined(_WIN32) || defined(__OS2__)
assert(_fios_path != nullptr);
*_fios_path = std::string{ item->title[0] } + ":" PATHSEP;
#endif
break;
case FIOS_TYPE_INVALID:
break;
case FIOS_TYPE_PARENT: {
assert(_fios_path != nullptr);
auto s = _fios_path->find_last_of(PATHSEPCHAR);
if (s != std::string::npos && s != 0) {
_fios_path->erase(s); // Remove last path separator character, so we can go up one level.
}
s = _fios_path->find_last_of(PATHSEPCHAR);
if (s != std::string::npos) {
_fios_path->erase(s + 1); // go up a directory
}
break;
}
case FIOS_TYPE_DIR:
assert(_fios_path != nullptr);
*_fios_path += item->name;
*_fios_path += PATHSEP;
break;
case FIOS_TYPE_DIRECT:
assert(_fios_path != nullptr);
*_fios_path = item->name;
break;
case FIOS_TYPE_FILE:
case FIOS_TYPE_OLDFILE:
case FIOS_TYPE_SCENARIO:
case FIOS_TYPE_OLD_SCENARIO:
case FIOS_TYPE_PNG:
case FIOS_TYPE_BMP:
return item->name;
}
return nullptr;
}
/**
* Construct a filename from its components in destination buffer \a buf.
* @param path Directory path, may be \c nullptr.
* @param name Filename.
* @param ext Filename extension (use \c "" for no extension).
* @return The completed filename.
*/
static std::string FiosMakeFilename(const std::string *path, const char *name, const char *ext)
{
std::string buf;
if (path != nullptr) {
buf = *path;
/* Remove trailing path separator, if present */
if (!buf.empty() && buf.back() == PATHSEPCHAR) buf.pop_back();
}
/* Don't append the extension if it is already there */
const char *period = strrchr(name, '.');
if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
return buf + PATHSEP + name + ext;
}
/**
* Make a save game or scenario filename from a name.
* @param buf Destination buffer for saving the filename.
* @param name Name of the file.
* @param last Last element of buffer \a buf.
* @return The completed filename.
*/
std::string FiosMakeSavegameName(const char *name)
{
const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
return FiosMakeFilename(_fios_path, name, extension);
}
/**
* Construct a filename for a height map.
* @param name Filename.
* @return The completed filename.
*/
std::string FiosMakeHeightmapName(const char *name)
{
std::string ext(".");
ext += GetCurrentScreenshotExtension();
return FiosMakeFilename(_fios_path, name, ext.c_str());
}
/**
* Delete a file.
* @param name Filename to delete.
* @return Whether the file deletion was successful.
*/
bool FiosDelete(const char *name)
{
std::string filename = FiosMakeSavegameName(name);
return unlink(filename.c_str()) == 0;
}
typedef FiosType fios_getlist_callback_proc(SaveLoadOperation fop, const std::string &filename, const char *ext, char *title, const char *last);
/**
* Scanner to scan for a particular type of FIOS file.
*/
class FiosFileScanner : public FileScanner {
SaveLoadOperation fop; ///< The kind of file we are looking for.
fios_getlist_callback_proc *callback_proc; ///< Callback to check whether the file may be added
FileList &file_list; ///< Destination of the found files.
public:
/**
* Create the scanner
* @param fop Purpose of collecting the list.
* @param callback_proc The function that is called where you need to do the filtering.
* @param file_list Destination of the found files.
*/
FiosFileScanner(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, FileList &file_list) :
fop(fop), callback_proc(callback_proc), file_list(file_list)
{}
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override;
};
/**
* Try to add a fios item set with the given filename.
* @param filename the full path to the file to read
* @param basepath_length amount of characters to chop of before to get a relative filename
* @return true if the file is added.
*/
bool FiosFileScanner::AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename)
{
auto sep = filename.rfind('.');
if (sep == std::string::npos) return false;
std::string ext = filename.substr(sep);
char fios_title[64];
fios_title[0] = '\0'; // reset the title;
FiosType type = this->callback_proc(this->fop, filename, ext.c_str(), fios_title, lastof(fios_title));
if (type == FIOS_TYPE_INVALID) return false;
for (const auto &fios : file_list) {
if (filename == fios.name) return false;
}
FiosItem *fios = &file_list.emplace_back();
#ifdef _WIN32
// Retrieve the file modified date using GetFileTime rather than stat to work around an obscure MSVC bug that affects Windows XP
HANDLE fh = CreateFile(OTTD2FS(filename).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
if (fh != INVALID_HANDLE_VALUE) {
FILETIME ft;
ULARGE_INTEGER ft_int64;
if (GetFileTime(fh, nullptr, nullptr, &ft) != 0) {
ft_int64.HighPart = ft.dwHighDateTime;
ft_int64.LowPart = ft.dwLowDateTime;
// Convert from hectonanoseconds since 01/01/1601 to seconds since 01/01/1970
fios->mtime = ft_int64.QuadPart / 10000000ULL - 11644473600ULL;
} else {
fios->mtime = 0;
}
CloseHandle(fh);
#else
struct stat sb;
if (stat(filename.c_str(), &sb) == 0) {
fios->mtime = sb.st_mtime;
#endif
} else {
fios->mtime = 0;
}
fios->type = type;
strecpy(fios->name, filename.c_str(), lastof(fios->name));
/* If the file doesn't have a title, use its filename */
const char *t = fios_title;
if (StrEmpty(fios_title)) {
auto ps = filename.rfind(PATHSEPCHAR);
t = filename.c_str() + (ps == std::string::npos ? 0 : ps + 1);
}
strecpy(fios->title, t, lastof(fios->title));
str_validate(fios->title, lastof(fios->title));
return true;
}
/**
* Fill the list of the files in a directory, according to some arbitrary rule.
* @param fop Purpose of collecting the list.
* @param callback_proc The function that is called where you need to do the filtering.
* @param subdir The directory from where to start (global) searching.
* @param file_list Destination of the found files.
*/
static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *callback_proc, Subdirectory subdir, FileList &file_list)
{
struct stat sb;
struct dirent *dirent;
DIR *dir;
FiosItem *fios;
size_t sort_start;
char d_name[sizeof(fios->name)];
file_list.clear();
assert(_fios_path != nullptr);
/* A parent directory link exists if we are not in the root directory */
if (!FiosIsRoot(_fios_path->c_str())) {
fios = &file_list.emplace_back();
fios->type = FIOS_TYPE_PARENT;
fios->mtime = 0;
strecpy(fios->name, "..", lastof(fios->name));
SetDParamStr(0, "..");
GetString(fios->title, STR_SAVELOAD_PARENT_DIRECTORY, lastof(fios->title));
}
/* Show subdirectories */
if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
while ((dirent = readdir(dir)) != nullptr) {
strecpy(d_name, FS2OTTD(dirent->d_name).c_str(), lastof(d_name));
/* found file must be directory, but not '.' or '..' */
if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
fios = &file_list.emplace_back();
fios->type = FIOS_TYPE_DIR;
fios->mtime = 0;
strecpy(fios->name, d_name, lastof(fios->name));
std::string dirname = std::string(d_name) + PATHSEP;
SetDParamStr(0, dirname.c_str());
GetString(fios->title, STR_SAVELOAD_DIRECTORY, lastof(fios->title));
str_validate(fios->title, lastof(fios->title));
}
}
closedir(dir);
}
/* Sort the subdirs always by name, ascending, remember user-sorting order */
{
SortingBits order = _savegame_sort_order;
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
std::sort(file_list.begin(), file_list.end());
_savegame_sort_order = order;
}
/* This is where to start sorting for the filenames */
sort_start = file_list.size();
/* Show files */
FiosFileScanner scanner(fop, callback_proc, file_list);
if (subdir == NO_DIRECTORY) {
scanner.Scan(nullptr, _fios_path->c_str(), false);
} else {
scanner.Scan(nullptr, subdir, true, true);
}
std::sort(file_list.begin() + sort_start, file_list.end());
/* Show drives */
FiosGetDrives(file_list);
file_list.shrink_to_fit();
}
/**
* Get the title of a file, which (if exists) is stored in a file named
* the same as the data file but with '.title' added to it.
* @param file filename to get the title for
* @param title the title buffer to fill
* @param last the last element in the title buffer
* @param subdir the sub directory to search in
*/
static void GetFileTitle(const std::string &file, char *title, const char *last, Subdirectory subdir)
{
std::string buf = file;
buf += ".title";
FILE *f = FioFOpenFile(buf, "r", subdir);
if (f == nullptr) return;
size_t read = fread(title, 1, last - title, f);
assert(title + read <= last);
title[read] = '\0';
str_validate(title, last);
FioFCloseFile(f);
}
/**
* Callback for FiosGetFileList. It tells if a file is a savegame or not.
* @param fop Purpose of collecting the list.
* @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file; nullptr to skip the lookup
* @param last Last available byte in buffer (to prevent buffer overflows); not used when title == nullptr
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a savegame
* @see FiosGetFileList
* @see FiosGetSavegameList
*/
FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show savegame files
* .SAV OpenTTD saved game
* .SS1 Transport Tycoon Deluxe preset game
* .SV1 Transport Tycoon Deluxe (Patch) saved game
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
/* Don't crash if we supply no extension */
if (ext == nullptr) return FIOS_TYPE_INVALID;
if (strcasecmp(ext, ".sav") == 0) {
GetFileTitle(file, title, last, SAVE_DIR);
return FIOS_TYPE_FILE;
}
if (fop == SLO_LOAD) {
if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
strcasecmp(ext, ".sv2") == 0) {
if (title != nullptr) GetOldSaveGameName(file, title, last);
return FIOS_TYPE_OLDFILE;
}
}
return FIOS_TYPE_INVALID;
}
/**
* Get a list of savegames.
* @param fop Purpose of collecting the list.
* @param file_list Destination of the found files.
* @see FiosGetFileList
*/
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
{
static std::optional<std::string> fios_save_path;
if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
_fios_path = &(*fios_save_path);
FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
}
/**
* Callback for FiosGetFileList. It tells if a file is a scenario or not.
* @param fop Purpose of collecting the list.
* @param file Name of the file to check.
* @param ext A pointer to the extension identifier inside file
* @param title Buffer if a callback wants to lookup the title of the file
* @param last Last available byte in buffer (to prevent buffer overflows)
* @return a FIOS_TYPE_* type of the found file, FIOS_TYPE_INVALID if not a scenario
* @see FiosGetFileList
* @see FiosGetScenarioList
*/
static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show scenario files
* .SCN OpenTTD style scenario file
* .SV0 Transport Tycoon Deluxe (Patch) scenario
* .SS0 Transport Tycoon Deluxe preset scenario */
if (strcasecmp(ext, ".scn") == 0) {
GetFileTitle(file, title, last, SCENARIO_DIR);
return FIOS_TYPE_SCENARIO;
}
if (fop == SLO_LOAD) {
if (strcasecmp(ext, ".sv0") == 0 || strcasecmp(ext, ".ss0") == 0 ) {
GetOldSaveGameName(file, title, last);
return FIOS_TYPE_OLD_SCENARIO;
}
}
return FIOS_TYPE_INVALID;
}
/**
* Get a list of scenarios.
* @param fop Purpose of collecting the list.
* @param file_list Destination of the found files.
* @see FiosGetFileList
*/
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
{
static std::optional<std::string> fios_scn_path;
/* Copy the default path on first run or on 'New Game' */
if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
_fios_path = &(*fios_scn_path);
std::string base_path = FioFindDirectory(SCENARIO_DIR);
Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
}
static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last)
{
/* Show heightmap files
* .PNG PNG Based heightmap files
* .BMP BMP Based heightmap files
*/
FiosType type = FIOS_TYPE_INVALID;
#ifdef WITH_PNG
if (strcasecmp(ext, ".png") == 0) type = FIOS_TYPE_PNG;
#endif /* WITH_PNG */
if (strcasecmp(ext, ".bmp") == 0) type = FIOS_TYPE_BMP;
if (type == FIOS_TYPE_INVALID) return FIOS_TYPE_INVALID;
TarFileList::iterator it = _tar_filelist[SCENARIO_DIR].find(file);
if (it != _tar_filelist[SCENARIO_DIR].end()) {
/* If the file is in a tar and that tar is not in a heightmap
* directory we are for sure not supposed to see it.
* Examples of this are pngs part of documentation within
* collections of NewGRFs or 32 bpp graphics replacement PNGs.
*/
bool match = false;
Searchpath sp;
FOR_ALL_SEARCHPATHS(sp) {
std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
match = true;
break;
}
}
if (!match) return FIOS_TYPE_INVALID;
}
GetFileTitle(file, title, last, HEIGHTMAP_DIR);
return type;
}
/**
* Get a list of heightmaps.
* @param fop Purpose of collecting the list.
* @param file_list Destination of the found files.
*/
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
{
static std::optional<std::string> fios_hmap_path;
if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
_fios_path = &(*fios_hmap_path);
std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
}
/**
* Get the directory for screenshots.
* @return path to screenshots
*/
const char *FiosGetScreenshotDir()
{
static std::optional<std::string> fios_screenshot_path;
if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
return fios_screenshot_path->c_str();
}
/** Basic data to distinguish a scenario. Used in the server list window */
struct ScenarioIdentifier {
uint32 scenid; ///< ID for the scenario (generated by content).
uint8 md5sum[16]; ///< MD5 checksum of file.
char filename[MAX_PATH]; ///< filename of the file.
bool operator == (const ScenarioIdentifier &other) const
{
return this->scenid == other.scenid &&
memcmp(this->md5sum, other.md5sum, sizeof(this->md5sum)) == 0;
}
bool operator != (const ScenarioIdentifier &other) const
{
return !(*this == other);
}
};
/**
* Scanner to find the unique IDs of scenarios
*/
class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
bool scanned; ///< Whether we've already scanned
public:
/** Initialise */
ScenarioScanner() : scanned(false) {}
/**
* Scan, but only if it's needed.
* @param rescan whether to force scanning even when it's not necessary
*/
void Scan(bool rescan)
{
if (this->scanned && !rescan) return;
this->FileScanner::Scan(".id", SCENARIO_DIR, true, true);
this->scanned = true;
}
bool AddFile(const std::string &filename, size_t basepath_length, const std::string &tar_filename) override
{
FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
if (f == nullptr) return false;
ScenarioIdentifier id;
int fret = fscanf(f, "%u", &id.scenid);
FioFCloseFile(f);
if (fret != 1) return false;
strecpy(id.filename, filename.c_str(), lastof(id.filename));
Md5 checksum;
uint8 buffer[1024];
size_t len, size;
/* open the scenario file, but first get the name.
* This is safe as we check on extension which
* must always exist. */
f = FioFOpenFile(filename.substr(0, filename.rfind('.')), "rb", SCENARIO_DIR, &size);
if (f == nullptr) return false;
/* calculate md5sum */
while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
size -= len;
checksum.Append(buffer, len);
}
checksum.Finish(id.md5sum);
FioFCloseFile(f);
include(*this, id);
return true;
}
};
/** Scanner for scenarios */
static ScenarioScanner _scanner;
/**
* Find a given scenario based on its unique ID.
* @param ci The content info to compare it to.
* @param md5sum Whether to look at the md5sum or the id.
* @return The filename of the file, else \c nullptr.
*/
const char *FindScenario(const ContentInfo *ci, bool md5sum)
{
_scanner.Scan(false);
for (ScenarioIdentifier &id : _scanner) {
if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
: (id.scenid == ci->unique_id)) {
return id.filename;
}
}
return nullptr;
}
/**
* Check whether we've got a given scenario based on its unique ID.
* @param ci The content info to compare it to.
* @param md5sum Whether to look at the md5sum or the id.
* @return True iff we've got the scenario.
*/
bool HasScenario(const ContentInfo *ci, bool md5sum)
{
return (FindScenario(ci, md5sum) != nullptr);
}
/**
* Force a (re)scan of the scenarios.
*/
void ScanScenarios()
{
_scanner.Scan(true);
}
|