changeset 7097:5c2889ace3ac draft

(svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
author peter1138 <peter1138@openttd.org>
date Wed, 27 Jun 2007 20:53:25 +0000
parents c1b9f000066e
children 11dca9242a8d
files src/engine.cpp src/functions.h src/group_cmd.cpp src/lang/english.txt src/misc_cmd.cpp src/station_cmd.cpp src/town_cmd.cpp src/vehicle.cpp src/waypoint.cpp
diffstat 9 files changed, 161 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -21,6 +21,8 @@
 #include "date.h"
 #include "table/engines.h"
 #include "group.h"
+#include "string.h"
+#include "strings.h"
 
 EngineInfo _engine_info[TOTAL_NUM_ENGINES];
 RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES];
@@ -368,6 +370,19 @@
 	}
 }
 
+static bool IsUniqueEngineName(const char *name)
+{
+	char buf[512];
+
+	for (EngineID i = 0; i < TOTAL_NUM_ENGINES; i++) {
+		SetDParam(0, i);
+		GetString(buf, STR_ENGINE_NAME, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /** Rename an engine.
  * @param tile unused
  * @param flags operation to perfom
@@ -378,9 +393,11 @@
 {
 	StringID str;
 
-	if (!IsEngineIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+	if (!IsEngineIndex(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
 
-	str = AllocateNameUnique(_cmd_text, 0);
+	if (!IsUniqueEngineName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+	str = AllocateName(_cmd_text, 0);
 	if (str == 0) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
--- a/src/functions.h
+++ b/src/functions.h
@@ -98,9 +98,6 @@
 void DeleteName(StringID id);
 char *GetName(char *buff, StringID id, const char* last);
 
-/* AllocateNameUnique also tests if the name used is not used anywere else
- * and if it is used, it returns an error. */
-#define AllocateNameUnique(name, skip) RealAllocateName(name, skip, true)
 #define AllocateName(name, skip) RealAllocateName(name, skip, false)
 StringID RealAllocateName(const char *name, byte skip, bool check_double);
 void ConvertNameArray();
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -4,6 +4,7 @@
 
 #include "stdafx.h"
 #include "openttd.h"
+#include "variables.h"
 #include "functions.h"
 #include "player.h"
 #include "table/strings.h"
@@ -17,6 +18,7 @@
 #include "string.h"
 #include "window.h"
 #include "vehicle_gui.h"
+#include "strings.h"
 
 /**
  * Update the num engines of a groupID. Decrease the old one and increase the new one
@@ -159,6 +161,19 @@
 	return CommandCost();
 }
 
+static bool IsUniqueGroupName(const char *name)
+{
+	const Group *g;
+	char buf[512];
+
+	FOR_ALL_GROUPS(g) {
+		SetDParam(0, g->index);
+		GetString(buf, STR_GROUP_NAME, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
 
 /**
  * Rename a group
@@ -174,6 +189,8 @@
 	Group *g = GetGroup(p1);
 	if (g->owner != _current_player) return CMD_ERROR;
 
+	if (!IsUniqueGroupName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
 	/* Create the name */
 	StringID str = AllocateName(_cmd_text, 0);
 	if (str == STR_NULL) return CMD_ERROR;
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -3359,4 +3359,6 @@
 STR_SIGN_NAME                                                   :{SIGN}
 STR_VEHICLE_NAME                                                :{VEHICLE}
 
+STR_NAME_MUST_BE_UNIQUE                                         :{WHITE}Name must be unique
+
 ########
--- a/src/misc_cmd.cpp
+++ b/src/misc_cmd.cpp
@@ -17,6 +17,7 @@
 #include "variables.h"
 #include "livery.h"
 #include "player_face.h"
+#include "strings.h"
 
 /** Change the player's face.
  * @param tile unused
@@ -194,6 +195,20 @@
 	return CommandCost();
 }
 
+static bool IsUniqueCompanyName(const char *name)
+{
+	const Player *p;
+	char buf[512];
+
+	FOR_ALL_PLAYERS(p) {
+		SetDParam(0, p->index);
+		GetString(buf, STR_COMPANY_NAME, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /** Change the name of the company.
  * @param tile unused
  * @param flags operation to perform
@@ -205,9 +220,11 @@
 	StringID str;
 	Player *p;
 
-	if (_cmd_text[0] == '\0') return CMD_ERROR;
+	if (StrEmpty(_cmd_text)) return CMD_ERROR;
 
-	str = AllocateNameUnique(_cmd_text, 4);
+	if (!IsUniqueCompanyName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+	str = AllocateName(_cmd_text, 4);
 	if (str == 0) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
@@ -222,6 +239,20 @@
 	return CommandCost();
 }
 
+static bool IsUniquePresidentName(const char *name)
+{
+	const Player *p;
+	char buf[512];
+
+	FOR_ALL_PLAYERS(p) {
+		SetDParam(0, p->index);
+		GetString(buf, STR_PLAYER_NAME, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /** Change the name of the president.
  * @param tile unused
  * @param flags operation to perform
@@ -233,9 +264,11 @@
 	StringID str;
 	Player *p;
 
-	if (_cmd_text[0] == '\0') return CMD_ERROR;
+	if (StrEmpty(_cmd_text)) return CMD_ERROR;
 
-	str = AllocateNameUnique(_cmd_text, 4);
+	if (!IsUniquePresidentName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+	str = AllocateName(_cmd_text, 4);
 	if (str == 0) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -41,6 +41,7 @@
 #include "misc/autoptr.hpp"
 #include "road.h"
 #include "cargotype.h"
+#include "strings.h"
 
 /**
  * Called if a new block is added to the station-pool
@@ -2479,6 +2480,20 @@
 	st->MarkTilesDirty(true);
 }
 
+static bool IsUniqueStationName(const char *name)
+{
+	const Station *st;
+	char buf[512];
+
+	FOR_ALL_STATIONS(st) {
+		SetDParam(0, st->index);
+		GetString(buf, STR_STATION, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /** Rename a station
  * @param tile unused
  * @param flags operation to perform
@@ -2487,12 +2502,14 @@
  */
 CommandCost CmdRenameStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	if (!IsValidStationID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+	if (!IsValidStationID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
 	Station *st = GetStation(p1);
 
 	if (!CheckOwnership(st->owner)) return CMD_ERROR;
 
-	StringID str = AllocateNameUnique(_cmd_text, 6);
+	if (!IsUniqueStationName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+	StringID str = AllocateName(_cmd_text, 6);
 	if (str == 0) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -1859,6 +1859,20 @@
 	if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1));
 }
 
+static bool IsUniqueTownName(const char *name)
+{
+	const Town *t;
+	char buf[512];
+
+	FOR_ALL_TOWNS(t) {
+		SetDParam(0, t->index);
+		GetString(buf, STR_TOWN, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /** Rename a town (server-only).
  * @param tile unused
  * @param flags type of operation
@@ -1870,11 +1884,13 @@
 	StringID str;
 	Town *t;
 
-	if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+	if (!IsValidTownID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
 
 	t = GetTown(p1);
 
-	str = AllocateNameUnique(_cmd_text, 4);
+	if (!IsUniqueTownName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+	str = AllocateName(_cmd_text, 4);
 	if (str == 0) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -43,6 +43,7 @@
 #include "helpers.hpp"
 #include "group.h"
 #include "economy.h"
+#include "strings.h"
 
 #define INVALID_COORD (0x7fffffff)
 #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
@@ -2396,6 +2397,30 @@
 	}
 }
 
+static bool IsUniqueVehicleName(const char *name)
+{
+	const Vehicle *v;
+	char buf[512];
+
+	FOR_ALL_VEHICLES(v) {
+		switch (v->type) {
+			case VEH_TRAIN:
+			case VEH_ROAD:
+			case VEH_AIRCRAFT:
+			case VEH_SHIP:
+				SetDParam(0, v->index);
+				GetString(buf, STR_VEHICLE_NAME, lastof(buf));
+				if (strcmp(buf, name) == 0) return false;
+				break;
+
+			default:
+				break;
+		}
+	}
+
+	return true;
+}
+
 /** Give a custom name to your vehicle
  * @param tile unused
  * @param flags type of operation
@@ -2407,13 +2432,15 @@
 	Vehicle *v;
 	StringID str;
 
-	if (!IsValidVehicleID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
+	if (!IsValidVehicleID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
 
 	v = GetVehicle(p1);
 
 	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 
-	str = AllocateNameUnique(_cmd_text, 2);
+	if (!IsUniqueVehicleName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+	str = AllocateName(_cmd_text, 2);
 	if (str == 0) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
--- a/src/waypoint.cpp
+++ b/src/waypoint.cpp
@@ -24,6 +24,8 @@
 #include "yapf/yapf.h"
 #include "date.h"
 #include "newgrf.h"
+#include "string.h"
+#include "strings.h"
 
 enum {
 	MAX_WAYPOINTS_PER_TOWN = 64,
@@ -341,6 +343,20 @@
 	return RemoveTrainWaypoint(tile, flags, true);
 }
 
+static bool IsUniqueWaypointName(const char *name)
+{
+	const Waypoint *wp;
+	char buf[512];
+
+	FOR_ALL_WAYPOINTS(wp) {
+		SetDParam(0, wp->index);
+		GetString(buf, STR_WAYPOINT_RAW, lastof(buf));
+		if (strcmp(buf, name) == 0) return false;
+	}
+
+	return true;
+}
+
 /**
  * Rename a waypoint.
  * @param tile unused
@@ -355,8 +371,10 @@
 
 	if (!IsValidWaypointID(p1)) return CMD_ERROR;
 
-	if (_cmd_text[0] != '\0') {
-		StringID str = AllocateNameUnique(_cmd_text, 0);
+	if (!StrEmpty(_cmd_text)) {
+		if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
+
+		StringID str = AllocateName(_cmd_text, 0);
 
 		if (str == 0) return CMD_ERROR;