Changeset - r27369:d93808e810cd
[Not reviewed]
master
0 4 0
Rubidium - 16 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
 
@@ -66,26 +66,26 @@ template <> const char *GetClassName<AII
 
	if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of AIInfo to RegisterAI");
 
	AIInfo *info = (AIInfo *)instance;
 

	
 
	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;
 
		}
 
	} else {
 
		info->api_version = "0.7";
 
@@ -146,13 +146,13 @@ bool AIInfo::CanLoadFromVersion(int vers
 
	if (res != 0) {
 
		delete library;
 
		return res;
 
	}
 

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

	
 
	/* Register the Library to the base system */
 
	library->GetScanner()->RegisterScript(library);
src/game/game_info.cpp
Show inline comments
 
@@ -57,26 +57,26 @@ template <> const char *GetClassName<Gam
 
	if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
 
	GameInfo *info = (GameInfo *)instance;
 

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

	
 
	/* Remove the link to the real instance, else it might get deleted by RegisterGame() */
 
@@ -116,13 +116,13 @@ bool GameInfo::CanLoadFromVersion(int ve
 
	if (res != 0) {
 
		delete library;
 
		return res;
 
	}
 

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

	
 
	/* Register the Library to the base system */
 
	library->GetScanner()->RegisterScript(library);
src/script/script_info.cpp
Show inline comments
 
@@ -15,33 +15,27 @@
 
#include "script_info.hpp"
 
#include "script_scanner.hpp"
 
#include "../3rdparty/fmt/format.h"
 

	
 
#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;
 
	}
 
	return true;
 
}
 

	
 
/* 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();
 

	
 
	/* Ensure the mandatory functions exist */
 
	static const char * const required_functions[] = {
 
@@ -59,36 +53,36 @@ bool ScriptInfo::CheckMethod(const char 
 

	
 
	/* Get location information of the scanner */
 
	info->main_script = info->scanner->GetMainScript();
 
	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;
 
	}
 

	
 
	return 0;
 
}
 

	
 
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)
 
{
 
	ScriptConfigItem config;
 
	uint items = 0;
src/script/script_info.hpp
Show inline comments
 
@@ -28,17 +28,15 @@ static const int MAX_GET_SETTING_OPS    
 

	
 
/** All static information from an Script like name, version, etc. */
 
class ScriptInfo : public SimpleCountedObject {
 
public:
 
	ScriptInfo() :
 
		engine(nullptr),
 
		SQ_instance(nullptr),
 
		version(0),
 
		scanner(nullptr)
 
	{}
 
	~ScriptInfo();
 

	
 
	/**
 
	 * Get the Author of the script.
 
	 */
 
	const std::string &GetAuthor() const { return this->author; }
 

	
 
@@ -136,13 +134,13 @@ public:
 
	 * Can this script be selected by developers only?
 
	 */
 
	virtual bool IsDeveloperOnly() const { return false; }
 

	
 
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:
 
	std::string main_script;      ///< The full path of the script.
 
	std::string tar_file;         ///< If, which tar file the script was in.
 
	std::string author;           ///< Author of the script.
0 comments (0 inline, 0 general)