mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r23777) -Codechange: refactor allocating memory and fetching strings into a single function for scripts
This commit is contained in:
parent
ab38e3227d
commit
4479f90843
|
@ -27,12 +27,8 @@
|
|||
{
|
||||
if (!IsValidBaseStation(station_id)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *name = MallocT<char>(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)
|
||||
|
|
|
@ -136,11 +136,7 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
|
|||
{
|
||||
if (!IsValidBridge(bridge_id)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *bridge_name = MallocT<char>(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)
|
||||
|
|
|
@ -55,12 +55,8 @@
|
|||
company = ResolveCompanyID(company);
|
||||
if (company == COMPANY_INVALID) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *company_name = MallocT<char>(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)
|
||||
|
|
|
@ -36,12 +36,8 @@
|
|||
{
|
||||
if (!IsValidEngine(engine_id)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *engine_name = MallocT<char>(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)
|
||||
|
|
|
@ -28,12 +28,9 @@ bool ScriptEventEnginePreview::IsEngineValid() const
|
|||
char *ScriptEventEnginePreview::GetName()
|
||||
{
|
||||
if (!this->IsEngineValid()) return NULL;
|
||||
static const int len = 64;
|
||||
char *engine_name = MallocT<char>(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()
|
||||
|
|
|
@ -64,12 +64,8 @@
|
|||
{
|
||||
if (!IsValidGroup(group_id)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *group_name = MallocT<char>(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)
|
||||
|
|
|
@ -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<char>(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)
|
||||
|
|
|
@ -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<char>(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)
|
||||
|
|
|
@ -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::~ActiveInstance()
|
|||
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);
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -25,11 +25,7 @@
|
|||
{
|
||||
if (!IsRailTypeAvailable(rail_type)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *railtype_name = MallocT<char>(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)
|
||||
|
|
|
@ -48,13 +48,8 @@
|
|||
{
|
||||
if (!IsValidSign(sign_id)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *sign_name = MallocT<char>(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)
|
||||
|
|
|
@ -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<char>(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)
|
||||
|
|
|
@ -270,12 +270,8 @@
|
|||
{
|
||||
if (!IsValidVehicle(vehicle_id)) return NULL;
|
||||
|
||||
static const int len = 64;
|
||||
char *vehicle_name = MallocT<char>(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)
|
||||
|
|
Loading…
Reference in New Issue