1
0
Fork 0

Codechange: use std::string for script API versions

pull/10801/head
Rubidium 2023-05-04 23:06:21 +02:00 committed by rubidium42
parent 3d8d99ba11
commit 9b0123ab66
7 changed files with 17 additions and 37 deletions

View File

@ -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<std::string> 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<AIInfo, ScriptType::AI>() { 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<AIInfo, ScriptType::AI>() { 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<AIInfo, ScriptType::AI>() { 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;

View File

@ -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. */

View File

@ -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<std::string> 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<GameInfo, ScriptType::GS>() { 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<GameInfo, ScriptType::GS>() { 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;

View File

@ -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. */

View File

@ -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;

View File

@ -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.

View File

@ -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);