| [2072] | 1 | /* | 
|---|
|  | 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
|  | 3 | *                    > www.orxonox.net < | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * | 
|---|
|  | 6 | *   License notice: | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This program is free software; you can redistribute it and/or | 
|---|
|  | 9 | *   modify it under the terms of the GNU General Public License | 
|---|
|  | 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
|  | 11 | *   of the License, or (at your option) any later version. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
|  | 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 16 | *   GNU General Public License for more details. | 
|---|
|  | 17 | * | 
|---|
|  | 18 | *   You should have received a copy of the GNU General Public License | 
|---|
|  | 19 | *   along with this program; if not, write to the Free Software | 
|---|
|  | 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
|  | 21 | * | 
|---|
|  | 22 | *   Author: | 
|---|
|  | 23 | *      Fabian 'x3n' Landau | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | #include "Template.h" | 
|---|
|  | 30 |  | 
|---|
| [3196] | 31 | #include <tinyxml/tinyxml.h> | 
|---|
| [2710] | 32 | #include <tinyxml/ticpp.h> | 
|---|
|  | 33 |  | 
|---|
| [8858] | 34 | #include "util/Output.h" | 
|---|
| [6746] | 35 | #include "CoreIncludes.h" | 
|---|
|  | 36 | #include "XMLPort.h" | 
|---|
| [2072] | 37 |  | 
|---|
|  | 38 | namespace orxonox | 
|---|
|  | 39 | { | 
|---|
| [9667] | 40 | RegisterClass(Template); | 
|---|
| [2072] | 41 |  | 
|---|
| [9667] | 42 | Template::Template(Context* context) : BaseObject(context) | 
|---|
| [2072] | 43 | { | 
|---|
| [3196] | 44 | this->xmlelement_ = new TiXmlElement(""); | 
|---|
|  | 45 |  | 
|---|
| [2072] | 46 | RegisterObject(Template); | 
|---|
|  | 47 |  | 
|---|
|  | 48 | this->bIsLink_ = false; | 
|---|
| [2662] | 49 | this->bLoadDefaults_ = true; | 
|---|
| [2072] | 50 | this->bIsReturningXMLElement_ = false; | 
|---|
|  | 51 | this->baseclassIdentifier_ = 0; | 
|---|
|  | 52 | } | 
|---|
|  | 53 |  | 
|---|
|  | 54 | Template::~Template() | 
|---|
|  | 55 | { | 
|---|
|  | 56 | Template::getTemplateMap().erase(this->getName()); | 
|---|
| [3196] | 57 | delete this->xmlelement_; | 
|---|
| [2072] | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | void Template::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
|  | 61 | { | 
|---|
|  | 62 | SUPER(Template, XMLPort, xmlelement, mode); | 
|---|
|  | 63 |  | 
|---|
| [2662] | 64 | XMLPortParam(Template, "link",      setLink,         getLink,         xmlelement, mode); | 
|---|
|  | 65 | XMLPortParam(Template, "baseclass", setBaseclass,    getBaseclass,    xmlelement, mode); | 
|---|
|  | 66 | XMLPortParam(Template, "defaults",  setLoadDefaults, getLoadDefaults, xmlelement, mode).defaultValues(true); | 
|---|
| [2072] | 67 |  | 
|---|
|  | 68 | Element* element = xmlelement.FirstChildElement(false); | 
|---|
|  | 69 | if (element) | 
|---|
|  | 70 | { | 
|---|
| [2896] | 71 | TiXmlElement* tixmlelement = static_cast<TiXmlElement*>(element->GetTiXmlPointer()); | 
|---|
| [2072] | 72 | if (tixmlelement) | 
|---|
|  | 73 | this->setXMLElement(*tixmlelement); | 
|---|
|  | 74 | } | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
|  | 77 | void Template::changedName() | 
|---|
|  | 78 | { | 
|---|
| [2662] | 79 | SUPER(Template, changedName); | 
|---|
|  | 80 |  | 
|---|
| [6417] | 81 | if (!this->getName().empty()) | 
|---|
| [2072] | 82 | { | 
|---|
|  | 83 | std::map<std::string, Template*>::iterator it; | 
|---|
|  | 84 | it = Template::getTemplateMap().find(this->getOldName()); | 
|---|
|  | 85 | if (it != Template::getTemplateMap().end()) | 
|---|
|  | 86 | Template::getTemplateMap().erase(it); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | it = Template::getTemplateMap().find(this->getName()); | 
|---|
|  | 89 | if (it != Template::getTemplateMap().end()) | 
|---|
| [8858] | 90 | orxout(internal_warning, context::templates) << "Template with name \"" << this->getName() << "\" already exists." << endl; | 
|---|
| [2072] | 91 | else | 
|---|
|  | 92 | Template::getTemplateMap()[this->getName()] = this; | 
|---|
|  | 93 | } | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
| [3196] | 96 | void Template::setXMLElement(const TiXmlElement& xmlelement) | 
|---|
|  | 97 | { | 
|---|
|  | 98 | *this->xmlelement_ = xmlelement; | 
|---|
|  | 99 | } | 
|---|
|  | 100 |  | 
|---|
| [7163] | 101 | const TiXmlElement& Template::getXMLElement() | 
|---|
| [2072] | 102 | { | 
|---|
|  | 103 | if (this->bIsLink_) | 
|---|
|  | 104 | { | 
|---|
|  | 105 | Template* temp = Template::getTemplate(this->link_); | 
|---|
|  | 106 | if (temp) | 
|---|
|  | 107 | { | 
|---|
| [7163] | 108 | this->bLoadDefaults_ = temp->bLoadDefaults_; | 
|---|
|  | 109 |  | 
|---|
| [2072] | 110 | if (!temp->bIsReturningXMLElement_) | 
|---|
|  | 111 | { | 
|---|
|  | 112 | this->bIsReturningXMLElement_ = true; | 
|---|
|  | 113 | const TiXmlElement& element = temp->getXMLElement(); | 
|---|
|  | 114 | this->bIsReturningXMLElement_ = false; | 
|---|
|  | 115 | return element; | 
|---|
|  | 116 | } | 
|---|
|  | 117 | else | 
|---|
|  | 118 | { | 
|---|
| [8858] | 119 | orxout(internal_warning, context::templates) << "Linking from \"" << this->getName() << "\" to \"" << this->link_ << "\" leads to an infinite loop. Returning own element." << endl; | 
|---|
| [2072] | 120 | } | 
|---|
|  | 121 | } | 
|---|
|  | 122 | else | 
|---|
|  | 123 | { | 
|---|
| [8858] | 124 | orxout(internal_warning, context::templates) << '"' << this->link_ << "\" is not an existing Template name. Returning own element." << endl; | 
|---|
| [2072] | 125 | } | 
|---|
|  | 126 | } | 
|---|
|  | 127 |  | 
|---|
| [3196] | 128 | return *this->xmlelement_; | 
|---|
| [2072] | 129 | } | 
|---|
|  | 130 |  | 
|---|
|  | 131 | void Template::setBaseclass(const std::string& baseclass) | 
|---|
|  | 132 | { | 
|---|
|  | 133 | this->baseclassIdentifier_ = ClassByString(baseclass); | 
|---|
|  | 134 | if (this->baseclassIdentifier_) | 
|---|
|  | 135 | this->baseclass_ = baseclass; | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | void Template::applyOn(BaseObject* object) | 
|---|
|  | 139 | { | 
|---|
|  | 140 | if (this->baseclassIdentifier_) | 
|---|
|  | 141 | { | 
|---|
|  | 142 | if (!object->isA(this->baseclassIdentifier_)) | 
|---|
|  | 143 | { | 
|---|
| [8858] | 144 | orxout(internal_error, context::templates) << "Can't apply template (name: " << this->getName() << "), object (name: " << object->getName() << ", class: " << object->getIdentifier()->getName() << ") is not a " << this->baseclassIdentifier_->getName() << endl; | 
|---|
| [2072] | 145 | return; | 
|---|
|  | 146 | } | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
| [8858] | 149 | orxout(verbose, context::templates) << object->getLoaderIndentation() << " aplying Template \"" << this->getName() << "\"..." << endl; | 
|---|
| [2072] | 150 |  | 
|---|
| [9348] | 151 | // check if the template is applied on an object of the right type | 
|---|
| [9667] | 152 | Identifier* identifier = ClassByString(this->getXMLElement().Value()); | 
|---|
| [9348] | 153 | if (!object->getIdentifier()->isA(identifier)) | 
|---|
|  | 154 | orxout(internal_warning, context::templates) << "Template was defined for " << identifier->getName() << " but the object is of type " << object->getIdentifier()->getName() << endl; | 
|---|
|  | 155 |  | 
|---|
| [2896] | 156 | Element temp = &const_cast<TiXmlElement&>(this->getXMLElement()); | 
|---|
| [2662] | 157 |  | 
|---|
|  | 158 | if (this->bLoadDefaults_) | 
|---|
|  | 159 | object->XMLPort(temp, XMLPort::LoadObject); | 
|---|
|  | 160 | else | 
|---|
|  | 161 | object->XMLPort(temp, XMLPort::ExpandObject); | 
|---|
| [2072] | 162 | } | 
|---|
|  | 163 |  | 
|---|
|  | 164 | std::map<std::string, Template*>& Template::getTemplateMap() | 
|---|
|  | 165 | { | 
|---|
|  | 166 | static std::map<std::string, Template*> templateMap; | 
|---|
|  | 167 | return templateMap; | 
|---|
|  | 168 | } | 
|---|
|  | 169 |  | 
|---|
|  | 170 | Template* Template::getTemplate(const std::string& name) | 
|---|
|  | 171 | { | 
|---|
|  | 172 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); | 
|---|
|  | 173 | if (it != Template::getTemplateMap().end()) | 
|---|
|  | 174 | return it->second; | 
|---|
|  | 175 | else | 
|---|
|  | 176 | { | 
|---|
| [8858] | 177 | orxout(internal_warning, context::templates) << "Template with name " << name << " doesn't exist." << endl; | 
|---|
| [2072] | 178 | return 0; | 
|---|
|  | 179 | } | 
|---|
|  | 180 | } | 
|---|
|  | 181 |  | 
|---|
|  | 182 | void Template::apply(const std::string& name, BaseObject* object) | 
|---|
|  | 183 | { | 
|---|
|  | 184 | std::map<std::string, Template*>::iterator it = Template::getTemplateMap().find(name); | 
|---|
|  | 185 | if (it != Template::getTemplateMap().end()) | 
|---|
|  | 186 | it->second->applyOn(object); | 
|---|
|  | 187 | } | 
|---|
|  | 188 | } | 
|---|