Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9869 in orxonox.OLD for trunk/src/lib/util/loading


Ignore:
Timestamp:
Oct 3, 2006, 12:19:30 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

Location:
trunk/src/lib/util/loading
Files:
14 edited
5 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/util/loading/dynamic_loader.cc

    r9406 r9869  
    2424
    2525
    26 
     26ObjectListDefinition(DynamicLoader);
    2727
    2828/**
     
    3131*/
    3232DynamicLoader::DynamicLoader (const std::string& libName)
    33     : Factory(NULL, CL_NULL)
     33    : Factory(libName)
    3434{
    35   this->setClassID(CL_DYNAMIC_LOADER, "DynamicLoader");
     35  this->registerObject(this, DynamicLoader::_objectList);
    3636
    3737  this->handle = NULL;
  • trunk/src/lib/util/loading/dynamic_loader.h

    r7193 r9869  
    1919class DynamicLoader : public Factory
    2020{
     21  ObjectListDeclaration(DynamicLoader);
    2122
    2223public:
  • trunk/src/lib/util/loading/factory.cc

    r9675 r9869  
    1515#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
    1616
    17 #include "util/loading/factory.h"
     17#include "factory.h"
    1818#include "debug.h"
    1919//#include "shell_command.h"
    2020
    21 
     21ObjectListDefinition(Factory);
    2222
    2323//SHELL_COMMAND(create, Factory, fabricate);
     
    2828 * set everything to zero and define factoryName
    2929 */
    30 Factory::Factory (const std::string& factoryName, ClassID classID)
    31     : classID(classID), className(factoryName)
     30Factory::Factory (const ClassID& classID)
     31    : _classID(classID)
    3232{
    33   this->setClassID(CL_FACTORY, "Factory");
    34   this->setName(factoryName);
     33  PRINTF(4)("Factory::create(%s::%d)\n", classID.name().c_str(), classID.id());
     34  //this->registerObject(this, Factory::_objectList);
     35  this->setName(classID.name());
    3536
    36   if( Factory::factoryList == NULL)
    37     Factory::factoryList = new std::list<Factory*>;
    38 
    39   Factory::factoryList->push_back(this);
     37  Factory::_factoryIDMap[classID] = this;
     38  Factory::_factoryStringMap[classID.name()] = this;
    4039}
    4140
    42 /** @brief a reference to the First Factory */
    43 std::list<Factory*>* Factory::factoryList = NULL;
     41/** @brief A Map of all Factories ordered by ID. */
     42Factory::FactoryIDMap Factory::_factoryIDMap;
     43
     44/** @brief A Map of all Factories ordered by Name. */
     45Factory::FactoryStringMap Factory::_factoryStringMap;
    4446
    4547/**
     
    4850Factory::~Factory ()
    4951{
    50   //  printf("%s\n", this->factoryName);
    51   //  Factory* tmpDel = this->next;
    52   //  this->next = NULL;
    53 }
     52  FactoryIDMap::iterator it = Factory::_factoryIDMap.find(this->_classID);
     53  if (it != Factory::_factoryIDMap.end() && (*it).second == this)
     54    Factory::_factoryIDMap.erase(it);
    5455
    55 /**
    56  * @brief deletes all the Factories. (cleanup)
    57  */
    58 void Factory::deleteFactories()
    59 {
    60   if (Factory::factoryList != NULL)
    61   {
    62     while(!Factory::factoryList->empty())
    63     {
    64       delete Factory::factoryList->front();
    65       Factory::factoryList->pop_front();
    66     }
    67     delete Factory::factoryList;
    68     Factory::factoryList = NULL;
    69   }
     56  FactoryStringMap::iterator stringIt = Factory::_factoryStringMap.find(this->_classID.name());
     57  if (stringIt != Factory::_factoryStringMap.end() && (*stringIt).second == this)
     58    Factory::_factoryStringMap.erase(stringIt);
    7059}
    7160
     
    7463 * @returns true on match, false otherwise
    7564 */
    76 bool Factory::operator==(ClassID classID) const
     65bool Factory::operator==(int classID) const
    7766{
    78   return (this->classID == classID);
     67  return (this->_classID == classID);
    7968}
    8069
    81 /**
    82  * @brief Compares the Factories Name against a given ClassName
    83  * @param className the Name of the Class to Query
    84  * @returns true on match, false otherwise.
    85  */
    86 bool Factory::operator==(const char* className) const
    87 {
    88   return (className != NULL && this->className == className);
    89 }
    9070
    9171/**
     
    9676bool Factory::operator==(const std::string& className) const
    9777{
    98   return (this->className == className);
     78  return (this->_classID.name() == className);
    9979}
    10080
     
    10787BaseObject* Factory::fabricate(const TiXmlElement* root)
    10888{
    109   assert (root != NULL && Factory::factoryList != NULL);
    110 
    111   std::list<Factory*>::const_iterator factory;
    112   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    113     if (*(*factory) == root->Value())
    114     {
    115       PRINTF(2)("Create a new Object of type %s\n", (*factory)->getCName());
    116       return (*factory)->fabricateObject(root);
    117     }
    118 
    119   PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
    120   return NULL;
     89  FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(root->Value());
     90  if (it != Factory::_factoryStringMap.end())
     91  {
     92    PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName());
     93    return (*it).second->fabricateObject(root);
     94  }
     95  else
     96  {
     97    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", root->Value());
     98    return NULL;
     99  }
    121100}
    122101
     
    129108BaseObject* Factory::fabricate(const std::string& className)
    130109{
    131   if (Factory::factoryList == NULL)
     110  FactoryStringMap::const_iterator it = Factory::_factoryStringMap.find(className);
     111  if (it != Factory::_factoryStringMap.end())
     112  {
     113    PRINTF(2)("Create a new Object of type %s\n", (*it).second->getCName());
     114    return (*it).second->fabricateObject(NULL);
     115  }
     116  else
     117  {
     118    PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str());
    132119    return NULL;
    133 
    134   std::list<Factory*>::const_iterator factory;
    135   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    136     if (*(*factory) == className)
    137     {
    138       PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    139       return (*factory)->fabricateObject(NULL);
    140     }
    141   PRINTF(2)("Could not Fabricate an Object of Class '%s'\n", className.c_str());
    142   return NULL;
     120  }
    143121}
    144 
    145122
    146123/**
     
    149126 * @returns a new Object of Type classID on match, NULL otherwise
    150127 */
    151 BaseObject* Factory::fabricate(ClassID classID)
     128BaseObject* Factory::fabricate(const ClassID& classID)
    152129{
    153   if (Factory::factoryList == NULL)
     130  FactoryIDMap::const_iterator it = Factory::_factoryIDMap.find(classID);
     131  if (it != Factory::_factoryIDMap.end())
     132  {
     133    PRINTF(4)("Create a new Object of type %s\n", (*it).second->getCName());
     134    return (*it).second->fabricateObject(NULL);
     135  }
     136  else
     137  {
     138    PRINTF(2)("Could not Fabricate an Object of ClassID '%d'\n", classID.id());
    154139    return NULL;
     140  }
     141}
    155142
    156   std::list<Factory*>::const_iterator factory;
    157   for (factory = Factory::factoryList->begin(); factory != Factory::factoryList->end(); factory++)
    158     if (*(*factory) == classID)
    159     {
    160       PRINTF(4)("Create a new Object of type %s\n", (*factory)->getCName());
    161       return (*factory)->fabricateObject(NULL);
    162143
    163     }
    164   PRINTF(2)("Could not Fabricate an Object of ClassID '0x%h'\n", classID);
    165   return NULL;
     144/**
     145 * @brief print out some nice litte debug information about the Factory.
     146 */
     147void Factory::debug() const
     148{
     149  PRINTF(0)("Factory of class '%s' with ClassID: %d\n", this->_classID.name().c_str(), this->_classID.id());
    166150}
     151
     152/**
     153 * @brief Prints out some nice Debug information about all factories
     154 */
     155void Factory::debugAll()
     156{
     157  PRINTF(0)("Debugging all %d Factories\n", Factory::_factoryStringMap.size());
     158  Factory::FactoryStringMap::const_iterator it;
     159  for (it = Factory::_factoryStringMap.begin(); it != Factory::_factoryStringMap.end(); ++it)
     160    (*it).second->debug();
     161}
  • trunk/src/lib/util/loading/factory.h

    r8148 r9869  
    2020
    2121
    22 #ifndef _FACTORY_H
    23 #define _FACTORY_H
     22#ifndef __FACTORY_H
     23#define __FACTORY_H
    2424
    2525class BaseObject;
     
    2727#include "parser/tinyxml/tinyxml.h"
    2828#include "base_object.h"
    29 #include <vector>
    30 #include <list>
     29#include <map>
    3130
    3231/**
     
    3433 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file)
    3534 */
    36 #define CREATE_FACTORY(CLASS_NAME, CLASS_ID) \
    37     tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
     35#define CREATE_FACTORY(CLASS_NAME) \
     36    tFactory<CLASS_NAME> global_##CLASS_NAME##_Factory = tFactory<CLASS_NAME>(CLASS_NAME::staticClassID())
    3837
    3938//! The Factory is a loadable object handler
    40 class Factory : public BaseObject {
    41 
    42  public:
     39class Factory : public BaseObject
     40{
     41  //! Declare the ObjectList at the BaseObject List
     42  ObjectListDeclaration(Factory);
     43public:
    4344  virtual ~Factory ();
    4445
    45   static void deleteFactories();
     46  static  BaseObject* fabricate(const std::string& className);
     47  static  BaseObject* fabricate(const ClassID& classID);
     48  static  BaseObject* fabricate(const TiXmlElement* root);
    4649
    47   static  BaseObject* fabricate(const std::string& className);
    48   static  BaseObject* fabricate(ClassID classID);
    49   static  BaseObject* fabricate(const TiXmlElement* root = NULL);
     50  bool operator==(int classID) const;
     51  bool operator==(const std::string& className) const;
     52  /** @param classID the ID to compare @returns true if the ID's match */
     53  bool operator==(const ClassID& classID) const { return _classID == classID; };
    5054
    5155
    52   bool operator==(ClassID classID) const;
    53   bool operator==(const char* className) const;
    54   bool operator==(const std::string& className) const;
     56  void debug() const;
     57  static void debugAll();
    5558
    56   protected:
    57     Factory (const std::string& factoryName, ClassID classID);
    58     virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0;
     59protected:
     60  Factory (const ClassID& id);
     61  /**
     62   * @brief The core function of the Factory. Creates objects of the Factories Type.
     63   * @param root The TiXmlElement of the Factory.
     64   * @returns the created Object in BaseType* format.
     65   */
     66  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0;
    5967
    60   protected:
    61     const ClassID                 classID;              //!< The Class-Identifyer of the Factory.
    62     const std::string             className;            //!< The name of the Class.
    63     static std::list<Factory*>*   factoryList;          //!< List of Registered Factories
     68private:
     69  //! Copy Constructor is hidden.
     70  Factory (const Factory&) {};
     71
     72protected:
     73  /** The Type of the FactoryMap that is sorted by ID */
     74  typedef std::map<const ClassID, Factory*>  FactoryIDMap;
     75  /** The Type of the FactoryMap that is sorted by Name */
     76  typedef std::map<std::string, Factory*>    FactoryStringMap;
     77
     78  const ClassID                 _classID;              //!< The Class-Identifyer of the Factory.
     79  static FactoryIDMap           _factoryIDMap;         //!< List of Registered Factories
     80  static FactoryStringMap       _factoryStringMap;     //!< List of Registered Factories
    6481};
    6582
     
    7087template<class T> class tFactory : public Factory
    7188{
    72  public:
    73  /**
    74   * @brief creates a new type Factory to enable the loading of T
    75   * @param factoryName the Name of the Factory to load.
    76   * @param classID the ID of the Class to be created.
    77   */
    78   tFactory (const char* factoryName, ClassID classID)
    79    : Factory(factoryName, classID)
     89public:
     90  /**
     91   * @brief creates a new type Factory to enable the loading of T
     92   * @param classID the ID of the Class to be created.
     93   */
     94  tFactory (const ClassID& classID)
     95      : Factory(classID)
    8096  {  }
     97  /**
     98   * @brief copy constructor
     99   * @param factory the Factory to copy
     100   */
     101  tFactory (const tFactory& factory) : Factory(factory._classID) {};
    81102
    82   private:
    83    /**
    84     * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
    85     * @param root the TiXmlElement T should load parameters from.
    86     * @return the newly fabricated T.
    87     */
    88     virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const
    89     {
    90       return new T(root);
    91     }
     103  /**
     104   * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
     105   * @param root the TiXmlElement T should load parameters from.
     106   * @return the newly fabricated T.
     107   */
     108  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const
     109  {
     110    return new T(root);
     111  }
    92112};
    93113
  • trunk/src/lib/util/loading/game_loader.cc

    r9110 r9869  
    2020#include "game_loader.h"
    2121#include "util/loading/load_param.h"
    22 
    23 #include "shell_command.h"
     22#include "util/loading/resource_manager.h"
     23#include "debug.h"
    2424#include "campaign.h"
    2525
    26 #include "util/loading/resource_manager.h"
    27 
    2826#include "key_mapper.h"
    2927
    30 
    31 
    32 SHELL_COMMAND(quit, GameLoader, stop)
    33 ->describe("quits the game")
    34 ->setAlias("orxoquit");
    35 
     28ObjectListDefinition(GameLoader);
    3629
    3730GameLoader* GameLoader::singletonRef = NULL;
     
    4336GameLoader::GameLoader ()
    4437{
    45   this->setClassID(CL_GAME_LOADER, "GameLoader");
     38  this->registerObject(this, GameLoader::_objectList);
    4639  this->setName("GameLoader");
    4740  this->bRun = true;
     
    5750    delete this->currentCampaign;
    5851  this->currentCampaign = NULL;
    59  
     52
    6053  GameLoader::singletonRef = NULL;
    6154}
     
    9083{
    9184  ErrorMessage errorCode;
    92   std::string campaignName = ResourceManager::getFullName(fileName);
     85  std::string campaignName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
    9386  if (!campaignName.empty())
    9487  {
     
    110103{
    111104  ErrorMessage errorCode;
    112   std::string campaignName = ResourceManager::getFullName(fileName);
     105  std::string campaignName = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(fileName);
    113106  if (!campaignName.empty())
    114107  {
  • trunk/src/lib/util/loading/game_loader.h

    r7868 r9869  
    3838class GameLoader : public EventListener
    3939{
    40  public:
     40  ObjectListDeclaration(GameLoader);
     41  public:
    4142  virtual ~GameLoader ();
    4243  /**  this class is a singleton class @returns an instance of itself  */
  • trunk/src/lib/util/loading/load_param.cc

    r9406 r9869  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
    1717
    18 #include "util/loading/load_param.h"
    19 #include "load_param_description.h"
    20 
    21 #include <stdarg.h>
    22 
     18#include "load_param.h"
     19#include "load_param_class_description.h"
     20#include "compiler.h"
     21#include "debug.h"
    2322/**
    24  * Constructs a new LoadParameter
     23 * @brief Constructs a new LoadParameter
    2524 * @param root the XML-element to load this Parameter from
    2625 * @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.
     26 * @param inLoadCycle If we are in a LoadCycle (loading differs.).
    2927 */
    30 CLoadParam::CLoadParam(const TiXmlElement* root, const std::string& paramName, BaseObject* object, Executor* executor, bool inLoadCycle)
    31   :  object(object), paramName(paramName)
     28LoadParamBase::LoadParamBase(const TiXmlElement* root, const std::string& paramName, bool inLoadCycle)
     29    :  paramName(paramName), inLoadCycle(inLoadCycle)
    3230{
    33   this->inLoadCycle = inLoadCycle;
    34 
    3531  // determin the LoadString.
    3632  if (likely(!inLoadCycle))
     
    4036  else
    4137    this->loadElem = NULL;
     38}
    4239
    43   // set the Executor.
    44   this->executor = executor;
    4540
    46   //if (this->executor)
    47   //  this->executor->setName(paramName);
     41/**
     42 * @param classID the ID of the class. This is needed to identify into what class this Parameter belongs.
     43 * @param descriptionText The text to set as a description for this Parameter
     44 * @returns a pointer to itself.
     45 */
     46void LoadParamBase::describe(const ClassID& classID, const std::string& descriptionText)
     47{
     48  PRINTF(5)("Describing Class '%s'(id:%d) Parameter '%s': description '%s'\n",
     49            classID.name().c_str(), classID.id(), paramName.c_str(), descriptionText.c_str());
     50
     51  if (LoadParamClassDescription::descriptionsCaptured())
     52    LoadParamClassDescription::describeClass(classID, paramName, descriptionText);
    4853}
    4954
    5055/**
    51  * This is a VERY SPECIAL deconsrtuctor.
    52  * It is made, so that it loads the Parameters on destruction.
    53  * meaning, if an Executor a valid Object exist, and all
    54  * Execution-Conditions are met, they are executed here.
     56 * @brief sets the Values of the Description to a usefull text.
    5557 */
    56 CLoadParam::~CLoadParam()
     58void LoadParamBase::setDescriptionValues(const ClassID& classID, unsigned int paramCount, const MultiType* const defaultValues, bool retVal)
    5759{
    58   if (likely(this->executor != NULL))
    59   {
    60     std::string loadString = "";
    61     if (this->loadElem != NULL &&  this->loadElem->ToText())
    62       loadString = this->loadElem->Value();
    63     if (likely(this->object != NULL) &&
    64         ( !loadString.empty() ||
    65           ((this->executor->getType() & Executor_NoLoadString) == Executor_NoLoadString)))
    66     {
    67       PRINTF(4)("Loading value '%s' with Parameters '%s' onto: %s::%s\n", this->paramName.c_str(), loadString.c_str(), this->object->getClassCName(), this->object->getCName());
    68       (*this->executor)(this->object, SubString(loadString, ",", SubString::WhiteSpaces, false, '\\'));
    69     }
    70     delete this->executor;
    71   }
     60  if(LoadParamClassDescription::descriptionsCaptured())
     61    LoadParamClassDescription::setValuesOf(classID, paramName, paramCount, defaultValues, retVal);
    7262}
    73 
    74 /**
    75  * @brief set the default values of the executor
    76  * @param value0 the first default value
    77  * @param value1 the second default value
    78  * @param value2 the third default value
    79  * @param value3 the fourth default value
    80  * @param value4 the fifth default value
    81  */
    82 CLoadParam& CLoadParam::defaultValues(const MultiType& value0, const MultiType& value1,
    83                                       const MultiType& value2, const MultiType& value3,
    84                                       const MultiType& value4)
    85 {
    86   assert(this->executor != NULL);
    87   this->executor->defaultValues(value0, value1, value2, value3, value4);
    88 
    89   return *this;
    90 }
    91 
    92 
    93 
    94 /**
    95  * @param descriptionText The text to set as a description for this Parameter
    96  * @returns a pointer to itself.
    97 */
    98 CLoadParam& CLoadParam::describe(const std::string& descriptionText)
    99 {
    100   if (LoadClassDescription::parametersDescription && this->paramDesc && this->paramDesc->getDescription().empty())
    101   {
    102     this->paramDesc->setDescription(descriptionText);
    103   }
    104   return *this;
    105 }
    106 
    107 // const LoadParamDescription* LoadParamDescription::getClass(const char* className)
    108 // {
    109 //   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
    110 //   LoadClassDescription* enumClassDesc = iterator->firstElement();
    111 //   while (enumClassDesc)
    112 //   {
    113 //     if (!strcmp(enumClassDesc->className, classNameBegin, className))
    114 //     {
    115 //       delete iterator;
    116 //       return enumClassDesc;
    117 //     }
    118 //     enumClassDesc = iterator->nextElement();
    119 //   }
    120 //   delete iterator;
    121 //
    122 //   return NULL;
    123 // }
    124 
    125 
    126 
    127 
    128 /*
    129  * @param object The object this Parameter is loaded too.
    130  * @param root: the XML-element to load this option from.
    131  * @param paramName: The name of the parameter loaded.
    132  * @param paramCount: how many parameters this loading-function takes
    133  * @param multi: if false LoadParam assumes only one occurence of this parameter in root, if true it assumes multiple occurences.
    134  * @param ...: the parameter information (1. Parameter, 2. Default Value for the Parameter, ...)
    135 */
    136 /*LoadParam::LoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName,
    137                              int paramCount, bool multi, const void* pointerToParam, ...)
    138 {
    139   this->setClassID(CL_LOAD_PARAM, "LoadParam");
    140   this->executor = NULL;
    141 
    142   this->loadString = NULL;
    143   this->pointerToParam = pointerToParam;
    144 
    145   if (paramCount == 0 || this->pointerToParam != NULL)
    146     this->loadString = "none";
    147   else
    148 {
    149       if (likely(!multi))
    150         this->loadString = grabParameter(root, paramName);
    151       else
    152 {
    153           if (!strcmp(root->Value(), paramName))
    154 {
    155               const TiXmlNode* val = root->FirstChild();
    156               if( val->ToText())
    157                 this->loadString = val->Value();
    158 }
    159 }
    160 }
    161 
    162   this->paramDesc = NULL;
    163   if (LoadClassDescription::parametersDescription)
    164 {
    165     // locating the class
    166     this->classDesc = LoadClassDescription::addClass(object->getClassCName());
    167 
    168     if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL)
    169 {
    170 
    171       this->paramDesc->paramCount = paramCount;
    172       this->paramDesc->types = new int[paramCount];
    173       this->paramDesc->defaultValues = new char*[paramCount];
    174 
    175       va_list types;
    176       va_start (types, pointerToParam);
    177       char defaultVal[512];
    178       for(int i = 0; i < paramCount; i++)
    179 {
    180         defaultVal[0] = '\0';
    181           // parameters parsed
    182         int tmpType = va_arg (types, int);
    183         this->paramDesc->types[i] = tmpType;
    184         switch (tmpType)
    185 {
    186   case MT_INT:
    187             sprintf(defaultVal, "%d", va_arg(types, int));
    188             break;
    189 //          case MT_LONG:
    190 //            sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE));
    191 //            break;
    192   case MT_FLOAT:
    193             sprintf(defaultVal, "%0.3f", va_arg(types, double));
    194             break;
    195   case MT_STRING:
    196             sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE));
    197             break;
    198   case MT_EXT1:
    199             sprintf(defaultVal, "");
    200             break;
    201 }
    202         this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1];
    203         strcpy(this->paramDesc->defaultValues[i], defaultVal);
    204 }
    205       va_end(types);
    206 
    207       int argCount = 0;
    208 }
    209 }
    210 }*/
    211 
    212 
    213 
    214 
    215 
    216 
    217 
    218 
    21963
    22064
     
    22771 * @returns the Value of the parameter if found, NULL otherwise
    22872*/
    229 std::string grabParameter(const TiXmlElement* root, const std::string& parameterName)
     73std::string LoadParamBase::grabParameter(const TiXmlElement* root, const std::string& parameterName)
    23074{
    231   const TiXmlElement* element;
    232   const TiXmlNode* node;
    233 
    234   if (root == NULL)
    235     return "";
    236 
    237   element = root->FirstChildElement( parameterName);
    238   if( element == NULL) return "";
    239 
    240   node = element->FirstChild();
    241   while( node != NULL)
     75  const TiXmlElement* const element = grabParameterElement(root, parameterName);
     76  if (element != NULL)
     77    return element->Value();
     78  else
    24279  {
    243     if( node->ToText()) return node->Value();
    244     node = node->NextSibling();
     80    static std::string empty("");
     81    return empty;
    24582  }
    246   return "";
    24783}
    24884
     
    25288 * @returns the Element of the parameter if found, NULL otherwise
    25389 */
    254 const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName)
     90const TiXmlElement* LoadParamBase::grabParameterElement(const TiXmlElement* root, const std::string& parameterName)
    25591{
    25692  const TiXmlElement* element;
     
    271107  return NULL;
    272108}
    273 
    274 
    275 
  • trunk/src/lib/util/loading/load_param.h

    r8048 r9869  
    2222#define _LOAD_PARAM_H
    2323
    24 #include "base_object.h"
     24#include "util/executor/executor_substring.h"
     25#include "util/executor/executor_member.h"
     26#include "util/executor/functor_member.h"
    2527
    26 #include "executor/executor.h"
    27 #include "executor/executor_xml.h"
     28#include "parser/tinyxml/tinyxml.h"
    2829
    2930// Forward Declaration //
    3031class LoadClassDescription;
    3132class LoadParamDescription;
    32 class MultiType;
    33 
     33class TiXmlElement;
    3434
    3535/**
    3636 * Loads a Parameter from ROOT named PARAMETER_NAME
    37  * onto OBJECT of CLASS, trough the FUNCTION
     37 * onto OBJECT of CLASS, trough FUNCTION
    3838 * @param ROOT the TiXmlElement to load the Parameter from
    3939 * @param PARAMETER_NAME the Name of the Parameter to load
     
    4343 */
    4444#define LoadParam(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \
    45          CLoadParam(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS>(&CLASS::FUNCTION), false)
     45         CLoadParam<CLASS>(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS, CLASS>(&CLASS::FUNCTION), false)
    4646
     47/**
     48 * @brief Does essentially the same as LoadParam, but within a Cycle in an ordered fashion.
     49 *
     50 * This Function looks in each Element, if the PARAMETER_NAME matches, and loads onto OBJECT
     51 * of CLASS the ROOT through FUNCTION
     52 *
     53 * @see LoadParam(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION)
     54 */
    4755#define LoadParam_CYCLE(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \
    48          CLoadParam(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS>(&CLASS::FUNCTION), true)
    49 
    50 #define LoadParamXML(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \
    51          CLoadParam(ROOT, PARAMETER_NAME, OBJECT, new ExecutorXML<CLASS>(&CLASS::FUNCTION, ROOT, PARAMETER_NAME), false)
    52 
    53 #define LoadParamXML_CYCLE(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \
    54          CLoadParam(ROOT, PARAMETER_NAME, OBJECT, new ExecutorXML<CLASS>(&CLASS::FUNCTION, ROOT, PARAMETER_NAME), true)
    55 
     56         CLoadParam<CLASS>(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS, CLASS>(&CLASS::FUNCTION), true)
    5657
    5758/**
     
    7879}
    7980
     81
    8082/**************************
    8183**** REAL DECLARATIONS ****
    8284**************************/
    83 //! abstract Base class for a Loadable parameter
    84 class CLoadParam : public BaseObject
     85//!< A BaseClass for all LoadParam's.
     86class LoadParamBase
    8587{
    86   public:
    87     CLoadParam(const TiXmlElement* root, const std::string& paramName, BaseObject* object, Executor* executor, bool inLoadCycle = false);
    88     virtual ~CLoadParam();
     88protected:
     89  LoadParamBase(const TiXmlElement* root, const std::string& paramName, bool inLoadCycle = false);
    8990
    90     CLoadParam& describe(const std::string& descriptionText);
    91     CLoadParam& defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL,
    92                               const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL,
    93                               const MultiType& value4 = MT_NULL);
    94     CLoadParam& attribute(const std::string& attributeName, const Executor& executor);
     91protected:
     92  void describe(const ClassID& classID, const std::string& descriptionText);
     93  void setDescriptionValues(const ClassID& classID, unsigned int paramCount, const MultiType* const defaultValues, bool retVal = false);
     94
     95public:
     96  static std::string grabParameter(const TiXmlElement* root, const std::string& parameterName);
     97  static const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName);
     98
     99protected:
     100  const std::string        paramName;            //!< The Name of the Parameter this LoadParams applies to.
     101  bool                     inLoadCycle;          //!< If the Parameter is in a LoadCycle.
     102
     103  const TiXmlElement*      loadElem;             //!< The Element to load.
     104};
    95105
    96106
    97   private:
    98     bool                     inLoadCycle;
    99     Executor*                executor;
    100     BaseObject*              object;
    101     const std::string        paramName;
     107//! The Loading Class of the LoadParam, that acctually executes the loading process.
     108template <class OperateClass> class CLoadParam : public LoadParamBase
     109{
     110public:
     111  /**
     112   * @brief generates a LoadParam based on:
     113   * @param root the Root Element to load onto the object. @param paramName the Parameter name that is loaded.
     114   * @param object the Object to apply the changes on. @param executor the Functional Object, that actually executes the function Call.
     115   * @param inLoadCycle If we are inside of a loading cycle. (Loading will be different here)
     116   */
     117  CLoadParam(const TiXmlElement* root, const std::string& paramName, OperateClass* object, Executor<const SubString, OperateClass>* executor, bool inLoadCycle = false)
     118      : LoadParamBase(root, paramName, inLoadCycle)
     119  {
     120    assert (executor != NULL);
     121    this->object = object;
     122    this->executor = executor;
     123  }
     124  virtual ~CLoadParam()
     125  {
     126    std::string loadString;
     127    if (this->loadElem != NULL &&  this->loadElem->ToText())
     128    {
     129      loadString = this->loadElem->Value();
     130      if (!loadString.empty())
     131      {
     132        /*      PRINTF(4)("Loading value '%s' with Parameters '%s' onto: %s::%s\n",
     133                this->paramName.c_str(), loadString.c_str(), this->object->getClassCName(), this->object->getCName());*/
     134        (*this->executor)(this->object, SubString(loadString, ",", SubString::WhiteSpaces, false, '\\'));
     135      }
     136    }
     137    this->setDescriptionValues(OperateClass::staticClassID(), executor->getParamCount(), executor->getDefaultValues(), executor->hasRetVal());
     138    delete this->executor;
     139  }
     140  /**
     141   * @brief set the default values of the executor
     142   * @param value0 the first default value   @param value1 the second default value
     143   * @param value2 the third default value   @param value3 the fourth default value
     144   * @param value4 the fifth default value
     145   */
     146  CLoadParam& defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL,
     147                            const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL,
     148                            const MultiType& value4 = MT_NULL)
     149  { this->executor->defaultValues(value0, value1, value2, value3, value4); return *this;  };
     150  //! Describes a LoadParam
     151  CLoadParam& describe(const std::string& descriptionText) { LoadParamBase::describe(OperateClass::staticClassID(), descriptionText); return *this; };
     152  //     CLoadParam& attribute(const std::string& attributeName, const Executor<SubString>& executor);
    102153
    103     LoadClassDescription*    classDesc;            //!< The LoadClassDescription of this CLoadParameter
    104     LoadParamDescription*    paramDesc;            //!< The LoadParameterDescription of this LoadParameter
    105     const TiXmlElement*      loadElem;             //!< The Element to load.
    106     const void*              pointerToParam;       //!< A Pointer to a Parameter.
    107 
    108     MultiType*               defaultValue;
     154private:
     155  Executor<const SubString, OperateClass>*         executor;            //!< The Executor, that actually executes the Loading process.
     156  OperateClass*                                    object;              //!< The Object this LoadParam operates on.
    109157};
    110158
    111 // helper function
    112 
    113 std::string grabParameter(const TiXmlElement* root, const std::string& parameterName);
    114 const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName);
    115 
    116159#endif /* _LOAD_PARAM_H */
  • trunk/src/lib/util/loading/load_param_description.cc

    r8362 r9869  
    1717
    1818#include "multi_type.h"
    19 #include <stdarg.h>
    2019#include "debug.h"
    2120
    2221/**
     22 * @brief Creates a Description of a LoadParam
    2323 * @param paramName the name of the parameter to load
    2424 */
    2525LoadParamDescription::LoadParamDescription(const std::string& paramName)
    26 {
    27   this->types = NULL;
    28   this->defaultValues = NULL;
    29   this->paramName = paramName;
    30 }
    31 
    32 /**
    33  *  removes all the alocated memory
    34  */
    35 LoadParamDescription::~LoadParamDescription()
    36 {
    37   if (this->defaultValues != NULL)
    38   {
    39     for(int i = 0; i < this->paramCount; i++)
    40     {
    41       delete[] this->defaultValues[i];
    42     }
    43   }
    44 
    45   delete[] this->types;
    46   delete[] this->defaultValues;
    47 }
     26    : _name(paramName), _parameterCount(0)
     27{ }
    4828
    4929/**
     
    5232void LoadParamDescription::setDescription(const std::string& descriptionText)
    5333{
    54   this->description = descriptionText;
     34  this->_description = descriptionText;
    5535}
    5636
    5737/**
    58  *  prints out this parameter, its input method and the description (if availiable)
     38 * @brief sets the Values of the LoadParam in the Description.
     39 * @param paramCount the count of arguments the underlying paramDescription takes.
     40 * @param defaultValues the default Values the underlying parameter takes.
     41 * @param retVal if the underlying parameter has a return value
    5942 */
    60 void LoadParamDescription::print() const
     43void LoadParamDescription::setValues(unsigned int paramCount,
     44                                     const MultiType* const defaultValues,
     45                                     bool retVal)
    6146{
    62   PRINT(3)(" <%s>", this->paramName.c_str());
    63   for (int i = 0; i < this->paramCount; i++)
     47  this->_parameterCount = paramCount;
     48  for (unsigned int i = 0; i < paramCount; ++i)
    6449  {
    65     if (i > 0)
    66       PRINT(3)(",");
    67     // FIXME
    68     //     switch (this->types[i])
    69 //     {
    70 //       default:
    71 //         PRINTF(3)("none");
    72 //         break;
    73 //       case ParameterBool:
    74 //         PRINT(3)("bool");
    75 //         break;
    76 //       case ParameterChar:
    77 //         PRINT(3)("char");
    78 //         break;
    79 //       case ParameterString:
    80 //         PRINT(3)("string");
    81 //         break;
    82 //       case ParameterInt:
    83 //         PRINT(3)("int");
    84 //         break;
    85 //       case ParameterUInt:
    86 //         PRINT(3)("Uint");
    87 //         break;
    88 //       case ParameterFloat:
    89 //         PRINT(3)("float");
    90 //         break;
    91 //       case ParameterLong:
    92 //         PRINT(3)("long");
    93 //         break;
    94 //       case ParameterXML:
    95 //         PRINT(3)("XML");
    96 //         break;
    97 //     }
     50    this->_defaultValues.push_back(defaultValues[i].getString());
     51    this->_types.push_back(MultiType::MultiTypeToString(defaultValues[i].getType()));
    9852  }
    99   PRINT(3)("</%s>", this->paramName.c_str());
    100   if (!this->description.empty())
    101     PRINT(3)(" -- %s", this->description.c_str());
    102   // default values
    103   if (this->paramCount > 0)
    104   {
    105     PRINT(3)(" (Default: ");
    106     for (int i = 0; i < this->paramCount; i++)
    107     {
    108       if (i > 0)
    109         PRINT(3)(", ");
    110       if (this->types[i] & MT_STRING)
    111       { // leave brackets !!
    112         PRINT(3)("\"%s\"", this->defaultValues[i]);
    113       }
    114       else
    115       {
    116         PRINT(3)("%s", this->defaultValues[i]);
    117       }
    118     }
    119     PRINT(3)(")");
    120   }
    121   PRINT(3)("\n");
    122 }
    12353
    124 /**
    125  *  A list, that holds all the classes that are loadable (classes not objects!!)
    126  */
    127 std::list<LoadClassDescription*>* LoadClassDescription::classList = NULL;
    128 
    129 /**
    130  *  if the description of Parameters should be executed
    131  */
    132 bool LoadClassDescription::parametersDescription = false;
    133 
    134 /**
    135  * @param className the name of the class to be loadable
    136  */
    137 LoadClassDescription::LoadClassDescription(const std::string& className)
    138 {
    139   this->className = className;
    140 
    141   if (LoadClassDescription::classList == NULL)
    142     LoadClassDescription::classList = new std::list<LoadClassDescription*>;
    143 
    144   LoadClassDescription::classList->push_back(this);
    145 }
    146 
    147 /**
    148  *  deletes a classDescription (deletes all the parameterDescriptions as well
    149  */
    150 LoadClassDescription::~LoadClassDescription()
    151 {
    152   std::list<LoadParamDescription*>::iterator it = this->paramList.begin();
    153   while (!this->paramList.empty())
    154   {
    155     delete this->paramList.front();
    156     this->paramList.pop_front();
    157   }
    158 }
    159 
    160 void LoadClassDescription::deleteAllDescriptions()
    161 {
    162   if (LoadClassDescription::classList != NULL)
    163   {
    164     while (!LoadClassDescription::classList->empty())
    165     {
    166       delete LoadClassDescription::classList->front();
    167       LoadClassDescription::classList->pop_front();
    168     }
    169     delete LoadClassDescription::classList;
    170   }
    171   LoadClassDescription::classList = NULL;
    17254}
    17355
    17456
    17557/**
    176  *  adds a class to the list of loadable classes
    177  * @param className The name of the class to add
    178 
    179    this function searches for the className string, and if found just returns the appropriate Class.
    180    Otherwise it returns a new classDescription
     58 * @brief prints out this parameter, its input method and the description (if availiable)
     59 * @param stream the stream to print to.
     60 * @param withComments if the comments should be appended.
    18161 */
    182 LoadClassDescription* LoadClassDescription::addClass(const std::string& className)
     62void LoadParamDescription::print(FILE* stream, bool withComments) const
    18363{
    184   if (LoadClassDescription::classList != NULL)
     64  fprintf(stream, " <%s>", this->_name.c_str());
     65  for (unsigned int i = 0; i < this->_parameterCount; i++)
    18566  {
    186     std::list<LoadClassDescription*>::iterator it = LoadClassDescription::classList->begin();
    187     while (it != LoadClassDescription::classList->end())
     67    if (i > 0)
     68      fprintf(stream, ",");
     69    fprintf(stream, "%s", this->_types[i].c_str());
     70  }
     71  fprintf(stream, "</%s>", this->_name.c_str());
     72  // here the comments are printed out.
     73  if (withComments)
     74  {
     75    if (!this->_description.empty())
     76      fprintf(stream, " <!-- %s", this->_description.c_str());
     77    // default values
     78    if (this->_parameterCount > 0)
    18879    {
    189       if ((*it)->className == className)
     80      fprintf(stream, " (Default: ");
     81      for (unsigned int i = 0; i < this->_parameterCount; i++)
    19082      {
    191         return (*it);
     83        if (i > 0)
     84          fprintf(stream, ", ");
     85        if (this->_types[i] == "string")
     86        { // leave brackets !!
     87          fprintf(stream, "\"%s\"", this->_defaultValues[i].c_str());
     88        }
     89        else
     90        {
     91          fprintf(stream, "%s", this->_defaultValues[i].c_str());
     92        }
    19293      }
    193       it++;
     94      fprintf(stream, ")");
    19495    }
     96    if (!this->_description.empty() || this->_parameterCount > 0)
     97      fprintf(stream, " -->");
    19598  }
    196   return new LoadClassDescription(className);
     99  fprintf(stream, "\n");
    197100}
    198101
    199 /**
    200  *  does the same as addClass(const std::string& className), but with params
    201  * @param paramName the name of the parameter to add.
    202  */
    203 LoadParamDescription* LoadClassDescription::addParam(const std::string& paramName)
    204 {
    205   std::list<LoadParamDescription*>::iterator it = this->paramList.begin();
    206   while (it != this->paramList.end())
    207   {
    208     if ((*it)->paramName == paramName)
    209     {
    210       return NULL;
    211     }
    212     it++;
    213   }
    214102
    215   LoadParamDescription* newParam = new LoadParamDescription(paramName);
    216 
    217   this->paramList.push_back(newParam);
    218   return newParam;
    219 }
    220 
    221 /**
    222  *  prints out all loadable Classes, and their parameters
    223  * @param fileName prints the output to a File
    224  * @todo implement it
    225  */
    226 void LoadClassDescription::printAll(const std::string& fileName)
    227 {
    228   PRINT(3)("===============================================================\n");
    229   PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n");
    230   if (LoadClassDescription::classList != NULL)
    231   {
    232     std::list<LoadClassDescription*>::iterator classDesc = LoadClassDescription::classList->begin();
    233     while (classDesc != LoadClassDescription::classList->end())
    234     {
    235       PRINT(3)("<%s>\n", (*classDesc)->className.c_str());
    236       std::list<LoadParamDescription*>::iterator param = (*classDesc)->paramList.begin();
    237       while (param != (*classDesc)->paramList.end())
    238       {
    239         (*param)->print();
    240         param++;
    241       }
    242       PRINT(3)("</%s>\n\n", (*classDesc)->className.c_str());
    243       classDesc++;
    244     }
    245   }
    246   else
    247     PRINT(3)("no Classes defined so far\n");
    248   PRINT(3)("===============================================================\n");
    249 }
    250 
    251 /**
    252  * searches for classes, which beginn with classNameBegin
    253  * @param classNameBegin the beginning string of a Class
    254  * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
    255  * !! The strings MUST NOT be deleted !!
    256  */
    257 std::list<std::string> LoadClassDescription::searchClassWithShort(const std::string& classNameBegin)
    258 {
    259   /// FIXME
    260   // NOT USED
    261 /*  unsigned int searchLength = strlen(classNameBegin);
    262   std::list<const std::string&> retVal;
    263 
    264   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
    265   LoadClassDescription* enumClassDesc = iterator->firstElement();
    266   while (enumClassDesc)
    267   {
    268     if (strlen(enumClassDesc->className)>searchLength+1 &&
    269         !strncasecmp(enumClassDesc->className, classNameBegin, searchLength))
    270     {
    271       retVal->add(enumClassDesc->className);
    272     }
    273     enumClassDesc = iterator->nextElement();
    274   }
    275   delete iterator;
    276 
    277   return retVal;*/
    278   std::list<std::string> a;
    279   return a;
    280 }
  • trunk/src/lib/util/loading/load_param_description.h

    r7221 r9869  
    1515
    1616/*!
    17  * @file load_param.h
     17 * @file load_param_description.h
    1818 * A Class and macro-functions, that makes our lives easy to load-in parameters
    1919 */
     
    2222#define _LOAD_PARAM_DESCRIPTION_H
    2323
    24 #include "base_object.h"
    25 #include <list>
     24#include <vector>
     25#include <string>
    2626
    2727// Forward Declaration //
     
    3434class LoadParamDescription
    3535{
    36   friend class LoadParam;
    37   friend class LoadClassDescription;
    3836public:
    39   LoadParamDescription(const std::string& paramName);
    40   ~LoadParamDescription();
     37  LoadParamDescription(const std::string& paramName = "");
     38
     39  //! Compares a LoadParamDescription with a String.
     40  bool operator==(const std::string& paramName) const { return this->_name == paramName; };
     41  //! Compares two LoadParamDescription
     42  bool operator==(const LoadParamDescription& paramDescr) const { return this->_name == paramDescr._name; };
     43  //! Compares two LoadParamDescription with the less operator
     44  bool operator<(const LoadParamDescription& paramDescr) const { return this->_name < paramDescr._name; };
    4145
    4246  void setDescription(const std::string& descriptionText);
     47  void setValues(unsigned int paramCount,
     48                 const MultiType* const defaultValues,
     49                 bool retVal = false);
     50
    4351  /** @returns the descriptionString */
    44   const std::string& getDescription() { return this->description; };
     52  const std::string& description() { return this->_description; };
    4553
    46   void print() const;
     54  void print(FILE* stream = stdout, bool withComments = true) const;
    4755
    4856private:
    49   std::string   paramName;             //!< The name of the parameter.
    50   int           paramCount;            //!< The count of parameters.
    51   int*          types;                 //!< What kind of parameters does this function take ??
    52   std::string   description;           //!< A longer description about this function.
    53   char**        defaultValues;         //!< The 'Default Values'. @TODO MAKE THIS A MULTITYPE
    54 };
     57  std::string                _name;                  //!< The Name of the Parameter.
     58  unsigned int               _parameterCount;        //!< The Count of parameters.
     59  std::string                _description;           //!< A longer description about this function.
    5560
    56 //! A class for descriptions of a loadable module
    57 class LoadClassDescription
    58 {
    59   friend class CLoadParam;
    60 public:
    61   LoadClassDescription(const std::string& className);
    62   ~LoadClassDescription();
    63 
    64   static LoadClassDescription* addClass(const std::string& className);
    65   LoadParamDescription* addParam(const std::string& paramName);
    66 
    67   static void deleteAllDescriptions();
    68 
    69   static void printAll(const std::string& fileName = "");
    70   static std::list<std::string> searchClassWithShort(const std::string& classNameBegin);
    71   //  static const LoadParamDescription* getClass(const std::string& className);
    72 
    73 private:
    74   static bool                              parametersDescription;  //!< if parameter-description should be enabled.
    75   static std::list<LoadClassDescription*>* classList;              //!< a list, that stores all the loadable classes. (after one instance has been loaded)
    76   std::string                              className;              //!< name of the class
    77 
    78   std::list<LoadParamDescription*>         paramList;              //!< List of parameters this class knows.
     61  std::vector<std::string>   _types;                 //!< A Vector of types of this Parameter.
     62  std::vector<std::string>   _defaultValues;         //!< A Vector of defaultValues of this Parameter.
    7963};
    8064
  • trunk/src/lib/util/loading/resource.cc

    r9406 r9869  
    1414*/
    1515
    16 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOADING
     16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
    1717
    1818#include "resource.h"
    19 
    20 
    21 
    22 
    23 /**
    24  * standard constructor
    25 */
    26 Resource::Resource (const std::string& fileName)
     19#include "resource_manager.h"
     20
     21#include "debug.h"
     22
     23
     24namespace Resources
    2725{
    28 //   this->setClassID(CL_RESOURCE, "Resource");
    29 
     26  //! Define an ObjectList for the Resources
     27  ObjectListDefinition(Resource);
     28
     29
     30  /**
     31   * @brief standard constructor
     32   * @param type the Type this resource belongs to.
     33   */
     34  Resource::Resource (Type* type)
     35      : _pointer(NULL), _type(type)
     36  {
     37    this->registerObject(this, Resource::_objectList);
     38  }
     39
     40  /**
     41   * @brief standard deconstructor
     42   */
     43  Resource::~Resource ()
     44  {
     45    // delete what has to be deleted here
     46  }
     47
     48  /**
     49   * @brief Locates a File inside of the Resources Paths and returns the appended path.
     50   *
     51   * @param fileName the Name of the file to look for.
     52   * @returns the Name of the File prepended with the PAth it is in, if found, empty String ("") otherwise.
     53   *
     54   * This Function searches in (ordered):
     55   * 1. mainGlobalPath (from ResourceManger)
     56   * 2. all of the global Paths (from ResourceManger)
     57   * 3. all of the Resources Paths (from Resources::Type)
     58   *
     59   * in each of these directory, first "./" is searched, and afterwards all of the subDirs (from Resources::Type) are searched.
     60   *
     61   * @todo finish it!!
     62   */
     63  std::string Resource::locateFile(const std::string& fileName) const
     64  {
     65    if ((ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).exists() )
     66      return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
     67
     68    std::string locatedFile;
     69    locatedFile = locateFileInSubDir(ResourceManager::getInstance()->mainGlobalPath(), fileName);
     70    if (!locatedFile.empty())
     71    {
     72      return locatedFile;
     73    }
     74
     75    if (File(fileName).exists())
     76      return fileName;
     77
     78    return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
     79  }
     80
     81  /**
     82   * @brief tests in all the SubDirectories defined in Resource under Directory if the fileName exists.
     83   * @param directory the directory to in what to search for all subdirectories for.
     84   * @param fileName the Name of the File to query for
     85   * @return true on success.
     86   */
     87  std::string Resource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const
     88  {
     89    std::vector<Directory>::const_iterator it;
     90    for (it = this->_type->resourceSubPaths().begin(); it != this->_type->resourceSubPaths().end(); ++it)
     91    {
     92      Directory dir = directory + (*it);
     93      File file = dir + File(fileName);
     94      if ((dir+ File(fileName)).exists())
     95        return (dir+File(fileName)).name();
     96    }
     97    return "";
     98  }
     99
     100
     101  /**
     102   * @param loadString the Identifier of the Resource.
     103   * @returns a Store-Pointer to the Resource if found, NULL otherwise
     104   */
     105  StorePointer* Resource::acquireResource(const std::string& loadString)
     106  {
     107    //const Type* const type = _resourceTypes[this->_type->id()];
     108
     109    for (unsigned int i = 0; i < _type->storedResources().size(); ++i)
     110    {
     111      if (_type->storedResources()[i]->loadString() == loadString)
     112        return _type->storedResources()[i];
     113    }
     114
     115    return NULL;
     116  }
     117
     118  /**
     119   * @brief registers a StorePointer to a Resource's Type.
     120   * @param pointer the StorePointer to register.
     121   */
     122  void Resource::addResource(StorePointer* pointer)
     123  {
     124    assert(pointer != NULL);
     125    this->_type->addResource(pointer);
     126  }
     127
     128
     129
     130
     131
     132
     133  ///////////////////
     134  //// KEEPLEVEL ////
     135  ///////////////////
     136
     137  //! Constructs a default KeepLevel as Set in the ResourceManager via setDefaultKeepLevel.
     138  KeepLevel::KeepLevel()
     139  {
     140    this->_keepLevel = ResourceManager::getInstance()->defaultKeepLevel().keepLevel();
     141  }
     142  /**
     143   * @param keepLevel the level to set.
     144   */
     145  KeepLevel::KeepLevel(unsigned int keepLevel)
     146  {
     147    _keepLevel = keepLevel;
     148  }
     149
     150  /**
     151   * @brief constructor of a KeepLevel.
     152   * @param keepLevelName the Name of the KeepLevel. Must be one Name of the defined Names in the ResourceManager.
     153   *
     154   * @note the Name is transformed into an Integer for fast interpretation.
     155   */
     156  KeepLevel::KeepLevel(const std::string& keepLevelName)
     157  {
     158    this->_keepLevel = ResourceManager::getInstance()->getKeepLevelID(keepLevelName);
     159  }
     160
     161  /**
     162   * @returns the name of the KeepLevel.
     163   */
     164  const std::string& KeepLevel::name() const
     165  {
     166    return ResourceManager::getInstance()->getKeepLevelName(this->_keepLevel);
     167  }
     168
     169
     170
     171  ///////////////////////
     172  //// STORE POINTER ////
     173  ///////////////////////
     174  /**
     175   * @brief allocates a StorePointer.
     176   * @param loadString An identifier String that is unique between all resources of this type.
     177   * @param keepLevel the KeepLevel at wich to keep this resource.
     178   */
     179  StorePointer::StorePointer(const std::string& loadString, const KeepLevel& keeplevel)
     180      : _loadString(loadString), _keepLevel(keeplevel)
     181  {
     182    PRINTF(4)("Acquired a Resource with LoadString '%s' and KeepLevel '%s'\n", _loadString.c_str(), _keepLevel.name().c_str());
     183  }
     184
     185  StorePointer::~StorePointer()
     186  {
     187    PRINTF(4)("Deleting Stored Resource '%s' from KeepLevel '%s'\n", _loadString.c_str(), _keepLevel.name().c_str());
     188  };
     189
     190
     191
     192  //////////////
     193  //// TYPE ////
     194  //////////////
     195  /**
     196   * @brief allocates a Type.
     197   * @param typeName the Name of the Type to be stored in this Container.
     198   */
     199  Type::Type(const std::string& typeName)
     200      : _typeName(typeName)
     201  {
     202    ResourceManager::getInstance()->registerType(this);
     203    PRINTF(4)("Created ResourceType '%s'\n", typeName.c_str());
     204  }
     205
     206  //! Destructs a Type.
     207  Type::~Type()
     208  {
     209    ResourceManager::getInstance()->unregisterType(this);
     210  }
     211
     212  /**
     213   * @brief adds a Resource to this Resource's type.
     214   * @param resource the Resource to add.
     215   */
     216  void Type::addResource(StorePointer* resource)
     217  {
     218    this->_storedResources.push_back(resource);
     219  }
     220
     221  /**
     222   * @brief adds a Path to the Type's resource-paths.
     223   * @param path the path-name to add.
     224   */
     225  bool Type::addResourcePath(const std::string& path)
     226  {
     227    std::vector<Directory>::const_iterator it;
     228    for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it)
     229      if ((*it) == path)
     230        return false;
     231    this->_resourcePaths.push_back(path);
     232    return true;
     233
     234  }
     235
     236  /**
     237   * @brief Adds a SubPath to the Type's resource-subpaths.
     238   * @param subPath the subpath to add.
     239   */
     240  bool Type::addResourceSubPath(const std::string& subPath)
     241  {
     242    std::vector<Directory>::const_iterator it;
     243    for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it)
     244      if ((*it) == subPath)
     245        return false;
     246    this->_resourceSubPaths.push_back(subPath);
     247    return true;
     248  }
     249
     250  /**
     251   * @brief Unloads all Resources below a certain Level.
     252   * @param keepLevel the KeepLevel at what to remove the Resources from.
     253   */
     254  void Type::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
     255  {
     256    std::vector<Resources::StorePointer*>::iterator it, it2;
     257    bool finished = false;
     258
     259    while (!finished)
     260    {
     261      finished = true;
     262      for (it = this->_storedResources.begin(); it != this->_storedResources.end();++it)
     263        if((*it)->keepLevel() < keepLevel && (*it)->last())
     264        {
     265          delete (*it);
     266          this->_storedResources.erase(it);
     267          finished = false;
     268          break;
     269        }
     270    }
     271  }
     272
     273  /**
     274   * @brief print out some nice Debug information in a beatifully designed style
     275   */
     276  void Type::debug() const
     277  {
     278    PRINT(0)(" ResourceType '%s' stores %d Resources\n", this->_typeName.c_str(), this->_storedResources.size());
     279    PRINT(0)("  Paths:\n");
     280    for (unsigned int i = 0; i < this->_resourcePaths.size(); ++i)
     281      PRINT(0)("    %s\n", this->_resourcePaths[i].name().c_str());
     282    PRINT(0)("  Sub-Paths:");
     283    for (unsigned int i = 0; i < this->_resourceSubPaths.size(); ++i)
     284      PRINT(0)(" '%s'", this->_resourceSubPaths[i].name().c_str());
     285    PRINT(0)("\n");
     286
     287    PRINT(0)("  Loaded Resources:\n");
     288    std::vector<Resources::StorePointer*>::const_iterator it;
     289    for (it = this->_storedResources.begin(); it != this->_storedResources.end(); ++it)
     290      PRINT(0)("    '%s' : KeepLevel '%s'\n", (*it)->loadString().c_str(), (*it)->keepLevel().name().c_str());
     291  }
    30292}
    31 
    32 /**
    33  * standard deconstructor
    34  */
    35 Resource::~Resource ()
    36 {
    37   // delete what has to be deleted here
    38 }
  • trunk/src/lib/util/loading/resource.h

    r7195 r9869  
    88
    99#include "base_object.h"
    10 #include "multi_type.h"
    1110#include <string>
     11#include <vector>
     12#include <set>
    1213
    13 // FORWARD DECLARATION
     14#include "filesys/directory.h"
     15
     16//! A Namespace Resources and ResourceHandling is defined in.
     17namespace Resources
     18{
     19  //! The KeepLevel handles the unloading of Resources.
     20  /**
     21   * Allocating a Resource also appends a KeepLevel to the Resource.
     22   * When the Resource is not used anymore it is decided on the grounds of the KeepLevel,
     23   * if the Resource should be deleted. (e.g. at the end of a Level, Campaign, or something like this).
     24   */
     25  class KeepLevel
     26  {
     27  public:
     28    KeepLevel();
     29    KeepLevel(unsigned int keepLevel);
     30    KeepLevel(const std::string& keepLevelName);
     31
     32    //! Compare equality
     33    inline bool operator==(const KeepLevel& keepLevel) const { return this->_keepLevel == keepLevel._keepLevel; };
     34    //! Compares inequality
     35    inline bool operator!=(const KeepLevel& keepLevel) const { return this->_keepLevel != keepLevel._keepLevel; };
     36    //! Compares less/equal than
     37    inline bool operator<=(const KeepLevel& keepLevel) const { return this->_keepLevel <= keepLevel._keepLevel; };
     38    //! Compares less than
     39    inline bool operator<(const KeepLevel& keepLevel) const { return this->_keepLevel < keepLevel._keepLevel; };
     40
     41    /** @returns the KeepLevel as a number */
     42    inline unsigned int keepLevel() const { return _keepLevel; };
     43    const std::string& name() const;
     44  private:
     45    unsigned int                _keepLevel;              //!< The KeepLevel a Resource is in.
     46  };
     47
     48
     49  ///////////////////
     50  // STORE POINTER //
     51  ///////////////////
     52  //! Stores a Resource-Pointer, the LoadString and it's keepLevel.
     53  class StorePointer
     54  {
     55  public:
     56    //! Virtual Destructor, that removes the Stored information-pointer.
     57    virtual ~StorePointer();
     58
     59    /** @returns the LoadString this resource was loaded with */
     60    const std::string& loadString() const { return _loadString; };
     61    /** @returns the KeepLevel of this resource */
     62    const Resources::KeepLevel& keepLevel() const { return _keepLevel; };
     63
     64    virtual bool last() const = 0;
     65
     66    protected:
     67      StorePointer(const std::string& loadString, const Resources::KeepLevel& keeplevel);
     68
     69    private:
     70      StorePointer(const StorePointer&) : _keepLevel(0) {};
     71
     72  private:
     73    std::string                 _loadString;             //!< An identifier, to match when loading a File.
     74    Resources::KeepLevel        _keepLevel;              //!< The Priority of this resource. (can only be increased, so none else will delete this)
     75  };
    1476
    1577
    1678
    17 //! An enumerator for different (UN)LOAD-types.
    18 /**
    19  * RP_NO:        will be unloaded on request
    20  * RP_LEVEL:     will be unloaded at the end of a Level
    21  * RP_CAMPAIGN:  will be unloaded at the end of a Campaign
    22  * RP_GAME:      will be unloaded at the end of the whole Game (when closing orxonox)
    23  */
    24 typedef enum ResourcePriority
    25 {
    26   RP_NO        =   0,
    27   RP_LEVEL     =   1,
    28   RP_CAMPAIGN  =   2,
    29   RP_GAME      =   3
    30 };
     79  ///////////////////
     80  // RESOURCE TYPE //
     81  ///////////////////
     82  //! A Type of Resources.
     83  /**
     84   * The Type is used to store the Pointers to already loaded Resources,
     85   * and also to store type-specific properties.
     86   * These are the Loading Paths, the subpaths and so on.
     87   */
     88  class Type
     89  {
     90  public:
     91    virtual ~Type();
     92    /** @returns true if the names match @param typeName the Name to compare. @brief compare the Type with a Name */
     93    bool operator==(const std::string& typeName) const { return this->_typeName == typeName; };
     94
     95    ////////////////////
     96    //// EXTENSIONS ////
     97    void addExtension(const std::string& extension);
     98
     99    ///////////////
     100    //// PATHS ////
     101    bool addResourcePath(const std::string& path);
     102    bool addResourceSubPath(const std::string& subPath);
     103
     104    /// Retrieve Functions
     105    /** @returns the name of the stored Class this Type loads Resources for */
     106    const std::string& storedClassName() const { return _typeName; };
     107    /** @returns the ID of the Type != ClassID */
     108    /** @returns the type-specific paths this Resource searches in. */
     109    const std::vector<Directory>& resourcePaths() const { return _resourcePaths; };
     110    /** @returns the Type specific SubPaths this Resource Searches in @see std::vector<std::string>  _resourceSubPaths */
     111    const std::vector<Directory>& resourceSubPaths() const { return _resourceSubPaths; };
     112    /** @returns the Pointers to the Stored resources. @note do not use this, for more than some lookup */
     113    const std::vector<Resources::StorePointer*>& storedResources() const { return _storedResources; };
     114
     115    ///////////////////////////////
     116    //// LOADING AND UNLOADING ////
     117    virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) = 0;
     118    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
     119
     120    ///////////////////
     121    //// INTERNALS ////
     122    void addResource(Resources::StorePointer* resource);
     123
     124    ///////////////
     125    //// DEBUG ////
     126    void debug() const;
     127
     128  protected:
     129    Type(const std::string& typeName);
     130
     131  private:
     132    Type(const Type& type) {};
     133  private:
     134    const std::string                     _typeName;          //!< Name of the Type. (Name of the Resource this loads.)
     135    std::vector<Directory>                _resourcePaths;     //!< The Paths to search for files in this type
     136    std::vector<Directory>                _resourceSubPaths;  //!< The subpaths that will be searched under all the _resourcePaths.
     137    std::vector<std::string>              _fileExtensions;    //!< File Extensions, this Resource supports.
     138
     139    std::vector<Resources::StorePointer*> _storedResources;   //!< An array of all the stored Resources.
     140  };
     141
     142  /**
     143   * @brief A Type Definition Class for any Object that is resourceable.
     144   *
     145   * This Class's main reason of Existence is, that resources can be dynamically
     146   * created over a loadString. For this the Type of Resource is required, and the Resource must
     147   * itself support the 'void createFromString(const std::string&)' function.
     148   */
     149  template<class T> class tType : public Type
     150  {
     151  public:
     152    /** Create the ResourceType @see Type(const std::string&) */
     153    tType(const std::string& typeName) : Type(typeName) {};
     154    /** @param loadString the String to load a Resource with @brief tries to create a Resource of Type T with a loadString */
     155    virtual void createFromString(const std::string& loadString, const KeepLevel& keepLevel = KeepLevel()) { T::createFromString(loadString, keepLevel); }
     156  };
    31157
    32158
    33159
    34 //! A Resource is an Object, that can be loaded from Disk
    35 /**
    36  *
    37  */
    38 class Resource : virtual public BaseObject {
     160  /////////////////////
     161  // RESOURCE ITSELF //
     162  /////////////////////
     163  //! A Resource is an Object, that can be loaded from Disk
     164  /**
     165   * The Resource Hanldes the location and stores pointers to data that can be retrieved.
     166   */
     167  class Resource : virtual public BaseObject
     168  {
     169    ObjectListDeclaration(Resource);
    39170
    40  public:
    41    Resource(const std::string& fileName);
    42   virtual ~Resource();
     171  public:
     172    Resource(Resources::Type* type);
     173    virtual ~Resource();
    43174
    44   virtual bool load(std::string& fileName, const MultiType& param1, const MultiType& param2);
    45   virtual bool reload();
    46   virtual bool unload();
     175    /** @brief reloads the underlying resource */
     176    virtual bool reload() { return false; };
     177    /** @brief unloads the underlying Resource */
     178    virtual bool unload() { return false; };
    47179
    48  private:
    49    std::string       fileName;
     180    std::string locateFile(const std::string& fileName) const;
    50181
    51    unsigned int      referenceCount;    //!< How many times this Resource has been loaded.
    52 /// TODO REMOVE THIS:   ResourceType      type;              //!< ResourceType of this Resource.
    53    ResourcePriority  prio;              //!< The Priority of this resource. (can only be increased, so noone else will delete this)
     182  protected:
     183    Resources::StorePointer* acquireResource(const std::string& loadString);
     184    void addResource(Resources::StorePointer* pointer);
    54185
    55    MultiType         param[3];          //!< The Parameters given to this Resource.
    56 };
     186  private:
     187    std::string locateFileInSubDir(const Directory& directory, const std::string& fileName) const;
     188
     189  private:
     190    Resources::StorePointer*       _pointer;                         //!< Virtual Pointer to the ResourceData.
     191    Resources::Type*               _type;                            //!< Type of the Resource.
     192  };
     193}
    57194
    58195#endif /* _RESOURCE_H */
  • trunk/src/lib/util/loading/resource_manager.cc

    r9406 r9869  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
    1717
    18 #include "util/loading/resource_manager.h"
    19 #include "substring.h"
     18#include "resource_manager.h"
    2019#include "debug.h"
    2120
    2221#include <algorithm>
    23 #include <assert.h>
    24 
    25 // different resource Types
    26 #ifndef NO_MODEL
    27 #include "objModel.h"
    28 #include "primitive_model.h"
    29 #include "md2/md2Model.h"
    30 #include "md3/md3_data.h"
    31 #include "md3/md3_animation_cfg.h"
    32 #endif /* NO_MODEL */
    33 #ifndef NO_TEXTURES
    34 #include "texture.h"
    35 #endif /* NO_TEXTURES */
    36 #ifndef NO_TEXT
    37 #include "font.h"
    38 #endif /* NO_TEXT */
    39 #ifndef NO_AUDIO
    40 #include "sound_buffer.h"
    41 #include "ogg_player.h"
    42 #endif /* NO_AUDIO */
    43 #ifndef NO_SHADERS
    44 #include "shader.h"
    45 #endif /* NO_SHADERS */
    46 
    47 // File Handling Includes
    48 #include <sys/types.h>
    49 #include <sys/stat.h>
    50 #include <unistd.h>
    51 
    52 
    53 
    54 /**
    55  * @brief standard constructor
    56 */
    57 ResourceManager::ResourceManager ()
     22#include <cassert>
     23
     24
     25namespace Resources
    5826{
    59   this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager");
    60   this->setName("ResourceManager");
    61 
    62   this->dataDir = "./";
    63   this->tryDataDir("./data");
     27  /// Definition of the ResourceManager's ObjectList.
     28  ObjectListDefinition(ResourceManager);
     29  //! Singleton Reference to the ResourceManager
     30  ResourceManager* ResourceManager::_singletonRef = NULL;
     31
     32
     33  /**
     34   * @brief standard constructor
     35  */
     36  ResourceManager::ResourceManager ()
     37  : _defaultKeepLevel(0)
     38  {
     39    this->registerObject(this, ResourceManager::_objectList);
     40    this->setName("ResourceManager");
     41    this->_mainGlobalPath = Directory("./");
     42  }
     43
     44
     45  /**
     46   * @brief standard destructor
     47  */
     48  ResourceManager::~ResourceManager ()
     49  {
     50    this->unloadAllBelowKeepLevel(this->_keepLevelNames.size());
     51    ResourceManager::_singletonRef = NULL;
     52  }
     53
     54  /**
     55   * @brief Registers a new Type to the ResourceManager.
     56   * @param type the Type to register.
     57   */
     58  void ResourceManager::registerType(Resources::Type* type)
     59  {
     60    this->_resourceTypes.push_back(type);
     61    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
     62  }
     63
     64  /**
     65   * @brief Unregisters a new Type to the ResourceManager.
     66   * @param type the Type to unregister.
     67   */
     68  void ResourceManager::unregisterType(Resources::Type* type)
     69  {
     70    std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type);
     71    if (it != this->_resourceTypes.end())
     72    {
     73      this->_resourceTypes.erase(it);
     74      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
     75    }
     76  }
     77
     78  /**
     79   * @brief Sets the main Global path (the main path Resources are searched for)
     80   * @param directory the directory to set.
     81   * @see Resource::locateFile
     82   */
     83  void ResourceManager::setMainGlobalPath(const Directory& directory)
     84  {
     85    this->_mainGlobalPath = directory;
     86    this->_mainGlobalPath.open();
     87  }
     88
     89  /**
     90   * @brief add a Global search path. (global paths besided the main path.)
     91   * @param directory a directory to add.
     92   */
     93  void ResourceManager::addGlobalPath(const Directory& directory)
     94  {
     95    std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory);
     96    if (it == this->_globalPaths.end())
     97      this->_globalPaths.push_back(directory);
     98  }
     99
     100  /**
     101   * @brief add a ResourcePath to a Type's Paths.
     102   * @param resourceName the Type's name of Resource to add the path to.
     103   * @param pathName pathName the Name of the path to add.
     104   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
     105   */
     106  bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName)
     107  {
     108    std::vector<Resources::Type*>::iterator it;
     109    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
     110      if (*(*it) == resourceName)
     111        return (*it)->addResourcePath(pathName);
     112    PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
     113    return false;
     114  }
     115
     116  /**
     117   * @brief add a ResourcePath to a Type's SubPaths.
     118   * @param resourceName the Type's name of Resource to add the subpath to.
     119   * @param pathName pathName the Name of the path to add.
     120   * @return true on success. (if a path was added (no duplicate, and resourceName existed).
     121   */
     122  bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
     123  {
     124    std::vector<Resources::Type*>::iterator it;
     125    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
     126      if (*(*it) == resourceName)
     127        return (*it)->addResourceSubPath(pathName);
     128    PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
     129    return false;
     130  }
     131
     132  /**
     133   * @brief checks wether a File is inside of the MainPath.
     134   * @param fileInside the file to check
     135   * @return true if the file is inside.
     136   */
     137  bool ResourceManager::checkFileInMainPath(const File& fileInside)
     138  {
     139    return (this->_mainGlobalPath + fileInside).exists();
     140  }
     141
     142  /**
     143   * @brief prepends the fileName by the MainGlobalPath (same as mainGlobalPath + '/' + fileName).
     144   * @param fileName The FileName to prepend
     145   * @returns the prepended file-name
     146   */
     147  std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName)
     148  {
     149    return (this->_mainGlobalPath + File(fileName)).name();
     150  }
     151
     152  /**
     153   * @brief add a KeepLevelName (this function just counts upwards).
     154   * @param keepLevelName the Name of the KeepLevel to set.
     155   * @returns the Level the Name was set to.
     156   */
     157  unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName)
     158  {
     159    this->_keepLevelNames.push_back(keepLevelName);
     160    return _keepLevelNames.size()-1;
     161  }
     162
     163  /**
     164   * @param keepLevelName the Name of the KeepLevel.
     165   * @returns the ID of the KeepLevel named keepLevelName
     166   */
     167  unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const
     168  {
     169    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
     170      if (this->_keepLevelNames[i] == keepLevelName)
     171        return i;
     172
     173    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
     174    return 0;
     175  }
     176
     177  /**
     178   * @param keepLevelID the ID to check.
     179   * @return the name of the KeepLevel.
     180   */
     181  const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const
     182  {
     183    assert(keepLevelID < this->_keepLevelNames.size());
     184    return this->_keepLevelNames[keepLevelID];
     185  }
     186
     187
     188  /**
     189   * @brief loads a Resource from a TypeName and a loadString.
     190   * @param resourceTypeName The Name of the Type to what to load a Resource from.
     191   * @param loadString the loadString to load in the Type.
     192   */
     193  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString, const KeepLevel& keepLevel)
     194  {
     195    std::vector<Resources::Type*>::const_iterator it;
     196    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
     197    {
     198      if (*(*it) == resourceTypeName)
     199      {
     200        (*it)->createFromString(loadString, keepLevel);
     201        /// TODO check if the resource was allocated!!
     202        return ;
     203      }
     204    }
     205    return ;
     206  }
     207
     208  /**
     209   * @brief unloads all Resources below a certain threshhold.
     210   * @param keepLevel the KeepLevel below which to erase.
     211   *
     212   * @not Resources will only be erased, if th keepLevel is below, and the resources are not
     213   * referenced anymore.
     214   */
     215  void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
     216  {
     217    std::vector<Resources::Type*>::const_iterator it;
     218    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
     219    {
     220      (*it)->unloadAllBelowKeepLevel(keepLevel);
     221    }
     222  }
     223
     224
     225  /**
     226   * @brief outputs debug information about the ResourceManager
     227   */
     228  void ResourceManager::debug() const
     229  {
     230    PRINT(0)("/==RM================================\\\n");
     231    PRINT(0)("| RESOURCE-MANAGER DEBUG INFORMATION |\n");
     232    PRINT(0)("\\====================================/\n");
     233    PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str());
     234    if(!this->_globalPaths.empty())
     235    {
     236      PRINT(0)(" Additional Global search Paths are: ");
     237      for (unsigned int i = 0; i < this->_globalPaths.size(); ++i)
     238        PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str());
     239      PRINT(0)("\n");
     240    }
     241    PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size());
     242    std::vector<Resources::Type*>::const_iterator it;
     243    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
     244    {
     245      (*it)->debug();
     246      if (it != --this->_resourceTypes.end())
     247        PRINT(0)(" ------------------------------------\n ");
     248    }
     249
     250    PRINT(0)("KeepLevels are: ");
     251    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
     252      PRINT(0)("%d:'%s'  ", i, this->_keepLevelNames[i].c_str());
     253    PRINT(0)("\n");
     254    PRINT(0)("=================================RM==/\n");
     255  }
    64256}
    65 
    66 //! Singleton Reference to the ResourceManager
    67 ResourceManager* ResourceManager::singletonRef = NULL;
    68 
    69 /**
    70  * @brief standard destructor
    71 */
    72 ResourceManager::~ResourceManager ()
    73 {
    74   // deleting the Resources-List
    75   this->unloadAllByPriority(RP_GAME);
    76 
    77   if (!this->resourceList.empty())
    78     PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
    79 
    80   ResourceManager::singletonRef = NULL;
    81 }
    82 
    83 /**
    84  * @brief sets the data main directory
    85  * @param dataDir the DataDirectory.
    86  */
    87 bool ResourceManager::setDataDir(const std::string& dataDir)
    88 {
    89   File dataDirectory(dataDir);
    90   if (dataDirectory.isDirectory())
    91   {
    92     this->dataDir = dataDirectory.name();
    93 
    94     if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
    95     {
    96       this->dataDir += '/';
    97     }
    98     return true;
    99   }
    100   else
    101   {
    102     PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir.c_str(), this->dataDir.c_str());
    103     return false;
    104   }
    105 }
    106 
    107 /**
    108  * @brief sets the data main directory
    109  * @param dataDir the DataDirectory.
    110  *
    111  * this is essentially the same as setDataDir, but it ommits the error-message
    112  */
    113 bool ResourceManager::tryDataDir(const std::string& dataDir)
    114 {
    115   File dataDirectory(dataDir);
    116   if (dataDirectory.isDirectory())
    117   {
    118     this->dataDir = dataDirectory.name();
    119 
    120     if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
    121     {
    122       this->dataDir += '/';
    123     }
    124     return true;
    125   }
    126   return false;
    127 }
    128 
    129 
    130 /**
    131  * @brief checks for the DataDirectory, by looking if
    132  * @param fileInside is iniside of the given directory.
    133 */
    134 bool ResourceManager::verifyDataDir(const std::string& fileInside)
    135 {
    136   File dataDirectory(this->dataDir);
    137   if (!dataDirectory.isDirectory())
    138   {
    139     PRINTF(1)("'%s' is not a directory\n", this->dataDir.c_str());
    140     return false;
    141   }
    142 
    143   File testFile(this->dataDir + fileInside);
    144   return testFile.isFile();
    145 }
    146 
    147 #ifndef NO_TEXTURES
    148 /**
    149  * @brief adds a new Path for Images
    150  * @param imageDir The path to insert
    151  * @returns true, if the Path was well and injected (or already existent within the list)
    152    false otherwise
    153 */
    154 bool ResourceManager::addImageDir(const std::string& imageDir)
    155 {
    156   std::string newDir = imageDir;
    157   if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\')
    158   {
    159     newDir += '/';
    160   }
    161   // check if the param is a Directory
    162   if (File(newDir).isDirectory())
    163   {
    164     // check if the Directory has been added before
    165     std::vector<std::string>::const_iterator imageDir;
    166     for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
    167     {
    168       if (*imageDir == newDir)
    169       {
    170         PRINTF(3)("Path %s already loaded\n", newDir.c_str());
    171         return true;
    172       }
    173     }
    174     // adding the directory to the List
    175     this->imageDirs.push_back(newDir);
    176     return true;
    177   }
    178   else
    179   {
    180     PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir.c_str());
    181     return false;
    182   }
    183 }
    184 #endif /* NO_TEXTURES */
    185 
    186 /**
    187  * @brief loads resources
    188  * @param fileName: The fileName of the resource to load
    189  * @param prio: The ResourcePriority of this resource (will only be increased)
    190  * @param param0: an additional option to parse (see the constuctors for more help)
    191  * @param param1: an additional option to parse (see the constuctors for more help)
    192  * @param param2: an additional option to parse (see the constuctors for more help)
    193  * @returns a pointer to a desired Resource.
    194 */
    195 BaseObject* ResourceManager::load(const std::string& fileName, ResourcePriority prio,
    196                                   const MultiType& param0, const MultiType& param1, const MultiType& param2)
    197 {
    198   ResourceType tmpType;
    199 #ifndef NO_MODEL
    200 #define __IF_OK
    201   if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".obj", 4))
    202     tmpType = OBJ;
    203   else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".md2", 4))
    204     tmpType = MD2;
    205   else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".md3", 4))
    206     tmpType = MD3;
    207   else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".cfg", 4))
    208     tmpType = MD3_CONFIG;
    209   else if (!strcasecmp(fileName.c_str(), "cube") ||
    210             !strcasecmp(fileName.c_str(), "sphere") ||
    211             !strcasecmp(fileName.c_str(), "plane") ||
    212             !strcasecmp(fileName.c_str(), "cylinder") ||
    213             !strcasecmp(fileName.c_str(), "cone"))
    214     tmpType = PRIM;
    215 #endif /* NO_MODEL */
    216 #ifndef NO_AUDIO
    217 #ifdef __IF_OK
    218   else
    219 #endif
    220 #define __IF_OK
    221     if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".wav", 4))
    222       tmpType = WAV;
    223     else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".mp3", 4))
    224       tmpType = MP3;
    225     else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ogg", 4))
    226       tmpType = OGG;
    227 #endif /* NO_AUDIO */
    228 #ifndef NO_TEXT
    229 #ifdef __IF_OK
    230     else
    231 #endif
    232 #define __IF_OK
    233       if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ttf", 4))
    234         tmpType = TTF;
    235 #endif /* NO_TEXT */
    236 #ifndef NO_SHADERS
    237 #ifdef __IF_OK
    238       else
    239 #endif
    240 #define __IF_OK
    241         if (!strncasecmp(fileName.c_str()+(fileName.size()-5), ".vert", 5))
    242           tmpType = SHADER;
    243 #endif /* NO_SHADERS */
    244 #ifndef NO_TEXTURES
    245 #ifdef __IF_OK
    246         else
    247 #else
    248   if
    249 #endif
    250           tmpType = IMAGE;
    251 #endif /* NO_TEXTURES */
    252 #undef __IF_OK
    253   return this->load(fileName, tmpType, prio, param0, param1, param2);
    254 }
    255 
    256 /**
    257  * @brief caches a Resource
    258  *
    259  * @see load;
    260  *
    261  * @brief returns true if ok, false otherwise.
    262  * This function loads a Resource without applying it to an Object.
    263  * This is for loading purposes, e.g, when the user is loading a Resource
    264  * during the initialisation instead of at Runtime.
    265  */
    266 bool ResourceManager::cache(const std::string& fileName, ResourceType type, ResourcePriority prio,
    267                             const MultiType& param0, const MultiType& param1, const MultiType& param2)
    268 {
    269   // searching if the resource was loaded before.
    270   Resource* tmpResource;
    271   // check if we already loaded this Resource
    272   tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
    273   // otherwise load it
    274   if (tmpResource == NULL)
    275     tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
    276   // return cached pointer.
    277   if (tmpResource != NULL) // if the resource was loaded before.
    278   {
    279     if(tmpResource->prio < prio)
    280       tmpResource->prio = prio;
    281     return true;
    282   }
    283   else
    284     return false;
    285 }
    286 
    287 /**
    288  * tells the ResourceManager to generate a Copy of the Resource.
    289  * @brief resourcePointer: The Pointer to the resource to copy
    290  * @returns the Resource pointed to resourcePointer.
    291  */
    292 BaseObject* ResourceManager::copy(BaseObject* resourcePointer)
    293 {
    294   Resource* tmp = locateResourceByPointer(resourcePointer);
    295   if (tmp!=NULL)
    296   {
    297     tmp->count++;
    298     return tmp->pointer;
    299   }
    300   else
    301     return NULL;
    302 }
    303 
    304 
    305 /**
    306  * @brief loads resources
    307  * @param fileName: The fileName of the resource to load
    308  * @param type: The Type of Resource to load.
    309  * @param prio: The ResourcePriority of this resource (will only be increased)
    310  * @param param0: an additional option to parse (see the constuctors for more help)
    311  * @param param1: an additional option to parse (see the constuctors for more help)
    312  * @param param2: an additional option to parse (see the constuctors for more help)
    313  * @returns a pointer to a desired Resource.
    314 */
    315 BaseObject* ResourceManager::load(const std::string& fileName, ResourceType type, ResourcePriority prio,
    316                                   const MultiType& param0, const MultiType& param1, const MultiType& param2)
    317 {
    318 
    319   // searching if the resource was loaded before.
    320   Resource* tmpResource;
    321   // check if we already loaded this Resource
    322   tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
    323   // otherwise load it
    324   if (tmpResource == NULL)
    325   {
    326     tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
    327   }
    328   // return cached pointer.
    329   if (tmpResource != NULL) // if the resource was loaded before.
    330   {
    331     tmpResource->count++;
    332     if(tmpResource->prio < prio)
    333       tmpResource->prio = prio;
    334 
    335     return tmpResource->pointer;
    336   }
    337   else
    338     return NULL;
    339 }
    340 
    341 
    342 /**
    343  * @brief loads resources for internal purposes
    344  * @param fileName: The fileName of the resource to load
    345  * @param type: The Type of Resource to load.
    346  * @param prio: The ResourcePriority of this resource (will only be increased)
    347  * @param param0: an additional option to parse (see the constuctors for more help)
    348  * @param param1: an additional option to parse (see the constuctors for more help)
    349  * @param param2: an additional option to parse (see the constuctors for more help)
    350  * @returns a pointer to a desired Resource.
    351  */
    352 Resource* ResourceManager::loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio,
    353                                         const MultiType& param0, const MultiType& param1, const MultiType& param2)
    354 {
    355   // Setting up the new Resource
    356   Resource* tmpResource = new Resource;
    357   tmpResource->count = 0;
    358   tmpResource->type = type;
    359   tmpResource->prio = prio;
    360   tmpResource->pointer = NULL;
    361   tmpResource->name = fileName;
    362 
    363   // creating the full name. (directoryName + FileName)
    364   std::string fullName = ResourceManager::getFullName(fileName);
    365   // Checking for the type of resource \see ResourceType
    366   switch(type)
    367   {
    368 #ifndef NO_MODEL
    369     case OBJ:
    370       if (param0.getType() != MT_NULL)
    371         tmpResource->param[0] = param0;
    372       else
    373         tmpResource->param[0] = 1.0f;
    374 
    375       if(File(fullName).isFile())
    376         tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
    377       else
    378       {
    379         PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName.c_str(), dataDir.c_str());
    380         tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());
    381       }
    382       break;
    383     case PRIM:
    384       if (param0 != MT_NULL)
    385         tmpResource->param[0] = param0;
    386       else
    387         tmpResource->param[0] = 1.0f;
    388 
    389       if (tmpResource->name == "cube")
    390         tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());
    391       else if (tmpResource->name == "sphere")
    392         tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());
    393       else if (tmpResource->name == "plane")
    394         tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());
    395       else if (tmpResource->name == "cylinder")
    396         tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());
    397       else if (tmpResource->name == "cone")
    398         tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());
    399       break;
    400     case MD2:
    401       if(File(fullName).isFile())
    402       {
    403         tmpResource->param[0] = param0;
    404         tmpResource->param[1] = param1;
    405         tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getCString(), tmpResource->param[1].getFloat());
    406       }
    407       break;
    408     case MD3:
    409       if(File(fullName).isFile())
    410       {
    411         tmpResource->param[0] = param0;
    412         tmpResource->param[1] = param1;
    413         tmpResource->pointer = new md3::MD3Data(fullName, tmpResource->param[0].getCString(), tmpResource->param[1].getFloat());
    414       }
    415       break;
    416     case MD3_CONFIG:
    417       if(File(fullName).isFile())
    418       {
    419         tmpResource->param[0] = param0;
    420         tmpResource->param[1] = param1;
    421         tmpResource->pointer = new md3::MD3AnimationCfg(fullName);
    422       }
    423       break;
    424 #endif /* NO_MODEL */
    425 #ifndef NO_TEXT
    426     case TTF:
    427       if (param0 != MT_NULL)
    428       {
    429         assert(param0.getInt() >= 0);
    430         tmpResource->param[0] = param0;
    431       }
    432       else
    433         tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
    434 
    435       if(File(fullName).isFile())
    436         tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
    437       else
    438         PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName.c_str(), this->dataDir.c_str());
    439       break;
    440 #endif /* NO_TEXT */
    441 #ifndef NO_AUDIO
    442     case WAV:
    443       if(File(fullName).isFile())
    444         tmpResource->pointer = new OrxSound::SoundBuffer(fullName);
    445       break;
    446     case OGG:
    447       if (File(fullName).isFile())
    448         tmpResource->pointer = new OrxSound::OggPlayer(fullName);
    449       break;
    450 #endif /* NO_AUDIO */
    451 #ifndef NO_TEXTURES
    452     case IMAGE:
    453       if (param0 != MT_NULL)
    454         tmpResource->param[0] = param0;
    455       else
    456         tmpResource->param[0] = GL_TEXTURE_2D;
    457       if(File(fullName).isFile())
    458       {
    459         PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), fullName.c_str());
    460         tmpResource->pointer = new Texture(fullName, tmpResource->param[0].getInt());
    461       }
    462       else
    463       {
    464         std::vector<std::string>::iterator imageDir;
    465         for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
    466         {
    467           std::string imgName = *imageDir + fileName;
    468           if(File(imgName).isFile())
    469           {
    470             PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), imgName.c_str());
    471             tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt());
    472             break;
    473           }
    474         }
    475       }
    476       if(!tmpResource)
    477         PRINTF(2)("!!Image %s not Found!!\n", fileName.c_str());
    478       break;
    479 #endif /* NO_TEXTURES */
    480 #ifndef NO_SHADERS
    481     case SHADER:
    482       if(File(fullName).isFile())
    483       {
    484         if (param0 != MT_NULL)
    485         {
    486           MultiType param = param0; /// HACK
    487           std::string secFullName = ResourceManager::getFullName(param.getCString());
    488           if (File(secFullName).isFile())
    489           {
    490             tmpResource->param[0] = secFullName;
    491             tmpResource->pointer = new Shader(fullName, secFullName);
    492           }
    493         }
    494         else
    495         {
    496           tmpResource->param[0] = param0;
    497           tmpResource->pointer = new Shader(fullName, "");
    498         }
    499       }
    500       break;
    501 #endif /* NO_SHADERS */
    502     default:
    503       tmpResource->pointer = NULL;
    504       PRINTF(1)("No type found for %s.\n   !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name.c_str());
    505       break;
    506   }
    507   if (tmpResource->pointer != NULL)
    508     this->resourceList.push_back(tmpResource);
    509 
    510   if (tmpResource->pointer != NULL)
    511     return tmpResource;
    512   else
    513   {
    514     PRINTF(2)("Resource %s could not be loaded\n", fileName.c_str());
    515     delete tmpResource;
    516     return NULL;
    517   }
    518 }
    519 
    520 /**
    521  * @brief unloads a Resource
    522  * @param pointer: The pointer to free
    523  * @param prio: the PriorityLevel to unload this resource
    524  * @returns true if successful (pointer found, and deleted), false otherwise
    525 */
    526 bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio)
    527 {
    528   if (pointer == NULL)
    529     return false;
    530   // if pointer is existent. and only one resource of this type exists.
    531   Resource* tmpResource = this->locateResourceByPointer(pointer);
    532   if (tmpResource != NULL)
    533     return unload(tmpResource, prio);
    534   else
    535   {
    536     PRINTF(2)("Resource not Found %p\n", pointer);
    537     return false;
    538   }
    539 }
    540 
    541 /**
    542  * @brief unloads a Resource
    543  * @param resource: The resource to unloade
    544  * @param prio the PriorityLevel to unload this resource
    545  * @returns true on success, false otherwise.
    546 */
    547 bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
    548 {
    549   if (resource == NULL)
    550     return false;
    551   if (resource->count > 0)
    552     resource->count--;
    553 
    554   if (resource->prio <= prio)
    555   {
    556     if (resource->count == 0)
    557     {
    558       delete resource->pointer;
    559       // deleting the List Entry:
    560       PRINTF(4)("Resource %s safely removed.\n", resource->name.c_str());
    561       std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
    562       this->resourceList.erase(resourceIT);
    563       delete resource;
    564     }
    565     else
    566       PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name.c_str(), resource->count);
    567   }
    568   else
    569     PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name.c_str());
    570   return true;
    571 }
    572 
    573 
    574 /**
    575  * @brief unloads all alocated Memory of Resources with a pririty lower than prio
    576  * @param prio The priority to delete
    577 */
    578 bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
    579 {
    580   bool removedAll = true;
    581   unsigned int removeCount;
    582   for (unsigned int round = 0; round < 3; round++)
    583   {
    584     int index = this->resourceList.size() - 1;
    585     removeCount = 0;
    586     while (index >= 0)
    587     {
    588       if (this->resourceList[index]->prio <= prio)
    589       {
    590         if (this->resourceList[index]->count == 0)
    591           unload(this->resourceList[index], prio);
    592         else
    593         {
    594           if (round == 3)
    595           {
    596             PRINTF(2)("unable to unload %s because there are still %d references to it\n",
    597                       this->resourceList[index]->name.c_str(), this->resourceList[index]->count);
    598             removedAll = false;
    599           }
    600           removeCount++;
    601         }
    602       }
    603       index--;
    604     }
    605     if (removeCount == 0) break;
    606   }
    607   return removedAll;
    608 }
    609 
    610 
    611 /**
    612  * @brief Searches for a Resource by some information
    613  * @param fileName: The name to look for
    614  * @param type the Type of resource to locate.
    615  * @param param0: an additional option to parse (see the constuctors for more help)
    616  * @param param1: an additional option to parse (see the constuctors for more help)
    617  * @param param2: an additional option to parse (see the constuctors for more help)
    618  * @returns a Pointer to the Resource if found, NULL otherwise.
    619 */
    620 Resource* ResourceManager::locateResourceByInfo(const std::string& fileName, ResourceType type,
    621     const MultiType& param0, const MultiType& param1, const MultiType& param2) const
    622 {
    623   std::vector<Resource*>::const_iterator resource;
    624   for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
    625   {
    626     if ((*resource)->type == type && fileName == (*resource)->name)
    627     {
    628       bool match = false;
    629       switch (type)
    630       {
    631 #ifndef NO_MODEL
    632         case PRIM:
    633         case OBJ:
    634           if (param0 == MT_NULL)
    635           {
    636             if ((*resource)->param[0] == 1.0f)
    637               match = true;
    638           }
    639           else if ((*resource)->param[0] == param0.getFloat())
    640             match = true;
    641           break;
    642         case MD2:
    643           if (param0 == MT_NULL && ((*resource)->param[0] == "") && param1 == MT_NULL && ((*resource)->param[0] == 1.0f))
    644               match = true;
    645           else if ((*resource)->param[0] == ((MultiType)param0).getString() && (*resource)->param[1] == ((MultiType)param1).getFloat())
    646             match = true;
    647           break;
    648         case MD3:
    649           if (param0 == MT_NULL && ((*resource)->param[0] == "") && param1 == MT_NULL && ((*resource)->param[0] == 1.0f))
    650             match = true;
    651           else if ((*resource)->param[0] == ((MultiType)param0).getString() && (*resource)->param[1] == ((MultiType)param1).getFloat())
    652             match = true;
    653           break;
    654         case MD3_CONFIG:
    655           if (param0 == MT_NULL && ((*resource)->param[0] == ""))
    656             match = true;
    657           else if ((*resource)->param[0] == ((MultiType)param0).getString())
    658             match = true;
    659           break;
    660 
    661 #endif /* NO_MODEL */
    662 #ifndef NO_TEXT
    663         case TTF:
    664           if (param0 == MT_NULL)
    665           {
    666             if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
    667               match = true;
    668           }
    669           else if ((*resource)->param[0] == param0.getInt())
    670             match = true;
    671           break;
    672 #endif /* NO_TEXT */
    673 #ifndef NO_SHADERS
    674         case SHADER:
    675           if (param0 == MT_NULL)
    676           {
    677             if ((*resource)->param[0] == "")
    678               match = true;
    679           }
    680           else if ((*resource)->param[0] == ((MultiType)param0).getString())
    681             match = true;
    682           break;
    683 #endif /* NO_SHADERS */
    684 #ifndef NO_TEXTURES
    685         case IMAGE:
    686           if (param0 == MT_NULL)
    687           {
    688             if ((*resource)->param[0] == GL_TEXTURE_2D)
    689               match = true;
    690           }
    691           else if ((*resource)->param[0] ==  param0.getInt())
    692             match = true;
    693           break;
    694 #endif /* NO_TEXTURES */
    695         default:
    696           match = true;
    697           break;
    698       }
    699       if (match)
    700       {
    701         return (*resource);
    702       }
    703     }
    704   }
    705   return NULL;
    706 }
    707 
    708 /**
    709  * @brief Searches for a Resource by Pointer
    710  * @param pointer the Pointer to search for
    711  * @returns a Pointer to the Resource if found, NULL otherwise.
    712  */
    713 Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
    714 {
    715   //  Resource* enumRes = resourceList->enumerate();
    716   std::vector<Resource*>::const_iterator resource;
    717   for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
    718     if (pointer == (*resource)->pointer)
    719       return (*resource);
    720   return NULL;
    721 }
    722 
    723 std::string ResourceManager::toResourcableString(unsigned int i)
    724 {
    725 /*  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
    726   len += this->resourceList[i]->name.size();
    727   if (this->resourceList[i]->param[0].getCString()) len += strlen(this->resourceList[i]->param[0].getCString()) +1;
    728   if (this->resourceList[i]->param[1].getCString()) len += strlen(this->resourceList[i]->param[1].getCString()) +1;
    729   if (this->resourceList[i]->param[2].getCString()) len += strlen(this->resourceList[i]->param[2].getCString()) +1;
    730   len += 10;
    731   std::string tmp = new char[len];
    732   tmp[0] = '\0';
    733   strcat(tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
    734   strcat(tmp,",");
    735   strcat (tmp, this->resourceList[i]->name);
    736   if (this->resourceList[i]->param[0].getCString() && this->resourceList[i]->param[0].getCString() != '\0')
    737   {
    738     strcat(tmp,",");
    739     strcat( tmp, this->resourceList[i]->param[0].getCString());
    740   }
    741   if (this->resourceList[i]->param[1].getCString() && this->resourceList[i]->param[1].getCString() != '\0')
    742   {
    743     strcat(tmp,",");
    744     strcat( tmp, this->resourceList[i]->param[1].getCString());
    745   }
    746   if (this->resourceList[i]->param[2].getCString() && this->resourceList[i]->param[2].getCString() != '\0')
    747   {
    748     strcat(tmp,",");
    749     strcat( tmp, this->resourceList[i]->param[2].getCString());
    750   }
    751   return tmp;*/
    752   return "";
    753 }
    754 
    755 /**
    756  * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
    757  * @param resourceableString the String to cache the resource from.
    758  */
    759 bool ResourceManager::fromResourceableString(const std::string& resourceableString)
    760 {
    761 /*  SubString splits(resourceableString, ',');
    762   splits.debug();
    763   if (splits.getCount() == 2)
    764     this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
    765                 RP_LEVEL);
    766   else if (splits.getCount() == 3)
    767     return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
    768                 RP_LEVEL, splits[2]);
    769   else if (splits.getCount() == 4)
    770     return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
    771                 RP_LEVEL, splits[2], splits[3]);
    772   else if (splits.getCount() == 5)
    773     return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
    774                 RP_LEVEL, splits[2], splits[3], splits[4]);*/
    775   return false;
    776 }
    777 
    778 
    779 /**
    780  * @param fileName the Name of the File to check
    781  * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
    782  * !!IMPORTANT: this has to be deleted from the outside!!
    783 */
    784 std::string ResourceManager::getFullName(const std::string& fileName)
    785 {
    786   if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
    787     return "";
    788 
    789   std::string retName = ResourceManager::getInstance()->getDataDir() +fileName;
    790   if (File(retName).isFile() || File(retName).isDirectory())
    791     return retName;
    792   else
    793     return "";
    794 }
    795 
    796 
    797 /**
    798  * @brief checks wether a file is in the DataDir.
    799  * @param fileName the File to check if it is in the Data-Dir structure.
    800  * @returns true if the file exists, false otherwise
    801  */
    802 bool ResourceManager::isInDataDir(const std::string& fileName)
    803 {
    804   if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
    805     return false;
    806 
    807   bool retVal = false;
    808   std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName;
    809 
    810   if (File(checkFile).exists())
    811     retVal = true;
    812   else
    813     retVal = false;
    814   return retVal;
    815 }
    816 
    817 
    818 /**
    819  * @brief outputs debug information about the ResourceManager
    820  */
    821 void ResourceManager::debug() const
    822 {
    823   PRINT(0)("=RM===================================\n");
    824   PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
    825   PRINT(0)("======================================\n");
    826   // if it is not initialized
    827   PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
    828   PRINT(0)(" Data-Directory is: %s\n", this->dataDir.c_str());
    829   PRINT(0)(" List of Image-Directories: ");
    830   std::vector<std::string>::const_iterator imageDir;
    831   for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
    832     PRINT(0)("%s ", (*imageDir).c_str());
    833   PRINT(0)("\n");
    834 
    835   PRINT(0)("List of all stored Resources:\n");
    836   std::vector<Resource*>::const_iterator resource;
    837   for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
    838 
    839   {
    840     PRINT(0)("-----------------------------------------\n");
    841     PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name.c_str(), (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
    842 
    843     PRINT(0)("gets deleted at ");
    844     switch((*resource)->prio)
    845     {
    846       default:
    847       case RP_NO:
    848         PRINT(0)("first posibility (0)\n");
    849         break;
    850       case RP_LEVEL:
    851         PRINT(0)("the end of the Level (1)\n");
    852         break;
    853       case RP_CAMPAIGN:
    854         PRINT(0)("the end of the campaign (2)\n");
    855         break;
    856       case RP_GAME:
    857         PRINT(0)("when leaving the game (3)\n");
    858         break;
    859     }
    860   }
    861 
    862 
    863 
    864   PRINT(0)("==================================RM==\n");
    865 }
    866 
    867 
    868 /**
    869  * @brief converts a ResourceType into the corresponding String
    870  * @param type the ResourceType to translate
    871  * @returns the converted String.
    872  */
    873 const char* ResourceManager::ResourceTypeToChar(ResourceType type)
    874 {
    875   return ResourceManager::resourceNames[type];
    876 }
    877 
    878 /**
    879  * @brief converts a String into a ResourceType (good for loading)
    880  * @param resourceType the name of the Type
    881  * @returns the Number of the Type, or 0 (defautl) if not found.
    882  */
    883 ResourceType ResourceManager::stringToResourceType(const std::string& resourceType)
    884 {
    885   for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
    886     if (resourceType == ResourceManager::resourceNames[i])
    887       return (ResourceType)i;
    888   return (ResourceType)0;
    889 }
    890 
    891 /**
    892  * The Names of the ResourceTypes
    893  */
    894 const char* ResourceManager::resourceNames[] =
    895   {
    896 #ifndef NO_MODEL
    897     "ObjectModel",
    898     "PrimitiveModel",
    899     "MD2-Data",
    900     "MD3-Data",
    901     "MD3-Config"
    902 #endif
    903 #ifndef NO_TEXT
    904     "Font",
    905 #endif
    906 #ifndef NO_AUDIO
    907     "Wav",
    908     "mp3",
    909     "ogg",
    910 #endif
    911 #ifndef NO_TEXTURES
    912     "Texture",
    913 #endif
    914 #ifndef NO_SHADERS
    915     "Shader",
    916 #endif
    917 
    918   };
  • trunk/src/lib/util/loading/resource_manager.h

    r8724 r9869  
    11/*!
    22 * @file resource_manager.h
    3   *  The Resource Manager checks if a file/resource is loaded.
    4 
    5     If a file/resource was already loaded the resourceManager will
    6     return a void pointer to the desired resource.
    7     Otherwise it will instruct the coresponding resource-loader to load,
    8     and receive a pointer to it.
    9 
    10     it is possible to compile the resource Manager without some modules by
    11     just adding the compile flag -D....
    12     (NO_MODEL)
    13     (NO_AUDIO)
    14     (NO_TEXT)
    15     (NO_TEXTURES)
    16     (NO_SHADERS)
    17 */
     3 */
    184
    195#ifndef _RESOURCE_MANAGER_H
    206#define _RESOURCE_MANAGER_H
    217
    22 #include "base_object.h"
     8#include "resource.h"
    239#include "filesys/file.h"
     10#include "filesys/directory.h"
    2411
    25 #include "multi_type.h"
    26 #include <vector>
     12namespace Resources
     13{
    2714
    28 //! An eumerator for different fileTypes the resourceManager supports
    29 typedef enum ResourceType
    30 {
    31 #ifndef NO_MODEL
    32   OBJ,                  //!< loading .obj file
    33   PRIM,                 //!< loading primitive model
    34   MD2,                  //!< loading md2-file
    35   MD3,                  //!< loading md3-file
    36   MD3_CONFIG,           //!< the md3 config file
    37 #endif /* NO_MODEL */
    38 #ifndef NO_TEXT
    39   TTF,                  //!< loading a TrueTypeFont
    40 #endif /* NO_TEXT */
    41 #ifndef NO_AUDIO
    42   WAV,                  //!< loading wav
    43   MP3,                  //!< loading mp3
    44   OGG,                  //!< loading ogg
    45 #endif /* NO_AUDIO */
    46 #ifndef NO_TEXTURES
    47   IMAGE,                //!< loading an image
    48 #endif /* NO_TEXTURES */
    49 #ifndef NO_SHADERS
    50   SHADER,               //!< openGL-shader program
    51 #endif /* NO_SHADERS */
    52   RESOURCE_TYPE_SIZE
    53 };
     15  class ResourceManager : public BaseObject
     16  {
     17    ObjectListDeclaration(ResourceManager);
     18  public:
     19    ///////////////////////
     20    //// INSTANZIATION ////
     21    /** @returns a Pointer to the only object of this Class */
     22    inline static ResourceManager* getInstance() { if (!_singletonRef) _singletonRef = new ResourceManager();  return _singletonRef; };
     23    /** @brief deletes the Instance if it exists. */
     24    inline static void deleteInstance() { if (_singletonRef) delete _singletonRef; };
    5425
    55 //! An enumerator for different UNLOAD-types.
    56 /**
    57    RP_NO:        will be unloaded on request
    58    RP_LEVEL:     will be unloaded at the end of a Level
    59    RP_CAMPAIGN:  will be unloaded at the end of a Campaign
    60    RP_GAME:      will be unloaded at the end of the whole Game (when closing orxonox)
    61 */
    62 typedef enum ResourcePriority
    63 {
    64   RP_NO        =   0,
    65   RP_LEVEL     =   1,
    66   RP_CAMPAIGN  =   2,
    67   RP_GAME      =   4
    68 };
     26    ////////////////////////
     27    //// RESOURCE PATHS ////
     28    void setMainGlobalPath(const Directory& directory);
     29    void addGlobalPath(const Directory& directory);
    6930
    70 //! A Struct that keeps track about a resource its name its Type, and so on
    71 struct Resource
    72 {
    73   BaseObject*       pointer;           //!< Pointer to the Resource.
    74   unsigned int      count;             //!< How many times this Resource has been loaded.
     31    bool addResourcePath(const std::string& resourceName, const std::string& pathName);
     32    bool addResourceSubPath(const std::string& resourceName, const std::string& pathName);
     33    void registerType(Resources::Type* type);
     34    void unregisterType(Resources::Type* type);
    7535
    76   std::string       name;              //!< Name of the Resource.
    77   ResourceType      type;              //!< ResourceType of this Resource.
    78   ResourcePriority  prio;              //!< The Priority of this resource. (This will only be increased)
     36    /** @returns the main global search Path */
     37    const Directory& mainGlobalPath() const { return _mainGlobalPath; };
     38    /** @returns all global paths without mainGlobalPath */
     39    const std::vector<Directory>& globalPaths() const { return _globalPaths; };
    7940
    80   MultiType         param[3];          //!< The Parameters given to this Resource.
    81 };
     41    ////////////////////
     42    //// KEEPLEVELS ////
     43    unsigned int addKeepLevelName(const std::string& keepLevelName);
     44    unsigned int getKeepLevelID(const std::string& keepLevelName) const;
     45    const std::string& getKeepLevelName(unsigned int keepLevelID) const;
     46    void setDefaultKeepLevel(const KeepLevel& keepLevel) { this->_defaultKeepLevel = keepLevel; };
     47    const KeepLevel& defaultKeepLevel() const { return this->_defaultKeepLevel; };
    8248
     49    //////////////////////////
     50    //// GENERAL QUERIES ////
     51    /** @returns the Types of Resources */
     52    const std::vector<Resources::Type*> resourceTypes() const { return _resourceTypes; };
    8353
    84 //! The ResourceManager is a class, that decides if a file/resource should be loaded
    85 /**
    86  * If a file/resource was already loaded the resourceManager will
    87  * return a pointer to the desired resource.
    88  * Otherwise it will instruct the corresponding resource-loader to load,
    89  * and receive the pointer to it.
    90  *
    91  * It does it by looking, if a desired file has already been loaded.
    92  * There is also the possibility to check for some variables
    93  */
    94 class ResourceManager : public BaseObject
    95 {
    96  public:
    97   virtual ~ResourceManager();
    98   /** @returns a Pointer to the only object of this Class */
    99   inline static ResourceManager* getInstance() { if (!singletonRef) singletonRef = new ResourceManager();  return singletonRef; };
     54    bool checkFileInMainPath(const File& fileInside);
     55    std::string prependAbsoluteMainPath(const std::string& fileName);
    10056
    101   bool setDataDir(const std::string& dataDir);
    102   /** @returns the Name of the data directory */
    103   inline const std::string& getDataDir() const { return this->dataDir; };
     57    ////////////////////////////////////////
     58    //// RESOURCE LOADING AND UNLOADING ////
     59    void loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString, const KeepLevel& keepLevel = KeepLevel());
     60    void loadFromLoadStringHACK(const std::string& resourceTypeName, const std::string& loadString) { this->loadFromLoadString(resourceTypeName, loadString); };
    10461
     62    void unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel);
     63    void unloadAllBelowKeepLevelINT(unsigned int level) { unloadAllBelowKeepLevel(level); };
    10564
    106   bool tryDataDir(const std::string& dataDir);
    107   bool verifyDataDir(const std::string& fileInside);
    108   bool addImageDir(const std::string& imageDir);
     65    ///////////////
     66    //// DEBUG ////
     67    void debug() const;
    10968
    110   bool cache(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO,
    111              const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType());
    112   BaseObject* copy(BaseObject* resourcePointer);
     69  private:
     70    ResourceManager();
     71    virtual ~ResourceManager();
    11372
    114   BaseObject* load(const std::string& fileName, ResourcePriority prio = RP_NO,
    115                    const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType());
    116   BaseObject* load(const std::string& fileName, ResourceType type, ResourcePriority prio = RP_NO,
    117                    const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType());
    118   bool unload(BaseObject* pointer, ResourcePriority prio = RP_NO);
    119   bool unload(Resource* resource, ResourcePriority = RP_NO);
    120   bool unloadAllByPriority(ResourcePriority prio);
     73  private:
     74    static ResourceManager*            _singletonRef;       //!< singleton Reference
    12175
    122   Resource* locateResourceByInfo(const std::string& fileName, ResourceType type,
    123                                  const MultiType& param0 = MultiType(), const MultiType& param1 = MultiType(), const MultiType& param2 = MultiType()) const;
    124   Resource* locateResourceByPointer(const void* pointer) const;
     76    Directory                          _mainGlobalPath;     //!< The main include directory (default at "./")
     77    std::vector<Directory>             _globalPaths;        //!< Additional Global include directories.
    12578
    126   std::string toResourcableString(unsigned int i);
    127   bool fromResourceableString(const std::string& resourceableString);
    128   /** @returns the Count of Resources the ResourceManager handles */
    129   unsigned int resourceCount() const { return this->resourceList.size(); }
     79    std::vector<Resources::Type*>      _resourceTypes;      //!< A Vector of all the stored ResourceTypes @see Resources::Type
    13080
    131   void debug() const;
     81    std::vector<std::string>           _keepLevelNames;     //!< Names of KeepLevels @see Resources::KeepLevel
     82    KeepLevel                          _defaultKeepLevel;   //!< The default KeepLevel.
     83  };
    13284
    133 
    134   // utility functions for handling files in and around the data-directory
    135   static std::string getFullName(const std::string& fileName);
    136   static bool isInDataDir(const std::string& fileName);
    137 
    138   static const char* ResourceTypeToChar(ResourceType type);
    139   static ResourceType stringToResourceType(const std::string& resourceType);
    140 
    141  private:
    142   ResourceManager();
    143   Resource* loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio,
    144                          const MultiType& param0, const MultiType& param1, const MultiType& param2);
    145 
    146  private:
    147   static ResourceManager*    singletonRef;       //!< singleton Reference
    148 
    149   std::string                dataDir;            //!< The Data Directory, where all relevant Data is stored.
    150   std::vector<std::string>   imageDirs;          //!< A list of directories in which images are stored.
    151 
    152   std::vector<Resource*>     resourceList;       //!< The List of Resources, that has already been loaded.
    153 
    154   static const char*         resourceNames[RESOURCE_TYPE_SIZE];
    155 };
     85}
    15686
    15787#endif /* _RESOURCE_MANAGER_H */
Note: See TracChangeset for help on using the changeset viewer.