src/newgrf_config.cpp
branchcpp_gui
changeset 6268 4b5241e5dd10
parent 6016 9d7b851ffe1c
child 6285 187e3ef04cc9
equal deleted inserted replaced
6267:7c8ec33959b1 6268:4b5241e5dd10
    14 #include "newgrf_config.h"
    14 #include "newgrf_config.h"
    15 #include "helpers.hpp"
    15 #include "helpers.hpp"
    16 
    16 
    17 #include "fileio.h"
    17 #include "fileio.h"
    18 #include "fios.h"
    18 #include "fios.h"
    19 #include <sys/types.h>
       
    20 #include <sys/stat.h>
    19 #include <sys/stat.h>
    21 
    20 
    22 #ifdef WIN32
    21 #ifdef WIN32
    23 # include <io.h>
    22 # include <io.h>
    24 #else
       
    25 # include <unistd.h>
       
    26 # include <dirent.h>
       
    27 #endif /* WIN32 */
    23 #endif /* WIN32 */
    28 
    24 
    29 
    25 
    30 GRFConfig *_all_grfs;
    26 GRFConfig *_all_grfs;
    31 GRFConfig *_grfconfig;
    27 GRFConfig *_grfconfig;
    93 	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
    89 	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
    94 	if (!HASBIT((*config)->flags, GCF_COPY)) {
    90 	if (!HASBIT((*config)->flags, GCF_COPY)) {
    95 		free((*config)->filename);
    91 		free((*config)->filename);
    96 		free((*config)->name);
    92 		free((*config)->name);
    97 		free((*config)->info);
    93 		free((*config)->info);
       
    94 		free((*config)->error);
    98 	}
    95 	}
    99 	free(*config);
    96 	free(*config);
   100 	*config = NULL;
    97 	*config = NULL;
   101 }
    98 }
   102 
    99 
   113 }
   110 }
   114 
   111 
   115 
   112 
   116 /** Copy a GRF Config list
   113 /** Copy a GRF Config list
   117  * @param dst pointer to destination list
   114  * @param dst pointer to destination list
   118  * @param srt pointer to source list values
   115  * @param src pointer to source list values
   119  * @return pointer to the last value added to the destination list */
   116  * @return pointer to the last value added to the destination list */
   120 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src)
   117 GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src)
   121 {
   118 {
   122 	/* Clear destination as it will be overwritten */
   119 	/* Clear destination as it will be overwritten */
   123 	ClearGRFConfigList(dst);
   120 	ClearGRFConfigList(dst);
   125 		GRFConfig *c = CallocT<GRFConfig>(1);
   122 		GRFConfig *c = CallocT<GRFConfig>(1);
   126 		*c = *src;
   123 		*c = *src;
   127 		if (src->filename != NULL) c->filename = strdup(src->filename);
   124 		if (src->filename != NULL) c->filename = strdup(src->filename);
   128 		if (src->name     != NULL) c->name     = strdup(src->name);
   125 		if (src->name     != NULL) c->name     = strdup(src->name);
   129 		if (src->info     != NULL) c->info     = strdup(src->info);
   126 		if (src->info     != NULL) c->info     = strdup(src->info);
       
   127 		if (src->error    != NULL) {
       
   128 			c->error = CallocT<GRFError>(1);
       
   129 			memcpy(c->error, src->error, sizeof(GRFError));
       
   130 		}
   130 
   131 
   131 		*dst = c;
   132 		*dst = c;
   132 		dst = &c->next;
   133 		dst = &c->next;
   133 	}
   134 	}
   134 
   135 
   174 {
   175 {
   175 	GRFConfig **tail = dst;
   176 	GRFConfig **tail = dst;
   176 	while (*tail != NULL) tail = &(*tail)->next;
   177 	while (*tail != NULL) tail = &(*tail)->next;
   177 
   178 
   178 	CopyGRFConfigList(tail, _grfconfig_static);
   179 	CopyGRFConfigList(tail, _grfconfig_static);
       
   180 	RemoveDuplicatesFromGRFConfigList(*dst);
       
   181 }
       
   182 
       
   183 /** Appends an element to a list of GRFs
       
   184  * @param dst the head of the list to add to */
       
   185 void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
       
   186 {
       
   187 	GRFConfig **tail = dst;
       
   188 	while (*tail != NULL) tail = &(*tail)->next;
       
   189 	*tail = el;
       
   190 
   179 	RemoveDuplicatesFromGRFConfigList(*dst);
   191 	RemoveDuplicatesFromGRFConfigList(*dst);
   180 }
   192 }
   181 
   193 
   182 
   194 
   183 /* Reset the current GRF Config to either blank or newgame settings */
   195 /* Reset the current GRF Config to either blank or newgame settings */
   243 				free(c->filename);
   255 				free(c->filename);
   244 				c->filename = strdup(f->filename);
   256 				c->filename = strdup(f->filename);
   245 				memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
   257 				memcpy(c->md5sum, f->md5sum, sizeof(c->md5sum));
   246 				if (c->name == NULL) c->name = strdup(f->name);
   258 				if (c->name == NULL) c->name = strdup(f->name);
   247 				if (c->info == NULL) c->info = strdup(f->info);
   259 				if (c->info == NULL) c->info = strdup(f->info);
       
   260 				c->error = NULL;
   248 			}
   261 			}
   249 		}
   262 		}
   250 	}
   263 	}
   251 
   264 
   252 	return res;
   265 	return res;
   261 	uint num = 0;
   274 	uint num = 0;
   262 	struct stat sb;
   275 	struct stat sb;
   263 	struct dirent *dirent;
   276 	struct dirent *dirent;
   264 	DIR *dir;
   277 	DIR *dir;
   265 
   278 
   266 	if ((dir = opendir(path)) == NULL) return 0;
   279 	if ((dir = ttd_opendir(path)) == NULL) return 0;
   267 
   280 
   268 	while ((dirent = readdir(dir)) != NULL) {
   281 	while ((dirent = readdir(dir)) != NULL) {
   269 		const char *d_name = FS2OTTD(dirent->d_name);
   282 		const char *d_name = FS2OTTD(dirent->d_name);
   270 		char filename[MAX_PATH];
   283 		char filename[MAX_PATH];
   271 
   284 
   439 };
   452 };
   440 
   453 
   441 
   454 
   442 static void Save_NGRF(void)
   455 static void Save_NGRF(void)
   443 {
   456 {
   444 	GRFConfig *c;
       
   445 	int index = 0;
   457 	int index = 0;
   446 
   458 
   447 	for (c = _grfconfig; c != NULL; c = c->next) {
   459 	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
   448 		if (HASBIT(c->flags, GCF_STATIC)) continue;
   460 		if (HASBIT(c->flags, GCF_STATIC)) continue;
   449 		SlSetArrayIndex(index++);
   461 		SlSetArrayIndex(index++);
   450 		SlObject(c, _grfconfig_desc);
   462 		SlObject(c, _grfconfig_desc);
   451 	}
   463 	}
   452 }
   464 }
   453 
   465 
   454 
   466 
   455 static void Load_NGRF(void)
   467 static void Load_NGRF(void)
   456 {
   468 {
   457 	GRFConfig *first = NULL;
   469 	ClearGRFConfigList(&_grfconfig);
   458 	GRFConfig **last = &first;
       
   459 
       
   460 	while (SlIterateArray() != -1) {
   470 	while (SlIterateArray() != -1) {
   461 		GRFConfig *c = CallocT<GRFConfig>(1);
   471 		GRFConfig *c = CallocT<GRFConfig>(1);
   462 		SlObject(c, _grfconfig_desc);
   472 		SlObject(c, _grfconfig_desc);
   463 
   473 		AppendToGRFConfigList(&_grfconfig, c);
   464 		/* Append our configuration to the list */
       
   465 		*last = c;
       
   466 		last = &c->next;
       
   467 	}
   474 	}
   468 
   475 
   469 	/* Append static NewGRF configuration */
   476 	/* Append static NewGRF configuration */
   470 	CopyGRFConfigList(last, _grfconfig_static);
       
   471 
       
   472 	ClearGRFConfigList(&_grfconfig);
       
   473 	_grfconfig = first;
       
   474 	AppendStaticGRFConfigs(&_grfconfig);
   477 	AppendStaticGRFConfigs(&_grfconfig);
   475 }
   478 }
   476 
   479 
   477 extern const ChunkHandler _newgrf_chunk_handlers[] = {
   480 extern const ChunkHandler _newgrf_chunk_handlers[] = {
   478 	{ 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST }
   481 	{ 'NGRF', Save_NGRF, Load_NGRF, CH_ARRAY | CH_LAST }