Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/resource.cc @ 9800

Last change on this file since 9800 was 9800, checked in by bensch, 18 years ago

namespaces

File size: 5.0 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
17
18#include "resource.h"
19#include "new_resource_manager.h"
20
21#include "debug.h"
22
23
24namespace Resources
25{
26  ObjectListDefinition(NewResource);
27
28
29  /**
30   * standard constructor
31  */
32  NewResource::NewResource (Type* type)
33      : _pointer(NULL), _type(type)
34  {
35    this->registerObject(this, NewResource::_objectList);
36  }
37
38  /**
39   * standard deconstructor
40   */
41  NewResource::~NewResource ()
42  {
43    // delete what has to be deleted here
44  }
45
46
47  std::string NewResource::locateFile(const std::string& fileName) const
48  {
49    if ((NewResourceManager::getInstance()->mainGlobalPath() + File(fileName)).exists() )
50      return (NewResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
51
52    std::string locatedFile;
53    locatedFile = locateFileInSubDir(NewResourceManager::getInstance()->mainGlobalPath(), fileName);
54    if (!locatedFile.empty())
55    {
56      printf("FILE found %s\n", locatedFile.c_str());
57      return locatedFile;
58    }
59
60    if (File(fileName).exists())
61      return fileName;
62
63    return (NewResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name();
64  }
65
66  /**
67   * @brief tests in all the SubDirectories defined in Resource under Directory if the fileName exists.
68   * @param directory the directory to in what to search for all subdirectories for.
69   * @param fileName the Name of the File to query for
70   * @return true on success.
71   */
72  std::string NewResource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const
73  {
74    std::vector<Directory>::const_iterator it;
75    for (it = this->_type->resourceSubPaths().begin(); it != this->_type->resourceSubPaths().end(); ++it)
76    {
77      Directory dir = directory + (*it);
78      File file = dir + File(fileName);
79      printf("Testing %s (from %s in directory %s)\n", file.name().c_str(), fileName.c_str(), dir.name().c_str());
80      if ((dir+ File(fileName)).exists())
81        return (dir+File(fileName)).name();
82    }
83    return "";
84  }
85
86
87
88  StorePointer* NewResource::acquireResource(const std::string& loadString)
89  {
90    //const Type* const type = _resourceTypes[this->_type->id()];
91
92    for (unsigned int i = 0; i < _type->storedResources().size(); ++i)
93    {
94      if (_type->storedResources()[i]->loadString() == loadString)
95        return _type->storedResources()[i];
96    }
97
98    return NULL;
99  }
100
101
102  void NewResource::addResource(StorePointer* pointer)
103  {
104    this->_type->addResource(pointer);
105  }
106
107
108
109
110
111
112  ///////////////////
113  //// KEEPLEVEL ////
114  ///////////////////
115  KeepLevel::KeepLevel(const std::string& keepLevelName)
116  {
117    this->_keepLevel = NewResourceManager::getInstance()->getKeepLevelID(keepLevelName);
118  }
119
120  const std::string& KeepLevel::name() const
121  {
122    return NewResourceManager::getInstance()->getKeepLevelName(this->_keepLevel);
123  }
124
125
126
127  ///////////////////////
128  //// STORE POINTER ////
129  ///////////////////////
130  StorePointer::StorePointer(const std::string& loadString, const KeepLevel& keeplevel)
131      : _loadString(loadString), _keepLevel(keeplevel)
132  {}
133
134
135
136
137  //////////////
138  //// TYPE ////
139  //////////////
140  Type::Type(const std::string& typeName)
141      : _id(-1), _typeName(typeName)
142  {
143    NewResourceManager::getInstance()->registerType(this);
144    PRINTF(4)("Created ResourceType '%s'\n", typeName.c_str());
145  }
146
147  Type::~Type()
148  {
149    NewResourceManager::getInstance()->unregisterType(this);
150  }
151
152  void Type::addResource(StorePointer* resource)
153  {
154    this->_storedResources.push_back(resource);
155
156  }
157
158  bool Type::addResourcePath(const std::string& path)
159  {
160    std::vector<Directory>::const_iterator it;
161    for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it)
162      if ((*it) == path)
163        return false;
164    this->_resourcePaths.push_back(path);
165    return true;
166
167  }
168
169  bool Type::addResourceSubPath(const std::string& subPath)
170  {
171    std::vector<Directory>::const_iterator it;
172    for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it)
173      if ((*it) == subPath)
174        return false;
175    this->_resourceSubPaths.push_back(subPath);
176    return true;
177  }
178
179
180  void Type::setID(int id)
181  {
182    this->_id = id;
183  }
184
185
186  void Type::debug() const
187  {
188    PRINT(0)(" ResourceType '%s' with ID %d stores %d Resources\n", this->_typeName.c_str(), this->_id, this->_storedResources.size());
189    PRINT(0)("  Paths:\n");
190    for (unsigned int i = 0; i < this->_resourcePaths.size(); ++i)
191      PRINT(0)("    %s\n", this->_resourcePaths[i].name().c_str());
192    PRINT(0)("  Sub-Paths:");
193    for (unsigned int i = 0; i < this->_resourceSubPaths.size(); ++i)
194      PRINT(0)(" '%s'", this->_resourceSubPaths[i].name().c_str());
195    PRINT(0)("\n");
196
197  }
198}
Note: See TracBrowser for help on using the repository browser.