80 lines
2.4 KiB
C++
80 lines
2.4 KiB
C++
/* -----------------------------------------------------------------------------
|
|
GSFramework
|
|
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
|
----------------------------------------------------------------------------- */
|
|
|
|
|
|
#include "script/script_unit.h"
|
|
#include "script/script_object.h"
|
|
#include "script/script_variant.h"
|
|
#include "log/log.h"
|
|
|
|
using namespace GS::Script;
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
bool Unit::SetupFunctionCall(const char *func, const Object *func_object)
|
|
{
|
|
if (!vm || !self || !vm->SetupFunctionCall(func, func_object, self))
|
|
return false;
|
|
return vm->SetFunctionCallContext(Variant(iface_object, iface_type));
|
|
}
|
|
bool Unit::PushUserObjectFunctionCallArgument(void *p, uint type, bool managed)
|
|
{ return vm->PushArgument(Variant(p, type)); }
|
|
bool Unit::PushFunctionCallArgument(const Variant &arg)
|
|
{ return vm->PushArgument(arg); }
|
|
bool Unit::DoFunctionCall(Variant *return_value)
|
|
{ return vm->DoFunctionCall(return_value); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
bool Unit::Open()
|
|
{
|
|
if (!vm)
|
|
return false;
|
|
if (self)
|
|
return true;
|
|
|
|
if (script_file.IsEmpty() || script_class.IsEmpty())
|
|
return true;
|
|
|
|
// Instantiate class.
|
|
if (!vm->CompileFile(script_file) || !vm->SetupFunctionCall(script_class))
|
|
return false;
|
|
|
|
Variant rv;
|
|
if (!vm->DoFunctionCall(&rv) || (rv.type != Variant::Type_ScriptObject))
|
|
{
|
|
vm->Kill(String::Format("Function call to '%s' failed.", script_class.c_str()));
|
|
return false;
|
|
}
|
|
self = rv.object;
|
|
rv.object = NULL; // Detach object from the variant so that it does not get deleted.
|
|
|
|
// Send parameters to the instance.
|
|
ListForeachPtr(GS::Variant *, v, parm_list)
|
|
vm->Set(v->id, Variant(*v), self);
|
|
|
|
return true;
|
|
}
|
|
void Unit::Close()
|
|
{
|
|
// Note: Do not delete the parameter list from here.
|
|
_safe_delete(self);
|
|
}
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
Unit::Unit(IVM *_vm) : vm(_vm)
|
|
{
|
|
iface_object = NULL;
|
|
iface_type = (uint)~0;
|
|
self = NULL;
|
|
}
|
|
Unit::~Unit()
|
|
{
|
|
ListDeleteAllPtr(GS::Variant *, parm_list)
|
|
Close();
|
|
}
|
|
//------------------------------------------------------------------------------
|