src/ai/ai_info.cpp
author truebrain
Mon, 30 Jun 2008 12:15:10 +0000
branchnoai
changeset 11097 6967c52c78c5
parent 10942 cd3f2d07199f
permissions -rw-r--r--
(svn r13654) [NoAI] -Change [API CHANGE]: moved AISetting::SetCommandDelay to AIController::SetCommandDelay, as it was silly to have it in a seperate class, while it was part of the controller.
/* $Id$ */

/** @file ai_info.cpp Implementation of AIFileInfo */

#include "../stdafx.h"
#include "../core/alloc_func.hpp"

#include <squirrel.h>
#include "../squirrel.hpp"
#include "../squirrel_helper.hpp"
#include "../squirrel_class.hpp"
#include "../squirrel_std.hpp"
#include "ai.h"
#include "ai_info.hpp"
#include "ai_squirrel.hpp"
#include "api/ai_controller.hpp"

AIFileInfo::~AIFileInfo()
{
	this->engine->ReleaseObject(this->SQ_instance);
	free((void *)this->author);
	free((void *)this->name);
	free((void *)this->description);
	free((void *)this->date);
	free((void *)this->instance_name);
	free(this->script_name);
	free(this->dir_name);
	free(this->SQ_instance);
}

const char *AIFileInfo::GetAuthor()
{
	if (this->author == NULL) this->author = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetAuthor");
	return this->author;
}

const char *AIFileInfo::GetName()
{
	if (this->name == NULL) this->name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetName");
	return this->name;
}

const char *AIFileInfo::GetDescription()
{
	if (this->description == NULL) this->description = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetDescription");
	return this->description;
}

int AIFileInfo::GetVersion()
{
	return this->engine->CallIntegerMethod(*this->SQ_instance, "GetVersion");
}

const char *AIFileInfo::GetDate()
{
	if (this->date == NULL) this->date = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetDate");
	return this->date;
}

const char *AIFileInfo::GetInstanceName()
{
	if (this->instance_name == NULL) this->instance_name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "CreateInstance");
	return this->instance_name;
}

bool AIFileInfo::AllowStartup()
{
	return true;
}

AIController *AIFileInfo::CreateInstance()
{
	AIController *ai_controller = new AIController(this->script_name, this->GetInstanceName());
	return ai_controller;
}

const char *AIFileInfo::GetDirName()
{
	return this->dir_name;
}

const char *AIFileInfo::GetScriptName()
{
	return this->script_name;
}

void AIFileInfo::CheckMethods(SQInteger *res, const char *name)
{
	if (!this->engine->MethodExists(*this->SQ_instance, name)) {
		char error[1024];
		snprintf(error, sizeof(error), "your AIFileInfo doesn't have the method '%s'", name);
		this->engine->ThrowError(error);
		*res = SQ_ERROR;
	}
}

/* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info)
{
	SQInteger res = 0;

	/* Set some basic info from the parent */
	info->SQ_instance = MallocT<SQObject>(sizeof(SQObject));
	Squirrel::GetInstance(vm, info->SQ_instance, 2);
	/* Make sure the instance stays alive over time */
	sq_addref(vm, info->SQ_instance);
	info->base = ((AISquirrel *)Squirrel::GetGlobalPointer(vm));
	info->engine = info->base->GetEngine();

	/* Check if all needed fields are there */
	info->CheckMethods(&res, "GetAuthor");
	info->CheckMethods(&res, "GetName");
	info->CheckMethods(&res, "GetDescription");
	info->CheckMethods(&res, "GetVersion");
	info->CheckMethods(&res, "GetDate");
	info->CheckMethods(&res, "CreateInstance");

	/* Abort if one method was missing */
	if (res != 0) return res;

	info->script_name = strdup(info->base->GetCurrentScript());
	info->dir_name = strdup(info->base->GetCurrentDirName());

	return 0;
}

/* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm)
{
	/* Create a new AIFileInfo */
	AIInfo *info = new AIInfo();

	SQInteger res = AIFileInfo::Constructor(vm, info);
	if (res != 0) return res;

	/* Register the AI to the base system */
	info->base->RegisterAI(info);

	return 0;
}

/* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
{
	/* Create a new AIFileInfo */
	AILibrary *library = new AILibrary();

	SQInteger res = AIFileInfo::Constructor(vm, library);
	if (res != 0) return res;

	/* Register the Library to the base system */
	library->base->RegisterLibrary(library);

	return 0;
}

/* static */ SQInteger AILibrary::Import(HSQUIRRELVM vm)
{
	SQConvert::SQAutoFreePointers ptr;
	const char *library = GetParam(SQConvert::ForceType<const char *>(), vm, 2, &ptr);
	const char *class_name = GetParam(SQConvert::ForceType<const char *>(), vm, 3, &ptr);
	int version = GetParam(SQConvert::ForceType<int>(), vm, 4, &ptr);

	if (!AI_ImportLibrary(library, class_name, version, vm)) return -1;
	return 1;
}