From c692d897cd1bdeb8d4e00349519a460fcdd2c262 Mon Sep 17 00:00:00 2001 From: peter1138 Date: Wed, 27 Jun 2007 20:53:25 +0000 Subject: (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type. --- src/engine.cpp | 21 +++++++++++++++++++-- src/functions.h | 3 --- src/group_cmd.cpp | 17 +++++++++++++++++ src/lang/english.txt | 2 ++ src/misc_cmd.cpp | 41 +++++++++++++++++++++++++++++++++++++---- src/station_cmd.cpp | 21 +++++++++++++++++++-- src/town_cmd.cpp | 20 ++++++++++++++++++-- src/vehicle.cpp | 31 +++++++++++++++++++++++++++++-- src/waypoint.cpp | 22 ++++++++++++++++++++-- 9 files changed, 161 insertions(+), 17 deletions(-) diff --git a/src/engine.cpp b/src/engine.cpp index b8207c481..09a1b889f 100644 --- 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 @@ void EnginesMonthlyLoop() } } +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 @@ CommandCost CmdRenameEngine(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { StringID str; - if (!IsEngineIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR; + if (!IsEngineIndex(p1) || StrEmpty(_cmd_text)) return CMD_ERROR; + + if (!IsUniqueEngineName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); - str = AllocateNameUnique(_cmd_text, 0); + str = AllocateName(_cmd_text, 0); if (str == 0) return CMD_ERROR; if (flags & DC_EXEC) { diff --git a/src/functions.h b/src/functions.h index de5faec9c..15bc4779a 100644 --- a/src/functions.h +++ b/src/functions.h @@ -98,9 +98,6 @@ bool IsCustomName(StringID id); 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(); diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index 7505d63b5..782324799 100644 --- 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 @@ CommandCost CmdDeleteGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) 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 @@ CommandCost CmdRenameGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) 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; diff --git a/src/lang/english.txt b/src/lang/english.txt index 68800d65d..677d2b28f 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -3359,4 +3359,6 @@ STR_PLAYER_NAME :{PLAYERNAME} STR_SIGN_NAME :{SIGN} STR_VEHICLE_NAME :{VEHICLE} +STR_NAME_MUST_BE_UNIQUE :{WHITE}Name must be unique + ######## diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp index 5ad8ffc24..3a0448f84 100644 --- 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 @@ CommandCost CmdDecreaseLoan(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) 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 @@ CommandCost CmdChangeCompanyName(TileIndex tile, uint32 flags, uint32 p1, uint32 StringID str; Player *p; - if (_cmd_text[0] == '\0') return CMD_ERROR; + if (StrEmpty(_cmd_text)) return CMD_ERROR; + + if (!IsUniqueCompanyName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); - str = AllocateNameUnique(_cmd_text, 4); + str = AllocateName(_cmd_text, 4); if (str == 0) return CMD_ERROR; if (flags & DC_EXEC) { @@ -222,6 +239,20 @@ CommandCost CmdChangeCompanyName(TileIndex tile, uint32 flags, uint32 p1, uint32 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 @@ CommandCost CmdChangePresidentName(TileIndex tile, uint32 flags, uint32 p1, uint StringID str; Player *p; - if (_cmd_text[0] == '\0') return CMD_ERROR; + if (StrEmpty(_cmd_text)) return CMD_ERROR; + + if (!IsUniquePresidentName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); - str = AllocateNameUnique(_cmd_text, 4); + str = AllocateName(_cmd_text, 4); if (str == 0) return CMD_ERROR; if (flags & DC_EXEC) { diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 514cf4185..c870d6309 100644 --- 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 @@ static void UpdateStationWaiting(Station *st, CargoID type, uint amount) 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 @@ static void UpdateStationWaiting(Station *st, CargoID type, uint amount) */ 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) { diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index f19c2266b..0250887df 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -1859,6 +1859,20 @@ void ClearTownHouse(Town *t, TileIndex tile) 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 @@ CommandCost CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) 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) { diff --git a/src/vehicle.cpp b/src/vehicle.cpp index e7080e1a5..45222cd69 100644 --- 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 @@ void VehicleEnterDepot(Vehicle *v) } } +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 @@ CommandCost CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) 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) { diff --git a/src/waypoint.cpp b/src/waypoint.cpp index c621bd71e..6b81cff41 100644 --- 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 @@ CommandCost CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint 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 @@ CommandCost CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2 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; -- cgit v1.2.3-54-g00ecf