Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added new_resource_manager

File size: 6.2 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 "filesys/file.h"
20#include "debug.h"
21
22ObjectListDefinition(NewResource);
23std::vector<NewResource::Type*>    NewResource::_resourceTypes;
24
25//! GLOBALS
26Directory NewResource::_mainGlobalPath;
27std::vector<Directory>        NewResource::_globalPaths;
28
29
30/**
31 * standard constructor
32*/
33NewResource::NewResource (NewResource::Type* type)
34    : _pointer(NULL), _type(type)
35{
36  this->registerObject(this, NewResource::_objectList);
37}
38
39/**
40 * standard deconstructor
41 */
42NewResource::~NewResource ()
43{
44  // delete what has to be deleted here
45}
46
47
48std::string NewResource::locateFile(const std::string& fileName) const
49{
50  if (File(fileName).exists())
51    return fileName;
52  else if ((NewResource::_mainGlobalPath + File(fileName)).exists() )
53    return (NewResource::_mainGlobalPath + File(fileName)).name();
54
55  printf("LOCATED %s\n", locateFileInSubDir(Directory("/home/bensch/svn/orxonox/data/"), fileName).c_str());
56
57  return std::string("/home/bensch/svn/orxonox/data/") + fileName;
58}
59
60/**
61 * @brief tests in all the SubDirectories defined in Resource under Directory if the fileName exists.
62 * @param directory the directory to in what to search for all subdirectories for.
63 * @param fileName the Name of the File to query for
64 * @return true on success.
65 */
66std::string NewResource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const
67{
68  std::vector<Directory>::const_iterator it;
69  for (it = this->_type->resourceSubPaths().begin(); it != this->_type->resourceSubPaths().end(); ++it)
70  {
71    Directory dir = directory + (*it);
72    File file = dir + File(fileName);
73    printf("Testing %s (from %s in directory %s)\n", file.name().c_str(), fileName.c_str(), dir.name().c_str());
74    if ((dir+ File(fileName)).exists())
75      return (dir+File(fileName)).name();
76  }
77  return "";
78}
79
80
81
82NewResource::StorePointer* NewResource::acquireResource(const std::string& loadString)
83{
84  //const NewResource::Type* const type = NewResource::_resourceTypes[this->_type->id()];
85
86  for (unsigned int i = 0; i < _type->storedResources().size(); ++i)
87  {
88    if (_type->storedResources()[i]->loadString() == loadString)
89      return _type->storedResources()[i];
90  }
91
92  return NULL;
93}
94
95void NewResource::setMainGlobalPath(const Directory& directory)
96{
97  NewResource::_mainGlobalPath = directory;
98  NewResource::_mainGlobalPath.open();
99}
100
101void NewResource::addGlobalPath(const Directory& directory)
102{
103  std::vector<Directory>::const_iterator it = std::find(NewResource::_globalPaths.begin(), NewResource::_globalPaths.end(), directory);
104  if (it == NewResource::_globalPaths.end())
105    NewResource::_globalPaths.push_back(directory);
106}
107
108
109
110void NewResource::addResource(NewResource::StorePointer* pointer)
111{
112  this->_type->addResource(pointer);
113}
114
115
116void NewResource::registerType(NewResource::Type* type)
117{
118  NewResource::debug();
119  if(type->id() == -1)
120  {
121    NewResource::_resourceTypes.push_back(type);
122    type->setID(NewResource::_resourceTypes.size()-1);
123  }
124  NewResource::debug();
125}
126
127bool NewResource::addResourcePath(const std::string& resourceName, const std::string& pathName)
128{
129  std::vector<NewResource::Type*>::iterator it;
130  for (it = NewResource::_resourceTypes.begin(); it != NewResource::_resourceTypes.end(); ++it)
131    if (*(*it) == resourceName)
132      return (*it)->addResourcePath(pathName);
133  PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
134  return false;
135}
136
137bool NewResource::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
138{
139  std::vector<NewResource::Type*>::iterator it;
140  for (it = NewResource::_resourceTypes.begin(); it != NewResource::_resourceTypes.end(); ++it)
141    if (*(*it) == resourceName)
142      return (*it)->addResourceSubPath(pathName);
143  PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
144  return false;
145}
146
147
148
149void NewResource::debug()
150{
151  PRINT(0)("==================================\n");
152  PRINT(0)("DEBUG output for all Resources\n");
153  PRINT(0)("==================================\n");
154
155  PRINT(0)("Listing %d Types: \n", NewResource::_resourceTypes.size());
156  std::vector<NewResource::Type*>::iterator it;
157  for (it = NewResource::_resourceTypes.begin(); it != NewResource::_resourceTypes.end(); ++it)
158    PRINT(0)("ResourceType '%s'\n", (*it)->storedClassID().name().c_str());
159
160  PRINT(0)("==================================\n");
161
162}
163
164
165
166
167
168std::vector<std::string> NewResource::KeepLevel::_keepLevelNames;
169void NewResource::KeepLevel::defineKeepLevelName(unsigned int level, const std::string& name)
170{
171  if (_keepLevelNames.size() <= level)
172    _keepLevelNames.resize(level+1);
173  _keepLevelNames[level] = name;
174}
175
176
177NewResource::Type::Type(const ClassID& classID)
178    : _id(-1), _classID(classID)
179{
180  NewResource::registerType(this);
181}
182
183void NewResource::Type::addResource(NewResource::StorePointer* resource)
184{
185  this->_storedResources.push_back(resource);
186
187}
188
189bool NewResource::Type::addResourcePath(const std::string& path)
190{
191  std::vector<Directory>::const_iterator it;
192  for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it)
193    if ((*it) == path)
194      return false;
195  this->_resourcePaths.push_back(path);
196  return true;
197
198}
199
200bool NewResource::Type::addResourceSubPath(const std::string& subPath)
201{
202  std::vector<Directory>::const_iterator it;
203  for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it)
204    if ((*it) == subPath)
205      return false;
206  this->_resourceSubPaths.push_back(subPath);
207  return true;
208}
209
210
211
212NewResource::StorePointer::StorePointer(const std::string& loadString, const NewResource::KeepLevel& keeplevel)
213    : _loadString(loadString), _keepLevel(keeplevel)
214{}
215
216
217void NewResource::Type::setID(int id)
218{
219  this->_id = id;
220}
Note: See TracBrowser for help on using the repository browser.