mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string for script API versions
parent
3d8d99ba11
commit
9b0123ab66
|
@ -23,7 +23,7 @@
|
||||||
* Check if the API version provided by the AI is supported.
|
* Check if the API version provided by the AI is supported.
|
||||||
* @param api_version The API version as provided by the AI.
|
* @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" };
|
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();
|
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. */
|
/* Try to get the API version the AI is written for. */
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "GetAPIVersion")) {
|
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)) {
|
if (!CheckAPIVersion(info->api_version)) {
|
||||||
Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
|
Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
}
|
}
|
||||||
} else {
|
} 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() */
|
/* 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;
|
SQUserPointer instance;
|
||||||
sq_getinstanceup(vm, 2, &instance, nullptr);
|
sq_getinstanceup(vm, 2, &instance, nullptr);
|
||||||
AIInfo *info = (AIInfo *)instance;
|
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);
|
SQInteger res = ScriptInfo::Constructor(vm, info);
|
||||||
if (res != 0) return res;
|
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() */
|
/* Remove the link to the real instance, else it might get deleted by RegisterAI() */
|
||||||
sq_setinstanceup(vm, 2, nullptr);
|
sq_setinstanceup(vm, 2, nullptr);
|
||||||
/* Register the AI to the base system */
|
/* Register the AI to the base system */
|
||||||
|
@ -122,16 +118,10 @@ template <> const char *GetClassName<AIInfo, ScriptType::AI>() { return "AIInfo"
|
||||||
|
|
||||||
AIInfo::AIInfo() :
|
AIInfo::AIInfo() :
|
||||||
min_loadable_version(0),
|
min_loadable_version(0),
|
||||||
use_as_random(false),
|
use_as_random(false)
|
||||||
api_version(nullptr)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
AIInfo::~AIInfo()
|
|
||||||
{
|
|
||||||
free(this->api_version);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool AIInfo::CanLoadFromVersion(int version) const
|
bool AIInfo::CanLoadFromVersion(int version) const
|
||||||
{
|
{
|
||||||
if (version == -1) return true;
|
if (version == -1) return true;
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
class AIInfo : public ScriptInfo {
|
class AIInfo : public ScriptInfo {
|
||||||
public:
|
public:
|
||||||
AIInfo();
|
AIInfo();
|
||||||
~AIInfo();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the functions of this class.
|
* Register the functions of this class.
|
||||||
|
@ -46,12 +45,12 @@ public:
|
||||||
/**
|
/**
|
||||||
* Get the API version this AI is written for.
|
* 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:
|
private:
|
||||||
int min_loadable_version; ///< The AI can load savegame data if the version is equal or greater than this.
|
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"?
|
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. */
|
/** All static information from an AI library like name, version, etc. */
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
* Check if the API version provided by the Game is supported.
|
* Check if the API version provided by the Game is supported.
|
||||||
* @param api_version The API version as provided by the Game.
|
* @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" };
|
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();
|
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. */
|
/* Try to get the API version the AI is written for. */
|
||||||
if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
|
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)) {
|
if (!CheckAPIVersion(info->api_version)) {
|
||||||
Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
|
Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
|
@ -88,16 +88,10 @@ template <> const char *GetClassName<GameInfo, ScriptType::GS>() { return "GSInf
|
||||||
|
|
||||||
GameInfo::GameInfo() :
|
GameInfo::GameInfo() :
|
||||||
min_loadable_version(0),
|
min_loadable_version(0),
|
||||||
is_developer_only(false),
|
is_developer_only(false)
|
||||||
api_version(nullptr)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
GameInfo::~GameInfo()
|
|
||||||
{
|
|
||||||
free(this->api_version);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GameInfo::CanLoadFromVersion(int version) const
|
bool GameInfo::CanLoadFromVersion(int version) const
|
||||||
{
|
{
|
||||||
if (version == -1) return true;
|
if (version == -1) return true;
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
class GameInfo : public ScriptInfo {
|
class GameInfo : public ScriptInfo {
|
||||||
public:
|
public:
|
||||||
GameInfo();
|
GameInfo();
|
||||||
~GameInfo();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the functions of this class.
|
* Register the functions of this class.
|
||||||
|
@ -36,14 +35,14 @@ public:
|
||||||
/**
|
/**
|
||||||
* Get the API version this Game is written for.
|
* 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; }
|
bool IsDeveloperOnly() const override { return this->is_developer_only; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int min_loadable_version; ///< The Game can load savegame data if the version is equal or greater than this.
|
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?
|
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. */
|
/** All static information from an Game library like name, version, etc. */
|
||||||
|
|
|
@ -51,7 +51,6 @@ static void PrintFunc(bool error_msg, const std::string &message)
|
||||||
|
|
||||||
ScriptInstance::ScriptInstance(const char *APIName) :
|
ScriptInstance::ScriptInstance(const char *APIName) :
|
||||||
engine(nullptr),
|
engine(nullptr),
|
||||||
versionAPI(nullptr),
|
|
||||||
controller(nullptr),
|
controller(nullptr),
|
||||||
storage(nullptr),
|
storage(nullptr),
|
||||||
instance(nullptr),
|
instance(nullptr),
|
||||||
|
@ -113,10 +112,9 @@ void ScriptInstance::RegisterAPI()
|
||||||
squirrel_register_std(this->engine);
|
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];
|
std::string script_name = fmt::format("compat_{}.nut", api_version);
|
||||||
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
|
|
||||||
for (Searchpath sp : _valid_searchpaths) {
|
for (Searchpath sp : _valid_searchpaths) {
|
||||||
std::string buf = FioGetDirectory(sp, dir);
|
std::string buf = FioGetDirectory(sp, dir);
|
||||||
buf += script_name;
|
buf += script_name;
|
||||||
|
|
|
@ -248,7 +248,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
class Squirrel *engine; ///< A wrapper around the squirrel vm.
|
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.
|
* Register all API functions to the VM.
|
||||||
|
@ -261,7 +261,7 @@ protected:
|
||||||
* @param dir Subdirectory to find the scripts in
|
* @param dir Subdirectory to find the scripts in
|
||||||
* @return true iff script loading should proceed
|
* @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.
|
* Tell the script it died.
|
||||||
|
|
|
@ -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, HSQOBJECT *ret, int suspend);
|
||||||
bool CallMethod(HSQOBJECT instance, const char *method_name, int suspend) { return this->CallMethod(instance, method_name, nullptr, 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 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 CallIntegerMethod(HSQOBJECT instance, const char *method_name, int *res, int suspend);
|
||||||
bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
|
bool CallBoolMethod(HSQOBJECT instance, const char *method_name, bool *res, int suspend);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue