Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 856


Ignore:
Timestamp:
Mar 5, 2008, 11:07:16 PM (16 years ago)
Author:
landauf
Message:

loader seems to work now
added function/macro XMLPortObject to load objects as subobjects into another object (like attaching a weapon to a player) + nice debug output with indentation

Location:
code/branches/core
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core/bin/levels/sample.oxw

    r853 r856  
    3636
    3737<Model name="starship" position="(200,0,500)" scale="10" mesh="starship.mesh" yaw="-90" pitch="-90" />
    38 <Model name="drone" position="-200,0,500" scale="10" mesh="drone.mesh" yaw="-90" pitch="-90" />
     38<Model name="drone" position="-200,0,500" scale="10" mesh="drone.mesh" yaw="-90" pitch="-90">
     39  <attached>
     40    <Model name="drone2" position="200,-100,-500" scale="10" mesh="drone.mesh" yaw="-90" pitch="-90">
     41      <attached>
     42        <Model name="starship2" position="(500,200,-500)" scale="10" mesh="starship.mesh" yaw="-90" pitch="-90" />
     43      </attached>
     44    </Model>
     45  </attached>
     46</Model>
    3947<!--
    4048<Model position="-200,1000,500" scale="10" mesh="hoover_body.mesh" yaw="-90" pitch="-90" />
     
    5159<!-- Unused at the moment -->
    5260<!--  <SpaceshipSteeringObject node="OgreHeadNode" forward="500" rotateupdown="200" rotaterightleft="200" looprightleft="200" /> -->
    53 
     61<!--
    5462<script file="intro.lua" />
     63-->
  • code/branches/core/src/orxonox/core/BaseObject.cc

    r853 r856  
    3636#include "BaseObject.h"
    3737#include "XMLPort.h"
     38#include "Level.h"
    3839
    3940namespace orxonox
     
    7980    Element& BaseObject::XMLPort(Element& xmlelement, bool loading)
    8081    {
    81         std::cout << "1_1: " << this->getName() << std::endl;
    8282        XMLPortParam(BaseObject, "name", setName, getName, xmlelement, loading);
    83 std::cout << "1_2: " << this->getName() << std::endl;
     83
    8484        return xmlelement;
    8585    }
     86
     87    /**
     88        @brief Returns the levelfile that loaded this object.
     89        @return The levelfile
     90    */
     91    const std::string& BaseObject::getLevelfile() const
     92    {
     93        return this->level_->getFile();
     94    }
    8695}
  • code/branches/core/src/orxonox/core/BaseObject.h

    r852 r856  
    7777            /** @brief Returns a pointer to the level that loaded this object. @return The level */
    7878            inline const Level* getLevel() const { return this->level_; }
     79            const std::string& getLevelfile() const;
     80
     81            /** @brief Sets the indentation of the debug output in the Loader. @param indentation The indentation */
     82            inline void setLoaderIndentation(const std::string& indentation) { this->loaderIndentation_ = indentation; }
     83            /** @brief Returns the indentation of the debug output in the Loader. @return The indentation */
     84            inline const std::string& getLoaderIndentation() const { return this->loaderIndentation_; }
    7985
    8086        private:
     
    8389            bool bVisible_;                             //!< True = the object is visible
    8490            const Level* level_;                        //!< The level that loaded this object
     91            std::string loaderIndentation_;             //!< Indentation of the debug output in the Loader
    8592    };
    8693}
  • code/branches/core/src/orxonox/core/CorePrereqs.h

    r847 r856  
    103103  template <class T>
    104104  class SubclassIdentifier;
    105   class XMLPortParamContainer;
     105  template <class T, class O>
     106  class XMLPortClassObjectContainer;
    106107  template <class T>
    107108  class XMLPortClassParamContainer;
     109  class XMLPortObjectContainer;
     110  class XMLPortParamContainer;
    108111}
    109112
  • code/branches/core/src/orxonox/core/Debug.h

    r813 r856  
    5555#define ORX_PRINT_DEBUG_OUTPUT 1 // <-- fix that! should be a configurable setting
    5656
    57 #define ORX_HARD_DEBUG_LEVEL ORX_DEBUG
     57#define ORX_HARD_DEBUG_LEVEL ORX_vDEBUG
    5858//#define ORX_SOFT_DEBUG_LEVEL ORX_WARNING // <-- fix that! should be a configurable setting
    5959
  • code/branches/core/src/orxonox/core/Identifier.h

    r847 r856  
    6262#include "Debug.h"
    6363#include "Iterator.h"
    64 //#include "XMLPort.h"
    6564
    6665namespace orxonox
     
    158157            virtual void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) = 0;
    159158
     159            virtual XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname) = 0;
     160            virtual void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) = 0;
     161
    160162            static bool identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list);
    161163
     
    238240            void addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container);
    239241
     242            XMLPortObjectContainer* getXMLPortObjectContainer(const std::string& sectionname);
     243            void addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container);
     244
    240245        private:
    241246            ClassIdentifier();
     
    246251            bool bSetName_;             //!< True if the name is set
    247252            std::map<std::string, XMLPortClassParamContainer<T>*> xmlportParamContainers_;
     253            std::map<std::string, XMLPortClassObjectContainer<T, class O>*> xmlportObjectContainers_;
    248254    };
    249255
     
    336342    {
    337343        this->xmlportParamContainers_[paramname] = (XMLPortClassParamContainer<T>*)container;
     344    }
     345
     346    template <class T>
     347    XMLPortObjectContainer* ClassIdentifier<T>::getXMLPortObjectContainer(const std::string& sectionname)
     348    {
     349        typename std::map<std::string, XMLPortClassObjectContainer<T, class O>*>::const_iterator it = xmlportObjectContainers_.find(sectionname);
     350        if (it != xmlportObjectContainers_.end())
     351            return (XMLPortObjectContainer*)((*it).second);
     352        else
     353            return 0;
     354    }
     355
     356    template <class T>
     357    void ClassIdentifier<T>::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
     358    {
     359        this->xmlportObjectContainers_[sectionname] = (XMLPortClassObjectContainer<T, class O>*)container;
    338360    }
    339361
  • code/branches/core/src/orxonox/core/Level.h

    r843 r856  
    3232
    3333#include "CorePrereqs.h"
     34#include "ClassTreeMask.h"
    3435
    3536namespace orxonox
  • code/branches/core/src/orxonox/core/Loader.cc

    r852 r856  
    3939{
    4040    std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s;
     41    ClassTreeMask Loader::currentMask_s;
    4142
    4243    bool Loader::open(const Level* level, const ClassTreeMask& mask)
     
    104105    bool Loader::load(const Level* level, const ClassTreeMask& mask)
    105106    {
    106         ClassTreeMask loadmask = level->getMask() * mask;
     107        Loader::currentMask_s = level->getMask() * mask;
    107108
    108109        try
    109110        {
    110111            COUT(0) << "Start loading " << level->getFile() << "..." << std::endl;
    111             COUT(3) << "Mask: " << loadmask << std::endl;
     112            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
    112113
    113114            ticpp::Document xmlfile(level->getFile());
    114115            xmlfile.LoadFile();
    115116
    116             for ( ticpp::Iterator< ticpp::Element > child = xmlfile.FirstChildElement(); child != child.end(); child++ )
     117            for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ )
    117118            {
    118119                Identifier* identifier = ID(child->Value());
    119120                if (identifier)
    120121                {
    121                     COUT(4) << "  fabricating " << child->Value() << "..." << std::endl;
    122                     BaseObject* newObject = identifier->fabricate();
    123                     newObject->XMLPort(*child, true);
     122                    if (Loader::currentMask_s.isIncluded(identifier))
     123                    {
     124                        COUT(4) << "  fabricating " << child->Value() << "..." << std::endl;
     125                        BaseObject* newObject = identifier->fabricate();
     126                        newObject->setLoaderIndentation("    ");
     127                        newObject->setLevel(level);
     128                        newObject->XMLPort(*child, true);
     129                        COUT(5) << "  ...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
     130                    }
    124131                }
    125132                else
    126133                {
    127                     COUT(2) << "  Warning: '"<< child->Value() <<"' is not a valid classname." << std::endl;
     134                    COUT(2) << "  Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
    128135                }
    129136            }
     
    135142        catch(ticpp::Exception& ex)
    136143        {
     144            COUT(1) << std::endl;
    137145            COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl;
    138146            COUT(1) << ex.what() << std::endl;
  • code/branches/core/src/orxonox/core/Loader.h

    r826 r856  
    5656            static bool reload(const Level* level, const ClassTreeMask& mask = ClassTreeMask());
    5757
     58            static ClassTreeMask currentMask_s;
     59
    5860        private:
    5961            static std::vector<std::pair<const Level*, ClassTreeMask> > levels_s;
  • code/branches/core/src/orxonox/core/XMLPort.cc

    r847 r856  
    3131namespace orxonox
    3232{
     33    // ###############################
     34    // ###  XMLPortParamContainer  ###
     35    // ###############################
    3336    XMLPortParamContainer::XMLPortParamContainer()
    3437    {
     
    5356        return GetLocalisation(this->description_);
    5457    }
     58
     59
     60    // ################################
     61    // ###  XMLPortObjectContainer  ###
     62    // ################################
     63    XMLPortObjectContainer::XMLPortObjectContainer()
     64    {
     65        this->bAddedDescription_ = false;
     66    }
     67
     68    XMLPortObjectContainer& XMLPortObjectContainer::description(const std::string description)
     69    {
     70        if (!this->bAddedDescription_)
     71        {
     72            this->description_ = std::string("XMLPortObjectContainer::" + this->classname_ + "::" + this->sectionname_);
     73            AddLanguageEntry(this->description_, description);
     74            this->bAddedDescription_ = true;
     75        }
     76
     77        return (*this);
     78    }
     79
     80    const std::string& XMLPortObjectContainer::getDescription()
     81    {
     82        return GetLocalisation(this->description_);
     83    }
    5584}
  • code/branches/core/src/orxonox/core/XMLPort.h

    r854 r856  
    3535#include "Functor.h"
    3636#include "Debug.h"
     37#include "CoreIncludes.h"
     38#include "BaseObject.h"
     39#include "Loader.h"
    3740
    3841#include "CorePrereqs.h"
     
    5760    xmlcontainer##loadfunction##savefunction->port(this, xmlelement, loading)
    5861
     62#define XMLPortObject(classname, objectclass, sectionname, loadfunction, savefunction, xmlelement, loading) \
     63    orxonox::XMLPortClassObjectContainer<classname, objectclass>* xmlcontainer##loadfunction##savefunction = (orxonox::XMLPortClassObjectContainer<classname, objectclass>*)(this->getIdentifier()->getXMLPortObjectContainer(sectionname)); \
     64    if (!xmlcontainer##loadfunction##savefunction) \
     65    { \
     66        xmlcontainer##loadfunction##savefunction = new orxonox::XMLPortClassObjectContainer<classname, objectclass>(this->getIdentifier()->getName(), std::string(sectionname), &classname::loadfunction, &classname::savefunction); \
     67        this->getIdentifier()->addXMLPortObjectContainer(sectionname, xmlcontainer##loadfunction##savefunction); \
     68    } \
     69    xmlcontainer##loadfunction##savefunction->port(this, xmlelement, loading)
     70
     71
    5972namespace orxonox
    6073{
     74    // ###############################
     75    // ###  XMLPortParamContainer  ###
     76    // ###############################
    6177    class _CoreExport XMLPortParamContainer
    6278    {
     
    108124                if (loading)
    109125                {
    110                     std::string attribute = xmlelement.GetAttribute(this->paramname_);
    111                     if (attribute.size() > 0)
    112                     {
    113                         SubString tokens(attribute, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0');
    114                         if ((unsigned int)tokens.size() >= (unsigned int)this->loadfunction_->getParamCount())
     126                    try
     127                    {
     128                        std::string attribute = xmlelement.GetAttribute(this->paramname_);
     129                        if (attribute.size() > 0)
    115130                        {
    116                             if (this->loadfunction_->getParamCount() == 1)
     131                            SubString tokens(attribute, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0');
     132                            if ((unsigned int)tokens.size() >= (unsigned int)this->loadfunction_->getParamCount())
    117133                            {
    118                                 (*this->loadfunction_)(object, MultiTypeMath(attribute));
     134                                COUT(5) << object->getLoaderIndentation() << "Loading parameter " << this->paramname_ << " in " << this->classname_ << " (objectname " << object->getName() << ") with ";
     135                                if (this->loadfunction_->getParamCount() == 1)
     136                                {
     137                                    COUT(5) << "1 parameter (using whole string):" << std::endl;
     138                                    COUT(5) << object->getLoaderIndentation() << "  " << attribute << std::endl;
     139                                    (*this->loadfunction_)(object, MultiTypeMath(attribute));
     140                                }
     141                                else
     142                                {
     143                                    COUT(5) << tokens.size() << " parameter (using MultiTypeMath)." << std::endl;
     144                                    MultiTypeMath param1, param2, param3, param4, param5;
     145                                    if (tokens.size() >= 1) param1 = tokens[0];
     146                                    if (tokens.size() >= 2) param1 = tokens[1];
     147                                    if (tokens.size() >= 3) param1 = tokens[2];
     148                                    if (tokens.size() >= 4) param1 = tokens[3];
     149                                    if (tokens.size() >= 5) param1 = tokens[4];
     150                                    COUT(5) << object->getLoaderIndentation() << "  " << attribute << std::endl;
     151                                    COUT(5) << object->getLoaderIndentation() << "  " << tokens[0] << " -> " << param1 << ", " << tokens[1] << " -> " << param2 << ", " << tokens[2] << " -> " << param3 << ", " << tokens[3] << " -> " << param4 << ", " << tokens[4] << " -> " << param5 << std::endl;
     152
     153                                    (*this->loadfunction_)(object, param1, param2, param3, param4, param5);
     154                                }
    119155                            }
    120156                            else
    121157                            {
    122                                 MultiTypeMath param1, param2, param3, param4, param5;
    123                                 if (tokens.size() >= 1) param1 = tokens[0];
    124                                 if (tokens.size() >= 2) param1 = tokens[1];
    125                                 if (tokens.size() >= 3) param1 = tokens[2];
    126                                 if (tokens.size() >= 4) param1 = tokens[3];
    127                                 if (tokens.size() >= 5) param1 = tokens[4];
    128 
    129                                 (*this->loadfunction_)(object, param1, param2, param3, param4, param5);
     158                                COUT(2) << object->getLoaderIndentation() << "Warning: Parameter \"" << this->paramname_ << "\" in \"" << this->classname_ << "\" (objectname: " << object->getName() << ") is incomplete and couln't be loaded." << std::endl;
    130159                            }
    131160                        }
    132                         else
    133                         {
    134                             COUT(2) << "  Warning: Parameter \"" << this->paramname_ << "\" in \"" << this->classname_ << "\" (objectname: " << object->getName() << ") is incomplete and couln't be loaded." << std::endl;
    135                         }
     161                    }
     162                    catch(ticpp::Exception& ex)
     163                    {
     164                        COUT(1) << std::endl;
     165                        COUT(1) << "An error occurred in XMLPort.h while loading attribute '" << this->paramname_ << "' of '" << this->classname_ << "' (objectname: " << object->getName() << ") in " << object->getLevelfile() << ":" << std::endl;
     166                        COUT(1) << ex.what() << std::endl;
    136167                    }
    137168                }
     
    151182            FunctorMember<T>* savefunction_;
    152183    };
     184
     185
     186    // ################################
     187    // ###  XMLPortObjectContainer  ###
     188    // ################################
     189    class _CoreExport XMLPortObjectContainer
     190    {
     191        public:
     192            XMLPortObjectContainer();
     193
     194            inline const std::string& getName() const
     195                { return this->sectionname_; }
     196
     197            XMLPortObjectContainer& description(const std::string description);
     198            const std::string& getDescription();
     199
     200        protected:
     201            std::string classname_;
     202            std::string sectionname_;
     203
     204        private:
     205            LanguageEntryLabel description_;
     206            bool bAddedDescription_;
     207    };
     208
     209    template <class T, class O>
     210    class XMLPortClassObjectContainer : public XMLPortObjectContainer
     211    {
     212        public:
     213            XMLPortClassObjectContainer(const std::string classname, const std::string sectionname, void (T::*loadfunction)(O*), const O* (T::*savefunction)(unsigned int))
     214            {
     215                this->classname_ = classname;
     216                this->sectionname_ = sectionname;
     217                this->loadfunction_ = loadfunction;
     218                this->savefunction_ = savefunction;
     219            }
     220
     221            XMLPortObjectContainer& port(T* object, Element& xmlelement, bool loading)
     222            {
     223                if (loading)
     224                {
     225                    try
     226                    {
     227                        Element* xmlsubelement = xmlelement.FirstChildElement(this->sectionname_, false);
     228
     229                        if (xmlsubelement)
     230                        {
     231                            for ( ticpp::Iterator<ticpp::Element> child = xmlsubelement->FirstChildElement(false); child != child.end(); child++ )
     232                            {
     233                                Identifier* identifier = ID(child->Value());
     234                                if (identifier)
     235                                {
     236                                    if (identifier->isA(Class(O)))
     237                                    {
     238                                        if (Loader::currentMask_s.isIncluded(identifier))
     239                                        {
     240                                            COUT(4) << object->getLoaderIndentation() << "fabricating " << child->Value() << "..." << std::endl;
     241                                            O* newObject = (O*)identifier->fabricate();
     242                                            newObject->setLoaderIndentation(object->getLoaderIndentation() + "  ");
     243                                            newObject->setLevel(object->getLevel());
     244                                            newObject->XMLPort(*child, true);
     245                                            COUT(4) << object->getLoaderIndentation() << "assigning " << child->Value() << " (objectname " << newObject->getName() << ") to " << this->classname_ << " (objectname " << object->getName() << ")" << std::endl;
     246                                            (*object.*this->loadfunction_)(newObject);
     247                                            COUT(5) << "  ...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
     248                                        }
     249                                    }
     250                                    else
     251                                    {
     252                                        COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a '" << Class(O)->getName() << "'." << std::endl;
     253                                    }
     254                                }
     255                                else
     256                                {
     257                                    COUT(2) << object->getLoaderIndentation() << "Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
     258                                }
     259                            }
     260                        }
     261                    }
     262                    catch(ticpp::Exception& ex)
     263                    {
     264                        COUT(1) << std::endl;
     265                        COUT(1) << "An error occurred in XMLPort.h while loading a '" << Class(O)->getName() << "' in '" << this->sectionname_ << "' of '" << this->classname_ << "' (objectname: " << object->getName() << ") in " << object->getLevelfile() << ":" << std::endl;
     266                        COUT(1) << ex.what() << std::endl;
     267                    }
     268                }
     269                else
     270                {
     271                }
     272
     273                return (*this);
     274            }
     275
     276        private:
     277            void     (T::*loadfunction_)(O*);
     278            const O* (T::*savefunction_)(unsigned int);
     279    };
    153280}
    154281
  • code/branches/core/src/orxonox/objects/WorldEntity.cc

    r853 r856  
    167167    Element& WorldEntity::XMLPort(Element& xmlelement, bool loading)
    168168    {
    169 std::cout << "2_1: " << this->getPosition() << std::endl;
    170169        BaseObject::XMLPort(xmlelement, loading);
    171170
     
    178177        XMLPortParam(WorldEntity, "rotationAxis", setRotationAxis, getRotationAxis, xmlelement, loading);
    179178        XMLPortParam(WorldEntity, "rotationRate", setRotationRate, getRotationRate, xmlelement, loading);
    180 std::cout << "2_2: " << this->getPosition() << std::endl;
     179
     180        XMLPortObject(WorldEntity, WorldEntity, "attached", attachWorldEntity, getAttachedWorldEntity, xmlelement, loading);
    181181
    182182        return xmlelement;
     
    210210      registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA);*/
    211211    }
     212
     213    void WorldEntity::attachWorldEntity(WorldEntity* entity)
     214    {
     215        this->attachedWorldEntities_.push_back(entity);
     216    }
     217
     218    const WorldEntity* WorldEntity::getAttachedWorldEntity(unsigned int index)
     219    {
     220        if (index < this->attachedWorldEntities_.size())
     221            return this->attachedWorldEntities_[index];
     222        else
     223            return 0;
     224    }
    212225}
  • code/branches/core/src/orxonox/objects/WorldEntity.h

    r853 r856  
    2727            virtual Element& XMLPort(Element& xmlelement, bool loading);
    2828            bool create();
     29
     30            void attachWorldEntity(WorldEntity* entity);
     31            const WorldEntity* getAttachedWorldEntity(unsigned int index);
    2932
    3033            inline Ogre::SceneNode* getNode()
     
    156159            Ogre::SceneNode* node_;
    157160            bool bStatic_;
     161            std::vector<WorldEntity*> attachedWorldEntities_;
    158162    };
    159163}
Note: See TracChangeset for help on using the changeset viewer.