diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp index 7e35d89bb7..29dfdf81f4 100644 --- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -23,7 +23,7 @@ * Check if the API version provided by the AI is supported. * @param api_version The API version as provided by the AI. */ -static bool CheckAPIVersion(const char *api_version) +static bool CheckAPIVersion(const std::string &api_version) { static const std::set versions = { "0.7", "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "1.10", "1.11", "12", "13", "14" }; return versions.find(api_version) != versions.end(); @@ -82,13 +82,13 @@ template <> const char *GetClassName() { return "AIInfo" } /* Try to get the API version the AI is written for. */ if (info->engine->MethodExists(*info->SQ_instance, "GetAPIVersion")) { - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR; if (!CheckAPIVersion(info->api_version)) { Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion()); return SQ_ERROR; } } else { - info->api_version = stredup("0.7"); + info->api_version = "0.7"; } /* Remove the link to the real instance, else it might get deleted by RegisterAI() */ @@ -104,15 +104,11 @@ template <> const char *GetClassName() { return "AIInfo" SQUserPointer instance; sq_getinstanceup(vm, 2, &instance, nullptr); AIInfo *info = (AIInfo *)instance; - info->api_version = nullptr; + info->api_version = fmt::format("{}.{}", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4)); SQInteger res = ScriptInfo::Constructor(vm, info); if (res != 0) return res; - char buf[8]; - seprintf(buf, lastof(buf), "%d.%d", GB(_openttd_newgrf_version, 28, 4), GB(_openttd_newgrf_version, 24, 4)); - info->api_version = stredup(buf); - /* Remove the link to the real instance, else it might get deleted by RegisterAI() */ sq_setinstanceup(vm, 2, nullptr); /* Register the AI to the base system */ @@ -122,16 +118,10 @@ template <> const char *GetClassName() { return "AIInfo" AIInfo::AIInfo() : min_loadable_version(0), - use_as_random(false), - api_version(nullptr) + use_as_random(false) { } -AIInfo::~AIInfo() -{ - free(this->api_version); -} - bool AIInfo::CanLoadFromVersion(int version) const { if (version == -1) return true; diff --git a/src/ai/ai_info.hpp b/src/ai/ai_info.hpp index 8f236e09ab..a87b6cc36c 100644 --- a/src/ai/ai_info.hpp +++ b/src/ai/ai_info.hpp @@ -16,7 +16,6 @@ class AIInfo : public ScriptInfo { public: AIInfo(); - ~AIInfo(); /** * Register the functions of this class. @@ -46,12 +45,12 @@ public: /** * Get the API version this AI is written for. */ - const char *GetAPIVersion() const { return this->api_version; } + const std::string &GetAPIVersion() const { return this->api_version; } private: int min_loadable_version; ///< The AI can load savegame data if the version is equal or greater than this. bool use_as_random; ///< Should this AI be used when the user wants a "random AI"? - const char *api_version; ///< API version used by this AI. + std::string api_version; ///< API version used by this AI. }; /** All static information from an AI library like name, version, etc. */ diff --git a/src/game/game_info.cpp b/src/game/game_info.cpp index b151586c10..2144ebd377 100644 --- a/src/game/game_info.cpp +++ b/src/game/game_info.cpp @@ -21,7 +21,7 @@ * Check if the API version provided by the Game is supported. * @param api_version The API version as provided by the Game. */ -static bool CheckAPIVersion(const char *api_version) +static bool CheckAPIVersion(const std::string &api_version) { static const std::set versions = { "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "1.10", "1.11", "12", "13", "14" }; return versions.find(api_version) != versions.end(); @@ -73,7 +73,7 @@ template <> const char *GetClassName() { return "GSInf } /* Try to get the API version the AI is written for. */ if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR; - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR; if (!CheckAPIVersion(info->api_version)) { Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion()); return SQ_ERROR; @@ -88,16 +88,10 @@ template <> const char *GetClassName() { return "GSInf GameInfo::GameInfo() : min_loadable_version(0), - is_developer_only(false), - api_version(nullptr) + is_developer_only(false) { } -GameInfo::~GameInfo() -{ - free(this->api_version); -} - bool GameInfo::CanLoadFromVersion(int version) const { if (version == -1) return true; diff --git a/src/game/game_info.hpp b/src/game/game_info.hpp index cfa900767c..76dbfd546f 100644 --- a/src/game/game_info.hpp +++ b/src/game/game_info.hpp @@ -16,7 +16,6 @@ class GameInfo : public ScriptInfo { public: GameInfo(); - ~GameInfo(); /** * Register the functions of this class. @@ -36,14 +35,14 @@ public: /** * Get the API version this Game is written for. */ - const char *GetAPIVersion() const { return this->api_version; } + const std::string &GetAPIVersion() const { return this->api_version; } bool IsDeveloperOnly() const override { return this->is_developer_only; } private: int min_loadable_version; ///< The Game can load savegame data if the version is equal or greater than this. bool is_developer_only; ///< Is the script selectable by non-developers? - const char *api_version; ///< API version used by this Game. + std::string api_version; ///< API version used by this Game. }; /** All static information from an Game library like name, version, etc. */ diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index 1383a4540b..c0a4f200d9 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -51,7 +51,6 @@ static void PrintFunc(bool error_msg, const std::string &message) ScriptInstance::ScriptInstance(const char *APIName) : engine(nullptr), - versionAPI(nullptr), controller(nullptr), storage(nullptr), instance(nullptr), @@ -113,10 +112,9 @@ void ScriptInstance::RegisterAPI() squirrel_register_std(this->engine); } -bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirectory dir) +bool ScriptInstance::LoadCompatibilityScripts(const std::string &api_version, Subdirectory dir) { - char script_name[32]; - seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version); + std::string script_name = fmt::format("compat_{}.nut", api_version); for (Searchpath sp : _valid_searchpaths) { std::string buf = FioGetDirectory(sp, dir); buf += script_name; diff --git a/src/script/script_instance.hpp b/src/script/script_instance.hpp index dae1f6940d..4e98b8df7e 100644 --- a/src/script/script_instance.hpp +++ b/src/script/script_instance.hpp @@ -248,7 +248,7 @@ public: protected: class Squirrel *engine; ///< A wrapper around the squirrel vm. - const char *versionAPI; ///< Current API used by this script. + std::string versionAPI; ///< Current API used by this script. /** * Register all API functions to the VM. @@ -261,7 +261,7 @@ protected: * @param dir Subdirectory to find the scripts in * @return true iff script loading should proceed */ - bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir); + bool LoadCompatibilityScripts(const std::string &api_version, Subdirectory dir); /** * Tell the script it died. diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp index 77cff710b6..d290c3ec1a 100644 --- a/src/script/squirrel.hpp +++ b/src/script/squirrel.hpp @@ -160,7 +160,7 @@ public: bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend); bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, nullptr, suspend); } bool CallStringMethodStrdup(HSQOBJECT instance, const char *method_name, const char **res, int suspend); - bool CallStringMethod(HSQOBJECT instance, const char *method_name, const std::string *res, int suspend); + bool CallStringMethod(HSQOBJECT instance, const char *method_name, std::string *res, int suspend); bool CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend); bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);