Changeset - r18917:144687613228
[Not reviewed]
master
0 14 0
rubidium - 12 years ago 2012-01-08 21:48:05
rubidium@openttd.org
(svn r23777) -Codechange: refactor allocating memory and fetching strings into a single function for scripts
14 files changed with 27 insertions and 60 deletions:
0 comments (0 inline, 0 general)
src/script/api/script_basestation.cpp
Show inline comments
 
@@ -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)
src/script/api/script_bridge.cpp
Show inline comments
 
@@ -136,11 +136,7 @@ static void _DoCommandReturnBuildBridge1
 
{
 
	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)
src/script/api/script_company.cpp
Show inline comments
 
@@ -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)
src/script/api/script_engine.cpp
Show inline comments
 
@@ -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)
src/script/api/script_event_types.cpp
Show inline comments
 
@@ -28,12 +28,9 @@ bool ScriptEventEnginePreview::IsEngineV
 
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()
src/script/api/script_group.cpp
Show inline comments
 
@@ -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)
src/script/api/script_industry.cpp
Show inline comments
 
@@ -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)
src/script/api/script_industrytype.cpp
Show inline comments
 
@@ -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)
src/script/api/script_object.cpp
Show inline comments
 
@@ -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);
src/script/api/script_object.hpp
Show inline comments
 
@@ -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.
src/script/api/script_rail.cpp
Show inline comments
 
@@ -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)
src/script/api/script_sign.cpp
Show inline comments
 
@@ -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)
src/script/api/script_town.cpp
Show inline comments
 
@@ -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)
src/script/api/script_vehicle.cpp
Show inline comments
 
@@ -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)
0 comments (0 inline, 0 general)