diff --git a/cmake/scripts/SquirrelExport.cmake b/cmake/scripts/SquirrelExport.cmake index 76fbdd68ab..0e91501682 100644 --- a/cmake/scripts/SquirrelExport.cmake +++ b/cmake/scripts/SquirrelExport.cmake @@ -42,7 +42,7 @@ macro(dump_class_templates NAME) string(APPEND SQUIRREL_EXPORT "\n return Param::Get(vm, index, ptr);") string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_STRING) {") - string(APPEND SQUIRREL_EXPORT "\n return new RawText(Param::Get(vm, index, ptr));") + string(APPEND SQUIRREL_EXPORT "\n return new RawText(Param::Get(vm, index, ptr));") string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n return nullptr;") string(APPEND SQUIRREL_EXPORT "\n }") diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 03ba8666c5..a5eda5c617 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -83,7 +83,7 @@ void AIInstance::LoadDummyScript() Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI"); } -int AIInstance::GetSetting(const char *name) +int AIInstance::GetSetting(const std::string &name) { return AIConfig::GetConfig(_current_company)->GetSetting(name); } diff --git a/src/ai/ai_instance.hpp b/src/ai/ai_instance.hpp index 12b5159425..015f966ed5 100644 --- a/src/ai/ai_instance.hpp +++ b/src/ai/ai_instance.hpp @@ -23,7 +23,7 @@ public: */ void Initialize(class AIInfo *info); - int GetSetting(const char *name) override; + int GetSetting(const std::string &name) override; ScriptInfo *FindLibrary(const std::string &library, int version) override; private: diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp index 9d23588702..59d6e0d7d0 100644 --- a/src/game/game_instance.cpp +++ b/src/game/game_instance.cpp @@ -53,7 +53,7 @@ void GameInstance::RegisterAPI() if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died(); } -int GameInstance::GetSetting(const char *name) +int GameInstance::GetSetting(const std::string &name) { return GameConfig::GetConfig()->GetSetting(name); } diff --git a/src/game/game_instance.hpp b/src/game/game_instance.hpp index 4b5e1b0452..0a7a83abe1 100644 --- a/src/game/game_instance.hpp +++ b/src/game/game_instance.hpp @@ -23,7 +23,7 @@ public: */ void Initialize(class GameInfo *info); - int GetSetting(const char *name) override; + int GetSetting(const std::string &name) override; ScriptInfo *FindLibrary(const std::string &library, int version) override; private: diff --git a/src/script/api/script_controller.cpp b/src/script/api/script_controller.cpp index cd39319f0d..2ffbf46730 100644 --- a/src/script/api/script_controller.cpp +++ b/src/script/api/script_controller.cpp @@ -84,7 +84,7 @@ ScriptController::ScriptController(CompanyID company) : return ScriptObject::GetActiveInstance()->GetOpsTillSuspend(); } -/* static */ int ScriptController::GetSetting(const char *name) +/* static */ int ScriptController::GetSetting(const std::string &name) { return ScriptObject::GetActiveInstance()->GetSetting(name); } @@ -94,7 +94,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; @@ -152,7 +152,7 @@ ScriptController::ScriptController(CompanyID company) : 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); diff --git a/src/script/api/script_controller.hpp b/src/script/api/script_controller.hpp index f1e7cc9ea5..58419fafb3 100644 --- a/src/script/api/script_controller.hpp +++ b/src/script/api/script_controller.hpp @@ -125,7 +125,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 @@ -201,7 +201,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 LoadedLibraryList; ///< The type for loaded libraries. diff --git a/src/script/api/script_gamesettings.cpp b/src/script/api/script_gamesettings.cpp index c8d99c36e6..2749bb6773 100644 --- a/src/script/api/script_gamesettings.cpp +++ b/src/script/api/script_gamesettings.cpp @@ -16,13 +16,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; @@ -31,7 +31,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; diff --git a/src/script/api/script_gamesettings.hpp b/src/script/api/script_gamesettings.hpp index 83d023e70f..d4bbf609de 100644 --- a/src/script/api/script_gamesettings.hpp +++ b/src/script/api/script_gamesettings.hpp @@ -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. diff --git a/src/script/api/script_text.cpp b/src/script/api/script_text.cpp index 2445d2044f..b6f46e9f59 100644 --- a/src/script/api/script_text.cpp +++ b/src/script/api/script_text.cpp @@ -20,7 +20,7 @@ #include "../../safeguards.h" -RawText::RawText(const char *text) : text(text) +RawText::RawText(const std::string &text) : text(text) { } diff --git a/src/script/api/script_text.hpp b/src/script/api/script_text.hpp index 597f2bdd74..8c8171de85 100644 --- a/src/script/api/script_text.hpp +++ b/src/script/api/script_text.hpp @@ -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: diff --git a/src/script/script_instance.hpp b/src/script/script_instance.hpp index 98132a6f6c..3666db38bd 100644 --- a/src/script/script_instance.hpp +++ b/src/script/script_instance.hpp @@ -62,7 +62,7 @@ public: * @param name The name of the setting. * @return the value for the setting, or -1 if the setting is not known. */ - virtual int GetSetting(const char *name) = 0; + virtual int GetSetting(const std::string &name) = 0; /** * Find a library. diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp index c73402c26d..f323f9c8e2 100644 --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -84,24 +84,9 @@ namespace SQConvert { template <> struct Param { static inline TileIndex Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return TileIndex((uint32)(int32)tmp); } }; template <> struct Param { static inline Money Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param { static inline bool Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } }; + template <> struct Param { /* Do not use const char *, use std::string& instead. */ }; template <> struct Param { static inline void *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } }; - template <> struct Param { - static inline const char *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) - { - /* Convert what-ever there is as parameter to a string */ - sq_tostring(vm, index); - - const SQChar *tmp; - sq_getstring(vm, -1, &tmp); - char *tmp_str = stredup(tmp); - sq_poptop(vm); - ptr->push_back((void *)tmp_str); - StrMakeValidInPlace(tmp_str); - return tmp_str; - } - }; - template <> struct Param { static inline const std::string Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) {