src/newgrf_config.cpp
branchnoai
changeset 9704 197cb8c6ae17
parent 9703 d2a6acdbd665
child 9722 ebf0ece7d8f6
equal deleted inserted replaced
9703:d2a6acdbd665 9704:197cb8c6ae17
   369 	if (ScanPathAddGrf(filename)) num++;
   369 	if (ScanPathAddGrf(filename)) num++;
   370 
   370 
   371 	return num;
   371 	return num;
   372 }
   372 }
   373 
   373 
       
   374 /**
       
   375  * Simple sorter for GRFS
       
   376  * @param p1 the first GRFConfig *
       
   377  * @param p2 the second GRFConfig *
       
   378  * @return the same strcmp would return for the name of the NewGRF.
       
   379  */
       
   380 static int CDECL GRFSorter(const void *p1, const void *p2)
       
   381 {
       
   382 	const GRFConfig *c1 = *(const GRFConfig **)p1;
       
   383 	const GRFConfig *c2 = *(const GRFConfig **)p2;
       
   384 
       
   385 	return strcmp(c1->name != NULL ? c1->name : c1->filename,
       
   386 		c2->name != NULL ? c2->name : c2->filename);
       
   387 }
       
   388 
   374 /* Scan for all NewGRFs */
   389 /* Scan for all NewGRFs */
   375 void ScanNewGRFFiles()
   390 void ScanNewGRFFiles()
   376 {
   391 {
   377 	Searchpath sp;
   392 	Searchpath sp;
   378 	char path[MAX_PATH];
   393 	char path[MAX_PATH];
   387 		num += ScanPath(path, strlen(path));
   402 		num += ScanPath(path, strlen(path));
   388 	}
   403 	}
   389 	FOR_ALL_TARS(tar) {
   404 	FOR_ALL_TARS(tar) {
   390 		num += ScanTar(tar);
   405 		num += ScanTar(tar);
   391 	}
   406 	}
       
   407 
   392 	DEBUG(grf, 1, "Scan complete, found %d files", num);
   408 	DEBUG(grf, 1, "Scan complete, found %d files", num);
       
   409 	if (num == 0 || _all_grfs == NULL) return;
       
   410 
       
   411 	/* Sort the linked list using quicksort.
       
   412 	 * For that we first have to make an array, the qsort and
       
   413 	 * then remake the linked list. */
       
   414 	GRFConfig **to_sort = MallocT<GRFConfig*>(num);
       
   415 	if (to_sort == NULL) return; // No memory, then don't sort
       
   416 
       
   417 	uint i = 0;
       
   418 	for (GRFConfig *p = _all_grfs; p != NULL; p = p->next, i++) {
       
   419 		to_sort[i] = p;
       
   420 	}
       
   421 	/* Number of files is not necessarily right */
       
   422 	num = i;
       
   423 
       
   424 	qsort(to_sort, num, sizeof(GRFConfig*), GRFSorter);
       
   425 
       
   426 	for (i = 1; i < num; i++) {
       
   427 		to_sort[i - 1]->next = to_sort[i];
       
   428 	}
       
   429 	to_sort[num - 1]->next = NULL;
       
   430 	_all_grfs = to_sort[0];
   393 }
   431 }
   394 
   432 
   395 
   433 
   396 /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
   434 /* Find a NewGRF in the scanned list, if md5sum is NULL, we don't care about it*/
   397 const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)
   435 const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum)