Merge branch 'master' into jgrpp

# Conflicts:
#	.github/workflows/ci-build.yml
#	.github/workflows/release-linux.yml
#	.github/workflows/release-macos.yml
#	.github/workflows/release-source.yml
#	.github/workflows/release.yml
#	CMakeLists.txt
#	COMPILING.md
#	src/ai/ai_core.cpp
#	src/ai/ai_gui.cpp
#	src/bridge_gui.cpp
#	src/company_gui.cpp
#	src/console_cmds.cpp
#	src/core/CMakeLists.txt
#	src/core/smallmap_type.hpp
#	src/disaster_vehicle.h
#	src/effectvehicle_base.h
#	src/fontcache.cpp
#	src/game/game_core.cpp
#	src/game/game_gui.cpp
#	src/gamelog.cpp
#	src/gamelog_internal.h
#	src/group_gui.cpp
#	src/linkgraph/linkgraph.h
#	src/misc.cpp
#	src/network/core/config.h
#	src/network/core/udp.cpp
#	src/network/network_chat_gui.cpp
#	src/network/network_content_gui.cpp
#	src/network/network_gui.cpp
#	src/newgrf.cpp
#	src/newgrf_gui.cpp
#	src/newgrf_profiling.cpp
#	src/newgrf_profiling.h
#	src/object_gui.cpp
#	src/openttd.cpp
#	src/openttd.h
#	src/order_gui.cpp
#	src/os/windows/font_win32.cpp
#	src/rail_gui.cpp
#	src/road.cpp
#	src/road_gui.cpp
#	src/saveload/afterload.cpp
#	src/saveload/saveload.h
#	src/script/api/script_controller.cpp
#	src/script/api/script_roadtypelist.cpp
#	src/script/script_config.cpp
#	src/script/script_config.hpp
#	src/script/script_instance.cpp
#	src/script/script_scanner.cpp
#	src/script/squirrel.cpp
#	src/script/squirrel_helper.hpp
#	src/settings_gui.cpp
#	src/settings_internal.h
#	src/settings_type.h
#	src/table/settings/network_private_settings.ini
#	src/timetable_gui.cpp
#	src/vehicle.cpp
#	src/vehicle_base.h
#	src/window_gui.h
This commit is contained in:
Jonathan G Rennison
2023-07-01 01:08:35 +01:00
246 changed files with 2023 additions and 1211 deletions

View File

@@ -24,9 +24,9 @@
return st != nullptr && (st->owner == ScriptObject::GetCompany() || ScriptCompanyMode::IsDeity() || st->owner == OWNER_NONE);
}
/* static */ char *ScriptBaseStation::GetName(StationID station_id)
/* static */ std::optional<std::string> 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);

View File

@@ -13,6 +13,8 @@
#include "script_text.hpp"
#include "script_date.hpp"
#include <optional>
/**
* Base class for stations and waypoints.
* @api ai game
@@ -43,7 +45,7 @@ public:
* @pre IsValidBaseStation(station_id).
* @return The name of the station.
*/
static char *GetName(StationID station_id);
static std::optional<std::string> GetName(StationID station_id);
/**
* Set the name this basestation.

View File

@@ -141,10 +141,10 @@ static void _DoCommandReturnBuildBridge1(class ScriptInstance *instance)
return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
}
/* static */ char *ScriptBridge::GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type)
/* static */ std::optional<std::string> 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]);
}

View File

@@ -11,6 +11,7 @@
#define SCRIPT_BRIDGE_HPP
#include "script_vehicle.hpp"
#include <optional>
/**
* Class that handles all bridge related functions.
@@ -69,7 +70,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<std::string> GetName(BridgeID bridge_id, ScriptVehicle::VehicleType vehicle_type);
/**
* Get the maximum speed of a bridge.

View File

@@ -27,26 +27,25 @@
return (towneffect_type >= (TownEffect)TE_BEGIN && towneffect_type < (TownEffect)TE_END);
}
/* static */ char *ScriptCargo::GetName(CargoID cargo_type)
/* static */ std::optional<std::string> 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<std::string> 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<char>(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;
}

View File

@@ -13,6 +13,7 @@
#include "script_object.hpp"
#include "../../cargotype.h"
#include "../../linkgraph/linkgraph_type.h"
#include <optional>
/**
* Class that handles all cargo related functions.
@@ -90,7 +91,7 @@ public:
* @pre IsValidCargo(cargo_type).
* @return The name of the cargo type.
*/
static char *GetName(CargoID cargo_type);
static std::optional<std::string> GetName(CargoID cargo_type);
/**
* Gets the string representation of the cargo label.
@@ -107,7 +108,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<std::string> GetCargoLabel(CargoID cargo_type);
/**
* Checks whether the give cargo is a freight or not.

View File

@@ -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<std::string> 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)

View File

@@ -14,6 +14,7 @@
#include "script_date.hpp"
#include "script_company.hpp"
#include "../../network/network_type.h"
#include <optional>
/**
* Class that handles all client related functions.
@@ -45,7 +46,7 @@ public:
* @pre ResolveClientID(client) != CLIENT_INVALID.
* @return The name of the given client.
*/
static char *GetName(ClientID client);
static std::optional<std::string> GetName(ClientID client);
/**
* Get the company in which the given client is playing.

View File

@@ -53,10 +53,10 @@
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_COMPANY, text);
}
/* static */ char *ScriptCompany::GetName(ScriptCompany::CompanyID company)
/* static */ std::optional<std::string> 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);
@@ -75,20 +75,13 @@
return ScriptObject::DoCommand(0, 0, 0, CMD_RENAME_PRESIDENT, text);
}
/* static */ char *ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company)
/* static */ std::optional<std::string> ScriptCompany::GetPresidentName(ScriptCompany::CompanyID company)
{
company = ResolveCompanyID(company);
if (company == COMPANY_INVALID) return std::nullopt;
static const int len = 64;
char *president_name = MallocT<char>(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)

View File

@@ -14,6 +14,7 @@
#include "../../economy_type.h"
#include "../../livery.h"
#include "../../gfx_type.h"
#include <optional>
/**
* Class that handles all company related functions.
@@ -151,7 +152,7 @@ public:
* @pre ResolveCompanyID(company) != COMPANY_INVALID.
* @return The name of the given company.
*/
static char *GetName(CompanyID company);
static std::optional<std::string> GetName(CompanyID company);
/**
* Set the name of your president.
@@ -169,7 +170,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<std::string> GetPresidentName(CompanyID company);
/**
* Set the gender of the president of your company.

View File

@@ -45,15 +45,13 @@
throw Script_Suspend(ticks, nullptr);
}
/* static */ void ScriptController::Break(const char* message)
/* static */ void ScriptController::Break(const std::string &message)
{
if (_network_dedicated || !_settings_client.gui.ai_developer_tools) return;
ScriptObject::GetActiveInstance()->Pause();
char log_message[1024];
seprintf(log_message, lastof(log_message), "Break: %s", message);
ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, log_message);
ScriptLog::Log(ScriptLogTypes::LOG_SQ_ERROR, fmt::format("Break: {}", message));
/* Inform script developer that their script has been paused and
* needs manual action to continue. */
@@ -64,7 +62,7 @@
}
}
/* static */ void ScriptController::Print(bool error_msg, const char *message)
/* static */ void ScriptController::Print(bool error_msg, const std::string &message)
{
ScriptLog::Log(error_msg ? ScriptLogTypes::LOG_SQ_ERROR : ScriptLogTypes::LOG_SQ_INFO, message);
}
@@ -91,7 +89,7 @@ ScriptController::ScriptController(CompanyID company) :
Squirrel::DecreaseOps(ScriptObject::GetActiveInstance()->engine->GetVM(), amount);
}
/* static */ int ScriptController::GetSetting(const char *name)
/* static */ int ScriptController::GetSetting(const std::string &name)
{
return ScriptObject::GetActiveInstance()->GetSetting(name);
}
@@ -101,7 +99,7 @@ ScriptController::ScriptController(CompanyID company) :
return _openttd_newgrf_version;
}
/* static */ HSQOBJECT ScriptController::Import(const char *library, const char *class_name, int version)
/* static */ HSQOBJECT ScriptController::Import(const std::string &library, const std::string &class_name, int version)
{
ScriptController *controller = ScriptObject::GetActiveInstance()->GetController();
Squirrel *engine = ScriptObject::GetActiveInstance()->engine;
@@ -109,9 +107,7 @@ ScriptController::ScriptController(CompanyID company) :
ScriptInfo *lib = ScriptObject::GetActiveInstance()->FindLibrary(library, version);
if (lib == nullptr) {
char error[1024];
seprintf(error, lastof(error), "couldn't find library '%s' with version %d", library, version);
throw sq_throwerror(vm, error);
throw sq_throwerror(vm, fmt::format("couldn't find library '{}' with version {}", library, version));
}
/* Internally we store libraries as 'library.version' */
@@ -138,9 +134,7 @@ ScriptController::ScriptController(CompanyID company) :
sq_newclass(vm, SQFalse);
/* Load the library */
if (!engine->LoadScript(vm, lib->GetMainScript(), false)) {
char error[1024];
seprintf(error, lastof(error), "there was a compile error when importing '%s' version %d", library, version);
throw sq_throwerror(vm, error);
throw sq_throwerror(vm, fmt::format("there was a compile error when importing '{}' version {}", library, version));
}
/* Create the fake class */
sq_newslot(vm, -3, SQFalse);
@@ -157,15 +151,13 @@ ScriptController::ScriptController(CompanyID company) :
}
sq_pushstring(vm, lib->GetInstanceName(), -1);
if (SQ_FAILED(sq_get(vm, -2))) {
char error[1024];
seprintf(error, lastof(error), "unable to find class '%s' in the library '%s' version %d", lib->GetInstanceName(), library, version);
throw sq_throwerror(vm, error);
throw sq_throwerror(vm, fmt::format("unable to find class '{}' in the library '{}' version {}", lib->GetInstanceName(), library, version));
}
HSQOBJECT obj;
sq_getstackobj(vm, -1, &obj);
sq_pop(vm, 3);
if (StrEmpty(class_name)) return obj;
if (class_name.empty()) return obj;
/* Now link the name the user wanted to our 'fake' class */
sq_pushobject(vm, parent);

View File

@@ -131,7 +131,7 @@ public:
* @param name The name of the setting.
* @return the value for the setting, or -1 if the setting is not known.
*/
static int GetSetting(const char *name);
static int GetSetting(const std::string &name);
/**
* Get the OpenTTD version of this executable. The version is formatted
@@ -187,7 +187,7 @@ public:
* @note gui.ai_developer_tools setting must be enabled or the break is
* ignored.
*/
static void Break(const char* message);
static void Break(const std::string &message);
/**
* When Squirrel triggers a print, this function is called.
@@ -196,7 +196,7 @@ public:
* @param message The message Squirrel logged.
* @note Use ScriptLog.Info/Warning/Error instead of 'print'.
*/
static void Print(bool error_msg, const char *message);
static void Print(bool error_msg, const std::string &message);
/**
* Import a library.
@@ -207,7 +207,7 @@ public:
* @return The loaded library object. If class_name is set, it is also available (under the scope of the import) under that name.
* @note This command can be called from the global space, and does not need an instance.
*/
static HSQOBJECT Import(const char *library, const char *class_name, int version);
static HSQOBJECT Import(const std::string &library, const std::string &class_name, int version);
private:
typedef std::map<std::string, std::string, CaseInsensitiveComparator> LoadedLibraryList; ///< The type for loaded libraries.

View File

@@ -40,9 +40,9 @@
return e != nullptr && ::IsEngineBuildable(engine_id, e->type, ScriptObject::GetCompany());
}
/* static */ char *ScriptEngine::GetName(EngineID engine_id)
/* static */ std::optional<std::string> 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);

View File

@@ -14,6 +14,7 @@
#include "script_rail.hpp"
#include "script_airport.hpp"
#include "script_date.hpp"
#include <optional>
/**
* Class that handles all engine related functions.
@@ -44,7 +45,7 @@ public:
* @pre IsValidEngine(engine_id).
* @return The name the engine has.
*/
static char *GetName(EngineID engine_id);
static std::optional<std::string> GetName(EngineID engine_id);
/**
* Get the cargo-type of an engine. In case it can transport multiple cargoes, it

View File

@@ -23,9 +23,9 @@ ScriptError::ScriptErrorMapString ScriptError::error_map_string = ScriptError::S
return ScriptObject::GetLastError();
}
/* static */ char *ScriptError::GetLastErrorString()
/* static */ std::optional<std::string> 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)

View File

@@ -13,6 +13,7 @@
#include "script_object.hpp"
#include "script_companymode.hpp"
#include <map>
#include <optional>
/**
* Helper to write precondition enforcers for the script API in an abbreviated manner.
@@ -193,7 +194,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<std::string> GetLastErrorString();
/**
* Get the error based on the OpenTTD StringID.

View File

@@ -26,9 +26,9 @@ bool ScriptEventEnginePreview::IsEngineValid() const
return e != nullptr && e->IsEnabled();
}
char *ScriptEventEnginePreview::GetName()
std::optional<std::string> ScriptEventEnginePreview::GetName()
{
if (!this->IsEngineValid()) return nullptr;
if (!this->IsEngineValid()) return std::nullopt;
::SetDParam(0, this->engine);
return GetString(STR_ENGINE_NAME);

View File

@@ -13,6 +13,7 @@
#include "script_event.hpp"
#include "script_goal.hpp"
#include "script_window.hpp"
#include <optional>
/**
* Event Vehicle Crash, indicating a vehicle of yours is crashed.
@@ -239,7 +240,7 @@ public:
* Get the name of the offered engine.
* @return The name the engine has.
*/
char *GetName();
std::optional<std::string> GetName();
/**
* Get the cargo-type of the offered engine. In case it can transport multiple cargoes, it

View File

@@ -15,13 +15,13 @@
#include "../../safeguards.h"
/* static */ bool ScriptGameSettings::IsValid(const char *setting)
/* static */ bool ScriptGameSettings::IsValid(const std::string &setting)
{
const SettingDesc *sd = GetSettingFromName(setting);
return sd != nullptr && sd->IsIntSetting();
}
/* static */ SQInteger ScriptGameSettings::GetValue(const char *setting)
/* static */ SQInteger ScriptGameSettings::GetValue(const std::string &setting)
{
if (!IsValid(setting)) return -1;
@@ -30,7 +30,7 @@
return sd->AsIntSetting()->Read(&_settings_game);
}
/* static */ bool ScriptGameSettings::SetValue(const char *setting, SQInteger value)
/* static */ bool ScriptGameSettings::SetValue(const std::string &setting, SQInteger value)
{
EnforceDeityOrCompanyModeValid(false);
if (!IsValid(setting)) return false;

View File

@@ -44,7 +44,7 @@ public:
* @note Results achieved in the past offer no guarantee for the future.
* @return True if and only if the setting is valid.
*/
static bool IsValid(const char *setting);
static bool IsValid(const std::string &setting);
/**
* Gets the value of the game setting.
@@ -57,7 +57,7 @@ public:
* @note Results achieved in the past offer no guarantee for the future.
* @return The value for the setting.
*/
static SQInteger GetValue(const char *setting);
static SQInteger GetValue(const std::string &setting);
/**
* Sets the value of the game setting.
@@ -69,7 +69,7 @@ public:
* @note Results achieved in the past offer no guarantee for the future.
* @api -ai
*/
static bool SetValue(const char *setting, SQInteger value);
static bool SetValue(const std::string &setting, SQInteger value);
/**
* Checks whether the given vehicle-type is disabled for companies.

View File

@@ -65,9 +65,9 @@
return ScriptObject::DoCommand(0, group_id, 0, CMD_ALTER_GROUP, text);
}
/* static */ char *ScriptGroup::GetName(GroupID group_id)
/* static */ std::optional<std::string> 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);

View File

@@ -12,6 +12,7 @@
#include "script_vehicle.hpp"
#include "../../group_type.h"
#include <optional>
/**
* Class that handles all group related functions.
@@ -84,7 +85,7 @@ public:
* @pre IsValidGroup(group_id).
* @return The name the group has.
*/
static char *GetName(GroupID group_id);
static std::optional<std::string> GetName(GroupID group_id);
/**
* Set parent group of a group.

View File

@@ -40,9 +40,9 @@
return ::GetIndustryIndex(tile);
}
/* static */ char *ScriptIndustry::GetName(IndustryID industry_id)
/* static */ std::optional<std::string> 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);

View File

@@ -14,6 +14,7 @@
#include "script_date.hpp"
#include "script_object.hpp"
#include "../../industry.h"
#include <optional>
/**
* Class that handles all industry related functions.
@@ -79,7 +80,7 @@ public:
* @pre IsValidIndustry(industry_id).
* @return The name of the industry.
*/
static char *GetName(IndustryID industry_id);
static std::optional<std::string> GetName(IndustryID industry_id);
/**
* Set the custom text of an industry, shown in the GUI.

View File

@@ -56,9 +56,9 @@
return ::GetIndustrySpec(industry_type)->GetConstructionCost();
}
/* static */ char *ScriptIndustryType::GetName(IndustryType industry_type)
/* static */ std::optional<std::string> ScriptIndustryType::GetName(IndustryType industry_type)
{
if (!IsValidIndustryType(industry_type)) return nullptr;
if (!IsValidIndustryType(industry_type)) return std::nullopt;
return GetString(::GetIndustrySpec(industry_type)->name);
}

View File

@@ -11,6 +11,7 @@
#define SCRIPT_INDUSTRYTYPE_HPP
#include "script_list.hpp"
#include <optional>
/**
* Class that handles all industry-type related functions.
@@ -39,7 +40,7 @@ public:
* @pre IsValidIndustryType(industry_type).
* @return The name of an industry.
*/
static char *GetName(IndustryType industry_type);
static std::optional<std::string> GetName(IndustryType industry_type);
/**
* Get a list of CargoID possible produced by this industry-type.

View File

@@ -28,7 +28,7 @@ public:
/**
* Virtual dtor, needed to mute warnings.
*/
virtual ~ScriptListSorter() { }
virtual ~ScriptListSorter() = default;
/**
* Get the first item of the sorter.

View File

@@ -17,22 +17,22 @@
#include "../../safeguards.h"
/* static */ void ScriptLog::Info(const char *message)
/* static */ void ScriptLog::Info(const std::string &message)
{
ScriptLog::Log(ScriptLogTypes::LOG_INFO, message);
}
/* static */ void ScriptLog::Warning(const char *message)
/* static */ void ScriptLog::Warning(const std::string &message)
{
ScriptLog::Log(ScriptLogTypes::LOG_WARNING, message);
}
/* static */ void ScriptLog::Error(const char *message)
/* static */ void ScriptLog::Error(const std::string &message)
{
ScriptLog::Log(ScriptLogTypes::LOG_ERROR, message);
}
/* static */ void ScriptLog::Log(ScriptLogTypes::ScriptLogType level, const char *message)
/* static */ void ScriptLog::Log(ScriptLogTypes::ScriptLogType level, const std::string &message)
{
ScriptLogTypes::LogData &logdata = ScriptObject::GetLogData();
@@ -43,8 +43,7 @@
line.type = level;
/* Cut string after first \n */
const char *newline = strchr(message, '\n');
line.text = std::string(message, 0, newline == nullptr ? strlen(message) : newline - message);
line.text = message.substr(0, message.find_first_of('\n'));
char logc;

View File

@@ -27,21 +27,21 @@ public:
* @param message The message to log.
* @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs.
*/
static void Info(const char *message);
static void Info(const std::string &message);
/**
* Print a Warning message to the logs.
* @param message The message to log.
* @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs.
*/
static void Warning(const char *message);
static void Warning(const std::string &message);
/**
* Print an Error message to the logs.
* @param message The message to log.
* @note Special characters such as U+0000-U+0019 and U+E000-U+E1FF are not supported and removed or replaced by a question mark. This includes newlines and tabs.
*/
static void Error(const char *message);
static void Error(const std::string &message);
/**
* Log this message once.
@@ -53,7 +53,7 @@ private:
/**
* Internal command to log the message in a common way.
*/
static void Log(ScriptLogTypes::ScriptLogType level, const char *message);
static void Log(ScriptLogTypes::ScriptLogType level, const std::string &message);
};
#endif /* SCRIPT_LOG_HPP */

View File

@@ -50,15 +50,15 @@ ScriptNewGRFList::ScriptNewGRFList()
return 0;
}
/* static */ char *ScriptNewGRF::GetName(SQInteger grfid)
/* static */ std::optional<std::string> 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;
}

View File

@@ -11,6 +11,7 @@
#define SCRIPT_NEWGRF_HPP
#include "script_list.hpp"
#include <optional>
/**
* Create a list of loaded NewGRFs.
@@ -50,7 +51,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<std::string> GetName(SQInteger grfid);
};
#endif /* SCRIPT_NEWGRF_HPP */

View File

@@ -311,12 +311,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)

View File

@@ -323,7 +323,7 @@ protected:
/**
* Get an allocated string with all control codes stripped off.
*/
static char *GetString(StringID string);
static std::string GetString(StringID string);
static bool IsNewUniqueLogMessage(const std::string &msg);

View File

@@ -22,9 +22,9 @@
return ObjectSpec::Get(object_type)->IsEverAvailable();
}
/* static */ char *ScriptObjectType::GetName(ObjectType object_type)
/* static */ std::optional<std::string> ScriptObjectType::GetName(ObjectType object_type)
{
EnforcePrecondition(nullptr, IsValidObjectType(object_type));
EnforcePrecondition(std::nullopt, IsValidObjectType(object_type));
return GetString(ObjectSpec::Get(object_type)->name);
}

View File

@@ -13,6 +13,7 @@
#include "script_list.hpp"
#include "../../newgrf_object.h"
#include <optional>
/**
* Class that handles all object-type related functions.
@@ -33,7 +34,7 @@ public:
* @pre IsValidObjectType(object_type).
* @return The name of an object.
*/
static char *GetName(ObjectType object_type);
static std::optional<std::string> GetName(ObjectType object_type);
/**
* Get the number of views for an object-type.

View File

@@ -21,9 +21,9 @@
#include "../../safeguards.h"
/* static */ char *ScriptRail::GetName(RailType rail_type)
/* static */ std::optional<std::string> 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);
}

View File

@@ -13,6 +13,7 @@
#include "script_tile.hpp"
#include "../../signal_type.h"
#include "../../track_type.h"
#include <optional>
/**
* Class that handles all rail related functions.
@@ -101,7 +102,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<std::string> GetName(RailType rail_type);
/**
* Checks whether the given tile is actually a tile with rail that can be

View File

@@ -21,9 +21,9 @@
return ScriptCargo::HasCargoClass(cargo_type, ScriptCargo::CC_PASSENGERS) ? ROADVEHTYPE_BUS : ROADVEHTYPE_TRUCK;
}
/* static */ char *ScriptRoad::GetName(RoadType road_type)
/* static */ std::optional<std::string> 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);
}
@@ -392,7 +392,7 @@ static bool NormaliseTileOffset(int32 *tile)
return false;
}
/* static */ SQInteger ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::Slope slope_, Array<> existing, TileIndex start_, TileIndex end_)
/* static */ SQInteger ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::Slope slope_, Array<> &&existing, TileIndex start_, TileIndex end_)
{
::Slope slope = (::Slope)slope_;
int32 start = start_;
@@ -433,7 +433,7 @@ static bool NormaliseTileOffset(int32 *tile)
if (HasBit(rb, i)) existing.emplace_back(neighbours[i]);
}
return ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::GetSlope(tile), existing, start - tile, end - tile);
return ScriptRoad::CanBuildConnectedRoadParts(ScriptTile::GetSlope(tile), std::move(existing), start - tile, end - tile);
}
/**

View File

@@ -13,6 +13,7 @@
#include "script_tile.hpp"
#include "../squirrel_helper_type.hpp"
#include "../../../road.h"
#include <optional>
/**
* Class that handles all road related functions.
@@ -90,7 +91,7 @@ public:
* @pre IsRoadTypeAvailable(road_type).
* @return The name the road type has.
*/
static char *GetName(RoadType road_type);
static std::optional<std::string> GetName(RoadType road_type);
/**
* Determines whether a busstop or a truckstop is needed to transport a certain cargo.
@@ -265,7 +266,7 @@ public:
* they are build or 2 when building the first part automatically
* builds the second part. -1 means the preconditions are not met.
*/
static SQInteger CanBuildConnectedRoadParts(ScriptTile::Slope slope, Array<> existing, TileIndex start, TileIndex end);
static SQInteger CanBuildConnectedRoadParts(ScriptTile::Slope slope, Array<> &&existing, TileIndex start, TileIndex end);
/**
* Lookup function for building road parts independent of whether the

View File

@@ -18,7 +18,7 @@ ScriptRoadTypeList::ScriptRoadTypeList(ScriptRoad::RoadTramTypes rtts)
EnforceDeityOrCompanyModeValid_Void();
for (RoadType rt = ROADTYPE_BEGIN; rt != ROADTYPE_END; rt++) {
if (!HasBit(rtts, GetRoadTramType(rt))) continue;
if ((ScriptCompanyMode::IsDeity() || ::HasRoadTypeAvail(ScriptObject::GetCompany(), rt)) &&
if (::HasRoadTypeAvail(ScriptObject::GetCompany(), rt) &&
!HasBit(GetRoadTypeInfo(rt)->extra_flags, RXTF_NOT_AVAILABLE_AI_GS)) {
this->AddItem(rt);
}

View File

@@ -46,9 +46,9 @@
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, text);
}
/* static */ char *ScriptSign::GetName(SignID sign_id)
/* static */ std::optional<std::string> 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);

View File

@@ -12,6 +12,7 @@
#include "script_company.hpp"
#include "script_error.hpp"
#include <optional>
/**
* Class that handles all sign related functions.
@@ -55,7 +56,7 @@ public:
* @pre IsValidSign(sign_id).
* @return The name of the sign.
*/
static char *GetName(SignID sign_id);
static std::optional<std::string> GetName(SignID sign_id);
/**
* Get the owner of a sign.

View File

@@ -23,7 +23,7 @@
#include "../../safeguards.h"
RawText::RawText(const char *text) : text(text)
RawText::RawText(const std::string &text) : text(text)
{
}

View File

@@ -42,7 +42,7 @@ public:
*/
class RawText : public Text {
public:
RawText(const char *text);
RawText(const std::string &text);
const std::string GetEncodedText() override { return this->text; }
private:

View File

@@ -32,9 +32,9 @@
return ::Town::IsValidID(town_id);
}
/* static */ char *ScriptTown::GetName(TownID town_id)
/* static */ std::optional<std::string> 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);

View File

@@ -13,6 +13,7 @@
#include "script_cargo.hpp"
#include "script_company.hpp"
#include "../../town_type.h"
#include <optional>
/**
* Class that handles all town related functions.
@@ -142,7 +143,7 @@ public:
* @pre IsValidTown(town_id).
* @return The name of the town.
*/
static char *GetName(TownID town_id);
static std::optional<std::string> GetName(TownID town_id);
/**
* Rename a town.

View File

@@ -298,9 +298,9 @@
return ::Vehicle::Get(vehicle_id)->unitnumber;
}
/* static */ char *ScriptVehicle::GetName(VehicleID vehicle_id)
/* static */ std::optional<std::string> 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);

View File

@@ -11,6 +11,7 @@
#define SCRIPT_VEHICLE_HPP
#include "script_road.hpp"
#include <optional>
/**
* Class that handles all vehicle related functions.
@@ -137,7 +138,7 @@ public:
* @pre IsPrimaryVehicle(vehicle_id).
* @return The name the vehicle has.
*/
static char *GetName(VehicleID vehicle_id);
static std::optional<std::string> GetName(VehicleID vehicle_id);
/**
* Get the owner of a vehicle.