src/newgrf_config.cpp
changeset 7330 577e92774f23
parent 6977 67b989528f3d
child 7369 26325201d4d7
equal deleted inserted replaced
7329:e7b5256df033 7330:577e92774f23
    38 	md5_state_t md5state;
    38 	md5_state_t md5state;
    39 	md5_byte_t buffer[1024];
    39 	md5_byte_t buffer[1024];
    40 	size_t len;
    40 	size_t len;
    41 
    41 
    42 	/* open the file */
    42 	/* open the file */
    43 	f = fopen(config->full_path, "rb");
    43 	f = FioFOpenFile(config->full_path);
    44 	if (f == NULL) return false;
    44 	if (f == NULL) return false;
    45 
    45 
    46 	/* calculate md5sum */
    46 	/* calculate md5sum */
    47 	md5_init(&md5state);
    47 	md5_init(&md5state);
    48 	while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
    48 	while ((len = fread(buffer, 1, sizeof(buffer), f)) != 0) {
    57 
    57 
    58 
    58 
    59 /* Find the GRFID and calculate the md5sum */
    59 /* Find the GRFID and calculate the md5sum */
    60 bool FillGRFDetails(GRFConfig *config, bool is_static)
    60 bool FillGRFDetails(GRFConfig *config, bool is_static)
    61 {
    61 {
    62 	if (!FileExists(config->full_path)) {
    62 	if (!FioCheckFileExists(config->full_path)) {
    63 		config->status = GCS_NOT_FOUND;
    63 		config->status = GCS_NOT_FOUND;
    64 		return false;
    64 		return false;
    65 	}
    65 	}
    66 
    66 
    67 	if (config->filename == NULL) {
    67 	if (config->filename == NULL) {
    68 		config->filename = strdup(strrchr(config->full_path, PATHSEPCHAR) + 1);
    68 		const char *t = strrchr(config->full_path, PATHSEPCHAR);
       
    69 		config->filename = strdup(t != NULL ? t + 1 : config->full_path);
    69 	}
    70 	}
    70 
    71 
    71 	/* Find and load the Action 8 information */
    72 	/* Find and load the Action 8 information */
    72 	/* 62 is the last file slot before sample.cat.
    73 	/* 62 is the last file slot before sample.cat.
    73 	 * Should perhaps be some "don't care" value */
    74 	 * Should perhaps be some "don't care" value */
   276 
   277 
   277 
   278 
   278 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
   279 extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb);
   279 
   280 
   280 /* Scan a path for NewGRFs */
   281 /* Scan a path for NewGRFs */
   281 static uint ScanPath(const char *path)
   282 static uint ScanPath(const char *path, int basepath_length)
   282 {
   283 {
   283 	uint num = 0;
   284 	uint num = 0;
   284 	struct stat sb;
   285 	struct stat sb;
   285 	struct dirent *dirent;
   286 	struct dirent *dirent;
   286 	DIR *dir;
   287 	DIR *dir;
   297 
   298 
   298 		if (sb.st_mode & S_IFDIR) {
   299 		if (sb.st_mode & S_IFDIR) {
   299 			/* Directory */
   300 			/* Directory */
   300 			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
   301 			if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue;
   301 			AppendPathSeparator(filename, lengthof(filename));
   302 			AppendPathSeparator(filename, lengthof(filename));
   302 			num += ScanPath(filename);
   303 			num += ScanPath(filename, basepath_length);
   303 		} else if (sb.st_mode & S_IFREG) {
   304 		} else if (sb.st_mode & S_IFREG) {
   304 			/* File */
   305 			/* File */
   305 			char *ext = strrchr(filename, '.');
   306 			char *ext = strrchr(filename, '.');
   306 
   307 
   307 			/* If no extension or extension isn't .grf, skip the file */
   308 			/* If no extension or extension isn't .grf, skip the file */
   308 			if (ext == NULL) continue;
   309 			if (ext == NULL) continue;
   309 			if (strcasecmp(ext, ".grf") != 0) continue;
   310 			if (strcasecmp(ext, ".grf") != 0) continue;
   310 
   311 
   311 			GRFConfig *c = CallocT<GRFConfig>(1);
   312 			GRFConfig *c = CallocT<GRFConfig>(1);
   312 			c->full_path = strdup(filename);
   313 			c->full_path = strdup(filename + basepath_length);
   313 
   314 
   314 			bool added = true;
   315 			bool added = true;
   315 			if (FillGRFDetails(c, false)) {
   316 			if (FillGRFDetails(c, false)) {
   316 				if (_all_grfs == NULL) {
   317 				if (_all_grfs == NULL) {
   317 					_all_grfs = c;
   318 					_all_grfs = c;
   358 	uint num;
   359 	uint num;
   359 
   360 
   360 	ClearGRFConfigList(&_all_grfs);
   361 	ClearGRFConfigList(&_all_grfs);
   361 
   362 
   362 	DEBUG(grf, 1, "Scanning for NewGRFs");
   363 	DEBUG(grf, 1, "Scanning for NewGRFs");
   363 	num  = ScanPath(_paths.data_dir);
   364 	num  = ScanPath(_paths.data_dir, strlen(_paths.data_dir));
   364 	num += ScanPath(_paths.second_data_dir);
   365 	if (_paths.second_data_dir != NULL) {
       
   366 		num += ScanPath(_paths.second_data_dir, strlen(_paths.second_data_dir));
       
   367 	}
   365 	DEBUG(grf, 1, "Scan complete, found %d files", num);
   368 	DEBUG(grf, 1, "Scan complete, found %d files", num);
   366 }
   369 }
   367 
   370 
   368 
   371 
   369 /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
   372 /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/