diff --git a/src/script/api/script_basestation.cpp b/src/script/api/script_basestation.cpp index 408543ae8a..1863ea8554 100644 --- a/src/script/api/script_basestation.cpp +++ b/src/script/api/script_basestation.cpp @@ -26,9 +26,9 @@ return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE); } -/* static */ char *ScriptBaseStation::GetName(StationID station_id) +/* static */ std::optional ScriptBaseStation::GetName(StationID station_id) { - if (!IsValidBaseStation(station_id)) return nullptr; + if (!IsValidBaseStation(station_id)) return std::nullopt; ::SetDParam(0, station_id); return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME); diff --git a/src/script/api/script_basestation.hpp b/src/script/api/script_basestation.hpp index 9f39368a30..52df57f78c 100644 --- a/src/script/api/script_basestation.hpp +++ b/src/script/api/script_basestation.hpp @@ -43,7 +43,7 @@ public: * @pre IsValidBaseStation(station_id). * @return The name of the station. */ - static char *GetName(StationID station_id); + static std::optional GetName(StationID station_id); /** * Set the name this basestation. diff --git a/src/script/api/script_bridge.cpp b/src/script/api/script_bridge.cpp index 939641ad94..d09c214e48 100644 --- a/src/script/api/script_bridge.cpp +++ b/src/script/api/script_bridge.cpp @@ -129,10 +129,10 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance) return ScriptObject::Command::Do(tile); } -/* static */ char *ScriptBridge::GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type) +/* static */ std::optional ScriptBridge::GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type) { - EnforcePrecondition(nullptr, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER); - if (!IsValidBridge(bridge_id)) return nullptr; + EnforcePrecondition(std::nullopt, vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER); + if (!IsValidBridge(bridge_id)) return std::nullopt; return GetString(vehicle_type == ScriptVehicle::VT_WATER ? STR_LAI_BRIDGE_DESCRIPTION_AQUEDUCT : ::GetBridgeSpec(bridge_id)->transport_name[vehicle_type]); } diff --git a/src/script/api/script_bridge.hpp b/src/script/api/script_bridge.hpp index cce84f812d..d7dc0df043 100644 --- a/src/script/api/script_bridge.hpp +++ b/src/script/api/script_bridge.hpp @@ -69,7 +69,7 @@ public: * @pre vehicle_type == ScriptVehicle::VT_ROAD || vehicle_type == ScriptVehicle::VT_RAIL || vehicle_type == ScriptVehicle::VT_WATER * @return The name the bridge has. */ - static char *GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type); + static std::optional GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type); /** * Get the maximum speed of a bridge. diff --git a/src/script/api/script_cargo.cpp b/src/script/api/script_cargo.cpp index b0377a3e0e..ba0aaf3799 100644 --- a/src/script/api/script_cargo.cpp +++ b/src/script/api/script_cargo.cpp @@ -28,26 +28,25 @@ return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END); } -/* static */ char *ScriptCargo::GetName(CargoID cargo_type) +/* static */ std::optional ScriptCargo::GetName(CargoID cargo_type) { - if (!IsValidCargo(cargo_type)) return nullptr; + if (!IsValidCargo(cargo_type)) return std::nullopt; ::SetDParam(0, 1ULL << cargo_type); return GetString(STR_JUST_CARGO_LIST); } -/* static */ char *ScriptCargo::GetCargoLabel(CargoID cargo_type) +/* static */ std::optional ScriptCargo::GetCargoLabel(CargoID cargo_type) { - if (!IsValidCargo(cargo_type)) return nullptr; + if (!IsValidCargo(cargo_type)) return std::nullopt; const CargoSpec *cargo = ::CargoSpec::Get(cargo_type); /* cargo->label is a uint32 packing a 4 character non-terminated string, * like "PASS", "COAL", "OIL_". New ones can be defined by NewGRFs */ - char *cargo_label = MallocT(sizeof(cargo->label) + 1); + std::string cargo_label; for (uint i = 0; i < sizeof(cargo->label); i++) { - cargo_label[i] = GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8); + cargo_label.push_back(GB(cargo->label, (uint8)(sizeof(cargo->label) - i - 1) * 8, 8)); } - cargo_label[sizeof(cargo->label)] = '\0'; return cargo_label; } diff --git a/src/script/api/script_cargo.hpp b/src/script/api/script_cargo.hpp index c5b5c1b5f0..f44d53dbae 100644 --- a/src/script/api/script_cargo.hpp +++ b/src/script/api/script_cargo.hpp @@ -90,7 +90,7 @@ public: * @pre IsValidCargo(cargo_type). * @return The name of the cargo type. */ - static char *GetName(CargoID cargo_type); + static std::optional GetName(CargoID cargo_type); /** * Gets the string representation of the cargo label. @@ -107,7 +107,7 @@ public: * - In other words: Only use the cargo label, if you know more about the behaviour * of a specific cargo from a specific industry set, than the API methods can tell you. */ - static char *GetCargoLabel(CargoID cargo_type); + static std::optional GetCargoLabel(CargoID cargo_type); /** * Checks whether the give cargo is a freight or not. diff --git a/src/script/api/script_client.cpp b/src/script/api/script_client.cpp index ede1b925fe..87f90d65c8 100644 --- a/src/script/api/script_client.cpp +++ b/src/script/api/script_client.cpp @@ -32,11 +32,11 @@ static NetworkClientInfo *FindClientInfo(ScriptClient::ClientID client) return (FindClientInfo(client) == nullptr ? ScriptClient::CLIENT_INVALID : client); } -/* static */ char *ScriptClient::GetName(ScriptClient::ClientID client) +/* static */ std::optional ScriptClient::GetName(ScriptClient::ClientID client) { NetworkClientInfo *ci = FindClientInfo(client); - if (ci == nullptr) return nullptr; - return stredup(ci->client_name.c_str()); + if (ci == nullptr) return std::nullopt; + return ci->client_name; } /* static */ ScriptCompany::CompanyID ScriptClient::GetCompany(ScriptClient::ClientID client) diff --git a/src/script/api/script_client.hpp b/src/script/api/script_client.hpp index 7400e7247c..dabd956f00 100644 --- a/src/script/api/script_client.hpp +++ b/src/script/api/script_client.hpp @@ -45,7 +45,7 @@ public: * @pre ResolveClientID(client) != CLIENT_INVALID. * @return The name of the given client. */ - static char *GetName(ClientID client); + static std::optional GetName(ClientID client); /** * Get the company in which the given client is playing. diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp index 9d1b452d7e..a50590c09b 100644 --- a/src/script/api/script_company.cpp +++ b/src/script/api/script_company.cpp @@ -57,10 +57,10 @@ return ScriptObject::Command::Do(text); } -/* static */ char *ScriptCompany::GetName(ScriptCompany::CompanyID company) +/* static */ std::optional ScriptCompany::GetName(ScriptCompany::CompanyID company) { company = ResolveCompanyID(company); - if (company == COMPANY_INVALID) return nullptr; + if (company == COMPANY_INVALID) return std::nullopt; ::SetDParam(0, company); return GetString(STR_COMPANY_NAME); @@ -79,20 +79,13 @@ return ScriptObject::Command::Do(text); } -/* static */ char *ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company) +/* static */ std::optional ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company) { company = ResolveCompanyID(company); + if (company == COMPANY_INVALID) return std::nullopt; - static const int len = 64; - char *president_name = MallocT(len); - if (company != COMPANY_INVALID) { - ::SetDParam(0, company); - ::GetString(president_name, STR_PRESIDENT_NAME, &president_name[len - 1]); - } else { - *president_name = '\0'; - } - - return president_name; + ::SetDParam(0, company); + return GetString(STR_PRESIDENT_NAME); } /* static */ bool ScriptCompany::SetPresidentGender(Gender gender) diff --git a/src/script/api/script_company.hpp b/src/script/api/script_company.hpp index cfb6cabf94..2afb47a3ff 100644 --- a/src/script/api/script_company.hpp +++ b/src/script/api/script_company.hpp @@ -150,7 +150,7 @@ public: * @pre ResolveCompanyID(company) != COMPANY_INVALID. * @return The name of the given company. */ - static char *GetName(CompanyID company); + static std::optional GetName(CompanyID company); /** * Set the name of your president. @@ -168,7 +168,7 @@ public: * @pre ResolveCompanyID(company) != COMPANY_INVALID. * @return The name of the president of the given company. */ - static char *GetPresidentName(CompanyID company); + static std::optional GetPresidentName(CompanyID company); /** * Set the gender of the president of your company. diff --git a/src/script/api/script_engine.cpp b/src/script/api/script_engine.cpp index db16861212..d833a91a33 100644 --- a/src/script/api/script_engine.cpp +++ b/src/script/api/script_engine.cpp @@ -41,9 +41,9 @@ return e != nullptr && ::IsEngineBuildable(engine_id, e->type, ScriptObject::GetCompany()); } -/* static */ char *ScriptEngine::GetName(EngineID engine_id) +/* static */ std::optional ScriptEngine::GetName(EngineID engine_id) { - if (!IsValidEngine(engine_id)) return nullptr; + if (!IsValidEngine(engine_id)) return std::nullopt; ::SetDParam(0, engine_id); return GetString(STR_ENGINE_NAME); diff --git a/src/script/api/script_engine.hpp b/src/script/api/script_engine.hpp index 73414c1b4c..2baed5bec3 100644 --- a/src/script/api/script_engine.hpp +++ b/src/script/api/script_engine.hpp @@ -44,7 +44,7 @@ public: * @pre IsValidEngine(engine_id). * @return The name the engine has. */ - static char *GetName(EngineID engine_id); + static std::optional GetName(EngineID engine_id); /** * Get the cargo-type of an engine. In case it can transport multiple cargoes, it diff --git a/src/script/api/script_error.cpp b/src/script/api/script_error.cpp index 318a6ff494..3d365c23bb 100644 --- a/src/script/api/script_error.cpp +++ b/src/script/api/script_error.cpp @@ -23,9 +23,9 @@ ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::S return ScriptObject::GetLastError(); } -/* static */ char *ScriptError::GetLastErrorString() +/* static */ std::optional ScriptError::GetLastErrorString() { - return stredup((*error_map_string.find(ScriptError::GetLastError())).second); + return (*error_map_string.find(ScriptError::GetLastError())).second; } /* static */ ScriptErrorType ScriptError::StringToError(StringID internal_string_id) diff --git a/src/script/api/script_error.hpp b/src/script/api/script_error.hpp index e461a7d46d..601e99dd0f 100644 --- a/src/script/api/script_error.hpp +++ b/src/script/api/script_error.hpp @@ -193,7 +193,7 @@ public: * Get the last error in string format (for human readability). * @return An ErrorMessage enum item, as string. */ - static char *GetLastErrorString(); + static std::optional GetLastErrorString(); /** * Get the error based on the OpenTTD StringID. diff --git a/src/script/api/script_event_types.cpp b/src/script/api/script_event_types.cpp index 9eb01a3682..e90b85f0a9 100644 --- a/src/script/api/script_event_types.cpp +++ b/src/script/api/script_event_types.cpp @@ -28,9 +28,9 @@ bool ScriptEventEnginePreview::IsEngineValid() const return e != nullptr && e->IsEnabled(); } -char *ScriptEventEnginePreview::GetName() +std::optional ScriptEventEnginePreview::GetName() { - if (!this->IsEngineValid()) return nullptr; + if (!this->IsEngineValid()) return std::nullopt; ::SetDParam(0, this->engine); return GetString(STR_ENGINE_NAME); diff --git a/src/script/api/script_event_types.hpp b/src/script/api/script_event_types.hpp index 24e50ae4b0..43eff7dc1b 100644 --- a/src/script/api/script_event_types.hpp +++ b/src/script/api/script_event_types.hpp @@ -239,7 +239,7 @@ public: * Get the name of the offered engine. * @return The name the engine has. */ - char *GetName(); + std::optional GetName(); /** * Get the cargo-type of the offered engine. In case it can transport multiple cargoes, it diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp index 1f83c55d0c..490da7564a 100644 --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -68,9 +68,9 @@ return ScriptObject::Command::Do(AlterGroupMode::Rename, group_id, 0, text); } -/* static */ char *ScriptGroup::GetName(GroupID group_id) +/* static */ std::optional ScriptGroup::GetName(GroupID group_id) { - if (!IsValidGroup(group_id)) return nullptr; + if (!IsValidGroup(group_id)) return std::nullopt; ::SetDParam(0, group_id); return GetString(STR_GROUP_NAME); diff --git a/src/script/api/script_group.hpp b/src/script/api/script_group.hpp index ba61be3fd4..9971525765 100644 --- a/src/script/api/script_group.hpp +++ b/src/script/api/script_group.hpp @@ -84,7 +84,7 @@ public: * @pre IsValidGroup(group_id). * @return The name the group has. */ - static char *GetName(GroupID group_id); + static std::optional GetName(GroupID group_id); /** * Set parent group of a group. diff --git a/src/script/api/script_industry.cpp b/src/script/api/script_industry.cpp index 1a9203b23e..5bc98251c1 100644 --- a/src/script/api/script_industry.cpp +++ b/src/script/api/script_industry.cpp @@ -42,9 +42,9 @@ return ::GetIndustryIndex(tile); } -/* static */ char *ScriptIndustry::GetName(IndustryID industry_id) +/* static */ std::optional ScriptIndustry::GetName(IndustryID industry_id) { - if (!IsValidIndustry(industry_id)) return nullptr; + if (!IsValidIndustry(industry_id)) return std::nullopt; ::SetDParam(0, industry_id); return GetString(STR_INDUSTRY_NAME); diff --git a/src/script/api/script_industry.hpp b/src/script/api/script_industry.hpp index d7fca868d0..e396da62cb 100644 --- a/src/script/api/script_industry.hpp +++ b/src/script/api/script_industry.hpp @@ -79,7 +79,7 @@ public: * @pre IsValidIndustry(industry_id). * @return The name of the industry. */ - static char *GetName(IndustryID industry_id); + static std::optional GetName(IndustryID industry_id); /** * Set the custom text of an industry, shown in the GUI. diff --git a/src/script/api/script_industrytype.cpp b/src/script/api/script_industrytype.cpp index 3d7073e40b..8d47e338cb 100644 --- a/src/script/api/script_industrytype.cpp +++ b/src/script/api/script_industrytype.cpp @@ -57,9 +57,9 @@ return ::GetIndustrySpec(industry_type)->GetConstructionCost(); } -/* static */ char *ScriptIndustryType::GetName(IndustryType industry_type) +/* static */ std::optional ScriptIndustryType::GetName(IndustryType industry_type) { - if (!IsValidIndustryType(industry_type)) return nullptr; + if (!IsValidIndustryType(industry_type)) return std::nullopt; return GetString(::GetIndustrySpec(industry_type)->name); } diff --git a/src/script/api/script_industrytype.hpp b/src/script/api/script_industrytype.hpp index a581e58444..dfa4586f02 100644 --- a/src/script/api/script_industrytype.hpp +++ b/src/script/api/script_industrytype.hpp @@ -39,7 +39,7 @@ public: * @pre IsValidIndustryType(industry_type). * @return The name of an industry. */ - static char *GetName(IndustryType industry_type); + static std::optional GetName(IndustryType industry_type); /** * Get a list of CargoID possible produced by this industry-type. diff --git a/src/script/api/script_newgrf.cpp b/src/script/api/script_newgrf.cpp index 10a0a966c3..884918c549 100644 --- a/src/script/api/script_newgrf.cpp +++ b/src/script/api/script_newgrf.cpp @@ -50,15 +50,15 @@ ScriptNewGRFList::ScriptNewGRFList() return 0; } -/* static */ char *ScriptNewGRF::GetName(SQInteger grfid) +/* static */ std::optional ScriptNewGRF::GetName(SQInteger grfid) { grfid = BSWAP32(GB(grfid, 0, 32)); // Match people's expectations. for (auto c = _grfconfig; c != nullptr; c = c->next) { if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) { - return ::stredup(c->GetName()); + return c->GetName(); } } - return nullptr; + return std::nullopt; } diff --git a/src/script/api/script_newgrf.hpp b/src/script/api/script_newgrf.hpp index c11202a112..7b9b2286c5 100644 --- a/src/script/api/script_newgrf.hpp +++ b/src/script/api/script_newgrf.hpp @@ -50,7 +50,7 @@ public: * @pre ScriptNewGRF::IsLoaded(grfid). * @return The name of the NewGRF or null if no name is defined. */ - static char *GetName(SQInteger grfid); + static std::optional GetName(SQInteger grfid); }; #endif /* SCRIPT_NEWGRF_HPP */ diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index b0692c2aa7..0f78464432 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -218,12 +218,9 @@ ScriptObject::ActiveInstance::~ActiveInstance() return GetStorage()->log_data; } -/* static */ char *ScriptObject::GetString(StringID string) +/* static */ std::string ScriptObject::GetString(StringID string) { - char buffer[64]; - ::GetString(buffer, string, lastof(buffer)); - ::StrMakeValidInPlace(buffer, lastof(buffer), SVS_NONE); - return ::stredup(buffer); + return ::StrMakeValid(::GetString(string)); } /* static */ void ScriptObject::SetCallbackVariable(int index, int value) diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 606bc7c584..f4b5694bcc 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -282,7 +282,7 @@ protected: /** * Get an allocated string with all control codes stripped off. */ - static char *GetString(StringID string); + static std::string GetString(StringID string); private: /* Helper functions for DoCommand. */ diff --git a/src/script/api/script_objecttype.cpp b/src/script/api/script_objecttype.cpp index 842bdac4b7..7e036a3e3d 100644 --- a/src/script/api/script_objecttype.cpp +++ b/src/script/api/script_objecttype.cpp @@ -23,9 +23,9 @@ return ObjectSpec::Get(object_type)->IsEverAvailable(); } -/* static */ char *ScriptObjectType::GetName(ObjectType object_type) +/* static */ std::optional ScriptObjectType::GetName(ObjectType object_type) { - EnforcePrecondition(nullptr, IsValidObjectType(object_type)); + EnforcePrecondition(std::nullopt, IsValidObjectType(object_type)); return GetString(ObjectSpec::Get(object_type)->name); } diff --git a/src/script/api/script_objecttype.hpp b/src/script/api/script_objecttype.hpp index c4b9949308..9a5c47278f 100644 --- a/src/script/api/script_objecttype.hpp +++ b/src/script/api/script_objecttype.hpp @@ -33,7 +33,7 @@ public: * @pre IsValidObjectType(object_type). * @return The name of an object. */ - static char *GetName(ObjectType object_type); + static std::optional GetName(ObjectType object_type); /** * Get the number of views for an object-type. diff --git a/src/script/api/script_rail.cpp b/src/script/api/script_rail.cpp index fbbb2038f9..5f97941ebd 100644 --- a/src/script/api/script_rail.cpp +++ b/src/script/api/script_rail.cpp @@ -24,9 +24,9 @@ #include "../../safeguards.h" -/* static */ char *ScriptRail::GetName(RailType rail_type) +/* static */ std::optional ScriptRail::GetName(RailType rail_type) { - if (!IsRailTypeAvailable(rail_type)) return nullptr; + if (!IsRailTypeAvailable(rail_type)) return std::nullopt; return GetString(GetRailTypeInfo((::RailType)rail_type)->strings.menu_text); } diff --git a/src/script/api/script_rail.hpp b/src/script/api/script_rail.hpp index 83a141e554..31425721e6 100644 --- a/src/script/api/script_rail.hpp +++ b/src/script/api/script_rail.hpp @@ -101,7 +101,7 @@ public: * means that the name could be something like "Maglev construction" instead * of just "Maglev". */ - static char *GetName(RailType rail_type); + static std::optional GetName(RailType rail_type); /** * Checks whether the given tile is actually a tile with rail that can be diff --git a/src/script/api/script_road.cpp b/src/script/api/script_road.cpp index 2326247753..8a44793029 100644 --- a/src/script/api/script_road.cpp +++ b/src/script/api/script_road.cpp @@ -25,9 +25,9 @@ return ScriptCargo::HasCargoClass(cargo_type, ScriptCargo::CC_PASSENGERS) ? ROADVEHTYPE_BUS : ROADVEHTYPE_TRUCK; } -/* static */ char *ScriptRoad::GetName(RoadType road_type) +/* static */ std::optional ScriptRoad::GetName(RoadType road_type) { - if (!IsRoadTypeAvailable(road_type)) return nullptr; + if (!IsRoadTypeAvailable(road_type)) return std::nullopt; return GetString(GetRoadTypeInfo((::RoadType)road_type)->strings.name); } diff --git a/src/script/api/script_road.hpp b/src/script/api/script_road.hpp index 78d2f1b77c..0f3968226a 100644 --- a/src/script/api/script_road.hpp +++ b/src/script/api/script_road.hpp @@ -90,7 +90,7 @@ public: * @pre IsRoadTypeAvailable(road_type). * @return The name the road type has. */ - static char *GetName(RoadType road_type); + static std::optional GetName(RoadType road_type); /** * Determines whether a busstop or a truckstop is needed to transport a certain cargo. diff --git a/src/script/api/script_sign.cpp b/src/script/api/script_sign.cpp index 01488b8953..59077aa55c 100644 --- a/src/script/api/script_sign.cpp +++ b/src/script/api/script_sign.cpp @@ -47,9 +47,9 @@ return ScriptObject::Command::Do(sign_id, text); } -/* static */ char *ScriptSign::GetName(SignID sign_id) +/* static */ std::optional ScriptSign::GetName(SignID sign_id) { - if (!IsValidSign(sign_id)) return nullptr; + if (!IsValidSign(sign_id)) return std::nullopt; ::SetDParam(0, sign_id); return GetString(STR_SIGN_NAME); diff --git a/src/script/api/script_sign.hpp b/src/script/api/script_sign.hpp index 06a17da92b..1569ad9932 100644 --- a/src/script/api/script_sign.hpp +++ b/src/script/api/script_sign.hpp @@ -55,7 +55,7 @@ public: * @pre IsValidSign(sign_id). * @return The name of the sign. */ - static char *GetName(SignID sign_id); + static std::optional GetName(SignID sign_id); /** * Get the owner of a sign. diff --git a/src/script/api/script_town.cpp b/src/script/api/script_town.cpp index c39b9e1185..b250fa8b65 100644 --- a/src/script/api/script_town.cpp +++ b/src/script/api/script_town.cpp @@ -32,9 +32,9 @@ return ::Town::IsValidID(town_id); } -/* static */ char *ScriptTown::GetName(TownID town_id) +/* static */ std::optional ScriptTown::GetName(TownID town_id) { - if (!IsValidTown(town_id)) return nullptr; + if (!IsValidTown(town_id)) return std::nullopt; ::SetDParam(0, town_id); return GetString(STR_TOWN_NAME); diff --git a/src/script/api/script_town.hpp b/src/script/api/script_town.hpp index bd2167f0f7..b23840024b 100644 --- a/src/script/api/script_town.hpp +++ b/src/script/api/script_town.hpp @@ -142,7 +142,7 @@ public: * @pre IsValidTown(town_id). * @return The name of the town. */ - static char *GetName(TownID town_id); + static std::optional GetName(TownID town_id); /** * Rename a town. diff --git a/src/script/api/script_vehicle.cpp b/src/script/api/script_vehicle.cpp index 144e5e3e29..1e06e4f5fb 100644 --- a/src/script/api/script_vehicle.cpp +++ b/src/script/api/script_vehicle.cpp @@ -299,9 +299,9 @@ return ::Vehicle::Get(vehicle_id)->unitnumber; } -/* static */ char *ScriptVehicle::GetName(VehicleID vehicle_id) +/* static */ std::optional ScriptVehicle::GetName(VehicleID vehicle_id) { - if (!IsPrimaryVehicle(vehicle_id)) return nullptr; + if (!IsPrimaryVehicle(vehicle_id)) return std::nullopt; ::SetDParam(0, vehicle_id); return GetString(STR_VEHICLE_NAME); diff --git a/src/script/api/script_vehicle.hpp b/src/script/api/script_vehicle.hpp index e76ffccec5..bd6d79ebcc 100644 --- a/src/script/api/script_vehicle.hpp +++ b/src/script/api/script_vehicle.hpp @@ -137,7 +137,7 @@ public: * @pre IsPrimaryVehicle(vehicle_id). * @return The name the vehicle has. */ - static char *GetName(VehicleID vehicle_id); + static std::optional GetName(VehicleID vehicle_id); /** * Get the owner of a vehicle. diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp index ed220b8b40..c73402c26d 100644 --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -52,11 +52,22 @@ namespace SQConvert { template <> struct Return { static inline int Set(HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, TileIndex res) { sq_pushinteger(vm, (int32)res.value); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } }; - template <> struct Return { static inline int Set(HSQUIRRELVM vm, char *res) { if (res == nullptr) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); free(res); } return 1; } }; - template <> struct Return { static inline int Set(HSQUIRRELVM vm, const char *res) { if (res == nullptr) sq_pushnull(vm); else { sq_pushstring(vm, res, -1); } return 1; } }; + template <> struct Return { /* Do not use char *, use std::optional instead. */ }; + template <> struct Return { /* Do not use const char *, use std::optional instead. */ }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, void *res) { sq_pushuserpointer(vm, res); return 1; } }; template <> struct Return { static inline int Set(HSQUIRRELVM vm, HSQOBJECT res) { sq_pushobject(vm, res); return 1; } }; + template <> struct Return> { + static inline int Set(HSQUIRRELVM vm, std::optional res) { + if (res.has_value()) { + sq_pushstring(vm, res.value(), -1); + } else { + sq_pushnull(vm); + } + return 1; + } + }; + /** * To get a param from squirrel, we use this helper class. It converts to the right format. * We use a class instead of a plain function to allow us to use partial template specializations.