(svn r7367) -Codechange: prevent adding files where the GRF ID is already in the list
authorpeter1138
Tue, 05 Dec 2006 11:13:22 +0000
changeset 5243 a564347a939b
parent 5242 db331cfced9a
child 5244 e110f52333e8
(svn r7367) -Codechange: prevent adding files where the GRF ID is already in the list
lang/english.txt
newgrf_gui.c
--- a/lang/english.txt	Tue Dec 05 09:19:14 2006 +0000
+++ b/lang/english.txt	Tue Dec 05 11:13:22 2006 +0000
@@ -2898,6 +2898,7 @@
 STR_NEWGRF_ADD_FILE_TIP                                         :{BLACK}Add the selected NewGRF file to your configuration
 STR_NEWGRF_RESCAN_FILES                                         :{BLACK}Rescan files
 STR_NEWGRF_RESCAN_FILES_TIP                                     :{BLACK}Update the list of available NewGRF files
+STR_NEWGRF_DUPLICATE_GRFID                                      :{WHITE}Cannot add file: duplicate GRF ID
 
 STR_NEWGRF_NOT_FOUND                                            :{RED}Matching file not found
 STR_NEWGRF_DISABLED                                             :{RED}Disabled
--- a/newgrf_gui.c	Tue Dec 05 09:19:14 2006 +0000
+++ b/newgrf_gui.c	Tue Dec 05 11:13:22 2006 +0000
@@ -167,13 +167,26 @@
 				case 6: /* Add selection to list */
 					if (WP(w, newgrf_add_d).sel != NULL) {
 						const GRFConfig *src = WP(w, newgrf_add_d).sel;
-						GRFConfig *c = calloc(1, sizeof(*c));
+						GRFConfig **list, *c;
+
+						/* Find last entry in the list, checking for duplicate grfid on the way */
+						for (list = WP(w, newgrf_add_d).list; *list != NULL; list = &(*list)->next) {
+							if ((*list)->grfid == src->grfid) {
+								ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_DUPLICATE_GRFID, 0, 0);
+								return;
+							}
+						}
+
+						/* Copy GRF details from scanned list */
+						c = calloc(1, sizeof(*c));
 						*c = *src;
 						c->filename = strdup(src->filename);
 						if (src->name != NULL) c->name = strdup(src->name);
 						if (src->info != NULL) c->info = strdup(src->info);
 						c->next = NULL;
-						*WP(w, newgrf_add_d).list = c;
+
+						/* Append GRF config to configuration list */
+						*list = c;
 
 						DeleteWindowByClass(WC_SAVELOAD);
 						InvalidateWindowData(WC_GAME_OPTIONS, 0);
@@ -313,8 +326,6 @@
 					w = AllocateWindowDesc(&_newgrf_add_dlg_desc);
 					w->resize.step_height = 10;
 
-					/* Find the last entry in the list */
-					for (; *list != NULL; list = &(*list)->next);
 					WP(w, newgrf_add_d).list = list;
 					break;
 				}