mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: remove manual allocation/free for SQ_instance
This commit is contained in:
parent
f9b5bc7ee6
commit
62fda0f505
|
@ -69,20 +69,20 @@ template <> const char *GetClassName<AIInfo, ScriptType::AI>() { return "AIInfo"
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
SQInteger res = ScriptInfo::Constructor(vm, info);
|
||||||
if (res != 0) return res;
|
if (res != 0) return res;
|
||||||
|
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
|
if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
|
||||||
if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
} else {
|
} else {
|
||||||
info->min_loadable_version = info->GetVersion();
|
info->min_loadable_version = info->GetVersion();
|
||||||
}
|
}
|
||||||
/* When there is an UseAsRandomAI function, call it. */
|
/* When there is an UseAsRandomAI function, call it. */
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "UseAsRandomAI")) {
|
if (info->engine->MethodExists(info->SQ_instance, "UseAsRandomAI")) {
|
||||||
if (!info->engine->CallBoolMethod(*info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info->engine->CallBoolMethod(info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
} else {
|
} else {
|
||||||
info->use_as_random = true;
|
info->use_as_random = true;
|
||||||
}
|
}
|
||||||
/* 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->CallStringMethod(*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;
|
||||||
|
@ -149,7 +149,7 @@ bool AIInfo::CanLoadFromVersion(int version) const
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cache the category */
|
/* Cache the category */
|
||||||
if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
|
if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
|
||||||
delete library;
|
delete library;
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,20 +60,20 @@ template <> const char *GetClassName<GameInfo, ScriptType::GS>() { return "GSInf
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
SQInteger res = ScriptInfo::Constructor(vm, info);
|
||||||
if (res != 0) return res;
|
if (res != 0) return res;
|
||||||
|
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
|
if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
|
||||||
if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
} else {
|
} else {
|
||||||
info->min_loadable_version = info->GetVersion();
|
info->min_loadable_version = info->GetVersion();
|
||||||
}
|
}
|
||||||
/* When there is an IsSelectable function, call it. */
|
/* When there is an IsSelectable function, call it. */
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "IsDeveloperOnly")) {
|
if (info->engine->MethodExists(info->SQ_instance, "IsDeveloperOnly")) {
|
||||||
if (!info->engine->CallBoolMethod(*info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info->engine->CallBoolMethod(info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
} else {
|
} else {
|
||||||
info->is_developer_only = false;
|
info->is_developer_only = false;
|
||||||
}
|
}
|
||||||
/* 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->CallStringMethod(*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;
|
||||||
|
@ -119,7 +119,7 @@ bool GameInfo::CanLoadFromVersion(int version) const
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Cache the category */
|
/* Cache the category */
|
||||||
if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
|
if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
|
||||||
delete library;
|
delete library;
|
||||||
return SQ_ERROR;
|
return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,14 +18,9 @@
|
||||||
|
|
||||||
#include "../safeguards.h"
|
#include "../safeguards.h"
|
||||||
|
|
||||||
ScriptInfo::~ScriptInfo()
|
|
||||||
{
|
|
||||||
free(this->SQ_instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ScriptInfo::CheckMethod(const char *name) const
|
bool ScriptInfo::CheckMethod(const char *name) const
|
||||||
{
|
{
|
||||||
if (!this->engine->MethodExists(*this->SQ_instance, name)) {
|
if (!this->engine->MethodExists(this->SQ_instance, name)) {
|
||||||
this->engine->ThrowError(fmt::format("your info.nut/library.nut doesn't have the method '{}'", name));
|
this->engine->ThrowError(fmt::format("your info.nut/library.nut doesn't have the method '{}'", name));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -35,10 +30,9 @@ bool ScriptInfo::CheckMethod(const char *name) const
|
||||||
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
|
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
|
||||||
{
|
{
|
||||||
/* Set some basic info from the parent */
|
/* Set some basic info from the parent */
|
||||||
info->SQ_instance = MallocT<SQObject>(1);
|
Squirrel::GetInstance(vm, &info->SQ_instance, 2);
|
||||||
Squirrel::GetInstance(vm, info->SQ_instance, 2);
|
|
||||||
/* Make sure the instance stays alive over time */
|
/* Make sure the instance stays alive over time */
|
||||||
sq_addref(vm, info->SQ_instance);
|
sq_addref(vm, &info->SQ_instance);
|
||||||
|
|
||||||
info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
|
info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
|
||||||
info->engine = info->scanner->GetEngine();
|
info->engine = info->scanner->GetEngine();
|
||||||
|
@ -62,21 +56,21 @@ bool ScriptInfo::CheckMethod(const char *name) const
|
||||||
info->tar_file = info->scanner->GetTarFile();
|
info->tar_file = info->scanner->GetTarFile();
|
||||||
|
|
||||||
/* Cache the data the info file gives us. */
|
/* Cache the data the info file gives us. */
|
||||||
if (!info->engine->CallStringMethod(*info->SQ_instance, "GetAuthor", &info->author, 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, "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, "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, "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->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->CallIntegerMethod(info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (!info->engine->CallStringMethod(*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. */
|
/* The GetURL function is optional. */
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) {
|
if (info->engine->MethodExists(info->SQ_instance, "GetURL")) {
|
||||||
if (!info->engine->CallStringMethod(*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 */
|
/* Check if we have settings */
|
||||||
if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) {
|
if (info->engine->MethodExists(info->SQ_instance, "GetSettings")) {
|
||||||
if (!info->GetSettings()) return SQ_ERROR;
|
if (!info->GetSettings()) return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +79,7 @@ bool ScriptInfo::CheckMethod(const char *name) const
|
||||||
|
|
||||||
bool ScriptInfo::GetSettings()
|
bool ScriptInfo::GetSettings()
|
||||||
{
|
{
|
||||||
return this->engine->CallMethod(*this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
|
return this->engine->CallMethod(this->SQ_instance, "GetSettings", nullptr, MAX_GET_SETTING_OPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm)
|
||||||
|
|
|
@ -31,11 +31,9 @@ class ScriptInfo : public SimpleCountedObject {
|
||||||
public:
|
public:
|
||||||
ScriptInfo() :
|
ScriptInfo() :
|
||||||
engine(nullptr),
|
engine(nullptr),
|
||||||
SQ_instance(nullptr),
|
|
||||||
version(0),
|
version(0),
|
||||||
scanner(nullptr)
|
scanner(nullptr)
|
||||||
{}
|
{}
|
||||||
~ScriptInfo();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Author of the script.
|
* Get the Author of the script.
|
||||||
|
@ -139,7 +137,7 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
class Squirrel *engine; ///< Engine used to register for Squirrel.
|
class Squirrel *engine; ///< Engine used to register for Squirrel.
|
||||||
HSQOBJECT *SQ_instance; ///< The Squirrel instance created for this info.
|
HSQOBJECT SQ_instance; ///< The Squirrel instance created for this info.
|
||||||
ScriptConfigItemList config_list; ///< List of settings from this Script.
|
ScriptConfigItemList config_list; ///< List of settings from this Script.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in New Issue