Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

ResourceManager now cleans up too

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