Files @ r24498:e9114d9ab04a
Branch filter:

Location: cpp/openttd-patchpack/source/src/fileio_func.h - annotation

Patric Stout
Fix #6468: don't store version of AIs-started-via-console in name

You can do: "startai myai.3", which starts version 3 of "myai".
This is very useful for testing save/load code between different
versions of your AI.

However, when using this syntax, the AI got saved as "myai.3" as
name of the AI, instead of "myai". This caused several problems,
like indicating to the user the AI could not be found, but still
load the AI. But in all cases, the AI never got the chance to
load the saved data, making the whole reason this exists pointless.

By splitting the name and version already in the console command,
the code becomes simpler and AIs started this way now follow the
normal flow after initialization.
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r18371:8ec67d29c1db
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r18006:071d51c996ad
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r17674:8ba1e586d6d1
r10039:727fb45b0424
r23607:36c15679007d
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r23607:36c15679007d
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r23607:36c15679007d
r18008:ce1722c7e707
r21390:b979565a1681
r21390:b979565a1681
r21390:b979565a1681
r21390:b979565a1681
r22887:895b475b0c41
r10039:727fb45b0424
r19849:42900960d059
r19849:42900960d059
r10039:727fb45b0424
r21390:b979565a1681
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r18370:7fbb35cad336
r10039:727fb45b0424
r20927:6e37de998bbe
r10039:727fb45b0424
r10039:727fb45b0424
r18016:ed7b28b213ba
r18016:ed7b28b213ba
r18016:ed7b28b213ba
r10039:727fb45b0424
r10047:088b8b265ea0
r10047:088b8b265ea0
r10047:088b8b265ea0
r11252:706b96dca92b
r11252:706b96dca92b
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r18073:853f8705886d
r10039:727fb45b0424
r10039:727fb45b0424
r18073:853f8705886d
r10039:727fb45b0424
r10039:727fb45b0424
r15138:94bac05d24df
r15138:94bac05d24df
r18370:7fbb35cad336
r15138:94bac05d24df
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r18747:12d6e8d373e7
r18747:12d6e8d373e7
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r23607:36c15679007d
r15138:94bac05d24df
r18388:b05150903c57
r18388:b05150903c57
r15138:94bac05d24df
r18371:8ec67d29c1db
r15138:94bac05d24df
r10039:727fb45b0424
r18371:8ec67d29c1db
r18371:8ec67d29c1db
r10039:727fb45b0424
r23088:6710c4c79ac6
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r23088:6710c4c79ac6
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r10039:727fb45b0424
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r22875:0dc0685c0eb6
r24219:58395a92e207
r24219:58395a92e207
r24219:58395a92e207
r24219:58395a92e207
r24219:58395a92e207
r24219:58395a92e207
r24219:58395a92e207
r24219:58395a92e207
r10039:727fb45b0424
/*
 * 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 fileio_func.h Functions for Standard In/Out file operations */

#ifndef FILEIO_FUNC_H
#define FILEIO_FUNC_H

#include "core/enum_type.hpp"
#include "fileio_type.h"

void FioSeekTo(size_t pos, int mode);
void FioSeekToFile(uint8 slot, size_t pos);
size_t FioGetPos();
const char *FioGetFilename(uint8 slot);
byte FioReadByte();
uint16 FioReadWord();
uint32 FioReadDword();
void FioCloseAll();
void FioOpenFile(int slot, const char *filename, Subdirectory subdir);
void FioReadBlock(void *ptr, size_t size);
void FioSkipBytes(int n);

/**
 * The search paths OpenTTD could search through.
 * At least one of the slots has to be filled with a path.
 * nullptr paths tell that there is no such path for the
 * current operating system.
 */
extern const char *_searchpaths[NUM_SEARCHPATHS];

/**
 * Checks whether the given search path is a valid search path
 * @param sp the search path to check
 * @return true if the search path is valid
 */
static inline bool IsValidSearchPath(Searchpath sp)
{
	return sp < NUM_SEARCHPATHS && _searchpaths[sp] != nullptr;
}

/** Iterator for all the search paths */
#define FOR_ALL_SEARCHPATHS(sp) for (sp = SP_FIRST_DIR; sp < NUM_SEARCHPATHS; sp++) if (IsValidSearchPath(sp))

void FioFCloseFile(FILE *f);
FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize = nullptr);
bool FioCheckFileExists(const char *filename, Subdirectory subdir);
char *FioGetFullPath(char *buf, const char *last, Searchpath sp, Subdirectory subdir, const char *filename);
char *FioFindFullPath(char *buf, const char *last, Subdirectory subdir, const char *filename);
char *FioAppendDirectory(char *buf, const char *last, Searchpath sp, Subdirectory subdir);
char *FioGetDirectory(char *buf, const char *last, Subdirectory subdir);
void FioCreateDirectory(const char *name);

const char *FiosGetScreenshotDir();

void SanitizeFilename(char *filename);
bool AppendPathSeparator(char *buf, const char *last);
void DeterminePaths(const char *exe);
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
bool FileExists(const char *filename);
bool ExtractTar(const char *tar_filename, Subdirectory subdir);

extern const char *_personal_dir; ///< custom directory for personal settings, saves, newgrf, etc.

/** Helper for scanning for files with a given name */
class FileScanner {
protected:
	Subdirectory subdir; ///< The current sub directory we are searching through
public:
	/** Destruct the proper one... */
	virtual ~FileScanner() {}

	uint Scan(const char *extension, Subdirectory sd, bool tars = true, bool recursive = true);
	uint Scan(const char *extension, const char *directory, bool recursive = true);

	/**
	 * Add a file 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
	 *                        filename relative to the search path.
	 * @param tar_filename    the name of the tar file the file is read from.
	 * @return true if the file is added.
	 */
	virtual bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename) = 0;
};

/** Helper for scanning for files with tar as extension */
class TarScanner : FileScanner {
	uint DoScan(Subdirectory sd);
public:
	/** The mode of tar scanning. */
	enum Mode {
		NONE     = 0,      ///< Scan nothing.
		BASESET  = 1 << 0, ///< Scan for base sets.
		NEWGRF   = 1 << 1, ///< Scan for non-base sets.
		AI       = 1 << 2, ///< Scan for AIs and its libraries.
		SCENARIO = 1 << 3, ///< Scan for scenarios and heightmaps.
		GAME     = 1 << 4, ///< Scan for game scripts.
		ALL      = BASESET | NEWGRF | AI | SCENARIO | GAME, ///< Scan for everything.
	};

	bool AddFile(const char *filename, size_t basepath_length, const char *tar_filename = nullptr) override;

	bool AddFile(Subdirectory sd, const char *filename);

	/** Do the scan for Tars. */
	static uint DoScan(TarScanner::Mode mode);
};

DECLARE_ENUM_AS_BIT_SET(TarScanner::Mode)

/* Implementation of opendir/readdir/closedir for Windows */
#if defined(_WIN32)
struct DIR;

struct dirent { // XXX - only d_name implemented
	TCHAR *d_name; // name of found file
	/* little hack which will point to parent DIR struct which will
	 * save us a call to GetFileAttributes if we want information
	 * about the file (for example in function fio_bla) */
	DIR *dir;
};

DIR *opendir(const TCHAR *path);
struct dirent *readdir(DIR *d);
int closedir(DIR *d);
#else
/* Use system-supplied opendir/readdir/closedir functions */
# include <sys/types.h>
# include <dirent.h>
#endif /* defined(_WIN32) */

/**
 * A wrapper around opendir() which will convert the string from
 * OPENTTD encoding to that of the filesystem. For all purposes this
 * function behaves the same as the original opendir function
 * @param path string to open directory of
 * @return DIR pointer
 */
static inline DIR *ttd_opendir(const char *path)
{
	return opendir(OTTD2FS(path));
}


/** Auto-close a file upon scope exit. */
class FileCloser {
	FILE *f;

public:
	FileCloser(FILE *_f) : f(_f) {}
	~FileCloser()
	{
		fclose(f);
	}
};

/** Helper to manage a FILE with a \c std::unique_ptr. */
struct FileDeleter {
	void operator()(FILE *f)
	{
		if (f) fclose(f);
	}
};

#endif /* FILEIO_FUNC_H */