1
0
Fork 0

Codechange: replace 'const char *' script API parameters with std::string

pull/10801/head
Rubidium 2023-05-06 10:07:54 +02:00 committed by rubidium42
parent b1b578f988
commit d9e93edc8b
13 changed files with 20 additions and 35 deletions

View File

@ -42,7 +42,7 @@ macro(dump_class_templates NAME)
string(APPEND SQUIRREL_EXPORT "\n return Param<ScriptText *>::Get(vm, index, ptr);") string(APPEND SQUIRREL_EXPORT "\n return Param<ScriptText *>::Get(vm, index, ptr);")
string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n }")
string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_STRING) {") string(APPEND SQUIRREL_EXPORT "\n if (sq_gettype(vm, index) == OT_STRING) {")
string(APPEND SQUIRREL_EXPORT "\n return new RawText(Param<const char *>::Get(vm, index, ptr));") string(APPEND SQUIRREL_EXPORT "\n return new RawText(Param<const std::string &>::Get(vm, index, ptr));")
string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n }")
string(APPEND SQUIRREL_EXPORT "\n return nullptr;") string(APPEND SQUIRREL_EXPORT "\n return nullptr;")
string(APPEND SQUIRREL_EXPORT "\n }") string(APPEND SQUIRREL_EXPORT "\n }")

View File

@ -83,7 +83,7 @@ void AIInstance::LoadDummyScript()
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI"); 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); return AIConfig::GetConfig(_current_company)->GetSetting(name);
} }

View File

@ -23,7 +23,7 @@ public:
*/ */
void Initialize(class AIInfo *info); 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; ScriptInfo *FindLibrary(const std::string &library, int version) override;
private: private:

View File

@ -53,7 +53,7 @@ void GameInstance::RegisterAPI()
if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died(); 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); return GameConfig::GetConfig()->GetSetting(name);
} }

View File

@ -23,7 +23,7 @@ public:
*/ */
void Initialize(class GameInfo *info); 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; ScriptInfo *FindLibrary(const std::string &library, int version) override;
private: private:

View File

@ -84,7 +84,7 @@ ScriptController::ScriptController(CompanyID company) :
return ScriptObject::GetActiveInstance()->GetOpsTillSuspend(); return ScriptObject::GetActiveInstance()->GetOpsTillSuspend();
} }
/* static */ int ScriptController::GetSetting(const char *name) /* static */ int ScriptController::GetSetting(const std::string &name)
{ {
return ScriptObject::GetActiveInstance()->GetSetting(name); return ScriptObject::GetActiveInstance()->GetSetting(name);
} }
@ -94,7 +94,7 @@ ScriptController::ScriptController(CompanyID company) :
return _openttd_newgrf_version; 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(); ScriptController *controller = ScriptObject::GetActiveInstance()->GetController();
Squirrel *engine = ScriptObject::GetActiveInstance()->engine; Squirrel *engine = ScriptObject::GetActiveInstance()->engine;
@ -152,7 +152,7 @@ ScriptController::ScriptController(CompanyID company) :
sq_getstackobj(vm, -1, &obj); sq_getstackobj(vm, -1, &obj);
sq_pop(vm, 3); 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 */ /* Now link the name the user wanted to our 'fake' class */
sq_pushobject(vm, parent); sq_pushobject(vm, parent);

View File

@ -125,7 +125,7 @@ public:
* @param name The name of the setting. * @param name The name of the setting.
* @return the value for the setting, or -1 if the setting is not known. * @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 * 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. * @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. * @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: private:
typedef std::map<std::string, std::string, CaseInsensitiveComparator> LoadedLibraryList; ///< The type for loaded libraries. typedef std::map<std::string, std::string, CaseInsensitiveComparator> LoadedLibraryList; ///< The type for loaded libraries.

View File

@ -16,13 +16,13 @@
#include "../../safeguards.h" #include "../../safeguards.h"
/* static */ bool ScriptGameSettings::IsValid(const char *setting) /* static */ bool ScriptGameSettings::IsValid(const std::string &setting)
{ {
const SettingDesc *sd = GetSettingFromName(setting); const SettingDesc *sd = GetSettingFromName(setting);
return sd != nullptr && sd->IsIntSetting(); 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; if (!IsValid(setting)) return -1;
@ -31,7 +31,7 @@
return sd->AsIntSetting()->Read(&_settings_game); 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); EnforceDeityOrCompanyModeValid(false);
if (!IsValid(setting)) return 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. * @note Results achieved in the past offer no guarantee for the future.
* @return True if and only if the setting is valid. * @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. * Gets the value of the game setting.
@ -57,7 +57,7 @@ public:
* @note Results achieved in the past offer no guarantee for the future. * @note Results achieved in the past offer no guarantee for the future.
* @return The value for the setting. * @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. * Sets the value of the game setting.
@ -69,7 +69,7 @@ public:
* @note Results achieved in the past offer no guarantee for the future. * @note Results achieved in the past offer no guarantee for the future.
* @api -ai * @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. * Checks whether the given vehicle-type is disabled for companies.

View File

@ -20,7 +20,7 @@
#include "../../safeguards.h" #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 { class RawText : public Text {
public: public:
RawText(const char *text); RawText(const std::string &text);
const std::string GetEncodedText() override { return this->text; } const std::string GetEncodedText() override { return this->text; }
private: private:

View File

@ -62,7 +62,7 @@ public:
* @param name The name of the setting. * @param name The name of the setting.
* @return the value for the setting, or -1 if the setting is not known. * @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. * Find a library.

View File

@ -84,24 +84,9 @@ namespace SQConvert {
template <> struct Param<TileIndex> { 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<TileIndex> { 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<Money> { static inline Money Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } }; template <> struct Param<Money> { static inline Money Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } };
template <> struct Param<bool> { static inline bool Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } }; template <> struct Param<bool> { static inline bool Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } };
template <> struct Param<const char *> { /* Do not use const char *, use std::string& instead. */ };
template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } }; template <> struct Param<void *> { static inline void *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } };
template <> struct Param<const char *> {
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<const std::string &> { template <> struct Param<const std::string &> {
static inline const std::string Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) static inline const std::string Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)
{ {