/* ----------------------------------------------------------------------------- GSFramework Copyright 2001-2013 Emmanuel Julien. All Rights Reserved. ----------------------------------------------------------------------------- */ #include "script/scripted_object.h" #include "script/script_unit.h" #include "metafile/nml.h" #include "log/log.h" using namespace GS::Script; using namespace GS::NML; //------------------------------------------------------------------------------ Unit *ScriptedObject::AddUnit(Unit *unit) { unit_list.Add(unit); return unit; } bool ScriptedObject::RemoveUnit(Unit *unit) { if (!unit || !unit_list.Remove(unit)) return false; _safe_delete(unit); return true; } Unit *ScriptedObject::GetUnit(const char *script_file, const char *script_class) const { ListForeachPtr(Unit *, unit, GetUnitList()) if ((unit->script_file == script_file) && (unit->script_class == script_class)) return unit; return NULL; } void ScriptedObject::RemoveAllUnit() { ListDeleteAllPtr(Unit *, unit_list) } //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ bool ScriptedObject::FromMetaTag(Tag &tag) { if (tag.name != "ScriptedObject") __ERR__(__LOG_E__ << "Could not parse scripted object, incorrect root tag (" << tag.name << ").\n", false) ListDeleteAllPtr(Unit *, unit_list) NMLTagForeach(pt, tag) { if (pt->name == "ScriptUnit") { if (Unit *unit = AddUnit(NewUnit())) if (unit->FromMetaTag(*pt)) unit->Open(); } else __LOG_W__ << "Unknown tag '" << pt->name << "' in .\n"; } return true; } Tag *ScriptedObject::AsMetaTag() const { if (!GetUnitList().GetCount()) return NULL; Tag *root = new Tag("ScriptedObject"); if (!root) __ERR__(__LOG_E__ << "Could not create root tag to serialize.\n", NULL) // Dump script units. ListForeachPtr(Unit *, unit, GetUnitList()) root->AddChild(unit->AsMetaTag()); return root; } //------------------------------------------------------------------------------ ScriptedObject::~ScriptedObject() { RemoveAllUnit(); }