Update to 1.10.0-beta1

This commit is contained in:
dP
2019-10-31 22:24:28 +03:00
parent b84a475e14
commit 599ccf0c2b
1470 changed files with 354219 additions and 16795 deletions

View File

@@ -13,11 +13,13 @@
*/
#include "stdafx.h"
#include "fios.h"
#include "3rdparty/md5/md5.h"
#include "fileio_func.h"
#include "tar_type.h"
#include "fios.h"
#include "network/network_content.h"
#include "screenshot.h"
#include "string_func.h"
#include "tar_type.h"
#include <sys/stat.h>
#ifndef _WIN32
@@ -45,22 +47,20 @@ extern void GetOldSaveGameName(const char *file, char *title, const char *last);
/**
* Compare two FiosItem's. Used with sort when sorting the file list.
* @param da A pointer to the first FiosItem to compare.
* @param db A pointer to the second FiosItem to compare.
* @return -1, 0 or 1, depending on how the two items should be sorted.
* @param other The FiosItem to compare to.
* @return for ascending order: returns true if da < db. Vice versa for descending order.
*/
int CDECL CompareFiosItems(const FiosItem *da, const FiosItem *db)
bool FiosItem::operator< (const FiosItem &other) const
{
int r = 0;
int r = false;
if ((_savegame_sort_order & SORT_BY_NAME) == 0 && da->mtime != db->mtime) {
r = da->mtime < db->mtime ? -1 : 1;
if ((_savegame_sort_order & SORT_BY_NAME) == 0 && (*this).mtime != other.mtime) {
r = (*this).mtime - other.mtime;
} else {
r = strnatcmp(da->title, db->title);
r = strnatcmp((*this).title, other.title);
}
if (_savegame_sort_order & SORT_DESCENDING) r = -r;
return r;
if (r == 0) return false;
return (_savegame_sort_order & SORT_DESCENDING) ? r > 0 : r < 0;
}
FileList::~FileList()
@@ -103,7 +103,7 @@ void FileList::BuildFileList(AbstractFileType abstract_filetype, SaveLoadOperati
* 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 NULL if the file is not available.
* @return The information on the file, or \c nullptr if the file is not available.
*/
const FiosItem *FileList::FindItem(const char *file)
{
@@ -128,14 +128,14 @@ const FiosItem *FileList::FindItem(const char *file)
if (strcmp(long_file, item->title) == 0) return item;
}
return NULL;
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 NULL)
* @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)
@@ -147,7 +147,7 @@ StringID FiosGetDescText(const char **path, uint64 *total_free)
/**
* 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 NULL.
* @return A filename w/path if we reached a file, otherwise \c nullptr.
*/
const char *FiosBrowseTo(const FiosItem *item)
{
@@ -162,19 +162,14 @@ const char *FiosBrowseTo(const FiosItem *item)
break;
case FIOS_TYPE_PARENT: {
/* Check for possible NULL ptr (not required for UNIXes, but AmigaOS-alikes) */
/* Check for possible nullptr ptr */
char *s = strrchr(_fios_path, PATHSEPCHAR);
if (s != NULL && s != _fios_path) {
if (s != nullptr && s != _fios_path) {
s[0] = '\0'; // Remove last path separator character, so we can go up one level.
}
s = strrchr(_fios_path, PATHSEPCHAR);
if (s != NULL) {
if (s != nullptr) {
s[1] = '\0'; // go up a directory
#if defined(__MORPHOS__) || defined(__AMIGAOS__)
/* On MorphOS or AmigaOS paths look like: "Volume:directory/subdirectory" */
} else if ((s = strrchr(_fios_path, ':')) != NULL) {
s[1] = '\0';
#endif
}
break;
}
@@ -197,39 +192,31 @@ const char *FiosBrowseTo(const FiosItem *item)
return item->name;
}
return NULL;
return nullptr;
}
/**
* Construct a filename from its components in destination buffer \a buf.
* @param buf Destination buffer.
* @param path Directory path, may be \c NULL.
* @param path Directory path, may be \c nullptr.
* @param name Filename.
* @param ext Filename extension (use \c "" for no extension).
* @param last Last element of buffer \a buf.
*/
static void FiosMakeFilename(char *buf, const char *path, const char *name, const char *ext, const char *last)
{
const char *period;
if (path != nullptr) {
const char *buf_start = buf;
buf = strecpy(buf, path, last);
/* Remove trailing path separator, if present */
if (buf > buf_start && buf[-1] == PATHSEPCHAR) buf--;
}
/* Don't append the extension if it is already there */
period = strrchr(name, '.');
if (period != NULL && strcasecmp(period, ext) == 0) ext = "";
#if defined(__MORPHOS__) || defined(__AMIGAOS__)
if (path != NULL) {
unsigned char sepchar = path[(strlen(path) - 1)];
const char *period = strrchr(name, '.');
if (period != nullptr && strcasecmp(period, ext) == 0) ext = "";
if (sepchar != ':' && sepchar != '/') {
seprintf(buf, last, "%s" PATHSEP "%s%s", path, name, ext);
} else {
seprintf(buf, last, "%s%s%s", path, name, ext);
}
} else {
seprintf(buf, last, "%s%s", name, ext);
}
#else
seprintf(buf, last, "%s" PATHSEP "%s%s", path, name, ext);
#endif
seprintf(buf, last, PATHSEP "%s%s", name, ext);
}
/**
@@ -293,7 +280,7 @@ public:
fop(fop), callback_proc(callback_proc), file_list(file_list)
{}
/* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename);
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override;
};
/**
@@ -305,7 +292,7 @@ public:
bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
{
const char *ext = strrchr(filename, '.');
if (ext == NULL) return false;
if (ext == nullptr) return false;
char fios_title[64];
fios_title[0] = '\0'; // reset the title;
@@ -353,7 +340,7 @@ bool FiosFileScanner::AddFile(const char *filename, size_t basepath_length, cons
const char *t = fios_title;
if (StrEmpty(fios_title)) {
t = strrchr(filename, PATHSEPCHAR);
t = (t == NULL) ? filename : (t + 1);
t = (t == nullptr) ? filename : (t + 1);
}
strecpy(fios->title, t, lastof(fios->title));
str_validate(fios->title, lastof(fios->title));
@@ -375,7 +362,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
struct dirent *dirent;
DIR *dir;
FiosItem *fios;
int sort_start;
size_t sort_start;
char d_name[sizeof(fios->name)];
file_list.Clear();
@@ -390,8 +377,8 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
}
/* Show subdirectories */
if ((dir = ttd_opendir(_fios_path)) != NULL) {
while ((dirent = readdir(dir)) != NULL) {
if ((dir = ttd_opendir(_fios_path)) != nullptr) {
while ((dirent = readdir(dir)) != nullptr) {
strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
/* found file must be directory, but not '.' or '..' */
@@ -413,7 +400,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
{
SortingBits order = _savegame_sort_order;
_savegame_sort_order = SORT_BY_NAME | SORT_ASCENDING;
QSortT(file_list.files.Begin(), file_list.files.Length(), CompareFiosItems);
std::sort(file_list.files.begin(), file_list.files.end());
_savegame_sort_order = order;
}
@@ -423,12 +410,12 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
/* Show files */
FiosFileScanner scanner(fop, callback_proc, file_list);
if (subdir == NO_DIRECTORY) {
scanner.Scan(NULL, _fios_path, false);
scanner.Scan(nullptr, _fios_path, false);
} else {
scanner.Scan(NULL, subdir, true, true);
scanner.Scan(nullptr, subdir, true, true);
}
QSortT(file_list.Get(sort_start), file_list.Length() - sort_start, CompareFiosItems);
std::sort(file_list.files.begin() + sort_start, file_list.files.end());
/* Show drives */
FiosGetDrives(file_list);
@@ -451,7 +438,7 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir
strecat(buf, ".title", lastof(buf));
FILE *f = FioFOpenFile(buf, "r", subdir);
if (f == NULL) return;
if (f == nullptr) return;
size_t read = fread(title, 1, last - title, f);
assert(title + read <= last);
@@ -465,8 +452,8 @@ static void GetFileTitle(const char *file, char *title, const char *last, Subdir
* @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; NULL to skip the lookup
* @param last Last available byte in buffer (to prevent buffer overflows); not used when title == NULL
* @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
@@ -480,7 +467,7 @@ FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, co
* .SV2 Transport Tycoon Deluxe (Patch) saved 2-player game */
/* Don't crash if we supply no extension */
if (ext == NULL) return FIOS_TYPE_INVALID;
if (ext == nullptr) return FIOS_TYPE_INVALID;
if (strcasecmp(ext, ".sav") == 0) {
GetFileTitle(file, title, last, SAVE_DIR);
@@ -490,7 +477,7 @@ FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, co
if (fop == SLO_LOAD) {
if (strcasecmp(ext, ".ss1") == 0 || strcasecmp(ext, ".sv1") == 0 ||
strcasecmp(ext, ".sv2") == 0) {
if (title != NULL) GetOldSaveGameName(file, title, last);
if (title != nullptr) GetOldSaveGameName(file, title, last);
return FIOS_TYPE_OLDFILE;
}
}
@@ -506,10 +493,10 @@ FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, co
*/
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
{
static char *fios_save_path = NULL;
static char *fios_save_path_last = NULL;
static char *fios_save_path = nullptr;
static char *fios_save_path_last = nullptr;
if (fios_save_path == NULL) {
if (fios_save_path == nullptr) {
fios_save_path = MallocT<char>(MAX_PATH);
fios_save_path_last = fios_save_path + MAX_PATH - 1;
FioGetDirectory(fios_save_path, fios_save_path_last, SAVE_DIR);
@@ -561,11 +548,11 @@ static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *f
*/
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
{
static char *fios_scn_path = NULL;
static char *fios_scn_path_last = NULL;
static char *fios_scn_path = nullptr;
static char *fios_scn_path_last = nullptr;
/* Copy the default path on first run or on 'New Game' */
if (fios_scn_path == NULL) {
if (fios_scn_path == nullptr) {
fios_scn_path = MallocT<char>(MAX_PATH);
fios_scn_path_last = fios_scn_path + MAX_PATH - 1;
FioGetDirectory(fios_scn_path, fios_scn_path_last, SCENARIO_DIR);
@@ -632,10 +619,10 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *
*/
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
{
static char *fios_hmap_path = NULL;
static char *fios_hmap_path_last = NULL;
static char *fios_hmap_path = nullptr;
static char *fios_hmap_path_last = nullptr;
if (fios_hmap_path == NULL) {
if (fios_hmap_path == nullptr) {
fios_hmap_path = MallocT<char>(MAX_PATH);
fios_hmap_path_last = fios_hmap_path + MAX_PATH - 1;
FioGetDirectory(fios_hmap_path, fios_hmap_path_last, HEIGHTMAP_DIR);
@@ -657,9 +644,9 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
*/
const char *FiosGetScreenshotDir()
{
static char *fios_screenshot_path = NULL;
static char *fios_screenshot_path = nullptr;
if (fios_screenshot_path == NULL) {
if (fios_screenshot_path == nullptr) {
fios_screenshot_path = MallocT<char>(MAX_PATH);
FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR);
}
@@ -667,10 +654,6 @@ const char *FiosGetScreenshotDir()
return fios_screenshot_path;
}
#if defined(ENABLE_NETWORK)
#include "network/network_content.h"
#include "3rdparty/md5/md5.h"
/** Basic data to distinguish a scenario. Used in the server list window */
struct ScenarioIdentifier {
uint32 scenid; ///< ID for the scenario (generated by content).
@@ -692,7 +675,7 @@ struct ScenarioIdentifier {
/**
* Scanner to find the unique IDs of scenarios
*/
class ScenarioScanner : protected FileScanner, public SmallVector<ScenarioIdentifier, 8> {
class ScenarioScanner : protected FileScanner, public std::vector<ScenarioIdentifier> {
bool scanned; ///< Whether we've already scanned
public:
/** Initialise */
@@ -710,10 +693,10 @@ public:
this->scanned = true;
}
/* virtual */ bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename)
bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) override
{
FILE *f = FioFOpenFile(filename, "r", SCENARIO_DIR);
if (f == NULL) return false;
if (f == nullptr) return false;
ScenarioIdentifier id;
int fret = fscanf(f, "%i", &id.scenid);
@@ -732,7 +715,7 @@ public:
strecpy(basename, filename, lastof(basename));
*strrchr(basename, '.') = '\0';
f = FioFOpenFile(basename, "rb", SCENARIO_DIR, &size);
if (f == NULL) return false;
if (f == nullptr) return false;
/* calculate md5sum */
while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
@@ -743,7 +726,7 @@ public:
FioFCloseFile(f);
this->Include(id);
include(*this, id);
return true;
}
};
@@ -755,20 +738,20 @@ 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 NULL.
* @return The filename of the file, else \c nullptr.
*/
const char *FindScenario(const ContentInfo *ci, bool md5sum)
{
_scanner.Scan(false);
for (ScenarioIdentifier *id = _scanner.Begin(); id != _scanner.End(); id++) {
if (md5sum ? (memcmp(id->md5sum, ci->md5sum, sizeof(id->md5sum)) == 0)
: (id->scenid == ci->unique_id)) {
return id->filename;
for (ScenarioIdentifier &id : _scanner) {
if (md5sum ? (memcmp(id.md5sum, ci->md5sum, sizeof(id.md5sum)) == 0)
: (id.scenid == ci->unique_id)) {
return id.filename;
}
}
return NULL;
return nullptr;
}
/**
@@ -779,7 +762,7 @@ const char *FindScenario(const ContentInfo *ci, bool md5sum)
*/
bool HasScenario(const ContentInfo *ci, bool md5sum)
{
return (FindScenario(ci, md5sum) != NULL);
return (FindScenario(ci, md5sum) != nullptr);
}
/**
@@ -789,5 +772,3 @@ void ScanScenarios()
{
_scanner.Scan(true);
}
#endif /* ENABLE_NETWORK */