Changeset - r27369:d93808e810cd
[Not reviewed]
master
0 4 0
Rubidium - 13 months ago 2023-05-06 12:19:41
rubidium@openttd.org
Codechange: remove manual allocation/free for SQ_instance
4 files changed with 28 insertions and 36 deletions:
0 comments (0 inline, 0 general)
src/ai/ai_info.cpp
Show inline comments
 
@@ -69,20 +69,20 @@ template <> const char *GetClassName<AII
 
	SQInteger res = ScriptInfo::Constructor(vm, info);
 
	if (res != 0) return res;
 

	
 
	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->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
 
		if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
 
	} else {
 
		info->min_loadable_version = info->GetVersion();
 
	}
 
	/* When there is an UseAsRandomAI function, call it. */
 
	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->MethodExists(info->SQ_instance, "UseAsRandomAI")) {
 
		if (!info->engine->CallBoolMethod(info->SQ_instance, "UseAsRandomAI", &info->use_as_random, MAX_GET_OPS)) return SQ_ERROR;
 
	} else {
 
		info->use_as_random = true;
 
	}
 
	/* Try to get the API version the AI is written for. */
 
	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->MethodExists(info->SQ_instance, "GetAPIVersion")) {
 
		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;
 
@@ -149,7 +149,7 @@ bool AIInfo::CanLoadFromVersion(int vers
 
	}
 

	
 
	/* 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;
 
		return SQ_ERROR;
 
	}
src/game/game_info.cpp
Show inline comments
 
@@ -60,20 +60,20 @@ template <> const char *GetClassName<Gam
 
	SQInteger res = ScriptInfo::Constructor(vm, info);
 
	if (res != 0) return res;
 

	
 
	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->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
 
		if (!info->engine->CallIntegerMethod(info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
 
	} else {
 
		info->min_loadable_version = info->GetVersion();
 
	}
 
	/* When there is an IsSelectable function, call it. */
 
	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->MethodExists(info->SQ_instance, "IsDeveloperOnly")) {
 
		if (!info->engine->CallBoolMethod(info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
 
	} else {
 
		info->is_developer_only = false;
 
	}
 
	/* Try to get the API version the AI is written for. */
 
	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)) {
 
		Debug(script, 1, "Loading info.nut from ({}.{}): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
 
		return SQ_ERROR;
 
@@ -119,7 +119,7 @@ bool GameInfo::CanLoadFromVersion(int ve
 
	}
 

	
 
	/* 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;
 
		return SQ_ERROR;
 
	}
src/script/script_info.cpp
Show inline comments
 
@@ -18,14 +18,9 @@
 

	
 
#include "../safeguards.h"
 

	
 
ScriptInfo::~ScriptInfo()
 
{
 
	free(this->SQ_instance);
 
}
 

	
 
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));
 
		return false;
 
	}
 
@@ -35,10 +30,9 @@ bool ScriptInfo::CheckMethod(const char 
 
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
 
{
 
	/* 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 */
 
	sq_addref(vm, info->SQ_instance);
 
	sq_addref(vm, &info->SQ_instance);
 

	
 
	info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
 
	info->engine = info->scanner->GetEngine();
 
@@ -62,21 +56,21 @@ bool ScriptInfo::CheckMethod(const char 
 
	info->tar_file = info->scanner->GetTarFile();
 

	
 
	/* 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, "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->CallStringMethod(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_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->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->CallStringMethod(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
 
	if (info->engine->MethodExists(info->SQ_instance, "GetURL")) {
 
		if (!info->engine->CallStringMethod(info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
 
	}
 

	
 
	/* 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;
 
	}
 

	
 
@@ -85,7 +79,7 @@ bool ScriptInfo::CheckMethod(const char 
 

	
 
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)
src/script/script_info.hpp
Show inline comments
 
@@ -31,11 +31,9 @@ class ScriptInfo : public SimpleCountedO
 
public:
 
	ScriptInfo() :
 
		engine(nullptr),
 
		SQ_instance(nullptr),
 
		version(0),
 
		scanner(nullptr)
 
	{}
 
	~ScriptInfo();
 

	
 
	/**
 
	 * Get the Author of the script.
 
@@ -139,7 +137,7 @@ public:
 

	
 
protected:
 
	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.
 

	
 
private:
0 comments (0 inline, 0 general)