Changeset - r15138:94bac05d24df
[Not reviewed]
master
0 3 0
rubidium - 14 years ago 2010-05-10 09:37:17
rubidium@openttd.org
(svn r19777) -Change: use the file scanner to find the .tars
3 files changed with 23 insertions and 64 deletions:
0 comments (0 inline, 0 general)
src/fileio.cpp
Show inline comments
 
@@ -531,13 +531,24 @@ static void SimplifyFileName(char *name)
 
	/* Tar-files always have '/' path-seperator, but we want our PATHSEPCHAR */
 
#if (PATHSEPCHAR != '/')
 
	for (char *n = name; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
 
#endif
 
}
 

	
 
bool TarListAddFile(const char *filename)
 
/* static */ uint TarScanner::DoScan() {
 
	DEBUG(misc, 1, "Scanning for tars");
 
	TarScanner fs;
 
	uint num = fs.Scan(".tar", DATA_DIR, false);
 
	num += fs.Scan(".tar", AI_DIR, false);
 
	num += fs.Scan(".tar", AI_LIBRARY_DIR, false);
 
	num += fs.Scan(".tar", SCENARIO_DIR, false);
 
	DEBUG(misc, 1, "Scan complete, found %d files", num);
 
	return num;
 
}
 

	
 
bool TarScanner::AddFile(const char *filename, size_t basepath_length)
 
{
 
	/* The TAR-header, repeated for every file */
 
	typedef struct TarHeader {
 
		char name[100];      ///< Name of the file
 
		char mode[8];
 
		char uid[8];
 
@@ -817,72 +828,12 @@ bool ExtractTar(const char *tar_filename
 
	}
 

	
 
	DEBUG(misc, 9, "  extraction successful");
 
	return true;
 
}
 

	
 
static int ScanPathForTarFiles(const char *path, size_t basepath_length)
 
{
 
	extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
 

	
 
	uint num = 0;
 
	struct stat sb;
 
	struct dirent *dirent;
 
	DIR *dir;
 

	
 
	if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0;
 

	
 
	while ((dirent = readdir(dir)) != NULL) {
 
		const char *d_name = FS2OTTD(dirent->d_name);
 
		char filename[MAX_PATH];
 

	
 
		if (!FiosIsValidFile(path, dirent, &sb)) continue;
 

	
 
		snprintf(filename, lengthof(filename), "%s%s", path, d_name);
 

	
 
		if (S_ISDIR(sb.st_mode)) {
 
			/* Directory */
 
			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
 
			AppendPathSeparator(filename, lengthof(filename));
 
			num += ScanPathForTarFiles(filename, basepath_length);
 
		} else if (S_ISREG(sb.st_mode)) {
 
			/* File */
 
			char *ext = strrchr(filename, '.');
 

	
 
			/* If no extension or extension isn't .tar, skip the file */
 
			if (ext == NULL) continue;
 
			if (strcasecmp(ext, ".tar") != 0) continue;
 

	
 
			if (TarListAddFile(filename)) num++;
 
		}
 
	}
 

	
 
	closedir(dir);
 
	return num;
 
}
 

	
 
void ScanForTarFiles()
 
{
 
	Searchpath sp;
 
	char path[MAX_PATH];
 
	uint num = 0;
 

	
 
	DEBUG(misc, 1, "Scanning for tars");
 
	FOR_ALL_SEARCHPATHS(sp) {
 
		FioAppendDirectory(path, MAX_PATH, sp, DATA_DIR);
 
		num += ScanPathForTarFiles(path, strlen(path));
 
		FioAppendDirectory(path, MAX_PATH, sp, AI_DIR);
 
		num += ScanPathForTarFiles(path, strlen(path));
 
		FioAppendDirectory(path, MAX_PATH, sp, AI_LIBRARY_DIR);
 
		num += ScanPathForTarFiles(path, strlen(path));
 
		FioAppendDirectory(path, MAX_PATH, sp, SCENARIO_DIR);
 
		num += ScanPathForTarFiles(path, strlen(path));
 
	}
 
	DEBUG(misc, 1, "Scan complete, found %d files", num);
 
}
 

	
 
#if defined(WIN32) || defined(WINCE)
 
/**
 
 * Determine the base (personal dir and game data dir) paths
 
 * @param exe the path from the current path to the executable
 
 * @note defined in the OS related files (os2.cpp, win32.cpp, unix.cpp etc)
 
 */
 
@@ -1076,13 +1027,13 @@ void DeterminePaths(const char *exe)
 
	if (!FileExists(_searchpaths[SP_AUTODOWNLOAD_DIR]))  {
 
		free((void*)_searchpaths[SP_AUTODOWNLOAD_DIR]);
 
		_searchpaths[SP_AUTODOWNLOAD_DIR] = NULL;
 
	}
 
#endif /* ENABLE_NETWORK */
 

	
 
	ScanForTarFiles();
 
	TarScanner::DoScan();
 
}
 

	
 
/**
 
 * Sanitizes a filename, i.e. removes all illegal characters from it.
 
 * @param filename the "\0" terminated filename
 
 */
src/fileio_func.h
Show inline comments
 
@@ -84,12 +84,20 @@ public:
 
	 *                        filename relative to the search path.
 
	 * @return true if the file is added.
 
	 */
 
	virtual bool AddFile(const char *filename, size_t basepath_length) = 0;
 
};
 

	
 
/** Helper for scanning for files with tar as extension */
 
class TarScanner : FileScanner {
 
public:
 
	/* virtual */ bool AddFile(const char *filename, size_t basepath_length);
 

	
 
	/** Do the scan for Tars. */
 
	static uint DoScan();
 
};
 

	
 
/* Implementation of opendir/readdir/closedir for Windows */
 
#if defined(WIN32)
 
#include <windows.h>
 
struct DIR;
 

	
src/network/network_content.cpp
Show inline comments
 
@@ -24,13 +24,12 @@
 
#include "table/strings.h"
 

	
 
#if defined(WITH_ZLIB)
 
#include <zlib.h>
 
#endif
 

	
 
extern bool TarListAddFile(const char *filename);
 
extern bool HasScenario(const ContentInfo *ci, bool md5sum);
 
ClientNetworkContentSocketHandler _network_content_client;
 

	
 
/** Wrapper function for the HasProc */
 
static bool HasGRFConfig(const ContentInfo *ci, bool md5sum)
 
{
 
@@ -495,13 +494,14 @@ void ClientNetworkContentSocketHandler::
 
	fclose(this->curFile);
 
	this->curFile = NULL;
 

	
 
	if (GunzipFile(this->curInfo)) {
 
		unlink(GetFullFilename(this->curInfo, true));
 

	
 
		TarListAddFile(GetFullFilename(this->curInfo, false));
 
		TarScanner ts;
 
		ts.AddFile(GetFullFilename(this->curInfo, false), 0);
 

	
 
		if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) {
 
			/* Music can't be in a tar. So extract the tar! */
 
			ExtractTar(GetFullFilename(this->curInfo, false));
 
			unlink(GetFullFilename(this->curInfo, false));
 
		}
0 comments (0 inline, 0 general)