mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: Use std::string in FIO search path handling.
This commit is contained in:
parent
0c6e8a8123
commit
f3326d34e7
138
src/fileio.cpp
138
src/fileio.cpp
|
@ -297,65 +297,52 @@ void FioFCloseFile(FILE *f)
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename)
|
|
||||||
{
|
|
||||||
assert(subdir < NUM_SUBDIRS);
|
|
||||||
assert(sp < NUM_SEARCHPATHS);
|
|
||||||
|
|
||||||
seprintf(buf, last, "%s%s%s", _searchpaths[sp].c_str(), _subdirs[subdir], filename);
|
|
||||||
return buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a path to the filename in one of the search directories.
|
* Find a path to the filename in one of the search directories.
|
||||||
* @param[out] buf Destination buffer for the path.
|
|
||||||
* @param last End of the destination buffer.
|
|
||||||
* @param subdir Subdirectory to try.
|
* @param subdir Subdirectory to try.
|
||||||
* @param filename Filename to look for.
|
* @param filename Filename to look for.
|
||||||
* @return \a buf containing the path if the path was found, else \c nullptr.
|
* @return String containing the path if the path was found, else an empty string.
|
||||||
*/
|
*/
|
||||||
char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename)
|
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
|
||||||
{
|
{
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
assert(subdir < NUM_SUBDIRS);
|
assert(subdir < NUM_SUBDIRS);
|
||||||
|
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
FOR_ALL_SEARCHPATHS(sp) {
|
||||||
FioGetFullPath(buf, last, sp, subdir, filename);
|
std::string buf = FioGetDirectory(sp, subdir);
|
||||||
if (FileExists(buf)) return buf;
|
buf += filename;
|
||||||
|
if (FileExists(buf.c_str())) return buf;
|
||||||
#if !defined(_WIN32)
|
#if !defined(_WIN32)
|
||||||
/* Be, as opening files, aware that sometimes the filename
|
/* Be, as opening files, aware that sometimes the filename
|
||||||
* might be in uppercase when it is in lowercase on the
|
* might be in uppercase when it is in lowercase on the
|
||||||
* disk. Of course Windows doesn't care about casing. */
|
* disk. Of course Windows doesn't care about casing. */
|
||||||
if (strtolower(buf + _searchpaths[sp].size() - 1) && FileExists(buf)) return buf;
|
if (strtolower(buf, _searchpaths[sp].size() - 1) && FileExists(buf.c_str())) return buf;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir)
|
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir)
|
||||||
{
|
{
|
||||||
assert(subdir < NUM_SUBDIRS);
|
assert(subdir < NUM_SUBDIRS);
|
||||||
assert(sp < NUM_SEARCHPATHS);
|
assert(sp < NUM_SEARCHPATHS);
|
||||||
|
|
||||||
seprintf(buf, last, "%s%s", _searchpaths[sp].c_str(), _subdirs[subdir]);
|
return _searchpaths[sp] + _subdirs[subdir];
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir)
|
std::string FioFindDirectory(Subdirectory subdir)
|
||||||
{
|
{
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
|
|
||||||
/* Find and return the first valid directory */
|
/* Find and return the first valid directory */
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
FOR_ALL_SEARCHPATHS(sp) {
|
||||||
char *ret = FioAppendDirectory(buf, last, sp, subdir);
|
std::string ret = FioGetDirectory(sp, subdir);
|
||||||
if (FileExists(buf)) return ret;
|
if (FileExists(ret.c_str())) return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Could not find the directory, fall back to a base path */
|
/* Could not find the directory, fall back to a base path */
|
||||||
strecpy(buf, _personal_dir.c_str(), last);
|
return _personal_dir;
|
||||||
|
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
|
static FILE *FioFOpenFileSp(const char *filename, const char *mode, Searchpath sp, Subdirectory subdir, size_t *filesize)
|
||||||
|
@ -545,21 +532,13 @@ void FioCreateDirectory(const char *name)
|
||||||
* Appends, if necessary, the path separator character to the end of the string.
|
* Appends, if necessary, the path separator character to the end of the string.
|
||||||
* It does not add the path separator to zero-sized strings.
|
* It does not add the path separator to zero-sized strings.
|
||||||
* @param buf string to append the separator to
|
* @param buf string to append the separator to
|
||||||
* @param last the last element of \a buf.
|
|
||||||
* @return true iff the operation succeeded
|
* @return true iff the operation succeeded
|
||||||
*/
|
*/
|
||||||
bool AppendPathSeparator(char *buf, const char *last)
|
void AppendPathSeparator(std::string &buf)
|
||||||
{
|
{
|
||||||
size_t s = strlen(buf);
|
if (buf.empty()) return;
|
||||||
|
|
||||||
/* Length of string + path separator + '\0' */
|
if (buf.back() != PATHSEPCHAR) buf.push_back(PATHSEPCHAR);
|
||||||
if (s != 0 && buf[s - 1] != PATHSEPCHAR) {
|
|
||||||
if (&buf[s] >= last) return false;
|
|
||||||
|
|
||||||
seprintf(buf + s, last, "%c", PATHSEPCHAR);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void TarAddLink(const std::string &srcParam, const std::string &destParam, Subdirectory subdir)
|
static void TarAddLink(const std::string &srcParam, const std::string &destParam, Subdirectory subdir)
|
||||||
|
@ -1013,9 +992,9 @@ bool DoScanWorkingDirectory()
|
||||||
/* No personal/home directory, so the working directory won't be that. */
|
/* No personal/home directory, so the working directory won't be that. */
|
||||||
if (_searchpaths[SP_PERSONAL_DIR].empty()) return true;
|
if (_searchpaths[SP_PERSONAL_DIR].empty()) return true;
|
||||||
|
|
||||||
char tmp[MAX_PATH];
|
std::string tmp = _searchpaths[SP_WORKING_DIR] + PERSONAL_DIR;
|
||||||
seprintf(tmp, lastof(tmp), "%s%s", _searchpaths[SP_WORKING_DIR].c_str(), PERSONAL_DIR);
|
AppendPathSeparator(tmp);
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
|
||||||
return _searchpaths[SP_PERSONAL_DIR] != tmp;
|
return _searchpaths[SP_PERSONAL_DIR] != tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1025,14 +1004,15 @@ bool DoScanWorkingDirectory()
|
||||||
*/
|
*/
|
||||||
void DetermineBasePaths(const char *exe)
|
void DetermineBasePaths(const char *exe)
|
||||||
{
|
{
|
||||||
char tmp[MAX_PATH];
|
std::string tmp;
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
||||||
const char *xdg_data_home = xdgDataHome(nullptr);
|
const char *xdg_data_home = xdgDataHome(nullptr);
|
||||||
seprintf(tmp, lastof(tmp), "%s" PATHSEP "%s", xdg_data_home,
|
tmp = xdg_data_home;
|
||||||
PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR);
|
tmp += PATHSEP;
|
||||||
|
tmp += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
||||||
free(xdg_data_home);
|
free(xdg_data_home);
|
||||||
|
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
_searchpaths[SP_PERSONAL_DIR_XDG] = tmp;
|
||||||
#endif
|
#endif
|
||||||
#if defined(OS2) || !defined(WITH_PERSONAL_DIR)
|
#if defined(OS2) || !defined(WITH_PERSONAL_DIR)
|
||||||
|
@ -1060,8 +1040,10 @@ void DetermineBasePaths(const char *exe)
|
||||||
|
|
||||||
if (homedir != nullptr) {
|
if (homedir != nullptr) {
|
||||||
ValidateString(homedir);
|
ValidateString(homedir);
|
||||||
seprintf(tmp, lastof(tmp), "%s" PATHSEP "%s", homedir, PERSONAL_DIR);
|
tmp = homedir;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
tmp += PATHSEP;
|
||||||
|
tmp += PERSONAL_DIR;
|
||||||
|
AppendPathSeparator(tmp);
|
||||||
|
|
||||||
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
||||||
free(homedir);
|
free(homedir);
|
||||||
|
@ -1071,8 +1053,8 @@ void DetermineBasePaths(const char *exe)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(WITH_SHARED_DIR)
|
#if defined(WITH_SHARED_DIR)
|
||||||
seprintf(tmp, lastof(tmp), "%s", SHARED_DIR);
|
tmp = SHARED_DIR;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_SHARED_DIR] = tmp;
|
_searchpaths[SP_SHARED_DIR] = tmp;
|
||||||
#else
|
#else
|
||||||
_searchpaths[SP_SHARED_DIR].clear();
|
_searchpaths[SP_SHARED_DIR].clear();
|
||||||
|
@ -1083,8 +1065,8 @@ void DetermineBasePaths(const char *exe)
|
||||||
|
|
||||||
if (_config_file.empty()) {
|
if (_config_file.empty()) {
|
||||||
/* Get the path to working directory of OpenTTD. */
|
/* Get the path to working directory of OpenTTD. */
|
||||||
if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0';
|
tmp = cwd;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_WORKING_DIR] = tmp;
|
_searchpaths[SP_WORKING_DIR] = tmp;
|
||||||
|
|
||||||
_do_scan_working_directory = DoScanWorkingDirectory();
|
_do_scan_working_directory = DoScanWorkingDirectory();
|
||||||
|
@ -1093,8 +1075,8 @@ void DetermineBasePaths(const char *exe)
|
||||||
size_t end = _config_file.find_last_of(PATHSEPCHAR);
|
size_t end = _config_file.find_last_of(PATHSEPCHAR);
|
||||||
if (end == std::string::npos) {
|
if (end == std::string::npos) {
|
||||||
/* _config_file is not in a folder, so use current directory. */
|
/* _config_file is not in a folder, so use current directory. */
|
||||||
if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0';
|
tmp = cwd;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_WORKING_DIR] = tmp;
|
_searchpaths[SP_WORKING_DIR] = tmp;
|
||||||
} else {
|
} else {
|
||||||
_searchpaths[SP_WORKING_DIR] = _config_file.substr(0, end + 1);
|
_searchpaths[SP_WORKING_DIR] = _config_file.substr(0, end + 1);
|
||||||
|
@ -1103,8 +1085,13 @@ void DetermineBasePaths(const char *exe)
|
||||||
|
|
||||||
/* Change the working directory to that one of the executable */
|
/* Change the working directory to that one of the executable */
|
||||||
if (ChangeWorkingDirectoryToExecutable(exe)) {
|
if (ChangeWorkingDirectoryToExecutable(exe)) {
|
||||||
if (getcwd(tmp, MAX_PATH) == nullptr) *tmp = '\0';
|
char buf[MAX_PATH];
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
if (getcwd(buf, lengthof(buf)) == nullptr) {
|
||||||
|
tmp.clear();
|
||||||
|
} else {
|
||||||
|
tmp = buf;
|
||||||
|
}
|
||||||
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_BINARY_DIR] = tmp;
|
_searchpaths[SP_BINARY_DIR] = tmp;
|
||||||
} else {
|
} else {
|
||||||
_searchpaths[SP_BINARY_DIR].clear();
|
_searchpaths[SP_BINARY_DIR].clear();
|
||||||
|
@ -1120,8 +1107,8 @@ void DetermineBasePaths(const char *exe)
|
||||||
#if !defined(GLOBAL_DATA_DIR)
|
#if !defined(GLOBAL_DATA_DIR)
|
||||||
_searchpaths[SP_INSTALLATION_DIR].clear();
|
_searchpaths[SP_INSTALLATION_DIR].clear();
|
||||||
#else
|
#else
|
||||||
seprintf(tmp, lastof(tmp), "%s", GLOBAL_DATA_DIR);
|
tmp = GLOBAL_DATA_DIR;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_INSTALLATION_DIR] = tmp;
|
_searchpaths[SP_INSTALLATION_DIR] = tmp;
|
||||||
#endif
|
#endif
|
||||||
#ifdef WITH_COCOA
|
#ifdef WITH_COCOA
|
||||||
|
@ -1146,14 +1133,13 @@ void DeterminePaths(const char *exe)
|
||||||
DetermineBasePaths(exe);
|
DetermineBasePaths(exe);
|
||||||
|
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
||||||
char config_home[MAX_PATH];
|
|
||||||
|
|
||||||
const char *xdg_config_home = xdgConfigHome(nullptr);
|
const char *xdg_config_home = xdgConfigHome(nullptr);
|
||||||
seprintf(config_home, lastof(config_home), "%s" PATHSEP "%s", xdg_config_home,
|
std::string config_home(xdg_config_home);
|
||||||
PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR);
|
config_home += PATHSEP;
|
||||||
|
config_home += PERSONAL_DIR[0] == '.' ? &PERSONAL_DIR[1] : PERSONAL_DIR;
|
||||||
free(xdg_config_home);
|
free(xdg_config_home);
|
||||||
|
|
||||||
AppendPathSeparator(config_home, lastof(config_home));
|
AppendPathSeparator(config_home);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
|
@ -1166,10 +1152,10 @@ void DeterminePaths(const char *exe)
|
||||||
if (!_config_file.empty()) {
|
if (!_config_file.empty()) {
|
||||||
config_dir = _searchpaths[SP_WORKING_DIR];
|
config_dir = _searchpaths[SP_WORKING_DIR];
|
||||||
} else {
|
} else {
|
||||||
char personal_dir[MAX_PATH];
|
std::string personal_dir = FioFindFullPath(BASE_DIR, "openttd.cfg");
|
||||||
if (FioFindFullPath(personal_dir, lastof(personal_dir), BASE_DIR, "openttd.cfg") != nullptr) {
|
if (!personal_dir.empty()) {
|
||||||
char *end = strrchr(personal_dir, PATHSEPCHAR);
|
auto end = personal_dir.find_last_of(PATHSEPCHAR);
|
||||||
if (end != nullptr) end[1] = '\0';
|
if (end != std::string::npos) personal_dir.erase(end + 1);
|
||||||
config_dir = personal_dir;
|
config_dir = personal_dir;
|
||||||
} else {
|
} else {
|
||||||
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
#if defined(WITH_XDG_BASEDIR) && defined(WITH_PERSONAL_DIR)
|
||||||
|
@ -1330,21 +1316,21 @@ static uint ScanPath(FileScanner *fs, const char *extension, const char *path, s
|
||||||
|
|
||||||
while ((dirent = readdir(dir)) != nullptr) {
|
while ((dirent = readdir(dir)) != nullptr) {
|
||||||
const char *d_name = FS2OTTD(dirent->d_name);
|
const char *d_name = FS2OTTD(dirent->d_name);
|
||||||
char filename[MAX_PATH];
|
|
||||||
|
|
||||||
if (!FiosIsValidFile(path, dirent, &sb)) continue;
|
if (!FiosIsValidFile(path, dirent, &sb)) continue;
|
||||||
|
|
||||||
seprintf(filename, lastof(filename), "%s%s", path, d_name);
|
std::string filename(path);
|
||||||
|
filename += d_name;
|
||||||
|
|
||||||
if (S_ISDIR(sb.st_mode)) {
|
if (S_ISDIR(sb.st_mode)) {
|
||||||
/* Directory */
|
/* Directory */
|
||||||
if (!recursive) continue;
|
if (!recursive) continue;
|
||||||
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
|
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
|
||||||
if (!AppendPathSeparator(filename, lastof(filename))) continue;
|
AppendPathSeparator(filename);
|
||||||
num += ScanPath(fs, extension, filename, basepath_length, recursive);
|
num += ScanPath(fs, extension, filename.c_str(), basepath_length, recursive);
|
||||||
} else if (S_ISREG(sb.st_mode)) {
|
} else if (S_ISREG(sb.st_mode)) {
|
||||||
/* File */
|
/* File */
|
||||||
if (MatchesExtension(extension, filename) && fs->AddFile(filename, basepath_length, nullptr)) num++;
|
if (MatchesExtension(extension, filename.c_str()) && fs->AddFile(filename.c_str(), basepath_length, nullptr)) num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1383,7 +1369,6 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r
|
||||||
this->subdir = sd;
|
this->subdir = sd;
|
||||||
|
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
char path[MAX_PATH];
|
|
||||||
TarFileList::iterator tar;
|
TarFileList::iterator tar;
|
||||||
uint num = 0;
|
uint num = 0;
|
||||||
|
|
||||||
|
@ -1391,8 +1376,8 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r
|
||||||
/* Don't search in the working directory */
|
/* Don't search in the working directory */
|
||||||
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
|
if (sp == SP_WORKING_DIR && !_do_scan_working_directory) continue;
|
||||||
|
|
||||||
FioAppendDirectory(path, lastof(path), sp, sd);
|
std::string path = FioGetDirectory(sp, sd);
|
||||||
num += ScanPath(this, extension, path, strlen(path), recursive);
|
num += ScanPath(this, extension, path.c_str(), path.size(), recursive);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tars && sd != NO_DIRECTORY) {
|
if (tars && sd != NO_DIRECTORY) {
|
||||||
|
@ -1425,8 +1410,7 @@ uint FileScanner::Scan(const char *extension, Subdirectory sd, bool tars, bool r
|
||||||
*/
|
*/
|
||||||
uint FileScanner::Scan(const char *extension, const char *directory, bool recursive)
|
uint FileScanner::Scan(const char *extension, const char *directory, bool recursive)
|
||||||
{
|
{
|
||||||
char path[MAX_PATH];
|
std::string path(directory);
|
||||||
strecpy(path, directory, lastof(path));
|
AppendPathSeparator(path);
|
||||||
if (!AppendPathSeparator(path, lastof(path))) return 0;
|
return ScanPath(this, extension, path.c_str(), path.size(), recursive);
|
||||||
return ScanPath(this, extension, path, strlen(path), recursive);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,16 +39,15 @@ bool IsValidSearchPath(Searchpath sp);
|
||||||
void FioFCloseFile(FILE *f);
|
void FioFCloseFile(FILE *f);
|
||||||
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
|
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
|
||||||
bool FioCheckFileExists(const char *filename, Subdirectory subdir);
|
bool FioCheckFileExists(const char *filename, Subdirectory subdir);
|
||||||
char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename);
|
std::string FioFindFullPath(Subdirectory subdir, const char *filename);
|
||||||
char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename);
|
std::string FioGetDirectory(Searchpath sp, Subdirectory subdir);
|
||||||
char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir);
|
std::string FioFindDirectory(Subdirectory subdir);
|
||||||
char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir);
|
|
||||||
void FioCreateDirectory(const char *name);
|
void FioCreateDirectory(const char *name);
|
||||||
|
|
||||||
const char *FiosGetScreenshotDir();
|
const char *FiosGetScreenshotDir();
|
||||||
|
|
||||||
void SanitizeFilename(char *filename);
|
void SanitizeFilename(char *filename);
|
||||||
bool AppendPathSeparator(char *buf, const char *last);
|
void AppendPathSeparator(std::string &buf);
|
||||||
void DeterminePaths(const char *exe);
|
void DeterminePaths(const char *exe);
|
||||||
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
|
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
|
||||||
bool FileExists(const char *filename);
|
bool FileExists(const char *filename);
|
||||||
|
|
109
src/fios.cpp
109
src/fios.cpp
|
@ -19,6 +19,8 @@
|
||||||
#include "string_func.h"
|
#include "string_func.h"
|
||||||
#include "tar_type.h"
|
#include "tar_type.h"
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <functional>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
|
@ -29,8 +31,7 @@
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
|
||||||
/* Variables to display file lists */
|
/* Variables to display file lists */
|
||||||
static char *_fios_path;
|
static std::string *_fios_path = nullptr;
|
||||||
static const char *_fios_path_last;
|
|
||||||
SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
|
SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
|
||||||
|
|
||||||
/* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
|
/* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
|
||||||
|
@ -138,7 +139,7 @@ const FiosItem *FileList::FindItem(const char *file)
|
||||||
*/
|
*/
|
||||||
StringID FiosGetDescText(const char **path, uint64 *total_free)
|
StringID FiosGetDescText(const char **path, uint64 *total_free)
|
||||||
{
|
{
|
||||||
*path = _fios_path;
|
*path = _fios_path->c_str();
|
||||||
return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
|
return FiosGetDiskFreeSpace(*path, total_free) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +153,8 @@ const char *FiosBrowseTo(const FiosItem *item)
|
||||||
switch (item->type) {
|
switch (item->type) {
|
||||||
case FIOS_TYPE_DRIVE:
|
case FIOS_TYPE_DRIVE:
|
||||||
#if defined(_WIN32) || defined(__OS2__)
|
#if defined(_WIN32) || defined(__OS2__)
|
||||||
seprintf(_fios_path, _fios_path_last, "%c:" PATHSEP, item->title[0]);
|
assert(_fios_path != nullptr);
|
||||||
|
*_fios_path = std::string{ item->title[0] } + ":" PATHSEP;
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -160,25 +162,28 @@ const char *FiosBrowseTo(const FiosItem *item)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FIOS_TYPE_PARENT: {
|
case FIOS_TYPE_PARENT: {
|
||||||
/* Check for possible nullptr ptr */
|
assert(_fios_path != nullptr);
|
||||||
char *s = strrchr(_fios_path, PATHSEPCHAR);
|
auto s = _fios_path->find_last_of(PATHSEPCHAR);
|
||||||
if (s != nullptr && s != _fios_path) {
|
if (s != std::string::npos && s != 0) {
|
||||||
s[0] = '\0'; // Remove last path separator character, so we can go up one level.
|
_fios_path->erase(s); // Remove last path separator character, so we can go up one level.
|
||||||
}
|
}
|
||||||
s = strrchr(_fios_path, PATHSEPCHAR);
|
|
||||||
if (s != nullptr) {
|
s = _fios_path->find_last_of(PATHSEPCHAR);
|
||||||
s[1] = '\0'; // go up a directory
|
if (s != std::string::npos) {
|
||||||
|
_fios_path->erase(s + 1); // go up a directory
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case FIOS_TYPE_DIR:
|
case FIOS_TYPE_DIR:
|
||||||
strecat(_fios_path, item->name, _fios_path_last);
|
assert(_fios_path != nullptr);
|
||||||
strecat(_fios_path, PATHSEP, _fios_path_last);
|
*_fios_path += item->name;
|
||||||
|
*_fios_path += PATHSEP;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FIOS_TYPE_DIRECT:
|
case FIOS_TYPE_DIRECT:
|
||||||
seprintf(_fios_path, _fios_path_last, "%s", item->name);
|
assert(_fios_path != nullptr);
|
||||||
|
*_fios_path = item->name;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FIOS_TYPE_FILE:
|
case FIOS_TYPE_FILE:
|
||||||
|
@ -227,7 +232,7 @@ void FiosMakeSavegameName(char *buf, const char *name, const char *last)
|
||||||
{
|
{
|
||||||
const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
|
const char *extension = (_game_mode == GM_EDITOR) ? ".scn" : ".sav";
|
||||||
|
|
||||||
FiosMakeFilename(buf, _fios_path, name, extension, last);
|
FiosMakeFilename(buf, _fios_path->c_str(), name, extension, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -242,7 +247,7 @@ void FiosMakeHeightmapName(char *buf, const char *name, const char *last)
|
||||||
ext[0] = '.';
|
ext[0] = '.';
|
||||||
strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext));
|
strecpy(ext + 1, GetCurrentScreenshotExtension(), lastof(ext));
|
||||||
|
|
||||||
FiosMakeFilename(buf, _fios_path, name, ext, last);
|
FiosMakeFilename(buf, _fios_path->c_str(), name, ext, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -365,8 +370,10 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
|
|
||||||
file_list.Clear();
|
file_list.Clear();
|
||||||
|
|
||||||
|
assert(_fios_path != nullptr);
|
||||||
|
|
||||||
/* A parent directory link exists if we are not in the root directory */
|
/* A parent directory link exists if we are not in the root directory */
|
||||||
if (!FiosIsRoot(_fios_path)) {
|
if (!FiosIsRoot(_fios_path->c_str())) {
|
||||||
fios = file_list.Append();
|
fios = file_list.Append();
|
||||||
fios->type = FIOS_TYPE_PARENT;
|
fios->type = FIOS_TYPE_PARENT;
|
||||||
fios->mtime = 0;
|
fios->mtime = 0;
|
||||||
|
@ -375,12 +382,12 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show subdirectories */
|
/* Show subdirectories */
|
||||||
if ((dir = ttd_opendir(_fios_path)) != nullptr) {
|
if ((dir = ttd_opendir(_fios_path->c_str())) != nullptr) {
|
||||||
while ((dirent = readdir(dir)) != nullptr) {
|
while ((dirent = readdir(dir)) != nullptr) {
|
||||||
strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
|
strecpy(d_name, FS2OTTD(dirent->d_name), lastof(d_name));
|
||||||
|
|
||||||
/* found file must be directory, but not '.' or '..' */
|
/* found file must be directory, but not '.' or '..' */
|
||||||
if (FiosIsValidFile(_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
|
if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) &&
|
||||||
(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
|
(!FiosIsHiddenFile(dirent) || strncasecmp(d_name, PERSONAL_DIR, strlen(d_name)) == 0) &&
|
||||||
strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
|
strcmp(d_name, ".") != 0 && strcmp(d_name, "..") != 0) {
|
||||||
fios = file_list.Append();
|
fios = file_list.Append();
|
||||||
|
@ -408,7 +415,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
|
||||||
/* Show files */
|
/* Show files */
|
||||||
FiosFileScanner scanner(fop, callback_proc, file_list);
|
FiosFileScanner scanner(fop, callback_proc, file_list);
|
||||||
if (subdir == NO_DIRECTORY) {
|
if (subdir == NO_DIRECTORY) {
|
||||||
scanner.Scan(nullptr, _fios_path, false);
|
scanner.Scan(nullptr, _fios_path->c_str(), false);
|
||||||
} else {
|
} else {
|
||||||
scanner.Scan(nullptr, subdir, true, true);
|
scanner.Scan(nullptr, subdir, true, true);
|
||||||
}
|
}
|
||||||
|
@ -491,17 +498,11 @@ FiosType FiosGetSavegameListCallback(SaveLoadOperation fop, const char *file, co
|
||||||
*/
|
*/
|
||||||
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
|
void FiosGetSavegameList(SaveLoadOperation fop, FileList &file_list)
|
||||||
{
|
{
|
||||||
static char *fios_save_path = nullptr;
|
static std::optional<std::string> fios_save_path;
|
||||||
static char *fios_save_path_last = nullptr;
|
|
||||||
|
|
||||||
if (fios_save_path == nullptr) {
|
if (!fios_save_path) fios_save_path = FioFindDirectory(SAVE_DIR);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
_fios_path = fios_save_path;
|
_fios_path = &(*fios_save_path);
|
||||||
_fios_path_last = fios_save_path_last;
|
|
||||||
|
|
||||||
FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
|
FiosGetFileList(fop, &FiosGetSavegameListCallback, NO_DIRECTORY, file_list);
|
||||||
}
|
}
|
||||||
|
@ -546,23 +547,15 @@ static FiosType FiosGetScenarioListCallback(SaveLoadOperation fop, const char *f
|
||||||
*/
|
*/
|
||||||
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
|
void FiosGetScenarioList(SaveLoadOperation fop, FileList &file_list)
|
||||||
{
|
{
|
||||||
static char *fios_scn_path = nullptr;
|
static std::optional<std::string> fios_scn_path;
|
||||||
static char *fios_scn_path_last = nullptr;
|
|
||||||
|
|
||||||
/* Copy the default path on first run or on 'New Game' */
|
/* Copy the default path on first run or on 'New Game' */
|
||||||
if (fios_scn_path == nullptr) {
|
if (!fios_scn_path) fios_scn_path = FioFindDirectory(SCENARIO_DIR);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
_fios_path = fios_scn_path;
|
_fios_path = &(*fios_scn_path);
|
||||||
_fios_path_last = fios_scn_path_last;
|
|
||||||
|
|
||||||
char base_path[MAX_PATH];
|
std::string base_path = FioFindDirectory(SCENARIO_DIR);
|
||||||
FioGetDirectory(base_path, lastof(base_path), SCENARIO_DIR);
|
Subdirectory subdir = (fop == SLO_LOAD && base_path == *_fios_path) ? SCENARIO_DIR : NO_DIRECTORY;
|
||||||
|
|
||||||
Subdirectory subdir = (fop == SLO_LOAD && strcmp(base_path, _fios_path) == 0) ? SCENARIO_DIR : NO_DIRECTORY;
|
|
||||||
FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
|
FiosGetFileList(fop, &FiosGetScenarioListCallback, subdir, file_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -593,10 +586,9 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *
|
||||||
bool match = false;
|
bool match = false;
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
FOR_ALL_SEARCHPATHS(sp) {
|
||||||
char buf[MAX_PATH];
|
std::string buf = FioGetDirectory(sp, HEIGHTMAP_DIR);
|
||||||
FioAppendDirectory(buf, lastof(buf), sp, HEIGHTMAP_DIR);
|
|
||||||
|
|
||||||
if (strncmp(buf, it->second.tar_filename, strlen(buf)) == 0) {
|
if (buf.compare(0, buf.size(), it->second.tar_filename, 0, buf.size()) == 0) {
|
||||||
match = true;
|
match = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -617,22 +609,14 @@ static FiosType FiosGetHeightmapListCallback(SaveLoadOperation fop, const char *
|
||||||
*/
|
*/
|
||||||
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
|
void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
|
||||||
{
|
{
|
||||||
static char *fios_hmap_path = nullptr;
|
static std::optional<std::string> fios_hmap_path;
|
||||||
static char *fios_hmap_path_last = nullptr;
|
|
||||||
|
|
||||||
if (fios_hmap_path == nullptr) {
|
if (!fios_hmap_path) fios_hmap_path = FioFindDirectory(HEIGHTMAP_DIR);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
_fios_path = fios_hmap_path;
|
_fios_path = &(*fios_hmap_path);
|
||||||
_fios_path_last = fios_hmap_path_last;
|
|
||||||
|
|
||||||
char base_path[MAX_PATH];
|
std::string base_path = FioFindDirectory(HEIGHTMAP_DIR);
|
||||||
FioGetDirectory(base_path, lastof(base_path), HEIGHTMAP_DIR);
|
Subdirectory subdir = base_path == *_fios_path ? HEIGHTMAP_DIR : NO_DIRECTORY;
|
||||||
|
|
||||||
Subdirectory subdir = strcmp(base_path, _fios_path) == 0 ? HEIGHTMAP_DIR : NO_DIRECTORY;
|
|
||||||
FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
|
FiosGetFileList(fop, &FiosGetHeightmapListCallback, subdir, file_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -642,14 +626,11 @@ void FiosGetHeightmapList(SaveLoadOperation fop, FileList &file_list)
|
||||||
*/
|
*/
|
||||||
const char *FiosGetScreenshotDir()
|
const char *FiosGetScreenshotDir()
|
||||||
{
|
{
|
||||||
static char *fios_screenshot_path = nullptr;
|
static std::optional<std::string> fios_screenshot_path;
|
||||||
|
|
||||||
if (fios_screenshot_path == nullptr) {
|
if (!fios_screenshot_path) fios_screenshot_path = FioFindDirectory(SCREENSHOT_DIR);
|
||||||
fios_screenshot_path = MallocT<char>(MAX_PATH);
|
|
||||||
FioGetDirectory(fios_screenshot_path, fios_screenshot_path + MAX_PATH - 1, SCREENSHOT_DIR);
|
|
||||||
}
|
|
||||||
|
|
||||||
return fios_screenshot_path;
|
return fios_screenshot_path->c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Basic data to distinguish a scenario. Used in the server list window */
|
/** Basic data to distinguish a scenario. Used in the server list window */
|
||||||
|
|
|
@ -368,22 +368,24 @@ public:
|
||||||
|
|
||||||
/* Select the initial directory. */
|
/* Select the initial directory. */
|
||||||
o_dir.type = FIOS_TYPE_DIRECT;
|
o_dir.type = FIOS_TYPE_DIRECT;
|
||||||
|
std::string dir;
|
||||||
switch (this->abstract_filetype) {
|
switch (this->abstract_filetype) {
|
||||||
case FT_SAVEGAME:
|
case FT_SAVEGAME:
|
||||||
FioGetDirectory(o_dir.name, lastof(o_dir.name), SAVE_DIR);
|
dir = FioFindDirectory(SAVE_DIR);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FT_SCENARIO:
|
case FT_SCENARIO:
|
||||||
FioGetDirectory(o_dir.name, lastof(o_dir.name), SCENARIO_DIR);
|
dir = FioFindDirectory(SCENARIO_DIR);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case FT_HEIGHTMAP:
|
case FT_HEIGHTMAP:
|
||||||
FioGetDirectory(o_dir.name, lastof(o_dir.name), HEIGHTMAP_DIR);
|
dir = FioFindDirectory(HEIGHTMAP_DIR);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
strecpy(o_dir.name, _personal_dir.c_str(), lastof(o_dir.name));
|
dir = _personal_dir;
|
||||||
}
|
}
|
||||||
|
strecpy(o_dir.name, dir.c_str(), lastof(o_dir.name));
|
||||||
|
|
||||||
switch (this->fop) {
|
switch (this->fop) {
|
||||||
case SLO_SAVE:
|
case SLO_SAVE:
|
||||||
|
|
|
@ -1048,14 +1048,12 @@ bool MidiFile::WriteSMF(const char *filename)
|
||||||
std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
||||||
{
|
{
|
||||||
if (song.filetype == MTT_STANDARDMIDI) {
|
if (song.filetype == MTT_STANDARDMIDI) {
|
||||||
char filename[MAX_PATH];
|
std::string filename = FioFindFullPath(Subdirectory::BASESET_DIR, song.filename);
|
||||||
if (FioFindFullPath(filename, lastof(filename), Subdirectory::BASESET_DIR, song.filename)) {
|
if (!filename.empty()) return filename;
|
||||||
return std::string(filename);
|
filename = FioFindFullPath(Subdirectory::OLD_GM_DIR, song.filename);
|
||||||
} else if (FioFindFullPath(filename, lastof(filename), Subdirectory::OLD_GM_DIR, song.filename)) {
|
if (!filename.empty()) return filename;
|
||||||
return std::string(filename);
|
|
||||||
} else {
|
return std::string();
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (song.filetype != MTT_MPSMIDI) return std::string();
|
if (song.filetype != MTT_MPSMIDI) return std::string();
|
||||||
|
@ -1077,17 +1075,16 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
||||||
*wp++ = '\0';
|
*wp++ = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
char tempdirname[MAX_PATH];
|
std::string tempdirname = FioGetDirectory(Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR);
|
||||||
FioGetFullPath(tempdirname, lastof(tempdirname), Searchpath::SP_AUTODOWNLOAD_DIR, Subdirectory::BASESET_DIR, basename);
|
tempdirname += basename;
|
||||||
if (!AppendPathSeparator(tempdirname, lastof(tempdirname))) return std::string();
|
AppendPathSeparator(tempdirname);
|
||||||
FioCreateDirectory(tempdirname);
|
FioCreateDirectory(tempdirname.c_str());
|
||||||
|
|
||||||
char output_filename[MAX_PATH];
|
std::string output_filename = tempdirname + std::to_string(song.cat_index) + ".mid";
|
||||||
seprintf(output_filename, lastof(output_filename), "%s%d.mid", tempdirname, song.cat_index);
|
|
||||||
|
|
||||||
if (FileExists(output_filename)) {
|
if (FileExists(output_filename.c_str())) {
|
||||||
/* If the file already exists, assume it's the correct decoded data */
|
/* If the file already exists, assume it's the correct decoded data */
|
||||||
return std::string(output_filename);
|
return output_filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte *data;
|
byte *data;
|
||||||
|
@ -1102,8 +1099,8 @@ std::string MidiFile::GetSMFFile(const MusicSongInfo &song)
|
||||||
}
|
}
|
||||||
free(data);
|
free(data);
|
||||||
|
|
||||||
if (midifile.WriteSMF(output_filename)) {
|
if (midifile.WriteSMF(output_filename.c_str())) {
|
||||||
return std::string(output_filename);
|
return output_filename;
|
||||||
} else {
|
} else {
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
|
@ -385,14 +385,14 @@ void ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(const Co
|
||||||
* @return a statically allocated buffer with the filename or
|
* @return a statically allocated buffer with the filename or
|
||||||
* nullptr when no filename could be made.
|
* nullptr when no filename could be made.
|
||||||
*/
|
*/
|
||||||
static char *GetFullFilename(const ContentInfo *ci, bool compressed)
|
static std::string GetFullFilename(const ContentInfo *ci, bool compressed)
|
||||||
{
|
{
|
||||||
Subdirectory dir = GetContentInfoSubDir(ci->type);
|
Subdirectory dir = GetContentInfoSubDir(ci->type);
|
||||||
if (dir == NO_DIRECTORY) return nullptr;
|
if (dir == NO_DIRECTORY) return {};
|
||||||
|
|
||||||
static char buf[MAX_PATH];
|
std::string buf = FioGetDirectory(SP_AUTODOWNLOAD_DIR, dir);
|
||||||
FioGetFullPath(buf, lastof(buf), SP_AUTODOWNLOAD_DIR, dir, ci->filename);
|
buf += ci->filename;
|
||||||
strecat(buf, compressed ? ".tar.gz" : ".tar", lastof(buf));
|
buf += compressed ? ".tar.gz" : ".tar";
|
||||||
|
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
@ -408,13 +408,13 @@ static bool GunzipFile(const ContentInfo *ci)
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
|
|
||||||
/* Need to open the file with fopen() to support non-ASCII on Windows. */
|
/* Need to open the file with fopen() to support non-ASCII on Windows. */
|
||||||
FILE *ftmp = fopen(GetFullFilename(ci, true), "rb");
|
FILE *ftmp = fopen(GetFullFilename(ci, true).c_str(), "rb");
|
||||||
if (ftmp == nullptr) return false;
|
if (ftmp == nullptr) return false;
|
||||||
/* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
|
/* Duplicate the handle, and close the FILE*, to avoid double-closing the handle later. */
|
||||||
gzFile fin = gzdopen(dup(fileno(ftmp)), "rb");
|
gzFile fin = gzdopen(dup(fileno(ftmp)), "rb");
|
||||||
fclose(ftmp);
|
fclose(ftmp);
|
||||||
|
|
||||||
FILE *fout = fopen(GetFullFilename(ci, false), "wb");
|
FILE *fout = fopen(GetFullFilename(ci, false).c_str(), "wb");
|
||||||
|
|
||||||
if (fin == nullptr || fout == nullptr) {
|
if (fin == nullptr || fout == nullptr) {
|
||||||
ret = false;
|
ret = false;
|
||||||
|
@ -509,8 +509,8 @@ bool ClientNetworkContentSocketHandler::BeforeDownload()
|
||||||
|
|
||||||
if (this->curInfo->filesize != 0) {
|
if (this->curInfo->filesize != 0) {
|
||||||
/* The filesize is > 0, so we are going to download it */
|
/* The filesize is > 0, so we are going to download it */
|
||||||
const char *filename = GetFullFilename(this->curInfo, true);
|
std::string filename = GetFullFilename(this->curInfo, true);
|
||||||
if (filename == nullptr || (this->curFile = fopen(filename, "wb")) == nullptr) {
|
if (filename.empty() || (this->curFile = fopen(filename.c_str(), "wb")) == nullptr) {
|
||||||
/* Unless that fails of course... */
|
/* Unless that fails of course... */
|
||||||
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
|
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
|
||||||
ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
|
ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD, STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD_FILE_NOT_WRITABLE, WL_ERROR);
|
||||||
|
@ -532,18 +532,19 @@ void ClientNetworkContentSocketHandler::AfterDownload()
|
||||||
this->curFile = nullptr;
|
this->curFile = nullptr;
|
||||||
|
|
||||||
if (GunzipFile(this->curInfo)) {
|
if (GunzipFile(this->curInfo)) {
|
||||||
unlink(GetFullFilename(this->curInfo, true));
|
unlink(GetFullFilename(this->curInfo, true).c_str());
|
||||||
|
|
||||||
Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
|
Subdirectory sd = GetContentInfoSubDir(this->curInfo->type);
|
||||||
if (sd == NO_DIRECTORY) NOT_REACHED();
|
if (sd == NO_DIRECTORY) NOT_REACHED();
|
||||||
|
|
||||||
TarScanner ts;
|
TarScanner ts;
|
||||||
ts.AddFile(sd, GetFullFilename(this->curInfo, false));
|
std::string fname = GetFullFilename(this->curInfo, false);
|
||||||
|
ts.AddFile(sd, fname.c_str());
|
||||||
|
|
||||||
if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
|
if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
|
||||||
/* Music can't be in a tar. So extract the tar! */
|
/* Music can't be in a tar. So extract the tar! */
|
||||||
ExtractTar(GetFullFilename(this->curInfo, false), BASESET_DIR);
|
ExtractTar(fname.c_str(), BASESET_DIR);
|
||||||
unlink(GetFullFilename(this->curInfo, false));
|
unlink(fname.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __EMSCRIPTEN__
|
#ifdef __EMSCRIPTEN__
|
||||||
|
|
|
@ -458,24 +458,23 @@ void DetermineBasePaths(const char *exe)
|
||||||
{
|
{
|
||||||
extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
|
extern std::array<std::string, NUM_SEARCHPATHS> _searchpaths;
|
||||||
|
|
||||||
char tmp[MAX_PATH];
|
|
||||||
TCHAR path[MAX_PATH];
|
TCHAR path[MAX_PATH];
|
||||||
#ifdef WITH_PERSONAL_DIR
|
#ifdef WITH_PERSONAL_DIR
|
||||||
if (SUCCEEDED(OTTDSHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) {
|
if (SUCCEEDED(OTTDSHGetFolderPath(nullptr, CSIDL_PERSONAL, nullptr, SHGFP_TYPE_CURRENT, path))) {
|
||||||
strecpy(tmp, FS2OTTD(path), lastof(tmp));
|
std::string tmp(FS2OTTD(path));
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
strecat(tmp, PERSONAL_DIR, lastof(tmp));
|
tmp += PERSONAL_DIR;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
_searchpaths[SP_PERSONAL_DIR] = tmp;
|
||||||
} else {
|
} else {
|
||||||
_searchpaths[SP_PERSONAL_DIR].clear();
|
_searchpaths[SP_PERSONAL_DIR].clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SUCCEEDED(OTTDSHGetFolderPath(nullptr, CSIDL_COMMON_DOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, path))) {
|
if (SUCCEEDED(OTTDSHGetFolderPath(nullptr, CSIDL_COMMON_DOCUMENTS, nullptr, SHGFP_TYPE_CURRENT, path))) {
|
||||||
strecpy(tmp, FS2OTTD(path), lastof(tmp));
|
std::string tmp(FS2OTTD(path));
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
strecat(tmp, PERSONAL_DIR, lastof(tmp));
|
tmp += PERSONAL_DIR;
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
AppendPathSeparator(tmp);
|
||||||
_searchpaths[SP_SHARED_DIR] = tmp;
|
_searchpaths[SP_SHARED_DIR] = tmp;
|
||||||
} else {
|
} else {
|
||||||
_searchpaths[SP_SHARED_DIR].clear();
|
_searchpaths[SP_SHARED_DIR].clear();
|
||||||
|
@ -486,10 +485,11 @@ void DetermineBasePaths(const char *exe)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (_config_file.empty()) {
|
if (_config_file.empty()) {
|
||||||
/* Get the path to working directory of OpenTTD. */
|
char cwd[MAX_PATH];
|
||||||
getcwd(tmp, lengthof(tmp));
|
getcwd(cwd, lengthof(cwd));
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
std::string cwd_s(cwd);
|
||||||
_searchpaths[SP_WORKING_DIR] = tmp;
|
AppendPathSeparator(cwd_s);
|
||||||
|
_searchpaths[SP_WORKING_DIR] = cwd_s;
|
||||||
} else {
|
} else {
|
||||||
/* Use the folder of the config file as working directory. */
|
/* Use the folder of the config file as working directory. */
|
||||||
TCHAR config_dir[MAX_PATH];
|
TCHAR config_dir[MAX_PATH];
|
||||||
|
@ -498,9 +498,10 @@ void DetermineBasePaths(const char *exe)
|
||||||
DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
|
DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
|
||||||
_searchpaths[SP_WORKING_DIR].clear();
|
_searchpaths[SP_WORKING_DIR].clear();
|
||||||
} else {
|
} else {
|
||||||
strecpy(tmp, convert_from_fs(config_dir, tmp, lengthof(tmp)), lastof(tmp));
|
std::string tmp(FS2OTTD(config_dir));
|
||||||
char *s = strrchr(tmp, PATHSEPCHAR);
|
auto pos = tmp.find_last_of(PATHSEPCHAR);
|
||||||
*(s + 1) = '\0';
|
if (pos != std::string::npos) tmp.erase(pos + 1);
|
||||||
|
|
||||||
_searchpaths[SP_WORKING_DIR] = tmp;
|
_searchpaths[SP_WORKING_DIR] = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -515,9 +516,10 @@ void DetermineBasePaths(const char *exe)
|
||||||
DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
|
DEBUG(misc, 0, "GetFullPathName failed (%lu)\n", GetLastError());
|
||||||
_searchpaths[SP_BINARY_DIR].clear();
|
_searchpaths[SP_BINARY_DIR].clear();
|
||||||
} else {
|
} else {
|
||||||
strecpy(tmp, convert_from_fs(exec_dir, tmp, lengthof(tmp)), lastof(tmp));
|
std::string tmp(FS2OTTD(exec_dir));
|
||||||
char *s = strrchr(tmp, PATHSEPCHAR);
|
auto pos = tmp.find_last_of(PATHSEPCHAR);
|
||||||
*(s + 1) = '\0';
|
if (pos != std::string::npos) tmp.erase(pos + 1);
|
||||||
|
|
||||||
_searchpaths[SP_BINARY_DIR] = tmp;
|
_searchpaths[SP_BINARY_DIR] = tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,17 +116,16 @@ bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirect
|
||||||
{
|
{
|
||||||
char script_name[32];
|
char script_name[32];
|
||||||
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
|
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
|
||||||
char buf[MAX_PATH];
|
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
FOR_ALL_SEARCHPATHS(sp) {
|
||||||
FioAppendDirectory(buf, lastof(buf), sp, dir);
|
std::string buf = FioGetDirectory(sp, dir);
|
||||||
strecat(buf, script_name, lastof(buf));
|
buf += script_name;
|
||||||
if (!FileExists(buf)) continue;
|
if (!FileExists(buf.c_str())) continue;
|
||||||
|
|
||||||
if (this->engine->LoadScript(buf)) return true;
|
if (this->engine->LoadScript(buf.c_str())) return true;
|
||||||
|
|
||||||
ScriptLog::Error("Failed to load API compatibility script");
|
ScriptLog::Error("Failed to load API compatibility script");
|
||||||
DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf);
|
DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1959,9 +1959,8 @@ void InitializeLanguagePacks()
|
||||||
Searchpath sp;
|
Searchpath sp;
|
||||||
|
|
||||||
FOR_ALL_SEARCHPATHS(sp) {
|
FOR_ALL_SEARCHPATHS(sp) {
|
||||||
char path[MAX_PATH];
|
std::string path = FioGetDirectory(sp, LANG_DIR);
|
||||||
FioAppendDirectory(path, lastof(path), sp, LANG_DIR);
|
GetLanguageList(path.c_str());
|
||||||
GetLanguageList(path);
|
|
||||||
}
|
}
|
||||||
if (_languages.size() == 0) usererror("No available language packs (invalid versions?)");
|
if (_languages.size() == 0) usererror("No available language packs (invalid versions?)");
|
||||||
|
|
||||||
|
|
|
@ -551,8 +551,8 @@ void cocoaSetApplicationBundleDir()
|
||||||
char tmp[MAXPATHLEN];
|
char tmp[MAXPATHLEN];
|
||||||
CFAutoRelease<CFURLRef> url(CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()));
|
CFAutoRelease<CFURLRef> url(CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()));
|
||||||
if (CFURLGetFileSystemRepresentation(url.get(), true, (unsigned char*)tmp, MAXPATHLEN)) {
|
if (CFURLGetFileSystemRepresentation(url.get(), true, (unsigned char*)tmp, MAXPATHLEN)) {
|
||||||
AppendPathSeparator(tmp, lastof(tmp));
|
|
||||||
_searchpaths[SP_APPLICATION_BUNDLE_DIR] = tmp;
|
_searchpaths[SP_APPLICATION_BUNDLE_DIR] = tmp;
|
||||||
|
AppendPathSeparator(_searchpaths[SP_APPLICATION_BUNDLE_DIR]);
|
||||||
} else {
|
} else {
|
||||||
_searchpaths[SP_APPLICATION_BUNDLE_DIR].clear();
|
_searchpaths[SP_APPLICATION_BUNDLE_DIR].clear();
|
||||||
}
|
}
|
||||||
|
|
|
@ -285,10 +285,10 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char icon_path[MAX_PATH];
|
std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
|
||||||
if (FioFindFullPath(icon_path, lastof(icon_path), BASESET_DIR, "openttd.32.bmp") != nullptr) {
|
if (!icon_path.empty()) {
|
||||||
/* Give the application an icon */
|
/* Give the application an icon */
|
||||||
SDL_Surface *icon = SDL_LoadBMP(icon_path);
|
SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
|
||||||
if (icon != nullptr) {
|
if (icon != nullptr) {
|
||||||
/* Get the colourkey, which will be magenta */
|
/* Get the colourkey, which will be magenta */
|
||||||
uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
|
uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
|
||||||
|
|
|
@ -266,10 +266,10 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
|
||||||
|
|
||||||
if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
|
if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
|
||||||
|
|
||||||
char icon_path[MAX_PATH];
|
std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
|
||||||
if (FioFindFullPath(icon_path, lastof(icon_path), BASESET_DIR, "openttd.32.bmp") != nullptr) {
|
if (!icon_path.empty()) {
|
||||||
/* Give the application an icon */
|
/* Give the application an icon */
|
||||||
icon = SDL_LoadBMP(icon_path);
|
icon = SDL_LoadBMP(icon_path.c_str());
|
||||||
if (icon != nullptr) {
|
if (icon != nullptr) {
|
||||||
/* Get the colourkey, which will be magenta */
|
/* Get the colourkey, which will be magenta */
|
||||||
uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
|
uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
|
||||||
|
|
Loading…
Reference in New Issue