# HG changeset patch # User rubidium # Date 2012-01-08 21:48:05 # Node ID 14468761322877cce0ae133c63296976d44c1e81 # Parent 70f09b429a744be429e83a8451ae5a70bd6eb3b9 (svn r23777) -Codechange: refactor allocating memory and fetching strings into a single function for scripts diff --git a/src/script/api/script_basestation.cpp b/src/script/api/script_basestation.cpp --- a/src/script/api/script_basestation.cpp +++ b/src/script/api/script_basestation.cpp @@ -27,12 +27,8 @@ { if (!IsValidBaseStation(station_id)) return NULL; - static const int len = 64; - char *name = MallocT(len); - ::SetDParam(0, station_id); - ::GetString(name, ::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME, &name[len - 1]); - return name; + return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME); } /* static */ bool ScriptBaseStation::SetName(StationID station_id, Text *name) diff --git a/src/script/api/script_bridge.cpp b/src/script/api/script_bridge.cpp --- a/src/script/api/script_bridge.cpp +++ b/src/script/api/script_bridge.cpp @@ -136,11 +136,7 @@ static void _DoCommandReturnBuildBridge1 { if (!IsValidBridge(bridge_id)) return NULL; - static const int len = 64; - char *bridge_name = MallocT(len); - - ::GetString(bridge_name, ::GetBridgeSpec(bridge_id)->transport_name[0], &bridge_name[len - 1]); - return bridge_name; + return GetString(::GetBridgeSpec(bridge_id)->transport_name[0]); } /* static */ int32 ScriptBridge::GetMaxSpeed(BridgeID bridge_id) diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp --- a/src/script/api/script_company.cpp +++ b/src/script/api/script_company.cpp @@ -55,12 +55,8 @@ company = ResolveCompanyID(company); if (company == COMPANY_INVALID) return NULL; - static const int len = 64; - char *company_name = MallocT(len); - ::SetDParam(0, company); - ::GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]); - return company_name; + return GetString(STR_COMPANY_NAME); } /* static */ bool ScriptCompany::SetPresidentName(Text *name) diff --git a/src/script/api/script_engine.cpp b/src/script/api/script_engine.cpp --- a/src/script/api/script_engine.cpp +++ b/src/script/api/script_engine.cpp @@ -36,12 +36,8 @@ { if (!IsValidEngine(engine_id)) return NULL; - static const int len = 64; - char *engine_name = MallocT(len); - ::SetDParam(0, engine_id); - ::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]); - return engine_name; + return GetString(STR_ENGINE_NAME); } /* static */ CargoID ScriptEngine::GetCargoType(EngineID engine_id) diff --git a/src/script/api/script_event_types.cpp b/src/script/api/script_event_types.cpp --- a/src/script/api/script_event_types.cpp +++ b/src/script/api/script_event_types.cpp @@ -28,12 +28,9 @@ bool ScriptEventEnginePreview::IsEngineV char *ScriptEventEnginePreview::GetName() { if (!this->IsEngineValid()) return NULL; - static const int len = 64; - char *engine_name = MallocT(len); ::SetDParam(0, this->engine); - ::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]); - return engine_name; + return GetString(STR_ENGINE_NAME); } CargoID ScriptEventEnginePreview::GetCargoType() diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -64,12 +64,8 @@ { if (!IsValidGroup(group_id)) return NULL; - static const int len = 64; - char *group_name = MallocT(len); - ::SetDParam(0, group_id); - ::GetString(group_name, STR_GROUP_NAME, &group_name[len - 1]); - return group_name; + return GetString(STR_GROUP_NAME); } /* static */ bool ScriptGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable) diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -38,13 +38,9 @@ /* static */ char *ScriptIndustry::GetName(IndustryID industry_id) { if (!IsValidIndustry(industry_id)) return NULL; - static const int len = 64; - char *industry_name = MallocT(len); ::SetDParam(0, industry_id); - ::GetString(industry_name, STR_INDUSTRY_NAME, &industry_name[len - 1]); - - return industry_name; + return GetString(STR_INDUSTRY_NAME); } /* static */ ScriptIndustry::CargoAcceptState ScriptIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id) diff --git a/src/script/api/script_industrytype.cpp b/src/script/api/script_industrytype.cpp --- a/src/script/api/script_industrytype.cpp +++ b/src/script/api/script_industrytype.cpp @@ -51,12 +51,8 @@ /* static */ char *ScriptIndustryType::GetName(IndustryType industry_type) { if (!IsValidIndustryType(industry_type)) return NULL; - static const int len = 64; - char *industrytype_name = MallocT(len); - ::GetString(industrytype_name, ::GetIndustrySpec(industry_type)->name, &industrytype_name[len - 1]); - - return industrytype_name; + return GetString(::GetIndustrySpec(industry_type)->name); } /* static */ ScriptList *ScriptIndustryType::GetProducedCargo(IndustryType industry_type) diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -16,6 +16,8 @@ #include "../../company_base.h" #include "../../network/network.h" #include "../../genworld.h" +#include "../../string_func.h" +#include "../../strings_func.h" #include "../script_storage.hpp" #include "../script_instance.hpp" @@ -233,6 +235,13 @@ ScriptObject::ActiveInstance::~ActiveIns return GetStorage()->log_data; } +/* static */ char *ScriptObject::GetString(StringID string) +{ + char buffer[64]; + ::GetString(buffer, string, lastof(buffer)); + return ::strdup(buffer); +} + /* static */ void ScriptObject::SetCallbackVariable(int index, int value) { if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1); diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -236,6 +236,11 @@ protected: */ static void *&GetLogPointer(); + /** + * Get an allocated string with all control codes stripped off. + */ + static char *GetString(StringID string); + private: /** * Store a new_vehicle_id per company. diff --git a/src/script/api/script_rail.cpp b/src/script/api/script_rail.cpp --- a/src/script/api/script_rail.cpp +++ b/src/script/api/script_rail.cpp @@ -25,11 +25,7 @@ { if (!IsRailTypeAvailable(rail_type)) return NULL; - static const int len = 64; - char *railtype_name = MallocT(len); - - ::GetString(railtype_name, GetRailTypeInfo((::RailType)rail_type)->strings.menu_text, &railtype_name[len - 1]); - return railtype_name; + return GetString(GetRailTypeInfo((::RailType)rail_type)->strings.menu_text); } /* static */ bool ScriptRail::IsRailTile(TileIndex tile) diff --git a/src/script/api/script_sign.cpp b/src/script/api/script_sign.cpp --- a/src/script/api/script_sign.cpp +++ b/src/script/api/script_sign.cpp @@ -48,13 +48,8 @@ { if (!IsValidSign(sign_id)) return NULL; - static const int len = 64; - char *sign_name = MallocT(len); - ::SetDParam(0, sign_id); - ::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]); - - return sign_name; + return GetString(STR_SIGN_NAME); } /* static */ TileIndex ScriptSign::GetLocation(SignID sign_id) diff --git a/src/script/api/script_town.cpp b/src/script/api/script_town.cpp --- a/src/script/api/script_town.cpp +++ b/src/script/api/script_town.cpp @@ -14,6 +14,7 @@ #include "script_map.hpp" #include "script_error.hpp" #include "../../town.h" +#include "../../string_func.h" #include "../../strings_func.h" #include "../../station_base.h" #include "../../landscape.h" @@ -32,13 +33,9 @@ /* static */ char *ScriptTown::GetName(TownID town_id) { if (!IsValidTown(town_id)) return NULL; - static const int len = 64; - char *town_name = MallocT(len); ::SetDParam(0, town_id); - ::GetString(town_name, STR_TOWN_NAME, &town_name[len - 1]); - - return town_name; + return GetString(STR_TOWN_NAME); } /* static */ bool ScriptTown::SetText(TownID town_id, Text *text) diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp --- a/src/script/api/script_vehicle.cpp +++ b/src/script/api/script_vehicle.cpp @@ -270,12 +270,8 @@ { if (!IsValidVehicle(vehicle_id)) return NULL; - static const int len = 64; - char *vehicle_name = MallocT(len); - ::SetDParam(0, vehicle_id); - ::GetString(vehicle_name, STR_VEHICLE_NAME, &vehicle_name[len - 1]); - return vehicle_name; + return GetString(STR_VEHICLE_NAME); } /* static */ int32 ScriptVehicle::GetAge(VehicleID vehicle_id)