diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 5f8ac9d164..03ba8666c5 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -70,7 +70,7 @@ void AIInstance::Died() if (info != nullptr) { ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING); - if (info->GetURL() != nullptr) { + if (!info->GetURL().empty()) { ScriptLog::Info("Please report the error to the following URL:"); ScriptLog::Info(info->GetURL()); } diff --git a/src/crashlog.cpp b/src/crashlog.cpp index 6ee4c83505..c4ab30a8a0 100644 --- a/src/crashlog.cpp +++ b/src/crashlog.cpp @@ -200,12 +200,12 @@ char *CrashLog::LogConfiguration(char *buffer, const char *last) const if (c->ai_info == nullptr) { buffer += seprintf(buffer, last, " %2i: Human\n", (int)c->index); } else { - buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName(), c->ai_info->GetVersion()); + buffer += seprintf(buffer, last, " %2i: %s (v%d)\n", (int)c->index, c->ai_info->GetName().c_str(), c->ai_info->GetVersion()); } } if (Game::GetInfo() != nullptr) { - buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName(), Game::GetInfo()->GetVersion()); + buffer += seprintf(buffer, last, " GS: %s (v%d)\n", Game::GetInfo()->GetName().c_str(), Game::GetInfo()->GetVersion()); } buffer += seprintf(buffer, last, "\n"); diff --git a/src/framerate_gui.cpp b/src/framerate_gui.cpp index eaa8fbf772..db9dea477f 100644 --- a/src/framerate_gui.cpp +++ b/src/framerate_gui.cpp @@ -368,7 +368,7 @@ static const PerformanceElement DISPLAY_ORDER_PFE[PFE_MAX] = { static const char * GetAIName(int ai_index) { if (!Company::IsValidAiID(ai_index)) return ""; - return Company::Get(ai_index)->ai_info->GetName(); + return Company::Get(ai_index)->ai_info->GetName().c_str(); } /** @hideinitializer */ diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp index 36c3b984c8..9d23588702 100644 --- a/src/game/game_instance.cpp +++ b/src/game/game_instance.cpp @@ -73,7 +73,7 @@ void GameInstance::Died() if (info != nullptr) { ShowErrorMessage(STR_ERROR_AI_PLEASE_REPORT_CRASH, INVALID_STRING_ID, WL_WARNING); - if (info->GetURL() != nullptr) { + if (!info->GetURL().empty()) { ScriptLog::Info("Please report the error to the following URL:"); ScriptLog::Info(info->GetURL()); } diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index dab4a28b18..09c7a0098b 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -153,7 +153,7 @@ struct ScriptListWindow : public Window { SetDParam(0, selected_info->GetVersion()); DrawString(tr, STR_AI_LIST_VERSION); tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; - if (selected_info->GetURL() != nullptr) { + if (!selected_info->GetURL().empty()) { SetDParamStr(0, selected_info->GetURL()); DrawString(tr, STR_AI_LIST_URL); tr.top += FONT_HEIGHT_NORMAL + WidgetDimensions::scaled.vsep_normal; diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index 8c2554a7ab..5e48acbcac 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -20,13 +20,6 @@ ScriptInfo::~ScriptInfo() { - free(this->author); - free(this->name); - free(this->short_name); - free(this->description); - free(this->date); - free(this->instance_name); - free(this->url); free(this->SQ_instance); } @@ -69,17 +62,17 @@ bool ScriptInfo::CheckMethod(const char *name) const info->tar_file = info->scanner->GetTarFile(); /* Cache the data the info file gives us. */ - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR; - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR; - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR; - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR; - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR; if (!info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR; - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR; /* The GetURL function is optional. */ if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) { - if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR; + if (!info->engine->CallStringMethod(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR; } /* Check if we have settings */ diff --git a/src/script/script_info.hpp b/src/script/script_info.hpp index 35b35622bb..afddbf5650 100644 --- a/src/script/script_info.hpp +++ b/src/script/script_info.hpp @@ -32,14 +32,7 @@ public: ScriptInfo() : engine(nullptr), SQ_instance(nullptr), - author(nullptr), - name(nullptr), - short_name(nullptr), - description(nullptr), - date(nullptr), - instance_name(nullptr), version(0), - url(nullptr), scanner(nullptr) {} ~ScriptInfo(); @@ -47,22 +40,22 @@ public: /** * Get the Author of the script. */ - const char *GetAuthor() const { return this->author; } + const std::string &GetAuthor() const { return this->author; } /** * Get the Name of the script. */ - const char *GetName() const { return this->name; } + const std::string &GetName() const { return this->name; } /** * Get the 4 character long short name of the script. */ - const char *GetShortName() const { return this->short_name; } + const std::string &GetShortName() const { return this->short_name; } /** * Get the description of the script. */ - const char *GetDescription() const { return this->description; } + const std::string &GetDescription() const { return this->description; } /** * Get the version of the script. @@ -72,27 +65,27 @@ public: /** * Get the last-modified date of the script. */ - const char *GetDate() const { return this->date; } + const std::string &GetDate() const { return this->date; } /** * Get the name of the instance of the script to create. */ - const char *GetInstanceName() const { return this->instance_name; } + const std::string &GetInstanceName() const { return this->instance_name; } /** * Get the website for this script. */ - const char *GetURL() const { return this->url; } + const std::string &GetURL() const { return this->url; } /** * Get the filename of the main.nut script. */ - const char *GetMainScript() const { return this->main_script.c_str(); } + const std::string &GetMainScript() const { return this->main_script; } /** * Get the filename of the tar the script is in. */ - std::string GetTarFile() const { return this->tar_file; } + const std::string &GetTarFile() const { return this->tar_file; } /** * Check if a given method exists. @@ -152,14 +145,14 @@ protected: private: std::string main_script; ///< The full path of the script. std::string tar_file; ///< If, which tar file the script was in. - const char *author; ///< Author of the script. - const char *name; ///< Full name of the script. - const char *short_name; ///< Short name (4 chars) which uniquely identifies the script. - const char *description; ///< Small description of the script. - const char *date; ///< The date the script was written at. - const char *instance_name; ///< Name of the main class in the script. + std::string author; ///< Author of the script. + std::string name; ///< Full name of the script. + std::string short_name; ///< Short name (4 chars) which uniquely identifies the script. + std::string description; ///< Small description of the script. + std::string date; ///< The date the script was written at. + std::string instance_name; ///< Name of the main class in the script. int version; ///< Version of the script. - const char *url; ///< URL of the script. + std::string url; ///< URL of the script. class ScriptScanner *scanner; ///< ScriptScanner object that was used to scan this script info. }; diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index b970aa7094..71365850f4 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -67,7 +67,7 @@ ScriptInstance::ScriptInstance(const char *APIName) : this->engine->SetPrintFunction(&PrintFunc); } -void ScriptInstance::Initialize(const char *main_script, const char *instance_name, CompanyID company) +void ScriptInstance::Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company) { ScriptObject::ActiveInstance active(this); @@ -80,7 +80,7 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na try { ScriptObject::SetAllowDoCommand(false); /* Load and execute the script for this script */ - if (strcmp(main_script, "%_dummy") == 0) { + if (main_script == "%_dummy") { this->LoadDummyScript(); } else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) { if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to load script. AI is not started."); diff --git a/src/script/script_instance.hpp b/src/script/script_instance.hpp index c9acf4c80a..98132a6f6c 100644 --- a/src/script/script_instance.hpp +++ b/src/script/script_instance.hpp @@ -55,7 +55,7 @@ public: * @param instance_name The name of the instance out of the script to load. * @param company Which company this script is serving. */ - void Initialize(const char *main_script, const char *instance_name, CompanyID company); + void Initialize(const std::string &main_script, const std::string &instance_name, CompanyID company); /** * Get the value of a setting of the current instance. diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index cb43fb0d0c..6192b4f00e 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -97,8 +97,8 @@ void ScriptScanner::RegisterScript(ScriptInfo *info) std::string script_name = fmt::format("{}.{}", script_original_name, info->GetVersion()); /* Check if GetShortName follows the rules */ - if (strlen(info->GetShortName()) != 4) { - Debug(script, 0, "The script '{}' returned a string from GetShortName() which is not four characaters. Unable to load the script.", info->GetName()); + if (info->GetShortName().size() != 4) { + Debug(script, 0, "The script '{}' returned a string from GetShortName() which is not four characters. Unable to load the script.", info->GetName()); delete info; return; } @@ -107,9 +107,9 @@ void ScriptScanner::RegisterScript(ScriptInfo *info) /* This script was already registered */ #ifdef _WIN32 /* Windows doesn't care about the case */ - if (StrEqualsIgnoreCase(this->info_list[script_name]->GetMainScript(), info->GetMainScript()) == 0) { + if (StrEqualsIgnoreCase(this->info_list[script_name]->GetMainScript(), info->GetMainScript())) { #else - if (strcmp(this->info_list[script_name]->GetMainScript(), info->GetMainScript()) == 0) { + if (this->info_list[script_name]->GetMainScript() == info->GetMainScript()) { #endif delete info; return; @@ -206,7 +206,7 @@ struct ScriptFileChecksumCreator : FileScanner { static bool IsSameScript(const ContentInfo *ci, bool md5sum, ScriptInfo *info, Subdirectory dir) { uint32 id = 0; - const char *str = info->GetShortName(); + const char *str = info->GetShortName().c_str(); for (int j = 0; j < 4 && *str != '\0'; j++, str++) id |= *str << (8 * j); if (id != ci->unique_id) return false; @@ -251,7 +251,7 @@ bool ScriptScanner::HasScript(const ContentInfo *ci, bool md5sum) const char *ScriptScanner::FindMainScript(const ContentInfo *ci, bool md5sum) { for (const auto &item : this->info_list) { - if (IsSameScript(ci, md5sum, item.second, this->GetDirectory())) return item.second->GetMainScript(); + if (IsSameScript(ci, md5sum, item.second, this->GetDirectory())) return item.second->GetMainScript().c_str(); } return nullptr; } diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index 3c8a802cc8..0d0d2724ba 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -469,7 +469,7 @@ bool Squirrel::CallBoolMethod(HSQOBJECT instance, const char *method_name, bool return true; } -/* static */ bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name) +/* static */ bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm, const std::string &class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name) { Squirrel *engine = (Squirrel *)sq_getforeignptr(vm); @@ -479,12 +479,9 @@ bool Squirrel::CallBoolMethod(HSQOBJECT instance, const char *method_name, bool sq_pushroottable(vm); if (prepend_API_name) { - size_t len = strlen(class_name) + strlen(engine->GetAPIName()) + 1; - char *class_name2 = MallocT(len); - seprintf(class_name2, class_name2 + len - 1, "%s%s", engine->GetAPIName(), class_name); - - sq_pushstring(vm, class_name2, -1); - free(class_name2); + std::string prepended_class_name = engine->GetAPIName(); + prepended_class_name += class_name; + sq_pushstring(vm, prepended_class_name, -1); } else { sq_pushstring(vm, class_name, -1); } @@ -520,7 +517,7 @@ bool Squirrel::CallBoolMethod(HSQOBJECT instance, const char *method_name, bool return true; } -bool Squirrel::CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance) +bool Squirrel::CreateClassInstance(const std::string &class_name, void *real_instance, HSQOBJECT *instance) { ScriptAllocatorScope alloc_scope(this); return Squirrel::CreateClassInstanceVM(this->vm, class_name, real_instance, instance, nullptr); diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp index 34325c7a3d..22c3435617 100644 --- a/src/script/squirrel.hpp +++ b/src/script/squirrel.hpp @@ -179,12 +179,12 @@ public: * @param prepend_API_name Optional parameter; if true, the class_name is prefixed with the current API name. * @return False if creating failed. */ - static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name = false); + static bool CreateClassInstanceVM(HSQUIRRELVM vm, const std::string &class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name = false); /** * Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel. */ - bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance); + bool CreateClassInstance(const std::string &class_name, void *real_instance, HSQOBJECT *instance); /** * Get the real-instance pointer.