| 1 | /* | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 |    ### File Specific: | 
|---|
| 12 |    main-programmer: Benjamin Grauer | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING | 
|---|
| 17 |  | 
|---|
| 18 | #include "load_param.h" | 
|---|
| 19 | #include "load_param_description.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include <stdarg.h> | 
|---|
| 22 |  | 
|---|
| 23 | /** | 
|---|
| 24 |  * Constructs a new LoadParameter | 
|---|
| 25 |  * @param root the XML-element to load this Parameter from | 
|---|
| 26 |  * @param paramName the Parameter to load | 
|---|
| 27 |  * @param object the BaseObject, to load this parameter on to (will be cast to executor's Parameter) | 
|---|
| 28 |  * @param executor the Executor, that executes the loading procedure. | 
|---|
| 29 |  */ | 
|---|
| 30 | CLoadParam::CLoadParam(const TiXmlElement* root, const char* paramName, BaseObject* object, const Executor& executor, bool inLoadCycle) | 
|---|
| 31 | { | 
|---|
| 32 |   this->paramName = paramName; | 
|---|
| 33 |   this->object = object; | 
|---|
| 34 |   this->inLoadCycle = inLoadCycle; | 
|---|
| 35 |  | 
|---|
| 36 |   // determin the LoadString. | 
|---|
| 37 |   if (likely(!inLoadCycle)) | 
|---|
| 38 |     this->loadElem = grabParameterElement(root, paramName); | 
|---|
| 39 |   //this->loadString = grabParameter(root, paramName); | 
|---|
| 40 |   else if (!strcmp(root->Value(), paramName)) | 
|---|
| 41 |     this->loadElem = (TiXmlElement*)root->FirstChild(); | 
|---|
| 42 |   else | 
|---|
| 43 |     this->loadElem = NULL; | 
|---|
| 44 |  | 
|---|
| 45 |   // set the Executor. | 
|---|
| 46 |   this->executor = executor.clone(); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | /** | 
|---|
| 50 |  * This is a VERY SPECIAL deconsrtuctor. | 
|---|
| 51 |  * It is made, so that it loads the Parameters on destruction. | 
|---|
| 52 |  * meaning, if an Executor a valid Object exist, and all | 
|---|
| 53 |  * Execution-Conditions are met, they are executed here. | 
|---|
| 54 |  */ | 
|---|
| 55 | CLoadParam::~CLoadParam() | 
|---|
| 56 | { | 
|---|
| 57 |   if (likely(this->executor != NULL)) | 
|---|
| 58 |   { | 
|---|
| 59 |     if (this->loadElem != NULL &&  this->loadElem->ToText()) | 
|---|
| 60 |       this->loadString = this->loadElem->Value(); | 
|---|
| 61 |     else | 
|---|
| 62 |       this->loadString = NULL; | 
|---|
| 63 |     if (likely(this->object != NULL) && | 
|---|
| 64 |         ( this->loadString != NULL || | 
|---|
| 65 |          ((this->executor->getType() & Executor_NoLoadString) == Executor_NoLoadString))) | 
|---|
| 66 |     { | 
|---|
| 67 |       PRINTF(4)("Loading '%s' with Parameters '%s' onto: '%s'(%s)\n", this->paramName, this->loadString, this->object->getName(), this->object->getClassName()); | 
|---|
| 68 |       this->executor->execute(this->object, this->loadString); | 
|---|
| 69 |     } | 
|---|
| 70 |     delete this->executor; | 
|---|
| 71 |   } | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | /** | 
|---|
| 75 |  * set the default values of the executor | 
|---|
| 76 |  * @param count how many default values to set. | 
|---|
| 77 |  * @param ... the default values !! must be at least count parameters!! | 
|---|
| 78 |  */ | 
|---|
| 79 | CLoadParam& CLoadParam::defaultValues(unsigned int count, ...) | 
|---|
| 80 | { | 
|---|
| 81 |   va_list values; | 
|---|
| 82 |   va_start(values, count); | 
|---|
| 83 |  | 
|---|
| 84 |   assert(executor != NULL); | 
|---|
| 85 |   this->executor->defaultValues(count, values); | 
|---|
| 86 |  | 
|---|
| 87 |   return *this; | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | /** | 
|---|
| 93 |  * @param descriptionText The text to set as a description for this Parameter | 
|---|
| 94 |  * @returns a pointer to itself. | 
|---|
| 95 | */ | 
|---|
| 96 | CLoadParam& CLoadParam::describe(const char* descriptionText) | 
|---|
| 97 | { | 
|---|
| 98 |   if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription()) | 
|---|
| 99 |     { | 
|---|
| 100 |       this->paramDesc->setDescription(descriptionText); | 
|---|
| 101 |     } | 
|---|
| 102 |   return *this; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | // const LoadParamDescription* LoadParamDescription::getClass(const char* className) | 
|---|
| 106 | // { | 
|---|
| 107 | //   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); | 
|---|
| 108 | //   LoadClassDescription* enumClassDesc = iterator->firstElement(); | 
|---|
| 109 | //   while (enumClassDesc) | 
|---|
| 110 | //   { | 
|---|
| 111 | //     if (!strcmp(enumClassDesc->className, classNameBegin, className)) | 
|---|
| 112 | //     { | 
|---|
| 113 | //       delete iterator; | 
|---|
| 114 | //       return enumClassDesc; | 
|---|
| 115 | //     } | 
|---|
| 116 | //     enumClassDesc = iterator->nextElement(); | 
|---|
| 117 | //   } | 
|---|
| 118 | //   delete iterator; | 
|---|
| 119 | // | 
|---|
| 120 | //   return NULL; | 
|---|
| 121 | // } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | /* | 
|---|
| 127 |  * @param object The object this Parameter is loaded too. | 
|---|
| 128 |  * @param root: the XML-element to load this option from. | 
|---|
| 129 |  * @param paramName: The name of the parameter loaded. | 
|---|
| 130 |  * @param paramCount: how many parameters this loading-function takes | 
|---|
| 131 |  * @param multi: if false LoadParam assumes only one occurence of this parameter in root, if true it assumes multiple occurences. | 
|---|
| 132 |  * @param ...: the parameter information (1. Parameter, 2. Default Value for the Parameter, ...) | 
|---|
| 133 | */ | 
|---|
| 134 | /*LoadParam::LoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, | 
|---|
| 135 |                              int paramCount, bool multi, const void* pointerToParam, ...) | 
|---|
| 136 | { | 
|---|
| 137 |   this->setClassID(CL_LOAD_PARAM, "LoadParam"); | 
|---|
| 138 |   this->executor = NULL; | 
|---|
| 139 |  | 
|---|
| 140 |   this->loadString = NULL; | 
|---|
| 141 |   this->pointerToParam = pointerToParam; | 
|---|
| 142 |  | 
|---|
| 143 |   if (paramCount == 0 || this->pointerToParam != NULL) | 
|---|
| 144 |     this->loadString = "none"; | 
|---|
| 145 |   else | 
|---|
| 146 | { | 
|---|
| 147 |       if (likely(!multi)) | 
|---|
| 148 |         this->loadString = grabParameter(root, paramName); | 
|---|
| 149 |       else | 
|---|
| 150 | { | 
|---|
| 151 |           if (!strcmp(root->Value(), paramName)) | 
|---|
| 152 | { | 
|---|
| 153 |               const TiXmlNode* val = root->FirstChild(); | 
|---|
| 154 |               if( val->ToText()) | 
|---|
| 155 |                 this->loadString = val->Value(); | 
|---|
| 156 | } | 
|---|
| 157 | } | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 |   this->paramDesc = NULL; | 
|---|
| 161 |   if (LoadClassDescription::parametersDescription) | 
|---|
| 162 | { | 
|---|
| 163 |     // locating the class | 
|---|
| 164 |     this->classDesc = LoadClassDescription::addClass(object->getClassName()); | 
|---|
| 165 |  | 
|---|
| 166 |     if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL) | 
|---|
| 167 | { | 
|---|
| 168 |  | 
|---|
| 169 |       this->paramDesc->paramCount = paramCount; | 
|---|
| 170 |       this->paramDesc->types = new int[paramCount]; | 
|---|
| 171 |       this->paramDesc->defaultValues = new char*[paramCount]; | 
|---|
| 172 |  | 
|---|
| 173 |       va_list types; | 
|---|
| 174 |       va_start (types, pointerToParam); | 
|---|
| 175 |       char defaultVal[512]; | 
|---|
| 176 |       for(int i = 0; i < paramCount; i++) | 
|---|
| 177 | { | 
|---|
| 178 |         defaultVal[0] = '\0'; | 
|---|
| 179 |           // parameters parsed | 
|---|
| 180 |         int tmpType = va_arg (types, int); | 
|---|
| 181 |         this->paramDesc->types[i] = tmpType; | 
|---|
| 182 |         switch (tmpType) | 
|---|
| 183 | { | 
|---|
| 184 |   case MT_INT: | 
|---|
| 185 |             sprintf(defaultVal, "%d", va_arg(types, int)); | 
|---|
| 186 |             break; | 
|---|
| 187 | //          case MT_LONG: | 
|---|
| 188 | //            sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE)); | 
|---|
| 189 | //            break; | 
|---|
| 190 |   case MT_FLOAT: | 
|---|
| 191 |             sprintf(defaultVal, "%0.3f", va_arg(types, double)); | 
|---|
| 192 |             break; | 
|---|
| 193 |   case MT_STRING: | 
|---|
| 194 |             sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE)); | 
|---|
| 195 |             break; | 
|---|
| 196 |   case MT_EXT1: | 
|---|
| 197 |             sprintf(defaultVal, ""); | 
|---|
| 198 |             break; | 
|---|
| 199 | } | 
|---|
| 200 |         this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1]; | 
|---|
| 201 |         strcpy(this->paramDesc->defaultValues[i], defaultVal); | 
|---|
| 202 | } | 
|---|
| 203 |       va_end(types); | 
|---|
| 204 |  | 
|---|
| 205 |       int argCount = 0; | 
|---|
| 206 | } | 
|---|
| 207 | } | 
|---|
| 208 | }*/ | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 |  | 
|---|
| 212 |  | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | ////////////////////// | 
|---|
| 220 | // HELPER FUNCTIONS // | 
|---|
| 221 | ////////////////////// | 
|---|
| 222 | /** | 
|---|
| 223 |  * @param root: The XML-element to grab a parameter from | 
|---|
| 224 |  * @param parameterName: the parameter to grab | 
|---|
| 225 |  * @returns the Value of the parameter if found, NULL otherwise | 
|---|
| 226 | */ | 
|---|
| 227 | const char* grabParameter(const TiXmlElement* root, const char* parameterName) | 
|---|
| 228 | { | 
|---|
| 229 |   const TiXmlElement* element; | 
|---|
| 230 |   const TiXmlNode* node; | 
|---|
| 231 |  | 
|---|
| 232 |   if (root == NULL || parameterName == NULL) | 
|---|
| 233 |     return NULL; | 
|---|
| 234 |   assert( parameterName != NULL); | 
|---|
| 235 |  | 
|---|
| 236 |   element = root->FirstChildElement( parameterName); | 
|---|
| 237 |   if( element == NULL) return NULL; | 
|---|
| 238 |  | 
|---|
| 239 |   node = element->FirstChild(); | 
|---|
| 240 |   while( node != NULL) | 
|---|
| 241 |     { | 
|---|
| 242 |       if( node->ToText()) return node->Value(); | 
|---|
| 243 |       node = node->NextSibling(); | 
|---|
| 244 |     } | 
|---|
| 245 |   return NULL; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | /** | 
|---|
| 249 |  * @param root: The XML-element to grab a parameter from | 
|---|
| 250 |  * @param parameterName: the parameter to grab | 
|---|
| 251 |  * @returns the Element of the parameter if found, NULL otherwise | 
|---|
| 252 |  */ | 
|---|
| 253 | const TiXmlElement* grabParameterElement(const TiXmlElement* root, const char* parameterName) | 
|---|
| 254 | { | 
|---|
| 255 |   const TiXmlElement* element; | 
|---|
| 256 |   const TiXmlNode* node; | 
|---|
| 257 |  | 
|---|
| 258 |   if (root == NULL || parameterName == NULL) | 
|---|
| 259 |     return NULL; | 
|---|
| 260 |   assert( parameterName != NULL); | 
|---|
| 261 |  | 
|---|
| 262 |   element = root->FirstChildElement( parameterName); | 
|---|
| 263 |   if( element == NULL) return NULL; | 
|---|
| 264 |  | 
|---|
| 265 |   node = element->FirstChild(); | 
|---|
| 266 |   while( node != NULL) | 
|---|
| 267 |   { | 
|---|
| 268 |     if( node->ToText()) return (TiXmlElement*)node; | 
|---|
| 269 |     node = node->NextSibling(); | 
|---|
| 270 |   } | 
|---|
| 271 |   return NULL; | 
|---|
| 272 | } | 
|---|
| 273 |  | 
|---|
| 274 |  | 
|---|
| 275 |  | 
|---|