summaryrefslogtreecommitdiff
path: root/src/script/api
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2012-01-08 21:48:05 +0000
committerrubidium <rubidium@openttd.org>2012-01-08 21:48:05 +0000
commit4479f90843d3597268f889a3955f3c5289b92e0a (patch)
tree969e79bea4a892f189791f593751fa0ff463240e /src/script/api
parentab38e3227dbbefab952530c4ddf5408605187fcb (diff)
downloadopenttd-4479f90843d3597268f889a3955f3c5289b92e0a.tar.xz
(svn r23777) -Codechange: refactor allocating memory and fetching strings into a single function for scripts
Diffstat (limited to 'src/script/api')
-rw-r--r--src/script/api/script_basestation.cpp6
-rw-r--r--src/script/api/script_bridge.cpp6
-rw-r--r--src/script/api/script_company.cpp6
-rw-r--r--src/script/api/script_engine.cpp6
-rw-r--r--src/script/api/script_event_types.cpp5
-rw-r--r--src/script/api/script_group.cpp6
-rw-r--r--src/script/api/script_industry.cpp6
-rw-r--r--src/script/api/script_industrytype.cpp6
-rw-r--r--src/script/api/script_object.cpp9
-rw-r--r--src/script/api/script_object.hpp5
-rw-r--r--src/script/api/script_rail.cpp6
-rw-r--r--src/script/api/script_sign.cpp7
-rw-r--r--src/script/api/script_town.cpp7
-rw-r--r--src/script/api/script_vehicle.cpp6
14 files changed, 27 insertions, 60 deletions
diff --git a/src/script/api/script_basestation.cpp b/src/script/api/script_basestation.cpp
index bb77f05e1..d1bcf6f6d 100644
--- 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<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)
diff --git a/src/script/api/script_bridge.cpp b/src/script/api/script_bridge.cpp
index 748489655..50f3e3f0a 100644
--- a/src/script/api/script_bridge.cpp
+++ b/src/script/api/script_bridge.cpp
@@ -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)
diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp
index 571cc80c6..dd79feb88 100644
--- 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<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)
diff --git a/src/script/api/script_engine.cpp b/src/script/api/script_engine.cpp
index 4ba4e9f82..54fc54cb1 100644
--- 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<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)
diff --git a/src/script/api/script_event_types.cpp b/src/script/api/script_event_types.cpp
index 382c6f0ba..0e1683440 100644
--- a/src/script/api/script_event_types.cpp
+++ b/src/script/api/script_event_types.cpp
@@ -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()
diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp
index 3dfc50a09..b04ece2fa 100644
--- 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<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)
diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp
index 5d76bb005..5e30f1377 100644
--- 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<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)
diff --git a/src/script/api/script_industrytype.cpp b/src/script/api/script_industrytype.cpp
index cbb8369b5..26a93cda1 100644
--- 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<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)
diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp
index 707b734aa..f56bce0c0 100644
--- 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::~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);
diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp
index 7c9e71460..4a39e618e 100644
--- 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
index e4f653e94..fd1e3e920 100644
--- 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<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)
diff --git a/src/script/api/script_sign.cpp b/src/script/api/script_sign.cpp
index f6dc922b4..ecf0b4c28 100644
--- 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<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)
diff --git a/src/script/api/script_town.cpp b/src/script/api/script_town.cpp
index 63093ecd5..331b98891 100644
--- 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<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)
diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp
index 13ccce518..b6a28af4f 100644
--- 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<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)